Skip to content

Commit 45c4f07

Browse files
defining encoder/decoder interfaces
1 parent fe349b4 commit 45c4f07

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/interfaces/Decoder.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
This file is part of the ArduinoIoTCloud 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+
#pragma once
12+
#include <message/Models.h>
13+
#include <stdint.h>
14+
15+
// using decoderFactory=std::function<Decoder*()>;
16+
17+
class Decoder {
18+
public:
19+
enum Status: uint8_t {
20+
Complete,
21+
InProgress,
22+
Error
23+
};
24+
25+
/**
26+
* Decode a buffer into a provided message structure
27+
* @param msg: the message structure that is going to be filled with data provided in the buffer
28+
* @param buf: the incoming buffer that needs to be decoded
29+
* @param len: the length of the incoming buffer, value will be updated with the used len of the buffer
30+
* @return SUCCESS: if the message is decoded correctly
31+
* ERROR: if the message wasn't decoded correctly
32+
*/
33+
virtual Status decode(Message* msg, const uint8_t* const buf, size_t &len) = 0;
34+
35+
/**
36+
* sets the preallocated empty message structure that needs to be decoded, before iterativley decoding it
37+
* @param Message the message that needs to be decoded, msg should be big enough to accommodate all the required fields
38+
*/
39+
virtual void setMessage(Message* msg) = 0;
40+
41+
/**
42+
* after having the Message struct set call this function to decode he chunks of buffers provided to this call
43+
* @param buffer the buffer the message will be encoded into
44+
* @param len the length of the provided buffer, value will be updated with the used len of the buffer
45+
* @return SUCCESS: the message is completely encoded
46+
* IN_PROGRESS: the message is encoded correctly so far, provide another buffer to continue
47+
* ERROR: error during the encoding of the message
48+
*/
49+
virtual Status feed(uint8_t* buf, size_t &len) = 0;
50+
};

src/interfaces/Encoder.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
This file is part of the ArduinoIoTCloud 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+
#pragma once
12+
#include <message/Models.h>
13+
#include <stdint.h>
14+
15+
// using encoderFactory=std::function<Encoder*()>;
16+
17+
class Encoder {
18+
public:
19+
enum Status: uint8_t {
20+
Complete,
21+
InProgress,
22+
Error
23+
};
24+
25+
/**
26+
* Encode a message into a buffer in a single shot
27+
* @param msg: the message that needs to be encoded
28+
* @param buf: the buffer the message will be encoded into
29+
* @param len: the length of the provided buffer, value will be updated with the consumed len of the buffer
30+
* @return SUCCESS: if the message is encoded correctly
31+
* ERROR: error during the encoding of the message
32+
*/
33+
virtual Status encode(Message* msg, uint8_t* buf, size_t& len) = 0;
34+
35+
/**
36+
* sets the message that needs to be encoded, before iterativley encoding it
37+
* @param Message the message that needs to be encoded
38+
*/
39+
virtual void setMessage(Message* msg) = 0;
40+
41+
/**
42+
* after having the Message struct set call this function to encode the message into a buffer
43+
* this action is performed incrementally into chunks of buffers
44+
* @param buf: the buffer the message will be encoded into
45+
* @param len: the length of the provided buffer, value will be updated with the consumed len of the buffer
46+
* @return SUCCESS: the message is completely encoded
47+
* IN_PROGRESS: the message is encoded correctly so far, provide another buffer to continue
48+
* ERROR: error during the encoding of the message
49+
*/
50+
virtual Status encode(uint8_t* buf, size_t& len) = 0;
51+
};

0 commit comments

Comments
 (0)