Skip to content

Use the WiFi Nina module for storing the OTA image #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ jobs:
- name: ArduinoECCX08
- name: RTCZero
- name: WiFi101
- name: WiFiNINA
# Use the version of WiFiNINA from the tip of its repository's default branch
- source-url: https://github.com/arduino-libraries/WiFiNINA.git
- name: Arduino_MKRMEM
sketch-paths: '"examples/utility/Provisioning" "examples/utility/WiFi_Cloud_Blink"'
# LoRaWAN boards
Expand Down
2 changes: 1 addition & 1 deletion extras/tools/bin2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 250 # This is delay between 2 consecutive chunks so as to not over load the embedded device.
INTER_CHUNK_DELAY_MS = 500 # 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 sketch.json")
Expand Down
7 changes: 6 additions & 1 deletion src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
#include "tls/utility/CryptoUtil.h"
#endif

#include "utility/ota/OTAStorage_SSU.h"
#include "utility/ota/OTAStorage_SNU.h"
#include "utility/ota/OTAStorage_SFU.h"
#include "utility/ota/OTAStorage_SSU.h"

#include "cbor/CBOREncoder.h"

Expand All @@ -44,6 +45,8 @@ TimeService time_service;
static OTAStorage_SSU ota_storage_ssu;
#elif OTA_STORAGE_SFU
static OTAStorage_SFU ota_storage_sfu;
#elif OTA_STORAGE_SNU
static OTAStorage_SNU ota_storage_snu;
#endif

/******************************************************************************
Expand Down Expand Up @@ -148,6 +151,8 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
setOTAStorage(ota_storage_ssu);
#elif OTA_STORAGE_SFU
setOTAStorage(ota_storage_sfu);
#elif OTA_STORAGE_SNU
setOTAStorage(ota_storage_snu);
#endif

return 1;
Expand Down
8 changes: 7 additions & 1 deletion src/ArduinoIoTCloud_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#define OTA_STORAGE_SFU (0)
#endif

#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT)
#define OTA_STORAGE_SNU (1)
#else
#define OTA_STORAGE_SNU (0)
#endif

#ifdef ARDUINO_SAMD_MKRGSM1400
#define OTA_STORAGE_SSU (1)
#else
Expand All @@ -36,7 +42,7 @@
* AUTOMATIC CONFIGURED DEFINES
******************************************************************************/

#if OTA_STORAGE_SFU || OTA_STORAGE_SSU
#if OTA_STORAGE_SFU || OTA_STORAGE_SSU || OTA_STORAGE_SNU
#define OTA_ENABLED (1)
#else
#define OTA_ENABLED (0)
Expand Down
101 changes: 101 additions & 0 deletions src/utility/ota/OTAStorage_SNU.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
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 [email protected].
*/

/******************************************************************************
* INCLUDE
******************************************************************************/

#include <ArduinoIoTCloud_Config.h>
#if OTA_STORAGE_SNU

#include "OTAStorage_SNU.h"

#include <WiFiStorage.h>

/******************************************************************************
* CONSTANTS
******************************************************************************/

static char const SNU_UPDATE_FILENAME[] = "/fs/UPDATE.BIN";
static char const SNU_TEMP_UPDATE_FILENAME[] = "/fs/UPDATE.BIN.TMP";

/******************************************************************************
* PUBLIC MEMBER FUNCTIONS
******************************************************************************/

bool OTAStorage_SNU::init()
{
/* Ensure that there are no remains of previous
* aborted downloads still existing in the memory
* of the nina module.
*/
WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME);
return true;
}

bool OTAStorage_SNU::open(char const * /* file_name */)
{
/* There's no need to explicitly open the file
* because when writing to it the file will always
* be opened with "ab+" mode and closed after each
* call to 'write'.
*/
return true;
}

size_t OTAStorage_SNU::write(uint8_t const * const buf, size_t const num_bytes)
{
WiFiStorageFile file(SNU_TEMP_UPDATE_FILENAME);

/* We have to write in chunks because otherwise we exceed the size of
* the SPI buffer within the nina module.
*/
size_t bytes_written = 0;
size_t const WRITE_CHUNK_SIZE = 32;

for(; bytes_written < (num_bytes - WRITE_CHUNK_SIZE); bytes_written += WRITE_CHUNK_SIZE)
{
if (file.write(buf + bytes_written, WRITE_CHUNK_SIZE) != WRITE_CHUNK_SIZE)
return bytes_written;
}

bytes_written += file.write(buf + bytes_written, num_bytes - bytes_written);

return bytes_written;
}

void OTAStorage_SNU::close()
{
/* Files are closed after each file operation on the nina side. */
}

void OTAStorage_SNU::remove(char const * /* file_name */)
{
WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME);
}

bool OTAStorage_SNU::rename(char const * /* old_file_name */, char const * /* new_file_name */)
{
return WiFiStorage.rename(SNU_TEMP_UPDATE_FILENAME, SNU_UPDATE_FILENAME);
}

void OTAStorage_SNU::deinit()
{
/* Nothing to do */
}

#endif /* OTA_STORAGE_SNU */
55 changes: 55 additions & 0 deletions src/utility/ota/OTAStorage_SNU.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 [email protected].
*/

#ifndef ARDUINO_OTA_STORAGE_SNU_H_
#define ARDUINO_OTA_STORAGE_SNU_H_

/******************************************************************************
* INCLUDE
******************************************************************************/

#include <ArduinoIoTCloud_Config.h>
#if OTA_STORAGE_SNU

#include <SNU.h>

#include "OTAStorage.h"

/******************************************************************************
* CLASS DECLARATION
******************************************************************************/

class OTAStorage_SNU : public OTAStorage
{
public:

virtual ~OTAStorage_SNU() { }


virtual bool init () 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(char const * file_name) override;
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
virtual void deinit() override;

};

#endif /* OTA_STORAGE_SNU */

#endif /* ARDUINO_OTA_STORAGE_SNU_H_ */