Skip to content

Commit afac651

Browse files
authored
Merge pull request #17 from andreagilardoni/cbor-messages
Cbor messages
2 parents 1b95a46 + a9a9089 commit afac651

36 files changed

+932
-2
lines changed

Diff for: .codespellrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[codespell]
44
# In the event of a false positive, add the problematic word, in all lowercase, to a comma-separated list here:
55
ignore-words-list = ,
6-
skip = ./.git,./.licenses,__pycache__,node_modules,./go.mod,./go.sum,./package-lock.json,./poetry.lock,./yarn.lock,./src/tinycbor
6+
skip = ./.git,./.licenses,__pycache__,node_modules,./go.mod,./go.sum,./package-lock.json,./poetry.lock,./yarn.lock,./src/cbor/tinycbor
77
builtin = clear,informal,en-GB_to_en-US
88
check-filenames =
99
check-hidden =

Diff for: .github/workflows/compile-examples.yml

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ jobs:
2626
- examples/crc32
2727
- examples/crc16
2828
- examples/sha256
29+
- examples/customCborDecoder
30+
- examples/customCborEncoder
2931
- examples/timedBlink
3032
SKETCHES_REPORTS_PATH: sketches-reports
3133

Diff for: examples/customCborDecoder/customCborDecoder.ino

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
11+
#include <Arduino_CBOR.h>
12+
13+
enum : MessageId {
14+
CBORTestMessageId = 0x0123,
15+
};
16+
17+
enum : CBORTag {
18+
CBORTestMessageTag = 0x0321,
19+
};
20+
21+
22+
struct CBORTestMessage {
23+
Message m;
24+
char parameter[20];
25+
};
26+
27+
class CustomMessageDecoder: public CBORMessageDecoderInterface {
28+
public:
29+
CustomMessageDecoder()
30+
: CBORMessageDecoderInterface(CBORTestMessageTag, CBORTestMessageId) {}
31+
32+
protected:
33+
MessageDecoder::Status decode(CborValue* iter, Message *msg) override {
34+
CBORTestMessage* test = (CBORTestMessage*) msg;
35+
size_t dest_size = 20;
36+
37+
if(!cbor_value_is_text_string(iter)) {
38+
return MessageDecoder::Status::Error;
39+
}
40+
41+
// NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
42+
if(_cbor_value_copy_string(iter, test->parameter, &dest_size, NULL) != CborNoError) {
43+
return MessageDecoder::Status::Error;
44+
}
45+
46+
return MessageDecoder::Status::Complete;
47+
}
48+
} customMessageDecoder;
49+
50+
void setup() {
51+
Serial.begin(9600);
52+
while(!Serial);
53+
54+
CBORMessageDecoder decoder;
55+
56+
CBORTestMessage expected_result {
57+
CBORTestMessageId,
58+
"abcdef",
59+
};
60+
61+
uint8_t buffer[] {
62+
0xD9, 0x03, 0x21, 0x81, 0x66, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
63+
};
64+
size_t buffer_len = sizeof(buffer);
65+
66+
CBORTestMessage cmd_res;
67+
MessageDecoder::Status res = decoder.decode((Message*)&cmd_res, buffer, buffer_len);
68+
69+
if(res == MessageDecoder::Status::Complete &&
70+
cmd_res.m.id == expected_result.m.id &&
71+
strcmp(cmd_res.parameter, expected_result.parameter) == 0) {
72+
73+
Serial.println("Decode operation completed with success");
74+
} else {
75+
Serial.println("Decode operation failed");
76+
}
77+
}
78+
79+
void loop() {}

Diff for: examples/customCborEncoder/customCborEncoder.ino

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
11+
#include <Arduino_CBOR.h>
12+
13+
enum : MessageId {
14+
CBORTestMessageId = 0x0123,
15+
};
16+
17+
enum : CBORTag {
18+
CBORTestMessageTag = 0x0321,
19+
};
20+
21+
22+
struct CBORTestMessage {
23+
Message m;
24+
char parameter[20];
25+
};
26+
27+
class CustomMessageEncoder: public CBORMessageEncoderInterface {
28+
public:
29+
CustomMessageEncoder()
30+
: CBORMessageEncoderInterface(CBORTestMessageTag, CBORTestMessageId) {}
31+
32+
protected:
33+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override {
34+
CBORTestMessage * testMessage = (CBORTestMessage *) msg;
35+
CborEncoder array_encoder;
36+
37+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
38+
return MessageEncoder::Status::Error;
39+
}
40+
41+
if(cbor_encode_text_stringz(&array_encoder, testMessage->parameter) != CborNoError) {
42+
return MessageEncoder::Status::Error;
43+
}
44+
45+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
46+
return MessageEncoder::Status::Error;
47+
}
48+
return MessageEncoder::Status::Complete;
49+
}
50+
} customMessageEncoder;
51+
52+
void setup() {
53+
Serial.begin(9600);
54+
while(!Serial);
55+
56+
CBORMessageEncoder encoder;
57+
uint8_t buffer[100]; // shared buffer for encoding
58+
uint8_t expected[] = {0xD9, 0x03, 0x21, 0x81, 0x66, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66};
59+
const size_t buf_len = sizeof(buffer);
60+
61+
CBORTestMessage cmd {
62+
CBORTestMessageId,
63+
"abcdef",
64+
};
65+
size_t res_len=buf_len;
66+
MessageEncoder::Status res = encoder.encode((Message*)&cmd, buffer, res_len);
67+
68+
if(res == MessageEncoder::Status::Complete &&
69+
memcmp(buffer, expected, res_len) == 0) {
70+
71+
Serial.println("Encode operation completed with success");
72+
} else {
73+
Serial.println("Encode operation failed");
74+
}
75+
}
76+
77+
void loop() {}

Diff for: extras/test/CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ set(TEST_SRCS
2828
src/crc16/test_crc16.cpp
2929
src/sha256/test_sha256.cpp
3030
src/hex/test_hex.cpp
31+
src/cbor/test_cbor_encoder.cpp
32+
src/cbor/test_cbor_decoder.cpp
3133
src/time/test_TimedAttempt.cpp
3234
)
3335

@@ -36,6 +38,19 @@ set(TEST_DUT_SRCS
3638
../../src/crc/crc16.cpp
3739
../../src/sha256/sha2.c
3840
../../src/hex/chex.h
41+
../../src/cbor/MessageDecoder.cpp
42+
../../src/cbor/MessageEncoder.cpp
43+
../../src/cbor/tinycbor
44+
../../src/cbor/tinycbor/src/cborencoder.c
45+
../../src/cbor/tinycbor/src/cborencoder_close_container_checked.c
46+
../../src/cbor/tinycbor/src/cborerrorstrings.c
47+
../../src/cbor/tinycbor/src/cborparser.c
48+
../../src/cbor/tinycbor/src/cborparser_dup_string.c
49+
../../src/cbor/tinycbor/src/cborpretty.c
50+
../../src/cbor/tinycbor/src/cborpretty_stdio.c
51+
../../src/cbor/tinycbor/src/cbortojson.c
52+
../../src/cbor/tinycbor/src/cborvalidation.c
53+
../../src/cbor/tinycbor/src/open_memstream.c
3954
../../src/time/TimedAttempt.cpp
4055
)
4156

Diff for: extras/test/src/cbor/test_cbor_decoder.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_string.hpp>
13+
#include <cbor/MessageDecoder.h>
14+
15+
#include <stdio.h>
16+
17+
enum : MessageId {
18+
CBORTestMessageId = 0x0123,
19+
};
20+
21+
enum : CBORTag {
22+
CBORTestMessageTag = 0x0321,
23+
};
24+
25+
struct CBORTestMessage {
26+
Message m;
27+
char parameter[20];
28+
};
29+
30+
class CustomMessageDecoder: public CBORMessageDecoderInterface {
31+
public:
32+
CustomMessageDecoder()
33+
: CBORMessageDecoderInterface(CBORTestMessageTag, CBORTestMessageId) {}
34+
35+
protected:
36+
MessageDecoder::Status decode(CborValue* iter, Message *msg) override {
37+
CBORTestMessage* test = (CBORTestMessage*) msg;
38+
size_t dest_size = 20;
39+
40+
if(!cbor_value_is_text_string(iter)) {
41+
return MessageDecoder::Status::Error;
42+
}
43+
44+
// NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string
45+
if(_cbor_value_copy_string(iter, test->parameter, &dest_size, NULL) != CborNoError) {
46+
return MessageDecoder::Status::Error;
47+
}
48+
49+
return MessageDecoder::Status::Complete;
50+
}
51+
} customMessageDecoder;
52+
53+
SCENARIO( "A custom decoder is defined", "[cbor][decode]" ) {
54+
CBORMessageDecoder decoder;
55+
GIVEN( "A buffer containing a cbor encoded message" ) {
56+
CBORTestMessage expected_result {
57+
CBORTestMessageId,
58+
"abcdef",
59+
};
60+
61+
uint8_t buffer[] {
62+
0xD9, 0x03, 0x21, 0x81, 0x66, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
63+
};
64+
size_t buffer_len = sizeof(buffer);
65+
66+
CBORTestMessage cmd_res;
67+
68+
MessageDecoder::Status res = decoder.decode((Message*)&cmd_res, buffer, buffer_len);
69+
70+
THEN( "Message decode result is Complete" ) {
71+
REQUIRE(res == MessageDecoder::Status::Complete);
72+
}
73+
74+
THEN( "the decode result matches the expectations" ) {
75+
REQUIRE(buffer_len == sizeof(buffer));
76+
77+
REQUIRE(expected_result.m.id == cmd_res.m.id);
78+
79+
std::string parameter_expected(expected_result.parameter);
80+
std::string parameter_result(cmd_res.parameter);
81+
82+
REQUIRE_THAT(parameter_result, Catch::Matchers::Equals(parameter_expected));
83+
}
84+
}
85+
}

Diff for: extras/test/src/cbor/test_cbor_encoder.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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/MessageEncoder.h>
14+
15+
enum : MessageId {
16+
CBORTestMessageId = 0x0123,
17+
};
18+
19+
enum : CBORTag {
20+
CBORTestMessageTag = 0x0321,
21+
};
22+
23+
24+
struct CBORTestMessage {
25+
Message m;
26+
char parameter[20];
27+
};
28+
29+
class CustomMessageEncoder: public CBORMessageEncoderInterface {
30+
public:
31+
CustomMessageEncoder()
32+
: CBORMessageEncoderInterface(CBORTestMessageTag, CBORTestMessageId) {}
33+
34+
protected:
35+
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override {
36+
CBORTestMessage * testMessage = (CBORTestMessage *) msg;
37+
CborEncoder array_encoder;
38+
39+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
40+
return MessageEncoder::Status::Error;
41+
}
42+
43+
if(cbor_encode_text_stringz(&array_encoder, testMessage->parameter) != CborNoError) {
44+
return MessageEncoder::Status::Error;
45+
}
46+
47+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
48+
return MessageEncoder::Status::Error;
49+
}
50+
return MessageEncoder::Status::Complete;
51+
}
52+
} customMessageEncoder;
53+
54+
SCENARIO( "A custom encoder is defined", "[cbor][encode]" ) {
55+
CBORMessageEncoder encoder;
56+
uint8_t buffer[100]; // shared buffer for encoding
57+
const size_t buf_len = sizeof(buffer);
58+
59+
GIVEN( "A Message with an id that the global encoder is able to encode" ) {
60+
CBORTestMessage cmd {
61+
CBORTestMessageId,
62+
"abcdef",
63+
};
64+
size_t res_len=buf_len;
65+
MessageEncoder::Status res = encoder.encode((Message*)&cmd, buffer, res_len);
66+
67+
THEN( "Message encode result is Complete" ) {
68+
REQUIRE(res == MessageEncoder::Status::Complete);
69+
}
70+
71+
THEN( "the encode result matches the expectations" ) {
72+
std::vector<uint8_t> res(buffer, buffer+res_len);
73+
74+
REQUIRE_THAT(res, Catch::Matchers::Equals(std::vector<uint8_t>{
75+
0xD9, 0x03, 0x21, 0x81, 0x66, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
76+
}));
77+
}
78+
}
79+
}

Diff for: src/Arduino_CBOR.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
#pragma once
11+
12+
#include "./cbor/MessageEncoder.h"
13+
#include "./cbor/MessageDecoder.h"

Diff for: src/Arduino_TinyCBOR.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
*/
1010
#pragma once
1111

12-
#include "./tinycbor/cbor-lib.h"
12+
#include "./cbor/tinycbor/cbor-lib.h"

0 commit comments

Comments
 (0)