KPN IoT / senml

Fork of kpn_senml by KPN IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers senml_binary_actuator.cpp Source File

senml_binary_actuator.cpp

00001 
00002 /*  _  __  ____    _   _ 
00003  * | |/ / |  _ \  | \ | |
00004  * | ' /  | |_) | |  \| |
00005  * | . \  |  __/  | |\  |
00006  * |_|\_\ |_|     |_| \_|
00007  * 
00008  * (c) 2018 KPN
00009  * License: MIT License.
00010  * Author: Jan Bogaerts
00011  * 
00012  * support for binary actuators
00013  */
00014 
00015 #include <senml_binary_actuator.h>
00016 #include <senml_logging.h>
00017 
00018 #ifdef ESP32
00019     extern "C" {
00020     #include "libb64/cdecode.h"
00021     }
00022     int base64_dec_len(char * input, int inputLen) {
00023         int i = 0;
00024         int numEq = 0;
00025         for(i = inputLen - 1; input[i] == '='; i--) {
00026             numEq++;
00027         }
00028 
00029         return ((6 * inputLen) / 8) - numEq;
00030     }
00031 #elif __MBED__
00032     #include <base64.h>
00033     int base64_dec_len(char * input, int inputLen) {
00034         int i = 0;
00035         int numEq = 0;
00036         for(i = inputLen - 1; input[i] == '='; i--) {
00037             numEq++;
00038         }
00039 
00040         return ((6 * inputLen) / 8) - numEq;
00041     }
00042 #else
00043     #include <Base64.h>
00044 #endif
00045 
00046 void SenMLBinaryActuator::actuate(const void* value, int dataLength, SenMLDataType dataType)
00047 {
00048     if(dataType == SENML_TYPE_DATA){
00049         int decodedLen = base64_dec_len((char*)value, dataLength);
00050         char decoded[decodedLen];
00051         #ifdef ESP32
00052             base64_decode_chars((const char*)value, dataLength, decoded);
00053         #elif __MBED__
00054             // todo: check result of function
00055             size_t olen;
00056             mbedtls_base64_decode((unsigned char*)decoded, decodedLen, &olen, (const unsigned char*)value, dataLength);
00057         #else
00058             base64_decode(decoded, (char*)value, dataLength); 
00059         #endif 
00060 
00061         this->set((unsigned char*)decoded, decodedLen);
00062         if(this->callback)
00063             this->callback((unsigned char*)decoded, decodedLen);
00064     }
00065     else if(dataType == CBOR_TYPE_DATA){
00066         this->set((unsigned char*)value, dataLength);
00067         if(this->callback)
00068             this->callback((unsigned char*)value, dataLength);
00069     }
00070     else
00071         log_debug("invalid type");
00072 }
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080