Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca5c266

Browse files
Redesigning OTALogic in a way that allows it to be kept on the stack without being allocated on the heap with 'new'
1 parent 2dc6251 commit ca5c266

File tree

6 files changed

+32
-27
lines changed

6 files changed

+32
-27
lines changed

‎extras/test/src/test_OTALogic.cpp‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ TEST_CASE("OTAStorage initialisation fails", "[OTAStorage::init() -> returns fal
6363

6464

6565
/* Perform test */
66-
OTALogic ota_logic(ota_storage.get());
66+
OTALogic ota_logic;
67+
ota_logic.setOTAStorage(ota_storage.get());
6768

6869
WHEN("OTALogic::update() is called")
6970
{
@@ -95,7 +96,8 @@ TEST_CASE("OTAStorage opening of storage file fails", "[OTAStorage::open() -> re
9596

9697

9798
/* Perform test */
98-
OTALogic ota_logic(ota_storage.get());
99+
OTALogic ota_logic;
100+
ota_logic.setOTAStorage(ota_storage.get());
99101

100102
WHEN("OTALogic::update() is called and some bytes have been received")
101103
{
@@ -131,7 +133,8 @@ TEST_CASE("OTAStorage writing to storage file fails", "[OTAStorage::write() -> f
131133

132134

133135
/* Perform test */
134-
OTALogic ota_logic(ota_storage.get());
136+
OTALogic ota_logic;
137+
ota_logic.setOTAStorage(ota_storage.get());
135138

136139
WHEN("OTALogic::update() is called and some bytes have been received")
137140
{
@@ -166,7 +169,8 @@ TEST_CASE("Data overrun due to receiving too much data", "[OTALogic - Data Overr
166169

167170

168171
/* Perform test */
169-
OTALogic ota_logic(ota_storage.get());
172+
OTALogic ota_logic;
173+
ota_logic.setOTAStorage(ota_storage.get());
170174

171175
WHEN("Too much data is received before OTALogic::update() is called again to process the incoming data")
172176
{
@@ -213,7 +217,8 @@ TEST_CASE("Valid OTA data is received ", "[OTALogic]")
213217

214218

215219
/* Perform test */
216-
OTALogic ota_logic(ota_storage.get());
220+
OTALogic ota_logic;
221+
ota_logic.setOTAStorage(ota_storage.get());
217222
simulateOTABinaryReception(ota_logic, valid_ota_test_data);
218223

219224

@@ -259,7 +264,8 @@ TEST_CASE("Invalid OTA data is received ", "[OTALogic - CRC wrong]")
259264

260265

261266
/* Perform test */
262-
OTALogic ota_logic(ota_storage.get());
267+
OTALogic ota_logic;
268+
ota_logic.setOTAStorage(ota_storage.get());
263269
simulateOTABinaryReception(ota_logic, invalid_valid_ota_test_data_crc_wrong);
264270

265271

‎src/ArduinoIoTCloudTCP.cpp‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
7878
, _dataTopicIn("")
7979
, _ota_topic_in{""}
8080
#if OTA_ENABLED
81-
, _ota_logic{nullptr}
8281
, _ota_storage_type{static_cast<int>(OTAStorage::Type::NotAvailable)}
8382
, _ota_error{static_cast<int>(OTAError::None)}
8483
#endif /* OTA_ENABLED */
@@ -155,10 +154,8 @@ void ArduinoIoTCloudTCP::update()
155154
* 'update' method here in order to process incoming data and generally
156155
* to transition to the OTA logic update states.
157156
*/
158-
if (_ota_logic) {
159-
OTAError const err = _ota_logic->update();
160-
_ota_error = static_cast<int>(err);
161-
}
157+
OTAError const err = _ota_logic.update();
158+
_ota_error = static_cast<int>(err);
162159
#endif /* OTA_ENABLED */
163160

164161
// Check if a primitive property wrapper is locally changed
@@ -217,8 +214,7 @@ void ArduinoIoTCloudTCP::setOTAStorage(OTAStorage & ota_storage)
217214
_ota_storage_type = static_cast<int>(ota_storage.type());
218215
addPropertyReal(_ota_storage_type, "OTA_STORAGE_TYPE", Permission::Read);
219216
addPropertyReal(_ota_error, "OTA_ERROR", Permission::Read);
220-
if(_ota_logic) delete _ota_logic;
221-
_ota_logic = new OTALogic(ota_storage);
217+
_ota_logic.setOTAStorage(ota_storage);
222218
}
223219
#endif /* OTA_ENABLED */
224220

@@ -287,8 +283,8 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
287283
_syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_VALUES_PROCESSED;
288284
}
289285
#if OTA_ENABLED
290-
if (_ota_logic && (_ota_topic_in == topic)) {
291-
_ota_logic->onOTADataReceived(bytes, length);
286+
if (_ota_topic_in == topic) {
287+
_ota_logic.onOTADataReceived(bytes, length);
292288
}
293289
#endif /* OTA_ENABLED */
294290
}

‎src/ArduinoIoTCloudTCP.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
128128
String _ota_topic_in;
129129

130130
#if OTA_ENABLED
131-
OTALogic * _ota_logic;
131+
OTALogic _ota_logic;
132132
int _ota_storage_type;
133133
int _ota_error;
134134
#endif /* OTA_ENABLED */

‎src/ArduinoIoTCloud_Config.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
******************************************************************************/
2424

2525
#ifndef OTA_STORAGE_MKRMEM
26-
#define OTA_STORAGE_MKRMEM (0)
26+
#define OTA_STORAGE_MKRMEM (1)
2727
#endif
2828

2929
/******************************************************************************

‎src/utility/ota/OTALogic.cpp‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
* CTOR/DTOR
4242
******************************************************************************/
4343

44-
OTALogic::OTALogic(OTAStorage & ota_storage)
45-
: _ota_storage(ota_storage)
44+
OTALogic::OTALogic()
45+
: _ota_storage{nullptr}
4646
, _ota_state{OTAState::Init}
4747
, _ota_error{OTAError::None}
4848
{
@@ -105,7 +105,7 @@ void OTALogic::onOTADataReceived(uint8_t const * const data, size_t const length
105105

106106
OTAState OTALogic::handle_Init()
107107
{
108-
if (_ota_storage.init()) {
108+
if (_ota_storage->init()) {
109109
return OTAState::Idle;
110110
} else {
111111
_ota_error = OTAError::StorageInitFailed;
@@ -123,7 +123,7 @@ OTAState OTALogic::handle_Idle()
123123

124124
OTAState OTALogic::handle_StartDownload()
125125
{
126-
if(_ota_storage.open()) {
126+
if(_ota_storage->open()) {
127127
return OTAState::WaitForHeader;
128128
} else {
129129
_ota_error = OTAError::StorageOpenFailed;
@@ -187,7 +187,7 @@ OTAState OTALogic::handle_WaitForBinary()
187187
OTAState OTALogic::handle_BinaryReceived()
188188
{
189189
/* Write to OTA storage */
190-
if(_ota_storage.write(_mqtt_ota_buf.buf, _mqtt_ota_buf.num_bytes) != _mqtt_ota_buf.num_bytes)
190+
if(_ota_storage->write(_mqtt_ota_buf.buf, _mqtt_ota_buf.num_bytes) != _mqtt_ota_buf.num_bytes)
191191
{
192192
_ota_error = OTAError::StorageWriteFailed;
193193
return OTAState::Error;
@@ -201,7 +201,7 @@ OTAState OTALogic::handle_BinaryReceived()
201201
_mqtt_ota_buf.num_bytes = 0;
202202

203203
if(_ota_bin_data.bytes_received >= _ota_bin_data.hdr_len) {
204-
_ota_storage.close();
204+
_ota_storage->close();
205205
_ota_bin_data.crc32 = crc_finalize(_ota_bin_data.crc32);
206206
return OTAState::Verify;
207207
}
@@ -212,10 +212,10 @@ OTAState OTALogic::handle_BinaryReceived()
212212
OTAState OTALogic::handle_Verify()
213213
{
214214
if(_ota_bin_data.crc32 == _ota_bin_data.hdr_crc32) {
215-
_ota_storage.deinit();
215+
_ota_storage->deinit();
216216
return OTAState::Reset;
217217
} else {
218-
_ota_storage.remove();
218+
_ota_storage->remove();
219219
_ota_error = OTAError::ChecksumMismatch;
220220
return OTAState::Error;
221221
}

‎src/utility/ota/OTALogic.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ class OTALogic
6767

6868
public:
6969

70-
OTALogic(OTAStorage & ota_storage);
70+
OTALogic();
71+
72+
73+
inline void setOTAStorage(OTAStorage & ota_storage) { _ota_storage = &ota_storage; }
7174

7275

7376
OTAError update();
@@ -95,7 +98,7 @@ class OTALogic
9598
crc_t crc32;
9699
} sOTABinaryData;
97100

98-
OTAStorage & _ota_storage;
101+
OTAStorage * _ota_storage;
99102
OTAState _ota_state;
100103
OTAError _ota_error;
101104
sMQTTOTABuffer _mqtt_ota_buf;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /