Skip to content

Commit 965ed17

Browse files
implementing cbor message encoder following cloud utils definition
1 parent b932dce commit 965ed17

File tree

4 files changed

+234
-239
lines changed

4 files changed

+234
-239
lines changed

src/cbor/IoTCloudMessageEncoder.cpp

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
This file is part of the ArduinoIoTCloud library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
/******************************************************************************
12+
* INCLUDE
13+
******************************************************************************/
14+
15+
#include "CBOREncoder.h"
16+
17+
#include "IoTCloudMessageEncoder.h"
18+
19+
/******************************************************************************
20+
* PUBLIC MEMBER FUNCTIONS
21+
******************************************************************************/
22+
23+
24+
/******************************************************************************
25+
PRIVATE MEMBER FUNCTIONS
26+
******************************************************************************/
27+
28+
MessageEncoder::Status OtaBeginCommandEncoder::encode(CborEncoder* encoder, Message *msg) {
29+
OtaBeginUp * otaBeginUp = (OtaBeginUp*) msg;
30+
CborEncoder array_encoder;
31+
32+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
33+
return MessageEncoder::Status::Error;
34+
}
35+
36+
if(cbor_encode_byte_string(&array_encoder, otaBeginUp->params.sha, SHA256_SIZE) != CborNoError) {
37+
return MessageEncoder::Status::Error;
38+
}
39+
40+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
41+
return MessageEncoder::Status::Error;
42+
}
43+
44+
return MessageEncoder::Status::Complete;
45+
}
46+
47+
MessageEncoder::Status ThingBeginCommandEncoder::encode(CborEncoder* encoder, Message *msg) {
48+
ThingBeginCmd * thingBeginCmd = (ThingBeginCmd*) msg;
49+
CborEncoder array_encoder;
50+
51+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
52+
return MessageEncoder::Status::Error;
53+
}
54+
55+
if(cbor_encode_text_stringz(&array_encoder, thingBeginCmd->params.thing_id) != CborNoError) {
56+
return MessageEncoder::Status::Error;
57+
}
58+
59+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
60+
return MessageEncoder::Status::Error;
61+
}
62+
63+
return MessageEncoder::Status::Complete;
64+
}
65+
66+
MessageEncoder::Status LastValuesBeginCommandEncoder::encode(CborEncoder* encoder, Message *msg) {
67+
// This command contains no parameters, it contains just the id of the message
68+
// nothing to perform here
69+
// (void)(encoder);
70+
(void)(msg);
71+
CborEncoder array_encoder;
72+
73+
// FIXME we are encoiding an empty array, this could be avoided
74+
if (cbor_encoder_create_array(encoder, &array_encoder, 0) != CborNoError){
75+
return MessageEncoder::Status::Error;
76+
}
77+
78+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
79+
return MessageEncoder::Status::Error;
80+
}
81+
82+
return MessageEncoder::Status::Complete;
83+
}
84+
85+
MessageEncoder::Status DeviceBeginCommandEncoder::encode(CborEncoder* encoder, Message *msg) {
86+
DeviceBeginCmd * deviceBeginCmd = (DeviceBeginCmd*) msg;
87+
CborEncoder array_encoder;
88+
89+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
90+
return MessageEncoder::Status::Error;
91+
}
92+
93+
if(cbor_encode_text_stringz(&array_encoder, deviceBeginCmd->params.lib_version) != CborNoError) {
94+
return MessageEncoder::Status::Error;
95+
}
96+
97+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
98+
return MessageEncoder::Status::Error;
99+
}
100+
101+
return MessageEncoder::Status::Complete;
102+
}
103+
104+
MessageEncoder::Status OtaProgressCommandUpEncoder::encode(CborEncoder* encoder, Message *msg) {
105+
OtaProgressCmdUp * ota = (OtaProgressCmdUp*) msg;
106+
CborEncoder array_encoder;
107+
108+
if(cbor_encoder_create_array(encoder, &array_encoder, 4) != CborNoError) {
109+
return MessageEncoder::Status::Error;
110+
}
111+
112+
if(cbor_encode_byte_string(&array_encoder, ota->params.id, ID_SIZE) != CborNoError) {
113+
return MessageEncoder::Status::Error;
114+
}
115+
116+
if(cbor_encode_simple_value(&array_encoder, ota->params.state) != CborNoError) {
117+
return MessageEncoder::Status::Error;
118+
}
119+
120+
if(cbor_encode_int(&array_encoder, ota->params.state_data) != CborNoError) {
121+
return MessageEncoder::Status::Error;
122+
}
123+
124+
if(cbor_encode_uint(&array_encoder, ota->params.time) != CborNoError) {
125+
return MessageEncoder::Status::Error;
126+
}
127+
128+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
129+
return MessageEncoder::Status::Error;
130+
}
131+
132+
return MessageEncoder::Status::Complete;
133+
}
134+
135+
MessageEncoder::Status TimezoneCommandUpEncoder::encode(CborEncoder* encoder, Message *msg) {
136+
// This command contains no parameters, it contains just the id of the message
137+
// nothing to perform here
138+
// (void)(encoder);
139+
(void)(msg);
140+
CborEncoder array_encoder;
141+
142+
// FIXME we are encoiding an empty array, this could be avoided
143+
if (cbor_encoder_create_array(encoder, &array_encoder, 0) != CborNoError){
144+
return MessageEncoder::Status::Error;
145+
}
146+
147+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
148+
return MessageEncoder::Status::Error;
149+
}
150+
151+
return MessageEncoder::Status::Complete;
152+
}
153+
154+
static OtaBeginCommandEncoder otaBeginCommandEncoder;
155+
static ThingBeginCommandEncoder thingBeginCommandEncoder;
156+
static LastValuesBeginCommandEncoder lastValuesBeginCommandEncoder;
157+
static DeviceBeginCommandEncoder deviceBeginCommandEncoder;
158+
static OtaProgressCommandUpEncoder otaProgressCommandUpEncoder;
159+
static TimezoneCommandUpEncoder timezoneCommandUpEncoder;

src/cbor/IoTCloudMessageEncoder.h

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
This file is part of the ArduinoIoTCloud library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#ifndef ARDUINO_CBOR_MESSAGE_ENCODER_H_
12+
#define ARDUINO_CBOR_MESSAGE_ENCODER_H_
13+
14+
/******************************************************************************
15+
* INCLUDE
16+
******************************************************************************/
17+
18+
#include "./CBOR.h"
19+
#include <cbor/MessageEncoder.h>
20+
#include "message/Commands.h"
21+
22+
/******************************************************************************
23+
* CLASS DECLARATION
24+
******************************************************************************/
25+
26+
class OtaBeginCommandEncoder: public CBORMessageEncoderInterface {
27+
public:
28+
OtaBeginCommandEncoder()
29+
: CBORMessageEncoderInterface(CBOROtaBeginUp, OtaBeginUpId) {}
30+
protected:
31+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override;
32+
};
33+
34+
class ThingBeginCommandEncoder: public CBORMessageEncoderInterface {
35+
public:
36+
ThingBeginCommandEncoder()
37+
: CBORMessageEncoderInterface(CBORThingBeginCmd, ThingBeginCmdId) {}
38+
protected:
39+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override;
40+
};
41+
42+
class LastValuesBeginCommandEncoder: public CBORMessageEncoderInterface {
43+
public:
44+
LastValuesBeginCommandEncoder()
45+
: CBORMessageEncoderInterface(CBORLastValuesBeginCmd, LastValuesBeginCmdId) {}
46+
protected:
47+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override;
48+
};
49+
50+
class DeviceBeginCommandEncoder: public CBORMessageEncoderInterface {
51+
public:
52+
DeviceBeginCommandEncoder()
53+
: CBORMessageEncoderInterface(CBORDeviceBeginCmd, DeviceBeginCmdId) {}
54+
protected:
55+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override;
56+
};
57+
58+
class OtaProgressCommandUpEncoder: public CBORMessageEncoderInterface {
59+
public:
60+
OtaProgressCommandUpEncoder()
61+
: CBORMessageEncoderInterface(CBOROtaProgressCmdUp, OtaProgressCmdUpId) {}
62+
protected:
63+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override;
64+
};
65+
66+
class TimezoneCommandUpEncoder: public CBORMessageEncoderInterface {
67+
public:
68+
TimezoneCommandUpEncoder()
69+
: CBORMessageEncoderInterface(CBORTimezoneCommandUp, TimezoneCommandUpId) {}
70+
protected:
71+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override;
72+
};
73+
74+
75+
#endif /* ARDUINO_CBOR_MESSAGE_ENCODER_H_ */

0 commit comments

Comments
 (0)