senml.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2021 Silke Hofstra
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
35 #include <stddef.h>
36 #include <stdbool.h>
37 #include <stdint.h>
38 
39 #include "modules.h"
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
48 #ifndef CONFIG_SENML_ATTR_SUM
49 #define CONFIG_SENML_ATTR_SUM 0
50 #endif
51 
55 #ifndef CONFIG_SENML_ATTR_VERSION
56 #define CONFIG_SENML_ATTR_VERSION 0
57 #endif
58 
62 #ifndef CONFIG_SENML_ATTR_UPDATE_TIME
63 #define CONFIG_SENML_ATTR_UPDATE_TIME 0
64 #endif
65 
74 typedef enum {
75  /* SenML units from RFC8428 */
136  /* SenML units from RFC8798 */
146  /* SenML units from ISO7027-1:2016 */
149  /* SenML secondary units from RFC8798 */
184  /* SenML secondary units from CoRE-1 */
191 } senml_unit_t;
192 
197 typedef enum {
204 
208 typedef struct {
209  int32_t e;
210  int32_t m;
212 
219 typedef struct {
221  union {
222  uint64_t u;
223  int64_t i;
224  float f;
225  double d;
226  struct { int32_t e; int32_t m; } df ;
227  } value;
229 
235 typedef struct {
236  const char *base_name;
240 #if IS_ACTIVE(CONFIG_SENML_ATTR_SUM) || defined(DOXYGEN)
242 #endif
243 #if IS_ACTIVE(CONFIG_SENML_ATTR_VERSION) || defined(DOXYGEN)
244  uint64_t base_version;
245 #endif
246  const char *name;
248 #if IS_ACTIVE(CONFIG_SENML_ATTR_SUM) || defined(DOXYGEN)
250 #endif
252 #if IS_ACTIVE(CONFIG_SENML_ATTR_UPDATE_TIME) || defined(DOXYGEN)
254 #endif
255 } senml_attr_t;
256 
260 typedef struct {
263 } senml_value_t;
264 
268 typedef struct {
270  const char *value;
271  size_t len;
273 
277 typedef struct {
279  bool value;
281 
285 typedef struct {
287  const uint8_t *value;
288  size_t len;
290 
297 static inline senml_numeric_t senml_float(float v)
298 {
300  .value = { .f = v } };
301 }
302 
309 static inline void senml_set_float(senml_numeric_t *n, float v)
310 {
312  n->value.f = v;
313 }
314 
321 static inline senml_numeric_t senml_double(double v)
322 {
324  .value = { .d = v } };
325 }
326 
333 static inline void senml_set_double(senml_numeric_t *n, double v)
334 {
336  n->value.d = v;
337 }
338 
345 static inline senml_numeric_t senml_int(int64_t v)
346 {
348  .value = { .i = v } };
349 }
350 
357 static inline void senml_set_int(senml_numeric_t *n, int64_t v)
358 {
360  n->value.i = v;
361 }
362 
369 static inline senml_numeric_t senml_uint(uint64_t v)
370 {
372  .value = { .u = v } };
373 }
374 
381 static inline void set_senml_uint(senml_numeric_t *n, uint64_t v)
382 {
384  n->value.u = v;
385 }
386 
394 static inline senml_numeric_t senml_decfrac(int32_t m, int32_t e)
395 {
397  .value = { .df = { .e = e, .m = m } } };
398 }
399 
407 static inline void senml_set_decfrac(senml_numeric_t *n, int32_t m, int32_t e)
408 {
410  n->value.df.e = e;
411  n->value.df.m = m;
412 }
413 
420 static inline senml_numeric_t senml_duration_s(int64_t s)
421 {
422  return senml_int(s);
423 }
424 
431 static inline void senml_set_duration_s(senml_numeric_t *n, int64_t s)
432 {
433  senml_set_int(n, s);
434 }
435 
442 static inline senml_numeric_t senml_duration_ms(int32_t ms)
443 {
444  return senml_decfrac(ms, -3);
445 }
446 
453 static inline void senml_set_duration_ms(senml_numeric_t *n, int32_t ms)
454 {
455  senml_set_decfrac(n, ms, -3);
456 }
457 
464 static inline senml_numeric_t senml_duration_us(int32_t us)
465 {
466  return senml_decfrac(us, -6);
467 }
468 
475 static inline void senml_set_duration_us(senml_numeric_t *n, int32_t us)
476 {
477  senml_set_decfrac(n, us, -6);
478 }
479 
486 static inline senml_numeric_t senml_duration_ns(int32_t ns)
487 {
488  return senml_decfrac(ns, -9);
489 }
490 
497 static inline void senml_set_duration_ns(senml_numeric_t *n, int32_t ns)
498 {
499  senml_set_decfrac(n, ns, -9);
500 }
501 
513 const char *senml_unit_to_str(senml_unit_t unit);
514 
515 #ifdef __cplusplus
516 }
517 #endif
518 
static senml_numeric_t senml_duration_s(int64_t s)
Get an integer representation of a duration in seconds.
Definition: senml.h:420
const char * senml_unit_to_str(senml_unit_t unit)
Convert a SenML unit to a string.
static senml_numeric_t senml_duration_ns(int32_t ns)
Get a senml_decfrac_t representation of a duration in nanoseconds.
Definition: senml.h:486
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:497
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:453
static senml_numeric_t senml_uint(uint64_t v)
Create an unsigned integer numeric value.
Definition: senml.h:369
static senml_numeric_t senml_float(float v)
Create a floating point numeric value.
Definition: senml.h:297
static void senml_set_float(senml_numeric_t *n, float v)
Set a floating point numeric value.
Definition: senml.h:309
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:407
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:394
static senml_numeric_t senml_duration_us(int32_t us)
Get a senml_decfrac_t representation of a duration in microseconds.
Definition: senml.h:464
static senml_numeric_t senml_duration_ms(int32_t ms)
Get a senml_decfrac_t representation of a duration in milliseconds.
Definition: senml.h:442
static void senml_set_int(senml_numeric_t *n, int64_t v)
Set an integer numeric value.
Definition: senml.h:357
static void set_senml_uint(senml_numeric_t *n, uint64_t v)
Set an unsigned integer numeric value.
Definition: senml.h:381
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:475
static senml_numeric_t senml_double(double v)
Create a double precision floating point numeric value.
Definition: senml.h:321
senml_unit_t
SenML units and secondary units.
Definition: senml.h:74
static senml_numeric_t senml_int(int64_t v)
Create an integer numeric value.
Definition: senml.h:345
senml_value_type_t
SenML numeric value types.
Definition: senml.h:197
static void senml_set_double(senml_numeric_t *n, double v)
Set a double precision floating point numeric value.
Definition: senml.h:333
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:431
@ SENML_UNIT_CENTIMETER
centimeter (cm, equivalent to 1/100 m)
Definition: senml.h:180
@ SENML_UNIT_PERMILLE
permille (/1000, equivalent to 1/1000 '/')
Definition: senml.h:177
@ SENML_UNIT_VOLT_AMPERE_HOUR
volt-ampere-hour (VAh, equivalent to 3600 VAs)
Definition: senml.h:187
@ SENML_UNIT_KATAL
katal (kat)
Definition: senml.h:106
@ SENML_UNIT_KILOMETER
kilometer (km, equivalent to 1000 m)
Definition: senml.h:181
@ SENML_UNIT_GIGABYTE
gigabyte (GB, equivalent to 1e9 B)
Definition: senml.h:165
@ SENML_UNIT_KILOVOLT_AMPERE_HOUR
kilovolt-ampere-hour (kVAh, equivalent to 3600000 VAs)
Definition: senml.h:162
@ SENML_UNIT_VOLT_AMPERE
volt-ampere (Apparent Power) (VA)
Definition: senml.h:138
@ SENML_UNIT_HERTZ
hertz (Hz)
Definition: senml.h:85
@ SENML_UNIT_PERCENT
percent (/100, equivalent to 1/100 '/')
Definition: senml.h:176
@ SENML_UNIT_MEGAHERTZ
megahertz (MHz, equivalent to 1000000 Hz)
Definition: senml.h:153
@ SENML_UNIT_JOULE
joule (J)
Definition: senml.h:90
@ SENML_UNIT_BYTE
Byte (information content) (B)
Definition: senml.h:137
@ SENML_UNIT_MILLIVOLT
millivolt (mV, equivalent to 1/1000 V)
Definition: senml.h:169
@ SENML_UNIT_VOLT_AMPERE_SECOND
volt-ampere second (Apparent Energy) (VAs)
Definition: senml.h:139
@ SENML_UNIT_KILOWATT_HOUR
kilowatt-hour (kWh, equivalent to 3600000 J)
Definition: senml.h:159
@ SENML_UNIT_KILOMETER_PER_HOUR
kilometer per hour (km/h, equivalent to 1/3.6 m/s)
Definition: senml.h:182
@ SENML_UNIT_METER_PER_SECOND
meter per second (velocity) (m/s)
Definition: senml.h:110
@ SENML_UNIT_HECTOPASCAL
hectopascal (hPa, equivalent to 100 Pa)
Definition: senml.h:178
@ SENML_UNIT_PARTS_PER_BILLION
parts per billion (ppb, equivalent to 1e-9 '/')
Definition: senml.h:185
@ SENML_UNIT_PARTS_PER_MILLION
parts per million (ppm, equivalent to 1e-6 '/')
Definition: senml.h:175
@ SENML_UNIT_BECQUEREL
becquerel (Bq)
Definition: senml.h:103
@ SENML_UNIT_KILOVOLT_AMPERE
kilovolt-ampere (kVA, equivalent to 1000 VA)
Definition: senml.h:155
@ SENML_UNIT_WATT_HOUR
watt-hour (Wh, equivalent to 3600 J)
Definition: senml.h:158
@ SENML_UNIT_KILOWATT
kilowatt (kW, equivalent to 1000 W)
Definition: senml.h:154
@ SENML_UNIT_CUBIC_METER_PER_SECOND
cubic meter per second (flow rate) (m3/s)
Definition: senml.h:112
@ SENML_UNIT_TESLA
tesla (T)
Definition: senml.h:98
@ SENML_UNIT_RATE
1 per second (event rate) (1/s)
Definition: senml.h:130
@ SENML_UNIT_MILLIMETER
millimeter (mm, equivalent to 1/1000 m)
Definition: senml.h:179
@ SENML_UNIT_BYTE_PER_SECOND
byte per second (B/s, equivalent to 8 bit/s)
Definition: senml.h:167
@ SENML_UNIT_MOLE
mole (mol)
Definition: senml.h:84
@ SENML_UNIT_AMPERE_HOUR
ampere-hour (Ah, equivalent to 3600 C)
Definition: senml.h:157
@ SENML_UNIT_HENRY
henry (H)
Definition: senml.h:99
@ SENML_UNIT_CELSIUS
degrees Celsius (Cel)
Definition: senml.h:100
@ SENML_UNIT_REMAINING_BATTERY_SECONDS
seconds (remaining battery energy level) (EL)
Definition: senml.h:129
@ SENML_UNIT_MILLIGRAM_PER_LITER
milligram per liter (mg/l, equivalent to 1/1000 kg/m3)
Definition: senml.h:188
@ SENML_UNIT_DBW
decibel relative to 1 W (power level) (dBW)
Definition: senml.h:122
@ SENML_UNIT_AMPERE
ampere (A)
Definition: senml.h:81
@ SENML_UNIT_MILLIAMPERE
milliampere (mA, equivalent to 1/1000 A)
Definition: senml.h:170
@ SENML_UNIT_RELATIVE_HUMIDITY_PERCENT
Percentage (Relative Humidity) (RH)
Definition: senml.h:127
@ SENML_UNIT_SIEMENS_PER_METER
Siemens per meter (conductivity) (S/m)
Definition: senml.h:134
@ SENML_UNIT_BEL
bel (sound pressure level; logarithmic quantity) (Bspl)
Definition: senml.h:123
@ SENML_UNIT_CANDELA_PER_SQUARE_METER
candela per square meter (luminance) (cd/m2)
Definition: senml.h:115
@ SENML_UNIT_SIEVERT
sievert (Sv)
Definition: senml.h:105
@ SENML_UNIT_KILOGRAM
kilogram (kg)
Definition: senml.h:78
@ SENML_UNIT_GRAM
gram (g)
Definition: senml.h:79
@ SENML_UNIT_KILOGRAM_PER_CUBIC_METER
kilogram per cubic meter (mass density, mass concentration) (kg/m3)
Definition: senml.h:143
@ SENML_UNIT_JOULE_PER_METER
joule per meter (Energy per distance) (J/m)
Definition: senml.h:142
@ SENML_UNIT_METER_PER_SQUARE_SECOND
meter per square second (acceleration) (m/s2)
Definition: senml.h:111
@ SENML_UNIT_MINUTE
minute (min, equivalent to 60 s)
Definition: senml.h:151
@ SENML_UNIT_VOLT_AMPERE_REACTIVE
volt-ampere reactive (Reactive Power) (var)
Definition: senml.h:140
@ SENML_UNIT_VOLT_AMPERE_REACTIVE_SECOND
volt-ampere-reactive second (Reactive Energy) (vars)
Definition: senml.h:141
@ SENML_UNIT_RATIO_2
1 (ratio e.g., value of a switch) (%)
Definition: senml.h:126
@ SENML_UNIT_BIT_PER_SECOND
bit per second (data rate) (bit/s)
Definition: senml.h:117
@ SENML_UNIT_WEBER
weber (Wb)
Definition: senml.h:97
@ SENML_UNIT_RATIO
1 (ratio e.g., value of a switch) (/)
Definition: senml.h:125
@ SENML_UNIT_LONGITUDE
degrees longitude (lon)
Definition: senml.h:119
@ SENML_UNIT_MICROGRAM_PER_LITER
microgram per liter (ug/l, equivalent to 1e-6 kg/m3)
Definition: senml.h:189
@ SENML_UNIT_MILLISECOND
millisecond (ms, equivalent to 1/1000 s)
Definition: senml.h:150
@ SENML_UNIT_VOLT
volt (V)
Definition: senml.h:93
@ SENML_UNIT_WATT
watt (W)
Definition: senml.h:91
@ SENML_UNIT_DECIBEL_MILLIWATT
decibel (milliwatt) (dBm, equivalent to -29 dBW)
Definition: senml.h:171
@ SENML_UNIT_KILOVAR
kilovar (kvar, equivalent to 1000 var)
Definition: senml.h:156
@ SENML_UNIT_PH
pH value (acidity; logarithmic quantity) (pH)
Definition: senml.h:120
@ SENML_UNIT_REMAINING_BATTERY_PERCENT
Percentage (remaining battery energy level) (EL)
Definition: senml.h:128
@ SENML_UNIT_COULOMB
coulomb (C)
Definition: senml.h:92
@ SENML_UNIT_NEPHELOMETRIC_TURBIDITY_UNIT
Nephelometric Turbidity Unit (NTU)
Definition: senml.h:147
@ SENML_UNIT_KILOVAR_HOUR
kilovar-hour (kvarh, equivalent to 3600000 vars)
Definition: senml.h:161
@ SENML_UNIT_NONE
No unit specified.
Definition: senml.h:76
@ SENML_UNIT_BEAT_PER_MINUTE
1 per minute (heart rate in beats per minute) (beat/min))
Definition: senml.h:132
@ SENML_UNIT_COUNT
1 (counter value) (count)
Definition: senml.h:124
@ SENML_UNIT_KIBIBYTE
kibibyte (KiB, equivalent to 1024 B)
Definition: senml.h:164
@ SENML_UNIT_CANDELA
candela (cd)
Definition: senml.h:83
@ SENML_UNIT_METER_PER_HOUR
meter per hour (m/h, equivalent to 1/3600 m/s)
Definition: senml.h:174
@ SENML_UNIT_LUX
lux (lx)
Definition: senml.h:102
@ SENML_UNIT_OHM
ohm (Ohm)
Definition: senml.h:95
@ SENML_UNIT_CUBIC_METER
cubic meter (volume) (m3)
Definition: senml.h:108
@ SENML_UNIT_WATT_HOUR_PER_KILOMETER
watt-hour per kilometer (Wh/km, equivalent to 3.6 J/m)
Definition: senml.h:163
@ SENML_UNIT_GRAM_PER_LITER
gram per liter (g/l, equivalent to 1 kg/m3)
Definition: senml.h:190
@ SENML_UNIT_RADIAN
radian (rad)
Definition: senml.h:86
@ SENML_UNIT_KELVIN
kelvin (K)
Definition: senml.h:82
@ SENML_UNIT_LATITUDE
degrees latitude (lat)
Definition: senml.h:118
@ SENML_UNIT_HOUR
hour (h, equivalent to 3600 s)
Definition: senml.h:152
@ SENML_UNIT_MEGABYTE_PER_SECOND
megabyte per second (MB/s, equivalent to 8000000 bit/s)
Definition: senml.h:168
@ SENML_UNIT_MICROGRAM_PER_CUBIC_METER
microgram per cubic meter (ug/m3, equivalent to 1e-9 kg/m3)
Definition: senml.h:172
@ SENML_UNIT_SQUARE_METER
square meter (area) (m2)
Definition: senml.h:107
@ SENML_UNIT_RPM
1 per minute (event rate, "rpm") (1/min)
Definition: senml.h:131
@ SENML_UNIT_SECOND
second (s)
Definition: senml.h:80
@ SENML_UNIT_WATT_PER_SQUARE_METER
watt per square meter (irradiance) (W/m2)
Definition: senml.h:114
@ SENML_UNIT_LITER
liter (volume) (l)
Definition: senml.h:109
@ SENML_UNIT_MILLIMETER_PER_HOUR
millimeter per hour (mm/h, equivalent to 1/3600000 m/s)
Definition: senml.h:173
@ SENML_UNIT_FARAD
farad (F)
Definition: senml.h:94
@ SENML_UNIT_DEGREE
degree (angle) (deg)
Definition: senml.h:144
@ SENML_UNIT_VAR_HOUR
var-hour (varh, equivalent to 3600 vars)
Definition: senml.h:160
@ SENML_UNIT_DECIBEL
decibel (logarithmic quantity) (dB)
Definition: senml.h:121
@ SENML_UNIT_PARTS_PER_TRILLION
parts per trillion (ppt, equivalent to 1e-12 '/')
Definition: senml.h:186
@ SENML_UNIT_SIEMENS
siemens (S)
Definition: senml.h:96
@ SENML_UNIT_LUMEN
lumen (lm)
Definition: senml.h:101
@ SENML_UNIT_STERADIAN
steradian (sr)
Definition: senml.h:87
@ SENML_UNIT_NEWTON
newton (N)
Definition: senml.h:88
@ SENML_UNIT_PASCAL
pascal (Pa)
Definition: senml.h:89
@ SENML_UNIT_BEATS
1 (Cumulative number of heart beats) (beats)
Definition: senml.h:133
@ SENML_UNIT_GRAY
gray (Gy)
Definition: senml.h:104
@ SENML_UNIT_BIT
bit (information content) (bit)
Definition: senml.h:116
@ SENML_UNIT_LITER_PER_SECOND
liter per second (flow rate) (l/s)
Definition: senml.h:113
@ SENML_UNIT_MEGABIT_PER_SECOND
megabit per second (Mbit/s, equivalent to 1000000 bit/s)
Definition: senml.h:166
@ SENML_UNIT_METER
meter (m)
Definition: senml.h:77
@ SENML_TYPE_NUMERIC_DOUBLE
Double-precision floating point number.
Definition: senml.h:201
@ SENML_TYPE_NUMERIC_UINT
Unsigned integer.
Definition: senml.h:198
@ SENML_TYPE_NUMERIC_FLOAT
Floating point number.
Definition: senml.h:200
@ SENML_TYPE_NUMERIC_DECFRAC
Decimal fraction.
Definition: senml.h:202
@ SENML_TYPE_NUMERIC_INT
Integer.
Definition: senml.h:199
Common macros and compiler attributes/pragmas configuration.
SenML common record attributes.
Definition: senml.h:235
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:253
senml_numeric_t time
Time of the measurement (relative or Unix) in seconds.
Definition: senml.h:251
senml_numeric_t base_time
Base Time.
Definition: senml.h:237
senml_numeric_t base_value
Base Value.
Definition: senml.h:239
senml_numeric_t sum
Sum, set CONFIG_SENML_ATTR_SUM to 1 to enable.
Definition: senml.h:249
const char * name
Name of the measurement.
Definition: senml.h:246
senml_unit_t unit
Unit.
Definition: senml.h:247
senml_unit_t base_unit
Base Unit.
Definition: senml.h:238
const char * base_name
Base Name.
Definition: senml.h:236
uint64_t base_version
Base Version, set CONFIG_SENML_ATTR_VERSION to 1 to enable.
Definition: senml.h:244
senml_numeric_t base_sum
Base Sum, set CONFIG_SENML_ATTR_SUM to 1 to enable.
Definition: senml.h:241
SenML boolean value.
Definition: senml.h:277
bool value
Value.
Definition: senml.h:279
senml_attr_t attr
SenML attributes.
Definition: senml.h:278
SenML data value.
Definition: senml.h:285
const uint8_t * value
Value.
Definition: senml.h:287
size_t len
Value length.
Definition: senml.h:288
senml_attr_t attr
SenML attributes.
Definition: senml.h:286
Decimal fraction containing a value in the form of m * 10^e.
Definition: senml.h:208
int32_t m
Mantissa.
Definition: senml.h:210
int32_t e
Exponent.
Definition: senml.h:209
SenML numeric value.
Definition: senml.h:219
union senml_numeric_t::@399 value
Value data.
struct senml_numeric_t::@399::@400 df
Decimal fraction.
senml_value_type_t type
Type of the value.
Definition: senml.h:220
SenML string value.
Definition: senml.h:268
senml_attr_t attr
SenML attributes.
Definition: senml.h:269
const char * value
Value.
Definition: senml.h:270
size_t len
Value length.
Definition: senml.h:271
SenML string value.
Definition: senml.h:260
senml_attr_t attr
SenML attributes.
Definition: senml.h:261
senml_numeric_t value
Value.
Definition: senml.h:262