Skip to content

Commit 7571052

Browse files
committed
feat: add detach command support
1 parent 0901fa2 commit 7571052

File tree

7 files changed

+79
-1
lines changed

7 files changed

+79
-1
lines changed

extras/test/src/test_command_decode.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,39 @@ SCENARIO("Test the decoding of command messages") {
4949
}
5050

5151
/****************************************************************************/
52+
WHEN("Decode the ThingDetachCmdDown message")
53+
{
54+
CommandDown command;
55+
/*
56+
57+
DA 00010300 # tag(66560)
58+
81 # array(1)
59+
78 24 # text(36)
60+
65343439346435352D383732612D346664322D393634362D393266383739343933393463 # "e4494d55-872a-4fd2-9646-92f87949394c"
61+
62+
*/
63+
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x04, 0x00, 0x81, 0x78, 0x24,
64+
0x65, 0x34, 0x34, 0x39, 0x34, 0x64, 0x35, 0x35,
65+
0x2D, 0x38, 0x37, 0x32, 0x61, 0x2D, 0x34, 0x66,
66+
0x64, 0x32, 0x2D, 0x39, 0x36, 0x34, 0x36, 0x2D,
67+
0x39, 0x32, 0x66, 0x38, 0x37, 0x39, 0x34, 0x39,
68+
0x33, 0x39, 0x34, 0x63};
69+
70+
int payload_length = sizeof(payload) / sizeof(uint8_t);
71+
CBORMessageDecoder decoder;
72+
Decoder::Status err = decoder.decode((Message*)&command, payload, payload_length);
73+
const char *thingIdToMatch = "e4494d55-872a-4fd2-9646-92f87949394c";
74+
75+
THEN("The decode is successful") {
76+
REQUIRE(err == Decoder::Status::Complete);
77+
REQUIRE(strcmp(command.thingDetachCmd.params.thing_id, thingIdToMatch) == 0);
78+
REQUIRE(command.c.id == ThingDetachCmdId);
79+
}
80+
81+
delete msg;
82+
}
83+
84+
/************************************************************************************/
5285

5386
WHEN("Decode the SetTimezoneCommand message")
5487
{

src/ArduinoIoTCloudTCP.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
466466
case CommandId::ThingUpdateCmdId:
467467
{
468468
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] device configuration received", __FUNCTION__, millis());
469-
if ( _thing_id != String(command.thingUpdateCmd.params.thing_id)) {
469+
if (_thing_id != String(command.thingUpdateCmd.params.thing_id)) {
470470
_thing_id = String(command.thingUpdateCmd.params.thing_id);
471471
Message message;
472472
/* If we are attached we need first to detach */
@@ -488,6 +488,22 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
488488
}
489489
break;
490490

491+
case CommandId::ThingDetachCmdId:
492+
{
493+
if (!_device.isAttached() || _thing_id != String(command.thingDetachCmd.params.thing_id)) {
494+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] thing detach rejected", __FUNCTION__, millis());
495+
}
496+
497+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] thing detach received", __FUNCTION__, millis());
498+
499+
detachThing();
500+
_thing_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
501+
502+
Message message = { DeviceDetachedCmdId };
503+
_device.handleMessage(&message);
504+
}
505+
break;
506+
491507
case CommandId::LastValuesUpdateCmdId:
492508
{
493509
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis());

src/cbor/CBOR.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ CommandId toCommandId(CBORCommandTag tag) {
3636
return CommandId::OtaUpdateCmdDownId;
3737
case CBORCommandTag::CBORThingUpdateCmd:
3838
return CommandId::ThingUpdateCmdId;
39+
case CBORCommandTag::CBORThingDetachCmd:
40+
return CommandId::ThingDetachCmdId;
3941
case CBORCommandTag::CBORLastValuesUpdate:
4042
return CommandId::LastValuesUpdateCmdId;
4143
case CBORCommandTag::CBORTimezoneCommandDown:
@@ -63,6 +65,8 @@ CBORCommandTag toCBORCommandTag(CommandId id) {
6365
return CBORCommandTag::CBOROtaUpdateCmdDown;
6466
case CommandId::ThingUpdateCmdId:
6567
return CBORCommandTag::CBORThingUpdateCmd;
68+
case CommandId::ThingDetachCmdId:
69+
return CBORCommandTag::CBORThingDetachCmd;
6670
case CommandId::LastValuesUpdateCmdId:
6771
return CBORCommandTag::CBORLastValuesUpdate;
6872
case CommandId::TimezoneCommandDownId:

src/cbor/CBOR.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum CBORCommandTag: uint64_t {
3131
// Commands DOWN
3232
CBOROtaUpdateCmdDown = 0x010100,
3333
CBORThingUpdateCmd = 0x010400,
34+
CBORThingDetachCmd = 0x011000,
3435
CBORLastValuesUpdate = 0x010600,
3536
CBORTimezoneCommandDown = 0x010900,
3637

src/cbor/MessageDecoder.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ CBORMessageDecoder::ArrayParserState CBORMessageDecoder::decodeThingUpdateCmd(Cb
135135
return ArrayParserState::LeaveArray;
136136
}
137137

138+
CBORMessageDecoder::ArrayParserState CBORMessageDecoder::decodeThingDetachCmd(CborValue * param, Message * message) {
139+
ThingDetachCmd * thingCommand = (ThingDetachCmd *) message;
140+
141+
// Message is composed of a single parameter, a string (thing_id)
142+
if (!copyCBORStringToArray(param, thingCommand->params.thing_id, sizeof(thingCommand->params.thing_id))) {
143+
return ArrayParserState::Error;
144+
}
145+
146+
return ArrayParserState::LeaveArray;
147+
}
148+
138149
CBORMessageDecoder::ArrayParserState CBORMessageDecoder::decodeTimezoneCommandDown(CborValue * param, Message * message) {
139150
TimezoneCommandDown * setTz = (TimezoneCommandDown *) message;
140151

@@ -223,6 +234,9 @@ CBORMessageDecoder::ArrayParserState CBORMessageDecoder::handle_Param(CborValue
223234
case CommandId::ThingUpdateCmdId:
224235
return CBORMessageDecoder::decodeThingUpdateCmd(param, message);
225236

237+
case CommandId::ThingDetachCmdId:
238+
return CBORMessageDecoder::decodeThingDetachCmd(param, message);
239+
226240
case CommandId::TimezoneCommandDownId:
227241
return CBORMessageDecoder::decodeTimezoneCommandDown(param, message);
228242

src/cbor/MessageDecoder.h

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class CBORMessageDecoder: public Decoder
6565

6666
// Message specific decoders
6767
ArrayParserState decodeThingUpdateCmd(CborValue * param, Message * message);
68+
ArrayParserState decodeThingDetachCmd(CborValue * param, Message * message);
6869
ArrayParserState decodeTimezoneCommandDown(CborValue * param, Message * message);
6970
ArrayParserState decodeLastValuesUpdateCmd(CborValue * param, Message * message);
7071
ArrayParserState decodeOtaUpdateCmdDown(CborValue * param, Message * message);

src/message/Commands.h

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum CommandId: uint32_t {
3737
DeviceBeginCmdId,
3838
ThingBeginCmdId,
3939
ThingUpdateCmdId,
40+
ThingDetachCmdId,
4041
DeviceRegisteredCmdId,
4142
DeviceAttachedCmdId,
4243
DeviceDetachedCmdId,
@@ -89,6 +90,13 @@ struct ThingUpdateCmd {
8990
} params;
9091
};
9192

93+
struct ThingDetachCmd {
94+
Command c;
95+
struct {
96+
char thing_id[THING_ID_SIZE];
97+
} params;
98+
};
99+
92100
struct LastValuesBeginCmd {
93101
Command c;
94102
};
@@ -144,6 +152,7 @@ union CommandDown {
144152
struct Command c;
145153
struct OtaUpdateCmdDown otaUpdateCmdDown;
146154
struct ThingUpdateCmd thingUpdateCmd;
155+
struct ThingDetachCmd thingDetachCmd;
147156
struct LastValuesUpdateCmd lastValuesUpdateCmd;
148157
struct TimezoneCommandDown timezoneCommandDown;
149158
};

0 commit comments

Comments
 (0)