xconnio / wampproto-esp32

Sans-IO WAMP protocol implementation in C++ for ESP32
MIT License
0 stars 1 forks source link

R&D: CBOR serialization #3

Open om26er opened 6 months ago

om26er commented 6 months ago

Need to check CBOR support

asimfarooq5 commented 2 months ago

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 commented 1 month ago

works as expected