Skip to content

Commit d69dfd5

Browse files
pennamandreagilardoni
authored andcommitted
ArduinoIoTCloudTCP: switch to messages
1 parent 3e281fa commit d69dfd5

File tree

2 files changed

+69
-35
lines changed

2 files changed

+69
-35
lines changed

src/ArduinoIoTCloudTCP.cpp

+60-20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <algorithm>
4444
#include "cbor/CBOREncoder.h"
4545
#include "utility/watchdog/Watchdog.h"
46+
#include <typeinfo>
4647

4748
/******************************************************************************
4849
LOCAL MODULE FUNCTIONS
@@ -74,10 +75,8 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
7475
, _password("")
7576
#endif
7677
, _mqttClient{nullptr}
77-
, _deviceTopicOut("")
78-
, _deviceTopicIn("")
79-
, _shadowTopicOut("")
80-
, _shadowTopicIn("")
78+
, _messageTopicOut("")
79+
, _messageTopicIn("")
8180
, _dataTopicOut("")
8281
, _dataTopicIn("")
8382
#if OTA_ENABLED
@@ -181,6 +180,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
181180
_mqttClient.setUsernamePassword(getDeviceId(), _password);
182181
}
183182
#endif
183+
184184
_mqttClient.onMessage(ArduinoIoTCloudTCP::onMessage);
185185
_mqttClient.setKeepAliveInterval(30 * 1000);
186186
_mqttClient.setConnectionTimeout(1500);
@@ -189,11 +189,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
189189
_deviceTopicOut = getTopic_deviceout();
190190
_deviceTopicIn = getTopic_devicein();
191191

192-
Property* p;
193-
p = new CloudWrapperString(_lib_version);
194-
addPropertyToContainer(_device.getPropertyContainer(), *p, "LIB_VERSION", Permission::Read, -1);
195-
p = new CloudWrapperString(_thing_id);
196-
_thing_id_property = &addPropertyToContainer(_device.getPropertyContainer(), *p, "thing_id", Permission::ReadWrite, -1).writeOnDemand();
192+
_messageTopicOut = getTopic_messageout();
193+
_messageTopicIn = getTopic_messagein();
197194

198195
_thing.begin();
199196
_device.begin();
@@ -322,6 +319,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
322319
{
323320
if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort))
324321
{
322+
_mqttClient.subscribe(getTopic_messagein());
325323
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s connected to %s:%d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort);
326324
/* Reconfigure timers for next state */
327325
_connection_attempt.begin(AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms, AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
@@ -465,19 +463,50 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
465463

466464
/* Topic for user input data */
467465
if (_dataTopicIn == topic) {
468-
CBORDecoder::decode(_thing.getPropertyContainer(), (uint8_t*)bytes, length);
466+
CBORDecoder::decode(getThing().getPropertyContainer(), (uint8_t*)bytes, length);
469467
}
470468

471-
/* Topic for sync Thing last values on connect */
472-
if (_shadowTopicIn == topic) {
473-
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis());
474-
/* Decode last values property array */
475-
CBORDecoder::decode(_thing.getPropertyContainer(), (uint8_t*)bytes, length, true);
476-
/* Unlock thing state machine waiting last values */
477-
Message message = { LastValuesUpdateCmdId };
478-
_thing.handleMessage(&message);
479-
/* Call ArduinoIoTCloud sync user callback*/
480-
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
469+
/* Topic for device commands */
470+
if (_messageTopicIn == topic) {
471+
CommandDown command;
472+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] received %d bytes", __FUNCTION__, millis(), length);
473+
CBORMessageDecoder decoder;
474+
475+
size_t buffer_length = length;
476+
if (decoder.decode((Message*)&command, bytes, buffer_length) != Decoder::Status::Error) {
477+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] received command id %d", __FUNCTION__, millis(), command.c.id);
478+
switch (command.c.id)
479+
{
480+
case CommandId::ThingUpdateCmdId:
481+
{
482+
_thing_id = String(command.thingBeginCmd.params.thing_id);
483+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] device configuration received", __FUNCTION__, millis());
484+
_device.handleMessage((Message*)&command);
485+
}
486+
break;
487+
488+
case CommandId::LastValuesUpdateCmdId:
489+
{
490+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis());
491+
CBORDecoder::decode(getThing().getPropertyContainer(),
492+
(uint8_t*)command.lastValuesUpdateCmd.params.last_values,
493+
command.lastValuesUpdateCmd.params.length, true);
494+
_thing.handleMessage((Message*)&command);
495+
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
496+
497+
/*
498+
* NOTE: in this current version properties are not properly integrated with the new paradigm of
499+
* modeling the messages with C structs. The current CBOR library allocates an array in the heap
500+
* thus we need to delete it after decoding it with the old CBORDecoder
501+
*/
502+
free(msg->params.last_values);
503+
}
504+
break;
505+
506+
default:
507+
break;
508+
}
509+
}
481510
}
482511
}
483512

@@ -504,6 +533,17 @@ void ArduinoIoTCloudTCP::sendMessage(Message * msg)
504533
default:
505534
break;
506535
}
536+
537+
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
538+
size_t bytes_encoded = sizeof(data);
539+
CBORMessageEncoder encoder;
540+
541+
if (encoder.encode(msg, data, bytes_encoded) == Encoder::Status::Complete &&
542+
bytes_encoded > 0) {
543+
write(_messageTopicOut, data, bytes_encoded);
544+
} else {
545+
DEBUG_ERROR("error encoding %d", msg->id);
546+
}
507547
}
508548

509549
void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, PropertyContainer & property_container, unsigned int & current_property_index)

src/ArduinoIoTCloudTCP.h

+9-15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
#include <utility/ota/OTA.h>
5757
#endif
5858

59+
#include "cbor/MessageDecoder.h"
60+
#include "cbor/MessageEncoder.h"
61+
5962
/******************************************************************************
6063
CONSTANTS
6164
******************************************************************************/
@@ -163,10 +166,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
163166

164167
MqttClient _mqttClient;
165168

166-
String _deviceTopicOut;
167-
String _deviceTopicIn;
168-
String _shadowTopicOut;
169-
String _shadowTopicIn;
169+
String _messageTopicOut;
170+
String _messageTopicIn;
170171
String _dataTopicOut;
171172
String _dataTopicIn;
172173

@@ -180,10 +181,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
180181
onOTARequestCallbackFunc _get_ota_confirmation;
181182
#endif /* OTA_ENABLED */
182183

183-
inline String getTopic_deviceout() { return String("/a/d/" + getDeviceId() + "/e/o");}
184-
inline String getTopic_devicein () { return String("/a/d/" + getDeviceId() + "/e/i");}
185-
inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); }
186-
inline String getTopic_shadowin () { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/i"); }
184+
inline String getTopic_messageout() { return String("/a/d/" + getDeviceId() + "/c/up");}
185+
inline String getTopic_messagein () { return String("/a/d/" + getDeviceId() + "/c/dw");}
186+
187187
inline String getTopic_dataout () { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/e/o"); }
188188
inline String getTopic_datain () { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/e/i"); }
189189

@@ -198,17 +198,11 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
198198
void sendMessage(Message * msg);
199199
void sendPropertyContainerToCloud(String const topic, PropertyContainer & property_container, unsigned int & current_property_index);
200200
void sendThingPropertiesToCloud();
201-
void sendDevicePropertiesToCloud();
202-
void requestLastValue();
203-
void requestThingId();
201+
204202
void attachThing();
205203
void detachThing();
206204
int write(String const topic, byte const data[], int const length);
207205

208-
#if OTA_ENABLED
209-
void sendDevicePropertyToCloud(String const name);
210-
#endif
211-
212206
};
213207

214208
/******************************************************************************

0 commit comments

Comments
 (0)