ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
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 PC_ENABLE_OCIT
12
13#include <string.h>
14
15size_t pc_ocit_build(uint8_t msg_type, uint16_t object_type, uint16_t instance, uint8_t data_type, const uint8_t *value,
16 size_t value_len, uint8_t *out, size_t cap)
17{
18 if (!out || (value_len && !value))
19 {
20 return 0;
21 }
22 size_t n = 6 + value_len; // msg-type + object-type(2) + instance(2) + data-type + value
23 if (n > cap)
24 {
25 return 0;
26 }
27 out[0] = msg_type;
28 out[1] = (uint8_t)(object_type >> 8);
29 out[2] = (uint8_t)object_type;
30 out[3] = (uint8_t)(instance >> 8);
31 out[4] = (uint8_t)instance;
32 out[5] = data_type;
33 if (value_len)
34 {
35 memcpy(out + 6, value, value_len);
36 }
37 return n;
38}
39
40size_t pc_ocit_set_u16(uint16_t object_type, uint16_t instance, uint16_t value, uint8_t *out, size_t cap)
41{
42 uint8_t v[2] = {(uint8_t)(value >> 8), (uint8_t)value};
43 return pc_ocit_build(OcitMsgType::OCIT_MSG_SET, object_type, instance, OcitType::OCIT_TYPE_UINT16, v, 2, out, cap);
44}
45
46bool pc_ocit_parse(const uint8_t *msg, size_t len, OcitMsg *out)
47{
48 if (!msg || !out || len < 6)
49 {
50 return false;
51 }
52 out->msg_type = msg[0];
53 out->object_type = (uint16_t)((msg[1] << 8) | msg[2]);
54 out->instance = (uint16_t)((msg[3] << 8) | msg[4]);
55 out->data_type = msg[5];
56 out->value = (len > 6) ? (msg + 6) : nullptr;
57 out->value_len = len - 6;
58 return true;
59}
60
61uint16_t pc_ocit_value_u16(const OcitMsg *m)
62{
63 if (!m || m->data_type != OcitType::OCIT_TYPE_UINT16 || m->value_len < 2 || !m->value)
64 {
65 return 0;
66 }
67 return (uint16_t)((m->value[0] << 8) | m->value[1]);
68}
69
70#endif // PC_ENABLE_OCIT
OCIT-Outstations message codec (PC_ENABLE_OCIT).