Skip to content

Commit 1c3fd9e

Browse files
committed
Providing capability for a unified interface described via class OTAStorage for storing downloaded binaries on different media
1 parent 0bae73c commit 1c3fd9e

File tree

7 files changed

+242
-1
lines changed

7 files changed

+242
-1
lines changed

.github/workflows/compile-examples.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
env:
1010
# libraries to install
11-
UNIVERSAL_LIBRARIES: '"ArduinoCloudThing" "Arduino_ConnectionHandler" "Arduino_DebugUtils" "ArduinoMqttClient"'
11+
UNIVERSAL_LIBRARIES: '"ArduinoCloudThing" "Arduino_ConnectionHandler" "Arduino_DebugUtils" "ArduinoMqttClient" "Arduino_MKRMEM"'
1212
# board-specific libraries
1313
WIFI_LIBRARIES: '"ArduinoBearSSL" "ArduinoECCX08" "RTCZero" "WiFi101" "WiFiNINA"'
1414
WAN_LIBRARIES: '"ArduinoBearSSL" "ArduinoECCX08" "RTCZero" "MKRWAN"'

src/ArduinoIoTCloudTCP.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
107107
_brokerAddress = brokerAddress;
108108
_brokerPort = brokerPort;
109109

110+
#if OTA_STORAGE_MKRMEM
111+
if (!_ota.init()) {
112+
Debug.print(DBG_ERROR, "OTA storage medium initialisation failed - can't perform OTA");
113+
}
114+
#endif /* OTA_ENABLE */
115+
110116
#ifdef BOARD_HAS_ECCX08
111117
if (!ECCX08.begin()) { Debug.print(DBG_ERROR, "Cryptography processor failure. Make sure you have a compatible board."); return 0; }
112118
if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) { Debug.print(DBG_ERROR, "Cryptography processor read failure."); return 0; }

src/ArduinoIoTCloudTCP.h

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333

3434
#include <ArduinoMqttClient.h>
3535

36+
#include "utility/ota/OTAConfig.h"
37+
#if OTA_STORAGE_MKRMEM
38+
#include "utility/ota/OTAStorage_MKRMEM.h"
39+
#endif /* OTA_STORAGE_MKRMEM */
40+
3641
/******************************************************************************
3742
CONSTANTS
3843
******************************************************************************/
@@ -117,6 +122,10 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
117122
String _ota_topic_in;
118123
String _ota_topic_out;
119124

125+
#if OTA_STORAGE_MKRMEM
126+
OTAStorage_MKRMEM _ota;
127+
#endif /* OTA_STORAGE_MKRMEM */
128+
120129
inline String getTopic_stdin () { return String("/a/d/" + getDeviceId() + "/s/i"); }
121130
inline String getTopic_stdout () { return String("/a/d/" + getDeviceId() + "/s/o"); }
122131
inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); }
@@ -132,6 +141,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
132141
void requestLastValue();
133142
ArduinoIoTConnectionStatus checkCloudConnection();
134143
int write(String const topic, byte const data[], int const length);
144+
135145
};
136146

137147
/******************************************************************************

src/utility/ota/OTAConfig.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_CONFIG_H_
19+
#define ARDUINO_OTA_CONFIG_H_
20+
21+
/******************************************************************************
22+
* DEFINE
23+
******************************************************************************/
24+
25+
#ifndef OTA_STORAGE_MKRMEM
26+
#define OTA_STORAGE_MKRMEM (1)
27+
#endif
28+
29+
#if !defined(ARDUINO_SAMD_MKR1000) && !defined(ARDUINO_SAMD_MKRWIFI1010) && !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500)
30+
#define OTA_STORAGE_MKRMEM (0)
31+
#endif
32+
33+
#endif /* ARDUINO_OTA_CONFIG_H_ */

src/utility/ota/OTAStorage.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_H_
19+
#define ARDUINO_OTA_STORAGE_H_
20+
21+
/******************************************************************************
22+
* INCLUDE
23+
******************************************************************************/
24+
25+
#include <stdlib.h>
26+
#include <stdint.h>
27+
#include <stdbool.h>
28+
29+
/******************************************************************************
30+
* CLASS DECLARATION
31+
******************************************************************************/
32+
33+
class OTAStorage
34+
{
35+
public:
36+
37+
virtual ~OTAStorage() { }
38+
39+
40+
virtual bool init () = 0;
41+
virtual bool open () = 0;
42+
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) = 0;
43+
virtual void close () = 0;
44+
virtual void deinit() = 0;
45+
46+
};
47+
48+
#endif /* ARDUINO_OTA_STORAGE_H_ */

src/utility/ota/OTAStorage_MKRMEM.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 "OTAStorage_MKRMEM.h"
23+
#if OTA_STORAGE_MKRMEM
24+
25+
#include <Arduino_DebugUtils.h>
26+
27+
/******************************************************************************
28+
* CTOR/DTOR
29+
******************************************************************************/
30+
31+
OTAStorage_MKRMEM::OTAStorage_MKRMEM()
32+
: _file{nullptr}
33+
{
34+
35+
}
36+
37+
/******************************************************************************
38+
* PUBLIC MEMBER FUNCTIONS
39+
******************************************************************************/
40+
41+
bool OTAStorage_MKRMEM::init()
42+
{
43+
flash.begin();
44+
if(SPIFFS_OK != filesystem.mount()) {
45+
Debug.print(DBG_ERROR, "OTAStorage_MKRMEM::init - mount() failed with error code %d", filesystem.err());
46+
return false;
47+
}
48+
49+
if(SPIFFS_OK != filesystem.check()) {
50+
Debug.print(DBG_ERROR, "OTAStorage_MKRMEM::init - check() failed with error code %d", filesystem.err());
51+
return false;
52+
}
53+
54+
return true;
55+
}
56+
57+
bool OTAStorage_MKRMEM::open()
58+
{
59+
filesystem.clearerr();
60+
_file = new File(filesystem.open("UPDATE.bin", CREATE | WRITE_ONLY| TRUNCATE));
61+
if(SPIFFS_OK != filesystem.err()) {
62+
Debug.print(DBG_ERROR, "OTAStorage_MKRMEM::open - open() failed with error code %d", filesystem.err());
63+
delete _file;
64+
return false;
65+
}
66+
return true;
67+
}
68+
69+
size_t OTAStorage_MKRMEM::write(uint8_t const * const buf, size_t const num_bytes)
70+
{
71+
return _file->write(const_cast<uint8_t *>(buf), num_bytes);
72+
}
73+
74+
void OTAStorage_MKRMEM::close()
75+
{
76+
_file->close();
77+
delete _file;
78+
}
79+
80+
void OTAStorage_MKRMEM::deinit()
81+
{
82+
filesystem.unmount();
83+
}
84+
85+
#endif /* OTA_STORAGE_MKRMEM */

src/utility/ota/OTAStorage_MKRMEM.h

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_MKRKEM_H_
19+
#define ARDUINO_OTA_STORAGE_MKRKEM_H_
20+
21+
/******************************************************************************
22+
* INCLUDE
23+
******************************************************************************/
24+
25+
#include "OTAConfig.h"
26+
#if OTA_STORAGE_MKRMEM
27+
28+
#include "OTAStorage.h"
29+
30+
#include <Arduino_MKRMEM.h>
31+
32+
/******************************************************************************
33+
* CLASS DECLARATION
34+
******************************************************************************/
35+
36+
class OTAStorage_MKRMEM : public OTAStorage
37+
{
38+
public:
39+
40+
OTAStorage_MKRMEM();
41+
virtual ~OTAStorage_MKRMEM() { }
42+
43+
44+
virtual bool init () override;
45+
virtual bool open () override;
46+
virtual size_t write (uint8_t const * const buf, size_t const num_bytes) override;
47+
virtual void close () override;
48+
virtual void deinit() override;
49+
50+
51+
private:
52+
53+
File * _file;
54+
55+
};
56+
57+
#endif /* OTA_STORAGE_MKRMEM */
58+
59+
#endif /* ARDUINO_OTA_STORAGE_MKRKEM_H_ */

0 commit comments

Comments
 (0)