Skip to content

Commit 9cc43f7

Browse files
authored
Merge pull request #297 from pennam/thing_id_discovery
Thing id discovery protocol
2 parents b0b75cd + 93d6fa8 commit 9cc43f7

6 files changed

+385
-118
lines changed

src/AIoTC_Config.h

+13-8
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,19 @@
140140
* CONSTANTS
141141
******************************************************************************/
142142

143-
#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
144-
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
145-
#define AIOT_CONFIG_SUBSCRIBE_RETRY_DELAY_ms (1000UL)
146-
#define AIOT_CONFIG_SUBSCRIBE_MAX_RETRY_CNT (10UL)
147-
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
148-
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)
149-
150-
#define AIOT_CONFIG_RP2040_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms (10*1000UL)
143+
#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
144+
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
145+
#define AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (5*1000UL)
146+
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms (32000UL)
147+
#define AIOT_CONFIG_THING_TOPICS_SUBSCRIBE_RETRY_DELAY_ms (1000UL)
148+
#define AIOT_CONFIG_THING_TOPICS_SUBSCRIBE_MAX_RETRY_CNT (10UL)
149+
#define AIOT_CONFIG_MAX_DEVICE_TOPIC_ATTACH_RETRY_DELAY_ms (1280000UL)
150+
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (30000UL)
151+
#define AIOT_CONFIG_LASTVALUES_SYNC_MAX_RETRY_CNT (10UL)
152+
153+
#define AIOT_CONFIG_RP2040_OTA_HTTP_HEADER_RECEIVE_TIMEOUT_ms (10*1000UL)
151154
#define AIOT_CONFIG_RP2040_OTA_HTTP_DATA_RECEIVE_TIMEOUT_ms (4*60*1000UL)
152155

156+
#define AIOT_CONFIG_LIB_VERSION "1.5.0"
157+
153158
#endif /* ARDUINO_AIOTC_CONFIG_H_ */

src/ArduinoIoTCloud.cpp

+78-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass()
3333
, _tz_dst_until{0}
3434
, _thing_id{""}
3535
, _device_id{""}
36+
, _lib_version{AIOT_CONFIG_LIB_VERSION}
3637
, _cloud_event_callback{nullptr}
38+
, _thing_id_outdated{false}
3739
{
3840

3941
}
@@ -44,12 +46,12 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass()
4446

4547
void ArduinoIoTCloudClass::push()
4648
{
47-
requestUpdateForAllProperties(_property_container);
49+
requestUpdateForAllProperties(_thing_property_container);
4850
}
4951

5052
bool ArduinoIoTCloudClass::setTimestamp(String const & prop_name, unsigned long const timestamp)
5153
{
52-
Property * p = getProperty(_property_container, prop_name);
54+
Property * p = getProperty(_thing_property_container, prop_name);
5355

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

8385
if (seconds == ON_CHANGE) {
84-
addPropertyToContainer(_property_container, property, name, permission, tag).publishOnChange(minDelta, Property::DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn);
86+
addPropertyToContainer(_thing_property_container, property, name, permission, tag).publishOnChange(minDelta, Property::DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS).onUpdate(fn).onSync(synFn);
8587
} else {
86-
addPropertyToContainer(_property_container, property, name, permission, tag).publishEvery(seconds).onUpdate(fn).onSync(synFn);
88+
addPropertyToContainer(_thing_property_container, property, name, permission, tag).publishEvery(seconds).onUpdate(fn).onSync(synFn);
8789
}
8890
}
8991

9092
Property& ArduinoIoTCloudClass::addPropertyReal(Property& property, String name, Permission const permission)
9193
{
92-
return addPropertyToContainer(_property_container, property, name, permission);
94+
return addPropertyToContainer(_thing_property_container, property, name, permission);
9395
}
9496

9597
Property& ArduinoIoTCloudClass::addPropertyReal(Property& property, String name, int tag, Permission const permission)
9698
{
97-
return addPropertyToContainer(_property_container, property, name, permission, tag);
99+
return addPropertyToContainer(_thing_property_container, property, name, permission, tag);
100+
}
101+
102+
Property& ArduinoIoTCloudClass::addPropertyReal(Property& property, PropertyContainer &prop_cont, String name, Permission const permission)
103+
{
104+
return addPropertyToContainer(prop_cont, property, name, permission, -1);
105+
}
106+
107+
Property& ArduinoIoTCloudClass::addPropertyReal(Property& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
108+
{
109+
return addPropertyToContainer(prop_cont, property, name, permission, tag);
98110
}
99111

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

111123
Property& ArduinoIoTCloudClass::addPropertyReal(bool& property, String name, Permission const permission)
112124
{
113-
return addPropertyReal(property, name, -1, permission);
125+
return addPropertyReal(property, _thing_property_container, name, -1, permission);
114126
}
115127

116128
Property& ArduinoIoTCloudClass::addPropertyReal(bool& property, String name, int tag, Permission const permission)
129+
{
130+
return addPropertyReal(property, _thing_property_container, name, tag, permission);
131+
}
132+
133+
Property& ArduinoIoTCloudClass::addPropertyReal(bool& property, PropertyContainer &prop_cont, String name, Permission const permission)
134+
{
135+
return addPropertyReal(property, prop_cont, name, -1, permission);
136+
}
137+
138+
Property& ArduinoIoTCloudClass::addPropertyReal(bool& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
117139
{
118140
Property* p = new CloudWrapperBool(property);
119-
return addPropertyToContainer(_property_container, *p, name, permission, tag);
141+
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
120142
}
121143

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

133155
Property& ArduinoIoTCloudClass::addPropertyReal(float& property, String name, Permission const permission)
134156
{
135-
return addPropertyReal(property, name, -1, permission);
157+
return addPropertyReal(property, _thing_property_container, name, -1, permission);
136158
}
137159

138160
Property& ArduinoIoTCloudClass::addPropertyReal(float& property, String name, int tag, Permission const permission)
161+
{
162+
return addPropertyReal(property, _thing_property_container, name, tag, permission);
163+
}
164+
165+
Property& ArduinoIoTCloudClass::addPropertyReal(float& property, PropertyContainer &prop_cont, String name, Permission const permission)
166+
{
167+
return addPropertyReal(property, prop_cont, name, -1, permission);
168+
}
169+
170+
Property& ArduinoIoTCloudClass::addPropertyReal(float& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
139171
{
140172
Property* p = new CloudWrapperFloat(property);
141-
return addPropertyToContainer(_property_container, *p, name, permission, tag);
173+
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
142174
}
143175

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

155187
Property& ArduinoIoTCloudClass::addPropertyReal(int& property, String name, Permission const permission)
156188
{
157-
return addPropertyReal(property, name, -1, permission);
189+
return addPropertyReal(property, _thing_property_container, name, -1, permission);
158190
}
159191

160192
Property& ArduinoIoTCloudClass::addPropertyReal(int& property, String name, int tag, Permission const permission)
193+
{
194+
return addPropertyReal(property, _thing_property_container, name, tag, permission);
195+
}
196+
197+
Property& ArduinoIoTCloudClass::addPropertyReal(int& property, PropertyContainer &prop_cont, String name, Permission const permission)
198+
{
199+
return addPropertyReal(property, prop_cont, name, -1, permission);
200+
}
201+
202+
Property& ArduinoIoTCloudClass::addPropertyReal(int& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
161203
{
162204
Property* p = new CloudWrapperInt(property);
163-
return addPropertyToContainer(_property_container, *p, name, permission, tag);
205+
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
164206
}
165207

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

177219
Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, Permission const permission)
178220
{
179-
return addPropertyReal(property, name, -1, permission);
221+
return addPropertyReal(property, _thing_property_container, name, -1, permission);
180222
}
181223

182224
Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, String name, int tag, Permission const permission)
225+
{
226+
return addPropertyReal(property, _thing_property_container, name, tag, permission);
227+
}
228+
229+
Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, PropertyContainer &prop_cont, String name, Permission const permission)
230+
{
231+
return addPropertyReal(property, prop_cont, name, -1, permission);
232+
}
233+
234+
Property& ArduinoIoTCloudClass::addPropertyReal(unsigned int& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
183235
{
184236
Property* p = new CloudWrapperUnsignedInt(property);
185-
return addPropertyToContainer(_property_container, *p, name, permission, tag);
237+
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
186238
}
187239

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

199251
Property& ArduinoIoTCloudClass::addPropertyReal(String& property, String name, Permission const permission)
200252
{
201-
return addPropertyReal(property, name, -1, permission);
253+
return addPropertyReal(property, _thing_property_container, name, -1, permission);
202254
}
203255

204256
Property& ArduinoIoTCloudClass::addPropertyReal(String& property, String name, int tag, Permission const permission)
257+
{
258+
return addPropertyReal(property, _thing_property_container, name, tag, permission);
259+
}
260+
261+
Property& ArduinoIoTCloudClass::addPropertyReal(String& property, PropertyContainer &prop_cont, String name, Permission const permission)
262+
{
263+
return addPropertyReal(property, prop_cont, name, -1, permission);
264+
}
265+
266+
Property& ArduinoIoTCloudClass::addPropertyReal(String& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission)
205267
{
206268
Property* p = new CloudWrapperString(property);
207-
return addPropertyToContainer(_property_container, *p, name, permission, tag);
269+
return addPropertyToContainer(prop_cont, *p, name, permission, tag);
208270
}
209271

210272
/******************************************************************************

src/ArduinoIoTCloud.h

+25-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ class ArduinoIoTCloudClass
9696
inline void setDeviceId(String const device_id) { _device_id = device_id; };
9797
inline String & getDeviceId() { return _device_id; };
9898

99+
inline void setThingIdOutdatedFlag() { _thing_id_outdated = true ; }
100+
inline void clrThingIdOutdatedFlag() { _thing_id_outdated = false ; }
101+
inline bool getThingIdOutdatedFlag() { return _thing_id_outdated; }
102+
103+
inline bool deviceNotAttached() { return _thing_id == ""; }
104+
99105
inline ConnectionHandler * getConnection() { return _connection; }
100106

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

133+
Property& addPropertyReal(Property& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
134+
Property& addPropertyReal(bool& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
135+
Property& addPropertyReal(float& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
136+
Property& addPropertyReal(int& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
137+
Property& addPropertyReal(unsigned int& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
138+
Property& addPropertyReal(String& property, PropertyContainer &prop_cont, String name, int tag, Permission const permission);
139+
140+
Property& addPropertyReal(Property& property, PropertyContainer &prop_cont, String name, Permission const permission);
141+
Property& addPropertyReal(bool& property, PropertyContainer &prop_cont, String name, Permission const permission);
142+
Property& addPropertyReal(float& property, PropertyContainer &prop_cont, String name, Permission const permission);
143+
Property& addPropertyReal(int& property, PropertyContainer &prop_cont, String name, Permission const permission);
144+
Property& addPropertyReal(unsigned int& property, PropertyContainer &prop_cont, String name, Permission const permission);
145+
Property& addPropertyReal(String& property, PropertyContainer &prop_cont, String name, Permission const permission);
146+
127147
/* The following methods are for MKR WAN 1300/1310 LoRa boards since
128148
* they use a number to identify a given property within a CBOR message.
129149
* This approach reduces the required amount of data which is of great
@@ -147,19 +167,22 @@ class ArduinoIoTCloudClass
147167
protected:
148168

149169
ConnectionHandler * _connection;
150-
PropertyContainer _property_container;
170+
PropertyContainer _device_property_container;
171+
PropertyContainer _thing_property_container;
151172
unsigned int _last_checked_property_index;
152173
TimeService & _time_service;
153174
int _tz_offset;
154175
unsigned int _tz_dst_until;
176+
String _thing_id;
177+
String _lib_version;
155178

156179
void execCloudEventCallback(ArduinoIoTCloudEvent const event);
157180

158181
private:
159182

160-
String _thing_id;
161183
String _device_id;
162184
OnCloudEventCallback _cloud_event_callback[3];
185+
bool _thing_id_outdated;
163186
};
164187

165188
#ifdef HAS_TCP

src/ArduinoIoTCloudLPWAN.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static size_t const CBOR_LORA_MSG_MAX_SIZE = 255;
3737
LOCAL MODULE FUNCTIONS
3838
******************************************************************************/
3939

40-
extern "C" unsigned long getTime()
40+
unsigned long getTime()
4141
{
4242
return ArduinoCloud.getInternalTime();
4343
}
@@ -120,8 +120,8 @@ ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_Connected()
120120
}
121121

122122
/* Check if a primitive property wrapper is locally changed. */
123-
updateTimestampOnLocallyChangedProperties(_property_container);
124-
123+
updateTimestampOnLocallyChangedProperties(_thing_property_container);
124+
125125
/* Decode available data. */
126126
if (_connection->available())
127127
decodePropertiesFromCloud();
@@ -142,15 +142,15 @@ void ArduinoIoTCloudLPWAN::decodePropertiesFromCloud()
142142
{
143143
lora_msg_buf[bytes_received] = _connection->read();
144144
}
145-
CBORDecoder::decode(_property_container, lora_msg_buf, bytes_received);
145+
CBORDecoder::decode(_thing_property_container, lora_msg_buf, bytes_received);
146146
}
147147

148148
void ArduinoIoTCloudLPWAN::sendPropertiesToCloud()
149149
{
150150
int bytes_encoded = 0;
151151
uint8_t data[CBOR_LORA_MSG_MAX_SIZE];
152152

153-
if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, _last_checked_property_index, true) == CborNoError)
153+
if (CBOREncoder::encode(_thing_property_container, data, sizeof(data), bytes_encoded, _last_checked_property_index, true) == CborNoError)
154154
if (bytes_encoded > 0)
155155
writeProperties(data, bytes_encoded);
156156
}

0 commit comments

Comments
 (0)