DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
ocit.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 ocit.cpp
6 * @brief OCIT-Outstations message codec (see ocit.h).
7 */
8
10
11#if DETWS_ENABLE_OCIT
12
13#include <string.h>
14
15size_t detws_ocit_build(uint8_t msg_type, uint16_t object_type, uint16_t instance, uint8_t data_type,
16 const uint8_t *value, size_t value_len, uint8_t *out, size_t cap)
17{
18 if (!out || (value_len && !value))
19 return 0;
20 size_t n = 6 + value_len; // msg-type + object-type(2) + instance(2) + data-type + value
21 if (n > cap)
22 return 0;
23 out[0] = msg_type;
24 out[1] = (uint8_t)(object_type >> 8);
25 out[2] = (uint8_t)object_type;
26 out[3] = (uint8_t)(instance >> 8);
27 out[4] = (uint8_t)instance;
28 out[5] = data_type;
29 if (value_len)
30 memcpy(out + 6, value, value_len);
31 return n;
32}
33
34size_t detws_ocit_set_u16(uint16_t object_type, uint16_t instance, uint16_t value, uint8_t *out, size_t cap)
35{
36 uint8_t v[2] = {(uint8_t)(value >> 8), (uint8_t)value};
37 return detws_ocit_build(OcitMsgType::OCIT_MSG_SET, object_type, instance, OcitType::OCIT_TYPE_UINT16, v, 2, out,
38 cap);
39}
40
41bool detws_ocit_parse(const uint8_t *msg, size_t len, OcitMsg *out)
42{
43 if (!msg || !out || len < 6)
44 return false;
45 out->msg_type = msg[0];
46 out->object_type = (uint16_t)((msg[1] << 8) | msg[2]);
47 out->instance = (uint16_t)((msg[3] << 8) | msg[4]);
48 out->data_type = msg[5];
49 out->value = (len > 6) ? (msg + 6) : nullptr;
50 out->value_len = len - 6;
51 return true;
52}
53
54uint16_t detws_ocit_value_u16(const OcitMsg *m)
55{
56 if (!m || m->data_type != OcitType::OCIT_TYPE_UINT16 || m->value_len < 2 || !m->value)
57 return 0;
58 return (uint16_t)((m->value[0] << 8) | m->value[1]);
59}
60
61#endif // DETWS_ENABLE_OCIT
OCIT-Outstations message codec (DETWS_ENABLE_OCIT).