Skip to content

Commit 83de5b3

Browse files
committed
Spit sendPropertiesToCloud() in sendThingPropertiesToCloud() and sendDevicePropertiesToCloud().
Provide functions to clear OTA_REQ flag and update OTA_ERROR and OTA_URL.
1 parent 86d07a4 commit 83de5b3

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
261261

262262
addPropertyReal(_tz_offset, _thing_property_container, "tz_offset", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
263263
addPropertyReal(_tz_dst_until, _thing_property_container, "tz_dst_until", Permission::ReadWrite).onSync(CLOUD_WINS).onUpdate(updateTimezoneInfo);
264-
265264
addPropertyReal(_thing_id, _device_property_container, "thing_id", Permission::ReadWrite).onUpdate(setThingIdOutdated);
266265

267266
#if OTA_STORAGE_PORTENTA_QSPI
@@ -419,9 +418,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
419418
return State::Disconnect;
420419
}
421420

422-
#if OTA_ENABLED
423-
sendOTAPropertiesToCloud();
424-
#endif
421+
sendDevicePropertiesToCloud();
425422
return State::SubscribeDeviceTopic;
426423
}
427424

@@ -656,18 +653,26 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
656653
_ota_error = static_cast<int>(OTAError::None);
657654
/* Clear the request flag. */
658655
_ota_req = false;
659-
/* Transmit the cleared error and request flags to the cloud. */
660-
sendOTAPropertiesToCloud();
656+
/* Transmit the cleared request flags to the cloud. */
657+
sendClearedOTARequestToCloud();
661658
/* Call member function to handle OTA request. */
662659
onOTARequest();
660+
/* If something fails send the OTA error to the cloud */
661+
sendOTAErrorToCloud();
663662
}
664663
}
664+
665+
/* Check if we have received the OTA_URL property and provide
666+
* echo to the cloud.
667+
*/
668+
sendOTAUrlToCloud();
669+
665670
#endif /* OTA_ENABLED */
666671

667672
/* Check if any properties need encoding and send them to
668673
* the cloud if necessary.
669674
*/
670-
sendPropertiesToCloud();
675+
sendThingPropertiesToCloud();
671676

672677
unsigned long const internal_posix_time = _time_service.getTime();
673678
if(internal_posix_time < _tz_dst_until) {
@@ -718,7 +723,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
718723
{
719724
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis());
720725
CBORDecoder::decode(_thing_property_container, (uint8_t*)bytes, length, true);
721-
sendPropertiesToCloud();
722726
_time_service.setTimeZoneData(_tz_offset, _tz_dst_until);
723727
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
724728
_last_sync_request_cnt = 0;
@@ -746,20 +750,75 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
746750
}
747751
}
748752

749-
void ArduinoIoTCloudTCP::sendPropertiesToCloud()
753+
void ArduinoIoTCloudTCP::sendThingPropertiesToCloud()
750754
{
751755
sendPropertyContainerToCloud(_dataTopicOut, _thing_property_container, _last_checked_property_index);
752756
}
753757

758+
void ArduinoIoTCloudTCP::sendDevicePropertiesToCloud()
759+
{
760+
PropertyContainer ro_device_property_container;
761+
unsigned int last_device_property_index = 0;
762+
763+
std::list<String> ro_device_property_list {"LIB_VERSION", "OTA_CAP", "OTA_ERROR", "OTA_SHA256"};
764+
std::for_each(ro_device_property_list.begin(),
765+
ro_device_property_list.end(),
766+
[this, &ro_device_property_container ] (String const & name)
767+
{
768+
Property* p = getProperty(this->_device_property_container, name);
769+
if(p != nullptr)
770+
addPropertyToContainer(ro_device_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
771+
}
772+
);
773+
774+
sendPropertyContainerToCloud(_deviceTopicOut, ro_device_property_container, last_device_property_index);
775+
}
776+
754777
#if OTA_ENABLED
755-
void ArduinoIoTCloudTCP::sendOTAPropertiesToCloud(bool include_ota_req)
778+
void ArduinoIoTCloudTCP::sendClearedOTARequestToCloud()
779+
{
780+
PropertyContainer ota_property_container;
781+
unsigned int last_ota_property_index = 0;
782+
783+
std::list<String> ota_property_list {"OTA_REQ"};
784+
std::for_each(ota_property_list.begin(),
785+
ota_property_list.end(),
786+
[this, &ota_property_container ] (String const & name)
787+
{
788+
Property* p = getProperty(this->_device_property_container, name);
789+
if(p != nullptr)
790+
addPropertyToContainer(ota_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
791+
}
792+
);
793+
794+
sendPropertyContainerToCloud(_deviceTopicOut, ota_property_container, last_ota_property_index);
795+
}
796+
797+
void ArduinoIoTCloudTCP::sendOTAErrorToCloud()
798+
{
799+
PropertyContainer ota_property_container;
800+
unsigned int last_ota_property_index = 0;
801+
802+
std::list<String> ota_property_list {"OTA_ERROR"};
803+
std::for_each(ota_property_list.begin(),
804+
ota_property_list.end(),
805+
[this, &ota_property_container ] (String const & name)
806+
{
807+
Property* p = getProperty(this->_device_property_container, name);
808+
if(p != nullptr)
809+
addPropertyToContainer(ota_property_container, *p, p->name(), p->isWriteableByCloud() ? Permission::ReadWrite : Permission::Read);
810+
}
811+
);
812+
813+
sendPropertyContainerToCloud(_deviceTopicOut, ota_property_container, last_ota_property_index);
814+
}
815+
816+
void ArduinoIoTCloudTCP::sendOTAUrlToCloud()
756817
{
757818
PropertyContainer ota_property_container;
758819
unsigned int last_ota_property_index = 0;
759820

760-
std::list<String> ota_property_list {"LIB_VERSION", "OTA_CAP", "OTA_ERROR", "OTA_SHA256"};
761-
if (include_ota_req)
762-
ota_property_list.push_back("OTA_REQ");
821+
std::list<String> ota_property_list {"OTA_URL"};
763822
std::for_each(ota_property_list.begin(),
764823
ota_property_list.end(),
765824
[this, &ota_property_container ] (String const & name)

src/ArduinoIoTCloudTCP.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,16 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
186186
static void onMessage(int length);
187187
void handleMessage(int length);
188188
void sendPropertyContainerToCloud(String const topic, PropertyContainer & property_container, unsigned int & current_property_index);
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();
198+
void sendOTAUrlToCloud();
196199
#endif
197200

198201
void updateThingTopics();

0 commit comments

Comments
 (0)