-
Notifications
You must be signed in to change notification settings - Fork 1
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
R&D: CBOR serialization #3
Comments
CBOR serializationCreate the CBORSerialization ClassCBORSerialization.h
CBORSerialization.cpp#include "CBORSerialization.h"
#include <tinycbor.h>
// Constructor for CBORSerialization
CBORSerialization::CBORSerialization() {}
// Function to encode CBOR data
size_t CBORSerialization::encodeCBOR(uint8_t* buffer, size_t bufferSize) {
CborEncoder encoder;
cbor_encoder_init(&encoder, buffer, bufferSize, 0);
// Start a new array of size 3
CborEncoder arrayEncoder;
cbor_encoder_create_array(&encoder, &arrayEncoder, 3);
// Example data to encode
int temperature = 25; // Example temperature
int humidity = 60; // Example humidity
const char* unit = "C"; // Example unit
// Add elements to the array
cbor_encode_int(&arrayEncoder, temperature);
cbor_encode_int(&arrayEncoder, humidity);
cbor_encode_text_string(&arrayEncoder, unit, strlen(unit));
// Close the array
cbor_encoder_close_container(&encoder, &arrayEncoder);
// Return the size of the encoded data
return cbor_encoder_get_buffer_size(&encoder, buffer);
}
// Function to decode CBOR data
void CBORSerialization::decodeCBOR(const uint8_t* cborData, size_t length) {
CborParser parser;
CborValue it;
// Initialize the CBOR parser
cbor_parser_init(cborData, length, 0, &parser, &it);
// Check if it's an array
if (cbor_value_is_array(&it)) {
CborValue arrayValue;
// Get the array iterator
cbor_value_enter_container(&it, &arrayValue);
// Read elements from the array
int temperature, humidity;
char unit[10]; // Buffer for the unit string
size_t unitLength = sizeof(unit);
// First element: temperature
cbor_value_get_int(&arrayValue, &temperature);
cbor_value_advance(&arrayValue);
// Second element: humidity
cbor_value_get_int(&arrayValue, &humidity);
cbor_value_advance(&arrayValue);
// Third element: unit of measurement
cbor_value_copy_text_string(&arrayValue, unit, &unitLength, NULL);
cbor_value_advance(&arrayValue);
// Print the values
Serial.println(temperature);
Serial.println(humidity);
Serial.println(unit);
}
}
Example UsageCBORSerialization.ino
Usage InstructionsCreate the Class Files:Create two files in your Arduino project: CBORSerialization.h and CBORSerialization.cpp. In your main sketch (CBORSerialization.ino), include the class by adding #include "CBORSerialization.h". Upload the Sketch:Upload the sketch to your Arduino board, Initialize the encoder and decoder, printing the result in HEX format. |
works as expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to check CBOR support
The text was updated successfully, but these errors were encountered: