99#include <stdio.h>
1010
1111#define MAX_RECEIVED_LEN 1024
12+ #define SEQUENCE_NUMBER_DUMMY 999
1213
1314void payload_task_init (slate_t * slate )
1415{
@@ -30,63 +31,139 @@ void payload_task_init(slate_t *slate)
3031 // NOTE: Turning on payload is handled by command_parser
3132}
3233
34+ bool send_payload_exec (slate_t * slate , char msg [], uint16_t seq_num )
35+ {
36+ LOG_INFO ("Executing Payload Command: %s" , msg );
37+ // First attempt to execute the command but do not throw it away
38+ // yet.
39+ payload_write_error_code exec_successful =
40+ payload_uart_write_packet (slate , msg , sizeof (msg ), seq_num );
41+
42+ if (exec_successful == SUCCESSFUL_WRITE )
43+ {
44+ return true;
45+ }
46+ else if (exec_successful == PACKET_TOO_BIG )
47+ {
48+ LOG_DEBUG ("Packet exceeds 4096 bytes..." );
49+ return false;
50+ }
51+ else if (exec_successful == SYN_UNSUCCESSFUL )
52+ {
53+ LOG_DEBUG ("PiCubed was unable to sync with the Payload..." );
54+ return false;
55+ }
56+ else if (exec_successful == UART_WRITE_TIMEDOUT )
57+ {
58+ LOG_DEBUG ("The transmission took too long and the write timed "
59+ "out..." );
60+ return false;
61+ }
62+ else if (exec_successful == HEADER_UNACKNOWLEDGED )
63+ {
64+ LOG_DEBUG ("Payload did not acknowledge the header..." );
65+ return false;
66+ }
67+ else if (exec_successful == FINAL_WRITE_UNSUCCESSFUL )
68+ {
69+ LOG_DEBUG ("Final packet transmission timed out..." );
70+ return false;
71+ }
72+ else
73+ {
74+ LOG_DEBUG ("Payload command execution failed, retrying..." );
75+ // If the command was not successful, we will not remove it
76+ // from the queue and will try again next time.
77+ return false;
78+ }
79+ }
80+
81+ bool ping_command (slate_t * slate )
82+ {
83+ char packet [] = "[\"ping\", [], {}]" ;
84+ bool write_success =
85+ send_payload_exec (slate , packet , SEQUENCE_NUMBER_DUMMY );
86+ if (!write_success )
87+ {
88+ LOG_INFO ("Writing packet to payload was not successful!" );
89+ return false;
90+ }
91+
92+ char received [MAX_RECEIVED_LEN ];
93+ uint16_t received_len = payload_uart_read_packet (slate , received );
94+ if (received_len == 0 )
95+ {
96+ LOG_INFO ("ACK was not received!" );
97+ return false;
98+ }
99+ else
100+ {
101+ LOG_INFO ("ACK received!" );
102+ LOG_INFO ("ACK:" );
103+ for (uint16_t i = 0 ; i < received_len ; i ++ )
104+ {
105+ printf ("%c" , received [i ]);
106+ }
107+ printf ("\n" );
108+ return true;
109+ }
110+ }
111+
112+ bool send_heartbeat (slate_t * slate )
113+ {
114+ if (slate -> is_payload_on )
115+ {
116+ uint64_t timeDelta =
117+ absolute_time_diff_us (slate -> payload_most_recent_ping_time ,
118+ get_absolute_time ()) /
119+ 1000 ;
120+ if (timeDelta >= PAYLOAD_HEARTBEAT_TIMEOUT_MS )
121+ {
122+ payload_restart (slate ); // this resets the most_recent_ping_time.
123+ // still return false; don't let the payload send commands (since we
124+ // aren't getting responses.)
125+ return false;
126+ }
127+ else
128+ {
129+ bool response_received = ping_command (slate );
130+ if (response_received )
131+ {
132+ slate -> payload_most_recent_ping_time = get_absolute_time ();
133+ return true;
134+ // operational! we can send commands now.
135+ }
136+ // if we don't get a response back, don't update the time.
137+ return false;
138+ }
139+ }
140+ return false;
141+ }
142+
33143bool try_execute_payload_command (slate_t * slate )
34144{
35145 if (!queue_is_empty (& slate -> payload_command_data ))
36146 {
37147 PAYLOAD_COMMAND_DATA payload_command ;
38148 if (queue_try_peek (& slate -> payload_command_data , & payload_command ))
39149 {
40- LOG_INFO ("Executing Payload Command: %s" ,
41- payload_command .serialized_command );
42- // First attempt to execute the command but do not throw it away
43- // yet.
44- payload_write_error_code exec_successful =
45- payload_uart_write_packet (
46- slate , payload_command .serialized_command ,
47- sizeof (payload_command .serialized_command ),
48- payload_command .seq_num );
150+ bool writeSuccess =
151+ send_payload_exec (slate , payload_command .serialized_command ,
152+ payload_command .seq_num );
153+ // removeFromQueue is true if we succesfully sent the packet to the
154+ // payload
155+
49156 // If the command was successful, remove it from the queue.
50157 // Alternatively, if we have already retried the command up to
51158 // a maximum number of times, remove it from the queue.
52- if (exec_successful == SUCCESSFUL_WRITE ||
53- RETRY_COUNT >= MAX_PAYLOAD_RETRY_COUNT )
159+ if (writeSuccess || RETRY_COUNT > MAX_PAYLOAD_RETRY_COUNT )
54160 {
55161 // Return success when the command is removed.
56162 return queue_try_remove (& slate -> payload_command_data ,
57163 & payload_command );
58164 }
59- else if (exec_successful == PACKET_TOO_BIG )
60- {
61- LOG_DEBUG ("Packet exceeds 4096 bytes..." );
62- return false;
63- }
64- else if (exec_successful == SYN_UNSUCCESSFUL )
65- {
66- LOG_DEBUG ("PiCubed was unable to sync with the Payload..." );
67- return false;
68- }
69- else if (exec_successful == UART_WRITE_TIMEDOUT )
70- {
71- LOG_DEBUG ("The transmission took too long and the write timed "
72- "out..." );
73- return false;
74- }
75- else if (exec_successful == HEADER_UNACKNOWLEDGED )
76- {
77- LOG_DEBUG ("Payload did not acknowledge the header..." );
78- return false;
79- }
80- else if (exec_successful == FINAL_WRITE_UNSUCCESSFUL )
81- {
82- LOG_DEBUG ("Final packet transmission timed out..." );
83- return false;
84- }
85165 else
86166 {
87- LOG_DEBUG ("Payload command execution failed, retrying..." );
88- // If the command was not successful, we will not remove it
89- // from the queue and will try again next time.
90167 return false;
91168 }
92169 }
@@ -135,32 +212,6 @@ void beacon_down_command_test(slate_t *slate)
135212 }
136213}
137214
138- void ping_command_test (slate_t * slate )
139- {
140- char packet [] = "[\"ping\", [], {}]" ;
141- int len = sizeof (packet ) - 1 ;
142- payload_uart_write_packet (slate , packet , len , 999 );
143-
144- safe_sleep_ms (1000 );
145-
146- char received [MAX_RECEIVED_LEN ];
147- uint16_t received_len = payload_uart_read_packet (slate , received );
148- if (received_len == 0 )
149- {
150- LOG_INFO ("ACK was not received!" );
151- }
152- else
153- {
154- LOG_INFO ("ACK received!" );
155- LOG_INFO ("ACK:" );
156- for (uint16_t i = 0 ; i < received_len ; i ++ )
157- {
158- printf ("%c" , received [i ]);
159- }
160- printf ("\n" );
161- }
162- }
163-
164215/*** BRINGUP TESTS ***/
165216void power_on_off_payload_test (slate_t * slate )
166217{
@@ -258,13 +309,13 @@ void payload_task_dispatch(slate_t *slate)
258309 neopixel_set_color_rgb (PAYLOAD_TASK_COLOR );
259310 LOG_INFO ("Sending an Info Request Command to the RPI..." );
260311 // beacon_down_command_test(slate);
261- // ping_command_test (slate);
312+ // ping_command (slate);
262313
263314 payload_uart_write_on_test (slate );
264315
265316 return ;
266317
267- if (slate -> is_payload_on )
318+ if (send_heartbeat ( slate ) )
268319 {
269320 LOG_INFO ("Payload is ON, executing commands..." );
270321 // Attempts to execute k commands per dispatch.
0 commit comments