|
| 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; |
0 commit comments