blob: 47956c34a468c26a81fb7bf98d00a1ec62d3108c [file] [log] [blame]
Ivan Kluchnikov487a1412011-12-21 13:17:53 +03001/* csn1.h
2 * Declarations and types for CSN1 dissection in wireshark.
3 * By Vincent Helfre, based on original code by Jari Sassi
4 * with the gracious authorization of STE
5 * Copyright (c) 2011 ST-Ericsson
6 *
7 * $Id: packet-csn1.h 36306 2011-03-24 09:20:14Z etxrab $
8 *
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030022 */
23
Max910a3872018-01-30 13:29:38 +010024#ifndef _PACKET_CSN1_H_
25#define _PACKET_CSN1_H_
26
Alexander Couzensccde5c92017-02-04 03:10:08 +010027#include <osmocom/core/bitvec.h>
Pau Espin Pedrolb2653fe2020-03-26 15:14:01 +010028#include "wireshark_compat.h"
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030029
30/* Error codes */
31#define CSN_OK 0
32#define CSN_ERROR_GENERAL -1
33#define CSN_ERROR_DATA_NOT_VALID -2
34#define CSN_ERROR_IN_SCRIPT -3
35#define CSN_ERROR_INVALID_UNION_INDEX -4
36#define CSN_ERROR_NEED_MORE_BITS_TO_UNPACK -5
37#define CSN_ERROR_ILLEGAL_BIT_VALUE -6
38#define CSN_ERROR_INTERNAL -7
39#define CSN_ERROR_STREAM_NOT_SUPPORTED -8
40#define CSN_ERROR_MESSAGE_TOO_LONG -9
41#define CSN_ERROR_ -10
42
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030043/* CallBack return status */
44typedef gint16 CSN_CallBackStatus_t;
45
46#define CSNCBS_OK 0
47#define CSNCBS_NOT_OK -10
48#define CSNCBS_NOT_TO_US -11
49#define CSNCBS_NOT_COMPLETE -12
50
51#define CSNCBS_REVISION_LIMIT_STOP -20 /* Stop packing/unpacking - revision limit */
52#define CSNCBS_NOT_SUPPORTED_IE -21 /* Handling of the unpacked IE is not supported by MS-software */
53
54
55
56#ifndef ElementsOf
57#define ElementsOf(array) (sizeof(array) / sizeof(array[0]))
58#endif
Pau Espin Pedrol5b716972020-01-24 17:24:01 +010059typedef void(*void_fn_t)(void);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030060
61/* Context holding CSN1 parameters */
62typedef struct
63{
64 gint remaining_bits_len; /* IN to an csn stream operation */
65 gint bit_offset; /* IN/OUT to an csn stream operation */
66 gint direction; /* 0 - decode; 1 - encode */
67} csnStream_t;
68
Vadim Yanitskiy39a65052020-01-25 01:24:59 +070069typedef gint16 (*StreamSerializeFcn_t)(csnStream_t* ar, struct bitvec *vector, unsigned *readIndex, void* data);
Pau Espin Pedrolf3ac06b2020-03-26 18:13:19 +010070typedef CSN_CallBackStatus_t (*DissectorCallbackFcn_t)(struct bitvec *vector, unsigned *readIndex, void* param1, void* param2);
71
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030072typedef enum
73{
74 CSN_END = 0,
75 CSN_BIT,
76 CSN_UINT,
77 CSN_TYPE,
78 CSN_CHOICE,
79 CSN_UNION,
80 CSN_UNION_LH,
81 CSN_UINT_ARRAY,
82 CSN_TYPE_ARRAY,
83 CSN_BITMAP, /* Bitmap with constant: <bitmap: bit(64)> */
84 CSN_VARIABLE_BITMAP, /* <N: bit (5)> <bitmap: bit(N + offset)> */
85 CSN_VARIABLE_BITMAP_1, /* <bitmap: bit**> i.e. to the end of message (R99) */
86 CSN_LEFT_ALIGNED_VAR_BMP, /* As variable bitmap but the result is left aligned (R99) */
87 CSN_LEFT_ALIGNED_VAR_BMP_1,/* As above only size is to the end of message (R99) */
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +040088 CSN_PADDING_BITS, /* Padding bits fill to the end of the buffer */
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030089 CSN_VARIABLE_ARRAY, /* Array with length specified in parameter: <N: bit(4)> <list: octet(N + offset)> */
90 CSN_VARIABLE_TARRAY, /* Type Array with length specified in parameter: <N: bit(x)> <Type>*N */
91 CSN_VARIABLE_TARRAY_OFFSET,/* As above but with offset. The offset is stored as third parameter of CSN_DESCR (descr.value) */
92 CSN_RECURSIVE_ARRAY, /* Recursive way to specify an array of uint: <list> ::= {1 <number: bit(4) <list>|0}; */
93 CSN_RECURSIVE_TARRAY, /* Recursive way to specify an array of type: <list> ::= {1 <type>} ** 0 ; */
94 CSN_RECURSIVE_TARRAY_1, /* same as above but first element always exist:<list> ::= <type> {1 <type>} ** 0 ; */
95 CSN_RECURSIVE_TARRAY_2, /* same as above but with reversed separators :<lists> ::= <type> { 0 <type> } ** 1 ; */
96 CSN_EXIST,
97 CSN_EXIST_LH,
98 CSN_NEXT_EXIST,
99 CSN_NEXT_EXIST_LH,
100 CSN_NULL,
101 CSN_FIXED,
102 CSN_CALLBACK,
103 CSN_UINT_OFFSET, /* unpack will add offset, inverse pack will subtract offset */
104 CSN_UINT_LH, /* Low High extraction of int */
105 CSN_SERIALIZE,
106 CSN_TRAP_ERROR
107} csn_type_t;
108
109/******************************************************************************************
110 * CSN_DESCR structure:
111 *
112 * type:
113 * This is the CSN type. All existing types are specified in the section above.
114 *
115 * i:
116 * Depending on the contents of the type parameter, the parameter "i" may have following meaning:
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400117 * - specifies the number of bits for the CSN_UINT or CSN_UINT_OR_NULL types
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300118 * - the offset for an array size by which the size is incremented
119 * for the CSN_VAR_ARRAY type
120 * - the length of each element of an array for the CSN_REC_ARRAY type
121 * - the number of the elements in an array for the CSN_TYPE_ARRAY type
122 * - the offset to the variable keeping the number of elements of an array for in the CSN_VAR_TARRAY type
123 * - the number of different data types in a union for the CSN_UNION, CSN_UNION_LH, and for the CSN_CHOICE types
124 * - the length in bits of the fixed number defined for the CSN_FIXED type
125 * - the number of lines to skip in the CSN_DESCR type specified for the CSN_NEXT_EXIST, CSN_NEXT_EXIST_LH,
126 * CSN_NEXT_EXIST_OR_NULL, and CSN_NEXT_EXIST_OR_NULL_LH types
127 * - the number of bits in a bitmap for the CSN_BITMAP type
128 * - the value by which the number of bits in a bitmap has to be incremented or decremented for the
129 * CSN_VAR_BITMAP, CSN_LEFT_VAR_BMP, and CSN_LEFT_BMP_1 types
130 * - the offset to param1 for the CSN_CALLBACK type
131 * - ERRORCODE used by the CSN_ERROR type
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400132 * - the bit-length of the LENGTH field in a CSN_SERIALISE type
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300133 *
134 * descr
135 * This parameter has different meaning depending on the value of the type parameter:
136 * - the offset for the CSN_UINT_OFFSET type
137 * - the number of the elements in an array of the CSN_UINT_ARRAY type
138 * - the offset to the parameter where the size of the array has to be stored for the CSN_REC_ARRAY type
139 * - the address of the internal structure, describing the member type (by means of the CSN_DESCR type) in the
140 * CSN_TYPE_ARRAY, CSN_VAR_TARRAY, and CSN_TYPE types
141 * - the address of the variable of type CSN_ChoiceElement_t describing all elements in the CSN_CHOICE type union
142 * - the offset to the variable where the number of bits has to be or is stored for the CSN_VAR_BITMAP,
143 * CSN_LEFT_VAR_BMP, and CSN_LEFT_BMP_1 types
144 * - the function number (case number) for the CSN_CALLBACK and CSN_CALLBACK_NO_ARGS types
145 * - the free text used by the CSN_TRAP_ERROR
146 *
147 * offset
148 * This is an offset to the _MEMBER parameter counting from the beginning of struct
149 * where the unpacked or packed value shall be stored or fetched. The meaning of the _MEMBER parameter
150 * varies depending on the type which is specified and so is the meaning of the offset parameter.
151 * Some types (and corresponding macros) do not have the _MEMBER parameter and then the offset parameter
152 * is not used or is different from the offset to the _MEMBER.
153 * - the fixed value for the CSN_FIXED type
154 * - an offset to the variable UnionType for CSN_UNION and CSN_UNION_LH types
155 * - an offset to the variable Exist for CSN_NEXT_EXIST and CSN_NEXT_EXIST_LH types
156 * - an offset to param2 in the CSN_CALLBACK type
157 *
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400158 * may_be_null
159 * TRUE: if dissection may be attempted at an offset beyond the length of existing data bits
160 * FALSE: othewise
161 *
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300162 * sz
163 * - is the name of the parameter within the descr where their unpacked or packed value shall be stored or fetched.
164 * This paramater is pointed out by the offset parameter in the same CSN_DESCR variable as the sz.
165 * - the free text used by the CSN_TRAP_ERROR (the same as parameter "i")
166 *
167 * serialize
168 * - stores the size of _MEMBER type in case of the M_TYPE_ARRAY and M_VAR_TARRAY,
169 * - the address of the function which is provided by the M_SERIALIZE type.
170 ******************************************************************************************/
171
172
173typedef struct
174{
175 gint16 type;
176 gint16 i;
177 union
178 {
Pau Espin Pedrol5b716972020-01-24 17:24:01 +0100179 const void* ptr;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300180 guint32 value;
181 } descr;
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +0400182 unsigned offset;
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400183 gboolean may_be_null;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300184 const char* sz;
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100185 guint32 value;
Pau Espin Pedrol5b716972020-01-24 17:24:01 +0100186 void_fn_t aux_fn;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300187} CSN_DESCR;
188
189typedef struct
190{
191 guint8 bits;
192 guint8 value;
Pau Espin Pedrol7cce8252020-01-24 16:41:14 +0100193 gboolean keep_bits;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300194 CSN_DESCR descr;
195} CSN_ChoiceElement_t;
196
197void csnStreamInit(csnStream_t* ar,gint BitOffset,gint BitCount);
198
199/******************************************************************************
200* FUNCTION: csnStreamDecoder
201* DESCRIPTION:
202* UnPacks data from bit stream. According to CSN description.
203* ARGS:
204* ar stream will hold the parameters to the pack function
205* ar->remaining_bits_len [IN] Number of bits to unpack [OUT] number of bits left to unpack.
206* ar->bit_offset [IN/OUT] is the current bit where to proceed with the next bit to unpack.
207
208* pDescr CSN description.
209* tvb buffer containing the bit stream to unpack.
210* data unpacked data.
211* ett_csn1 tree
212*
213* RETURNS: int Number of bits left to be unpacked. Negative Error code if failed to unpack all bits
214******************************************************************************/
215
Vadim Yanitskiy39a65052020-01-25 01:24:59 +0700216gint16 csnStreamDecoder(csnStream_t* ar, const CSN_DESCR* pDescr, struct bitvec *vector, unsigned *readIndex, void* data);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300217
Vadim Yanitskiy39a65052020-01-25 01:24:59 +0700218gint16 csnStreamEncoder(csnStream_t* ar, const CSN_DESCR* pDescr, struct bitvec *vector, unsigned *writeIndex, void* data);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300219
220/* CSN struct macro's */
221#define CSN_DESCR_BEGIN(_STRUCT)\
222 CSN_DESCR CSNDESCR_##_STRUCT[] = {
223
224#define CSN_DESCR_END(_STRUCT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100225 {CSN_END, 0, {0}, 0, FALSE, "", 0, NULL} };
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300226
227/******************************************************************************
228 * CSN_ERROR(Par1, Par2, Par3)
229 * May be called at any time when an abort of packing or unpacking of a message
230 * is desired
231 * Par1: C structure name
232 * Par2: free text which will appear in the error handler
233 * Par3: Error code
234 *****************************************************************************/
235#define CSN_ERROR(_STRUCT, _Text, _ERRCODE)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100236 {CSN_TRAP_ERROR, _ERRCODE, {_Text}, 0, FALSE, _Text, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300237
238/******************************************************************************
239 * M_BIT(Par1, Par2)
240 * Defines one bit element in the CSN1 syntax.
241 * Par1: C structure name
242 * Par2: C structure element name
243 *****************************************************************************/
244#define M_BIT(_STRUCT, _MEMBER)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100245 {CSN_BIT, 0, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400246
247/******************************************************************************
248 * M_BIT_OR_NULL(Par1, Par2)
249 * Similar to the M_BIT except that not only bit 0 or 1 but also the
250 * end of the message may be encountered when looking for the next element in
251 * the message.
252 * Covers the case {null | 0 | 1}
253 *****************************************************************************/
254 #define M_BIT_OR_NULL(_STRUCT, _MEMBER)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100255 {CSN_BIT, 0, {0}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300256
257/******************************************************************************
258 * M_NEXT_EXIST(Par1, Par2, Par3)
Pau Espin Pedrol5b716972020-01-24 17:24:01 +0100259 * Indicates whether the next element or a group of elements defined in the
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300260 * structure is present or not.
261 * Par1: C structure name
262 * Par2: C structure element name
263 * Par3: number of lines to skip in the CSN_DESCR type specified if the
264 * element(s) does not exist
265 *****************************************************************************/
266#define M_NEXT_EXIST(_STRUCT, _MEMBER, _NoOfExisting)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100267 {CSN_NEXT_EXIST, _NoOfExisting, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300268
269/******************************************************************************
270 * M_NEXT_EXIST_LH(Par1, Par2, Par3)
271 * similar to the M_NEXT_EXIST except that instead of bit 0/1 which is fetched
272 * from the message in order to find out whether the next element/elements are
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100273 * present in the message, the logical operation XOR with the background
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300274 * pattern 0x2B is performed on the read bit before the decision is made.
275 *****************************************************************************/
276#define M_NEXT_EXIST_LH(_STRUCT, _MEMBER, _NoOfExisting)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100277 {CSN_NEXT_EXIST_LH, _NoOfExisting, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300278
279/******************************************************************************
280 * M_NEXT_EXIST_OR_NULL(Par1, Par2, Par3)
281 * Similar to the M_NEXT_EXIST except that not only bit 0 or 1 but also the end
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100282 * of the message may be encountered when looking for the next element in the
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300283 * message.
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100284 * Covers the case {null | 0 | 1 < IE >}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300285 *****************************************************************************/
286#define M_NEXT_EXIST_OR_NULL(_STRUCT, _MEMBER, _NoOfExisting)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100287 {CSN_NEXT_EXIST, _NoOfExisting, {0}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300288
289/******************************************************************************
290 * M_NEXT_EXIST_OR_NULL_LH(Par1, Par2, Par3)
291 * Similar to the M_NEXT_EXIST_LH except that not only bit 0 or 1 but also the
292 * end of the message may be encountered when looking for the next element in
293 * the message.
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100294 * Covers the case {null | L | H < IE >}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300295 *****************************************************************************/
296#define M_NEXT_EXIST_OR_NULL_LH(_STRUCT, _MEMBER, _NoOfExisting)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100297 {CSN_NEXT_EXIST_LH, _NoOfExisting, {(void*)1}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300298
299/******************************************************************************
300 * M_UINT(Par1, Par2, Par3)
301 * Defines an integer number.
302 * Par1: C structure name
303 * Par2: C structure element name
304 * Par3: number of bits used to code the element (between 1 and 32)
305 *****************************************************************************/
306#define M_UINT(_STRUCT, _MEMBER, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100307 {CSN_UINT, _BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400308
309 /******************************************************************************
310 * M_UINT_OR_NULL(Par1, Par2, Par3)
311 * Similar to the M_UINT except that not only the request set of bits but also the
312 * end of the message may be encountered when looking for the next element in
313 * the message.
314 * Covers the case {null | 0 | 1 < IE >}
315 *****************************************************************************/
316 #define M_UINT_OR_NULL(_STRUCT, _MEMBER, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100317 {CSN_UINT, _BITS, {0}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300318
319/******************************************************************************
320 * M_UINT(Par1, Par2, Par3)
321 * This macro has the same functionality as M_UINT except that in addition the
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100322 * logical "exclusive or" operation with the background value "0x2B" is
323 * performed before the final value of the integer number is delivered from the
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300324 * received CSN.1 message
325 *****************************************************************************/
326#define M_UINT_LH(_STRUCT, _MEMBER, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100327 {CSN_UINT_LH, _BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300328
329/******************************************************************************
330 * M_UINT_OFFSET(Par1, Par2, Par3, Par4)
331 * Defines an integer number.
332 * Par1: C structure name
333 * Par2: C structure element name
334 * Par3: number of bits used to code the element (between 1 and 32)
335 * Par4: value added to the returned integer (offset)
336 *****************************************************************************/
337#define M_UINT_OFFSET(_STRUCT, _MEMBER, _BITS, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100338 {CSN_UINT_OFFSET, _BITS, {(void*)_OFFSET}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300339
340/******************************************************************************
341 * M_UINT_ARRAY(Par1, Par2, Par3, Par4)
342 * Defines an array of integer numbers. The size of the array is fixed.
343 * Par1: C structure name
344 * Par2: C structure element name
345 * Par3: number of bits used to code the each integer element (between 1 and 32)
346 * Par4: number of elements in the array (fixed integer value)
347 *****************************************************************************/
348#define M_UINT_ARRAY(_STRUCT, _MEMBER, _BITS, _ElementCount)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100349 {CSN_UINT_ARRAY, _BITS, {(void*)_ElementCount}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300350
351/******************************************************************************
352 * M_VAR_UINT_ARRAY(Par1, Par2, Par3, Par4)
353 * Defines an array of integer numbers. The size of the array is variable.
354 * Par1: C structure name
355 * Par2: C structure element name
356 * Par3: number of bits used to code the each integer element (between 1 and 32)
357 * Par4: number of elements in the array supplied by reference to the
358 * structure member holding the length value
359 *****************************************************************************/
360#define M_VAR_UINT_ARRAY(_STRUCT, _MEMBER, _BITS, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100361 {CSN_UINT_ARRAY, _BITS, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 1, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300362
363/******************************************************************************
364 * M_VAR_ARRAY(Par1, Par2, Par3, Par4)
365 * Defines an array of 8 bit large integer numbers. The size of the array is variable.
366 * Par1: C structure name
367 * Par2: C structure element name
368 * Par3: name of the structure member holding the size of the array
369 * Par4: offset that is added to the Par3 to get the actual size of the array
370 *****************************************************************************/
371#define M_VAR_ARRAY(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100372 {CSN_VARIABLE_ARRAY, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300373
374/******************************************************************************
375 * M_VAR_TARRAY(Par1, Par2, Par3, Par4)
376 * Similar to M_TYPE_ARRAY except that the size of the array is variable.
377 * Par1: C structure name
378 * Par2: C structure element name
379 * Par3: the type of each element of the array
380 * Par4: name of the structure member holding the size of the array
381 *****************************************************************************/
382#define M_VAR_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100383 {CSN_VARIABLE_TARRAY, offsetof(_STRUCT, _ElementCountField), {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, sizeof(_MEMBER_TYPE), NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300384
385/******************************************************************************
386 * M_VAR_TARRAY_OFFSET(Par1, Par2, Par3, Par4)
387 * Same as M_VAR_TARRAY with offset
388 *****************************************************************************/
389#define M_VAR_TARRAY_OFFSET(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100390 {CSN_VARIABLE_TARRAY_OFFSET, offsetof(_STRUCT, _ElementCountField), {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, sizeof(_MEMBER_TYPE), NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300391
392/******************************************************************************
393 * M_REC_ARRAY(Par1, Par2, Par3, Par4)
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100394 * similar to the M_VAR_ARRAY. The difference is that the size of the array is
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300395 * not known in advance and it has to be calculated during unpacking. Its value
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100396 * is stored in a variable which belongs to the same structure as the array.
397 * A zero element terminates the array. The CSN.1 syntax describes it
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300398 * recursively as:
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100399 * <array> ::={1 <element> <array>| 0}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300400 *
401 * Par1: C structure name
402 * Par2: C structure element name
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100403 * Par3: name of the structure member where the calculated the size of the
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300404 * array will be stored
405 * Par4: length of each element in bits
406 *****************************************************************************/
407#define M_REC_ARRAY(_STRUCT, _MEMBER, _ElementCountField, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100408 {CSN_RECURSIVE_ARRAY, _BITS, {(const void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300409
410/******************************************************************************
411 * M_VAR_TYPE_ARRAY(Par1, Par2, Par3, Par4)
412 * Defines an array of structures. The size of the array is variable.
413 * Par1: C structure name
414 * Par2: C structure element name
415 * Par3: name of the structure
416 * Par4: number of elements in the array (fixed integer value)
417 *****************************************************************************/
418#define M_TYPE_ARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCount)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100419 {CSN_TYPE_ARRAY, _ElementCount, {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, sizeof(_MEMBER_TYPE), NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300420
421/******************************************************************************
422 * M_REC_TARRAY(Par1, Par2, Par3, Par4)
423 * Defines an recursive array of structures. The size of the array is variable.
424 * <list> ::= {1 <type>} ** 0 ;
425 * Par1: C structure name
426 * Par2: C structure element name
427 * Par3: name of the structure
428 * Par4: will hold the number of element in the array after unpacking
429 *****************************************************************************/
430#define M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrolefad80b2020-03-23 14:35:26 +0100431 {CSN_RECURSIVE_TARRAY, offsetof(_STRUCT, _ElementCountField), {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, sizeof(_MEMBER_TYPE), (void_fn_t)ElementsOf(((_STRUCT*)0)->_MEMBER)}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300432
433/******************************************************************************
434 * M_REC_TARRAY1(Par1, Par2, Par3, Par4)
435 * Same as M_REC_TARRAY but first element always exist:
436 * <list> ::= <type> {1 <type>} ** 0 ;
437 *****************************************************************************/
438#define M_REC_TARRAY_1(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrolefad80b2020-03-23 14:35:26 +0100439 {CSN_RECURSIVE_TARRAY_1, offsetof(_STRUCT, _ElementCountField), {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, sizeof(_MEMBER_TYPE), (void_fn_t)ElementsOf(((_STRUCT*)0)->_MEMBER)}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300440
441/******************************************************************************
442 * M_REC_TARRAY2(Par1, Par2, Par3, Par4)
443 * Same as M_REC_TARRAY but with reversed separators :
444 * <lists> ::= <type> { 0 <type> } ** 1 ;
445 *****************************************************************************/
446#define M_REC_TARRAY_2(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrolefad80b2020-03-23 14:35:26 +0100447 {CSN_RECURSIVE_TARRAY_2, offsetof(_STRUCT, _ElementCountField), {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, sizeof(_MEMBER_TYPE), (void_fn_t)ElementsOf(((_STRUCT*)0)->_MEMBER)}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300448
449/******************************************************************************
450 * M_TYPE(Par1, Par2, Par3)
451 * Defines a reference to a structure which is described elsewhere
452 * <list> ::= {1 <type>} ** 0 ;
453 * Par1: C structure name
454 * Par2: C structure element name
455 * Par3: type of member
456 *****************************************************************************/
457#define M_TYPE(_STRUCT, _MEMBER, _MEMBER_TYPE)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100458 {CSN_TYPE, 0, {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300459
460/******************************************************************************
Pau Espin Pedrolebdc0d82021-10-19 16:48:16 +0200461 * M_TYPE_OR_NULL(Par1, Par2, Par3)
462 * Similar to the M_TYPE except that not only the request set of bits but also the
463 * end of the message may be encountered when looking for the next element in
464 * the message.
465 * Covers the case {null | 0 | 1 < IE >}
466 *****************************************************************************/
467#define M_TYPE_OR_NULL(_STRUCT, _MEMBER, _MEMBER_TYPE)\
468 {CSN_TYPE, 0, {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
469
470/******************************************************************************
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300471 * M_UNION(Par1, Par2)
472 * Informs the CSN.1 library that a union follows and how many possible choices
473 * there are in the union. The actual value of the choice, which points out the
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100474 * chosen element of the union is stored in the uint8 variable and is usually
475 * called UnionType. The elements of the union have to be listed directly after
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300476 * the M_UNION statement.
477 * Par1: C structure name
478 * Par2: number of possible choice in the union
479 *****************************************************************************/
480#define M_UNION(_STRUCT, _COUNT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100481 {CSN_UNION, _COUNT, {0}, offsetof(_STRUCT, UnionType), FALSE, "UnionType", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300482
483/******************************************************************************
484 * M_UNION_LH(Par1, Par2)
485 * Same as M_UNION but masked with background value 0x2B
486 *****************************************************************************/
487#define M_UNION_LH(_STRUCT, _COUNT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100488 {CSN_UNION_LH, _COUNT, {0}, offsetof(_STRUCT, UnionType), FALSE, "UnionType", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300489
490/******************************************************************************
491 * M_CHOICE(Par1, Par2, Par3, Par4)
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100492 * Similar to the M_UNION. In the M_UNION the selected element of all possible
493 * choices in the union is referred as a sequential numbers, i.e., the first
494 * choice is addressed as choice 0 the second as choice 1, the third as choice
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300495 * 2 and so on, both in the encoded message and in the variable UnionType which
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100496 * is the part of the message. In the CSN_CHOICE case, this rule does not
497 * apply. There is free but predefined mapping of the element of the union and
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300498 * the value which addresses this element.
Vadim Yanitskiyc9915662020-05-23 17:24:37 +0700499 * The value of the address is called a selector. Up to 256 (UCHAR_MAX) unique
500 * selectors can be handled, longer choice list would cause CSN_ERROR_IN_SCRIPT.
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300501 * After unpacking, this value is then converted to the sequential number of the
Vadim Yanitskiy93ad3fd2020-05-23 18:17:19 +0700502 * element in the union and stored in the UnionType variable (Par2).
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300503 * Par1: C structure name
Vadim Yanitskiy93ad3fd2020-05-23 18:17:19 +0700504 * Par2: C structure field name holding sequential number of the chosen element.
505 * BEWARE! Never use an enumerated type here, because its length is
506 * compiler/machine dependent, while decoder would cast it to guint8.
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300507 * Par3: address of an array of type CSN_ChoiceElement_t where all possible
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100508 * values of the selector are provided, together with the selector
509 * length expressed in bits and the address of the CSN_DESCR type
510 * where the element is defined. For every element in the union
511 * there is one line in the Choice variable. These lines have to
512 * appear in the _CHOICE in the same order as the elements in the
513 * union. The element of the union selected in the message through
514 * the _CHOICE parameter is after unpacking translated to the
515 * corresponding sequential number of this element and stored in
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300516 * the variable pointed out by the _MEMBER
517 * Par4: number of possible choices in the union
518 *****************************************************************************/
519#define M_CHOICE(_STRUCT, _MEMBER, _CHOICE, _ElementCount)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100520 {CSN_CHOICE, _ElementCount, {(const void*)_CHOICE}, offsetof(_STRUCT, _MEMBER), FALSE, #_CHOICE, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300521
522/******************************************************************************
523 * M_FIXED(Par1, Par2, Par3)
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100524 * Defines a fixed value of type integer which should be fetched from or stored
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300525 * in the message
526 * Par1: C structure name
527 * Par2: gives the length of the fixed number in bits.
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100528 * Par3: the value of the number. If the expected value is not present in
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300529* the message the unpacking procedure is aborted
530 *****************************************************************************/
531#define M_FIXED(_STRUCT, _BITS, _BITVALUE)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100532 {CSN_FIXED, _BITS, {0}, _BITVALUE, FALSE, #_BITVALUE, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300533
534/******************************************************************************
535 * M_SERIALIZE(Par1, Par2, Par3)
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100536 * Allows using a complete free format of data being encoded or decoded.
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400537 * When the M_SERIALIZE is encounted during encoding or decoding of a message
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100538 * the CSNstream program passes the control over to the specified function
539 * together with all necessary parameters about the current position within
540 * the message being unpacked or packed. When transferring of "serialized"
541 * data to or from the message is finished by the function the CSNstream gets
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300542 * back control over the data stream and continues to work with the message.
543 *****************************************************************************/
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400544#define M_SERIALIZE(_STRUCT, _MEMBER, _LENGTH_LEN, _SERIALIZEFCN)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100545 {CSN_SERIALIZE, _LENGTH_LEN, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, (void_fn_t)_SERIALIZEFCN}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300546
547#define M_CALLBACK(_STRUCT, _CSNCALLBACKFCN, _PARAM1, _PARAM2)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100548 {CSN_CALLBACK, offsetof(_STRUCT, _PARAM1), {0}, offsetof(_STRUCT, _PARAM2), FALSE, "CallBack_"#_CSNCALLBACKFCN, 0, (void_fn_t)_CSNCALLBACKFCN}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300549
550/******************************************************************************
551 * M_BITMAP(Par1, Par2, Par3)
Pau Espin Pedrole5e2f742020-03-26 15:14:17 +0100552 * Defines a type which consists of a bitmap. The size of the bitmap in bits
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300553 * is fixed and provided by the parameter Par3
554 * Par1: C structure name
555 * Par2: C structure element name
556 * Par3: length of the bitmap expressed in bits
557 *****************************************************************************/
558#define M_BITMAP(_STRUCT, _MEMBER, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100559 {CSN_BITMAP, _BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300560
561/* variable length, right aligned bitmap i.e. _ElementCountField = 11 => 00000111 11111111 */
562#define M_VAR_BITMAP(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100563 {CSN_VARIABLE_BITMAP, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300564
565/* variable length, right aligned bitmap filling the rest of message
566 * - when unpacking the _ElementCountField will be set in runtime
567 * - when packing _ElementCountField contains the size of bitmap
568 */
569#define M_VAR_BITMAP_1(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100570 {CSN_VARIABLE_BITMAP_1, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300571
572/* variable length, left aligned bitmap i.e. _ElementCountField = 11 => 11111111 11100000 */
573#define M_LEFT_VAR_BMP(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100574 {CSN_LEFT_ALIGNED_VAR_BMP, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300575
576/* variable length, left aligned bitmap filling the rest of message
577 *- when unpacking the _ElementCountField will be set in runtime
578 * - when packing _ElementCountField contains the size of bitmap
579 */
580#define M_LEFT_VAR_BMP_1(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100581 {CSN_LEFT_ALIGNED_VAR_BMP_1, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400582
583/* todo: dissect padding bits looking for unexpected extensions */
584#define M_PADDING_BITS(_STRUCT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100585 {CSN_PADDING_BITS, 0, {0}, 0, TRUE, "Padding", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300586
Anders Broman72c102a2020-01-24 14:31:15 +0100587#define M_NULL(_STRUCT, _MEMBER, _SKIP_BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100588 {CSN_NULL, _SKIP_BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300589
590#define M_THIS_EXIST(_STRUCT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100591 {CSN_EXIST, 0, {0}, offsetof(_STRUCT, Exist), FALSE, "Exist", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300592
593#define M_THIS_EXIST_LH(_STRUCT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100594 {CSN_EXIST_LH, 0, {0}, offsetof(_STRUCT, Exist), FALSE, "Exist", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300595
596/* return value 0 if ok else discontionue the unpacking */
597typedef gint16 (*CsnCallBackFcn_t)(void* pv ,...);
598
599#define CSNDESCR(_FuncType) CSNDESCR_##_FuncType
600
Pau Espin Pedrolc90e6f82021-10-19 14:45:17 +0200601#define pvDATA(_pv, _offset) ((void*) ((unsigned char*)_pv + _offset))
602#define pui8DATA(_pv, _offset) ((guint8*) pvDATA(_pv, _offset))
603#define pui16DATA(_pv, _offset) ((guint16*) pvDATA(_pv, _offset))
604#define pui32DATA(_pv, _offset) ((guint32*) pvDATA(_pv, _offset))
605#define pui64DATA(_pv, _offset) ((guint64*) pvDATA(_pv, _offset))
606/* used to tag existence of next element in variable length lists */
607#define STANDARD_TAG 1
608#define REVERSED_TAG 0
609
610gint16 ProcessError_impl(const char *file, int line, unsigned *readIndex,
611 const char* sz, gint16 err, const CSN_DESCR* pDescr);
612#define ProcessError(readIndex, sz, err, pDescr) \
613 ProcessError_impl(__FILE__, __LINE__, readIndex, sz, err, pDescr)
614
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300615#endif /*_PACKET_CSN1_H_*/