DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
powerlink.cpp
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 powerlink.cpp
6 * @brief Ethernet POWERLINK basic frame codec (see powerlink.h).
7 */
8
10
11#if DETWS_ENABLE_POWERLINK
12
13#include <string.h>
14
15size_t detws_epl_build(uint8_t msg_type, uint8_t dest, uint8_t source, const uint8_t *payload, size_t payload_len,
16 uint8_t *out, size_t cap)
17{
18 if (!out || (payload_len && !payload))
19 return 0;
20 size_t n = 3 + payload_len;
21 if (n > cap)
22 return 0;
23 out[0] = msg_type;
24 out[1] = dest;
25 out[2] = source;
26 if (payload_len)
27 memcpy(out + 3, payload, payload_len);
28 return n;
29}
30
31size_t detws_epl_soc(uint8_t source, uint8_t *out, size_t cap)
32{
33 return detws_epl_build(Epl::EPL_MSG_SOC, Epl::EPL_NODE_BROADCAST, source, nullptr, 0, out, cap);
34}
35
36size_t detws_epl_preq(uint8_t dest_cn, uint8_t source, const uint8_t *pdo, size_t pdo_len, uint8_t *out, size_t cap)
37{
38 return detws_epl_build(Epl::EPL_MSG_PREQ, dest_cn, source, pdo, pdo_len, out, cap);
39}
40
41size_t detws_epl_pres(uint8_t source_cn, const uint8_t *pdo, size_t pdo_len, uint8_t *out, size_t cap)
42{
43 return detws_epl_build(Epl::EPL_MSG_PRES, Epl::EPL_NODE_BROADCAST, source_cn, pdo, pdo_len, out, cap);
44}
45
46bool detws_epl_parse(const uint8_t *frame, size_t len, EplFrame *out)
47{
48 if (!frame || !out || len < 3)
49 return false;
50 uint8_t mt = frame[0];
51 if (mt != Epl::EPL_MSG_SOC && mt != Epl::EPL_MSG_PREQ && mt != Epl::EPL_MSG_PRES && mt != Epl::EPL_MSG_SOA &&
52 mt != Epl::EPL_MSG_ASND)
53 return false;
54 out->msg_type = mt;
55 out->dest = frame[1];
56 out->source = frame[2];
57 out->payload = (len > 3) ? (frame + 3) : nullptr;
58 out->payload_len = len - 3;
59 return true;
60}
61
62#endif // DETWS_ENABLE_POWERLINK