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.
senml_double_pack.cpp@0:a9259748d982, 2018-05-19 (annotated)
- Committer:
- kpniot
- Date:
- Sat May 19 17:35:20 2018 +0000
- Revision:
- 0:a9259748d982
first commit
Who changed what in which revision?
User | Revision | Line number | New 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 double base values |
kpniot | 0:a9259748d982 | 12 | */ |
kpniot | 0:a9259748d982 | 13 | |
kpniot | 0:a9259748d982 | 14 | |
kpniot | 0:a9259748d982 | 15 | #include <senml_double_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 SenMLDoublePack::setupStreamCtx(Stream *dest, SenMLStreamMethod format) |
kpniot | 0:a9259748d982 | 21 | { |
kpniot | 0:a9259748d982 | 22 | SenMLPack::setupStreamCtx(dest, format); |
kpniot | 0:a9259748d982 | 23 | _streamCtx->baseValue.baseDouble = this->getBaseValue(); |
kpniot | 0:a9259748d982 | 24 | _streamCtx->baseSum.baseDouble = this->getBaseValue(); |
kpniot | 0:a9259748d982 | 25 | _streamCtx->baseDataType = CBOR_TYPE_DOUBLE; |
kpniot | 0:a9259748d982 | 26 | } |
kpniot | 0:a9259748d982 | 27 | |
kpniot | 0:a9259748d982 | 28 | void SenMLDoublePack::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.baseDouble = this->getBaseValue(); |
kpniot | 0:a9259748d982 | 32 | _streamCtx->baseSum.baseDouble = this->getBaseValue(); |
kpniot | 0:a9259748d982 | 33 | _streamCtx->baseDataType = CBOR_TYPE_DOUBLE; |
kpniot | 0:a9259748d982 | 34 | } |
kpniot | 0:a9259748d982 | 35 | |
kpniot | 0:a9259748d982 | 36 | void SenMLDoublePack::fieldsToJson() |
kpniot | 0:a9259748d982 | 37 | { |
kpniot | 0:a9259748d982 | 38 | double val; |
kpniot | 0:a9259748d982 | 39 | SenMLPack::fieldsToJson(); |
kpniot | 0:a9259748d982 | 40 | val = this->getBaseValue(); |
kpniot | 0:a9259748d982 | 41 | if(val != 0){ |
kpniot | 0:a9259748d982 | 42 | printText(",\"bv\":", 6); |
kpniot | 0:a9259748d982 | 43 | printDouble(val, SENML_MAX_DOUBLE_PRECISION); |
kpniot | 0:a9259748d982 | 44 | } |
kpniot | 0:a9259748d982 | 45 | |
kpniot | 0:a9259748d982 | 46 | val = this->getBaseSum(); |
kpniot | 0:a9259748d982 | 47 | if(val != 0){ |
kpniot | 0:a9259748d982 | 48 | printText(",\"bs\":", 6); |
kpniot | 0:a9259748d982 | 49 | printDouble(val, SENML_MAX_DOUBLE_PRECISION); |
kpniot | 0:a9259748d982 | 50 | } |
kpniot | 0:a9259748d982 | 51 | |
kpniot | 0:a9259748d982 | 52 | } |
kpniot | 0:a9259748d982 | 53 | |
kpniot | 0:a9259748d982 | 54 | int SenMLDoublePack::fieldsToCbor() |
kpniot | 0:a9259748d982 | 55 | { |
kpniot | 0:a9259748d982 | 56 | int val; |
kpniot | 0:a9259748d982 | 57 | int res = SenMLPack::fieldsToCbor(); |
kpniot | 0:a9259748d982 | 58 | |
kpniot | 0:a9259748d982 | 59 | val = this->getBaseValue(); |
kpniot | 0:a9259748d982 | 60 | if(val){ |
kpniot | 0:a9259748d982 | 61 | res += cbor_serialize_int(SENML_CBOR_VB_LABEL); |
kpniot | 0:a9259748d982 | 62 | res += cbor_serialize_double(val); |
kpniot | 0:a9259748d982 | 63 | } |
kpniot | 0:a9259748d982 | 64 | |
kpniot | 0:a9259748d982 | 65 | val = this->getBaseSum(); |
kpniot | 0:a9259748d982 | 66 | if(val){ |
kpniot | 0:a9259748d982 | 67 | res += cbor_serialize_int(SENML_CBOR_BS_LABEL); |
kpniot | 0:a9259748d982 | 68 | res += cbor_serialize_double(val); |
kpniot | 0:a9259748d982 | 69 | } |
kpniot | 0:a9259748d982 | 70 | return res; |
kpniot | 0:a9259748d982 | 71 | } |
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 |