Skip to content

Commit b4bb06e

Browse files
committed
Decoder refactoring
1 parent e802b62 commit b4bb06e

File tree

4 files changed

+49
-68
lines changed

4 files changed

+49
-68
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,32 @@ void ArduinoIoTCloudTCP::decode(){
304304

305305
uint8_t const payload[] = {0xDA, 0x00, 0x01, 0x03, 0x00, 0x81, 0x78, 0x24, 0x65, 0x34, 0x34, 0x39, 0x34, 0x64, 0x35, 0x35, 0x2D, 0x38, 0x37, 0x32, 0x61, 0x2D, 0x34, 0x66, 0x64, 0x32, 0x2D, 0x39, 0x36, 0x34, 0x36, 0x2D, 0x39, 0x32, 0x66, 0x38, 0x37, 0x39, 0x34, 0x39, 0x33, 0x39, 0x34, 0x63};
306306

307-
int const payload_length = sizeof(payload) / sizeof(uint8_t);
307+
int payload_length = sizeof(payload) / sizeof(uint8_t);
308308
MessageDecoder::decode((Message*) message, payload, payload_length);
309309

310-
311-
DEBUG_INFO("Message id is %d", message->command);
310+
DEBUG_INFO("Message THING id is %d", message->command);
312311

313312
GetThingIdCommand * thingCommand = (GetThingIdCommand*) message;
314313
DEBUG_INFO("Thing id is %s", thingCommand->fields.params.thing_id);
315314

316315
delete message;
316+
delete thingCommand;
317+
318+
// Fake last value down message
319+
uint8_t const payloadLV[] = {0xDA, 0x00, 0x01, 0x06, 0x00, 0x81, 0x4D, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12};
320+
payload_length = sizeof(payloadLV) / sizeof(uint8_t);
321+
MessageDecoder::decode((Message*) message, payloadLV, payload_length);
322+
323+
DEBUG_INFO("Message LV id is %d", message->command);
324+
ThingGetLastValueCmdDown * lv = (ThingGetLastValueCmdDown*) message;
325+
DEBUG_INFO("LV payload (size: %d)", lv->fields.params.length);
326+
for (int i = 0; i < lv->fields.params.length; i++) {
327+
DEBUG_INFO("%02X", lv->fields.params.last_values[i]);
328+
}
329+
330+
delete lv->fields.params.last_values;
331+
delete lv;
332+
delete message;
317333
}
318334

319335
void ArduinoIoTCloudTCP::encode(){

src/cbor/MessageDecoder.cpp

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ MessageDecoder::ArrayParserState MessageDecoder::handle_EnterArray(CborValue * m
120120
return next_state;
121121
}
122122

123-
MessageDecoder::ArrayParserState MessageDecoder::decodeGetThingIdCommand(CborValue * param, Message * message) {
123+
MessageDecoder::ArrayParserState MessageDecoder::handle_LeaveArray(CborValue * array_iter) {
124+
return ArrayParserState::Complete;
125+
}
126+
127+
/******************************************************************************
128+
MESSAGE DECODE FUNCTIONS
129+
******************************************************************************/
130+
131+
MessageDecoder::ArrayParserState MessageDecoder::decodeThingGetIdCmdDown(CborValue * param, Message * message) {
124132
ThingGetIdCmdDown * thingCommand = (ThingGetIdCmdDown *) message;
125133

126134
// Message is composed of a single parameter, a string (thing_id)
@@ -131,7 +139,7 @@ MessageDecoder::ArrayParserState MessageDecoder::decodeGetThingIdCommand(CborVal
131139
}
132140
}
133141

134-
MessageDecoder::ArrayParserState MessageDecoder::decodeSetTimezoneCommand(CborValue * param, Message * message) {
142+
MessageDecoder::ArrayParserState MessageDecoder::decodeTimezoneCommandDown(CborValue * param, Message * message) {
135143
SetTimezoneCommand * setTz = (SetTimezoneCommand *) message;
136144

137145
// Message is composed of 2 parameters, offset and until 32-bit unsigned integer
@@ -159,16 +167,20 @@ MessageDecoder::ArrayParserState MessageDecoder::decodeSetTimezoneCommand(CborVa
159167
return ArrayParserState::LeaveArray;
160168
}
161169

162-
MessageDecoder::ArrayParserState MessageDecoder::decodeSetLastValueCommand(CborValue * param, Message * message) {
170+
MessageDecoder::ArrayParserState MessageDecoder::decodeThingGetLastValueCmdDown(CborValue * param, Message * message) {
163171
ThingGetLastValueCmdDown * setLv = (ThingGetLastValueCmdDown *) message;
164172

165-
// Message is composed TODO
166-
173+
// Message is composed by a single parameter, a variable lenght byte array.
174+
if (cbor_value_is_byte_string(param)) {
175+
if (cbor_value_dup_byte_string(param, &setLv->fields.params.last_values, &setLv->fields.params.length, NULL) != CborNoError) {
176+
return ArrayParserState::Error;
177+
}
178+
}
167179

168180
return ArrayParserState::LeaveArray;
169181
}
170182

171-
MessageDecoder::ArrayParserState MessageDecoder::decodeOTAAvailableCommand(CborValue * param, Message * message) {
183+
MessageDecoder::ArrayParserState MessageDecoder::decodeOtaUpdateCmdDown(CborValue * param, Message * message) {
172184
OtaUpdateCmdDown * ota = (OtaUpdateCmdDown *) message;
173185

174186
// Message is composed 4 parameters: id, url, initialSha, finalSha
@@ -217,70 +229,20 @@ MessageDecoder::ArrayParserState MessageDecoder::handle_Param(CborValue * param,
217229
switch (message->id)
218230
{
219231
case CommandID::ThingGetIdCmdDownId:
220-
return MessageDecoder::decodeGetThingIdCommand(param, message);
232+
return MessageDecoder::decodeThingGetIdCmdDown(param, message);
221233

222234
case CommandID::TimezoneCommandDownId:
223-
return MessageDecoder::decodeSetTimezoneCommand(param, message);
235+
return MessageDecoder::decodeTimezoneCommandDown(param, message);
224236

225237
case CommandID::ThingGetLastValueCmdDownId:
226-
return MessageDecoder::decodeSetLastValueCommand(param, message);
238+
return MessageDecoder::decodeThingGetLastValueCmdDown(param, message);
227239

228240
case CommandID::OtaUpdateCmdDownId:
229-
return MessageDecoder::decodeOTAAvailableCommand(param, message);
241+
return MessageDecoder::decodeOtaUpdateCmdDown(param, message);
230242

231243
default:
232244
return ArrayParserState::MessageNotSupported;
233245
}
234246

235247
return ArrayParserState::LeaveArray;
236248
}
237-
238-
MessageDecoder::ArrayParserState MessageDecoder::handle_LeaveArray(CborValue * array_iter) {
239-
return ArrayParserState::Complete;
240-
}
241-
242-
bool MessageDecoder::ifNumericConvertToDouble(CborValue * value_iter, double * numeric_val) {
243-
244-
if (cbor_value_is_integer(value_iter)) {
245-
int64_t val = 0;
246-
if (cbor_value_get_int64(value_iter, &val) == CborNoError) {
247-
*numeric_val = static_cast<double>(val);
248-
return true;
249-
}
250-
} else if (cbor_value_is_double(value_iter)) {
251-
double val = 0.0;
252-
if (cbor_value_get_double(value_iter, &val) == CborNoError) {
253-
*numeric_val = val;
254-
return true;
255-
}
256-
} else if (cbor_value_is_float(value_iter)) {
257-
float val = 0.0;
258-
if (cbor_value_get_float(value_iter, &val) == CborNoError) {
259-
*numeric_val = static_cast<double>(val);
260-
return true;
261-
}
262-
} else if (cbor_value_is_half_float(value_iter)) {
263-
uint16_t val = 0;
264-
if (cbor_value_get_half_float(value_iter, &val) == CborNoError) {
265-
*numeric_val = static_cast<double>(convertCborHalfFloatToDouble(val));
266-
return true;
267-
}
268-
}
269-
270-
return false;
271-
}
272-
273-
/* Source Idea from https://tools.ietf.org/html/rfc7049 : Page: 50 */
274-
double MessageDecoder::convertCborHalfFloatToDouble(uint16_t const half_val) {
275-
int exp = (half_val >> 10) & 0x1f;
276-
int mant = half_val & 0x3ff;
277-
double val;
278-
if (exp == 0) {
279-
val = ldexp(mant, -24);
280-
} else if (exp != 31) {
281-
val = ldexp(mant + 1024, exp - 25);
282-
} else {
283-
val = mant == 0 ? INFINITY : NAN;
284-
}
285-
return half_val & 0x8000 ? -val : val;
286-
}

src/cbor/MessageDecoder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ class MessageDecoder
7373
static double convertCborHalfFloatToDouble(uint16_t const half_val);
7474

7575
// Message specific decoders
76-
static ArrayParserState decodeGetThingIdCommand(CborValue * param, Message * message);
77-
static ArrayParserState decodeSetTimezoneCommand(CborValue * param, Message * message);
78-
static ArrayParserState decodeSetLastValueCommand(CborValue * param, Message * message);
79-
static ArrayParserState decodeOTAAvailableCommand(CborValue * param, Message * message);
76+
static ArrayParserState decodeThingGetIdCmdDown(CborValue * param, Message * message);
77+
static ArrayParserState decodeTimezoneCommandDown(CborValue * param, Message * message);
78+
static ArrayParserState decodeThingGetLastValueCmdDown(CborValue * param, Message * message);
79+
static ArrayParserState decodeOtaUpdateCmdDown(CborValue * param, Message * message);
8080

8181
};
8282

src/models/models.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
#define ID_SIZE 32
1010

1111
enum CommandID: uint32_t {
12+
// Commands UP
1213
ThingGetIdCmdUpId = 66304,
1314
ThingGetLastValueCmdUpId = 66816,
1415
DeviceBeginCmdUpId = 67328,
1516
OtaProgressCmdUpId = 66048,
17+
TimezoneCommandUpId = 67398,
18+
19+
// Commands DOWN
1620
OtaUpdateCmdDownId = 65792,
1721
ThingGetIdCmdDownId = 66560,
1822
ThingGetLastValueCmdDownId = 67072,
19-
TimezoneCommandUpId = 67398,
2023
TimezoneCommandDownId = 67428
2124
};
2225

0 commit comments

Comments
 (0)