From 4e406244e95e42d664dfa6d47540fb5b9cbc9b35 Mon Sep 17 00:00:00 2001 From: fabik111 Date: Tue, 3 Jun 2025 18:12:30 +0200 Subject: [PATCH 1/3] add standard encoder for wifi fw version --- extras/test/CMakeLists.txt | 2 + .../test/src/cbor/test_cbor_standard_enc.cpp | 48 +++++++++++++++++++ src/cbor/standards/StandardEncoders.cpp | 32 +++++++++++++ src/cbor/standards/StandardEncoders.h | 21 ++++++++ src/cbor/standards/StandardMessages.h | 29 +++++++++++ src/interfaces/message.h | 1 + 6 files changed, 133 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..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, }; From 5faf0316a9576275032f0819a7f27fc208b412d9 Mon Sep 17 00:00:00 2001 From: fabik111 Date: Wed, 18 Jun 2025 17:51:57 +0200 Subject: [PATCH 2/3] add standard encoder for version message --- extras/test/src/cbor/test_cbor_standard_enc.cpp | 4 ++-- src/cbor/standards/StandardEncoders.cpp | 8 ++++---- src/cbor/standards/StandardEncoders.h | 6 +++--- src/cbor/standards/StandardMessages.h | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/extras/test/src/cbor/test_cbor_standard_enc.cpp b/extras/test/src/cbor/test_cbor_standard_enc.cpp index bb42548..17958f6 100644 --- a/extras/test/src/cbor/test_cbor_standard_enc.cpp +++ b/extras/test/src/cbor/test_cbor_standard_enc.cpp @@ -20,9 +20,9 @@ SCENARIO("Test the encoding of command messages") { WHEN("Encode a message with provisioning wifi fw version ") { - WiFiFWVersionMessage command; + VersionMessage command; command.c.id = StandardMessageId::WiFiFWVersionMessageId; - command.params.wiFiFWVersion = "1.6.0"; + command.params.version = "1.6.0"; uint8_t buffer[512]; size_t bytes_encoded = sizeof(buffer); diff --git a/src/cbor/standards/StandardEncoders.cpp b/src/cbor/standards/StandardEncoders.cpp index a12c03b..512ebba 100644 --- a/src/cbor/standards/StandardEncoders.cpp +++ b/src/cbor/standards/StandardEncoders.cpp @@ -10,15 +10,15 @@ #include "StandardEncoders.h" -MessageEncoder::Status WiFiFWVersionMessageEncoder::encode(CborEncoder* encoder, Message *msg) { - WiFiFWVersionMessage * wiFiFWVersionMsg = (WiFiFWVersionMessage*) msg; +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, wiFiFWVersionMsg->params.wiFiFWVersion) != CborNoError) { + if(cbor_encode_text_stringz(&array_encoder, versionMsg->params.version) != CborNoError) { return MessageEncoder::Status::Error; } @@ -29,4 +29,4 @@ MessageEncoder::Status WiFiFWVersionMessageEncoder::encode(CborEncoder* encoder, return MessageEncoder::Status::Complete; } -static WiFiFWVersionMessageEncoder wifiFWVersionMessageEncoderCbor; +static VersionMessageEncoder wifiFWVersionMessageEncoderCbor(CBORWiFiFWVersionMessage, WiFiFWVersionMessageId); diff --git a/src/cbor/standards/StandardEncoders.h b/src/cbor/standards/StandardEncoders.h index 16adb44..0b28898 100644 --- a/src/cbor/standards/StandardEncoders.h +++ b/src/cbor/standards/StandardEncoders.h @@ -12,10 +12,10 @@ #include "StandardMessages.h" -class WiFiFWVersionMessageEncoder: public CBORMessageEncoderInterface { +class VersionMessageEncoder: public CBORMessageEncoderInterface { public: - WiFiFWVersionMessageEncoder() - : CBORMessageEncoderInterface(CBORWiFiFWVersionMessage, WiFiFWVersionMessageId) {} + 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 index 174bf59..4154f98 100644 --- a/src/cbor/standards/StandardMessages.h +++ b/src/cbor/standards/StandardMessages.h @@ -21,9 +21,9 @@ enum StandardMessageId: MessageId { WiFiFWVersionMessageId = ArduinoStandardMessageStartId, }; -struct WiFiFWVersionMessage { +struct VersionMessage { Message c; struct { - const char *wiFiFWVersion; //The payload is a string. + const char *version; //The payload is a string. } params; }; From ed7b70bad042c5e845aac949bc34474d4f446168 Mon Sep 17 00:00:00 2001 From: fabik111 Date: Thu, 19 Jun 2025 10:41:20 +0200 Subject: [PATCH 3/3] 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() {}