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 * (c) 2018 KPN
kpniot 0:a9259748d982 8 * License: MIT License.
kpniot 0:a9259748d982 9 * Author: Jan Bogaerts
kpniot 0:a9259748d982 10 *
kpniot 0:a9259748d982 11 * pack (document) with int base values
kpniot 0:a9259748d982 12 */
kpniot 0:a9259748d982 13
kpniot 0:a9259748d982 14
kpniot 0:a9259748d982 15 #include <senml_int_pack.h>
kpniot 0:a9259748d982 16 #include <senml_helpers.h>
kpniot 0:a9259748d982 17 #include <cbor.h>
kpniot 0:a9259748d982 18
kpniot 0:a9259748d982 19
kpniot 0:a9259748d982 20 void SenMLIntPack::setupStreamCtx(Stream *dest, SenMLStreamMethod format)
kpniot 0:a9259748d982 21 {
kpniot 0:a9259748d982 22 SenMLPack::setupStreamCtx(dest, format);
kpniot 0:a9259748d982 23 _streamCtx->baseValue.baseInt = this->getBaseValue();
kpniot 0:a9259748d982 24 _streamCtx->baseSum.baseInt = this->getBaseValue();
kpniot 0:a9259748d982 25 _streamCtx->baseDataType = CBOR_TYPE_INT;
kpniot 0:a9259748d982 26 }
kpniot 0:a9259748d982 27
kpniot 0:a9259748d982 28 void SenMLIntPack::setupStreamCtx(char *dest, int length, SenMLStreamMethod format)
kpniot 0:a9259748d982 29 {
kpniot 0:a9259748d982 30 SenMLPack::setupStreamCtx(dest, length, format);
kpniot 0:a9259748d982 31 _streamCtx->baseValue.baseInt = this->getBaseValue();
kpniot 0:a9259748d982 32 _streamCtx->baseSum.baseInt = this->getBaseValue();
kpniot 0:a9259748d982 33 _streamCtx->baseDataType = CBOR_TYPE_INT;
kpniot 0:a9259748d982 34 }
kpniot 0:a9259748d982 35
kpniot 0:a9259748d982 36 void SenMLIntPack::fieldsToJson()
kpniot 0:a9259748d982 37 {
kpniot 0:a9259748d982 38 int val;
kpniot 0:a9259748d982 39 String strVal;
kpniot 0:a9259748d982 40 SenMLPack::fieldsToJson();
kpniot 0:a9259748d982 41 val = this->getBaseValue();
kpniot 0:a9259748d982 42 if(val != 0){
kpniot 0:a9259748d982 43 printText(",\"bv\":", 6);
kpniot 0:a9259748d982 44 strVal = val;
kpniot 0:a9259748d982 45 printText(strVal.c_str(), strVal.length());
kpniot 0:a9259748d982 46 }
kpniot 0:a9259748d982 47
kpniot 0:a9259748d982 48 val = this->getBaseSum();
kpniot 0:a9259748d982 49 if(val != 0){
kpniot 0:a9259748d982 50 printText(",\"bs\":", 6);
kpniot 0:a9259748d982 51 strVal = val;
kpniot 0:a9259748d982 52 printText(strVal.c_str(), strVal.length());
kpniot 0:a9259748d982 53 }
kpniot 0:a9259748d982 54
kpniot 0:a9259748d982 55 }
kpniot 0:a9259748d982 56
kpniot 0:a9259748d982 57 int SenMLIntPack::fieldsToCbor()
kpniot 0:a9259748d982 58 {
kpniot 0:a9259748d982 59 int val;
kpniot 0:a9259748d982 60 int res = SenMLPack::fieldsToCbor();
kpniot 0:a9259748d982 61
kpniot 0:a9259748d982 62 val = this->getBaseValue();
kpniot 0:a9259748d982 63 if(val){
kpniot 0:a9259748d982 64 res += cbor_serialize_int(SENML_CBOR_VB_LABEL);
kpniot 0:a9259748d982 65 res += cbor_serialize_int(val);
kpniot 0:a9259748d982 66 }
kpniot 0:a9259748d982 67
kpniot 0:a9259748d982 68 val = this->getBaseSum();
kpniot 0:a9259748d982 69 if(val){
kpniot 0:a9259748d982 70 res += cbor_serialize_int(SENML_CBOR_BS_LABEL);
kpniot 0:a9259748d982 71 res += cbor_serialize_int(val);
kpniot 0:a9259748d982 72 }
kpniot 0:a9259748d982 73 return res;
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
kpniot 0:a9259748d982 81