Skip to content

Commit 6b1e802

Browse files
committed
Replace verification if connected() with a time-out check. Otherwise it might happen that the connection has been terminated by the server, although not all available data has been read from the NINA.
1 parent 421eb9d commit 6b1e802

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/utility/ota/OTA-nano-rp2040.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

src/utility/ota/OTA.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ enum class OTAError : int
4040
DownloadFailed = 1,
4141
RP2040_UrlParseError = RP2040_OTA_ERROR_BASE - 0,
4242
RP2040_ServerConnectError = RP2040_OTA_ERROR_BASE - 1,
43-
RP2040_ErrorOpenUpdateFile = RP2040_OTA_ERROR_BASE - 2,
44-
RP2040_ErrorWriteUpdateFile = RP2040_OTA_ERROR_BASE - 3,
45-
RP2040_ErrorParseHttpHeader = RP2040_OTA_ERROR_BASE - 4,
43+
RP2040_HttpHeaderError = RP2040_OTA_ERROR_BASE - 2,
44+
RP2040_HttpDataError = RP2040_OTA_ERROR_BASE - 3,
45+
RP2040_ErrorOpenUpdateFile = RP2040_OTA_ERROR_BASE - 4,
46+
RP2040_ErrorWriteUpdateFile = RP2040_OTA_ERROR_BASE - 5,
47+
RP2040_ErrorParseHttpHeader = RP2040_OTA_ERROR_BASE - 6,
4648
};
4749

4850
/******************************************************************************

0 commit comments

Comments
 (0)