DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
wave.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 wave.h
6 * @brief IEEE 1609 WAVE (WSMP + 1609.2 security envelope) codec (DETWS_ENABLE_WAVE).
7 *
8 * IEEE 1609 (WAVE - Wireless Access in Vehicular Environments) is the radio stack that carries the
9 * J2735 V2X messages (services/j2735). This codec provides the two framing layers below J2735:
10 *
11 * - **1609.3 WSMP** (WAVE Short Message Protocol): a compact header - version/subtype, a
12 * transmit-power/channel WAVE-element block, a PSID (Provider Service Identifier, a variable-length
13 * p-encoded integer identifying the application, e.g. BSM/SPaT), and a length - then the payload.
14 * - **1609.2** secured-message envelope: `protocolVersion(3) + contentType` header that wraps a signed
15 * or unsecured-data payload (the full signature/cert machinery is the crypto layer on top).
16 *
17 * This builds/parses the WSMP header + PSID p-encoding and the 1609.2 envelope header. Pure, zero heap,
18 * no stdlib, host-testable; the DSRC / C-V2X radio is an external module.
19 */
20
21#ifndef DETERMINISTICESPASYNCWEBSERVER_WAVE_H
22#define DETERMINISTICESPASYNCWEBSERVER_WAVE_H
23
24#include "ServerConfig.h"
25#include <stddef.h>
26#include <stdint.h>
27
28#if DETWS_ENABLE_WAVE
29
30// WSMP / 1609.2 versions + content types + PSIDs: wire values, so integer constants in a struct.
31struct Wave
32{
33 static constexpr uint16_t WSMP_VERSION = 0x03; ///< WSMP version (in the low nibble of byte 0).
34 static constexpr uint16_t WAVE_16092_VERSION = 0x03; ///< 1609.2 protocolVersion.
35 static constexpr uint16_t WAVE_16092_UNSECURED = 0x00; ///< content type: unsecuredData.
36 static constexpr uint16_t WAVE_16092_SIGNED = 0x01; ///< content type: signedData.
37 // Common PSIDs (Provider Service Identifiers).
38 static constexpr uint16_t WAVE_PSID_BSM = 0x20; ///< vehicle safety (BSM), PSID 0x20.
39 static constexpr uint16_t WAVE_PSID_SPAT = 0x8002; ///< signal phase and timing.
40 static constexpr uint16_t WAVE_PSID_MAP = 0x8003; ///< map data.
41};
42
43/**
44 * @brief Encode a PSID as a P-encoded (variable-length) integer.
45 *
46 * The PSID length is signalled by the top bits of the first octet: 1 octet if < 0x80, 2 if < 0x4000,
47 * 3 if < 0x200000, 4 otherwise. @return octets written (1..4), or 0 if it will not fit.
48 */
49size_t detws_wave_encode_psid(uint32_t psid, uint8_t *out, size_t cap);
50
51/** @brief Decode a P-encoded PSID. @return octets consumed (1..4), or 0 if malformed; sets @p psid. */
52size_t detws_wave_decode_psid(const uint8_t *in, size_t len, uint32_t *psid);
53
54/**
55 * @brief Build a WSMP data frame: WSMP header (version + PSID + a 1-byte length) then the payload.
56 * @param psid the Provider Service Identifier.
57 * @param payload the WSM payload (e.g. a 1609.2-wrapped J2735 message).
58 * @param payload_len 0..255.
59 * @return the frame length, or 0 on overflow / bad args.
60 */
61size_t detws_wsmp_build(uint32_t psid, const uint8_t *payload, size_t payload_len, uint8_t *out, size_t cap);
62
63/** @brief A parsed WSMP frame (payload points into the input). */
64struct WsmpFrame
65{
66 uint32_t psid;
67 const uint8_t *payload;
68 size_t payload_len;
69};
70
71/** @brief Parse a WSMP data frame. @return true if well-formed. */
72bool detws_wsmp_parse(const uint8_t *frame, size_t len, WsmpFrame *out);
73
74/**
75 * @brief Build a 1609.2 secured-message envelope header + payload: [version][contentType][payload...].
76 * @param content_type WAVE_16092_UNSECURED / WAVE_16092_SIGNED.
77 * @return the length written (2 + payload_len), or 0 on overflow.
78 */
79size_t detws_wave_1609dot2_wrap(uint8_t content_type, const uint8_t *payload, size_t payload_len, uint8_t *out,
80 size_t cap);
81
82#endif // DETWS_ENABLE_WAVE
83#endif // DETERMINISTICESPASYNCWEBSERVER_WAVE_H
User-facing configuration for DeterministicESPAsyncWebServer.