From 30dbe5368e31d01e69801374a76bebe22abc6f1b Mon Sep 17 00:00:00 2001 From: fabik111 Date: Tue, 3 Jun 2025 18:12:30 +0200 Subject: [PATCH 1/2] add standard encoder for version message and wifi fw version --- extras/test/CMakeLists.txt | 2 + .../test/src/cbor/test_cbor_standard_enc.cpp | 82 +++++++++++++++++++ src/cbor/standards/StandardEncoders.cpp | 32 ++++++++ src/cbor/standards/StandardEncoders.h | 21 +++++ src/cbor/standards/StandardMessages.h | 29 +++++++ src/interfaces/message.h | 2 + 6 files changed, 168 insertions(+) create mode 100644 extras/test/src/cbor/test_cbor_standard_enc.cpp create mode 100644 src/cbor/standards/StandardEncoders.cpp create mode 100644 src/cbor/standards/StandardEncoders.h create mode 100644 src/cbor/standards/StandardMessages.h 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..ef44def --- /dev/null +++ b/extras/test/src/cbor/test_cbor_standard_enc.cpp @@ -0,0 +1,82 @@ +/* + 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 ") + { + VersionMessage command; + command.c.id = StandardMessageId::WiFiFWVersionMessageId; + command.params.version = "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); + } + } + + WHEN("Error encoding a message with provisioning wifi fw version not enough space for array") + { + VersionMessage command; + command.c.id = StandardMessageId::WiFiFWVersionMessageId; + command.params.version = "1.6.0"; + uint8_t buffer[5]; // Not enough space + size_t bytes_encoded = sizeof(buffer); + + CBORMessageEncoder encoder; + MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); + + // Test the encoding fails due to insufficient space + THEN("The encoding fails with an error") { + REQUIRE(err == MessageEncoder::Status::Error); + } + } + + WHEN("Error encoding a message with provisioning wifi fw version not enough space for version string") + { + VersionMessage command; + command.c.id = StandardMessageId::WiFiFWVersionMessageId; + command.params.version = "1.6.0"; + uint8_t buffer[7]; // Not enough space + size_t bytes_encoded = sizeof(buffer); + + CBORMessageEncoder encoder; + MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); + + // Test the encoding fails due to insufficient space + THEN("The encoding fails with an error") { + REQUIRE(err == MessageEncoder::Status::Error); + } + } + +} diff --git a/src/cbor/standards/StandardEncoders.cpp b/src/cbor/standards/StandardEncoders.cpp new file mode 100644 index 0000000..512ebba --- /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 VersionMessageEncoder::encode(CborEncoder* encoder, Message *msg) { + VersionMessage * versionMsg = (VersionMessage*) 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, versionMsg->params.version) != CborNoError) { + return MessageEncoder::Status::Error; + } + + if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) { + return MessageEncoder::Status::Error; + } + + return MessageEncoder::Status::Complete; +} + +static VersionMessageEncoder wifiFWVersionMessageEncoderCbor(CBORWiFiFWVersionMessage, WiFiFWVersionMessageId); diff --git a/src/cbor/standards/StandardEncoders.h b/src/cbor/standards/StandardEncoders.h new file mode 100644 index 0000000..0b28898 --- /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 VersionMessageEncoder: public CBORMessageEncoderInterface { +public: + VersionMessageEncoder(CBORStandardMessageTag tag, StandardMessageId id) + : CBORMessageEncoderInterface(tag, id) {} +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..4fc8d4e --- /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 = ArduinoVersionsStartId, +}; + +struct VersionMessage { + Message c; + struct { + const char *version; //The payload is a string. + } params; +}; diff --git a/src/interfaces/message.h b/src/interfaces/message.h index ba09c87..a41fa0a 100644 --- a/src/interfaces/message.h +++ b/src/interfaces/message.h @@ -29,6 +29,8 @@ struct Message { * and boundaries and avoid value clashing */ enum : MessageId { + ArduinoMessageStartId = 0x000, ArduinoIOTCloudStartMessageId = 0x100, ArduinoProvisioningStartMessageId = 0x200, + ArduinoVersionsStartId = 0x300, }; From 2338a6fcd42da64b3440a5b397b8736f6559d2c5 Mon Sep 17 00:00:00 2001 From: fabik111 Date: Thu, 19 Jun 2025 10:41:20 +0200 Subject: [PATCH 2/2] add example --- .github/workflows/compile-examples.yml | 1 + .../versionCborEncoder/versionCborEncoder.ino | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 examples/versionCborEncoder/versionCborEncoder.ino diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 8a3386c..91bcae1 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -30,6 +30,7 @@ jobs: - examples/customCborEncoder - examples/timedBlink - examples/flashFormatter + - examples/versionCborEncoder SKETCHES_REPORTS_PATH: sketches-reports strategy: diff --git a/examples/versionCborEncoder/versionCborEncoder.ino b/examples/versionCborEncoder/versionCborEncoder.ino new file mode 100644 index 0000000..bc11d76 --- /dev/null +++ b/examples/versionCborEncoder/versionCborEncoder.ino @@ -0,0 +1,42 @@ +/* + 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 +#include + +void setup() { + Serial.begin(9600); + while(!Serial); + + VersionMessage command; + command.c.id = StandardMessageId::WiFiFWVersionMessageId; + command.params.version = "1.6.0"; + uint8_t buffer[512]; + size_t buf_len = sizeof(buffer); + + CBORMessageEncoder encoder; + MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, buf_len); + + uint8_t expected_result[] = { + 0xda, 0x00, 0x01, 0x20, 0x14, 0x81, 0x65, 0x31, 0x2E, 0x36, 0x2E, 0x30 + }; + size_t res_len=buf_len; + MessageEncoder::Status res = encoder.encode((Message*)&command, buffer, res_len); + + if(res == MessageEncoder::Status::Complete && + memcmp(buffer, expected_result, res_len) == 0) { + + Serial.println("Encode operation completed with success"); + } else { + Serial.println("Encode operation failed"); + } +} + +void loop() {} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy