11#if DETWS_ENABLE_BACNET
15size_t bvlc_build(uint8_t *buf,
size_t cap, uint8_t function,
const uint8_t *npdu,
size_t npdu_len)
17 if (!buf || (npdu_len && !npdu))
19 size_t total = BVLC_HEADER_SIZE + npdu_len;
20 if (total > 0xFFFF || total > cap)
22 buf[0] = BVLC_TYPE_BIP;
24 buf[2] = (uint8_t)(total >> 8);
25 buf[3] = (uint8_t)(total & 0xFF);
27 memcpy(buf + BVLC_HEADER_SIZE, npdu, npdu_len);
31bool bvlc_parse(
const uint8_t *buf,
size_t len, uint8_t *function,
const uint8_t **npdu,
size_t *npdu_len)
33 if (!buf || len < BVLC_HEADER_SIZE || buf[0] != BVLC_TYPE_BIP)
35 size_t total = ((size_t)buf[2] << 8) | buf[3];
36 if (total < BVLC_HEADER_SIZE || total > len)
41 *npdu = buf + BVLC_HEADER_SIZE;
43 *npdu_len = total - BVLC_HEADER_SIZE;
47size_t npdu_build(uint8_t *buf,
size_t cap,
bool expecting_reply, uint8_t priority,
bool has_dest, uint16_t dnet,
48 const uint8_t *dadr, uint8_t dadr_len, uint8_t hop_count,
const uint8_t *apdu,
size_t apdu_len)
50 if (!buf || (apdu_len && !apdu) || (dadr_len && !dadr))
52 size_t need = 2 + apdu_len;
54 need += 2 + 1 + dadr_len + 1;
59 buf[p++] = NPDU_VERSION;
60 uint8_t control = (uint8_t)(priority & NPCI_PRIORITY_MASK);
62 control |= NPCI_EXPECTING_REPLY;
64 control |= NPCI_DEST_PRESENT;
68 buf[p++] = (uint8_t)(dnet >> 8);
69 buf[p++] = (uint8_t)(dnet & 0xFF);
73 memcpy(buf + p, dadr, dadr_len);
80 memcpy(buf + p, apdu, apdu_len);
86bool npdu_parse(
const uint8_t *buf,
size_t len, NpduInfo *out)
88 if (!buf || !out || len < 2 || buf[0] != NPDU_VERSION)
90 uint8_t control = buf[1];
93 out->control = control;
94 out->network_message = (control & NPCI_NETWORK_MSG) != 0;
95 out->dest_present = (control & NPCI_DEST_PRESENT) != 0;
96 out->src_present = (control & NPCI_SRC_PRESENT) != 0;
101 if (out->dest_present)
105 out->dnet = (uint16_t)((buf[p] << 8) | buf[p + 1]);
106 uint8_t dlen = buf[p + 2];
111 if (out->src_present)
115 out->snet = (uint16_t)((buf[p] << 8) | buf[p + 1]);
116 uint8_t slen = buf[p + 2];
121 if (out->dest_present)
125 out->hop_count = buf[p++];
128 out->apdu_len = len - p;
BACnet/IP BVLC + NPDU codec (DETWS_ENABLE_BACNET) - zero-heap framing for the ASHRAE 135 building-aut...