Skip to content

Commit 00e46c7

Browse files
committed
Download MKR WiFi 1010/Nano 33 IoT OTA update image via WiFiNiNa.downloadOTA API
1 parent e3256fe commit 00e46c7

File tree

2 files changed

+27
-47
lines changed

2 files changed

+27
-47
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ void ArduinoIoTCloudTCP::update()
173173
* 'update' method here in order to process incoming data and generally
174174
* to transition to the OTA logic update states.
175175
*/
176-
OTAError const err = _ota_logic.update();
177-
_ota_error = static_cast<int>(err);
176+
//OTAError const err = _ota_logic.update();
177+
//_ota_error = static_cast<int>(err);
178178
#endif /* OTA_ENABLED */
179179

180180
/* Run through the state machine. */
@@ -432,55 +432,34 @@ void ArduinoIoTCloudTCP::onOTARequest()
432432

433433
if (_ota_req)
434434
{
435-
WiFiSSLClient ota_client;
436-
if (!ota_client.connect("www.107-systems.org", 443)) {
437-
DBG_VERBOSE("ArduinoIoTCloudTCP::%s ota_client.connect failed", __FUNCTION__);
438-
return;
439-
}
440-
441-
/* Request binary via http-get */
442-
/*
443-
char get_msg[128];
444-
snprintf(get_msg, 128, "GET /ota/%s HTTP/1.1", _ota_url.c_str());
445-
DBG_VERBOSE("ArduinoIoTCloudTCP::%s \"%s\"", __FUNCTION__, get_msg);
435+
/* Clear the request flag. */
436+
_ota_req = false;
446437

447-
ota_client.println(get_msg);
448-
*/
449-
ota_client.println("GET /ota/wifi1010-sha256-remote.ota HTTP/1.1");
450-
ota_client.println("Host: www.107-systems.org");
451-
ota_client.println("Connection: close");
452-
ota_client.println();
438+
/* Status flag to prevent the reset from being executed
439+
* when HTTPS download is not supported.
440+
*/
441+
bool ota_download_success = false;
453442

454-
/* Read and parse the received data. */
455-
bool is_header_complete = false;
456-
size_t bytes_recv = 0;
457-
String http_header;
443+
#if OTA_STORAGE_SNU
444+
/* Just to be safe delete any remains from previous updates. */
445+
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS");
446+
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP");
458447

459-
for(; _ota_error == static_cast<int>(OTAError::None);)
448+
/* Trigger direct download to nina module. */
449+
if (!WiFiStorage.downloadOTA(_ota_url.c_str()))
460450
{
461-
while (ota_client.available())
462-
{
463-
char const c = ota_client.read();
464-
465-
/* Check if header is complete. */
466-
if(!is_header_complete)
467-
{
468-
http_header += c;
469-
is_header_complete = http_header.endsWith("\r\n\r\n");
470-
break;
471-
}
472-
473-
/* If we reach this point then the HTTP header has
474-
* been received and we can feed the incoming binary
475-
* data into the OTA state machine.
476-
*/
477-
if (_ota_logic.onOTADataReceived(reinterpret_cast<uint8_t const *>(&c), 1) == 200)
478-
{
479-
_ota_error = static_cast<int>(_ota_logic.update());
480-
bytes_recv += 200;
481-
}
482-
}
451+
DBG_ERROR("ArduinoIoTCloudTCP::%s download to NiNa failed", __FUNCTION__, _ota_req ? "true" : "false");
452+
_ota_error = static_cast<int>(OTAError::DownloadFailed);
453+
return;
483454
}
455+
456+
/* The download was a success. */
457+
ota_download_success = true;
458+
#endif /* OTA_STORAGE_SNU */
459+
460+
/* Perform the reset to reboot to SxU. */
461+
if (ota_download_success)
462+
NVIC_SystemReset();
484463
}
485464
}
486465
#endif

src/utility/ota/OTALogic.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ enum class OTAError : int
5757
ChecksumMismatch = 4,
5858
ReceivedDataOverrun = 5,
5959
RenameOfTempFileFailed = 6,
60-
NoOTAStorageConfigured = 7
60+
NoOTAStorageConfigured = 7,
61+
DownloadFailed = 8,
6162
};
6263

6364
/******************************************************************************

0 commit comments

Comments
 (0)