Skip to content

Commit 2934dbc

Browse files
Added example for CborEncoder and CborDecoder usage
1 parent a1f020f commit 2934dbc

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

.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
SKETCHES_REPORTS_PATH: sketches-reports
3032

3133
strategy:
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() {}
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 { // FIXME better names
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() {}

0 commit comments

Comments
 (0)