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.

Fork of kpn_senml by KPN IoT

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 * support for binary sensors header
kpniot 0:a9259748d982 12 */
kpniot 0:a9259748d982 13
kpniot 0:a9259748d982 14 #ifndef SENMLBINARYRECORD
kpniot 0:a9259748d982 15 #define SENMLBINARYRECORD
kpniot 0:a9259748d982 16
kpniot 0:a9259748d982 17 #include <senml_record.h>
kpniot 0:a9259748d982 18
kpniot 0:a9259748d982 19 /**
kpniot 0:a9259748d982 20 * A SenMLRecord that stores binary data.
kpniot 0:a9259748d982 21 * This type of object can only be used for sensor data. If actuation is needed, use SenMLBinaryActuator
kpniot 0:a9259748d982 22 * instead.
kpniot 0:a9259748d982 23 */
kpniot 0:a9259748d982 24 class SenMLBinaryRecord: public SenMLRecord
kpniot 0:a9259748d982 25 {
kpniot 0:a9259748d982 26 public:
kpniot 0:a9259748d982 27 SenMLBinaryRecord(const char* name);
kpniot 0:a9259748d982 28 SenMLBinaryRecord(const char* name, SenMLUnit unit);
kpniot 0:a9259748d982 29 ~SenMLBinaryRecord(){ if(this->_value) free(this->_value); };
kpniot 0:a9259748d982 30
kpniot 0:a9259748d982 31
kpniot 0:a9259748d982 32 #ifdef __MBED__
kpniot 0:a9259748d982 33
kpniot 0:a9259748d982 34 /**
kpniot 0:a9259748d982 35 * set the value and length.
kpniot 0:a9259748d982 36 * warning: value is not copied over. a direct reference to the buffer is stored, so if this
kpniot 0:a9259748d982 37 * memory is changed/freed, then the data stored in the object will also be changed.
kpniot 0:a9259748d982 38 */
kpniot 0:a9259748d982 39 bool set(unsigned char* value, unsigned int length)
kpniot 0:a9259748d982 40 {
kpniot 0:a9259748d982 41 return this->set(value, length, NAN);
kpniot 0:a9259748d982 42 }
kpniot 0:a9259748d982 43
kpniot 0:a9259748d982 44 /**
kpniot 0:a9259748d982 45 * set the value and length.
kpniot 0:a9259748d982 46 * warning: value is not copied over. a direct reference to the buffer is stored, so if this
kpniot 0:a9259748d982 47 * memory is changed/freed, then the data stored in the object will also be changed.
kpniot 0:a9259748d982 48 */
kpniot 0:a9259748d982 49 bool set(unsigned char* value, unsigned int length, double time);
kpniot 0:a9259748d982 50
kpniot 0:a9259748d982 51 #else
kpniot 0:a9259748d982 52 /**
kpniot 0:a9259748d982 53 * set the value and length.
kpniot 0:a9259748d982 54 * warning: value is not copied over. a direct reference to the buffer is stored, so if this
kpniot 0:a9259748d982 55 * memory is changed/freed, then the data stored in the object will also be changed.
kpniot 0:a9259748d982 56 */
kpniot 0:a9259748d982 57 bool set(unsigned char* value, unsigned int length, double time = NAN);
kpniot 0:a9259748d982 58
kpniot 0:a9259748d982 59 #endif
kpniot 0:a9259748d982 60
kpniot 0:a9259748d982 61 /**
kpniot 0:a9259748d982 62 * renders all the fields to json, without the starting and ending brackets.
kpniot 0:a9259748d982 63 * Inheriters can extend this function if they want to add extra fields to the json output
kpniot 0:a9259748d982 64 * note: this is public so that custom implementations for the record object can use other objects
kpniot 0:a9259748d982 65 * internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for lat, lon & alt.
kpniot 0:a9259748d982 66 * @returns: None
kpniot 0:a9259748d982 67 */
kpniot 0:a9259748d982 68 void fieldsToJson();
kpniot 0:a9259748d982 69
kpniot 0:a9259748d982 70 /**
kpniot 0:a9259748d982 71 * renders all the fields to cbor format. renders all the fields of the object without the length info
kpniot 0:a9259748d982 72 * at the beginning
kpniot 0:a9259748d982 73 * note: this is public so that custom implementations for the record object can use other objects
kpniot 0:a9259748d982 74 * internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for
kpniot 0:a9259748d982 75 * lat, lon & alt.
kpniot 0:a9259748d982 76 * @returns: The number of bytes that were written.
kpniot 0:a9259748d982 77 */
kpniot 0:a9259748d982 78 virtual int fieldsToCbor();
kpniot 0:a9259748d982 79
kpniot 0:a9259748d982 80 protected:
kpniot 0:a9259748d982 81
kpniot 0:a9259748d982 82
kpniot 0:a9259748d982 83 private:
kpniot 0:a9259748d982 84 unsigned char* _value; //raw data buffer (not null terminated)
kpniot 0:a9259748d982 85 unsigned int _length; //size of the _data packet
kpniot 0:a9259748d982 86
kpniot 0:a9259748d982 87 };
kpniot 0:a9259748d982 88
kpniot 0:a9259748d982 89 #endif // SENMLBINARYRECORD
kpniot 0:a9259748d982 90
kpniot 0:a9259748d982 91
kpniot 0:a9259748d982 92
kpniot 0:a9259748d982 93
kpniot 0:a9259748d982 94
kpniot 0:a9259748d982 95
kpniot 0:a9259748d982 96