DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
nmea2000.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 nmea2000.h
6 * @brief NMEA 2000 codec (DETWS_ENABLE_NMEA2000) - the marine instrumentation network, built on
7 * J1939 over CAN.
8 *
9 * NMEA 2000 is J1939 at the transport layer (the same 29-bit priority / PGN / source /
10 * destination identifier), so this codec reuses the J1939 id encode / decode
11 * (`DETWS_ENABLE_NMEA2000` force-enables `DETWS_ENABLE_J1939`). What it adds is the
12 * NMEA-specific **Fast Packet** transport: messages of 9..223 octets are split across CAN
13 * frames using a per-frame control octet (sequence counter + frame counter) instead of the
14 * J1939 BAM/CMDT protocol. The first frame carries the total length; continuations carry 7
15 * data octets each.
16 *
17 * Pure and host-tested. Drive it from the ESP32 TWAI peripheral or an MCP2515 over SPI to
18 * bridge an NMEA 2000 backbone (GPS, wind, depth, engine PGNs) onto Wi-Fi.
19 *
20 * @author Douglas Quigg (dstroy0)
21 * @date 2026
22 */
23
24#ifndef DETERMINISTICESPASYNCWEBSERVER_NMEA2000_H
25#define DETERMINISTICESPASYNCWEBSERVER_NMEA2000_H
26
27#include "ServerConfig.h"
28
29#if DETWS_ENABLE_NMEA2000
30
31#include "services/j1939/j1939.h" // reuses the J1939 29-bit identifier codec
33#include <stddef.h>
34#include <stdint.h>
35
36#define N2K_FP_SEQ_SHIFT 5 ///< control octet: sequence counter in bits 7..5
37#define N2K_FP_FRAME_MASK 0x1Fu ///< control octet: frame counter in bits 4..0
38#define N2K_FP_F0_DATA 6u ///< data octets in the first frame (after control + length octets)
39#define N2K_FP_FN_DATA 7u ///< data octets in a continuation frame
40
41/** @brief Result of feeding a frame to the Fast Packet reassembler. */
42enum class N2kFpResult : uint8_t
43{
44 N2K_FP_IGNORED = 0, ///< not part of the active sequence
45 N2K_FP_STARTED, ///< first frame opened a sequence
46 N2K_FP_PROGRESS, ///< a continuation frame was accepted
47 N2K_FP_COMPLETE, ///< the message is fully reassembled
48 N2K_FP_ERR, ///< out-of-order / too large
49};
50
51/** @brief Fast Packet reassembly context (one in-flight message). */
52struct N2kFastPacketRx
53{
54 bool active;
55 uint8_t seq; ///< sequence counter of the in-progress message
56 uint8_t sa; ///< source address
57 uint32_t pgn; ///< the message PGN
58 uint16_t total_len; ///< announced total length
59 uint16_t received; ///< octets stored so far
60 uint8_t next_frame; ///< next expected frame counter
61 uint8_t buf[DETWS_N2K_FP_MAX];
62};
63
64/** @brief Number of Fast Packet frames needed for @p total_len octets. */
65uint8_t n2k_fastpacket_num_frames(uint16_t total_len);
66
67/**
68 * @brief Build Fast Packet frame @p frame_idx (0-based) of a message.
69 * @p seq is the 0..7 sequence counter for this message; @p total_len is the whole payload.
70 */
71bool n2k_fastpacket_build_frame(CanFrame *out, uint8_t seq, uint8_t frame_idx, uint8_t priority, uint32_t pgn,
72 uint8_t sa, uint8_t da, const uint8_t *data, uint16_t total_len);
73
74/** @brief Reset a Fast Packet reassembly context to idle. */
75void n2k_fastpacket_reset(N2kFastPacketRx *rx);
76
77/** @brief Feed a received frame to the Fast Packet reassembler; see @ref N2kFpResult. */
78N2kFpResult n2k_fastpacket_feed(N2kFastPacketRx *rx, const CanFrame *f);
79
80/** @brief Build a single-frame (<= 8 octet) NMEA 2000 message (a thin wrap of J1939). */
81bool n2k_build_single(CanFrame *out, uint8_t priority, uint32_t pgn, uint8_t sa, uint8_t da, const uint8_t *data,
82 uint8_t len);
83
84#endif // DETWS_ENABLE_NMEA2000
85#endif // DETERMINISTICESPASYNCWEBSERVER_NMEA2000_H
User-facing configuration for DeterministicESPAsyncWebServer.
Shared CAN 2.0 frame type for the CAN-based industrial codecs (one source of truth).
SAE J1939 message codec (DETWS_ENABLE_J1939) - the heavy-duty-vehicle / agriculture / marine / genset...
Definition can.h:44