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 * (c) 2018 KPN
kpniot 0:a9259748d982 8 * License: MIT License.
kpniot 0:a9259748d982 9 * Author: Jan Bogaerts
kpniot 0:a9259748d982 10 *
kpniot 0:a9259748d982 11 * felper functions header
kpniot 0:a9259748d982 12 */
kpniot 0:a9259748d982 13
kpniot 0:a9259748d982 14 #ifndef SENMLHELPERS
kpniot 0:a9259748d982 15 #define SENMLHELPERS
kpniot 0:a9259748d982 16
kpniot 0:a9259748d982 17 #ifdef __MBED__
kpniot 0:a9259748d982 18 #include "mbed.h"
kpniot 0:a9259748d982 19 #include "sstream"
kpniot 0:a9259748d982 20 #else
kpniot 0:a9259748d982 21 #include <stream.h>
kpniot 0:a9259748d982 22 #endif
kpniot 0:a9259748d982 23 #include <senml_enums.h>
kpniot 0:a9259748d982 24 #include <math.h>
kpniot 0:a9259748d982 25
kpniot 0:a9259748d982 26 #ifndef SENML_MAX_DOUBLE_PRECISION
kpniot 0:a9259748d982 27 #define SENML_MAX_DOUBLE_PRECISION 4
kpniot 0:a9259748d982 28 #endif // !SENML_MAX_DOUBLE_PRECISION
kpniot 0:a9259748d982 29
kpniot 0:a9259748d982 30 /**
kpniot 0:a9259748d982 31 * Internal helper struct that stores the base value to be used during rendering
kpniot 0:a9259748d982 32 */
kpniot 0:a9259748d982 33 typedef union BaseData_t{
kpniot 0:a9259748d982 34 uint64_t baseUint;
kpniot 0:a9259748d982 35 int64_t baseInt;
kpniot 0:a9259748d982 36 double baseDouble;
kpniot 0:a9259748d982 37 } BaseData;
kpniot 0:a9259748d982 38
kpniot 0:a9259748d982 39 /**
kpniot 0:a9259748d982 40 * Internal helper struct that stores information needed during the rendering process
kpniot 0:a9259748d982 41 */
kpniot 0:a9259748d982 42 typedef struct SenmlMemoryData_t{
kpniot 0:a9259748d982 43 char* data; //pointer to the buffer that constitutes the input/output buffer
kpniot 0:a9259748d982 44 int curPos; //current position in the buffer.
kpniot 0:a9259748d982 45 int length; //length of the memory buffer.
kpniot 0:a9259748d982 46 } SenmlMemoryData; //a record used when the input for the parser or output for the renderer is a data blob stored in memory
kpniot 0:a9259748d982 47
kpniot 0:a9259748d982 48 /**
kpniot 0:a9259748d982 49 * Internal helper struct that stores information needed during the rendering process
kpniot 0:a9259748d982 50 */
kpniot 0:a9259748d982 51 typedef union SenmlData_t{
kpniot 0:a9259748d982 52 Stream* stream;
kpniot 0:a9259748d982 53 SenmlMemoryData blob;
kpniot 0:a9259748d982 54 } SenmlData; //choose between input/outpu from a stream object (ex: direct input from uart) or from buffered data.
kpniot 0:a9259748d982 55
kpniot 0:a9259748d982 56 /**
kpniot 0:a9259748d982 57 * Internal helper struct that stores information needed during the rendering process
kpniot 0:a9259748d982 58 */
kpniot 0:a9259748d982 59 typedef struct StreamContext_t{
kpniot 0:a9259748d982 60 bool dataAsBlob; //when true, data is from a memory blob, otherwise it comes from/goes to a stream object (ex: direct from/to uart)
kpniot 0:a9259748d982 61 SenmlData data; //the data source to be parsed or destination to render to. can be from a stream or from data buffered in memory
kpniot 0:a9259748d982 62 SenMLStreamMethod format;
kpniot 0:a9259748d982 63 BaseData baseValue; //filled in when records need to adjust while rendering data.
kpniot 0:a9259748d982 64 BaseData baseSum;
kpniot 0:a9259748d982 65 SenMLDataType baseDataType; //so we know which data type to use for baseValue and baseSum
kpniot 0:a9259748d982 66 } StreamContext;
kpniot 0:a9259748d982 67
kpniot 0:a9259748d982 68 /**
kpniot 0:a9259748d982 69 * Internal data field used for the rendering process.
kpniot 0:a9259748d982 70 */
kpniot 0:a9259748d982 71 extern StreamContext* _streamCtx;
kpniot 0:a9259748d982 72
kpniot 0:a9259748d982 73 /**
kpniot 0:a9259748d982 74 * Helper function for the generation process
kpniot 0:a9259748d982 75 * write a double as string to the stream (_streamCtx).
kpniot 0:a9259748d982 76 * @param f the value to print
kpniot 0:a9259748d982 77 * @param digits the nr of digits that should be printed after the comma.
kpniot 0:a9259748d982 78 */
kpniot 0:a9259748d982 79 void printDouble(double f, unsigned int digits) ;
kpniot 0:a9259748d982 80
kpniot 0:a9259748d982 81 /**
kpniot 0:a9259748d982 82 * Helper function for the generation process
kpniot 0:a9259748d982 83 * convert the data array to base64 string and write to stream (_streamCtx)
kpniot 0:a9259748d982 84 * @param data pointer to the binary data blob that needs to be rendered as base64 to the stream.
kpniot 0:a9259748d982 85 * @param length the length of the data blob
kpniot 0:a9259748d982 86 */
kpniot 0:a9259748d982 87 void printBinaryAsBase64(const unsigned char* data, unsigned int length);
kpniot 0:a9259748d982 88
kpniot 0:a9259748d982 89 /**
kpniot 0:a9259748d982 90 * Helper function for the generation process
kpniot 0:a9259748d982 91 * convert the unit to string and write to stream (_streamCtx).
kpniot 0:a9259748d982 92 * @param unit the value to write as text to the stream.
kpniot 0:a9259748d982 93 */
kpniot 0:a9259748d982 94 void printUnit(SenMLUnit unit);
kpniot 0:a9259748d982 95
kpniot 0:a9259748d982 96 /**
kpniot 0:a9259748d982 97 * Helper function for the generation process
kpniot 0:a9259748d982 98 * Writes the specified text to the stream (_streamCtx).
kpniot 0:a9259748d982 99 * @param value the value to write
kpniot 0:a9259748d982 100 * @param len the length of the value.
kpniot 0:a9259748d982 101 */
kpniot 0:a9259748d982 102 void printText(const char*value, int len);
kpniot 0:a9259748d982 103
kpniot 0:a9259748d982 104
kpniot 0:a9259748d982 105
kpniot 0:a9259748d982 106 /**
kpniot 0:a9259748d982 107 * Helper function for the parsing process
kpniot 0:a9259748d982 108 * read a character from the current data stream (_streamCtx)
kpniot 0:a9259748d982 109 * takes into account that the stream might contain hex values
kpniot 0:a9259748d982 110 */
kpniot 0:a9259748d982 111 int readChar();
kpniot 0:a9259748d982 112
kpniot 0:a9259748d982 113 /**
kpniot 0:a9259748d982 114 * Helper function for the parsing process
kpniot 0:a9259748d982 115 * peek a character from the current data stream.
kpniot 0:a9259748d982 116 * takes into account that the stream might contain hex values.
kpniot 0:a9259748d982 117 */
kpniot 0:a9259748d982 118 int peekChar();
kpniot 0:a9259748d982 119
kpniot 0:a9259748d982 120 /**
kpniot 0:a9259748d982 121 * Helper function for the parsing process
kpniot 0:a9259748d982 122 * flush and reset all input for the current data stream.
kpniot 0:a9259748d982 123 */
kpniot 0:a9259748d982 124 void flush();
kpniot 0:a9259748d982 125
kpniot 0:a9259748d982 126 /**
kpniot 0:a9259748d982 127 * Helper function for the parsing process
kpniot 0:a9259748d982 128 * Reads the specified nr of characters from the stream (_streamCtx)
kpniot 0:a9259748d982 129 * takes into account that the stream might contain hex values
kpniot 0:a9259748d982 130 * @param buffer the buffer to store the values in.
kpniot 0:a9259748d982 131 * @param len the nr of characters to read from the stream into the buffer.
kpniot 0:a9259748d982 132 * If the input contains hex values, the actual nr of characters read from the
kpniot 0:a9259748d982 133 * stream is double this value.
kpniot 0:a9259748d982 134 */
kpniot 0:a9259748d982 135 inline void readChars(unsigned char* buffer, int len)
kpniot 0:a9259748d982 136 {
kpniot 0:a9259748d982 137 for(int i = 0; i < len; i++){
kpniot 0:a9259748d982 138 buffer[i] = readChar();
kpniot 0:a9259748d982 139 }
kpniot 0:a9259748d982 140 }
kpniot 0:a9259748d982 141
kpniot 0:a9259748d982 142 /**
kpniot 0:a9259748d982 143 * Helper function for the parsing process
kpniot 0:a9259748d982 144 * Checks if there is data available on the stream.
kpniot 0:a9259748d982 145 * Warning: THIS CAN BE PROBLEMATIC ON MBED SYSTEMS>
kpniot 0:a9259748d982 146 */
kpniot 0:a9259748d982 147 inline bool charAvailable(){
kpniot 0:a9259748d982 148 if(_streamCtx->dataAsBlob){
kpniot 0:a9259748d982 149 return _streamCtx->data.blob.curPos < _streamCtx->data.blob.length;
kpniot 0:a9259748d982 150 }
kpniot 0:a9259748d982 151 else{
kpniot 0:a9259748d982 152 #ifdef __MBED__
kpniot 0:a9259748d982 153 return _streamCtx->data.stream->readable() != 0;
kpniot 0:a9259748d982 154 #else
kpniot 0:a9259748d982 155 return _streamCtx->data.stream->available() != 0;
kpniot 0:a9259748d982 156 #endif
kpniot 0:a9259748d982 157 }
kpniot 0:a9259748d982 158 }
kpniot 0:a9259748d982 159
kpniot 0:a9259748d982 160 #endif // SENMLHELPERS
kpniot 0:a9259748d982 161
kpniot 0:a9259748d982 162
kpniot 0:a9259748d982 163
kpniot 0:a9259748d982 164
kpniot 0:a9259748d982 165
kpniot 0:a9259748d982 166
kpniot 0:a9259748d982 167