Skip to content

Commit 134308b

Browse files
implementing cbor message encoder following cloud utils definition
1 parent a619b12 commit 134308b

File tree

4 files changed

+235
-239
lines changed

4 files changed

+235
-239
lines changed

src/cbor/IoTCloudMessageEncoder.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 the cloud cbor decode is able to accept an empty array */
75+
if (cbor_encoder_create_array(encoder, &array_encoder, 0) != CborNoError){
76+
return MessageEncoder::Status::Error;
77+
}
78+
79+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
80+
return MessageEncoder::Status::Error;
81+
}
82+
83+
return MessageEncoder::Status::Complete;
84+
}
85+
86+
MessageEncoder::Status DeviceBeginCommandEncoder::encode(CborEncoder* encoder, Message *msg) {
87+
DeviceBeginCmd * deviceBeginCmd = (DeviceBeginCmd*) msg;
88+
CborEncoder array_encoder;
89+
90+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
91+
return MessageEncoder::Status::Error;
92+
}
93+
94+
if(cbor_encode_text_stringz(&array_encoder, deviceBeginCmd->params.lib_version) != CborNoError) {
95+
return MessageEncoder::Status::Error;
96+
}
97+
98+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
99+
return MessageEncoder::Status::Error;
100+
}
101+
102+
return MessageEncoder::Status::Complete;
103+
}
104+
105+
MessageEncoder::Status OtaProgressCommandUpEncoder::encode(CborEncoder* encoder, Message *msg) {
106+
OtaProgressCmdUp * ota = (OtaProgressCmdUp*) msg;
107+
CborEncoder array_encoder;
108+
109+
if(cbor_encoder_create_array(encoder, &array_encoder, 4) != CborNoError) {
110+
return MessageEncoder::Status::Error;
111+
}
112+
113+
if(cbor_encode_byte_string(&array_encoder, ota->params.id, ID_SIZE) != CborNoError) {
114+
return MessageEncoder::Status::Error;
115+
}
116+
117+
if(cbor_encode_simple_value(&array_encoder, ota->params.state) != CborNoError) {
118+
return MessageEncoder::Status::Error;
119+
}
120+
121+
if(cbor_encode_int(&array_encoder, ota->params.state_data) != CborNoError) {
122+
return MessageEncoder::Status::Error;
123+
}
124+
125+
if(cbor_encode_uint(&array_encoder, ota->params.time) != CborNoError) {
126+
return MessageEncoder::Status::Error;
127+
}
128+
129+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
130+
return MessageEncoder::Status::Error;
131+
}
132+
133+
return MessageEncoder::Status::Complete;
134+
}
135+
136+
MessageEncoder::Status TimezoneCommandUpEncoder::encode(CborEncoder* encoder, Message *msg) {
137+
// This command contains no parameters, it contains just the id of the message
138+
// nothing to perform here
139+
// (void)(encoder);
140+
(void)(msg);
141+
CborEncoder array_encoder;
142+
143+
// FIXME we are encoiding an empty array, this could be avoided
144+
if (cbor_encoder_create_array(encoder, &array_encoder, 0) != CborNoError){
145+
return MessageEncoder::Status::Error;
146+
}
147+
148+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
149+
return MessageEncoder::Status::Error;
150+
}
151+
152+
return MessageEncoder::Status::Complete;
153+
}
154+
155+
static OtaBeginCommandEncoder otaBeginCommandEncoder;
156+
static ThingBeginCommandEncoder thingBeginCommandEncoder;
157+
static LastValuesBeginCommandEncoder lastValuesBeginCommandEncoder;
158+
static DeviceBeginCommandEncoder deviceBeginCommandEncoder;
159+
static OtaProgressCommandUpEncoder otaProgressCommandUpEncoder;
160+
static TimezoneCommandUpEncoder timezoneCommandUpEncoder;

src/cbor/IoTCloudMessageEncoder.h

Lines changed: 75 additions & 0 deletions
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)