Skip to content
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

Open
om26er opened this issue May 29, 2024 · 2 comments
Open

R&D: CBOR serialization #3

om26er opened this issue May 29, 2024 · 2 comments
Assignees

Comments

@om26er
Copy link
Member

om26er commented May 29, 2024

Need to check CBOR support

@asimfarooq5
Copy link
Collaborator

asimfarooq5 commented Sep 24, 2024

CBOR serialization

Create the CBORSerialization Class

CBORSerialization.h

#ifndef CBORSerialization_H
#define CBORSerialization_H

#include <tinycbor.h>
#include <Arduino.h>

class CBORSerialization {
public:
    CBORSerialization();
    size_t encodeCBOR(uint8_t* buffer, size_t bufferSize); // Ensure this matches
    void decodeCBOR(const uint8_t* cborData, size_t length);
};

#endif

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 Usage

CBORSerialization.ino

#include <Arduino.h>
#include "CBORSerialization.h"

// Buffer size for CBOR encoding
#define BUFFER_SIZE 128

uint8_t buffer[BUFFER_SIZE];

CBORSerialization serializer; // Use the correct class name

void setup() {
    Serial.begin(115200);
}

void loop() {
    delay(500);

    // Encode CBOR data
    size_t cborLength = serializer.encodeCBOR(buffer, BUFFER_SIZE);

    // Print the encoded CBOR data in HEX
    Serial.print("Encoded CBOR data: ");
    for (size_t i = 0; i < cborLength; i++) {
        Serial.print(buffer[i], HEX);
    }
    Serial.println();

    // Decode the CBOR data
    serializer.decodeCBOR(buffer, cborLength); // Call decodeCBOR on the correct class instance
}

Usage Instructions

Create the Class Files:

Create two files in your Arduino project: CBORSerialization.h and CBORSerialization.cpp.
Paste the class definitions into these files.
Include the Class:

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.

@muzzammilshahid
Copy link
Member

works as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants