-
Notifications
You must be signed in to change notification settings - Fork 3
Add classes for standard cbor encoders/decoders #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fabik111
wants to merge
3
commits into
arduino-libraries:main
Choose a base branch
from
fabik111:add-standard-encoders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <Arduino_CBOR.h> | ||
#include <cbor/standards/StandardMessages.h> | ||
|
||
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() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <catch2/catch_test_macros.hpp> | ||
#include <catch2/matchers/catch_matchers.hpp> | ||
#include <catch2/matchers/catch_matchers_vector.hpp> | ||
#include <cbor/standards/StandardEncoders.h> | ||
|
||
/****************************************************************************** | ||
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); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 VersionMessage { | ||
Message c; | ||
struct { | ||
const char *version; //The payload is a string. | ||
} params; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer keeping the versions messages IDs starting from ArduinoStandardMessageStartId. My idea is that the StandardMessageEncoder is a collection of common encoders