DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
amqp.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 amqp.h
6 * @brief AMQP 0-9-1 frame codec (DETWS_ENABLE_AMQP) - zero-heap frame builder + parser for
7 * the RabbitMQ wire protocol, so a device can be an AMQP client over the shipped
8 * outbound client transport.
9 *
10 * The connection opens with an 8-octet protocol header (`"AMQP" 0 0 9 1`). After that every
11 * frame is:
12 * @code
13 * type(1) channel(2, big-endian) size(4, big-endian) payload(size) frame-end(1)=0xCE
14 * @endcode
15 * - Frame types: 1 METHOD, 2 HEADER (content header), 3 BODY (content body), 8 HEARTBEAT.
16 * - A method frame's payload is `class-id(2) method-id(2)` then the method arguments.
17 * - A heartbeat is type 8 on channel 0 with an empty payload.
18 *
19 * The builders frame a payload into a caller buffer (fail-closed, validating the 0xCE end on
20 * parse); the method arguments are the application's. Frame format per the AMQP 0-9-1 spec.
21 *
22 * @author Douglas Quigg (dstroy0)
23 * @date 2026
24 */
25
26#ifndef DETERMINISTICESPASYNCWEBSERVER_AMQP_H
27#define DETERMINISTICESPASYNCWEBSERVER_AMQP_H
28
29#include "ServerConfig.h"
30
31#if DETWS_ENABLE_AMQP
32
33#include <stddef.h>
34#include <stdint.h>
35
36// Frame types (octet 0).
37#define AMQP_FRAME_METHOD 1
38#define AMQP_FRAME_HEADER 2 ///< content header
39#define AMQP_FRAME_BODY 3 ///< content body
40#define AMQP_FRAME_HEARTBEAT 8
41
42#define AMQP_FRAME_END 0xCE ///< the mandatory frame terminator octet
43#define AMQP_FRAME_OVERHEAD 8 ///< type(1) + channel(2) + size(4) + frame-end(1)
44
45/** @brief Write the 8-octet protocol header ("AMQP" + 0 0 9 1). Returns 8, or 0 on overflow. */
46size_t amqp_protocol_header(uint8_t *buf, size_t cap);
47
48/** @brief Build a frame: type + channel + size + payload + 0xCE. Returns total octets, or 0. */
49size_t amqp_build_frame(uint8_t *buf, size_t cap, uint8_t type, uint16_t channel, const uint8_t *payload,
50 size_t payload_len);
51
52/** @brief Build a METHOD frame: payload = class-id + method-id + @p args. */
53size_t amqp_build_method(uint8_t *buf, size_t cap, uint16_t channel, uint16_t class_id, uint16_t method_id,
54 const uint8_t *args, size_t args_len);
55
56/** @brief Build a heartbeat frame (type 8, channel 0, empty payload). Returns 8, or 0. */
57size_t amqp_build_heartbeat(uint8_t *buf, size_t cap);
58
59/** @brief A parsed frame. @ref payload points INTO the source buffer. */
60struct AmqpFrame
61{
62 uint8_t type;
63 uint16_t channel;
64 const uint8_t *payload;
65 size_t payload_len;
66};
67
68/**
69 * @brief Parse one frame at the head of [buf, buf+len), validating the 0xCE frame-end.
70 * @param consumed receives the full frame length so the caller can advance.
71 * @return true on a complete, terminated frame; false if incomplete or the end octet is wrong.
72 */
73bool amqp_parse_frame(const uint8_t *buf, size_t len, AmqpFrame *out, size_t *consumed);
74
75/** @brief Split a METHOD frame payload into class-id / method-id / arguments. */
76bool amqp_parse_method(const uint8_t *payload, size_t payload_len, uint16_t *class_id, uint16_t *method_id,
77 const uint8_t **args, size_t *args_len);
78
79#endif // DETWS_ENABLE_AMQP
80
81#endif // DETERMINISTICESPASYNCWEBSERVER_AMQP_H
User-facing configuration for DeterministicESPAsyncWebServer.