Skip to content

Commit e6b252c

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 44167e9 commit e6b252c

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
@@ -262,7 +262,6 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
262262

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

268267
#if OTA_STORAGE_PORTENTA_QSPI
@@ -426,9 +425,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SendDeviceProperties()
426425
return State::Disconnect;
427426
}
428427

429-
#if OTA_ENABLED
430-
sendOTAPropertiesToCloud();
431-
#endif
428+
sendDevicePropertiesToCloud();
432429
return State::SubscribeDeviceTopic;
433430
}
434431

@@ -663,18 +660,26 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
663660
_ota_error = static_cast<int>(OTAError::None);
664661
/* Clear the request flag. */
665662
_ota_req = false;
666-
/* Transmit the cleared error and request flags to the cloud. */
667-
sendOTAPropertiesToCloud();
663+
/* Transmit the cleared request flags to the cloud. */
664+
sendClearedOTARequestToCloud();
668665
/* Call member function to handle OTA request. */
669666
onOTARequest();
667+
/* If something fails send the OTA error to the cloud */
668+
sendOTAErrorToCloud();
670669
}
671670
}
671+
672+
/* Check if we have received the OTA_URL property and provide
673+
* echo to the cloud.
674+
*/
675+
sendOTAUrlToCloud();
676+
672677
#endif /* OTA_ENABLED */
673678

674679
/* Check if any properties need encoding and send them to
675680
* the cloud if necessary.
676681
*/
677-
sendPropertiesToCloud();
682+
sendThingPropertiesToCloud();
678683

679684
unsigned long const internal_posix_time = _time_service.getTime();
680685
if(internal_posix_time < _tz_dst_until) {
@@ -725,7 +730,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
725730
{
726731
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis());
727732
CBORDecoder::decode(_thing_property_container, (uint8_t*)bytes, length, true);
728-
sendPropertiesToCloud();
729733
_time_service.setTimeZoneData(_tz_offset, _tz_dst_until);
730734
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
731735
_last_sync_request_cnt = 0;
@@ -753,20 +757,75 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
753757
}
754758
}
755759

756-
void ArduinoIoTCloudTCP::sendPropertiesToCloud()
760+
void ArduinoIoTCloudTCP::sendThingPropertiesToCloud()
757761
{
758762
sendPropertyContainerToCloud(_dataTopicOut, _thing_property_container, _last_checked_property_index);
759763
}
760764

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

767-
std::list<String> ota_property_list {"LIB_VERSION", "OTA_CAP", "OTA_ERROR", "OTA_SHA256"};
768-
if (include_ota_req)
769-
ota_property_list.push_back("OTA_REQ");
828+
std::list<String> ota_property_list {"OTA_URL"};
770829
std::for_each(ota_property_list.begin(),
771830
ota_property_list.end(),
772831
[this, &ota_property_container ] (String const & name)

src/ArduinoIoTCloudTCP.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,16 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
188188
static void onMessage(int length);
189189
void handleMessage(int length);
190190
void sendPropertyContainerToCloud(String const topic, PropertyContainer & property_container, unsigned int & current_property_index);
191-
void sendPropertiesToCloud();
191+
void sendThingPropertiesToCloud();
192+
void sendDevicePropertiesToCloud();
192193
void requestLastValue();
193194
int write(String const topic, byte const data[], int const length);
194195

195196
#if OTA_ENABLED
196197
void onOTARequest();
197-
void sendOTAPropertiesToCloud(bool include_ota_req = false);
198+
void sendClearedOTARequestToCloud();
199+
void sendOTAErrorToCloud();
200+
void sendOTAUrlToCloud();
198201
#endif
199202

200203
void updateThingTopics();

0 commit comments

Comments
 (0)