Skip to content

Thing id discovery protocol #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fc0ddb9
Split _property_container in _device_property_container and _thing_pr…
pennam Feb 3, 2022
1af4701
ArduinoIoTCloudTCP state machine changes to handle thig_id discovery …
pennam Jan 11, 2022
f4af0d2
If board has a valid _dataTopicIn also _shadowTopicIn is valid
pennam Dec 7, 2021
79656c4
Add configuration flag to sendOTAPropertiesToCloud() fuction in order…
pennam Dec 10, 2021
48494fa
Execute handle_CheckDeviceConfig() only on thing_id changes ignoring …
pennam Dec 10, 2021
65b82e6
Do not try to reconfigure device if connection is dropped
pennam Dec 23, 2021
850095d
There is no more distinction between not configured and not attached
pennam Jan 10, 2022
08964b3
Move thing_id outdated flag check outside update function and add han…
pennam Jan 10, 2022
95eeec8
Use specific macros to define device topic subscription delays
pennam Feb 3, 2022
ae45d0c
Use incremental long delay before retry subscribtion when the device …
pennam Jan 11, 2022
221b176
Add LIB_VERSION property to let cloud decide to use thing_topic or de…
pennam Jan 24, 2022
4aa628a
Spit sendPropertiesToCloud() in sendThingPropertiesToCloud() and send…
pennam Jan 24, 2022
0ebefe8
Remove the onSync callback end event from device properties
pennam Jan 24, 2022
366bde1
Use a single function to keep OTA properties in sync with cloud
pennam Jan 24, 2022
e0a1d5a
Use better define name to distinguish between topic and device SUBSCR…
pennam Jan 24, 2022
12d3b26
Remove extern C declaration from local module functions
pennam Feb 3, 2022
c35be6f
Do not print Thing ID at startup because its value will be ignored
pennam Feb 3, 2022
c9cd3f5
Print Thing ID on serial monitor when board connects to the cloud
pennam Feb 3, 2022
d373405
Call DISCONNECT event when device is detached from a thing and print …
pennam Feb 3, 2022
4a121ae
Add DEBUG_ERROR if device fails to subscribe to _deviceTopicIn
pennam Feb 8, 2022
93d6fa8
Add DEBUG_ERROR if device is not receiving a valid thing_id from the …
pennam Feb 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/AIoTC_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,19 @@
* CONSTANTS
******************************************************************************/

#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
#define AIOT_CONFIG_SUBSCRIBE_RETRY_DELAY_ms (1000UL)
#define AIOT_CONFIG_SUBSCRIBE_MAX_RETRY_CNT (10UL)
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)

#define AIOT_CONFIG_RP2040_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms (10*1000UL)
#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
#define AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (5*1000UL)
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (32000UL)
#define AIOT_CONFIG_THING_TOPICS_SUBSCRIBE_RETRY_DELAY_ms (1000UL)
#define AIOT_CONFIG_THING_TOPICS_SUBSCRIBE_MAX_RETRY_CNT (10UL)
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms (1280000UL)
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)

#define AIOT_CONFIG_RP2040_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms (10*1000UL)
#define AIOT_CONFIG_RP2040_OTA_HTTP_DATA_RECEIVE_TIMEOUT_ms (4*60*1000UL)

#define AIOT_CONFIG_LIB_VERSION "1.5.0"

#endif /* ARDUINO_AIOTC_CONFIG_H_ */
94 changes: 78 additions & 16 deletions src/ArduinoIoTCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass()
, _tz_dst_until{0}
, _thing_id{""}
, _device_id{""}
, _lib_version{AIOT_CONFIG_LIB_VERSION}
, _cloud_event_callback{nullptr}
, _thing_id_outdated{false}
{

}
Expand All @@ -44,12 +46,12 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass()

void ArduinoIoTCloudClass::push()
{
requestUpdateForAllProperties(_property_container);
requestUpdateForAllProperties(_thing_property_container);
}

bool ArduinoIoTCloudClass::setTimestamp(String const & prop_name, unsigned long const timestamp)
{
Property * p = getProperty(_property_container, prop_name);
Property * p = getProperty(_thing_property_container, prop_name);

if (p == nullptr)
return false;
Expand Down Expand Up @@ -81,20 +83,30 @@ void ArduinoIoTCloudClass::addPropertyReal(Property& property, String name, int
}

if (seconds == ON_CHANGE) {
addPropertyToContainer(_property_container, property, name, permission, tag).publishOnChange(minDelta, Property::DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn);
addPropertyToContainer(_thing_property_container, property, name, permission, tag).publishOnChange(minDelta, Property::DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn);
} else {
addPropertyToContainer(_property_container, property, name, permission, tag).publishEvery(seconds).onUpdate(fn).onSync(synFn);
addPropertyToContainer(_thing_property_container, property, name, permission, tag).publishEvery(seconds).onUpdate(fn).onSync(synFn);
}
}

Property& ArduinoIoTCloudClass::addPropertyReal(Property& property, String name, Permission const permission)
{
return addPropertyToContainer(_property_container, property, name, permission);
return addPropertyToContainer(_thing_property_container, property, name, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(Property& property, String name, int tag, Permission const permission)
{
return addPropertyToContainer(_property_container, property, name, permission, tag);
return addPropertyToContainer(_thing_property_container, property, name, permission, tag);
}

Property& ArduinoIoTCloudClass::addPropertyReal(Property& property, PropertyContainer &prop_cont, String name, Permission const permission)
{
return addPropertyToContainer(prop_cont, property, name, permission, -1);
}

Property& ArduinoIoTCloudClass::addPropertyReal(Property& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
{
return addPropertyToContainer(prop_cont, property, name, permission, tag);
}

void ArduinoIoTCloudClass::addPropertyReal(bool& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property))
Expand All @@ -110,13 +122,23 @@ void ArduinoIoTCloudClass::addPropertyReal(bool& property, String name, int tag,

Property& ArduinoIoTCloudClass::addPropertyReal(bool& property, String name, Permission const permission)
{
return addPropertyReal(property, name, -1, permission);
return addPropertyReal(property, _thing_property_container, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(bool& property, String name, int tag, Permission const permission)
{
return addPropertyReal(property, _thing_property_container, name, tag, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(bool& property, PropertyContainer &prop_cont, String name, Permission const permission)
{
return addPropertyReal(property, prop_cont, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(bool& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
{
Property* p = new CloudWrapperBool(property);
return addPropertyToContainer(_property_container, *p, name, permission, tag);
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
}

void ArduinoIoTCloudClass::addPropertyReal(float& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property))
Expand All @@ -132,13 +154,23 @@ void ArduinoIoTCloudClass::addPropertyReal(float& property, String name, int tag

Property& ArduinoIoTCloudClass::addPropertyReal(float& property, String name, Permission const permission)
{
return addPropertyReal(property, name, -1, permission);
return addPropertyReal(property, _thing_property_container, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(float& property, String name, int tag, Permission const permission)
{
return addPropertyReal(property, _thing_property_container, name, tag, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(float& property, PropertyContainer &prop_cont, String name, Permission const permission)
{
return addPropertyReal(property, prop_cont, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(float& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
{
Property* p = new CloudWrapperFloat(property);
return addPropertyToContainer(_property_container, *p, name, permission, tag);
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
}

void ArduinoIoTCloudClass::addPropertyReal(int& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property))
Expand All @@ -154,13 +186,23 @@ void ArduinoIoTCloudClass::addPropertyReal(int& property, String name, int tag,

Property& ArduinoIoTCloudClass::addPropertyReal(int& property, String name, Permission const permission)
{
return addPropertyReal(property, name, -1, permission);
return addPropertyReal(property, _thing_property_container, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(int& property, String name, int tag, Permission const permission)
{
return addPropertyReal(property, _thing_property_container, name, tag, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(int& property, PropertyContainer &prop_cont, String name, Permission const permission)
{
return addPropertyReal(property, prop_cont, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(int& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
{
Property* p = new CloudWrapperInt(property);
return addPropertyToContainer(_property_container, *p, name, permission, tag);
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
}

void ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property))
Expand All @@ -176,13 +218,23 @@ void ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name,

Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, Permission const permission)
{
return addPropertyReal(property, name, -1, permission);
return addPropertyReal(property, _thing_property_container, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, int tag, Permission const permission)
{
return addPropertyReal(property, _thing_property_container, name, tag, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, PropertyContainer &prop_cont, String name, Permission const permission)
{
return addPropertyReal(property, prop_cont, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
{
Property* p = new CloudWrapperUnsignedInt(property);
return addPropertyToContainer(_property_container, *p, name, permission, tag);
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
}

void ArduinoIoTCloudClass::addPropertyReal(String& property, String name, permissionType permission_type, long seconds, void(*fn)(void), float minDelta, void(*synFn)(Property & property))
Expand All @@ -198,13 +250,23 @@ void ArduinoIoTCloudClass::addPropertyReal(String& property, String name, int ta

Property& ArduinoIoTCloudClass::addPropertyReal(String& property, String name, Permission const permission)
{
return addPropertyReal(property, name, -1, permission);
return addPropertyReal(property, _thing_property_container, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(String& property, String name, int tag, Permission const permission)
{
return addPropertyReal(property, _thing_property_container, name, tag, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(String& property, PropertyContainer &prop_cont, String name, Permission const permission)
{
return addPropertyReal(property, prop_cont, name, -1, permission);
}

Property& ArduinoIoTCloudClass::addPropertyReal(String& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
{
Property* p = new CloudWrapperString(property);
return addPropertyToContainer(_property_container, *p, name, permission, tag);
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
}

/******************************************************************************
Expand Down
27 changes: 25 additions & 2 deletions src/ArduinoIoTCloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class ArduinoIoTCloudClass
inline void setDeviceId(String const device_id) { _device_id = device_id; };
inline String & getDeviceId() { return _device_id; };

inline void setThingIdOutdatedFlag() { _thing_id_outdated = true ; }
inline void clrThingIdOutdatedFlag() { _thing_id_outdated = false ; }
inline bool getThingIdOutdatedFlag() { return _thing_id_outdated; }

inline bool deviceNotAttached() { return _thing_id == ""; }

inline ConnectionHandler * getConnection() { return _connection; }

inline unsigned long getInternalTime() { return _time_service.getTime(); }
Expand Down Expand Up @@ -124,6 +130,20 @@ class ArduinoIoTCloudClass
Property& addPropertyReal(unsigned int& property, String name, Permission const permission);
Property& addPropertyReal(String& property, String name, Permission const permission);

Property& addPropertyReal(Property& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
Property& addPropertyReal(bool& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
Property& addPropertyReal(float& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
Property& addPropertyReal(int& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
Property& addPropertyReal(unsigned int& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
Property& addPropertyReal(String& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);

Property& addPropertyReal(Property& property, PropertyContainer &prop_cont, String name, Permission const permission);
Property& addPropertyReal(bool& property, PropertyContainer &prop_cont, String name, Permission const permission);
Property& addPropertyReal(float& property, PropertyContainer &prop_cont, String name, Permission const permission);
Property& addPropertyReal(int& property, PropertyContainer &prop_cont, String name, Permission const permission);
Property& addPropertyReal(unsigned int& property, PropertyContainer &prop_cont, String name, Permission const permission);
Property& addPropertyReal(String& property, PropertyContainer &prop_cont, String name, Permission const permission);

/* The following methods are for MKR WAN 1300/1310 LoRa boards since
* they use a number to identify a given property within a CBOR message.
* This approach reduces the required amount of data which is of great
Expand All @@ -147,19 +167,22 @@ class ArduinoIoTCloudClass
protected:

ConnectionHandler * _connection;
PropertyContainer _property_container;
PropertyContainer _device_property_container;
PropertyContainer _thing_property_container;
unsigned int _last_checked_property_index;
TimeService & _time_service;
int _tz_offset;
unsigned int _tz_dst_until;
String _thing_id;
String _lib_version;

void execCloudEventCallback(ArduinoIoTCloudEvent const event);

private:

String _thing_id;
String _device_id;
OnCloudEventCallback _cloud_event_callback[3];
bool _thing_id_outdated;
};

#ifdef HAS_TCP
Expand Down
10 changes: 5 additions & 5 deletions src/ArduinoIoTCloudLPWAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static size_t const CBOR_LORA_MSG_MAX_SIZE = 255;
LOCAL MODULE FUNCTIONS
******************************************************************************/

extern "C" unsigned long getTime()
unsigned long getTime()
{
return ArduinoCloud.getInternalTime();
}
Expand Down Expand Up @@ -120,8 +120,8 @@ ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_Connected()
}

/* Check if a primitive property wrapper is locally changed. */
updateTimestampOnLocallyChangedProperties(_property_container);
updateTimestampOnLocallyChangedProperties(_thing_property_container);

/* Decode available data. */
if (_connection->available())
decodePropertiesFromCloud();
Expand All @@ -142,15 +142,15 @@ void ArduinoIoTCloudLPWAN::decodePropertiesFromCloud()
{
lora_msg_buf[bytes_received] = _connection->read();
}
CBORDecoder::decode(_property_container, lora_msg_buf, bytes_received);
CBORDecoder::decode(_thing_property_container, lora_msg_buf, bytes_received);
}

void ArduinoIoTCloudLPWAN::sendPropertiesToCloud()
{
int bytes_encoded = 0;
uint8_t data[CBOR_LORA_MSG_MAX_SIZE];

if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, _last_checked_property_index, true) == CborNoError)
if (CBOREncoder::encode(_thing_property_container, data, sizeof(data), bytes_encoded, _last_checked_property_index, true) == CborNoError)
if (bytes_encoded > 0)
writeProperties(data, bytes_encoded);
}
Expand Down
Loading