-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ") | ||
{ | ||
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); | ||
} | ||
} | ||
|
||
} |
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 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; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of struct VersionMessage {
Message c;
struct {
const char *versionString;
} params;
};
VersionMessageEncoder(CborTag tag, MessageId id)
: CBORMessageEncoderInterface(tag, id) {}
VersionMessageEncoder WiFiFWVersionEncoder(CBORWiFiFWVersionMessage, WiFiFWVersionMessageId);
typedef VersionMessage WiFiFWVersionMessage; In this way we can encode all kind of version messages with just this class. |
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 WiFiFWVersionMessageEncoder: public CBORMessageEncoderInterface { | ||
public: | ||
WiFiFWVersionMessageEncoder() | ||
: CBORMessageEncoderInterface(CBORWiFiFWVersionMessage, WiFiFWVersionMessageId) {} | ||
protected: | ||
MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; | ||
}; |
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 WiFiFWVersionMessage { | ||
Message c; | ||
struct { | ||
const char *wiFiFWVersion; //The payload is a string. | ||
} params; | ||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -29,6 +29,7 @@ struct Message { | |||||||||||||||
* and boundaries and avoid value clashing | ||||||||||||||||
*/ | ||||||||||||||||
enum : MessageId { | ||||||||||||||||
ArduinoStandardMessageStartId = 0x000, | ||||||||||||||||
ArduinoIOTCloudStartMessageId = 0x100, | ||||||||||||||||
ArduinoProvisioningStartMessageId = 0x200, | ||||||||||||||||
Comment on lines
+32
to
34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
}; |
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.
Can you add some examples to improve code coverage?