KPN IoT / senml

Fork of kpn_senml by KPN IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers senml_base.h Source File

senml_base.h

00001 /*  _  __  ____    _   _ 
00002  * | |/ / |  _ \  | \ | |
00003  * | ' /  | |_) | |  \| |
00004  * | . \  |  __/  | |\  |
00005  * |_|\_\ |_|     |_| \_|
00006  * 
00007  * (c) 2018 KPN
00008  * License: MIT License.
00009  * Author: Jan Bogaerts
00010  * 
00011  * base class for all senml objects header
00012  */
00013 
00014 #ifndef SENMLBASE
00015 #define SENMLBASE
00016 
00017 #ifdef __MBED__
00018     #include "mbed.h"
00019     #include "sstream"
00020 #else
00021     #include <stream.h>
00022 #endif
00023 #include <senml_enums.h>
00024 #include <math.h>
00025 
00026 /**
00027  * the base class for all objects that can be used in the senml data tree.
00028  */
00029 class SenMLBase
00030 {
00031 friend class SenMLPack; 
00032 friend class SenMLRecord; 
00033 friend class SenMLJsonListener; 
00034 friend class SenMLBaseParser;
00035 public:
00036     SenMLBase();
00037     ~SenMLBase();
00038 
00039     /** get the next item in the list.
00040      * @returns: a pointer to the next SenMLBase object in the list or NULL when at the end of the list.
00041      */
00042     SenMLBase* getNext(){ return this->_next; };
00043 
00044     /**
00045      * Get the root object of this list. Usually, this is a SenMLPack object.
00046      * The root object is defined as the first item in the list. 
00047      * @returns: a pointer to the first SenMLBase object in the list or NULL when there is none.
00048      */
00049     SenMLBase* getRoot();
00050 
00051     //This function is called by the root SenMLPack object to indicate that the object
00052     //should adjust it's time info relative to the new base time (if applicable)
00053     //doesn't do anything by default
00054     //params: prev: previous base time
00055     //        time: new base time
00056     virtual void adjustToBaseTime(double prev, double time) {};
00057 
00058     /**
00059      * renders all the fields to json, without the starting and ending brackets.
00060      * Inheriters can extend this function if they want to add extra fields to the json output
00061      * note: this is public so that custom implementations for the record object can use other objects 
00062      * internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for lat, lon & alt.
00063      * @returns: None
00064     */
00065     virtual void fieldsToJson() = 0;
00066 
00067     /**
00068      * renders all the fields to cbor format. renders all the fields of the object without the length info 
00069      * at the beginning
00070      * note: this is public so that custom implementations for the record object can use other objects 
00071      * internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for 
00072      * lat, lon & alt.
00073      * @returns: The number of bytes that were written.
00074     */
00075     virtual int fieldsToCbor() = 0;
00076 
00077 protected:
00078 
00079     /*
00080     renders all the fields to json, with the starting and ending brackets.
00081     Inheriters can extend this function if they want to add extra fields to the json output
00082     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.
00083     */
00084     virtual void contentToJson() = 0;
00085 
00086 
00087     //assign the element in the list that this object points to.
00088     void setNext(SenMLBase* value);
00089     
00090     //assign the previous element in the list that thisobject points to.
00091     void setPrev(SenMLBase* value);
00092     //assign the previous element in the list that thisobject points to.
00093     SenMLBase* getPrev();
00094 
00095     //derived classes can use this function to see if the root object (getRoot) is a SenMLPack
00096     //class or not.
00097     virtual bool isPack() { return false; }
00098 
00099     //renders the content of the pack object without [], but still with {} for objects
00100     virtual int contentToCbor() = 0;    
00101 
00102     //calculates the nr of items that this object will put in the json array in senml representation
00103     //this is used for rendering cbor which needs to declare the nr of elements in an array.
00104     //packs can have multiple elements in the array, but also custom implementations for records that wrap muliple 
00105     //records.
00106     virtual int getArrayLength() { return 1; };
00107 
00108     //calculates the nr of fields that this object will produce.
00109     virtual int getFieldLength() = 0;
00110 
00111 
00112 private:
00113     SenMLBase* _next;                               //reference to the next element in the list.
00114     SenMLBase* _prev;                               //reference to the previous element, needed for deletion of record
00115 };
00116 
00117 
00118 
00119 #endif // SENMLBASE
00120 
00121 
00122 
00123 
00124 
00125 
00126