DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
lonworks.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 lonworks.h
6 * @brief LonWorks / LON-IP (ISO/IEC 14908) network-variable codec (DETWS_ENABLE_LONWORKS).
7 *
8 * LonWorks is the ISO/IEC 14908 building-automation network. Devices exchange **network variables**
9 * (SNVTs - Standard Network Variable Types) as LonTalk application PDUs. LON/IP (14908-4) tunnels those
10 * PDUs over UDP so a device speaks LON without a Neuron chip. This codec builds/parses the LonTalk
11 * application-layer message a network-variable update carries:
12 *
13 * [msg-code : 1][nv-selector : 2 (14-bit, big-endian)][value...]
14 *
15 * where the message code identifies a NetVar update (`0x80 | direction`), the selector addresses the
16 * bound network variable, and the value is the SNVT-encoded data. It also provides the two most-common
17 * SNVT scalar encodings: **SNVT_temp** (temperature, 0.01 K resolution, offset) and **SNVT_switch**
18 * (a level 0..100.5% + a state), so an app reads/writes those without a full SNVT table. Pure, zero heap,
19 * no stdlib, host-testable; the LON/IP UDP transport is the shipped UDP layer.
20 */
21
22#ifndef DETERMINISTICESPASYNCWEBSERVER_LONWORKS_H
23#define DETERMINISTICESPASYNCWEBSERVER_LONWORKS_H
24
25#include "ServerConfig.h"
26#include <stddef.h>
27#include <stdint.h>
28
29#if DETWS_ENABLE_LONWORKS
30
31// LonTalk NV message codes + selector limit: wire values, so integer constants in a struct.
32struct Lon
33{
34 static constexpr uint16_t LON_MSG_NV_UPDATE = 0x80; ///< network-variable update message code (base).
35 static constexpr uint16_t LON_MSG_NV_POLL = 0x81; ///< network-variable poll (request).
36 static constexpr uint16_t LON_NV_SELECTOR_MAX = 0x3FFF; ///< the NV selector is 14 bits.
37};
38
39/**
40 * @brief Build a LonTalk NV-update application PDU: [msg-code][selector:2][value...].
41 * @param msg_code LON_MSG_NV_UPDATE / LON_MSG_NV_POLL.
42 * @param selector the 14-bit NV selector (0..0x3FFF).
43 * @param value the SNVT-encoded value (may be null if value_len == 0).
44 * @param value_len value length.
45 * @return the PDU length (3 + value_len), or 0 on overflow / bad args.
46 */
47size_t detws_lon_build_nv(uint8_t msg_code, uint16_t selector, const uint8_t *value, size_t value_len, uint8_t *out,
48 size_t cap);
49
50/** @brief A parsed LonTalk NV PDU (value points into the input). */
51struct LonNv
52{
53 uint8_t msg_code;
54 uint16_t selector;
55 const uint8_t *value;
56 size_t value_len;
57};
58
59/** @brief Parse a LonTalk NV PDU. @return true if @p len >= 3. */
60bool detws_lon_parse_nv(const uint8_t *pdu, size_t len, LonNv *out);
61
62/** @brief Encode a SNVT_temp value (degrees C) as the 2-byte big-endian fixed-point (0.01 K, +273.15). */
63void detws_lon_snvt_temp_encode(double celsius, uint8_t out[2]);
64/** @brief Decode a SNVT_temp 2-byte value to degrees C. */
65double detws_lon_snvt_temp_decode(const uint8_t in[2]);
66
67/** @brief Encode a SNVT_switch (value 0..100.5 %, state 0/1) into the 2-byte value. */
68void detws_lon_snvt_switch_encode(double percent, uint8_t state, uint8_t out[2]);
69/** @brief Decode a SNVT_switch 2-byte value (percent out via @p percent, state via @p state). */
70void detws_lon_snvt_switch_decode(const uint8_t in[2], double *percent, uint8_t *state);
71
72#endif // DETWS_ENABLE_LONWORKS
73#endif // DETERMINISTICESPASYNCWEBSERVER_LONWORKS_H
User-facing configuration for DeterministicESPAsyncWebServer.