KPN Things Device SDK
SenMLPack Class Reference

#include <senml_pack.h>

Inheritance diagram for SenMLPack:
SenMLBasePack SenMLBase SenMLPackTemplate< double > SenMLPackTemplate< int > SenMLPackTemplate< T > SenMLDoublePack SenMLIntPack

Public Member Functions

 SenMLPack ()
 
 SenMLPack (const char *baseName)
 
 SenMLPack (const char *baseName, SenMLUnit baseUnit)
 
 SenMLPack (const char *baseName, SenMLUnit baseUnit, double baseTime)
 
 SenMLPack (PACK_ACTUATOR_SIGNATURE)
 
 SenMLPack (const char *baseName, PACK_ACTUATOR_SIGNATURE)
 
 SenMLPack (const char *baseName, SenMLUnit baseUnit, PACK_ACTUATOR_SIGNATURE)
 
 SenMLPack (const char *baseName, SenMLUnit baseUnit, double baseTime, PACK_ACTUATOR_SIGNATURE)
 
 ~SenMLPack ()
 
void setBaseName (const char *name)
 
const char * getBaseName ()
 
void setBaseUnit (SenMLUnit unit)
 
SenMLUnit getBaseUnit ()
 
void setBaseTime (double time)
 
void setBaseTimeSilently (double time)
 
double getBaseTime ()
 
virtual int fieldsToJson ()
 
virtual int fieldsToCbor ()
 
- Public Member Functions inherited from SenMLBasePack
 SenMLBasePack ()
 
 ~SenMLBasePack ()
 
int toJson (Stream &dest, SenMLStreamMethod format=SENML_RAW)
 
int toJson (char *dest, int length, SenMLStreamMethod format=SENML_RAW)
 
int toCbor (Stream &dest, SenMLStreamMethod format=SENML_RAW)
 
int toCbor (char *dest, int length, SenMLStreamMethod format=SENML_RAW)
 
void fromJson (Stream &source, SenMLStreamMethod format=SENML_RAW)
 
void fromJson (const char *source)
 
void fromCbor (Stream &source, SenMLStreamMethod format=SENML_RAW)
 
void fromCbor (char *source, int length, SenMLStreamMethod format)
 
bool add (SenMLBase &item)
 
bool clear ()
 
SenMLBasegetFirst ()
 
virtual int getCount ()
 
virtual int fieldsToJson ()=0
 
virtual int fieldsToCbor ()=0
 
virtual const char * getBaseName ()
 
virtual SenMLUnit getBaseUnit ()
 
- Public Member Functions inherited from SenMLBase
 SenMLBase ()
 
 ~SenMLBase ()
 
SenMLBasegetNext ()
 
SenMLBasegetRoot ()
 
virtual void adjustToBaseTime (double prev, double time)
 
virtual int fieldsToJson ()=0
 
virtual int fieldsToCbor ()=0
 

Protected Member Functions

bool isActuator ()
 
void actuate (const char *pack, const char *record, const void *value, int valueLength, SenMLDataType dataType)
 
virtual int getFieldLength ()
 
- Protected Member Functions inherited from SenMLBasePack
virtual bool isPack ()
 
virtual bool isActuator ()
 
void setLast (SenMLBase &value)
 
virtual int contentToCbor ()
 
virtual int contentToJson ()
 
virtual int getArrayLength ()
 
virtual int getFieldLength ()
 
virtual void setupStreamCtx (char *dest, int length, SenMLStreamMethod format)
 
virtual void setupStreamCtx (Stream *dest, SenMLStreamMethod format)
 
- Protected Member Functions inherited from SenMLBase
virtual int contentToJson ()=0
 
void setNext (SenMLBase *value)
 
void setPrev (SenMLBase *value)
 
SenMLBasegetPrev ()
 
virtual bool isPack ()
 
virtual int contentToCbor ()=0
 
virtual int getArrayLength ()
 
virtual int getFieldLength ()=0
 

Friends

class SenMLJsonListener
 
class SenMLCborParser
 

Detailed Description

SenMLPack represents a single senml document that can be sent or received.

A senmlPack object has SenMLRecords and/or other SenMLPack objects as children. These represent the data that the object contains. A SenMLRecord represents a single value, while child SenMLPacks represent data of other devices. When a SenMLPack contains other SenMLPack objects, the root object represents a gateway.

A SenMLPack object is able to render and parse the data to/from a json string or to/from binary CBOR data. Both rendering and parsing can be done either directly from a stream (like an UART connected to a modem), or from a memory buffer. Rendering to and parsing from a stream is useful for devices that have extreme low memory available. Almost no buffers are used in this case. Error handling is limited in this case though. The parser and generator are able to render in the native format (strings for json, binary data for cbor) and as a hex string. This is especially useful when directly working on a stream: some modems (ex lora) accept instructions with data in HEX format.

example:

#include <thingsml.h>
void setup(){
doc.add(temperature);
Serial.begin(57600);
senMLSetLogger(Serial);
delay(1000);
Serial.println("start");
}
void loop(){
int val = 10; //just give it some value
doc.toJson(Serial); //as text
Serial.println();
doc.toJson(Serial, SENML_HEX); //in hex format (often used in communication with lora modems)
Serial.println();
delay(1000);
}
bool add(SenMLBase &item)
Definition: senml_basepack.cpp:33
int toJson(Stream &dest, SenMLStreamMethod format=SENML_RAW)
Definition: senml_basepack.cpp:137
Definition: senml_double_record.h:26
Definition: senml_pack.h:68
@ THINGSML_TEMPERATURE
Definition: senml_enums.h:53
@ SENML_HEX
Definition: senml_enums.h:49
void senMLSetLogger(Stream &logger)
Definition: senml_logging.cpp:18

Constructor & Destructor Documentation

◆ SenMLPack() [1/8]

SenMLPack::SenMLPack ( )
inline

create a SenMLPack object.

◆ SenMLPack() [2/8]

SenMLPack::SenMLPack ( const char *  baseName)
inline

create a SenMLPack object.

Parameters
baseNamethe string that will be prepended to all records in this pack. Is used to represent the name of the device.

◆ SenMLPack() [3/8]

SenMLPack::SenMLPack ( const char *  baseName,
SenMLUnit  baseUnit 
)
inline

create a SenMLPack object.

Parameters
baseNamethe string that will be prepended to all records in this pack. Is used to represent the name of the device.
baseUnitthe unit name that will be used by default if the record doesnt not define one.

◆ SenMLPack() [4/8]

SenMLPack::SenMLPack ( const char *  baseName,
SenMLUnit  baseUnit,
double  baseTime 
)
inline

create a SenMLPack object.

Parameters
baseNamethe string that will be prepended to all records in this pack. Is used to represent the name of the device.
baseUnitthe unit name that will be used by default if the record doesnt not define one.
baseTimethe time that will be added to each record. When specified, each record that does not specify a time, will receive this time. When the record does have a time, the baseTime of the pack is added to it, so the time of the record becomes relative to that of the pack.

◆ SenMLPack() [5/8]

SenMLPack::SenMLPack ( PACK_ACTUATOR_SIGNATURE  )
inline

create a SenMLPack object.

Parameters
callbacka function that will be called while parsing incomming data, when no record can be found that matches any of the defined ones in the object. The parameters of the callback must be: const char* packName the name of the pack that the record belongs to. The data is for a child SenMLPack when this field is different then the name of the root pack. const char* recordName the name of the record const void* value a pointer to the memory blob that contains the actual value. int size the size of the memory blobl SenMLDataType dataType: defines how to interprete the memory blob (ex: pointer to integer,..)

◆ SenMLPack() [6/8]

SenMLPack::SenMLPack ( const char *  baseName,
PACK_ACTUATOR_SIGNATURE   
)
inline

create a SenMLPack object.

Parameters
baseNamethe string that will be prepended to all records in this pack. Is used to represent the name of the device.
callbacka function that will be called while parsing incomming data, when no record can be found that matches any of the defined ones in the object. The parameters of the callback must be: const char* packName the name of the pack that the record belongs to. The data is for a child SenMLPack when this field is different then the name of the root pack. const char* recordName the name of the record const void* value a pointer to the memory blob that contains the actual value. int size the size of the memory blobl SenMLDataType dataType: defines how to interprete the memory blob (ex: pointer to integer,..)

◆ SenMLPack() [7/8]

SenMLPack::SenMLPack ( const char *  baseName,
SenMLUnit  baseUnit,
PACK_ACTUATOR_SIGNATURE   
)
inline

create a SenMLPack object.

Parameters
baseNamethe string that will be prepended to all records in this pack. Is used to represent the name of the device.
baseUnitthe unit name that will be used by default if the record doesnt not define one.
callbacka function that will be called while parsing incomming data, when no record can be found that matches any of the defined ones in the object. The parameters of the callback must be: const char* packName the name of the pack that the record belongs to. The data is for a child SenMLPack when this field is different then the name of the root pack. const char* recordName the name of the record const void* value a pointer to the memory blob that contains the actual value. int size the size of the memory blobl SenMLDataType dataType: defines how to interprete the memory blob (ex: pointer to integer,..)

◆ SenMLPack() [8/8]

SenMLPack::SenMLPack ( const char *  baseName,
SenMLUnit  baseUnit,
double  baseTime,
PACK_ACTUATOR_SIGNATURE   
)
inline

create a SenMLPack object.

Parameters
baseNamethe string that will be prepended to all records in this pack. Is used to represent the name of the device.
baseUnitthe unit name that will be used by default if the record doesnt not define one.
baseTimethe time that will be added to each record. When specified, each record that does not specify a time, will receive this time. When the record does have a time, the baseTime of the pack is added to it, so the time of the record becomes relative to that of the pack.
callbacka function that will be called while parsing incomming data, when no record can be found that matches any of the defined ones in the object. The parameters of the callback must be: const char* packName the name of the pack that the record belongs to. The data is for a child SenMLPack when this field is different then the name of the root pack. const char* recordName the name of the record const void* value a pointer to the memory blob that contains the actual value. int size the size of the memory blobl SenMLDataType dataType: defines how to interprete the memory blob (ex: pointer to integer,..)

◆ ~SenMLPack()

SenMLPack::~SenMLPack ( )
inline

destroys the SenMLPack object.

Member Function Documentation

◆ actuate()

void SenMLPack::actuate ( const char *  pack,
const char *  record,
const void *  value,
int  valueLength,
SenMLDataType  dataType 
)
inlineprotected

◆ fieldsToCbor()

int SenMLPack::fieldsToCbor ( )
virtual

renders all the fields to cbor format. renders all the fields of the object without the length info at the beginning note: this is public so that custom implementations for the record object can use other objects internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for lat, lon & alt.

Returns
: The number of bytes that were written.

Implements SenMLBasePack.

Reimplemented in SenMLDoublePack, and SenMLIntPack.

◆ fieldsToJson()

int SenMLPack::fieldsToJson ( )
virtual

renders all the fields to json, without the starting and ending brackets. Inheriters can extend this function if they want to add extra fields to the json output note: this is public so that custom implementations for the record object can use other objects internally and render to json using this function (ex: coordinatesRecord using 3 floatRecrods for lat, lon & alt.

Returns
: None

Implements SenMLBasePack.

Reimplemented in SenMLDoublePack, and SenMLIntPack.

◆ getBaseName()

const char * SenMLPack::getBaseName ( )
virtual

Get the base name. see the spec on base fields for more info.

Returns
the name of the device as an immutable string.

Reimplemented from SenMLBasePack.

◆ getBaseTime()

double SenMLPack::getBaseTime ( )
inline

Get the base time. see the spec on base fields for more info.

Returns
a double value that is used as the default unit for records that don't define a unit of their own. if no base time is set, NaN will be returned.

◆ getBaseUnit()

SenMLUnit SenMLPack::getBaseUnit ( )
inlinevirtual

Get the base unit. see the spec on base fields for more info.

Returns
a SenMLUnit enum value that is used as the default unit for records that don't define a unit of their own.

Reimplemented from SenMLBasePack.

◆ getFieldLength()

int SenMLPack::getFieldLength ( )
protectedvirtual

Reimplemented from SenMLBasePack.

◆ isActuator()

bool SenMLPack::isActuator ( )
inlineprotectedvirtual

Reimplemented from SenMLBasePack.

◆ setBaseName()

void SenMLPack::setBaseName ( const char *  name)

assign a basename to the SenMLPack object. This represents the name of the device. see the spec on base fields for more info. Every SenMLPack object must have a basename. This field will always be rendered in the output, even if the string is empty.

Parameters
namean immutable string that will be used to represent the name of the device. An internal copy of the value will be made.
Returns
none

◆ setBaseTime()

void SenMLPack::setBaseTime ( double  time)

Set the base time. see the spec on base fields for more info.

Parameters
timethe value to use as base time. set bt to NaN if the field should not be included in the output.
Returns
none

◆ setBaseTimeSilently()

void SenMLPack::setBaseTimeSilently ( double  time)

Set the base time without adjusting all it's records. see the spec on basefields for more info.

Parameters
timethe value to use as base time. set bt to NaN if the field should not be included in the output.
Returns
none

◆ setBaseUnit()

void SenMLPack::setBaseUnit ( SenMLUnit  unit)

Set the base unit that will be used as the default unit for all records that don't define their own unit. see the spec on base fields for more info. Set to SENML_UNIT_NONE for ommiting the base unit from the output (default).

Parameters
unitthe unit to use as default. See SenMLUnit for all supported unit names.
Returns
none

Friends And Related Function Documentation

◆ SenMLCborParser

friend class SenMLCborParser
friend

◆ SenMLJsonListener

friend class SenMLJsonListener
friend

The documentation for this class was generated from the following files: