Skip to content

Commit a93b4e2

Browse files
authored
Merge pull request #145 from arduino-libraries/ota-nina
Use the WiFi Nina module for storing the OTA image
2 parents 167baba + b00964e commit a93b4e2

File tree

6 files changed

+172
-4
lines changed

6 files changed

+172
-4
lines changed

Diff for: .github/workflows/compile-examples.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ jobs:
5757
- name: ArduinoECCX08
5858
- name: RTCZero
5959
- name: WiFi101
60-
- name: WiFiNINA
60+
# Use the version of WiFiNINA from the tip of its repository's default branch
61+
- source-url: https://github.com/arduino-libraries/WiFiNINA.git
6162
- name: Arduino_MKRMEM
6263
sketch-paths: '"examples/utility/Provisioning" "examples/utility/WiFi_Cloud_Blink"'
6364
# LoRaWAN boards

Diff for: extras/tools/bin2json.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import base64
66

77
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.
8-
INTER_CHUNK_DELAY_MS = 250 # This is delay between 2 consecutive chunks so as to not over load the embedded device.
8+
INTER_CHUNK_DELAY_MS = 500 # This is delay between 2 consecutive chunks so as to not over load the embedded device.
99

1010
if len(sys.argv) != 3:
1111
print ("Usage: bin2json.py sketch.ota sketch.json")

Diff for: src/ArduinoIoTCloudTCP.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
#include "tls/utility/CryptoUtil.h"
3030
#endif
3131

32-
#include "utility/ota/OTAStorage_SSU.h"
32+
#include "utility/ota/OTAStorage_SNU.h"
3333
#include "utility/ota/OTAStorage_SFU.h"
34+
#include "utility/ota/OTAStorage_SSU.h"
3435

3536
#include "cbor/CBOREncoder.h"
3637

@@ -44,6 +45,8 @@ TimeService time_service;
4445
static OTAStorage_SSU ota_storage_ssu;
4546
#elif OTA_STORAGE_SFU
4647
static OTAStorage_SFU ota_storage_sfu;
48+
#elif OTA_STORAGE_SNU
49+
static OTAStorage_SNU ota_storage_snu;
4750
#endif
4851

4952
/******************************************************************************
@@ -148,6 +151,8 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
148151
setOTAStorage(ota_storage_ssu);
149152
#elif OTA_STORAGE_SFU
150153
setOTAStorage(ota_storage_sfu);
154+
#elif OTA_STORAGE_SNU
155+
setOTAStorage(ota_storage_snu);
151156
#endif
152157

153158
return 1;

Diff for: src/ArduinoIoTCloud_Config.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
#define OTA_STORAGE_SFU (0)
2727
#endif
2828

29+
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT)
30+
#define OTA_STORAGE_SNU (1)
31+
#else
32+
#define OTA_STORAGE_SNU (0)
33+
#endif
34+
2935
#ifdef ARDUINO_SAMD_MKRGSM1400
3036
#define OTA_STORAGE_SSU (1)
3137
#else
@@ -36,7 +42,7 @@
3642
* AUTOMATIC CONFIGURED DEFINES
3743
******************************************************************************/
3844

39-
#if OTA_STORAGE_SFU || OTA_STORAGE_SSU
45+
#if OTA_STORAGE_SFU || OTA_STORAGE_SSU || OTA_STORAGE_SNU
4046
#define OTA_ENABLED (1)
4147
#else
4248
#define OTA_ENABLED (0)

Diff for: src/utility/ota/OTAStorage_SNU.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
/******************************************************************************
19+
* INCLUDE
20+
******************************************************************************/
21+
22+
#include <ArduinoIoTCloud_Config.h>
23+
#if OTA_STORAGE_SNU
24+
25+
#include "OTAStorage_SNU.h"
26+
27+
#include <WiFiStorage.h>
28+
29+
/******************************************************************************
30+
* CONSTANTS
31+
******************************************************************************/
32+
33+
static char const SNU_UPDATE_FILENAME[] = "/fs/UPDATE.BIN";
34+
static char const SNU_TEMP_UPDATE_FILENAME[] = "/fs/UPDATE.BIN.TMP";
35+
36+
/******************************************************************************
37+
* PUBLIC MEMBER FUNCTIONS
38+
******************************************************************************/
39+
40+
bool OTAStorage_SNU::init()
41+
{
42+
/* Ensure that there are no remains of previous
43+
* aborted downloads still existing in the memory
44+
* of the nina module.
45+
*/
46+
WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME);
47+
return true;
48+
}
49+
50+
bool OTAStorage_SNU::open(char const * /* file_name */)
51+
{
52+
/* There's no need to explicitly open the file
53+
* because when writing to it the file will always
54+
* be opened with "ab+" mode and closed after each
55+
* call to 'write'.
56+
*/
57+
return true;
58+
}
59+
60+
size_t OTAStorage_SNU::write(uint8_t const * const buf, size_t const num_bytes)
61+
{
62+
WiFiStorageFile file(SNU_TEMP_UPDATE_FILENAME);
63+
64+
/* We have to write in chunks because otherwise we exceed the size of
65+
* the SPI buffer within the nina module.
66+
*/
67+
size_t bytes_written = 0;
68+
size_t const WRITE_CHUNK_SIZE = 32;
69+
70+
for(; bytes_written < (num_bytes - WRITE_CHUNK_SIZE); bytes_written += WRITE_CHUNK_SIZE)
71+
{
72+
if (file.write(buf + bytes_written, WRITE_CHUNK_SIZE) != WRITE_CHUNK_SIZE)
73+
return bytes_written;
74+
}
75+
76+
bytes_written += file.write(buf + bytes_written, num_bytes - bytes_written);
77+
78+
return bytes_written;
79+
}
80+
81+
void OTAStorage_SNU::close()
82+
{
83+
/* Files are closed after each file operation on the nina side. */
84+
}
85+
86+
void OTAStorage_SNU::remove(char const * /* file_name */)
87+
{
88+
WiFiStorage.remove(SNU_TEMP_UPDATE_FILENAME);
89+
}
90+
91+
bool OTAStorage_SNU::rename(char const * /* old_file_name */, char const * /* new_file_name */)
92+
{
93+
return WiFiStorage.rename(SNU_TEMP_UPDATE_FILENAME, SNU_UPDATE_FILENAME);
94+
}
95+
96+
void OTAStorage_SNU::deinit()
97+
{
98+
/* Nothing to do */
99+
}
100+
101+
#endif /* OTA_STORAGE_SNU */

Diff for: src/utility/ota/OTAStorage_SNU.h

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifndef ARDUINO_OTA_STORAGE_SNU_H_
19+
#define ARDUINO_OTA_STORAGE_SNU_H_
20+
21+
/******************************************************************************
22+
* INCLUDE
23+
******************************************************************************/
24+
25+
#include <ArduinoIoTCloud_Config.h>
26+
#if OTA_STORAGE_SNU
27+
28+
#include <SNU.h>
29+
30+
#include "OTAStorage.h"
31+
32+
/******************************************************************************
33+
* CLASS DECLARATION
34+
******************************************************************************/
35+
36+
class OTAStorage_SNU : public OTAStorage
37+
{
38+
public:
39+
40+
virtual ~OTAStorage_SNU() { }
41+
42+
43+
virtual bool init () override;
44+
virtual bool open (char const * file_name) override;
45+
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override;
46+
virtual void close () override;
47+
virtual void remove(char const * file_name) override;
48+
virtual bool rename(char const * old_file_name, char const * new_file_name) override;
49+
virtual void deinit() override;
50+
51+
};
52+
53+
#endif /* OTA_STORAGE_SNU */
54+
55+
#endif /* ARDUINO_OTA_STORAGE_SNU_H_ */

0 commit comments

Comments
 (0)