EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
acn_pdu.h
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
20 /* etcpal/acn_pdu.h: Functions to parse and pack a generic ACN PDU as defined in ANSI E1.17. */
21 
22 #ifndef ETCPAL_ACN_PDU_H_
23 #define ETCPAL_ACN_PDU_H_
24 
25 #include <stdbool.h>
26 #include <stddef.h>
27 #include <stdint.h>
28 
57 #define ACN_PDU_L_FLAG_SET(flags_byte) ((bool)((flags_byte)&0x80u))
58 
64 #define ACN_PDU_V_FLAG_SET(flags_byte) ((bool)((flags_byte)&0x40u))
65 
71 #define ACN_PDU_H_FLAG_SET(flags_byte) ((bool)((flags_byte)&0x20u))
72 
78 #define ACN_PDU_D_FLAG_SET(flags_byte) ((bool)((flags_byte)&0x10u))
79 
89 #define ACN_PDU_LENGTH(pdu_buf) \
90  ((uint32_t)(ACN_PDU_L_FLAG_SET(pdu_buf[0]) \
91  ? ((uint32_t)((pdu_buf[0] & 0x0fu) << 16) | (uint32_t)(pdu_buf[1] << 8) | (uint32_t)pdu_buf[2]) \
92  : ((uint32_t)((pdu_buf[0] & 0x0fu) << 8) | (uint32_t)pdu_buf[1])))
93 
105 #define ACN_PDU_SET_L_FLAG(flags_byte) (flags_byte |= 0x80u)
106 
111 #define ACN_PDU_SET_V_FLAG(flags_byte) (flags_byte |= 0x40u)
112 
117 #define ACN_PDU_SET_H_FLAG(flags_byte) (flags_byte |= 0x20u)
118 
123 #define ACN_PDU_SET_D_FLAG(flags_byte) (flags_byte |= 0x10u)
124 
137 #define ACN_PDU_PACK_NORMAL_LEN(pdu_buf, length) \
138  do \
139  { \
140  (pdu_buf)[0] = (uint8_t)(((pdu_buf)[0] & 0xf0) | (((length) >> 8) & 0x0fu)); \
141  (pdu_buf)[1] = (uint8_t)(length)&0xffu; \
142  } while (0)
143 
152 #define ACN_PDU_PACK_EXT_LEN(pdu_buf, length) \
153  do \
154  { \
155  (pdu_buf)[0] = (uint8_t)(((pdu_buf)[0] & 0xf0) | (((length) >> 16) & 0x0fu)); \
156  (pdu_buf)[1] = (uint8_t)(((length) >> 8) & 0xffu); \
157  (pdu_buf)[2] = (uint8_t)(length)&0xffu; \
158  } while (0)
159 
161 typedef struct AcnPdu
162 {
163  const uint8_t* pvector;
164  const uint8_t* pheader;
165  const uint8_t* pdata;
166  size_t data_len;
167  const uint8_t* pnextpdu;
169 
172 #define ACN_PDU_INIT \
173  { \
174  NULL, NULL, NULL, 0, NULL \
175  }
176 
183 #define ACN_INIT_PDU(pduptr) \
184  do \
185  { \
186  (pduptr)->pvector = NULL; \
187  (pduptr)->pheader = NULL; \
188  (pduptr)->pdata = NULL; \
189  (pduptr)->data_len = 0; \
190  (pduptr)->pnextpdu = NULL; \
191  } while (0)
192 
194 typedef struct AcnPduConstraints
195 {
196  size_t vector_size;
197  size_t header_size;
199 
200 #ifdef __cplusplus
201 extern "C" {
202 #endif
203 
204 bool acn_parse_pdu(const uint8_t* buf, size_t buflen, const AcnPduConstraints* constraints, AcnPdu* pdu);
205 
206 #ifdef __cplusplus
207 }
208 #endif
209 
214 #endif /* ETCPAL_ACN_PDU_H_ */
struct AcnPdu AcnPdu
Holds state data used when parsing multiple PDUs in a PDU block.
struct AcnPduConstraints AcnPduConstraints
Contains specific PDU info used by the generic helper acn_parse_pdu().
bool acn_parse_pdu(const uint8_t *buf, size_t buflen, const AcnPduConstraints *constraints, AcnPdu *pdu)
Parse a generic ACN PDU.
Definition: acn_pdu.c:35
Contains specific PDU info used by the generic helper acn_parse_pdu().
Definition: acn_pdu.h:195
size_t vector_size
The size of the Vector segment of this PDU.
Definition: acn_pdu.h:196
size_t header_size
The size of the Header segment of this PDU.
Definition: acn_pdu.h:197
Holds state data used when parsing multiple PDUs in a PDU block.
Definition: acn_pdu.h:162
const uint8_t * pvector
Pointer to the most recent PDU's vector.
Definition: acn_pdu.h:163
const uint8_t * pdata
Pointer to the most recent PDU's data.
Definition: acn_pdu.h:165
const uint8_t * pnextpdu
Pointer to the beginning of the next PDU.
Definition: acn_pdu.h:167
const uint8_t * pheader
Pointer to the most recent PDU's header.
Definition: acn_pdu.h:164
size_t data_len
Length of the PDU's data section.
Definition: acn_pdu.h:166