From 1efcc58e4e75e5140b46517c60729a5dac866af6 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 4 Aug 2020 08:46:52 +0200 Subject: [PATCH 1/4] Add 'downloadOTA' command to download OTA file and verify length/CRC --- src/WiFiStorage.h | 4 ++++ src/utility/wifi_drv.cpp | 31 +++++++++++++++++++++++++++++++ src/utility/wifi_drv.h | 1 + src/utility/wifi_spi.h | 1 + 4 files changed, 37 insertions(+) diff --git a/src/WiFiStorage.h b/src/WiFiStorage.h index e762f8fe..f3eb93fe 100644 --- a/src/WiFiStorage.h +++ b/src/WiFiStorage.h @@ -58,6 +58,10 @@ class WiFiStorageClass WiFiDrv::downloadFile(url, strlen(url), filename, strlen(filename)); return true; } + static bool downloadOTA(const char * url) { + return (WiFiDrv::downloadOTA(url, strlen(url)) == 0); + } + static bool remove(String filename) { return remove(filename.c_str()); diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index 7a183e22..9110af56 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -1146,6 +1146,37 @@ int8_t WiFiDrv::downloadFile(const char* url, uint8_t url_len, const char *filen return _data; } +int8_t WiFiDrv::downloadOTA(const char* url, uint8_t url_len) +{ + WAIT_FOR_SLAVE_SELECT(); + // Send Command + SpiDrv::sendCmd(DOWNLOAD_OTA, PARAM_NUMS_1); + SpiDrv::sendParam((uint8_t*)url, url_len, LAST_PARAM); + + // pad to multiple of 4 + int commandSize = 6 + url_len; + while (commandSize % 4) { + SpiDrv::readChar(); + commandSize++; + } + + SpiDrv::spiSlaveDeselect(); + //Wait the reply elaboration + SpiDrv::waitForSlaveReady(); + SpiDrv::spiSlaveSelect(); + + // Wait for reply + uint8_t _data = 0; + uint8_t _dataLen = 0; + if (!SpiDrv::waitResponseCmd(DOWNLOAD_OTA, PARAM_NUMS_1, &_data, &_dataLen)) + { + WARN("error waitResponse"); + _data = WL_FAILURE; + } + SpiDrv::spiSlaveDeselect(); + return _data; +} + 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) { WAIT_FOR_SLAVE_SELECT(); diff --git a/src/utility/wifi_drv.h b/src/utility/wifi_drv.h index c97a1c1c..4127d475 100644 --- a/src/utility/wifi_drv.h +++ b/src/utility/wifi_drv.h @@ -294,6 +294,7 @@ class WiFiDrv static void analogWrite(uint8_t pin, uint8_t value); static int8_t downloadFile(const char* url, uint8_t url_len, const char *filename, uint8_t filename_len); + static int8_t downloadOTA(const char* url, uint8_t url_len); 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); static int8_t fileOperation(uint8_t operation, const char *filename, uint8_t filename_len, uint32_t offset, uint8_t* buffer, uint32_t len); diff --git a/src/utility/wifi_spi.h b/src/utility/wifi_spi.h index f50f76c2..0dc46810 100644 --- a/src/utility/wifi_spi.h +++ b/src/utility/wifi_spi.h @@ -111,6 +111,7 @@ enum { DOWNLOAD_FILE = 0x64, APPLY_OTA_COMMAND = 0x65, RENAME_FILE = 0x66, + DOWNLOAD_OTA = 0x67, }; From 6956ea957ab2ff29277329211b758dac9d25d6e9 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 4 Aug 2020 15:06:44 +0200 Subject: [PATCH 2/4] Bugfix: When only one param is sent, the total length field is not send, to the overhead bytes are 5 instead of 6 --- src/utility/wifi_drv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utility/wifi_drv.cpp b/src/utility/wifi_drv.cpp index 9110af56..24beece8 100644 --- a/src/utility/wifi_drv.cpp +++ b/src/utility/wifi_drv.cpp @@ -1154,7 +1154,7 @@ int8_t WiFiDrv::downloadOTA(const char* url, uint8_t url_len) SpiDrv::sendParam((uint8_t*)url, url_len, LAST_PARAM); // pad to multiple of 4 - int commandSize = 6 + url_len; + int commandSize = 5 + url_len; while (commandSize % 4) { SpiDrv::readChar(); commandSize++; From 2c4518c81755ca3e21b342c1d9bee28f43f9cd97 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 6 Aug 2020 08:18:00 +0200 Subject: [PATCH 3/4] Adding String API to C-String API for downloadOTA --- src/WiFiStorage.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/WiFiStorage.h b/src/WiFiStorage.h index f3eb93fe..249ed1c8 100644 --- a/src/WiFiStorage.h +++ b/src/WiFiStorage.h @@ -78,6 +78,9 @@ class WiFiStorageClass static bool download(String url, String filename) { return download(url.c_str(), filename.c_str()); } + static bool download(String url) { + return downloadOTA(url.c_str()); + } }; extern WiFiStorageClass WiFiStorage; From ee28a2136a8ce59c48803617bf06fe7f4e7634f1 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 6 Aug 2020 08:27:03 +0200 Subject: [PATCH 4/4] Allow to retrieve the error code from the nina module that happend during OTA download: typedef enum OTA_Error { ERR_NO_ERROR = 0, ERR_OPEN = 1, ERR_LENGTH = 2, ERR_CRC = 3, ERR_RENAME = 4, }; --- src/WiFiStorage.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/WiFiStorage.h b/src/WiFiStorage.h index 249ed1c8..54f744a0 100644 --- a/src/WiFiStorage.h +++ b/src/WiFiStorage.h @@ -58,8 +58,12 @@ class WiFiStorageClass WiFiDrv::downloadFile(url, strlen(url), filename, strlen(filename)); return true; } - static bool downloadOTA(const char * url) { - return (WiFiDrv::downloadOTA(url, strlen(url)) == 0); + static bool downloadOTA(const char * url, uint8_t * res_ota_download = NULL) { + uint8_t const res = WiFiDrv::downloadOTA(url, strlen(url)); + if (res_ota_download) + *res_ota_download = res; + bool const success = (res == 0); + return success; } @@ -78,8 +82,8 @@ class WiFiStorageClass static bool download(String url, String filename) { return download(url.c_str(), filename.c_str()); } - static bool download(String url) { - return downloadOTA(url.c_str()); + static bool download(String url, uint8_t * res_ota_download = NULL) { + return downloadOTA(url.c_str(), res_ota_download); } };