Skip to content

Commit 8856429

Browse files
committed
Add commands
1 parent 3918e62 commit 8856429

File tree

4 files changed

+135
-27
lines changed

4 files changed

+135
-27
lines changed

extras/test/src/test_command_encode.cpp

Lines changed: 99 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,46 @@
1818
**************************************************************************************/
1919

2020
SCENARIO("Test the encoding of command messages") {
21+
/************************************************************************************/
22+
23+
WHEN("Encode the OtaBeginUp message")
24+
{
25+
OtaBeginUp * command = new OtaBeginUp();
26+
uint8_t sha[SHA256_SIZE] = {0x01, 0x02, 0x03, 0x04};
27+
memcpy(command->fields.params.sha, sha, SHA256_SIZE);
28+
29+
Message * message = new Message();
30+
message->id = CommandID::DeviceBeginCmdUpId;
31+
32+
uint8_t buffer[256];
33+
int bytes_encoded = 0;
34+
35+
CborError err = MessageEncoder::encode(message, buffer, sizeof(buffer), bytes_encoded);
36+
37+
// Test the encoding is
38+
// DA 00010700 # tag(67328)
39+
// 81 # array(1)
40+
// 44 # bytes(4)
41+
// 01020304 # "\u0001\u0002\u0003\u0004"
42+
THEN("The encoding is successful") {
43+
REQUIRE(err == CborNoError);
44+
REQUIRE(bytes_encoded == 7);
45+
REQUIRE(buffer[0] == 0xDA);
46+
REQUIRE(buffer[1] == 0x00);
47+
REQUIRE(buffer[2] == 0x01);
48+
REQUIRE(buffer[3] == 0x07);
49+
REQUIRE(buffer[4] == 0x81);
50+
REQUIRE(buffer[5] == 0x44);
51+
REQUIRE(buffer[6] == 0x01);
52+
REQUIRE(buffer[7] == 0x02);
53+
REQUIRE(buffer[8] == 0x03);
54+
REQUIRE(buffer[9] == 0x04);
55+
}
56+
57+
delete message;
58+
}
59+
60+
2161
/************************************************************************************/
2262

2363
WHEN("Encode the ThingGetIdCmdUp message")
@@ -90,14 +130,10 @@ SCENARIO("Test the encoding of command messages") {
90130
delete message;
91131
}
92132

93-
/************************************************************************************/
133+
/************************************************************************************/
94134

95-
WHEN("Encode the DeviceBeginCmdUp message")
135+
WHEN("Encode the DeviceBeginCmdUpId message")
96136
{
97-
DeviceBeginCmdUp * command = new DeviceBeginCmdUp();
98-
uint8_t sha[SHA256_SIZE] = {0x01, 0x02, 0x03, 0x04};
99-
memcpy(command->fields.params.sha, sha, SHA256_SIZE);
100-
101137
Message * message = new Message();
102138
message->id = CommandID::DeviceBeginCmdUpId;
103139

@@ -107,25 +143,44 @@ SCENARIO("Test the encoding of command messages") {
107143
CborError err = MessageEncoder::encode(message, buffer, sizeof(buffer), bytes_encoded);
108144

109145
// Test the encoding is
110-
// DA 00010700 # tag(67328)
111-
// 81 # array(1)
112-
// 44 # bytes(4)
113-
// 01020304 # "\u0001\u0002\u0003\u0004"
146+
// DA 00010700 # tag(67328)
147+
// 80 # array(0)
114148
THEN("The encoding is successful") {
115149
REQUIRE(err == CborNoError);
116-
REQUIRE(bytes_encoded == 7);
150+
REQUIRE(bytes_encoded == 4);
117151
REQUIRE(buffer[0] == 0xDA);
118152
REQUIRE(buffer[1] == 0x00);
119153
REQUIRE(buffer[2] == 0x01);
120154
REQUIRE(buffer[3] == 0x07);
121-
REQUIRE(buffer[4] == 0x81);
122-
REQUIRE(buffer[5] == 0x44);
123-
REQUIRE(buffer[6] == 0x01);
124-
REQUIRE(buffer[7] == 0x02);
125-
REQUIRE(buffer[8] == 0x03);
126-
REQUIRE(buffer[9] == 0x04);
155+
REQUIRE(buffer[4] == 0x80);
127156
}
157+
delete message;
158+
}
128159

160+
/************************************************************************************/
161+
162+
WHEN("Encode the OtaUpdateCmdDown message")
163+
{
164+
Message * message = new Message();
165+
message->id = CommandID::OtaUpdateCmdDownId;
166+
167+
uint8_t buffer[256];
168+
int bytes_encoded = 0;
169+
170+
CborError err = MessageEncoder::encode(message, buffer, sizeof(buffer), bytes_encoded);
171+
172+
// Test the encoding is
173+
// DA 00010600 # tag(65792)
174+
// 80 # array(0)
175+
THEN("The encoding is successful") {
176+
REQUIRE(err == CborNoError);
177+
REQUIRE(bytes_encoded == 4);
178+
REQUIRE(buffer[0] == 0xDA);
179+
REQUIRE(buffer[1] == 0x00);
180+
REQUIRE(buffer[2] == 0x01);
181+
REQUIRE(buffer[3] == 0x06);
182+
REQUIRE(buffer[4] == 0x80);
183+
}
129184
delete message;
130185
}
131186

@@ -183,4 +238,31 @@ SCENARIO("Test the encoding of command messages") {
183238
delete message;
184239
}
185240

241+
/************************************************************************************/
242+
243+
WHEN("Encode the TimezoneCommandUpId message")
244+
{
245+
Message * message = new Message();
246+
message->id = CommandID::TimezoneCommandUpId;
247+
248+
uint8_t buffer[256];
249+
int bytes_encoded = 0;
250+
251+
CborError err = MessageEncoder::encode(message, buffer, sizeof(buffer), bytes_encoded);
252+
253+
// Test the encoding is
254+
// DA 00010800 # tag(67584)
255+
// 80 # array(0)
256+
THEN("The encoding is successful") {
257+
REQUIRE(err == CborNoError);
258+
REQUIRE(bytes_encoded == 4);
259+
REQUIRE(buffer[0] == 0xDA);
260+
REQUIRE(buffer[1] == 0x00);
261+
REQUIRE(buffer[2] == 0x01);
262+
REQUIRE(buffer[3] == 0x08);
263+
REQUIRE(buffer[4] == 0x80);
264+
}
265+
delete message;
266+
}
267+
186268
}

src/cbor/MessageEncoder.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,22 @@ MessageEncoder::EncoderState MessageEncoder::handle_EncodeParam(CborEncoder * en
9999
DEBUG_INFO("message id is %d", message->id);
100100
switch (message->id)
101101
{
102+
case CommandID::OtaBeginUpId:
103+
error = MessageEncoder::encodeOtaBeginUp(array_encoder, message);
104+
break;
102105
case CommandID::ThingGetIdCmdUpId:
103-
DEBUG_INFO("Encode thing id up");
104106
error = MessageEncoder::encodeThingGetIdCmdUp(array_encoder, message);
105107
break;
106-
case CommandID::ThingGetLastValueCmdUpId:
107-
break;
108108
case CommandID::DeviceBeginCmdUpId:
109109
error = MessageEncoder::encodeDeviceBeginCmdUp(array_encoder, message);
110110
break;
111+
case CommandID::ThingGetLastValueCmdUpId:
112+
break;
111113
case CommandID::OtaProgressCmdUpId:
112114
error = MessageEncoder::encodeOtaProgressCmdUp(array_encoder, message);
113115
break;
116+
case CommandID::TimezoneCommandUpId:
117+
break;
114118
default:
115119
return EncoderState::MessageNotSupported;
116120
}
@@ -136,6 +140,13 @@ MessageEncoder::EncoderState MessageEncoder::handle_CloseArray(CborEncoder * enc
136140
}
137141

138142
// Message specific encoders
143+
CborError MessageEncoder::encodeOtaBeginUp(CborEncoder * array_encoder, Message * message)
144+
{
145+
OtaBeginUp * otaBeginUp = (OtaBeginUp *) message;
146+
CHECK_CBOR(cbor_encode_byte_string(array_encoder, otaBeginUp->fields.params.sha, SHA256_SIZE));
147+
return CborNoError;
148+
}
149+
139150
CborError MessageEncoder::encodeThingGetIdCmdUp(CborEncoder * array_encoder, Message * message)
140151
{
141152
ThingGetIdCmdUp * thingGetIdCmdUp = (ThingGetIdCmdUp *) message;
@@ -147,7 +158,7 @@ CborError MessageEncoder::encodeThingGetIdCmdUp(CborEncoder * array_encoder, Mes
147158
CborError MessageEncoder::encodeDeviceBeginCmdUp(CborEncoder * array_encoder, Message * message)
148159
{
149160
DeviceBeginCmdUp * deviceBeginCmdUp = (DeviceBeginCmdUp *) message;
150-
CHECK_CBOR(cbor_encode_byte_string(array_encoder, deviceBeginCmdUp->fields.params.sha, SHA256_SIZE));
161+
CHECK_CBOR(cbor_encode_text_stringz(array_encoder, deviceBeginCmdUp->fields.params.lib_version));
151162
return CborNoError;
152163
}
153164

@@ -161,4 +172,3 @@ CborError MessageEncoder::encodeOtaProgressCmdUp(CborEncoder * array_encoder, Me
161172
return CborNoError;
162173
}
163174

164-

src/cbor/MessageEncoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class MessageEncoder
6464

6565
// Message specific encoders
6666
static CborError encodeThingGetIdCmdUp(CborEncoder * array_encoder, Message * message);
67+
static CborError encodeOtaBeginUp(CborEncoder * array_encoder, Message * message);
6768
static CborError encodeDeviceBeginCmdUp(CborEncoder * array_encoder, Message * message);
6869
static CborError encodeOtaProgressCmdUp(CborEncoder * array_encoder, Message * message);
6970
};

src/models/models.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
#define THING_ID_SIZE 37
66
#define SHA256_SIZE 32
77
#define URL_SIZE 64
8-
#define STATE_SIZE 32
8+
#define MAX_STATE_SIZE 32
99
#define ID_SIZE 32
10+
#define MAX_LIB_VERSION_SIZE 10
1011

1112
enum CommandID: uint32_t {
1213
// Commands UP
14+
OtaBeginUpId = 65536,
1315
ThingGetIdCmdUpId = 66304,
1416
ThingGetLastValueCmdUpId = 66816,
1517
DeviceBeginCmdUpId = 67328,
1618
OtaProgressCmdUpId = 66048,
17-
TimezoneCommandUpId = 67398,
19+
TimezoneCommandUpId = 67584,
1820

1921
// Commands DOWN
2022
OtaUpdateCmdDownId = 65792,
2123
ThingGetIdCmdDownId = 66560,
2224
ThingGetLastValueCmdDownId = 67072,
23-
TimezoneCommandDownId = 67428
25+
TimezoneCommandDownId = 67840
2426
};
2527

2628
struct Command {
@@ -57,6 +59,19 @@ union ThingGetLastValueCmdUp {
5759
} fields;
5860
};
5961

62+
struct OtaBeginUpCommand {
63+
Command command;
64+
};
65+
66+
union OtaBeginUp {
67+
struct {
68+
OtaBeginUpCommand otaBeginUpCommand;
69+
struct {
70+
uint8_t sha [SHA256_SIZE];
71+
} params;
72+
} fields;
73+
};
74+
6075
struct DeviceBeginCmdUpCommand {
6176
Command command;
6277
};
@@ -65,7 +80,7 @@ union DeviceBeginCmdUp {
6580
struct {
6681
DeviceBeginCmdUpCommand deviceBeginCmdUpCommand;
6782
struct {
68-
uint8_t sha [SHA256_SIZE];
83+
char lib_version[MAX_LIB_VERSION_SIZE];
6984
} params;
7085
} fields;
7186
};
@@ -79,7 +94,7 @@ union OtaProgressCmdUp {
7994
OtaProgressCmdUpCommand otaProgressCmdUpCommand;
8095
struct {
8196
char id[ID_SIZE];
82-
char state[STATE_SIZE];
97+
char state[MAX_STATE_SIZE];
8398
uint32_t time;
8499
uint32_t count;
85100
} params;

0 commit comments

Comments
 (0)