|
| 1 | +/* |
| 2 | + This file is part of the Arduino_CloudUtils 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 | +#include "CborEncoder.h" |
| 11 | + |
| 12 | +// static CBORMessageEncoderSingleton* singleton = nullptr; |
| 13 | + |
| 14 | +Encoder::Status CBORMessageEncoderSingleton::encode(Message* message, uint8_t * data, size_t& len) { // TODO do we need to propagate the maximum length? |
| 15 | + // prepare cbor structure |
| 16 | + CborEncoder encoder; |
| 17 | + |
| 18 | + cbor_encoder_init(&encoder, data, len, 0); |
| 19 | + |
| 20 | + auto encoder_it = encoders.find(message->id); |
| 21 | + |
| 22 | + // check if message.id exists on the encoders list or return error |
| 23 | + if(encoder_it == encoders.end()) { |
| 24 | + return Encoder::Status::Error; |
| 25 | + } |
| 26 | + |
| 27 | + // encode the message |
| 28 | + if(encoder_it->second->_encode(&encoder, message) == Encoder::Status::Error) { |
| 29 | + return Encoder::Status::Error; |
| 30 | + } |
| 31 | + |
| 32 | + len = cbor_encoder_get_buffer_size(&encoder, data); |
| 33 | + |
| 34 | + return Encoder::Status::Complete; |
| 35 | +} |
| 36 | + |
| 37 | +CBORMessageEncoderSingleton& CBORMessageEncoderSingleton::getInstance() { |
| 38 | + static CBORMessageEncoderSingleton singleton; |
| 39 | + |
| 40 | + return singleton; |
| 41 | +} |
| 42 | + |
| 43 | +CBORMessageEncoderInterface::CBORMessageEncoderInterface(const CBORTag tag, const MessageId id) |
| 44 | +: tag(tag), id(id) { |
| 45 | + // call singleton/global variable and insert this encoder |
| 46 | + CBORMessageEncoderSingleton::getInstance().append(id, this); |
| 47 | +} |
| 48 | + |
| 49 | +Encoder::Status CBORMessageEncoderInterface::_encode(CborEncoder* encoder, Message *msg) { |
| 50 | + // this must always be true, it could mean that there are issues in the map of encoders |
| 51 | + assert(msg->id == id); |
| 52 | + |
| 53 | + if (tag == cbor::tag::CBORUnknownCmdTag16b || |
| 54 | + tag == cbor::tag::CBORUnknownCmdTag32b || |
| 55 | + tag == cbor::tag::CBORUnknownCmdTag64b || |
| 56 | + cbor_encode_tag(encoder, tag) != CborNoError) { |
| 57 | + return Encoder::Status::Error; |
| 58 | + } |
| 59 | + |
| 60 | + return this->encode(encoder, msg); |
| 61 | +} |
0 commit comments