From 44c99877ef709647d086989529c423928afd7517 Mon Sep 17 00:00:00 2001 From: Giampaolo Mancini Date: Tue, 30 Jun 2020 09:24:23 +0200 Subject: [PATCH 1/5] Add support for storage via U-201 filsystem --- src/utility/ota/OTAStorage_MKRGSM.cpp | 73 +++++++++++++++++++++++++++ src/utility/ota/OTAStorage_MKRGSM.h | 54 ++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/utility/ota/OTAStorage_MKRGSM.cpp create mode 100644 src/utility/ota/OTAStorage_MKRGSM.h diff --git a/src/utility/ota/OTAStorage_MKRGSM.cpp b/src/utility/ota/OTAStorage_MKRGSM.cpp new file mode 100644 index 000000000..6987bf53a --- /dev/null +++ b/src/utility/ota/OTAStorage_MKRGSM.cpp @@ -0,0 +1,73 @@ +/* + This file is part of ArduinoIoTCloud. + + Copyright 2020 ARDUINO SA (http://www.arduino.cc/) + + This software is released under the GNU General Public License version 3, + which covers the main part of arduino-cli. + The terms of this license can be found at: + https://www.gnu.org/licenses/gpl-3.0.en.html + + You can be released from the requirements of the above licenses by purchasing + a commercial license. Buying such a license is mandatory if you want to modify or + otherwise use the software for commercial activities involving the Arduino + software without disclosing the source code of your own applications. To purchase + a commercial license, send an email to license@arduino.cc. +*/ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include "OTAStorage_MKRGSM.h" + +#include + +/****************************************************************************** + CTOR/DTOR + ******************************************************************************/ + +OTAStorage_MKRGSM::OTAStorage_MKRGSM() +{ +} + +// GSMFileUtils _fileUtils; +// String filename = "UPDATE.BIN"; +constexpr char * filename { "UPDATE.BIN" }; +/****************************************************************************** + PUBLIC MEMBER FUNCTIONS + ******************************************************************************/ + +bool OTAStorage_MKRGSM::init() +{ + return _fileUtils.begin(); +} + +bool OTAStorage_MKRGSM::open() +{ + auto size = _fileUtils.listFile(filename); + _fileUtils.deleteFile(filename); + return true; +} + +size_t OTAStorage_MKRGSM::write(uint8_t const* const buf, size_t const num_bytes) +{ + // Serial.print("size: "); + // Serial.println(num_bytes); + _fileUtils.appendFile(filename, (const char*)buf, num_bytes); + + return num_bytes; +} + +void OTAStorage_MKRGSM::close() +{ +} + +void OTAStorage_MKRGSM::remove() +{ + _fileUtils.deleteFile(filename); +} + +void OTAStorage_MKRGSM::deinit() +{ +} diff --git a/src/utility/ota/OTAStorage_MKRGSM.h b/src/utility/ota/OTAStorage_MKRGSM.h new file mode 100644 index 000000000..6719e806e --- /dev/null +++ b/src/utility/ota/OTAStorage_MKRGSM.h @@ -0,0 +1,54 @@ +/* + This file is part of ArduinoIoTCloud. + + Copyright 2020 ARDUINO SA (http://www.arduino.cc/) + + This software is released under the GNU General Public License version 3, + which covers the main part of arduino-cli. + The terms of this license can be found at: + https://www.gnu.org/licenses/gpl-3.0.en.html + + You can be released from the requirements of the above licenses by purchasing + a commercial license. Buying such a license is mandatory if you want to modify or + otherwise use the software for commercial activities involving the Arduino + software without disclosing the source code of your own applications. To purchase + a commercial license, send an email to license@arduino.cc. +*/ + +#ifndef ARDUINO_OTA_STORAGE_MKRGSM_H_ +#define ARDUINO_OTA_STORAGE_MKRGSM_H_ + +/****************************************************************************** + * INCLUDE + ******************************************************************************/ + +#include "OTAStorage.h" + +#include + +/****************************************************************************** + * CLASS DECLARATION + ******************************************************************************/ + +class OTAStorage_MKRGSM : public OTAStorage +{ +public: + + OTAStorage_MKRGSM(); + virtual ~OTAStorage_MKRGSM() { } + + + virtual Type type () override { return Type::MKRGSMFile; } + virtual bool init () override; + virtual bool open () override; + virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override; + virtual void close () override; + virtual void remove() override; + virtual void deinit() override; + +private: + GSMFileUtils _fileUtils; + +}; + +#endif /* ARDUINO_OTA_STORAGE_MKRGSM_H_ */ From f21421508592c60418019fb87673b6c0218a13d8 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 6 Jul 2020 13:14:34 +0200 Subject: [PATCH 2/5] Changing interface of OTAStorage_MRKGSM to be compliant with the latest version of OTAStorage --- src/utility/ota/OTAStorage.h | 3 +- src/utility/ota/OTAStorage_MKRGSM.cpp | 54 ++++++++++++++++----------- src/utility/ota/OTAStorage_MKRGSM.h | 7 ++-- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/utility/ota/OTAStorage.h b/src/utility/ota/OTAStorage.h index 29a09c752..19d38f7e4 100644 --- a/src/utility/ota/OTAStorage.h +++ b/src/utility/ota/OTAStorage.h @@ -40,7 +40,8 @@ class OTAStorage enum class Type : int { NotAvailable = -1, - MKRMEM = 0 + MKRMEM = 0, + MKRGSMFile = 2, }; virtual Type type () = 0; diff --git a/src/utility/ota/OTAStorage_MKRGSM.cpp b/src/utility/ota/OTAStorage_MKRGSM.cpp index 6987bf53a..a1fdb2132 100644 --- a/src/utility/ota/OTAStorage_MKRGSM.cpp +++ b/src/utility/ota/OTAStorage_MKRGSM.cpp @@ -21,53 +21,65 @@ #include "OTAStorage_MKRGSM.h" -#include - /****************************************************************************** - CTOR/DTOR + CONSTANTS ******************************************************************************/ -OTAStorage_MKRGSM::OTAStorage_MKRGSM() -{ -} +static char const SSU_UPDATE_FILENAME[] = "UPDATE.BIN"; +static char const SSU_CHECK_FILE_NAME[] = "UPDATE.OK"; -// GSMFileUtils _fileUtils; -// String filename = "UPDATE.BIN"; -constexpr char * filename { "UPDATE.BIN" }; /****************************************************************************** PUBLIC MEMBER FUNCTIONS ******************************************************************************/ bool OTAStorage_MKRGSM::init() { - return _fileUtils.begin(); + if (!_fileUtils.begin()) + return false; + + if (_fileUtils.listFile(SSU_UPDATE_FILENAME) > 0) + if (!_fileUtils.deleteFile(SSU_UPDATE_FILENAME)) + return false; + + if (_fileUtils.listFile(SSU_CHECK_FILE_NAME) > 0) + if (!_fileUtils.deleteFile(SSU_CHECK_FILE_NAME)) + return false; } -bool OTAStorage_MKRGSM::open() +bool OTAStorage_MKRGSM::open(char const * /* file_name */) { - auto size = _fileUtils.listFile(filename); - _fileUtils.deleteFile(filename); - return true; + return true; } size_t OTAStorage_MKRGSM::write(uint8_t const* const buf, size_t const num_bytes) { - // Serial.print("size: "); - // Serial.println(num_bytes); - _fileUtils.appendFile(filename, (const char*)buf, num_bytes); - - return num_bytes; + _fileUtils.appendFile(SSU_UPDATE_FILENAME, (const char*)buf, num_bytes); + return num_bytes; } void OTAStorage_MKRGSM::close() { + /* Nothing to do */ +} + +void OTAStorage_MKRGSM::remove(char const * /* file_name */) +{ + _fileUtils.deleteFile(SSU_UPDATE_FILENAME); } -void OTAStorage_MKRGSM::remove() +bool OTAStorage_MKRGSM::rename(char const * /* old_file_name */, char const * /* new_file_name */) { - _fileUtils.deleteFile(filename); + /* Create a file 'UPDATE.OK' which is used by the SSU + * 2nd stage bootloader to recognise that the update + * went okay. Normally this is done by renaming 'UPDATE.BIN.TMP' + * to 'UPDATE.BIN' but the SARE module does not support + * a rename function. + */ + char c = 'X'; + return (_fileUtils.appendFile(SSU_CHECK_FILE_NAME, &c, sizeof(c)) == sizeof(c)); } void OTAStorage_MKRGSM::deinit() { + /* Nothing to do */ } diff --git a/src/utility/ota/OTAStorage_MKRGSM.h b/src/utility/ota/OTAStorage_MKRGSM.h index 6719e806e..c14299202 100644 --- a/src/utility/ota/OTAStorage_MKRGSM.h +++ b/src/utility/ota/OTAStorage_MKRGSM.h @@ -34,19 +34,20 @@ class OTAStorage_MKRGSM : public OTAStorage { public: - OTAStorage_MKRGSM(); virtual ~OTAStorage_MKRGSM() { } virtual Type type () override { return Type::MKRGSMFile; } virtual bool init () override; - virtual bool open () override; + virtual bool open (char const * file_name) override; virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override; virtual void close () override; - virtual void remove() override; + virtual void remove(char const * file_name) override; + virtual bool rename(char const * old_file_name, char const * new_file_name) override; virtual void deinit() override; private: + GSMFileUtils _fileUtils; }; From c226affff01dc52953241fe7c0887c5c23b87df0 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 6 Jul 2020 13:17:18 +0200 Subject: [PATCH 3/5] Adding conditional compilation for using the MKRGSM for OTA storage since the MKRGSM library is only available when the AIoTC stack is compiled for MKR GSM 1400 --- src/ArduinoIoTCloud_Config.h | 6 +++++- src/utility/ota/OTAStorage_MKRGSM.cpp | 5 +++++ src/utility/ota/OTAStorage_MKRGSM.h | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ArduinoIoTCloud_Config.h b/src/ArduinoIoTCloud_Config.h index d6840558a..2d05b8d9e 100644 --- a/src/ArduinoIoTCloud_Config.h +++ b/src/ArduinoIoTCloud_Config.h @@ -26,6 +26,10 @@ #define OTA_STORAGE_MKRMEM (0) #endif +#ifndef OTA_STORAGE_MKRGSM + #define OTA_STORAGE_MKRGSM (0) +#endif + /****************************************************************************** * AUTOMATIC CONFIGURED DEFINES ******************************************************************************/ @@ -34,7 +38,7 @@ #define OTA_STORAGE_MKRMEM (0) #endif -#if OTA_STORAGE_MKRMEM +#if OTA_STORAGE_MKRMEM || OTA_STORAGE_MKRGSM #define OTA_ENABLED (1) #else #define OTA_ENABLED (0) diff --git a/src/utility/ota/OTAStorage_MKRGSM.cpp b/src/utility/ota/OTAStorage_MKRGSM.cpp index a1fdb2132..07731bacb 100644 --- a/src/utility/ota/OTAStorage_MKRGSM.cpp +++ b/src/utility/ota/OTAStorage_MKRGSM.cpp @@ -19,6 +19,9 @@ INCLUDE ******************************************************************************/ +#include +#if OTA_STORAGE_MKRGSM + #include "OTAStorage_MKRGSM.h" /****************************************************************************** @@ -83,3 +86,5 @@ void OTAStorage_MKRGSM::deinit() { /* Nothing to do */ } + +#endif /* OTA_STORAGE_MKRGSM */ diff --git a/src/utility/ota/OTAStorage_MKRGSM.h b/src/utility/ota/OTAStorage_MKRGSM.h index c14299202..748ead1aa 100644 --- a/src/utility/ota/OTAStorage_MKRGSM.h +++ b/src/utility/ota/OTAStorage_MKRGSM.h @@ -22,6 +22,9 @@ * INCLUDE ******************************************************************************/ +#include +#if OTA_STORAGE_MKRGSM + #include "OTAStorage.h" #include @@ -52,4 +55,6 @@ class OTAStorage_MKRGSM : public OTAStorage }; +#endif /* OTA_STORAGE_MKRGSM */ + #endif /* ARDUINO_OTA_STORAGE_MKRGSM_H_ */ From a51b09ea3f65a89f9d929d683cf9ab22c8efb192 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 6 Jul 2020 15:17:32 +0200 Subject: [PATCH 4/5] Due to slowness of external storage for MGRGSM OTA storage the timeout needs to be set to 250 ms between 2 consecutive MQTT chunks --- extras/tools/bin2json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/tools/bin2json.py b/extras/tools/bin2json.py index 0d01cbcef..7c775c6a5 100755 --- a/extras/tools/bin2json.py +++ b/extras/tools/bin2json.py @@ -5,7 +5,7 @@ import base64 CHUNK_SIZE = 256 # This is the chunk size of how the binary is split on the server side for not overloading the embedded device receive buffers. -INTER_CHUNK_DELAY_MS = 100 # This is delay between 2 consecutive chunks so as to not over load the embedded device. +INTER_CHUNK_DELAY_MS = 250 # This is delay between 2 consecutive chunks so as to not over load the embedded device. if len(sys.argv) != 3: print ("Usage: bin2json.py sketch-ota.bin sketch-ota.json") From 4b7b489866215bbb96af762ea4f52d0f4db3daec Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 6 Jul 2020 15:30:04 +0200 Subject: [PATCH 5/5] Harmonizing OTA tool usage description --- extras/tools/README.md | 8 ++++---- extras/tools/bin2json.py | 2 +- extras/tools/bin2ota.py | 2 +- extras/tools/ota-upload.sh | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/extras/tools/README.md b/extras/tools/README.md index a44c544c5..475fe41f3 100644 --- a/extras/tools/README.md +++ b/extras/tools/README.md @@ -4,7 +4,7 @@ This tool can be used to extend (actually prefix) a binary generated with e.g. t ### How-To-Use ```bash -./bin2ota.py sketch.bin sketch-ota.bin +./bin2ota.py sketch.bin sketch.ota ``` #### `sketch.bin` ```bash @@ -17,7 +17,7 @@ This tool can be used to extend (actually prefix) a binary generated with e.g. t * `length(sketch.bin) = 0x0003'A5E0` * `CRC32(sketch.bin) = 0xA9D1'265B` -#### `sketch-ota.bin` +#### `sketch.ota` ```bash 0000000 A5E0 0003 265B A9D1 8000 2000 749D 0000 0000010 7485 0000 7485 0000 0000 0000 0000 0000 @@ -31,7 +31,7 @@ This tool converts the binary file into base64 encoded JSON which is necessary f ### How-To-Use ```bash -./bin2json.py sketch-ota.bin sketch-ota.json +./bin2json.py sketch.ota sketch.json ``` `ota-upload.sh` @@ -40,5 +40,5 @@ This tool allows to upload a OTA binary to a device via a Arduino cloud server. ### How-To-Use ```bash -./ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch-ota.json +./ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch.json ``` \ No newline at end of file diff --git a/extras/tools/bin2json.py b/extras/tools/bin2json.py index 7c775c6a5..b7371c87e 100755 --- a/extras/tools/bin2json.py +++ b/extras/tools/bin2json.py @@ -8,7 +8,7 @@ INTER_CHUNK_DELAY_MS = 250 # This is delay between 2 consecutive chunks so as to not over load the embedded device. if len(sys.argv) != 3: - print ("Usage: bin2json.py sketch-ota.bin sketch-ota.json") + print ("Usage: bin2json.py sketch.ota sketch.json") sys.exit() ifile = sys.argv[1] diff --git a/extras/tools/bin2ota.py b/extras/tools/bin2ota.py index 49ff4075d..0df4646ce 100755 --- a/extras/tools/bin2ota.py +++ b/extras/tools/bin2ota.py @@ -4,7 +4,7 @@ import crccheck if len(sys.argv) != 3: - print ("Usage: bin2ota.py sketch.bin sketch-ota.bin") + print ("Usage: bin2ota.py sketch.bin sketch.ota") sys.exit() ifile = sys.argv[1] diff --git a/extras/tools/ota-upload.sh b/extras/tools/ota-upload.sh index 0470ba979..d70f1b019 100755 --- a/extras/tools/ota-upload.sh +++ b/extras/tools/ota-upload.sh @@ -1,7 +1,7 @@ #!/bin/bash if [ "$#" -ne 4 ]; then - echo "Usage: ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch-ota.base64" + echo "Usage: ota-upload.sh CLIENT_ID CLIENT_SECRET DEVICE_ID sketch.json" exit 1 fi