DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
lwm2m_tlv.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file lwm2m_tlv.h
6 * @brief OMA LwM2M TLV codec (DETWS_ENABLE_LWM2M) - zero-heap writer + cursor reader for the
7 * `application/vnd.oma.lwm2m+tlv` resource encoding, carried over the shipped CoAP
8 * service for LwM2M device management.
9 *
10 * Each TLV is `Type(1) Identifier(1-2) Length(0-3) Value(n)`:
11 * - Type bits 7-6: identifier kind - 00 Object Instance, 01 Resource Instance, 10 Multiple
12 * Resource, 11 Resource with Value.
13 * - Type bit 5: identifier width (0 = 8-bit, 1 = 16-bit).
14 * - Type bits 4-3: length kind - 00 inline in bits 2-0, 01 = 8-bit, 10 = 16-bit, 11 = 24-bit.
15 * - Type bits 2-0: the value length when the length kind is inline (0..7).
16 * Identifiers and lengths are big-endian. Resource values are big-endian: integers are the
17 * shortest of 1/2/4/8 octets (two's complement), booleans one octet, strings UTF-8.
18 *
19 * The writer emits TLVs into a caller buffer (fail-closed on overflow); the reader is a
20 * cursor that decodes one TLV at the buffer head. Type-byte layout verified against the
21 * OMA LwM2M spec.
22 *
23 * @author Douglas Quigg (dstroy0)
24 * @date 2026
25 */
26
27#ifndef DETERMINISTICESPASYNCWEBSERVER_LWM2M_TLV_H
28#define DETERMINISTICESPASYNCWEBSERVER_LWM2M_TLV_H
29
30#include "ServerConfig.h"
31
32#if DETWS_ENABLE_LWM2M
33
34#include <stddef.h>
35#include <stdint.h>
36
37// Identifier kinds (Type byte bits 7-6).
38#define LWM2M_TLV_OBJECT_INSTANCE 0x00
39#define LWM2M_TLV_RESOURCE_INSTANCE 0x40
40#define LWM2M_TLV_MULTIPLE_RESOURCE 0x80
41#define LWM2M_TLV_RESOURCE 0xC0
42
43// Type-byte bit-field layout (Type byte = idkind | id16 | lentype | inline-len).
44#define LWM2M_TLV_IDKIND_MASK 0xC0 ///< bits 7-6: identifier kind (the LWM2M_TLV_* above)
45#define LWM2M_TLV_ID16_FLAG 0x20 ///< bit 5: identifier is 16-bit (else 8-bit)
46#define LWM2M_TLV_LENTYPE_SHIFT 3 ///< bits 4-3: length-type field position
47#define LWM2M_TLV_LENTYPE_MASK 0x03 ///< 0 = inline, 1 = 8-bit, 2 = 16-bit, 3 = 24-bit length
48#define LWM2M_TLV_INLINE_LEN_MASK 0x07 ///< bits 2-0: the value length when length-type == 0
49
50/** @brief Cursor for building a TLV payload. Treat the fields as opaque. */
51struct Lwm2mTlvWriter
52{
53 uint8_t *buf;
54 size_t cap;
55 size_t pos;
56 bool error;
57};
58
59void lwm2m_tlv_init(Lwm2mTlvWriter *w, uint8_t *buf, size_t cap);
60
61/** @brief Write a TLV with raw value bytes. @p id_type is one of LWM2M_TLV_*. */
62bool lwm2m_tlv_write(Lwm2mTlvWriter *w, uint8_t id_type, uint16_t id, const uint8_t *value, size_t value_len);
63
64/** @brief Write a Resource integer (shortest of 1/2/4/8 octets, big-endian two's complement). */
65bool lwm2m_tlv_write_int(Lwm2mTlvWriter *w, uint16_t id, int64_t v);
66
67/** @brief Write a Resource boolean (one octet, 0/1). */
68bool lwm2m_tlv_write_bool(Lwm2mTlvWriter *w, uint16_t id, bool v);
69
70/** @brief Write a Resource UTF-8 string. */
71bool lwm2m_tlv_write_string(Lwm2mTlvWriter *w, uint16_t id, const char *s);
72
73/** @brief Write a Resource float (8-octet IEEE-754, big-endian). */
74bool lwm2m_tlv_write_float(Lwm2mTlvWriter *w, uint16_t id, double v);
75
76/** @brief Bytes written so far, or 0 if any write overflowed. */
77size_t lwm2m_tlv_finish(Lwm2mTlvWriter *w);
78
79/** @brief One decoded TLV; @ref value points INTO the source buffer. */
80struct Lwm2mTlv
81{
82 uint8_t id_type; ///< LWM2M_TLV_* (Type bits 7-6)
83 uint16_t id;
84 const uint8_t *value;
85 size_t value_len;
86};
87
88/**
89 * @brief Read one TLV at [buf+*pos]; advances *pos past it.
90 * @return true on a complete TLV; false at end-of-buffer or on truncation.
91 */
92bool lwm2m_tlv_read(const uint8_t *buf, size_t len, size_t *pos, Lwm2mTlv *out);
93
94/** @brief Decode a TLV integer value (1/2/4/8 octets, big-endian two's complement). */
95bool lwm2m_tlv_value_int(const uint8_t *value, size_t len, int64_t *out);
96
97#endif // DETWS_ENABLE_LWM2M
98
99#endif // DETERMINISTICESPASYNCWEBSERVER_LWM2M_TLV_H
User-facing configuration for DeterministicESPAsyncWebServer.