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.

Committer:
kpniot
Date:
Sat May 19 17:35:20 2018 +0000
Revision:
0:a9259748d982
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kpniot 0:a9259748d982 1
kpniot 0:a9259748d982 2 /* _ __ ____ _ _
kpniot 0:a9259748d982 3 * | |/ / | _ \ | \ | |
kpniot 0:a9259748d982 4 * | ' / | |_) | | \| |
kpniot 0:a9259748d982 5 * | . \ | __/ | |\ |
kpniot 0:a9259748d982 6 * |_|\_\ |_| |_| \_|
kpniot 0:a9259748d982 7 *
kpniot 0:a9259748d982 8 * (c) 2018 KPN
kpniot 0:a9259748d982 9 * License: MIT License.
kpniot 0:a9259748d982 10 * Author: Jan Bogaerts
kpniot 0:a9259748d982 11 *
kpniot 0:a9259748d982 12 * support for binary actuators
kpniot 0:a9259748d982 13 */
kpniot 0:a9259748d982 14
kpniot 0:a9259748d982 15 #include <senml_binary_actuator.h>
kpniot 0:a9259748d982 16 #include <senml_logging.h>
kpniot 0:a9259748d982 17
kpniot 0:a9259748d982 18 #ifdef ESP32
kpniot 0:a9259748d982 19 extern "C" {
kpniot 0:a9259748d982 20 #include "libb64/cdecode.h"
kpniot 0:a9259748d982 21 }
kpniot 0:a9259748d982 22 int base64_dec_len(char * input, int inputLen) {
kpniot 0:a9259748d982 23 int i = 0;
kpniot 0:a9259748d982 24 int numEq = 0;
kpniot 0:a9259748d982 25 for(i = inputLen - 1; input[i] == '='; i--) {
kpniot 0:a9259748d982 26 numEq++;
kpniot 0:a9259748d982 27 }
kpniot 0:a9259748d982 28
kpniot 0:a9259748d982 29 return ((6 * inputLen) / 8) - numEq;
kpniot 0:a9259748d982 30 }
kpniot 0:a9259748d982 31 #elif __MBED__
kpniot 0:a9259748d982 32 #include <base64.h>
kpniot 0:a9259748d982 33 int base64_dec_len(char * input, int inputLen) {
kpniot 0:a9259748d982 34 int i = 0;
kpniot 0:a9259748d982 35 int numEq = 0;
kpniot 0:a9259748d982 36 for(i = inputLen - 1; input[i] == '='; i--) {
kpniot 0:a9259748d982 37 numEq++;
kpniot 0:a9259748d982 38 }
kpniot 0:a9259748d982 39
kpniot 0:a9259748d982 40 return ((6 * inputLen) / 8) - numEq;
kpniot 0:a9259748d982 41 }
kpniot 0:a9259748d982 42 #else
kpniot 0:a9259748d982 43 #include <Base64.h>
kpniot 0:a9259748d982 44 #endif
kpniot 0:a9259748d982 45
kpniot 0:a9259748d982 46 void SenMLBinaryActuator::actuate(const void* value, int dataLength, SenMLDataType dataType)
kpniot 0:a9259748d982 47 {
kpniot 0:a9259748d982 48 if(dataType == SENML_TYPE_DATA){
kpniot 0:a9259748d982 49 int decodedLen = base64_dec_len((char*)value, dataLength);
kpniot 0:a9259748d982 50 char decoded[decodedLen];
kpniot 0:a9259748d982 51 #ifdef ESP32
kpniot 0:a9259748d982 52 base64_decode_chars((const char*)value, dataLength, decoded);
kpniot 0:a9259748d982 53 #elif __MBED__
kpniot 0:a9259748d982 54 // todo: check result of function
kpniot 0:a9259748d982 55 size_t olen;
kpniot 0:a9259748d982 56 mbedtls_base64_decode((unsigned char*)decoded, decodedLen, &olen, (const unsigned char*)value, dataLength);
kpniot 0:a9259748d982 57 #else
kpniot 0:a9259748d982 58 base64_decode(decoded, (char*)value, dataLength);
kpniot 0:a9259748d982 59 #endif
kpniot 0:a9259748d982 60
kpniot 0:a9259748d982 61 this->set((unsigned char*)decoded, decodedLen);
kpniot 0:a9259748d982 62 if(this->callback)
kpniot 0:a9259748d982 63 this->callback((unsigned char*)decoded, decodedLen);
kpniot 0:a9259748d982 64 }
kpniot 0:a9259748d982 65 else if(dataType == CBOR_TYPE_DATA){
kpniot 0:a9259748d982 66 this->set((unsigned char*)value, dataLength);
kpniot 0:a9259748d982 67 if(this->callback)
kpniot 0:a9259748d982 68 this->callback((unsigned char*)value, dataLength);
kpniot 0:a9259748d982 69 }
kpniot 0:a9259748d982 70 else
kpniot 0:a9259748d982 71 log_debug("invalid type");
kpniot 0:a9259748d982 72 }
kpniot 0:a9259748d982 73
kpniot 0:a9259748d982 74
kpniot 0:a9259748d982 75
kpniot 0:a9259748d982 76
kpniot 0:a9259748d982 77
kpniot 0:a9259748d982 78
kpniot 0:a9259748d982 79
kpniot 0:a9259748d982 80