Skip to content

Commit 4aa628a

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 221b176 commit 4aa628a

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

src/ArduinoIoTCloudTCP.cpp

+72-13
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;
@@ -745,20 +749,75 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
745749
}
746750
}
747751

748-
void ArduinoIoTCloudTCP::sendPropertiesToCloud()
752+
void ArduinoIoTCloudTCP::sendThingPropertiesToCloud()
749753
{
750754
sendPropertyContainerToCloud(_dataTopicOut, _thing_property_container, _last_checked_property_index);
751755
}
752756

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

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

src/ArduinoIoTCloudTCP.h

+5-2
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)