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