KPN Things Device SDK
senml_helpers.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 * helper functions header
12 */
13
14#ifndef THINGSML_SENML_HELPERS
15#define THINGSML_SENML_HELPERS
16
17#include "defaults.h"
18#include "senml_enums.h"
19#include <math.h>
20
21#ifndef SENML_MAX_DOUBLE_PRECISION
22#define SENML_MAX_DOUBLE_PRECISION 8
23#endif // !SENML_MAX_DOUBLE_PRECISION
24
28typedef union BaseData_t {
29 uint64_t baseUint;
30 int64_t baseInt;
31 double baseDouble;
33
37typedef struct SenmlMemoryData_t {
38 char *data; // pointer to the buffer that constitutes the input/output buffer
39 int curPos; // current position in the buffer.
40 int length; // length of the memory buffer.
41} SenmlMemoryData; // a record used when the input for the parser or output for the renderer is a data blob stored in
42 // memory
43
47typedef union SenmlData_t {
50} SenmlData; // choose between input/outpu from a stream object (ex: direct input from uart) or from buffered data.
51
55typedef struct StreamContext_t {
56 bool dataAsBlob; // when true, data is from a memory blob, otherwise it comes from/goes to a stream object (ex:
57 // direct from/to uart)
58 SenmlData data; // the data source to be parsed or destination to render to. can be from a stream or from data
59 // buffered in memory
61 BaseData baseValue; // filled in when records need to adjust while rendering data.
63 SenMLDataType baseDataType; // so we know which data type to use for baseValue and baseSum
65
70
76int printByte(uint8_t value);
77
83int printInt(int i);
84
91int printDouble(double f, unsigned int digits);
92
99int printBinaryAsBase64(const unsigned char *data, unsigned int length);
100
106int printUnit(SenMLUnit unit);
107
114int printText(const char *value, int length);
115
122bool canPrint(int length);
123
124#ifdef THINGSML_LOW_RAM_DEVICE
125int printText(const __FlashStringHelper *value, int length);
126#else
127#define F(str) str
128#endif
129
135int readChar();
136
142int peekChar();
143
148void flush();
149
159inline void readChars(unsigned char *buffer, int len) {
160 for (int i = 0; i < len; i++) {
161 buffer[i] = readChar();
162 }
163}
164
170inline bool charAvailable() {
171 if (_streamCtx->dataAsBlob) {
173 } else {
174#ifdef __MBED__
175 return _streamCtx->data.stream->readable() != 0;
176#else
177 return _streamCtx->data.stream->available() != 0;
178#endif
179 }
180}
181
182#endif // THINGSML_SENML_HELPERS
Definition: defaults.h:50
int available()
Definition: defaults.h:58
SenMLUnit
Definition: senml_enums.h:97
SenMLStreamMethod
Definition: senml_enums.h:49
SenMLDataType
Definition: senml_enums.h:25
void readChars(unsigned char *buffer, int len)
Definition: senml_helpers.h:159
StreamContext * _streamCtx
Definition: senml_helpers.cpp:27
int printInt(int i)
Definition: senml_helpers.cpp:69
int printBinaryAsBase64(const unsigned char *data, unsigned int length)
Definition: senml_helpers.cpp:105
int printText(const char *value, int length)
Definition: senml_helpers.cpp:129
int printByte(uint8_t value)
Definition: senml_helpers.cpp:31
union SenmlData_t SenmlData
bool canPrint(int length)
Definition: senml_helpers.cpp:219
struct StreamContext_t StreamContext
int printUnit(SenMLUnit unit)
Definition: senml_helpers.cpp:117
bool charAvailable()
Definition: senml_helpers.h:170
struct SenmlMemoryData_t SenmlMemoryData
int peekChar()
Definition: senml_helpers.cpp:284
int printDouble(double f, unsigned int digits)
Definition: senml_helpers.cpp:79
union BaseData_t BaseData
void flush()
Definition: senml_helpers.cpp:297
int readChar()
Definition: senml_helpers.cpp:231
Definition: senml_helpers.h:37
int length
Definition: senml_helpers.h:40
char * data
Definition: senml_helpers.h:38
int curPos
Definition: senml_helpers.h:39
Definition: senml_helpers.h:55
SenMLDataType baseDataType
Definition: senml_helpers.h:63
BaseData baseValue
Definition: senml_helpers.h:61
SenmlData data
Definition: senml_helpers.h:58
BaseData baseSum
Definition: senml_helpers.h:62
SenMLStreamMethod format
Definition: senml_helpers.h:60
bool dataAsBlob
Definition: senml_helpers.h:56
Definition: senml_helpers.h:28
double baseDouble
Definition: senml_helpers.h:31
uint64_t baseUint
Definition: senml_helpers.h:29
int64_t baseInt
Definition: senml_helpers.h:30
Definition: senml_helpers.h:47
Stream * stream
Definition: senml_helpers.h:48
SenmlMemoryData blob
Definition: senml_helpers.h:49