Skip to content

Commit 96f0346

Browse files
pennamandreagilardoni
authored andcommitted
tcp fixup
1 parent 241a834 commit 96f0346

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

src/ArduinoIoTCloudTCP.cpp

+48-31
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
6363
, _connection_attempt(0,0)
6464
, _message_stream(std::bind(&ArduinoIoTCloudTCP::sendMessage, this, std::placeholders::_1))
6565
, _thing(&_message_stream)
66-
, _thing_id_property{nullptr}
6766
, _device(&_message_stream)
6867
, _mqtt_data_buf{0}
6968
, _mqtt_data_len{0}
@@ -77,6 +76,8 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
7776
, _mqttClient{nullptr}
7877
, _deviceTopicOut("")
7978
, _deviceTopicIn("")
79+
, _messageTopicOut("")
80+
, _messageTopicIn("")
8081
, _dataTopicOut("")
8182
, _dataTopicIn("")
8283
#if OTA_ENABLED
@@ -319,10 +320,16 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
319320
{
320321
if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort))
321322
{
323+
/* Subscribe to message topic to receive commands */
322324
_mqttClient.subscribe(_messageTopicIn);
323-
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s connected to %s:%d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort);
325+
326+
/* Temoporarly subsribe to device topic to receive OTA properties */
327+
_mqttClient.subscribe(_deviceTopicIn);
328+
324329
/* Reconfigure timers for next state */
325330
_connection_attempt.begin(AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms, AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
331+
332+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s connected to %s:%d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort);
326333
return State::Connected;
327334
}
328335

@@ -339,6 +346,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
339346
{
340347
if (!_mqttClient.connected() || !_thing.connected() || !_device.connected())
341348
{
349+
Serial.println("State::Disconnect");
342350
return State::Disconnect;
343351
}
344352

@@ -437,28 +445,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
437445
/* Topic for OTA properties and device configuration */
438446
if (_deviceTopicIn == topic) {
439447
CBORDecoder::decode(_device.getPropertyContainer(), (uint8_t*)bytes, length);
440-
441-
/* Temporary check to avoid flooding device state machine with usless messages */
442-
if (_thing_id_property->isDifferentFromCloud()) {
443-
_thing_id_property->fromCloudToLocal();
444-
445-
Message message;
446-
/* If we are attached we need first to detach */
447-
if (_device.isAttached()) {
448-
detachThing();
449-
message = { DeviceDetachedCmdId };
450-
}
451-
/* If received thing id is valid attach to the new thing */
452-
if (_thing_id.length()) {
453-
attachThing();
454-
message = { DeviceAttachedCmdId };
455-
} else {
456-
/* Send message to device state machine to inform we have received a null thing-id */
457-
_thing_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
458-
message = { DeviceRegisteredCmdId };
459-
}
460-
_device.handleMessage(&message);
461-
}
462448
}
463449

464450
/* Topic for user input data */
@@ -479,9 +465,26 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
479465
{
480466
case CommandId::ThingUpdateCmdId:
481467
{
482-
_thing_id = String(command.thingBeginCmd.params.thing_id);
483468
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] device configuration received", __FUNCTION__, millis());
484-
_device.handleMessage((Message*)&command);
469+
if ( _thing_id != String(command.thingBeginCmd.params.thing_id)) {
470+
_thing_id = String(command.thingBeginCmd.params.thing_id);
471+
Message message;
472+
/* If we are attached we need first to detach */
473+
if (_device.isAttached()) {
474+
detachThing();
475+
message = { DeviceDetachedCmdId };
476+
}
477+
/* If received thing id is valid attach to the new thing */
478+
if (_thing_id.length()) {
479+
attachThing();
480+
message = { DeviceAttachedCmdId };
481+
} else {
482+
/* Send message to device state machine to inform we have received a null thing-id */
483+
_thing_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
484+
message = { DeviceRegisteredCmdId };
485+
}
486+
_device.handleMessage(&message);
487+
}
485488
}
486489
break;
487490

@@ -516,6 +519,25 @@ void ArduinoIoTCloudTCP::sendMessage(Message * msg)
516519
size_t bytes_encoded = sizeof(data);
517520
CBORMessageEncoder encoder;
518521

522+
switch (msg->id) {
523+
case PropertiesUpdateCmdId:
524+
return sendPropertyContainerToCloud(_dataTopicOut,
525+
_thing.getPropertyContainer(),
526+
_thing.getPropertyContainerIndex());
527+
break;
528+
529+
#if OTA_ENABLED
530+
case DeviceBeginCmdId:
531+
sendDevicePropertyToCloud("OTA_CAP");
532+
sendDevicePropertyToCloud("OTA_ERROR");
533+
sendDevicePropertyToCloud("OTA_SHA256");
534+
break;
535+
#endif
536+
537+
default:
538+
break;
539+
}
540+
519541
if (encoder.encode(msg, data, bytes_encoded) == Encoder::Status::Complete &&
520542
bytes_encoded > 0) {
521543
write(_messageTopicOut, data, bytes_encoded);
@@ -544,11 +566,6 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
544566
}
545567
}
546568

547-
void ArduinoIoTCloudTCP::sendThingPropertiesToCloud()
548-
{
549-
sendPropertyContainerToCloud(_dataTopicOut, _thing.getPropertyContainer(), _thing.getPropertyContainerIndex());
550-
}
551-
552569
#if OTA_ENABLED
553570
void ArduinoIoTCloudTCP::sendDevicePropertyToCloud(String const name)
554571
{

src/ArduinoIoTCloudTCP.h

-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
128128
TimedAttempt _connection_attempt;
129129
MessageStream _message_stream;
130130
ArduinoCloudThing _thing;
131-
Property * _thing_id_property;
132131
ArduinoCloudDevice _device;
133132

134133
String _brokerAddress;
@@ -202,7 +201,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
202201
void handleMessage(int length);
203202
void sendMessage(Message * msg);
204203
void sendPropertyContainerToCloud(String const topic, PropertyContainer & property_container, unsigned int & current_property_index);
205-
void sendThingPropertiesToCloud();
206204

207205
void attachThing();
208206
void detachThing();

0 commit comments

Comments
 (0)