Skip to content

Commit 00fa53f

Browse files
robgee86pennam
authored andcommitted
Add detach command support
1 parent c43aa15 commit 00fa53f

File tree

7 files changed

+71
-0
lines changed

7 files changed

+71
-0
lines changed

extras/test/src/test_command_decode.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ SCENARIO("Test the decoding of command messages") {
5050
}
5151

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

5485
WHEN("Decode the ThingUpdateCmdId message containing a number instead of a string")
5586
{

src/ArduinoIoTCloudTCP.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,17 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
410410
}
411411
break;
412412

413+
case CommandId::ThingDetachCmdId:
414+
{
415+
if (!_device.isAttached() || _thing_id != String(command.thingDetachCmd.params.thing_id)) {
416+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] thing detach rejected", __FUNCTION__, millis());
417+
}
418+
419+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] thing detach received", __FUNCTION__, millis());
420+
detachThing();
421+
}
422+
break;
423+
413424
case CommandId::LastValuesUpdateCmdId:
414425
{
415426
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
@@ -130,6 +130,17 @@ CBORMessageDecoder::ArrayParserState CBORMessageDecoder::decodeThingUpdateCmd(Cb
130130
return ArrayParserState::LeaveArray;
131131
}
132132

133+
CBORMessageDecoder::ArrayParserState CBORMessageDecoder::decodeThingDetachCmd(CborValue * param, Message * message) {
134+
ThingDetachCmd * thingCommand = (ThingDetachCmd *) message;
135+
136+
// Message is composed of a single parameter, a string (thing_id)
137+
if (!copyCBORStringToArray(param, thingCommand->params.thing_id, sizeof(thingCommand->params.thing_id))) {
138+
return ArrayParserState::Error;
139+
}
140+
141+
return ArrayParserState::LeaveArray;
142+
}
143+
133144
CBORMessageDecoder::ArrayParserState CBORMessageDecoder::decodeTimezoneCommandDown(CborValue * param, Message * message) {
134145
TimezoneCommandDown * setTz = (TimezoneCommandDown *) message;
135146

@@ -213,6 +224,9 @@ CBORMessageDecoder::ArrayParserState CBORMessageDecoder::handle_Param(CborValue
213224
case CommandId::ThingUpdateCmdId:
214225
return CBORMessageDecoder::decodeThingUpdateCmd(param, message);
215226

227+
case CommandId::ThingDetachCmdId:
228+
return CBORMessageDecoder::decodeThingDetachCmd(param, message);
229+
216230
case CommandId::TimezoneCommandDownId:
217231
return CBORMessageDecoder::decodeTimezoneCommandDown(param, message);
218232

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)