diff --git a/extras/test/CMakeLists.txt b/extras/test/CMakeLists.txt index b604c53..84a625c 100644 --- a/extras/test/CMakeLists.txt +++ b/extras/test/CMakeLists.txt @@ -30,6 +30,7 @@ set(TEST_SRCS src/hex/test_hex.cpp src/cbor/test_cbor_encoder.cpp src/cbor/test_cbor_decoder.cpp + src/cbor/test_cbor_standard_enc.cpp src/time/test_TimedAttempt.cpp ) @@ -40,6 +41,7 @@ set(TEST_DUT_SRCS ../../src/hex/chex.h ../../src/cbor/MessageDecoder.cpp ../../src/cbor/MessageEncoder.cpp + ../../src/cbor/standards/StandardEncoders.cpp ../../src/cbor/tinycbor ../../src/cbor/tinycbor/src/cborencoder.c ../../src/cbor/tinycbor/src/cborencoder_close_container_checked.c diff --git a/extras/test/src/cbor/test_cbor_standard_enc.cpp b/extras/test/src/cbor/test_cbor_standard_enc.cpp new file mode 100644 index 0000000..bb42548 --- /dev/null +++ b/extras/test/src/cbor/test_cbor_standard_enc.cpp @@ -0,0 +1,48 @@ +/* + This file is part of the Arduino_CloudUtils 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 +#include +#include + +/****************************************************************************** + TEST CODE + ******************************************************************************/ + +SCENARIO("Test the encoding of command messages") { + + WHEN("Encode a message with provisioning wifi fw version ") + { + WiFiFWVersionMessage command; + command.c.id = StandardMessageId::WiFiFWVersionMessageId; + command.params.wiFiFWVersion = "1.6.0"; + uint8_t buffer[512]; + size_t bytes_encoded = sizeof(buffer); + + CBORMessageEncoder encoder; + MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); + + uint8_t expected_result[] = { + 0xda, 0x00, 0x01, 0x20, 0x14, 0x81, 0x65, 0x31, 0x2E, 0x36, 0x2E, 0x30 + }; + + // Test the encoding is + //DA 00012014 # tag(73748) + // 81 # array(1) + // 65 # text(5) + // 312E362E30 # "1.6.0" + THEN("The encoding is successful") { + REQUIRE(err == MessageEncoder::Status::Complete); + REQUIRE(bytes_encoded == sizeof(expected_result)); + REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0); + } + } + +} diff --git a/src/cbor/standards/StandardEncoders.cpp b/src/cbor/standards/StandardEncoders.cpp new file mode 100644 index 0000000..a12c03b --- /dev/null +++ b/src/cbor/standards/StandardEncoders.cpp @@ -0,0 +1,32 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2025 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 "StandardEncoders.h" + +MessageEncoder::Status WiFiFWVersionMessageEncoder::encode(CborEncoder* encoder, Message *msg) { + WiFiFWVersionMessage * wiFiFWVersionMsg = (WiFiFWVersionMessage*) msg; + CborEncoder array_encoder; + + if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) { + return MessageEncoder::Status::Error; + } + + if(cbor_encode_text_stringz(&array_encoder, wiFiFWVersionMsg->params.wiFiFWVersion) != CborNoError) { + return MessageEncoder::Status::Error; + } + + if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) { + return MessageEncoder::Status::Error; + } + + return MessageEncoder::Status::Complete; +} + +static WiFiFWVersionMessageEncoder wifiFWVersionMessageEncoderCbor; diff --git a/src/cbor/standards/StandardEncoders.h b/src/cbor/standards/StandardEncoders.h new file mode 100644 index 0000000..16adb44 --- /dev/null +++ b/src/cbor/standards/StandardEncoders.h @@ -0,0 +1,21 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2025 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/. +*/ + +#pragma once + +#include "StandardMessages.h" + +class WiFiFWVersionMessageEncoder: public CBORMessageEncoderInterface { +public: + WiFiFWVersionMessageEncoder() + : CBORMessageEncoderInterface(CBORWiFiFWVersionMessage, WiFiFWVersionMessageId) {} +protected: + MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; +}; diff --git a/src/cbor/standards/StandardMessages.h b/src/cbor/standards/StandardMessages.h new file mode 100644 index 0000000..174bf59 --- /dev/null +++ b/src/cbor/standards/StandardMessages.h @@ -0,0 +1,29 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2025 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/. +*/ + +#pragma once +#include "../MessageEncoder.h" + +enum CBORStandardMessageTag: CBORTag { + + CBORWiFiFWVersionMessage = 0x012014 //Next tag starts at 0x013000 +}; + +enum StandardMessageId: MessageId { + /* Standard commands*/ + WiFiFWVersionMessageId = ArduinoStandardMessageStartId, +}; + +struct WiFiFWVersionMessage { + Message c; + struct { + const char *wiFiFWVersion; //The payload is a string. + } params; +}; diff --git a/src/interfaces/message.h b/src/interfaces/message.h index ba09c87..40aeede 100644 --- a/src/interfaces/message.h +++ b/src/interfaces/message.h @@ -29,6 +29,7 @@ struct Message { * and boundaries and avoid value clashing */ enum : MessageId { + ArduinoStandardMessageStartId = 0x000, ArduinoIOTCloudStartMessageId = 0x100, ArduinoProvisioningStartMessageId = 0x200, };