|
24 | 24 | #undef max
|
25 | 25 | #undef min
|
26 | 26 | #include <algorithm>
|
| 27 | +#include <iterator> |
27 | 28 |
|
28 | 29 | #include "lib/tinycbor/cbor-lib.h"
|
29 | 30 |
|
30 | 31 | /******************************************************************************
|
31 | 32 | * PUBLIC MEMBER FUNCTIONS
|
32 | 33 | ******************************************************************************/
|
33 | 34 |
|
34 |
| -CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t * data, size_t const size, int & bytes_encoded, bool lightPayload) |
| 35 | +CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t * data, size_t const size, int & bytes_encoded, unsigned int & current_property_index, bool lightPayload) |
35 | 36 | {
|
36 |
| - CborEncoder encoder, arrayEncoder; |
| 37 | + EncoderState current_state = EncoderState::InitPropertyEncoder, |
| 38 | + next_state = EncoderState::InitPropertyEncoder; |
37 | 39 |
|
38 |
| - cbor_encoder_init(&encoder, data, size, 0); |
| 40 | + PropertyContainerEncoder propertyEncoder(property_container, current_property_index); |
39 | 41 |
|
40 |
| - CHECK_CBOR(cbor_encoder_create_array(&encoder, &arrayEncoder, CborIndefiniteLength)); |
| 42 | + while (current_state != EncoderState::SendMessage) { |
41 | 43 |
|
42 |
| - /* Check if backing storage and cloud has diverged |
43 |
| - * time interval may be elapsed or property may be changed |
| 44 | + switch (current_state) { |
| 45 | + case EncoderState::InitPropertyEncoder : next_state = handle_InitPropertyEncoder(propertyEncoder); break; |
| 46 | + case EncoderState::OpenCBORContainer : next_state = handle_OpenCBORContainer(propertyEncoder, data, size); break; |
| 47 | + case EncoderState::TryAppend : next_state = handle_TryAppend(propertyEncoder, lightPayload); break; |
| 48 | + case EncoderState::OutOfMemory : next_state = handle_OutOfMemory(propertyEncoder); break; |
| 49 | + case EncoderState::SkipProperty : next_state = handle_SkipProperty(propertyEncoder); break; |
| 50 | + case EncoderState::TrimAppend : next_state = handle_TrimAppend(propertyEncoder); break; |
| 51 | + case EncoderState::CloseCBORContainer : next_state = handle_CloseCBORContainer(propertyEncoder); break; |
| 52 | + case EncoderState::TrimClose : next_state = handle_TrimClose(propertyEncoder); break; |
| 53 | + case EncoderState::FinishAppend : next_state = handle_FinishAppend(propertyEncoder); break; |
| 54 | + case EncoderState::SendMessage : /* Nothing to do */ break; |
| 55 | + case EncoderState::Error : return CborErrorInternalError; break; |
| 56 | + } |
| 57 | + |
| 58 | + current_state = next_state; |
| 59 | + } |
| 60 | + |
| 61 | + if (propertyEncoder.encoded_property_count > 0) |
| 62 | + bytes_encoded = cbor_encoder_get_buffer_size(&propertyEncoder.encoder, data); |
| 63 | + else |
| 64 | + bytes_encoded = 0; |
| 65 | + |
| 66 | + return CborNoError; |
| 67 | +} |
| 68 | + |
| 69 | +/****************************************************************************** |
| 70 | + PRIVATE MEMBER FUNCTIONS |
| 71 | + ******************************************************************************/ |
| 72 | + |
| 73 | +CBOREncoder::EncoderState CBOREncoder::handle_InitPropertyEncoder(PropertyContainerEncoder & propertyEncoder) |
| 74 | +{ |
| 75 | + propertyEncoder.encoded_property_count = 0; |
| 76 | + propertyEncoder.checked_property_count = 0; |
| 77 | + propertyEncoder.encoded_property_limit = 0; |
| 78 | + propertyEncoder.property_limit_active = false; |
| 79 | + return EncoderState::OpenCBORContainer; |
| 80 | +} |
| 81 | + |
| 82 | +CBOREncoder::EncoderState CBOREncoder::handle_OpenCBORContainer(PropertyContainerEncoder & propertyEncoder, uint8_t * data, size_t const size) |
| 83 | +{ |
| 84 | + propertyEncoder.encoded_property_count = 0; |
| 85 | + propertyEncoder.checked_property_count = 0; |
| 86 | + cbor_encoder_init(&propertyEncoder.encoder, data, size, 0); |
| 87 | + cbor_encoder_create_array(&propertyEncoder.encoder, &propertyEncoder.arrayEncoder, CborIndefiniteLength); |
| 88 | + return EncoderState::TryAppend; |
| 89 | +} |
| 90 | + |
| 91 | +CBOREncoder::EncoderState CBOREncoder::handle_TryAppend(PropertyContainerEncoder & propertyEncoder, bool & lightPayload) |
| 92 | +{ |
| 93 | + /* Check if backing storage and cloud has diverged. Time interval may be elapsed or property may be changed |
44 | 94 | * and if that's the case encode the property into the CBOR.
|
45 | 95 | */
|
46 | 96 | CborError error = CborNoError;
|
47 |
| - int num_encoded_properties = 0; |
48 |
| - std::for_each(property_container.begin(), |
49 |
| - property_container.end(), |
50 |
| - [lightPayload, &arrayEncoder, &error, &num_encoded_properties](Property * p) |
51 |
| - { |
52 |
| - if (p->shouldBeUpdated() && p->isReadableByCloud()) |
53 |
| - { |
54 |
| - error = p->append(&arrayEncoder, lightPayload); |
55 |
| - if(error == CborNoError) |
56 |
| - num_encoded_properties++; |
57 |
| - else |
58 |
| - return; |
59 |
| - } |
60 |
| - }); |
61 |
| - if ((CborNoError != error) && |
62 |
| - (CborErrorOutOfMemory != error)) |
63 |
| - return error; |
64 |
| - |
65 |
| - CHECK_CBOR(cbor_encoder_close_container(&encoder, &arrayEncoder)); |
66 |
| - |
67 |
| - if (num_encoded_properties > 0) |
68 |
| - bytes_encoded = cbor_encoder_get_buffer_size(&encoder, data); |
| 97 | + PropertyContainer::iterator iter = propertyEncoder.property_container.begin(); |
| 98 | + std::advance(iter, propertyEncoder.current_property_index); |
| 99 | + |
| 100 | + for(; iter != propertyEncoder.property_container.end(); iter++) |
| 101 | + { |
| 102 | + Property * p = * iter; |
| 103 | + |
| 104 | + if (p->shouldBeUpdated() && p->isReadableByCloud()) |
| 105 | + { |
| 106 | + error = p->append(&propertyEncoder.arrayEncoder, lightPayload); |
| 107 | + if(error == CborNoError) |
| 108 | + propertyEncoder.encoded_property_count++; |
| 109 | + } |
| 110 | + if(error == CborNoError) |
| 111 | + propertyEncoder.checked_property_count++; |
| 112 | + |
| 113 | + bool const maximum_number_of_properties_reached = (propertyEncoder.encoded_property_count >= propertyEncoder.encoded_property_limit) && (propertyEncoder.property_limit_active == true); |
| 114 | + bool const cbor_encoder_error = (error != CborNoError); |
| 115 | + |
| 116 | + if (maximum_number_of_properties_reached || cbor_encoder_error) |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + if (CborErrorOutOfMemory == error) |
| 121 | + return EncoderState::OutOfMemory; |
| 122 | + else if (CborNoError == error) |
| 123 | + return EncoderState::CloseCBORContainer; |
| 124 | + else if (CborErrorSplitItems == error) |
| 125 | + return EncoderState::TrimAppend; |
69 | 126 | else
|
70 |
| - bytes_encoded = 0; |
| 127 | + return EncoderState::Error; |
| 128 | +} |
71 | 129 |
|
72 |
| - return CborNoError; |
| 130 | +CBOREncoder::EncoderState CBOREncoder::handle_OutOfMemory(PropertyContainerEncoder & propertyEncoder) |
| 131 | +{ |
| 132 | + if(propertyEncoder.encoded_property_count > 0) |
| 133 | + return EncoderState::CloseCBORContainer; |
| 134 | + else |
| 135 | + return EncoderState::SkipProperty; |
| 136 | +} |
| 137 | + |
| 138 | +CBOREncoder::EncoderState CBOREncoder::handle_SkipProperty(PropertyContainerEncoder & propertyEncoder) |
| 139 | +{ |
| 140 | + /* Better to skip this property otherwise we will stay blocked here. This happens only with a message property |
| 141 | + * that not fits into the CBOR buffer |
| 142 | + */ |
| 143 | + propertyEncoder.current_property_index++; |
| 144 | + if(propertyEncoder.current_property_index >= propertyEncoder.property_container.size()) |
| 145 | + propertyEncoder.current_property_index = 0; |
| 146 | + return EncoderState::Error; |
| 147 | +} |
| 148 | + |
| 149 | +CBOREncoder::EncoderState CBOREncoder::handle_TrimAppend(PropertyContainerEncoder & propertyEncoder) |
| 150 | +{ |
| 151 | + /* Trim the number of properties to be included in the next message to avoid multivalue property split */ |
| 152 | + propertyEncoder.encoded_property_limit = propertyEncoder.encoded_property_count; |
| 153 | + propertyEncoder.property_limit_active = true; |
| 154 | + if(propertyEncoder.encoded_property_limit > 0) |
| 155 | + return EncoderState::OpenCBORContainer; |
| 156 | + else |
| 157 | + return EncoderState::Error; |
| 158 | +} |
| 159 | + |
| 160 | +CBOREncoder::EncoderState CBOREncoder::handle_CloseCBORContainer(PropertyContainerEncoder & propertyEncoder) |
| 161 | +{ |
| 162 | + CborError error = cbor_encoder_close_container(&propertyEncoder.encoder, &propertyEncoder.arrayEncoder); |
| 163 | + if (CborNoError != error) |
| 164 | + return EncoderState::TrimClose; |
| 165 | + else |
| 166 | + return EncoderState::FinishAppend; |
| 167 | +} |
| 168 | + |
| 169 | +CBOREncoder::EncoderState CBOREncoder::handle_TrimClose(PropertyContainerEncoder & propertyEncoder) |
| 170 | +{ |
| 171 | + /* Trim the number of properties to be included in the next message to avoid error closing container */ |
| 172 | + propertyEncoder.encoded_property_limit = propertyEncoder.encoded_property_count - 1; |
| 173 | + propertyEncoder.property_limit_active = true; |
| 174 | + if(propertyEncoder.encoded_property_limit > 0) |
| 175 | + return EncoderState::OpenCBORContainer; |
| 176 | + else |
| 177 | + return EncoderState::Error; |
| 178 | +} |
| 179 | + |
| 180 | +CBOREncoder::EncoderState CBOREncoder::handle_FinishAppend(PropertyContainerEncoder & propertyEncoder) |
| 181 | +{ |
| 182 | + /* Restore property message limit to CBOR_ENCODER_NO_PROPERTIES_LIMIT */ |
| 183 | + propertyEncoder.property_limit_active = false; |
| 184 | + |
| 185 | + /* The append process has been successful, so we don't need to terty to send this properties set. Cleanup _has_been_appended_but_not_sended flag */ |
| 186 | + PropertyContainer::iterator iter = propertyEncoder.property_container.begin(); |
| 187 | + std::advance(iter, propertyEncoder.current_property_index); |
| 188 | + int num_appended_properties = 0; |
| 189 | + |
| 190 | + for(; iter != propertyEncoder.property_container.end(); iter++) |
| 191 | + { |
| 192 | + Property * p = * iter; |
| 193 | + if (num_appended_properties >= propertyEncoder.checked_property_count) |
| 194 | + break; |
| 195 | + |
| 196 | + p->appendCompleted(); |
| 197 | + num_appended_properties++; |
| 198 | + } |
| 199 | + |
| 200 | + /* Advance property index for the nex message */ |
| 201 | + propertyEncoder.current_property_index += propertyEncoder.checked_property_count; |
| 202 | + |
| 203 | + if(propertyEncoder.current_property_index >= propertyEncoder.property_container.size()) |
| 204 | + propertyEncoder.current_property_index = 0; |
| 205 | + |
| 206 | + return EncoderState::SendMessage; |
73 | 207 | }
|
0 commit comments