Skip to content

Commit f47a720

Browse files
Merge pull request #33 from fabik111/add-standard-encoders
Add classes for standard cbor encoders/decoders
2 parents 4db4576 + 2338a6f commit f47a720

File tree

8 files changed

+211
-0
lines changed

8 files changed

+211
-0
lines changed

.github/workflows/compile-examples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
- examples/customCborEncoder
3131
- examples/timedBlink
3232
- examples/flashFormatter
33+
- examples/versionCborEncoder
3334
SKETCHES_REPORTS_PATH: sketches-reports
3435

3536
strategy:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2025 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+
11+
#include <Arduino_CBOR.h>
12+
#include <cbor/standards/StandardMessages.h>
13+
14+
void setup() {
15+
Serial.begin(9600);
16+
while(!Serial);
17+
18+
VersionMessage command;
19+
command.c.id = StandardMessageId::WiFiFWVersionMessageId;
20+
command.params.version = "1.6.0";
21+
uint8_t buffer[512];
22+
size_t buf_len = sizeof(buffer);
23+
24+
CBORMessageEncoder encoder;
25+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, buf_len);
26+
27+
uint8_t expected_result[] = {
28+
0xda, 0x00, 0x01, 0x20, 0x14, 0x81, 0x65, 0x31, 0x2E, 0x36, 0x2E, 0x30
29+
};
30+
size_t res_len=buf_len;
31+
MessageEncoder::Status res = encoder.encode((Message*)&command, buffer, res_len);
32+
33+
if(res == MessageEncoder::Status::Complete &&
34+
memcmp(buffer, expected_result, res_len) == 0) {
35+
36+
Serial.println("Encode operation completed with success");
37+
} else {
38+
Serial.println("Encode operation failed");
39+
}
40+
}
41+
42+
void loop() {}

extras/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(TEST_SRCS
3030
src/hex/test_hex.cpp
3131
src/cbor/test_cbor_encoder.cpp
3232
src/cbor/test_cbor_decoder.cpp
33+
src/cbor/test_cbor_standard_enc.cpp
3334
src/time/test_TimedAttempt.cpp
3435
)
3536

@@ -40,6 +41,7 @@ set(TEST_DUT_SRCS
4041
../../src/hex/chex.h
4142
../../src/cbor/MessageDecoder.cpp
4243
../../src/cbor/MessageEncoder.cpp
44+
../../src/cbor/standards/StandardEncoders.cpp
4345
../../src/cbor/tinycbor
4446
../../src/cbor/tinycbor/src/cborencoder.c
4547
../../src/cbor/tinycbor/src/cborencoder_close_container_checked.c
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 <catch2/catch_test_macros.hpp>
11+
#include <catch2/matchers/catch_matchers.hpp>
12+
#include <catch2/matchers/catch_matchers_vector.hpp>
13+
#include <cbor/standards/StandardEncoders.h>
14+
15+
/******************************************************************************
16+
TEST CODE
17+
******************************************************************************/
18+
19+
SCENARIO("Test the encoding of command messages") {
20+
21+
WHEN("Encode a message with provisioning wifi fw version ")
22+
{
23+
VersionMessage command;
24+
command.c.id = StandardMessageId::WiFiFWVersionMessageId;
25+
command.params.version = "1.6.0";
26+
uint8_t buffer[512];
27+
size_t bytes_encoded = sizeof(buffer);
28+
29+
CBORMessageEncoder encoder;
30+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
31+
32+
uint8_t expected_result[] = {
33+
0xda, 0x00, 0x01, 0x20, 0x14, 0x81, 0x65, 0x31, 0x2E, 0x36, 0x2E, 0x30
34+
};
35+
36+
// Test the encoding is
37+
//DA 00012014 # tag(73748)
38+
// 81 # array(1)
39+
// 65 # text(5)
40+
// 312E362E30 # "1.6.0"
41+
THEN("The encoding is successful") {
42+
REQUIRE(err == MessageEncoder::Status::Complete);
43+
REQUIRE(bytes_encoded == sizeof(expected_result));
44+
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
45+
}
46+
}
47+
48+
WHEN("Error encoding a message with provisioning wifi fw version not enough space for array")
49+
{
50+
VersionMessage command;
51+
command.c.id = StandardMessageId::WiFiFWVersionMessageId;
52+
command.params.version = "1.6.0";
53+
uint8_t buffer[5]; // Not enough space
54+
size_t bytes_encoded = sizeof(buffer);
55+
56+
CBORMessageEncoder encoder;
57+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
58+
59+
// Test the encoding fails due to insufficient space
60+
THEN("The encoding fails with an error") {
61+
REQUIRE(err == MessageEncoder::Status::Error);
62+
}
63+
}
64+
65+
WHEN("Error encoding a message with provisioning wifi fw version not enough space for version string")
66+
{
67+
VersionMessage command;
68+
command.c.id = StandardMessageId::WiFiFWVersionMessageId;
69+
command.params.version = "1.6.0";
70+
uint8_t buffer[7]; // Not enough space
71+
size_t bytes_encoded = sizeof(buffer);
72+
73+
CBORMessageEncoder encoder;
74+
MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
75+
76+
// Test the encoding fails due to insufficient space
77+
THEN("The encoding fails with an error") {
78+
REQUIRE(err == MessageEncoder::Status::Error);
79+
}
80+
}
81+
82+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2025 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+
11+
#include "StandardEncoders.h"
12+
13+
MessageEncoder::Status VersionMessageEncoder::encode(CborEncoder* encoder, Message *msg) {
14+
VersionMessage * versionMsg = (VersionMessage*) msg;
15+
CborEncoder array_encoder;
16+
17+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
18+
return MessageEncoder::Status::Error;
19+
}
20+
21+
if(cbor_encode_text_stringz(&array_encoder, versionMsg->params.version) != CborNoError) {
22+
return MessageEncoder::Status::Error;
23+
}
24+
25+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
26+
return MessageEncoder::Status::Error;
27+
}
28+
29+
return MessageEncoder::Status::Complete;
30+
}
31+
32+
static VersionMessageEncoder wifiFWVersionMessageEncoderCbor(CBORWiFiFWVersionMessage, WiFiFWVersionMessageId);

src/cbor/standards/StandardEncoders.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2025 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+
11+
#pragma once
12+
13+
#include "StandardMessages.h"
14+
15+
class VersionMessageEncoder: public CBORMessageEncoderInterface {
16+
public:
17+
VersionMessageEncoder(CBORStandardMessageTag tag, StandardMessageId id)
18+
: CBORMessageEncoderInterface(tag, id) {}
19+
protected:
20+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override;
21+
};

src/cbor/standards/StandardMessages.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2025 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+
11+
#pragma once
12+
#include "../MessageEncoder.h"
13+
14+
enum CBORStandardMessageTag: CBORTag {
15+
16+
CBORWiFiFWVersionMessage = 0x012014 //Next tag starts at 0x013000
17+
};
18+
19+
enum StandardMessageId: MessageId {
20+
/* Standard commands*/
21+
WiFiFWVersionMessageId = ArduinoVersionsStartId,
22+
};
23+
24+
struct VersionMessage {
25+
Message c;
26+
struct {
27+
const char *version; //The payload is a string.
28+
} params;
29+
};

src/interfaces/message.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct Message {
2929
* and boundaries and avoid value clashing
3030
*/
3131
enum : MessageId {
32+
ArduinoMessageStartId = 0x000,
3233
ArduinoIOTCloudStartMessageId = 0x100,
3334
ArduinoProvisioningStartMessageId = 0x200,
35+
ArduinoVersionsStartId = 0x300,
3436
};

0 commit comments

Comments
 (0)