Skip to content

Commit 4e40624

Browse files
committed
add standard encoder for wifi fw version
1 parent 4db4576 commit 4e40624

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
WiFiFWVersionMessage command;
24+
command.c.id = StandardMessageId::WiFiFWVersionMessageId;
25+
command.params.wiFiFWVersion = "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+
}
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 WiFiFWVersionMessageEncoder::encode(CborEncoder* encoder, Message *msg) {
14+
WiFiFWVersionMessage * wiFiFWVersionMsg = (WiFiFWVersionMessage*) 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, wiFiFWVersionMsg->params.wiFiFWVersion) != 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 WiFiFWVersionMessageEncoder wifiFWVersionMessageEncoderCbor;

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 WiFiFWVersionMessageEncoder: public CBORMessageEncoderInterface {
16+
public:
17+
WiFiFWVersionMessageEncoder()
18+
: CBORMessageEncoderInterface(CBORWiFiFWVersionMessage, WiFiFWVersionMessageId) {}
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 = ArduinoStandardMessageStartId,
22+
};
23+
24+
struct WiFiFWVersionMessage {
25+
Message c;
26+
struct {
27+
const char *wiFiFWVersion; //The payload is a string.
28+
} params;
29+
};

src/interfaces/message.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct Message {
2929
* and boundaries and avoid value clashing
3030
*/
3131
enum : MessageId {
32+
ArduinoStandardMessageStartId = 0x000,
3233
ArduinoIOTCloudStartMessageId = 0x100,
3334
ArduinoProvisioningStartMessageId = 0x200,
3435
};

0 commit comments

Comments
 (0)