KPN Things Device SDK
senml_basepack.h
Go to the documentation of this file.
1/* _ __ ____ _ _
2 * | |/ / | _ \ | \ | |
3 * | ' / | |_) | | \| |
4 * | . \ | __/ | |\ |
5 * |_|\_\ |_| |_| \_|
6 *
7 * (c) 2020 KPN
8 * License: MIT License.
9 * Author: Joseph Verburg, Jan Bogaerts
10 *
11 * pack (document) without base values headers
12 */
13
14#ifndef THINGSML_SENML_BASE_PACK
15#define THINGSML_SENML_BASE_PACK
16
17#include "defaults.h"
18#include "senml_base.h"
19
24class SenMLBasePack : public SenMLBase {
25 friend class SenMLJsonListener;
26 friend class SenMLCborParser;
27 friend class SenMLPack;
28 friend class SenMLBase;
29
30 public:
40 int toJson(Stream &dest, SenMLStreamMethod format = SENML_RAW);
41
51 int toJson(char *dest, int length, SenMLStreamMethod format = SENML_RAW);
52
60 int toCbor(Stream &dest, SenMLStreamMethod format = SENML_RAW);
61
71 int toCbor(char *dest, int length, SenMLStreamMethod format = SENML_RAW);
72
86 void fromJson(Stream &source, SenMLStreamMethod format = SENML_RAW);
87
97 void fromJson(const char *source);
98
112 void fromCbor(Stream &source, SenMLStreamMethod format = SENML_RAW);
113
125 void fromCbor(char *source, int length, SenMLStreamMethod format);
126
136 bool add(SenMLBase &item);
137
142 bool clear();
143
149 inline SenMLBase *getFirst() {
150 return this->_start;
151 };
152
156 virtual int getCount();
157
165 virtual int fieldsToJson() = 0;
166
175 virtual int fieldsToCbor() = 0;
176
177 virtual const char *getBaseName() {
178 return "";
179 };
180
181 virtual inline SenMLUnit getBaseUnit() {
182 return SENML_UNIT_NONE;
183 };
184
185 protected:
186 // derived classes can use this function to see if the root object (getRoot) is a SenMLPack
187 // class or not.
188 virtual bool isPack() {
189 return true;
190 }
191
192 virtual bool isActuator() {
193 return false;
194 }
195
196 // store a ref to the last item in the list for quick link operations
197 void setLast(SenMLBase &value);
198
199 // renders the content of the pack object without []
200 virtual int contentToCbor();
201
202 virtual int contentToJson();
203
204 // calculates the nr of items that there will be in the json array in senml representation
205 // this is used for rendering cbor which needs to declare the nr of elements in an array.
206 virtual int getArrayLength();
207
208 virtual int getFieldLength() {
209 return 0;
210 };
211
212 virtual void setupStreamCtx(char *dest, int length, SenMLStreamMethod format);
213
214 virtual void setupStreamCtx(Stream *dest, SenMLStreamMethod format);
215
216 private:
217 SenMLBase *_end = NULL; // keeps track of the end of the list
218 SenMLBase *_start = NULL; // keeps track of the start of the list
219
220 int internalToJson();
221
222 inline char readHexChar(Stream *source) {
223#ifdef __MBED__
224 unsigned char first = source->getc();
225 unsigned char second = source->getc();
226#else
227 unsigned char first = source->read();
228 unsigned char second = source->read();
229#endif
230 first = (first < '9') ? first - '0' : first - '7';
231 second = (second < '9') ? second - '0' : second - '7';
232 return (16 * first) + second;
233 };
234};
235
236#endif // THINGSML_SENML_BASE_PACK
Definition: senml_base.h:24
Definition: senml_basepack.h:24
virtual int contentToCbor()
Definition: senml_basepack.cpp:190
int toCbor(Stream &dest, SenMLStreamMethod format=SENML_RAW)
Definition: senml_basepack.cpp:228
virtual int contentToJson()
Definition: senml_basepack.cpp:164
virtual bool isActuator()
Definition: senml_basepack.h:192
SenMLBasePack()
Definition: senml_basepack.h:31
void fromCbor(Stream &source, SenMLStreamMethod format=SENML_RAW)
Definition: senml_basepack.cpp:127
void setLast(SenMLBase &value)
Definition: senml_basepack.cpp:24
virtual const char * getBaseName()
Definition: senml_basepack.h:177
virtual bool isPack()
Definition: senml_basepack.h:188
virtual int getFieldLength()
Definition: senml_basepack.h:208
virtual int getArrayLength()
Definition: senml_basepack.cpp:251
virtual int fieldsToJson()=0
bool add(SenMLBase &item)
Definition: senml_basepack.cpp:33
SenMLBase * getFirst()
Definition: senml_basepack.h:149
~SenMLBasePack()
Definition: senml_basepack.h:32
int toJson(Stream &dest, SenMLStreamMethod format=SENML_RAW)
Definition: senml_basepack.cpp:137
virtual SenMLUnit getBaseUnit()
Definition: senml_basepack.h:181
bool clear()
Definition: senml_basepack.cpp:51
virtual int getCount()
Definition: senml_basepack.cpp:70
void fromJson(Stream &source, SenMLStreamMethod format=SENML_RAW)
Definition: senml_basepack.cpp:85
virtual int fieldsToCbor()=0
virtual void setupStreamCtx(char *dest, int length, SenMLStreamMethod format)
Definition: senml_basepack.cpp:208
Definition: senml_cbor_parser.h:31
Definition: senml_json_parser.h:23
Definition: senml_pack.h:68
Definition: defaults.h:50
unsigned char read()
Definition: defaults.h:52
SenMLUnit
Definition: senml_enums.h:97
@ SENML_UNIT_NONE
Definition: senml_enums.h:98
SenMLStreamMethod
Definition: senml_enums.h:49
@ SENML_RAW
Definition: senml_enums.h:49