Loading...
Searching...
No Matches
senml.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Silke Hofstra
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
36#ifndef SENML_H
37#define SENML_H
38
39#include <stddef.h>
40#include <stdbool.h>
41#include <stdint.h>
42
43#include "modules.h"
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
52#ifndef CONFIG_SENML_ATTR_SUM
53#define CONFIG_SENML_ATTR_SUM 0
54#endif
55
59#ifndef CONFIG_SENML_ATTR_VERSION
60#define CONFIG_SENML_ATTR_VERSION 0
61#endif
62
66#ifndef CONFIG_SENML_ATTR_UPDATE_TIME
67#define CONFIG_SENML_ATTR_UPDATE_TIME 0
68#endif
69
78typedef enum {
79 /* SenML units from RFC8428 */
140 /* SenML units from RFC8798 */
150 /* SenML units from ISO7027-1:2016 */
153 /* SenML secondary units from RFC8798 */
188 /* SenML secondary units from CoRE-1 */
196
208
212typedef struct {
213 int32_t e;
214 int32_t m;
216
223typedef struct {
225 union {
226 uint64_t u;
227 int64_t i;
228 float f;
229 double d;
230 struct { int32_t e; int32_t m; } df ;
231 } value;
233
239typedef struct {
240 const char *base_name;
244#if IS_ACTIVE(CONFIG_SENML_ATTR_SUM) || defined(DOXYGEN)
246#endif
247#if IS_ACTIVE(CONFIG_SENML_ATTR_VERSION) || defined(DOXYGEN)
248 uint64_t base_version;
249#endif
250 const char *name;
252#if IS_ACTIVE(CONFIG_SENML_ATTR_SUM) || defined(DOXYGEN)
254#endif
256#if IS_ACTIVE(CONFIG_SENML_ATTR_UPDATE_TIME) || defined(DOXYGEN)
258#endif
260
268
272typedef struct {
274 const char *value;
275 size_t len;
277
281typedef struct {
283 bool value;
285
289typedef struct {
291 const uint8_t *value;
292 size_t len;
294
301static inline senml_numeric_t senml_float(float v)
302{
304 .value = { .f = v } };
305}
306
313static inline void senml_set_float(senml_numeric_t *n, float v)
314{
316 n->value.f = v;
317}
318
325static inline senml_numeric_t senml_double(double v)
326{
328 .value = { .d = v } };
329}
330
337static inline void senml_set_double(senml_numeric_t *n, double v)
338{
340 n->value.d = v;
341}
342
349static inline senml_numeric_t senml_int(int64_t v)
350{
352 .value = { .i = v } };
353}
354
361static inline void senml_set_int(senml_numeric_t *n, int64_t v)
362{
364 n->value.i = v;
365}
366
373static inline senml_numeric_t senml_uint(uint64_t v)
374{
376 .value = { .u = v } };
377}
378
385static inline void set_senml_uint(senml_numeric_t *n, uint64_t v)
386{
388 n->value.u = v;
389}
390
398static inline senml_numeric_t senml_decfrac(int32_t m, int32_t e)
399{
401 .value = { .df = { .e = e, .m = m } } };
402}
403
411static inline void senml_set_decfrac(senml_numeric_t *n, int32_t m, int32_t e)
412{
414 n->value.df.e = e;
415 n->value.df.m = m;
416}
417
424static inline senml_numeric_t senml_duration_s(int64_t s)
425{
426 return senml_int(s);
427}
428
435static inline void senml_set_duration_s(senml_numeric_t *n, int64_t s)
436{
437 senml_set_int(n, s);
438}
439
446static inline senml_numeric_t senml_duration_ms(int32_t ms)
447{
448 return senml_decfrac(ms, -3);
449}
450
458static inline void senml_set_duration_ms(senml_numeric_t *n, int32_t ms)
459{
460 senml_set_decfrac(n, ms, -3);
461}
462
469static inline senml_numeric_t senml_duration_us(int32_t us)
470{
471 return senml_decfrac(us, -6);
472}
473
481static inline void senml_set_duration_us(senml_numeric_t *n, int32_t us)
482{
483 senml_set_decfrac(n, us, -6);
484}
485
492static inline senml_numeric_t senml_duration_ns(int32_t ns)
493{
494 return senml_decfrac(ns, -9);
495}
496
503static inline void senml_set_duration_ns(senml_numeric_t *n, int32_t ns)
504{
505 senml_set_decfrac(n, ns, -9);
506}
507
520
521#ifdef __cplusplus
522}
523#endif
524
525#endif /* SENML_H */
static senml_numeric_t senml_duration_s(int64_t s)
Get an integer representation of a duration in seconds.
Definition senml.h:424
static senml_numeric_t senml_duration_ns(int32_t ns)
Get a senml_decfrac_t representation of a duration in nanoseconds.
Definition senml.h:492
static void senml_set_duration_ns(senml_numeric_t *n, int32_t ns)
Set a senml_decfrac_t representation of a duration in nanoseconds.
Definition senml.h:503
static void senml_set_duration_ms(senml_numeric_t *n, int32_t ms)
Set a senml_decfrac_t representation of a duration in milliseconds.
Definition senml.h:458
static senml_numeric_t senml_uint(uint64_t v)
Create an unsigned integer numeric value.
Definition senml.h:373
static senml_numeric_t senml_float(float v)
Create a floating point numeric value.
Definition senml.h:301
static void senml_set_float(senml_numeric_t *n, float v)
Set a floating point numeric value.
Definition senml.h:313
static void senml_set_decfrac(senml_numeric_t *n, int32_t m, int32_t e)
Set a decimal fraction numeric value in the form m*10^e.
Definition senml.h:411
static senml_numeric_t senml_decfrac(int32_t m, int32_t e)
Create a decimal fraction numeric value in the form m*10^e.
Definition senml.h:398
static senml_numeric_t senml_duration_us(int32_t us)
Get a senml_decfrac_t representation of a duration in microseconds.
Definition senml.h:469
static senml_numeric_t senml_duration_ms(int32_t ms)
Get a senml_decfrac_t representation of a duration in milliseconds.
Definition senml.h:446
static void senml_set_int(senml_numeric_t *n, int64_t v)
Set an integer numeric value.
Definition senml.h:361
static void set_senml_uint(senml_numeric_t *n, uint64_t v)
Set an unsigned integer numeric value.
Definition senml.h:385
static void senml_set_duration_us(senml_numeric_t *n, int32_t us)
Get a senml_decfrac_t representation of a duration in microseconds.
Definition senml.h:481
static senml_numeric_t senml_double(double v)
Create a double precision floating point numeric value.
Definition senml.h:325
senml_unit_t
SenML units and secondary units.
Definition senml.h:78
static senml_numeric_t senml_int(int64_t v)
Create an integer numeric value.
Definition senml.h:349
senml_value_type_t
SenML numeric value types.
Definition senml.h:201
const char * senml_unit_to_str(senml_unit_t unit)
Convert a SenML unit to a string.
static void senml_set_double(senml_numeric_t *n, double v)
Set a double precision floating point numeric value.
Definition senml.h:337
static void senml_set_duration_s(senml_numeric_t *n, int64_t s)
Set an integer representation of a duration in seconds.
Definition senml.h:435
@ SENML_UNIT_CENTIMETER
centimeter (cm, equivalent to 1/100 m)
Definition senml.h:184
@ SENML_UNIT_PERMILLE
permille (/1000, equivalent to 1/1000 '/')
Definition senml.h:181
@ SENML_UNIT_VOLT_AMPERE_HOUR
volt-ampere-hour (VAh, equivalent to 3600 VAs)
Definition senml.h:191
@ SENML_UNIT_KATAL
katal (kat)
Definition senml.h:110
@ SENML_UNIT_KILOMETER
kilometer (km, equivalent to 1000 m)
Definition senml.h:185
@ SENML_UNIT_GIGABYTE
gigabyte (GB, equivalent to 1e9 B)
Definition senml.h:169
@ SENML_UNIT_KILOVOLT_AMPERE_HOUR
kilovolt-ampere-hour (kVAh, equivalent to 3600000 VAs)
Definition senml.h:166
@ SENML_UNIT_VOLT_AMPERE
volt-ampere (Apparent Power) (VA)
Definition senml.h:142
@ SENML_UNIT_HERTZ
hertz (Hz)
Definition senml.h:89
@ SENML_UNIT_PERCENT
percent (/100, equivalent to 1/100 '/')
Definition senml.h:180
@ SENML_UNIT_MEGAHERTZ
megahertz (MHz, equivalent to 1000000 Hz)
Definition senml.h:157
@ SENML_UNIT_JOULE
joule (J)
Definition senml.h:94
@ SENML_UNIT_BYTE
Byte (information content) (B)
Definition senml.h:141
@ SENML_UNIT_MILLIVOLT
millivolt (mV, equivalent to 1/1000 V)
Definition senml.h:173
@ SENML_UNIT_VOLT_AMPERE_SECOND
volt-ampere second (Apparent Energy) (VAs)
Definition senml.h:143
@ SENML_UNIT_KILOWATT_HOUR
kilowatt-hour (kWh, equivalent to 3600000 J)
Definition senml.h:163
@ SENML_UNIT_KILOMETER_PER_HOUR
kilometer per hour (km/h, equivalent to 1/3.6 m/s)
Definition senml.h:186
@ SENML_UNIT_METER_PER_SECOND
meter per second (velocity) (m/s)
Definition senml.h:114
@ SENML_UNIT_HECTOPASCAL
hectopascal (hPa, equivalent to 100 Pa)
Definition senml.h:182
@ SENML_UNIT_PARTS_PER_BILLION
parts per billion (ppb, equivalent to 1e-9 '/')
Definition senml.h:189
@ SENML_UNIT_PARTS_PER_MILLION
parts per million (ppm, equivalent to 1e-6 '/')
Definition senml.h:179
@ SENML_UNIT_BECQUEREL
becquerel (Bq)
Definition senml.h:107
@ SENML_UNIT_KILOVOLT_AMPERE
kilovolt-ampere (kVA, equivalent to 1000 VA)
Definition senml.h:159
@ SENML_UNIT_WATT_HOUR
watt-hour (Wh, equivalent to 3600 J)
Definition senml.h:162
@ SENML_UNIT_KILOWATT
kilowatt (kW, equivalent to 1000 W)
Definition senml.h:158
@ SENML_UNIT_CUBIC_METER_PER_SECOND
cubic meter per second (flow rate) (m3/s)
Definition senml.h:116
@ SENML_UNIT_TESLA
tesla (T)
Definition senml.h:102
@ SENML_UNIT_RATE
1 per second (event rate) (1/s)
Definition senml.h:134
@ SENML_UNIT_MILLIMETER
millimeter (mm, equivalent to 1/1000 m)
Definition senml.h:183
@ SENML_UNIT_BYTE_PER_SECOND
byte per second (B/s, equivalent to 8 bit/s)
Definition senml.h:171
@ SENML_UNIT_MOLE
mole (mol)
Definition senml.h:88
@ SENML_UNIT_AMPERE_HOUR
ampere-hour (Ah, equivalent to 3600 C)
Definition senml.h:161
@ SENML_UNIT_HENRY
henry (H)
Definition senml.h:103
@ SENML_UNIT_CELSIUS
degrees Celsius (Cel)
Definition senml.h:104
@ SENML_UNIT_REMAINING_BATTERY_SECONDS
seconds (remaining battery energy level) (EL)
Definition senml.h:133
@ SENML_UNIT_MILLIGRAM_PER_LITER
milligram per liter (mg/l, equivalent to 1/1000 kg/m3)
Definition senml.h:192
@ SENML_UNIT_DBW
decibel relative to 1 W (power level) (dBW)
Definition senml.h:126
@ SENML_UNIT_AMPERE
ampere (A)
Definition senml.h:85
@ SENML_UNIT_MILLIAMPERE
milliampere (mA, equivalent to 1/1000 A)
Definition senml.h:174
@ SENML_UNIT_RELATIVE_HUMIDITY_PERCENT
Percentage (Relative Humidity) (RH)
Definition senml.h:131
@ SENML_UNIT_SIEMENS_PER_METER
Siemens per meter (conductivity) (S/m)
Definition senml.h:138
@ SENML_UNIT_BEL
bel (sound pressure level; logarithmic quantity) (Bspl)
Definition senml.h:127
@ SENML_UNIT_CANDELA_PER_SQUARE_METER
candela per square meter (luminance) (cd/m2)
Definition senml.h:119
@ SENML_UNIT_SIEVERT
sievert (Sv)
Definition senml.h:109
@ SENML_UNIT_KILOGRAM
kilogram (kg)
Definition senml.h:82
@ SENML_UNIT_GRAM
gram (g)
Definition senml.h:83
@ SENML_UNIT_KILOGRAM_PER_CUBIC_METER
kilogram per cubic meter (mass density, mass concentration) (kg/m3)
Definition senml.h:147
@ SENML_UNIT_JOULE_PER_METER
joule per meter (Energy per distance) (J/m)
Definition senml.h:146
@ SENML_UNIT_METER_PER_SQUARE_SECOND
meter per square second (acceleration) (m/s2)
Definition senml.h:115
@ SENML_UNIT_MINUTE
minute (min, equivalent to 60 s)
Definition senml.h:155
@ SENML_UNIT_VOLT_AMPERE_REACTIVE
volt-ampere reactive (Reactive Power) (var)
Definition senml.h:144
@ SENML_UNIT_VOLT_AMPERE_REACTIVE_SECOND
volt-ampere-reactive second (Reactive Energy) (vars)
Definition senml.h:145
@ SENML_UNIT_RATIO_2
1 (ratio e.g., value of a switch) (%)
Definition senml.h:130
@ SENML_UNIT_BIT_PER_SECOND
bit per second (data rate) (bit/s)
Definition senml.h:121
@ SENML_UNIT_WEBER
weber (Wb)
Definition senml.h:101
@ SENML_UNIT_RATIO
1 (ratio e.g., value of a switch) (/)
Definition senml.h:129
@ SENML_UNIT_LONGITUDE
degrees longitude (lon)
Definition senml.h:123
@ SENML_UNIT_MICROGRAM_PER_LITER
microgram per liter (ug/l, equivalent to 1e-6 kg/m3)
Definition senml.h:193
@ SENML_UNIT_MILLISECOND
millisecond (ms, equivalent to 1/1000 s)
Definition senml.h:154
@ SENML_UNIT_VOLT
volt (V)
Definition senml.h:97
@ SENML_UNIT_WATT
watt (W)
Definition senml.h:95
@ SENML_UNIT_DECIBEL_MILLIWATT
decibel (milliwatt) (dBm, equivalent to -29 dBW)
Definition senml.h:175
@ SENML_UNIT_KILOVAR
kilovar (kvar, equivalent to 1000 var)
Definition senml.h:160
@ SENML_UNIT_PH
pH value (acidity; logarithmic quantity) (pH)
Definition senml.h:124
@ SENML_UNIT_REMAINING_BATTERY_PERCENT
Percentage (remaining battery energy level) (EL)
Definition senml.h:132
@ SENML_UNIT_COULOMB
coulomb (C)
Definition senml.h:96
@ SENML_UNIT_NEPHELOMETRIC_TURBIDITY_UNIT
Nephelometric Turbidity Unit (NTU)
Definition senml.h:151
@ SENML_UNIT_KILOVAR_HOUR
kilovar-hour (kvarh, equivalent to 3600000 vars)
Definition senml.h:165
@ SENML_UNIT_NONE
No unit specified.
Definition senml.h:80
@ SENML_UNIT_BEAT_PER_MINUTE
1 per minute (heart rate in beats per minute) (beat/min))
Definition senml.h:136
@ SENML_UNIT_COUNT
1 (counter value) (count)
Definition senml.h:128
@ SENML_UNIT_KIBIBYTE
kibibyte (KiB, equivalent to 1024 B)
Definition senml.h:168
@ SENML_UNIT_CANDELA
candela (cd)
Definition senml.h:87
@ SENML_UNIT_METER_PER_HOUR
meter per hour (m/h, equivalent to 1/3600 m/s)
Definition senml.h:178
@ SENML_UNIT_LUX
lux (lx)
Definition senml.h:106
@ SENML_UNIT_OHM
ohm (Ohm)
Definition senml.h:99
@ SENML_UNIT_CUBIC_METER
cubic meter (volume) (m3)
Definition senml.h:112
@ SENML_UNIT_WATT_HOUR_PER_KILOMETER
watt-hour per kilometer (Wh/km, equivalent to 3.6 J/m)
Definition senml.h:167
@ SENML_UNIT_GRAM_PER_LITER
gram per liter (g/l, equivalent to 1 kg/m3)
Definition senml.h:194
@ SENML_UNIT_RADIAN
radian (rad)
Definition senml.h:90
@ SENML_UNIT_KELVIN
kelvin (K)
Definition senml.h:86
@ SENML_UNIT_LATITUDE
degrees latitude (lat)
Definition senml.h:122
@ SENML_UNIT_HOUR
hour (h, equivalent to 3600 s)
Definition senml.h:156
@ SENML_UNIT_MEGABYTE_PER_SECOND
megabyte per second (MB/s, equivalent to 8000000 bit/s)
Definition senml.h:172
@ SENML_UNIT_MICROGRAM_PER_CUBIC_METER
microgram per cubic meter (ug/m3, equivalent to 1e-9 kg/m3)
Definition senml.h:176
@ SENML_UNIT_SQUARE_METER
square meter (area) (m2)
Definition senml.h:111
@ SENML_UNIT_RPM
1 per minute (event rate, "rpm") (1/min)
Definition senml.h:135
@ SENML_UNIT_SECOND
second (s)
Definition senml.h:84
@ SENML_UNIT_WATT_PER_SQUARE_METER
watt per square meter (irradiance) (W/m2)
Definition senml.h:118
@ SENML_UNIT_LITER
liter (volume) (l)
Definition senml.h:113
@ SENML_UNIT_MILLIMETER_PER_HOUR
millimeter per hour (mm/h, equivalent to 1/3600000 m/s)
Definition senml.h:177
@ SENML_UNIT_FARAD
farad (F)
Definition senml.h:98
@ SENML_UNIT_DEGREE
degree (angle) (deg)
Definition senml.h:148
@ SENML_UNIT_VAR_HOUR
var-hour (varh, equivalent to 3600 vars)
Definition senml.h:164
@ SENML_UNIT_DECIBEL
decibel (logarithmic quantity) (dB)
Definition senml.h:125
@ SENML_UNIT_PARTS_PER_TRILLION
parts per trillion (ppt, equivalent to 1e-12 '/')
Definition senml.h:190
@ SENML_UNIT_SIEMENS
siemens (S)
Definition senml.h:100
@ SENML_UNIT_LUMEN
lumen (lm)
Definition senml.h:105
@ SENML_UNIT_STERADIAN
steradian (sr)
Definition senml.h:91
@ SENML_UNIT_NEWTON
newton (N)
Definition senml.h:92
@ SENML_UNIT_PASCAL
pascal (Pa)
Definition senml.h:93
@ SENML_UNIT_BEATS
1 (Cumulative number of heart beats) (beats)
Definition senml.h:137
@ SENML_UNIT_GRAY
gray (Gy)
Definition senml.h:108
@ SENML_UNIT_BIT
bit (information content) (bit)
Definition senml.h:120
@ SENML_UNIT_LITER_PER_SECOND
liter per second (flow rate) (l/s)
Definition senml.h:117
@ SENML_UNIT_MEGABIT_PER_SECOND
megabit per second (Mbit/s, equivalent to 1000000 bit/s)
Definition senml.h:170
@ SENML_UNIT_METER
meter (m)
Definition senml.h:81
@ SENML_TYPE_NUMERIC_DOUBLE
Double-precision floating point number.
Definition senml.h:205
@ SENML_TYPE_NUMERIC_UINT
Unsigned integer.
Definition senml.h:202
@ SENML_TYPE_NUMERIC_FLOAT
Floating point number.
Definition senml.h:204
@ SENML_TYPE_NUMERIC_DECFRAC
Decimal fraction.
Definition senml.h:206
@ SENML_TYPE_NUMERIC_INT
Integer.
Definition senml.h:203
Common macros and compiler attributes/pragmas configuration.
SenML common record attributes.
Definition senml.h:239
senml_numeric_t update_time
Maximum time before the next sensor value, set CONFIG_SENML_ATTR_UPDATE_TIME to 1 to enable.
Definition senml.h:257
senml_numeric_t time
Time of the measurement (relative or Unix) in seconds.
Definition senml.h:255
senml_numeric_t base_time
Base Time.
Definition senml.h:241
senml_numeric_t base_value
Base Value.
Definition senml.h:243
senml_numeric_t sum
Sum, set CONFIG_SENML_ATTR_SUM to 1 to enable.
Definition senml.h:253
const char * name
Name of the measurement.
Definition senml.h:250
senml_unit_t unit
Unit.
Definition senml.h:251
senml_unit_t base_unit
Base Unit.
Definition senml.h:242
const char * base_name
Base Name.
Definition senml.h:240
uint64_t base_version
Base Version, set CONFIG_SENML_ATTR_VERSION to 1 to enable.
Definition senml.h:248
senml_numeric_t base_sum
Base Sum, set CONFIG_SENML_ATTR_SUM to 1 to enable.
Definition senml.h:245
SenML boolean value.
Definition senml.h:281
bool value
Value.
Definition senml.h:283
senml_attr_t attr
SenML attributes.
Definition senml.h:282
SenML data value.
Definition senml.h:289
const uint8_t * value
Value.
Definition senml.h:291
size_t len
Value length.
Definition senml.h:292
senml_attr_t attr
SenML attributes.
Definition senml.h:290
Decimal fraction containing a value in the form of m * 10^e.
Definition senml.h:212
int32_t m
Mantissa.
Definition senml.h:214
int32_t e
Exponent.
Definition senml.h:213
SenML numeric value.
Definition senml.h:223
senml_value_type_t type
Type of the value.
Definition senml.h:224
struct senml_numeric_t::@390::@391 df
Decimal fraction.
union senml_numeric_t::@390 value
Value data.
SenML string value.
Definition senml.h:272
senml_attr_t attr
SenML attributes.
Definition senml.h:273
const char * value
Value.
Definition senml.h:274
size_t len
Value length.
Definition senml.h:275
SenML string value.
Definition senml.h:264
senml_attr_t attr
SenML attributes.
Definition senml.h:265
senml_numeric_t value
Value.
Definition senml.h:266