The KPN SenML library helps you create and parse senml documents in both json and cbor format. The library can be used for sending sensor data and receiving actuator commands.

Fork of kpn_senml by KPN IoT

senml_binary_actuator.cpp

Committer:
kpniot
Date:
2018-05-27
Revision:
2:9b44be6e79ac
Parent:
0:a9259748d982

File content as of revision 2:9b44be6e79ac:


/*  _  __  ____    _   _ 
 * | |/ / |  _ \  | \ | |
 * | ' /  | |_) | |  \| |
 * | . \  |  __/  | |\  |
 * |_|\_\ |_|     |_| \_|
 * 
 * (c) 2018 KPN
 * License: MIT License.
 * Author: Jan Bogaerts
 * 
 * support for binary actuators
 */

#include <senml_binary_actuator.h>
#include <senml_logging.h>

#ifdef ESP32
    extern "C" {
    #include "libb64/cdecode.h"
    }
    int base64_dec_len(char * input, int inputLen) {
        int i = 0;
        int numEq = 0;
        for(i = inputLen - 1; input[i] == '='; i--) {
            numEq++;
        }

        return ((6 * inputLen) / 8) - numEq;
    }
#elif __MBED__
    #include <base64.h>
    int base64_dec_len(char * input, int inputLen) {
        int i = 0;
        int numEq = 0;
        for(i = inputLen - 1; input[i] == '='; i--) {
            numEq++;
        }

        return ((6 * inputLen) / 8) - numEq;
    }
#else
    #include <Base64.h>
#endif

void SenMLBinaryActuator::actuate(const void* value, int dataLength, SenMLDataType dataType)
{
    if(dataType == SENML_TYPE_DATA){
        int decodedLen = base64_dec_len((char*)value, dataLength);
        char decoded[decodedLen];
        #ifdef ESP32
            base64_decode_chars((const char*)value, dataLength, decoded);
        #elif __MBED__
            // todo: check result of function
            size_t olen;
            mbedtls_base64_decode((unsigned char*)decoded, decodedLen, &olen, (const unsigned char*)value, dataLength);
        #else
            base64_decode(decoded, (char*)value, dataLength); 
        #endif 

        this->set((unsigned char*)decoded, decodedLen);
        if(this->callback)
            this->callback((unsigned char*)decoded, decodedLen);
    }
    else if(dataType == CBOR_TYPE_DATA){
        this->set((unsigned char*)value, dataLength);
        if(this->callback)
            this->callback((unsigned char*)value, dataLength);
    }
    else
        log_debug("invalid type");
}