Skip to content

Commit 566accb

Browse files
committed
Fix OTA flow
1 parent 58b2603 commit 566accb

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

src/AIoTC_Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,6 @@
151151
#define AIOT_CONFIG_RP2040_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms (10*1000UL)
152152
#define AIOT_CONFIG_RP2040_OTA_HTTP_DATA_RECEIVE_TIMEOUT_ms (4*60*1000UL)
153153

154+
#define AIOT_CONFIG_LIB_VERSION "1.5.0"
155+
154156
#endif /* ARDUINO_AIOTC_CONFIG_H_ */

src/ArduinoIoTCloud.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass()
3232
, _tz_dst_until{0}
3333
, _thing_id{""}
3434
, _device_id{""}
35+
, _lib_version{AIOT_CONFIG_LIB_VERSION}
3536
, _cloud_event_callback{nullptr}
3637
, _thing_id_outdated{false}
3738
{

src/ArduinoIoTCloud.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class ArduinoIoTCloudClass
173173
int _tz_offset;
174174
unsigned int _tz_dst_until;
175175
String _thing_id;
176-
176+
String _lib_version;
177177
void execCloudEventCallback(ArduinoIoTCloudEvent const event);
178178

179179
private:

src/ArduinoIoTCloudTCP.cpp

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
250250
_deviceTopicOut = getTopic_deviceout();
251251
_deviceTopicIn = getTopic_devicein();
252252

253+
addPropertyReal(_lib_version, _device_property_container, "LIB_VERSION", Permission::Read);
253254
#if OTA_ENABLED
254255
addPropertyReal(_ota_cap, _device_property_container, "OTA_CAP", Permission::Read);
255256
addPropertyReal(_ota_error, _device_property_container, "OTA_ERROR", Permission::Read);
@@ -260,7 +261,6 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
260261

261262
addPropertyReal(_tz_offset, _thing_property_container, "tz_offset", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
262263
addPropertyReal(_tz_dst_until, _thing_property_container, "tz_dst_until", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
263-
264264
addPropertyReal(_thing_id, _device_property_container, "thing_id", Permission::ReadWrite).onUpdate(setThingIdOutdated);
265265

266266
#if OTA_STORAGE_PORTENTA_QSPI
@@ -428,9 +428,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
428428

429429
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s", __FUNCTION__);
430430

431-
#if OTA_ENABLED
432-
sendOTAPropertiesToCloud();
433-
#endif
431+
sendDevicePropertiesToCloud();
434432
return State::SubscribeDeviceTopic;
435433
}
436434

@@ -653,18 +651,20 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
653651
_ota_error = static_cast<int>(OTAError::None);
654652
/* Clear the request flag. */
655653
_ota_req = false;
656-
/* Transmit the cleared error and request flags to the cloud. */
657-
sendOTAPropertiesToCloud();
654+
/* Transmit the cleared request flags to the cloud. */
655+
sendClearedOTARequestToCloud();
658656
/* Call member function to handle OTA request. */
659657
onOTARequest();
658+
/* If something fails send the OTA error to the cloud */
659+
sendOTAErrorToCloud();
660660
}
661661
}
662662
#endif /* OTA_ENABLED */
663663

664664
/* Check if any properties need encoding and send them to
665665
* the cloud if necessary.
666666
*/
667-
sendPropertiesToCloud();
667+
sendThingPropertiesToCloud();
668668

669669
unsigned long const internal_posix_time = _time_service.getTime();
670670
if(internal_posix_time < _tz_dst_until) {
@@ -700,7 +700,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
700700

701701
/* Topic for OTA properties and device configuration */
702702
if (_deviceTopicIn == topic) {
703-
CBORDecoder::decode(_device_property_container, (uint8_t*)bytes, length);
703+
CBORDecoder::decode(_device_property_container, (uint8_t*)bytes, length, true);
704704
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] device configuration or OTA message received", __FUNCTION__, millis());
705705
_last_device_subscribe_cnt = 0;
706706
_next_device_subscribe_attempt_tick = 0;
@@ -716,7 +716,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
716716
{
717717
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis());
718718
CBORDecoder::decode(_thing_property_container, (uint8_t*)bytes, length, true);
719-
sendPropertiesToCloud();
720719
_time_service.setTimeZoneData(_tz_offset, _tz_dst_until);
721720
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
722721
_last_sync_request_cnt = 0;
@@ -744,19 +743,53 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
744743
}
745744
}
746745

747-
void ArduinoIoTCloudTCP::sendPropertiesToCloud()
746+
void ArduinoIoTCloudTCP::sendThingPropertiesToCloud()
748747
{
749748
sendPropertyContainerToCloud(_dataTopicOut, _thing_property_container);
750749
}
751750

751+
void ArduinoIoTCloudTCP::sendDevicePropertiesToCloud()
752+
{
753+
PropertyContainer ro_device_property_container;
754+
755+
std::list<String> ro_device_property_list {"LIB_VERSION", "OTA_CAP", "OTA_ERROR", "OTA_SHA256"};
756+
std::for_each(ro_device_property_list.begin(),
757+
ro_device_property_list.end(),
758+
[this, &ro_device_property_container ] (String const & name)
759+
{
760+
Property* p = getProperty(this->_device_property_container, name);
761+
if(p != nullptr)
762+
addPropertyToContainer(ro_device_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
763+
}
764+
);
765+
766+
sendPropertyContainerToCloud(_deviceTopicOut, ro_device_property_container);
767+
}
768+
752769
#if OTA_ENABLED
753-
void ArduinoIoTCloudTCP::sendOTAPropertiesToCloud(bool include_ota_req)
770+
void ArduinoIoTCloudTCP::sendClearedOTARequestToCloud()
771+
{
772+
PropertyContainer ota_property_container;
773+
774+
std::list<String> ota_property_list {"OTA_REQ"};
775+
std::for_each(ota_property_list.begin(),
776+
ota_property_list.end(),
777+
[this, &ota_property_container ] (String const & name)
778+
{
779+
Property* p = getProperty(this->_device_property_container, name);
780+
if(p != nullptr)
781+
addPropertyToContainer(ota_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
782+
}
783+
);
784+
785+
sendPropertyContainerToCloud(_deviceTopicOut, ota_property_container);
786+
}
787+
788+
void ArduinoIoTCloudTCP::sendOTAErrorToCloud()
754789
{
755790
PropertyContainer ota_property_container;
756791

757-
std::list<String> ota_property_list {"OTA_CAP", "OTA_ERROR", "OTA_SHA256"};
758-
if (include_ota_req)
759-
ota_property_list.push_back("OTA_REQ");
792+
std::list<String> ota_property_list {"OTA_ERROR"};
760793
std::for_each(ota_property_list.begin(),
761794
ota_property_list.end(),
762795
[this, &ota_property_container ] (String const & name)

src/ArduinoIoTCloudTCP.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,15 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
186186
static void onMessage(int length);
187187
void handleMessage(int length);
188188
void sendPropertyContainerToCloud(String const topic, PropertyContainer & property_container);
189-
void sendPropertiesToCloud();
189+
void sendThingPropertiesToCloud();
190+
void sendDevicePropertiesToCloud();
190191
void requestLastValue();
191192
int write(String const topic, byte const data[], int const length);
192193

193194
#if OTA_ENABLED
194195
void onOTARequest();
195-
void sendOTAPropertiesToCloud(bool include_ota_req = false);
196+
void sendClearedOTARequestToCloud();
197+
void sendOTAErrorToCloud();
196198
#endif
197199

198200
void updateThingTopics();

0 commit comments

Comments
 (0)