This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsenml_pack.cpp
338 lines (290 loc) · 9.79 KB
/
senml_pack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* _ __ ____ _ _
* | |/ / | _ \ | \ | |
* | ' / | |_) | | \| |
* | . \ | __/ | |\ |
* |_|\_\ |_| |_| \_|
*
* (c) 2018 KPN
* License: MIT License.
* Author: Jan Bogaerts
*
* pack (document) without base values
*/
#include <senml_pack.h>
#include <senml_base.h>
#include <senml_helpers.h>
#include <senml_json_parser.h>
#include <senml_cbor_parser.h>
#include <senml_JsonStreamingParser.h>
#include <math.h>
#include <cbor.h>
#include <senml_logging.h>
//todo: inline these:
void SenMLPack::setBaseName(const char* name)
{
this->_bn = name;
}
const char* SenMLPack::getBaseName()
{
return this->_bn.c_str();
}
void SenMLPack::setBaseUnit(SenMLUnit unit)
{
this->_bu = unit;
}
void SenMLPack::setBaseTime(double time)
{
double prev = this->_bt;
this->_bt = time; //set before asking children -> could be better to do it afterwards?
SenMLBase *item = this->_start;
while(item){
item->adjustToBaseTime(prev, time);
item = item->getNext();
}
}
void SenMLPack::setLast(SenMLBase* value)
{
if(value == this) //if we become the last item in the list, then the list is empty.
this->_end = NULL;
else
this->_end = value;
}
bool SenMLPack::add(SenMLBase* item)
{
if(item->getNext() != NULL){
log_debug("already in list");
return false;
}
SenMLBase* last = this->_end;
if(last){
last->setNext(item);
item->setPrev(last);
}
else{
this->_start = item;
item->setPrev(this);
}
this->_end = item;
return true;
}
bool SenMLPack::clear()
{
SenMLBase *item = this->_start;
while(item){
if(item->isPack()) //if it's a pack element, it also needs to clear out it's children.
((SenMLPack*)item)->clear();
item->setPrev(NULL);
SenMLBase *next = item->getNext();
item->setNext(NULL);
item = next;
}
this->setNext(NULL);
this->setPrev(NULL);
this->_end = NULL;
this->_start = NULL;
return true;
}
void SenMLPack::fromJson(Stream *source, SenMLStreamMethod format)
{
JsonStreamingParser parser;
SenMLJsonListener listener(this);
parser.setListener(&listener);
char data;
if(format == SENML_RAW) {
#ifdef __MBED__
data = source->getc();
#else
data = source->read();
#endif
}
else{
data = readHexChar(source);
}
while(data != -1){
parser.parse(data);
if(format == SENML_RAW){
#ifdef __MBED__
data = source->getc();
#else
data = source->read();
#endif
}
else
data = readHexChar(source);
}
// when we get here, all the data is stored in the document and callbacks have been called.
}
void SenMLPack::fromJson(const char *source)
{
JsonStreamingParser parser;
SenMLJsonListener listener(this);
parser.setListener(&listener);
for(int i = 0; source[i] != 0; i++){
parser.parse(source[i]);
}
// when we get here, all the data is stored in the document and callbacks have been called.
}
void SenMLPack::fromCbor(Stream* source, SenMLStreamMethod format)
{
SenMLCborParser parser(this, format);
parser.parse(source);
}
void SenMLPack::fromCbor(char* source, int length, SenMLStreamMethod format)
{
SenMLCborParser parser(this, format);
parser.parse(source, length);
}
void SenMLPack::toJson(Stream *dest, SenMLStreamMethod format)
{
StreamContext renderTo; //set up the global record that configures the rendering. This saves us some bytes on the stack and in code by not having to pass along the values as function arguments.
_streamCtx = &renderTo;
this->setupStreamCtx(dest, format);
this->internalToJson();
}
void SenMLPack::toJson(char *dest, int length, SenMLStreamMethod format)
{
StreamContext renderTo; //set up the global record that configures the rendering. This saves us some bytes on the stack and in code by not having to pass along the values as function arguments.
_streamCtx = &renderTo;
this->setupStreamCtx(dest, length, format);
this->internalToJson();
}
//render the content of the current object to json data (string)
void SenMLPack::internalToJson()
{
printText("[", 1);
this->contentToJson();
printText("]", 1);
}
void SenMLPack::fieldsToJson()
{
int bnLength = this->_bn.length();
if(bnLength > 0){
printText("\"bn\":\"", 6);
printText(this->_bn.c_str(), bnLength);
printText("\"", 1);
}
if(this->_bu){
printText(",\"bu\":\"", 7);
printUnit(this->_bu);
printText("\"", 1);
}
if(!isnan(this->_bt)){
printText(",\"bt\":", 6);
printDouble(this->_bt, SENML_MAX_DOUBLE_PRECISION);
}
}
void SenMLPack::contentToJson()
{
printText("{", 1);
this->fieldsToJson();
SenMLBase *next = this->_start;
if(next && next->isPack() == false){ //we can only inline the first record. If the first item is a Pack (child device), then don't inline it.
printText(",", 1);
next->fieldsToJson();
next = next->getNext();
}
printText("}", 1);
while(next){
printText(",", 1);
next->contentToJson();
next = next->getNext();
}
}
void SenMLPack::setupStreamCtx(char *dest, int length, SenMLStreamMethod format)
{
_streamCtx->data.blob.data = dest;
_streamCtx->data.blob.length = length;
_streamCtx->data.blob.curPos = 0;
_streamCtx->dataAsBlob = true;
_streamCtx->format = format;
_streamCtx->baseValue.baseUint = 0; //by default, there is no base value or sum
_streamCtx->baseSum.baseUint = 0;
_streamCtx->baseDataType = CBOR_TYPE_DATA; //data never adjusts for basevalue, so this is safe.
}
void SenMLPack::setupStreamCtx(Stream *dest, SenMLStreamMethod format)
{
_streamCtx->data.stream = dest;
_streamCtx->format = format;
_streamCtx->dataAsBlob = false;
_streamCtx->baseValue.baseUint = 0; //by default, there is no base value or sum
_streamCtx->baseSum.baseUint = 0;
_streamCtx->baseDataType = CBOR_TYPE_DATA; //data never adjusts for basevalue, so this is safe.
}
int SenMLPack::toCbor(Stream *dest, SenMLStreamMethod format)
{
StreamContext renderTo; //set up the global record that configures the rendering. This saves us some bytes on the stack and in code by not having to pass along the values as function arguments.
_streamCtx = &renderTo;
this->setupStreamCtx(dest, format);
int res = cbor_serialize_array(this->getArrayLength());
res += this->contentToCbor();
return res;
}
int SenMLPack::toCbor(char *dest, int length, SenMLStreamMethod format)
{
StreamContext renderTo; //set up the global record that configures the rendering. This saves us some bytes on the stack and in code by not having to pass along the values as function arguments.
_streamCtx = &renderTo;
this->setupStreamCtx(dest, length, format);
int res = cbor_serialize_array(this->getArrayLength());
res += this->contentToCbor();
return res;
}
int SenMLPack::contentToCbor()
{
int length = cbor_serialize_map(this->getFieldLength());
int res = this->fieldsToCbor();
SenMLBase *next = this->_start;
if(next && next->isPack() == false){ //we can only inline the first record. If the first item is a Pack (child device), then don't inline it.
res += next->fieldsToCbor();
next = next->getNext();
}
while(next){
if(next->isPack() == false)
res += next->contentToCbor();
else
res += ((SenMLPack*)next)->contentToCbor();
next = next->getNext();
}
return res;
}
int SenMLPack::getArrayLength()
{
int result = 0; //init to 0 cause if there is a record, the first will become part of the first element in the array, if we were to init to 1, we would have 1 record too many.
SenMLBase *next = this->_start;
while(next){ //we can only inline the first record. If the first item is a Pack (child device), then don't inline it.
result += next->getArrayLength(); //custom record implementations may wrap multiple records.
next = next->getNext();
}
if(result == 0) //if there are no items in this pack, then we still render 1 array element, that of the pack itself.
result = 1;
return result;
}
int SenMLPack::getFieldLength()
{
int result = 0;
if(this->_bn.length() > 0 ) result++;
if(this->_bu) result++;
if(!isnan(this->_bt)) result++;
SenMLBase *next = this->_start;
if(next && next->isPack() == false){ //we can only inline the first record. If the first item is a Pack (child device), then don't inline it.
result += next->getFieldLength();
next = next->getNext();
}
return result;
}
int SenMLPack::fieldsToCbor()
{
int res = 0 ;
if(this->_bn.length() > 0 ){
res += cbor_serialize_int(SENML_CBOR_BN_LABEL);
res += cbor_serialize_unicode_string(this->_bn.c_str());
}
if(this->_bu){
res += cbor_serialize_int(SENML_CBOR_BU_LABEL);
res += cbor_serialize_unicode_string(senml_units_names[this->_bu]);
}
if(!isnan(this->_bt)){
res += cbor_serialize_int(SENML_CBOR_BT_LABEL);
res += cbor_serialize_double(this->_bt);
}
return res;
}