lwpa  0.1.0
LightWeight Platform Abstraction (lwpa)
View other versions:
lwpa_pdu.h
1 /******************************************************************************
2  * Copyright 2018 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 lwpa. For more information, go to:
17  * https://github.com/ETCLabs/lwpa
18  ******************************************************************************/
19 
20 /* pdu.h: Functions to parse and pack a generic PDU as defined in ANSI
21  * E1.17. */
22 #ifndef _LWPA_PDU_H_
23 #define _LWPA_PDU_H_
24 
25 #include <stddef.h>
26 #include "lwpa_int.h"
27 #include "lwpa_bool.h"
28 
47 #define l_flag_set(flags_byte) ((bool)(flags_byte & 0x80))
51 #define v_flag_set(flags_byte) ((bool)(flags_byte & 0x40))
55 #define h_flag_set(flags_byte) ((bool)(flags_byte & 0x20))
59 #define d_flag_set(flags_byte) ((bool)(flags_byte & 0x10))
65 #define pdu_length(pdu_buf) \
66  ((uint32_t)(l_flag_set(pdu_buf[0]) ? (((pdu_buf[0] & 0x0f) << 16) | (pdu_buf[1] << 8) | pdu_buf[2]) \
67  : (((pdu_buf[0] & 0x0f) << 8) | pdu_buf[1])))
68 
77 #define set_l_flag(flags_byte) (flags_byte |= 0x80)
80 #define set_v_flag(flags_byte) (flags_byte |= 0x40)
83 #define set_h_flag(flags_byte) (flags_byte |= 0x20)
86 #define set_d_flag(flags_byte) (flags_byte |= 0x10)
94 #define pdu_pack_normal_len(pdu_buf, length) \
95  do \
96  { \
97  (pdu_buf)[0] = (((pdu_buf)[0] & 0xf0) | (((length) >> 8) & 0x0f)); \
98  (pdu_buf)[1] = (length)&0xff; \
99  } while (0)
100 
106 #define pdu_pack_ext_len(pdu_buf, length) \
107  do \
108  { \
109  (pdu_buf)[0] = (((pdu_buf)[0] & 0xf0) | (((length) >> 16) & 0x0f)); \
110  (pdu_buf)[1] = (((length) >> 8) & 0xff); \
111  (pdu_buf)[2] = (length)&0xff; \
112  } while (0)
113 
115 typedef struct LwpaPdu
116 {
117  const uint8_t *pvector;
118  const uint8_t *pheader;
119  const uint8_t *pdata;
120  size_t datalen;
121  const uint8_t *pnextpdu;
123 
126 #define PDU_INIT \
127  { \
128  NULL, NULL, NULL, 0, NULL \
129  }
130 
134 #define init_pdu(pduptr) \
135  do \
136  { \
137  (pduptr)->pvector = NULL; \
138  (pduptr)->pheader = NULL; \
139  (pduptr)->pdata = NULL; \
140  (pduptr)->datalen = 0; \
141  (pduptr)->pnextpdu = NULL; \
142  } while (0)
143 
145 typedef struct LwpaPduConstraints
146 {
147  size_t vector_size;
148  size_t header_size;
150 
151 #ifdef __cplusplus
152 extern "C" {
153 #endif
154 
155 bool parse_pdu(const uint8_t *buf, size_t buflen, const LwpaPduConstraints *constraints, LwpaPdu *pdu);
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
163 #endif /* _LWPA_PDU_H_ */
bool parse_pdu(const uint8_t *buf, size_t buflen, const LwpaPduConstraints *constraints, LwpaPdu *pdu)
Parse a generic PDU.
Definition: lwpa_pdu.c:37
struct LwpaPduConstraints LwpaPduConstraints
Contains specific PDU info used by the generic helper parse_pdu().
struct LwpaPdu LwpaPdu
Holds state data used when parsing multiple PDUs in a PDU block.
Contains specific PDU info used by the generic helper parse_pdu().
Definition: lwpa_pdu.h:146
size_t vector_size
The size of the Vector segment of this PDU.
Definition: lwpa_pdu.h:147
size_t header_size
The size of the Header segment of this PDU.
Definition: lwpa_pdu.h:148
Holds state data used when parsing multiple PDUs in a PDU block.
Definition: lwpa_pdu.h:116