forked from arduino-libraries/ArduinoIoTCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageEncoder.cpp
174 lines (145 loc) · 5.9 KB
/
MessageEncoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
This file is part of the ArduinoIoTCloud library.
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/******************************************************************************
* INCLUDE
******************************************************************************/
#include "CBOREncoder.h"
#undef max
#undef min
#include <algorithm>
#include <iterator>
#include "MessageEncoder.h"
/******************************************************************************
* PUBLIC MEMBER FUNCTIONS
******************************************************************************/
Encoder::Status CBORMessageEncoder::encode(Message * message, uint8_t * data, size_t& len)
{
EncoderState current_state = EncoderState::EncodeTag,
next_state = EncoderState::Error;
CborEncoder encoder;
CborEncoder arrayEncoder;
cbor_encoder_init(&encoder, data, len, 0);
while (current_state != EncoderState::Complete) {
switch (current_state) {
case EncoderState::EncodeTag : next_state = handle_EncodeTag(&encoder, message); break;
case EncoderState::EncodeArray : next_state = handle_EncodeArray(&encoder, &arrayEncoder, message); break;
case EncoderState::EncodeParam : next_state = handle_EncodeParam(&arrayEncoder, message); break;
case EncoderState::CloseArray : next_state = handle_CloseArray(&encoder, &arrayEncoder); break;
case EncoderState::Complete : /* Nothing to do */ break;
case EncoderState::MessageNotSupported :
case EncoderState::Error : return Encoder::Status::Error;
}
current_state = next_state;
}
len = cbor_encoder_get_buffer_size(&encoder, data);
return Encoder::Status::Complete;
}
/******************************************************************************
PRIVATE MEMBER FUNCTIONS
******************************************************************************/
CBORMessageEncoder::EncoderState CBORMessageEncoder::handle_EncodeTag(CborEncoder * encoder, Message * message)
{
CborTag commandTag = toCBORCommandTag(message->id);
if (commandTag == CBORCommandTag::CBORUnknownCmdTag16b ||
commandTag == CBORCommandTag::CBORUnknownCmdTag32b ||
commandTag == CBORCommandTag::CBORUnknownCmdTag64b ||
cbor_encode_tag(encoder, commandTag) != CborNoError) {
return EncoderState::Error;
}
return EncoderState::EncodeArray;
}
CBORMessageEncoder::EncoderState CBORMessageEncoder::handle_EncodeArray(CborEncoder * encoder, CborEncoder * array_encoder, Message * message)
{
// Set array size based on the message id
size_t array_size = 0;
switch (message->id)
{
case CommandId::OtaBeginUpId:
array_size = 1;
break;
case CommandId::ThingBeginCmdId:
array_size = 1;
break;
case CommandId::DeviceBeginCmdId:
array_size = 1;
break;
case CommandId::LastValuesBeginCmdId:
break;
case CommandId::OtaProgressCmdUpId:
array_size = 4;
break;
case CommandId::TimezoneCommandUpId:
break;
default:
return EncoderState::MessageNotSupported;
}
// Start an array with fixed width based on message type
if (cbor_encoder_create_array(encoder, array_encoder, array_size) != CborNoError){
return EncoderState::Error;
}
return EncoderState::EncodeParam;
}
CBORMessageEncoder::EncoderState CBORMessageEncoder::handle_EncodeParam(CborEncoder * array_encoder, Message * message)
{
CborError error = CborNoError;
switch (message->id)
{
case CommandId::OtaBeginUpId:
error = CBORMessageEncoder::encodeOtaBeginUp(array_encoder, message);
break;
case CommandId::ThingBeginCmdId:
error = CBORMessageEncoder::encodeThingBeginCmd(array_encoder, message);
break;
case CommandId::DeviceBeginCmdId:
error = CBORMessageEncoder::encodeDeviceBeginCmd(array_encoder, message);
break;
case CommandId::LastValuesBeginCmdId:
break;
case CommandId::OtaProgressCmdUpId:
error = CBORMessageEncoder::encodeOtaProgressCmdUp(array_encoder, message);
break;
case CommandId::TimezoneCommandUpId:
break;
default:
return EncoderState::MessageNotSupported;
}
return (error != CborNoError) ? EncoderState::Error : EncoderState::CloseArray;
}
CBORMessageEncoder::EncoderState CBORMessageEncoder::handle_CloseArray(CborEncoder * encoder, CborEncoder * array_encoder)
{
CborError error = cbor_encoder_close_container(encoder, array_encoder);
return (error != CborNoError) ? EncoderState::Error : EncoderState::Complete;
}
// Message specific encoders
CborError CBORMessageEncoder::encodeOtaBeginUp(CborEncoder * array_encoder, Message * message)
{
OtaBeginUp * otaBeginUp = (OtaBeginUp *) message;
CHECK_CBOR(cbor_encode_byte_string(array_encoder, otaBeginUp->params.sha, SHA256_SIZE));
return CborNoError;
}
CborError CBORMessageEncoder::encodeThingBeginCmd(CborEncoder * array_encoder, Message * message)
{
ThingBeginCmd * thingBeginCmd = (ThingBeginCmd *) message;
CHECK_CBOR(cbor_encode_text_stringz(array_encoder, thingBeginCmd->params.thing_id));
return CborNoError;
}
CborError CBORMessageEncoder::encodeDeviceBeginCmd(CborEncoder * array_encoder, Message * message)
{
DeviceBeginCmd * deviceBeginCmd = (DeviceBeginCmd *) message;
CHECK_CBOR(cbor_encode_text_stringz(array_encoder, deviceBeginCmd->params.lib_version));
return CborNoError;
}
CborError CBORMessageEncoder::encodeOtaProgressCmdUp(CborEncoder * array_encoder, Message * message)
{
OtaProgressCmdUp * ota = (OtaProgressCmdUp *)message;
CHECK_CBOR(cbor_encode_byte_string(array_encoder, ota->params.id, ID_SIZE));
CHECK_CBOR(cbor_encode_simple_value(array_encoder, ota->params.state));
CHECK_CBOR(cbor_encode_int(array_encoder, ota->params.state_data));
CHECK_CBOR(cbor_encode_uint(array_encoder, ota->params.time));
return CborNoError;
}