|
14 | 14 |
|
15 | 15 | #include "CBOREncoder.h"
|
16 | 16 |
|
17 |
| -#undef max |
18 |
| -#undef min |
19 |
| -#include <algorithm> |
20 |
| -#include <iterator> |
21 |
| - |
22 | 17 | #include "MessageEncoder.h"
|
23 | 18 |
|
24 | 19 | /******************************************************************************
|
25 | 20 | * PUBLIC MEMBER FUNCTIONS
|
26 | 21 | ******************************************************************************/
|
27 | 22 |
|
28 |
| -Encoder::Status CBORMessageEncoder::encode(Message * message, uint8_t * data, size_t& len) |
29 |
| -{ |
30 |
| - EncoderState current_state = EncoderState::EncodeTag, |
31 |
| - next_state = EncoderState::Error; |
32 |
| - |
33 |
| - CborEncoder encoder; |
34 |
| - CborEncoder arrayEncoder; |
35 | 23 |
|
36 |
| - cbor_encoder_init(&encoder, data, len, 0); |
| 24 | +/****************************************************************************** |
| 25 | + PRIVATE MEMBER FUNCTIONS |
| 26 | + ******************************************************************************/ |
37 | 27 |
|
38 |
| - while (current_state != EncoderState::Complete) { |
| 28 | +Encoder::Status OtaBeginCommandEncoder::encode(CborEncoder* encoder, Message *msg) { |
| 29 | + OtaBeginUp * otaBeginUp = (OtaBeginUp*) msg; |
| 30 | + CborEncoder array_encoder; |
39 | 31 |
|
40 |
| - switch (current_state) { |
41 |
| - case EncoderState::EncodeTag : next_state = handle_EncodeTag(&encoder, message); break; |
42 |
| - case EncoderState::EncodeArray : next_state = handle_EncodeArray(&encoder, &arrayEncoder, message); break; |
43 |
| - case EncoderState::EncodeParam : next_state = handle_EncodeParam(&arrayEncoder, message); break; |
44 |
| - case EncoderState::CloseArray : next_state = handle_CloseArray(&encoder, &arrayEncoder); break; |
45 |
| - case EncoderState::Complete : /* Nothing to do */ break; |
46 |
| - case EncoderState::MessageNotSupported : |
47 |
| - case EncoderState::Error : return Encoder::Status::Error; |
48 |
| - } |
| 32 | + if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) { |
| 33 | + return Encoder::Status::Error; |
| 34 | + } |
49 | 35 |
|
50 |
| - current_state = next_state; |
| 36 | + if(cbor_encode_byte_string(&array_encoder, otaBeginUp->params.sha, SHA256_SIZE) != CborNoError) { |
| 37 | + return Encoder::Status::Error; |
51 | 38 | }
|
52 | 39 |
|
53 |
| - len = cbor_encoder_get_buffer_size(&encoder, data); |
| 40 | + if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) { |
| 41 | + return Encoder::Status::Error; |
| 42 | + } |
54 | 43 |
|
55 | 44 | return Encoder::Status::Complete;
|
56 | 45 | }
|
57 | 46 |
|
58 |
| -/****************************************************************************** |
59 |
| - PRIVATE MEMBER FUNCTIONS |
60 |
| - ******************************************************************************/ |
| 47 | +Encoder::Status ThingBeginCommandEncoder::encode(CborEncoder* encoder, Message *msg) { |
| 48 | + ThingBeginCmd * thingBeginCmd = (ThingBeginCmd*) msg; |
| 49 | + CborEncoder array_encoder; |
61 | 50 |
|
62 |
| -CBORMessageEncoder::EncoderState CBORMessageEncoder::handle_EncodeTag(CborEncoder * encoder, Message * message) |
63 |
| -{ |
64 |
| - CborTag commandTag = toCBORCommandTag(message->id); |
65 |
| - if (commandTag == CBORCommandTag::CBORUnknownCmdTag16b || |
66 |
| - commandTag == CBORCommandTag::CBORUnknownCmdTag32b || |
67 |
| - commandTag == CBORCommandTag::CBORUnknownCmdTag64b || |
68 |
| - cbor_encode_tag(encoder, commandTag) != CborNoError) { |
69 |
| - return EncoderState::Error; |
| 51 | + if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) { |
| 52 | + return Encoder::Status::Error; |
70 | 53 | }
|
71 | 54 |
|
72 |
| - return EncoderState::EncodeArray; |
73 |
| -} |
| 55 | + if(cbor_encode_text_stringz(&array_encoder, thingBeginCmd->params.thing_id) != CborNoError) { |
| 56 | + return Encoder::Status::Error; |
| 57 | + } |
74 | 58 |
|
75 |
| -CBORMessageEncoder::EncoderState CBORMessageEncoder::handle_EncodeArray(CborEncoder * encoder, CborEncoder * array_encoder, Message * message) |
76 |
| -{ |
77 |
| - // Set array size based on the message id |
78 |
| - size_t array_size = 0; |
79 |
| - switch (message->id) |
80 |
| - { |
81 |
| - case CommandId::OtaBeginUpId: |
82 |
| - array_size = 1; |
83 |
| - break; |
84 |
| - case CommandId::ThingBeginCmdId: |
85 |
| - array_size = 1; |
86 |
| - break; |
87 |
| - case CommandId::DeviceBeginCmdId: |
88 |
| - array_size = 1; |
89 |
| - break; |
90 |
| - case CommandId::LastValuesBeginCmdId: |
91 |
| - break; |
92 |
| - case CommandId::OtaProgressCmdUpId: |
93 |
| - array_size = 4; |
94 |
| - break; |
95 |
| - case CommandId::TimezoneCommandUpId: |
96 |
| - break; |
97 |
| - default: |
98 |
| - return EncoderState::MessageNotSupported; |
99 |
| - } |
100 |
| - |
101 |
| - // Start an array with fixed width based on message type |
102 |
| - if (cbor_encoder_create_array(encoder, array_encoder, array_size) != CborNoError){ |
103 |
| - return EncoderState::Error; |
104 |
| - } |
105 |
| - |
106 |
| - return EncoderState::EncodeParam; |
107 |
| -} |
| 59 | + if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) { |
| 60 | + return Encoder::Status::Error; |
| 61 | + } |
108 | 62 |
|
109 |
| -CBORMessageEncoder::EncoderState CBORMessageEncoder::handle_EncodeParam(CborEncoder * array_encoder, Message * message) |
110 |
| -{ |
111 |
| - CborError error = CborNoError; |
112 |
| - switch (message->id) |
113 |
| - { |
114 |
| - case CommandId::OtaBeginUpId: |
115 |
| - error = CBORMessageEncoder::encodeOtaBeginUp(array_encoder, message); |
116 |
| - break; |
117 |
| - case CommandId::ThingBeginCmdId: |
118 |
| - error = CBORMessageEncoder::encodeThingBeginCmd(array_encoder, message); |
119 |
| - break; |
120 |
| - case CommandId::DeviceBeginCmdId: |
121 |
| - error = CBORMessageEncoder::encodeDeviceBeginCmd(array_encoder, message); |
122 |
| - break; |
123 |
| - case CommandId::LastValuesBeginCmdId: |
124 |
| - break; |
125 |
| - case CommandId::OtaProgressCmdUpId: |
126 |
| - error = CBORMessageEncoder::encodeOtaProgressCmdUp(array_encoder, message); |
127 |
| - break; |
128 |
| - case CommandId::TimezoneCommandUpId: |
129 |
| - break; |
130 |
| - default: |
131 |
| - return EncoderState::MessageNotSupported; |
132 |
| - } |
133 |
| - |
134 |
| - return (error != CborNoError) ? EncoderState::Error : EncoderState::CloseArray; |
| 63 | + return Encoder::Status::Complete; |
135 | 64 | }
|
136 | 65 |
|
137 |
| -CBORMessageEncoder::EncoderState CBORMessageEncoder::handle_CloseArray(CborEncoder * encoder, CborEncoder * array_encoder) |
138 |
| -{ |
139 |
| - CborError error = cbor_encoder_close_container(encoder, array_encoder); |
| 66 | +Encoder::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; |
140 | 72 |
|
141 |
| - return (error != CborNoError) ? EncoderState::Error : EncoderState::Complete; |
142 |
| -} |
| 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 Encoder::Status::Error; |
| 76 | + } |
| 77 | + |
| 78 | + if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) { |
| 79 | + return Encoder::Status::Error; |
| 80 | + } |
143 | 81 |
|
144 |
| -// Message specific encoders |
145 |
| -CborError CBORMessageEncoder::encodeOtaBeginUp(CborEncoder * array_encoder, Message * message) |
146 |
| -{ |
147 |
| - OtaBeginUp * otaBeginUp = (OtaBeginUp *) message; |
148 |
| - CHECK_CBOR(cbor_encode_byte_string(array_encoder, otaBeginUp->params.sha, SHA256_SIZE)); |
149 |
| - return CborNoError; |
| 82 | + return Encoder::Status::Complete; |
150 | 83 | }
|
151 | 84 |
|
152 |
| -CborError CBORMessageEncoder::encodeThingBeginCmd(CborEncoder * array_encoder, Message * message) |
153 |
| -{ |
154 |
| - ThingBeginCmd * thingBeginCmd = (ThingBeginCmd *) message; |
155 |
| - CHECK_CBOR(cbor_encode_text_stringz(array_encoder, thingBeginCmd->params.thing_id)); |
156 |
| - return CborNoError; |
| 85 | +Encoder::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 Encoder::Status::Error; |
| 91 | + } |
| 92 | + |
| 93 | + if(cbor_encode_text_stringz(&array_encoder, deviceBeginCmd->params.lib_version) != CborNoError) { |
| 94 | + return Encoder::Status::Error; |
| 95 | + } |
| 96 | + |
| 97 | + if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) { |
| 98 | + return Encoder::Status::Error; |
| 99 | + } |
| 100 | + |
| 101 | + return Encoder::Status::Complete; |
157 | 102 | }
|
158 | 103 |
|
159 |
| -CborError CBORMessageEncoder::encodeDeviceBeginCmd(CborEncoder * array_encoder, Message * message) |
160 |
| -{ |
161 |
| - DeviceBeginCmd * deviceBeginCmd = (DeviceBeginCmd *) message; |
162 |
| - CHECK_CBOR(cbor_encode_text_stringz(array_encoder, deviceBeginCmd->params.lib_version)); |
163 |
| - return CborNoError; |
| 104 | +Encoder::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 Encoder::Status::Error; |
| 110 | + } |
| 111 | + |
| 112 | + if(cbor_encode_byte_string(&array_encoder, ota->params.id, ID_SIZE) != CborNoError) { |
| 113 | + return Encoder::Status::Error; |
| 114 | + } |
| 115 | + |
| 116 | + if(cbor_encode_simple_value(&array_encoder, ota->params.state) != CborNoError) { |
| 117 | + return Encoder::Status::Error; |
| 118 | + } |
| 119 | + |
| 120 | + if(cbor_encode_int(&array_encoder, ota->params.state_data) != CborNoError) { |
| 121 | + return Encoder::Status::Error; |
| 122 | + } |
| 123 | + |
| 124 | + if(cbor_encode_uint(&array_encoder, ota->params.time) != CborNoError) { |
| 125 | + return Encoder::Status::Error; |
| 126 | + } |
| 127 | + |
| 128 | + if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) { |
| 129 | + return Encoder::Status::Error; |
| 130 | + } |
| 131 | + |
| 132 | + return Encoder::Status::Complete; |
164 | 133 | }
|
165 | 134 |
|
166 |
| -CborError CBORMessageEncoder::encodeOtaProgressCmdUp(CborEncoder * array_encoder, Message * message) |
167 |
| -{ |
168 |
| - OtaProgressCmdUp * ota = (OtaProgressCmdUp *)message; |
169 |
| - CHECK_CBOR(cbor_encode_byte_string(array_encoder, ota->params.id, ID_SIZE)); |
170 |
| - CHECK_CBOR(cbor_encode_simple_value(array_encoder, ota->params.state)); |
171 |
| - CHECK_CBOR(cbor_encode_int(array_encoder, ota->params.state_data)); |
172 |
| - CHECK_CBOR(cbor_encode_uint(array_encoder, ota->params.time)); |
173 |
| - return CborNoError; |
| 135 | +Encoder::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 Encoder::Status::Error; |
| 145 | + } |
| 146 | + |
| 147 | + if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) { |
| 148 | + return Encoder::Status::Error; |
| 149 | + } |
| 150 | + |
| 151 | + return Encoder::Status::Complete; |
174 | 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; |
0 commit comments