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

Committer:
kpniot
Date:
Sun May 27 14:31:44 2018 +0000
Revision:
2:9b44be6e79ac
Parent:
0:a9259748d982
try to fix repo

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 * base class for all senml objects header
kpniot 0:a9259748d982 12 */
kpniot 0:a9259748d982 13
kpniot 0:a9259748d982 14 #ifndef SENMLBASE
kpniot 0:a9259748d982 15 #define SENMLBASE
kpniot 0:a9259748d982 16
kpniot 0:a9259748d982 17 #ifdef __MBED__
kpniot 0:a9259748d982 18 #include "mbed.h"
kpniot 0:a9259748d982 19 #include "sstream"
kpniot 0:a9259748d982 20 #else
kpniot 0:a9259748d982 21 #include <stream.h>
kpniot 0:a9259748d982 22 #endif
kpniot 0:a9259748d982 23 #include <senml_enums.h>
kpniot 0:a9259748d982 24 #include <math.h>
kpniot 0:a9259748d982 25
kpniot 0:a9259748d982 26 /**
kpniot 0:a9259748d982 27 * the base class for all objects that can be used in the senml data tree.
kpniot 0:a9259748d982 28 */
kpniot 0:a9259748d982 29 class SenMLBase
kpniot 0:a9259748d982 30 {
kpniot 0:a9259748d982 31 friend class SenMLPack;
kpniot 0:a9259748d982 32 friend class SenMLRecord;
kpniot 0:a9259748d982 33 friend class SenMLJsonListener;
kpniot 0:a9259748d982 34 friend class SenMLBaseParser;
kpniot 0:a9259748d982 35 public:
kpniot 0:a9259748d982 36 SenMLBase();
kpniot 0:a9259748d982 37 ~SenMLBase();
kpniot 0:a9259748d982 38
kpniot 0:a9259748d982 39 /** get the next item in the list.
kpniot 0:a9259748d982 40 * @returns: a pointer to the next SenMLBase object in the list or NULL when at the end of the list.
kpniot 0:a9259748d982 41 */
kpniot 0:a9259748d982 42 SenMLBase* getNext(){ return this->_next; };
kpniot 0:a9259748d982 43
kpniot 0:a9259748d982 44 /**
kpniot 0:a9259748d982 45 * Get the root object of this list. Usually, this is a SenMLPack object.
kpniot 0:a9259748d982 46 * The root object is defined as the first item in the list.
kpniot 0:a9259748d982 47 * @returns: a pointer to the first SenMLBase object in the list or NULL when there is none.
kpniot 0:a9259748d982 48 */
kpniot 0:a9259748d982 49 SenMLBase* getRoot();
kpniot 0:a9259748d982 50
kpniot 0:a9259748d982 51 //This function is called by the root SenMLPack object to indicate that the object
kpniot 0:a9259748d982 52 //should adjust it's time info relative to the new base time (if applicable)
kpniot 0:a9259748d982 53 //doesn't do anything by default
kpniot 0:a9259748d982 54 //params: prev: previous base time
kpniot 0:a9259748d982 55 // time: new base time
kpniot 0:a9259748d982 56 virtual void adjustToBaseTime(double prev, double time) {};
kpniot 0:a9259748d982 57
kpniot 0:a9259748d982 58 /**
kpniot 0:a9259748d982 59 * renders all the fields to json, without the starting and ending brackets.
kpniot 0:a9259748d982 60 * Inheriters can extend this function if they want to add extra fields to the json output
kpniot 0:a9259748d982 61 * note: this is public so that custom implementations for the record object can use other objects
kpniot 0:a9259748d982 62 * internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for lat, lon & alt.
kpniot 0:a9259748d982 63 * @returns: None
kpniot 0:a9259748d982 64 */
kpniot 0:a9259748d982 65 virtual void fieldsToJson() = 0;
kpniot 0:a9259748d982 66
kpniot 0:a9259748d982 67 /**
kpniot 0:a9259748d982 68 * renders all the fields to cbor format. renders all the fields of the object without the length info
kpniot 0:a9259748d982 69 * at the beginning
kpniot 0:a9259748d982 70 * note: this is public so that custom implementations for the record object can use other objects
kpniot 0:a9259748d982 71 * internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for
kpniot 0:a9259748d982 72 * lat, lon & alt.
kpniot 0:a9259748d982 73 * @returns: The number of bytes that were written.
kpniot 0:a9259748d982 74 */
kpniot 0:a9259748d982 75 virtual int fieldsToCbor() = 0;
kpniot 0:a9259748d982 76
kpniot 0:a9259748d982 77 protected:
kpniot 0:a9259748d982 78
kpniot 0:a9259748d982 79 /*
kpniot 0:a9259748d982 80 renders all the fields to json, with the starting and ending brackets.
kpniot 0:a9259748d982 81 Inheriters can extend this function if they want to add extra fields to the json output
kpniot 0:a9259748d982 82 note: tihs is public so that custom implementations for the record object can use other objects internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for lat, lon & alt.
kpniot 0:a9259748d982 83 */
kpniot 0:a9259748d982 84 virtual void contentToJson() = 0;
kpniot 0:a9259748d982 85
kpniot 0:a9259748d982 86
kpniot 0:a9259748d982 87 //assign the element in the list that this object points to.
kpniot 0:a9259748d982 88 void setNext(SenMLBase* value);
kpniot 0:a9259748d982 89
kpniot 0:a9259748d982 90 //assign the previous element in the list that thisobject points to.
kpniot 0:a9259748d982 91 void setPrev(SenMLBase* value);
kpniot 0:a9259748d982 92 //assign the previous element in the list that thisobject points to.
kpniot 0:a9259748d982 93 SenMLBase* getPrev();
kpniot 0:a9259748d982 94
kpniot 0:a9259748d982 95 //derived classes can use this function to see if the root object (getRoot) is a SenMLPack
kpniot 0:a9259748d982 96 //class or not.
kpniot 0:a9259748d982 97 virtual bool isPack() { return false; }
kpniot 0:a9259748d982 98
kpniot 0:a9259748d982 99 //renders the content of the pack object without [], but still with {} for objects
kpniot 0:a9259748d982 100 virtual int contentToCbor() = 0;
kpniot 0:a9259748d982 101
kpniot 0:a9259748d982 102 //calculates the nr of items that this object will put in the json array in senml representation
kpniot 0:a9259748d982 103 //this is used for rendering cbor which needs to declare the nr of elements in an array.
kpniot 0:a9259748d982 104 //packs can have multiple elements in the array, but also custom implementations for records that wrap muliple
kpniot 0:a9259748d982 105 //records.
kpniot 0:a9259748d982 106 virtual int getArrayLength() { return 1; };
kpniot 0:a9259748d982 107
kpniot 0:a9259748d982 108 //calculates the nr of fields that this object will produce.
kpniot 0:a9259748d982 109 virtual int getFieldLength() = 0;
kpniot 0:a9259748d982 110
kpniot 0:a9259748d982 111
kpniot 0:a9259748d982 112 private:
kpniot 0:a9259748d982 113 SenMLBase* _next; //reference to the next element in the list.
kpniot 0:a9259748d982 114 SenMLBase* _prev; //reference to the previous element, needed for deletion of record
kpniot 0:a9259748d982 115 };
kpniot 0:a9259748d982 116
kpniot 0:a9259748d982 117
kpniot 0:a9259748d982 118
kpniot 0:a9259748d982 119 #endif // SENMLBASE
kpniot 0:a9259748d982 120
kpniot 0:a9259748d982 121
kpniot 0:a9259748d982 122
kpniot 0:a9259748d982 123
kpniot 0:a9259748d982 124
kpniot 0:a9259748d982 125
kpniot 0:a9259748d982 126