@@ -82,6 +82,8 @@ int rp2040_connect_onOTARequest(char const * ota_url)
8282{
8383 SFU::begin ();
8484
85+ remove (" /ota/UPDATE.BIN.LZSS" );
86+
8587 mbed_watchdog_reset ();
8688
8789 URI url (ota_url);
@@ -127,8 +129,13 @@ int rp2040_connect_onOTARequest(char const * ota_url)
127129
128130 /* Receive HTTP header. */
129131 String http_header;
130- for (bool is_header_complete = false ; client->connected () && !is_header_complete; )
132+ bool is_header_complete = false ,
133+ is_http_header_timeout = false ;
134+ for (unsigned long const start = millis (); !is_header_complete;)
131135 {
136+ is_http_header_timeout = (millis () - start) > (10 *1000 );
137+ if (is_http_header_timeout) break ;
138+
132139 if (client->available ())
133140 {
134141 mbed_watchdog_reset ();
@@ -141,6 +148,12 @@ int rp2040_connect_onOTARequest(char const * ota_url)
141148 }
142149 }
143150
151+ if (!is_header_complete) {
152+ fclose (file);
153+ DEBUG_ERROR (" %s: Error receiving HTTP header %s" , __FUNCTION__, is_http_header_timeout ? " (timeout)" :" " );
154+ return static_cast <int >(OTAError::RP2040_HttpHeaderError);
155+ }
156+
144157 /* Extract concent length from HTTP header. A typical entry looks like
145158 * "Content-Length: 123456"
146159 */
@@ -161,9 +174,13 @@ int rp2040_connect_onOTARequest(char const * ota_url)
161174 DEBUG_VERBOSE (" %s: Length of OTA binary according to HTTP header = %d bytes" , __FUNCTION__, content_length_val);
162175
163176 /* Receive as many bytes as are indicated by the HTTP header - or die trying. */
164- for (int bytes_received = 0 ;
165- (bytes_received < content_length_val) && client->connected ();)
177+ int bytes_received = 0 ;
178+ bool is_http_data_timeout = false ;
179+ for (unsigned long const start = millis (); bytes_received < content_length_val;)
166180 {
181+ is_http_data_timeout = (millis () - start) > (60 *1000 );
182+ if (is_http_data_timeout) break ;
183+
167184 if (client->available ())
168185 {
169186 mbed_watchdog_reset ();
@@ -181,10 +198,16 @@ int rp2040_connect_onOTARequest(char const * ota_url)
181198 }
182199 }
183200
201+ if (bytes_received != content_length_val) {
202+ fclose (file);
203+ DEBUG_ERROR (" %s: Error receiving HTTP data %s (%d bytes received, %d expected)" , __FUNCTION__, is_http_data_timeout ? " (timeout)" :" " , bytes_received, content_length_val);
204+ return static_cast <int >(OTAError::RP2040_HttpDataError);
205+ }
206+
184207 /* Determine length. */
185208 int const file_len = ftell (file);
186209 fclose (file);
187- DEBUG_DEBUG (" %s: %d bytes received" , __FUNCTION__, file_len);
210+ DEBUG_DEBUG (" %s: %d bytes received (%d expected) " , __FUNCTION__, file_len, content_length_val );
188211
189212 /* Perform the reset to reboot to SxU. */
190213 NVIC_SystemReset ();
0 commit comments