DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
wisun.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 wisun.h
6 * @brief Wi-SUN FAN border-router connector (DETWS_ENABLE_WISUN).
7 *
8 * Wi-SUN FAN is an IPv6 / UDP / CoAP mesh, not a byte-level radio the ESP32 drives - the FAN radio is
9 * terminated by a **border router / devboard** and each mesh node is reached as an ordinary IPv6 CoAP
10 * endpoint. So the connector rides the existing IP stack: it keeps a table of the FAN nodes (their IPv6
11 * `DetIp` addresses + join state) behind the border router, and builds the CoAP client requests to their
12 * resources (the CoAP service ships a *server*, so the client-request builder is here). The app sends the
13 * built PDU to the node's address over `det_udp`; the specific devboard only sets which border router you
14 * point at, not this code.
15 *
16 * Pure: `wisun_build_coap` frames an RFC 7252 request (header + Uri-Path options + payload), the node
17 * registry tracks the mesh, and `wisun_nodes_json` exposes it to the web. No heap, no stdlib,
18 * host-testable.
19 */
20
21#ifndef DETERMINISTICESPASYNCWEBSERVER_WISUN_H
22#define DETERMINISTICESPASYNCWEBSERVER_WISUN_H
23
24#include "ServerConfig.h"
26#include <stddef.h>
27#include <stdint.h>
28
29#if DETWS_ENABLE_WISUN
30
31/** @brief CoAP message type + method codes (RFC 7252) used by the connector. */
32struct WisunCoap
33{
34 static constexpr uint8_t WISUN_COAP_CON = 0; ///< Confirmable.
35 static constexpr uint8_t WISUN_COAP_NON = 1; ///< Non-confirmable.
36 static constexpr uint8_t WISUN_COAP_GET = 1; ///< method code 0.01.
37 static constexpr uint8_t WISUN_COAP_PUT = 3; ///< method code 0.03.
38};
39
40/**
41 * @brief Build a CoAP client request: header + Uri-Path options (one per `/` segment) + optional payload.
42 * @param type WISUN_COAP_CON / WISUN_COAP_NON.
43 * @param code method code (WISUN_COAP_GET / WISUN_COAP_PUT).
44 * @param msg_id the 16-bit message id (echoed in the ACK).
45 * @param token correlation token (0..8 bytes; may be null if @p tkl == 0).
46 * @param tkl token length.
47 * @param uri_path resource path, e.g. "sensors/temp" (leading / optional).
48 * @param payload request body (may be null if @p plen == 0).
49 * @param plen payload length.
50 * @return the PDU length, or 0 on overflow / bad args (tkl > 8).
51 */
52size_t wisun_build_coap(uint8_t type, uint8_t code, uint16_t msg_id, const uint8_t *token, uint8_t tkl,
53 const char *uri_path, const uint8_t *payload, size_t plen, uint8_t *out, size_t cap);
54
55/** @brief One FAN mesh node behind the border router. */
56struct WisunNode
57{
58 DetIp addr; ///< the node's IPv6 address on the mesh.
59 bool joined; ///< true once the node has joined the FAN.
60 uint32_t last_seen; ///< tick of the last contact.
61};
62
63/** @brief The FAN connector state over a caller-owned node table. */
64struct WisunFan
65{
66 DetIp border_router; ///< the border router / devboard address.
67 WisunNode *nodes;
68 size_t count;
69 size_t cap;
70};
71
72/** @brief Initialize the connector over caller storage. */
73void wisun_init(WisunFan *fan, const DetIp *border_router, WisunNode *storage, size_t cap);
74
75/**
76 * @brief Register (or refresh) a node by address; sets joined + last_seen.
77 * @return the node index, or -1 if the table is full / bad args.
78 */
79int wisun_node_register(WisunFan *fan, const DetIp *addr, uint32_t now);
80
81/** @brief Find a node by address. @p idx (may be null) receives the index. @return found. */
82bool wisun_node_find(const WisunFan *fan, const DetIp *addr, size_t *idx);
83
84/** @brief Number of joined nodes. */
85size_t wisun_joined_count(const WisunFan *fan);
86
87/**
88 * @brief Serialize the node table as `[{"addr":"..","joined":bool},...]` for the web.
89 * @return length written (excl NUL), or 0 on overflow / bad args.
90 */
91size_t wisun_nodes_json(const WisunFan *fan, char *out, size_t cap);
92
93#endif // DETWS_ENABLE_WISUN
94#endif // DETERMINISTICESPASYNCWEBSERVER_WISUN_H
User-facing configuration for DeterministicESPAsyncWebServer.
Layer 3 (Network) - a family-tagged IP address (IPv4 or IPv6) with RFC-faithful text parsing,...
A v4 or v6 address in network (big-endian) byte order.
Definition ip.h:52