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