Skip to content

Commit a8d4745

Browse files
authored
Merge pull request #124 from arduino-libraries/nina-download-ota
Add 'downloadOTA' command to download OTA file and verify length/CRC
2 parents 114a48c + ee28a21 commit a8d4745

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/WiFiStorage.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ class WiFiStorageClass
5858
WiFiDrv::downloadFile(url, strlen(url), filename, strlen(filename));
5959
return true;
6060
}
61+
static bool downloadOTA(const char * url, uint8_t * res_ota_download = NULL) {
62+
uint8_t const res = WiFiDrv::downloadOTA(url, strlen(url));
63+
if (res_ota_download)
64+
*res_ota_download = res;
65+
bool const success = (res == 0);
66+
return success;
67+
}
68+
6169

6270
static bool remove(String filename) {
6371
return remove(filename.c_str());
@@ -74,6 +82,9 @@ class WiFiStorageClass
7482
static bool download(String url, String filename) {
7583
return download(url.c_str(), filename.c_str());
7684
}
85+
static bool download(String url, uint8_t * res_ota_download = NULL) {
86+
return downloadOTA(url.c_str(), res_ota_download);
87+
}
7788
};
7889

7990
extern WiFiStorageClass WiFiStorage;

src/utility/wifi_drv.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,37 @@ int8_t WiFiDrv::downloadFile(const char* url, uint8_t url_len, const char *filen
11461146
return _data;
11471147
}
11481148

1149+
int8_t WiFiDrv::downloadOTA(const char* url, uint8_t url_len)
1150+
{
1151+
WAIT_FOR_SLAVE_SELECT();
1152+
// Send Command
1153+
SpiDrv::sendCmd(DOWNLOAD_OTA, PARAM_NUMS_1);
1154+
SpiDrv::sendParam((uint8_t*)url, url_len, LAST_PARAM);
1155+
1156+
// pad to multiple of 4
1157+
int commandSize = 5 + url_len;
1158+
while (commandSize % 4) {
1159+
SpiDrv::readChar();
1160+
commandSize++;
1161+
}
1162+
1163+
SpiDrv::spiSlaveDeselect();
1164+
//Wait the reply elaboration
1165+
SpiDrv::waitForSlaveReady();
1166+
SpiDrv::spiSlaveSelect();
1167+
1168+
// Wait for reply
1169+
uint8_t _data = 0;
1170+
uint8_t _dataLen = 0;
1171+
if (!SpiDrv::waitResponseCmd(DOWNLOAD_OTA, PARAM_NUMS_1, &_data, &_dataLen))
1172+
{
1173+
WARN("error waitResponse");
1174+
_data = WL_FAILURE;
1175+
}
1176+
SpiDrv::spiSlaveDeselect();
1177+
return _data;
1178+
}
1179+
11491180
int8_t WiFiDrv::renameFile(const char * old_file_name, uint8_t const old_file_name_len, const char * new_file_name, uint8_t const new_file_name_len)
11501181
{
11511182
WAIT_FOR_SLAVE_SELECT();

src/utility/wifi_drv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ class WiFiDrv
294294
static void analogWrite(uint8_t pin, uint8_t value);
295295

296296
static int8_t downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len);
297+
static int8_t downloadOTA(const char* url, uint8_t url_len);
297298
static int8_t renameFile(const char * old_file_name, uint8_t const old_file_name_len, const char * new_file_name, uint8_t const new_file_name_len);
298299

299300
static int8_t fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t len);

src/utility/wifi_spi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ enum {
111111
DOWNLOAD_FILE = 0x64,
112112
APPLY_OTA_COMMAND = 0x65,
113113
RENAME_FILE = 0x66,
114+
DOWNLOAD_OTA = 0x67,
114115
};
115116

116117

0 commit comments

Comments
 (0)