18#define SENML_LBL_BN (-2)
19#define SENML_LBL_BT (-3)
28static bool is_integral(
double d)
30 return d >= -9.2e18 && d <= 9.2e18 && d == (double)(int64_t)d;
39 snprintf(tmp,
sizeof(tmp),
"%lld", (
long long)d);
41 snprintf(tmp,
sizeof(tmp),
"%g", d);
45size_t senml_json_build(
char *buf,
size_t cap,
const SenmlRecord *records,
size_t count)
47 if (!buf || (count && !records))
51 for (
size_t i = 0; i < count; i++)
53 const SenmlRecord &r = records[i];
56 w.
kv_str(
"bn", r.base_name);
60 json_num(w, r.base_time);
68 case SenmlValueKind::SENML_V_FLOAT:
72 case SenmlValueKind::SENML_V_STRING:
74 w.
kv_str(
"vs", r.value_str);
76 case SenmlValueKind::SENML_V_BOOL:
79 case SenmlValueKind::SENML_V_NONE:
94static void cbor_num(CborWriter *w,
double d)
97 cbor_int(w, (int64_t)d);
99 cbor_float(w, (
float)d);
102static size_t record_fields(
const SenmlRecord &r)
113 if (r.value_kind != SenmlValueKind::SENML_V_NONE &&
114 !(r.value_kind == SenmlValueKind::SENML_V_STRING && !r.value_str))
121size_t senml_cbor_build(uint8_t *buf,
size_t cap,
const SenmlRecord *records,
size_t count)
123 if (!buf || (count && !records))
126 cbor_init(&w, buf, cap);
127 cbor_array(&w, count);
128 for (
size_t i = 0; i < count; i++)
130 const SenmlRecord &r = records[i];
131 cbor_map(&w, record_fields(r));
134 cbor_int(&w, SENML_LBL_BN);
135 cbor_text(&w, r.base_name);
139 cbor_int(&w, SENML_LBL_BT);
140 cbor_num(&w, r.base_time);
144 cbor_int(&w, SENML_LBL_N);
145 cbor_text(&w, r.name);
149 cbor_int(&w, SENML_LBL_U);
150 cbor_text(&w, r.unit);
152 switch (r.value_kind)
154 case SenmlValueKind::SENML_V_FLOAT:
155 cbor_int(&w, SENML_LBL_V);
156 cbor_num(&w, r.value);
158 case SenmlValueKind::SENML_V_STRING:
161 cbor_int(&w, SENML_LBL_VS);
162 cbor_text(&w, r.value_str);
165 case SenmlValueKind::SENML_V_BOOL:
166 cbor_int(&w, SENML_LBL_VB);
167 cbor_bool(&w, r.value_bool);
169 case SenmlValueKind::SENML_V_NONE:
174 cbor_int(&w, SENML_LBL_T);
175 cbor_num(&w, r.time);
178 return cbor_ok(&w) ? cbor_len(&w) : 0;
Layer 6 (Presentation) - zero-heap CBOR (RFC 8949) encoder.
Builds a JSON document into a fixed caller buffer, no heap.
void begin_object()
Open { (as a value/element where applicable).
void key(const char *k)
Emit an object member name ("k":); follow with one value.
void raw(const char *literal)
Emit a pre-formatted literal verbatim.
void kv_bool(const char *k, bool v)
"k":true|false.
void kv_str(const char *k, const char *v)
"k":"v" (escaped).
bool ok() const
< False after any overflow / structural error.
void end_object()
Close }.
size_t length() const
< Bytes written so far (excludes the NUL).
void begin_array()
Open [.
Layer 6 (Presentation) - zero-heap JSON: a bounded writer and top-level reader.
SenML (RFC 8428) sensor-measurement pack builder (DETWS_ENABLE_SENML) - zero-heap SenML-JSON and SenM...