DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
wamp.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 wamp.h
6 * @brief WAMP (Web Application Messaging Protocol) codec (DETWS_ENABLE_WAMP) - zero-heap
7 * builders + a positional parser for the unified RPC + PubSub protocol, which rides
8 * the shipped WebSocket layer (subprotocol `wamp.2.json`).
9 *
10 * A WAMP message is a JSON array whose first element is the integer message type, e.g.
11 * `SUBSCRIBE = [32, Request|id, Options|dict, Topic|uri]`. The builders drive the shared
12 * @ref JsonWriter to emit these arrays into a caller buffer (Options/Details default to
13 * `{}`; Arguments / ArgumentsKw are passed as pre-formatted JSON literals or omitted). The
14 * parser is a small positional scanner over an inbound array: extract the message type, an
15 * id at a given position, or a URI - enough to drive WELCOME / SUBSCRIBED / EVENT / RESULT
16 * / INVOCATION / ERROR handling. Message codes verified against the WAMP spec.
17 *
18 * The WebSocket connection and the session / subscription / registration tables are the
19 * application's; this is the message codec.
20 *
21 * @author Douglas Quigg (dstroy0)
22 * @date 2026
23 */
24
25#ifndef DETERMINISTICESPASYNCWEBSERVER_WAMP_H
26#define DETERMINISTICESPASYNCWEBSERVER_WAMP_H
27
28#include "ServerConfig.h"
29
30#if DETWS_ENABLE_WAMP
31
32#include <stddef.h>
33#include <stdint.h>
34
35// WAMP message type codes (basic + advanced profile).
36#define WAMP_HELLO 1
37#define WAMP_WELCOME 2
38#define WAMP_ABORT 3
39#define WAMP_GOODBYE 6
40#define WAMP_ERROR 8
41#define WAMP_PUBLISH 16
42#define WAMP_PUBLISHED 17
43#define WAMP_SUBSCRIBE 32
44#define WAMP_SUBSCRIBED 33
45#define WAMP_UNSUBSCRIBE 34
46#define WAMP_UNSUBSCRIBED 35
47#define WAMP_EVENT 36
48#define WAMP_CALL 48
49#define WAMP_RESULT 50
50#define WAMP_REGISTER 64
51#define WAMP_REGISTERED 65
52#define WAMP_UNREGISTER 66
53#define WAMP_UNREGISTERED 67
54#define WAMP_INVOCATION 68
55#define WAMP_YIELD 70
56
57// ---- builders (return bytes written, or 0 on overflow / bad input) ----
58// For the *_json params: pass a pre-formatted JSON literal, or nullptr for the default
59// (`{}` for options/details). Passing kwargs without args emits an empty `[]` for args so
60// the positional layout stays valid.
61
62/** @brief HELLO: `[1, "realm", Details]`. */
63size_t wamp_build_hello(char *buf, size_t cap, const char *realm, const char *details_json);
64
65/** @brief GOODBYE: `[6, Details, "reason_uri"]`. */
66size_t wamp_build_goodbye(char *buf, size_t cap, const char *reason_uri, const char *details_json);
67
68/** @brief SUBSCRIBE: `[32, Request, Options, "topic"]`. */
69size_t wamp_build_subscribe(char *buf, size_t cap, uint64_t request, const char *topic, const char *options_json);
70
71/** @brief UNSUBSCRIBE: `[34, Request, SubscriptionId]`. */
72size_t wamp_build_unsubscribe(char *buf, size_t cap, uint64_t request, uint64_t subscription_id);
73
74/** @brief PUBLISH: `[16, Request, Options, "topic"(, Arguments(, ArgumentsKw))]`. */
75size_t wamp_build_publish(char *buf, size_t cap, uint64_t request, const char *topic, const char *options_json,
76 const char *args_json, const char *kwargs_json);
77
78/** @brief CALL: `[48, Request, Options, "procedure"(, Arguments(, ArgumentsKw))]`. */
79size_t wamp_build_call(char *buf, size_t cap, uint64_t request, const char *procedure, const char *options_json,
80 const char *args_json, const char *kwargs_json);
81
82/** @brief REGISTER: `[64, Request, Options, "procedure"]`. */
83size_t wamp_build_register(char *buf, size_t cap, uint64_t request, const char *procedure, const char *options_json);
84
85/** @brief YIELD: `[70, Request, Options(, Arguments(, ArgumentsKw))]` (replies to an INVOCATION). */
86size_t wamp_build_yield(char *buf, size_t cap, uint64_t request, const char *options_json, const char *args_json,
87 const char *kwargs_json);
88
89// ---- parser (positional access over an inbound JSON-array message) ----
90
91/** @brief Slice the raw text of the top-level array element at @p index (into @p msg). */
92bool wamp_element(const char *msg, size_t index, const char **start, size_t *len);
93
94/** @brief Read the message type (element 0) as an integer. */
95bool wamp_get_type(const char *msg, int *out);
96
97/** @brief Read the unsigned integer (e.g. a Request / Subscription / Publication id) at @p index. */
98bool wamp_get_uint(const char *msg, size_t index, uint64_t *out);
99
100/**
101 * @brief Copy the URI/string element at @p index (surrounding quotes stripped) into @p out.
102 * @note No unescaping - WAMP URIs are restricted to unescaped characters. Bounded by @p out_cap.
103 */
104bool wamp_get_uri(const char *msg, size_t index, char *out, size_t out_cap);
105
106#endif // DETWS_ENABLE_WAMP
107
108#endif // DETERMINISTICESPASYNCWEBSERVER_WAMP_H
User-facing configuration for DeterministicESPAsyncWebServer.