blob: f445dd2d0988402058c9c372b3b1e382de635c34 [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.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
Max910a3872018-01-30 13:29:38 +010028#ifndef _PACKET_CSN1_H_
29#define _PACKET_CSN1_H_
30
Alexander Couzensccde5c92017-02-04 03:10:08 +010031extern "C" {
32#include <osmocom/core/bitvec.h>
33}
34
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030035#include <iostream>
36#include <cstdlib>
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030037
38#define MIN(a,b) (((a)<(b))?(a):(b))
39//#define max(a,b) (((a)>(b))?(a):(b))
40
41/* Error codes */
42#define CSN_OK 0
43#define CSN_ERROR_GENERAL -1
44#define CSN_ERROR_DATA_NOT_VALID -2
45#define CSN_ERROR_IN_SCRIPT -3
46#define CSN_ERROR_INVALID_UNION_INDEX -4
47#define CSN_ERROR_NEED_MORE_BITS_TO_UNPACK -5
48#define CSN_ERROR_ILLEGAL_BIT_VALUE -6
49#define CSN_ERROR_INTERNAL -7
50#define CSN_ERROR_STREAM_NOT_SUPPORTED -8
51#define CSN_ERROR_MESSAGE_TOO_LONG -9
52#define CSN_ERROR_ -10
53
54#define FALSE (0)
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +040055#define TRUE (1)
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030056typedef signed int gint32;
57typedef signed short gint16;
58typedef int gint;
Pascal Quantinc5155512020-01-24 17:33:06 +010059typedef unsigned int guint;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030060typedef gint gboolean;
61typedef unsigned char guint8;
62typedef unsigned short guint16;
63typedef unsigned int guint32;
64typedef unsigned long guint64;
65/* CallBack return status */
66typedef gint16 CSN_CallBackStatus_t;
67
68#define CSNCBS_OK 0
69#define CSNCBS_NOT_OK -10
70#define CSNCBS_NOT_TO_US -11
71#define CSNCBS_NOT_COMPLETE -12
72
73#define CSNCBS_REVISION_LIMIT_STOP -20 /* Stop packing/unpacking - revision limit */
74#define CSNCBS_NOT_SUPPORTED_IE -21 /* Handling of the unpacked IE is not supported by MS-software */
75
76
77
78#ifndef ElementsOf
79#define ElementsOf(array) (sizeof(array) / sizeof(array[0]))
80#endif
Pau Espin Pedrol5b716972020-01-24 17:24:01 +010081typedef void(*void_fn_t)(void);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030082
83/* Context holding CSN1 parameters */
84typedef struct
85{
86 gint remaining_bits_len; /* IN to an csn stream operation */
87 gint bit_offset; /* IN/OUT to an csn stream operation */
88 gint direction; /* 0 - decode; 1 - encode */
89} csnStream_t;
90
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +040091typedef gint16 (*StreamSerializeFcn_t)(csnStream_t* ar, bitvec *vector, unsigned& readIndex, void* data);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030092typedef enum
93{
94 CSN_END = 0,
95 CSN_BIT,
96 CSN_UINT,
97 CSN_TYPE,
98 CSN_CHOICE,
99 CSN_UNION,
100 CSN_UNION_LH,
101 CSN_UINT_ARRAY,
102 CSN_TYPE_ARRAY,
103 CSN_BITMAP, /* Bitmap with constant: <bitmap: bit(64)> */
104 CSN_VARIABLE_BITMAP, /* <N: bit (5)> <bitmap: bit(N + offset)> */
105 CSN_VARIABLE_BITMAP_1, /* <bitmap: bit**> i.e. to the end of message (R99) */
106 CSN_LEFT_ALIGNED_VAR_BMP, /* As variable bitmap but the result is left aligned (R99) */
107 CSN_LEFT_ALIGNED_VAR_BMP_1,/* As above only size is to the end of message (R99) */
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400108 CSN_PADDING_BITS, /* Padding bits fill to the end of the buffer */
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300109 CSN_VARIABLE_ARRAY, /* Array with length specified in parameter: <N: bit(4)> <list: octet(N + offset)> */
110 CSN_VARIABLE_TARRAY, /* Type Array with length specified in parameter: <N: bit(x)> <Type>*N */
111 CSN_VARIABLE_TARRAY_OFFSET,/* As above but with offset. The offset is stored as third parameter of CSN_DESCR (descr.value) */
112 CSN_RECURSIVE_ARRAY, /* Recursive way to specify an array of uint: <list> ::= {1 <number: bit(4) <list>|0}; */
113 CSN_RECURSIVE_TARRAY, /* Recursive way to specify an array of type: <list> ::= {1 <type>} ** 0 ; */
114 CSN_RECURSIVE_TARRAY_1, /* same as above but first element always exist:<list> ::= <type> {1 <type>} ** 0 ; */
115 CSN_RECURSIVE_TARRAY_2, /* same as above but with reversed separators :<lists> ::= <type> { 0 <type> } ** 1 ; */
116 CSN_EXIST,
117 CSN_EXIST_LH,
118 CSN_NEXT_EXIST,
119 CSN_NEXT_EXIST_LH,
120 CSN_NULL,
121 CSN_FIXED,
122 CSN_CALLBACK,
123 CSN_UINT_OFFSET, /* unpack will add offset, inverse pack will subtract offset */
124 CSN_UINT_LH, /* Low High extraction of int */
125 CSN_SERIALIZE,
126 CSN_TRAP_ERROR
127} csn_type_t;
128
129/******************************************************************************************
130 * CSN_DESCR structure:
131 *
132 * type:
133 * This is the CSN type. All existing types are specified in the section above.
134 *
135 * i:
136 * Depending on the contents of the type parameter, the parameter "i" may have following meaning:
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400137 * - specifies the number of bits for the CSN_UINT or CSN_UINT_OR_NULL types
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300138 * - the offset for an array size by which the size is incremented
139 * for the CSN_VAR_ARRAY type
140 * - the length of each element of an array for the CSN_REC_ARRAY type
141 * - the number of the elements in an array for the CSN_TYPE_ARRAY type
142 * - the offset to the variable keeping the number of elements of an array for in the CSN_VAR_TARRAY type
143 * - the number of different data types in a union for the CSN_UNION, CSN_UNION_LH, and for the CSN_CHOICE types
144 * - the length in bits of the fixed number defined for the CSN_FIXED type
145 * - the number of lines to skip in the CSN_DESCR type specified for the CSN_NEXT_EXIST, CSN_NEXT_EXIST_LH,
146 * CSN_NEXT_EXIST_OR_NULL, and CSN_NEXT_EXIST_OR_NULL_LH types
147 * - the number of bits in a bitmap for the CSN_BITMAP type
148 * - the value by which the number of bits in a bitmap has to be incremented or decremented for the
149 * CSN_VAR_BITMAP, CSN_LEFT_VAR_BMP, and CSN_LEFT_BMP_1 types
150 * - the offset to param1 for the CSN_CALLBACK type
151 * - ERRORCODE used by the CSN_ERROR type
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400152 * - the bit-length of the LENGTH field in a CSN_SERIALISE type
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300153 *
154 * descr
155 * This parameter has different meaning depending on the value of the type parameter:
156 * - the offset for the CSN_UINT_OFFSET type
157 * - the number of the elements in an array of the CSN_UINT_ARRAY type
158 * - the offset to the parameter where the size of the array has to be stored for the CSN_REC_ARRAY type
159 * - the address of the internal structure, describing the member type (by means of the CSN_DESCR type) in the
160 * CSN_TYPE_ARRAY, CSN_VAR_TARRAY, and CSN_TYPE types
161 * - the address of the variable of type CSN_ChoiceElement_t describing all elements in the CSN_CHOICE type union
162 * - the offset to the variable where the number of bits has to be or is stored for the CSN_VAR_BITMAP,
163 * CSN_LEFT_VAR_BMP, and CSN_LEFT_BMP_1 types
164 * - the function number (case number) for the CSN_CALLBACK and CSN_CALLBACK_NO_ARGS types
165 * - the free text used by the CSN_TRAP_ERROR
166 *
167 * offset
168 * This is an offset to the _MEMBER parameter counting from the beginning of struct
169 * where the unpacked or packed value shall be stored or fetched. The meaning of the _MEMBER parameter
170 * varies depending on the type which is specified and so is the meaning of the offset parameter.
171 * Some types (and corresponding macros) do not have the _MEMBER parameter and then the offset parameter
172 * is not used or is different from the offset to the _MEMBER.
173 * - the fixed value for the CSN_FIXED type
174 * - an offset to the variable UnionType for CSN_UNION and CSN_UNION_LH types
175 * - an offset to the variable Exist for CSN_NEXT_EXIST and CSN_NEXT_EXIST_LH types
176 * - an offset to param2 in the CSN_CALLBACK type
177 *
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400178 * may_be_null
179 * TRUE: if dissection may be attempted at an offset beyond the length of existing data bits
180 * FALSE: othewise
181 *
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300182 * sz
183 * - is the name of the parameter within the descr where their unpacked or packed value shall be stored or fetched.
184 * This paramater is pointed out by the offset parameter in the same CSN_DESCR variable as the sz.
185 * - the free text used by the CSN_TRAP_ERROR (the same as parameter "i")
186 *
187 * serialize
188 * - stores the size of _MEMBER type in case of the M_TYPE_ARRAY and M_VAR_TARRAY,
189 * - the address of the function which is provided by the M_SERIALIZE type.
190 ******************************************************************************************/
191
192
193typedef struct
194{
195 gint16 type;
196 gint16 i;
197 union
198 {
Pau Espin Pedrol5b716972020-01-24 17:24:01 +0100199 const void* ptr;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300200 guint32 value;
201 } descr;
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +0400202 unsigned offset;
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400203 gboolean may_be_null;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300204 const char* sz;
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100205 guint32 value;
Pau Espin Pedrol5b716972020-01-24 17:24:01 +0100206 void_fn_t aux_fn;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300207} CSN_DESCR;
208
209typedef struct
210{
211 guint8 bits;
212 guint8 value;
Pau Espin Pedrol7cce8252020-01-24 16:41:14 +0100213 gboolean keep_bits;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300214 CSN_DESCR descr;
215} CSN_ChoiceElement_t;
216
217void csnStreamInit(csnStream_t* ar,gint BitOffset,gint BitCount);
218
219/******************************************************************************
220* FUNCTION: csnStreamDecoder
221* DESCRIPTION:
222* UnPacks data from bit stream. According to CSN description.
223* ARGS:
224* ar stream will hold the parameters to the pack function
225* ar->remaining_bits_len [IN] Number of bits to unpack [OUT] number of bits left to unpack.
226* ar->bit_offset [IN/OUT] is the current bit where to proceed with the next bit to unpack.
227
228* pDescr CSN description.
229* tvb buffer containing the bit stream to unpack.
230* data unpacked data.
231* ett_csn1 tree
232*
233* RETURNS: int Number of bits left to be unpacked. Negative Error code if failed to unpack all bits
234******************************************************************************/
235
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +0400236gint16 csnStreamDecoder(csnStream_t* ar, const CSN_DESCR* pDescr, bitvec *vector, unsigned& readIndex, void* data);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300237
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +0400238gint16 csnStreamEncoder(csnStream_t* ar, const CSN_DESCR* pDescr, bitvec *vector, unsigned& readIndex, void* data);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300239
240/* CSN struct macro's */
241#define CSN_DESCR_BEGIN(_STRUCT)\
242 CSN_DESCR CSNDESCR_##_STRUCT[] = {
243
244#define CSN_DESCR_END(_STRUCT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100245 {CSN_END, 0, {0}, 0, FALSE, "", 0, NULL} };
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300246
247/******************************************************************************
248 * CSN_ERROR(Par1, Par2, Par3)
249 * May be called at any time when an abort of packing or unpacking of a message
250 * is desired
251 * Par1: C structure name
252 * Par2: free text which will appear in the error handler
253 * Par3: Error code
254 *****************************************************************************/
255#define CSN_ERROR(_STRUCT, _Text, _ERRCODE)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100256 {CSN_TRAP_ERROR, _ERRCODE, {_Text}, 0, FALSE, _Text, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300257
258/******************************************************************************
259 * M_BIT(Par1, Par2)
260 * Defines one bit element in the CSN1 syntax.
261 * Par1: C structure name
262 * Par2: C structure element name
263 *****************************************************************************/
264#define M_BIT(_STRUCT, _MEMBER)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100265 {CSN_BIT, 0, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400266
267/******************************************************************************
268 * M_BIT_OR_NULL(Par1, Par2)
269 * Similar to the M_BIT except that not only bit 0 or 1 but also the
270 * end of the message may be encountered when looking for the next element in
271 * the message.
272 * Covers the case {null | 0 | 1}
273 *****************************************************************************/
274 #define M_BIT_OR_NULL(_STRUCT, _MEMBER)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100275 {CSN_BIT, 0, {0}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300276
277/******************************************************************************
278 * M_NEXT_EXIST(Par1, Par2, Par3)
Pau Espin Pedrol5b716972020-01-24 17:24:01 +0100279 * Indicates whether the next element or a group of elements defined in the
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300280 * structure is present or not.
281 * Par1: C structure name
282 * Par2: C structure element name
283 * Par3: number of lines to skip in the CSN_DESCR type specified if the
284 * element(s) does not exist
285 *****************************************************************************/
286#define M_NEXT_EXIST(_STRUCT, _MEMBER, _NoOfExisting)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100287 {CSN_NEXT_EXIST, _NoOfExisting, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300288
289/******************************************************************************
290 * M_NEXT_EXIST_LH(Par1, Par2, Par3)
291 * similar to the M_NEXT_EXIST except that instead of bit 0/1 which is fetched
292 * from the message in order to find out whether the next element/elements are
293 * present in the message, the logical operation XOR with the background
294 * pattern 0x2B is performed on the read bit before the decision is made.
295 *****************************************************************************/
296#define M_NEXT_EXIST_LH(_STRUCT, _MEMBER, _NoOfExisting)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100297 {CSN_NEXT_EXIST_LH, _NoOfExisting, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300298
299/******************************************************************************
300 * M_NEXT_EXIST_OR_NULL(Par1, Par2, Par3)
301 * Similar to the M_NEXT_EXIST except that not only bit 0 or 1 but also the end
302 * of the message may be encountered when looking for the next element in the
303 * message.
304 * Covers the case {null | 0 | 1 < IE >}
305 *****************************************************************************/
306#define M_NEXT_EXIST_OR_NULL(_STRUCT, _MEMBER, _NoOfExisting)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100307 {CSN_NEXT_EXIST, _NoOfExisting, {0}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300308
309/******************************************************************************
310 * M_NEXT_EXIST_OR_NULL_LH(Par1, Par2, Par3)
311 * Similar to the M_NEXT_EXIST_LH except that not only bit 0 or 1 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 | L | H < IE >}
315 *****************************************************************************/
316#define M_NEXT_EXIST_OR_NULL_LH(_STRUCT, _MEMBER, _NoOfExisting)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100317 {CSN_NEXT_EXIST_LH, _NoOfExisting, {(void*)1}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300318
319/******************************************************************************
320 * M_UINT(Par1, Par2, Par3)
321 * Defines an integer number.
322 * Par1: C structure name
323 * Par2: C structure element name
324 * Par3: number of bits used to code the element (between 1 and 32)
325 *****************************************************************************/
326#define M_UINT(_STRUCT, _MEMBER, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100327 {CSN_UINT, _BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400328
329 /******************************************************************************
330 * M_UINT_OR_NULL(Par1, Par2, Par3)
331 * Similar to the M_UINT except that not only the request set of bits but also the
332 * end of the message may be encountered when looking for the next element in
333 * the message.
334 * Covers the case {null | 0 | 1 < IE >}
335 *****************************************************************************/
336 #define M_UINT_OR_NULL(_STRUCT, _MEMBER, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100337 {CSN_UINT, _BITS, {0}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300338
339/******************************************************************************
340 * M_UINT(Par1, Par2, Par3)
341 * This macro has the same functionality as M_UINT except that in addition the
342 * logical "exclusive or" operation with the background value "0x2B" is
343 * performed before the final value of the integer number is delivered from the
344 * received CSN.1 message
345 *****************************************************************************/
346#define M_UINT_LH(_STRUCT, _MEMBER, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100347 {CSN_UINT_LH, _BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300348
349/******************************************************************************
350 * M_UINT_OFFSET(Par1, Par2, Par3, Par4)
351 * Defines an integer number.
352 * Par1: C structure name
353 * Par2: C structure element name
354 * Par3: number of bits used to code the element (between 1 and 32)
355 * Par4: value added to the returned integer (offset)
356 *****************************************************************************/
357#define M_UINT_OFFSET(_STRUCT, _MEMBER, _BITS, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100358 {CSN_UINT_OFFSET, _BITS, {(void*)_OFFSET}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300359
360/******************************************************************************
361 * M_UINT_ARRAY(Par1, Par2, Par3, Par4)
362 * Defines an array of integer numbers. The size of the array is fixed.
363 * Par1: C structure name
364 * Par2: C structure element name
365 * Par3: number of bits used to code the each integer element (between 1 and 32)
366 * Par4: number of elements in the array (fixed integer value)
367 *****************************************************************************/
368#define M_UINT_ARRAY(_STRUCT, _MEMBER, _BITS, _ElementCount)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100369 {CSN_UINT_ARRAY, _BITS, {(void*)_ElementCount}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300370
371/******************************************************************************
372 * M_VAR_UINT_ARRAY(Par1, Par2, Par3, Par4)
373 * Defines an array of integer numbers. The size of the array is variable.
374 * Par1: C structure name
375 * Par2: C structure element name
376 * Par3: number of bits used to code the each integer element (between 1 and 32)
377 * Par4: number of elements in the array supplied by reference to the
378 * structure member holding the length value
379 *****************************************************************************/
380#define M_VAR_UINT_ARRAY(_STRUCT, _MEMBER, _BITS, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100381 {CSN_UINT_ARRAY, _BITS, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 1, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300382
383/******************************************************************************
384 * M_VAR_ARRAY(Par1, Par2, Par3, Par4)
385 * Defines an array of 8 bit large integer numbers. The size of the array is variable.
386 * Par1: C structure name
387 * Par2: C structure element name
388 * Par3: name of the structure member holding the size of the array
389 * Par4: offset that is added to the Par3 to get the actual size of the array
390 *****************************************************************************/
391#define M_VAR_ARRAY(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100392 {CSN_VARIABLE_ARRAY, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300393
394/******************************************************************************
395 * M_VAR_TARRAY(Par1, Par2, Par3, Par4)
396 * Similar to M_TYPE_ARRAY except that the size of the array is variable.
397 * Par1: C structure name
398 * Par2: C structure element name
399 * Par3: the type of each element of the array
400 * Par4: name of the structure member holding the size of the array
401 *****************************************************************************/
402#define M_VAR_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100403 {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 +0300404
405/******************************************************************************
406 * M_VAR_TARRAY_OFFSET(Par1, Par2, Par3, Par4)
407 * Same as M_VAR_TARRAY with offset
408 *****************************************************************************/
409#define M_VAR_TARRAY_OFFSET(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100410 {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 +0300411
412/******************************************************************************
413 * M_REC_ARRAY(Par1, Par2, Par3, Par4)
414 * similar to the M_VAR_ARRAY. The difference is that the size of the array is
415 * not known in advance and it has to be calculated during unpacking. Its value
416 * is stored in a variable which belongs to the same structure as the array.
417 * A zero element terminates the array. The CSN.1 syntax describes it
418 * recursively as:
419 * <array> ::={1 <element> <array>| 0}
420 *
421 * Par1: C structure name
422 * Par2: C structure element name
423 * Par3: name of the structure member where the calculated the size of the
424 * array will be stored
425 * Par4: length of each element in bits
426 *****************************************************************************/
427#define M_REC_ARRAY(_STRUCT, _MEMBER, _ElementCountField, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100428 {CSN_RECURSIVE_ARRAY, _BITS, {(const void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300429
430/******************************************************************************
431 * M_VAR_TYPE_ARRAY(Par1, Par2, Par3, Par4)
432 * Defines an array of structures. The size of the array is variable.
433 * Par1: C structure name
434 * Par2: C structure element name
435 * Par3: name of the structure
436 * Par4: number of elements in the array (fixed integer value)
437 *****************************************************************************/
438#define M_TYPE_ARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCount)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100439 {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 +0300440
441/******************************************************************************
442 * M_REC_TARRAY(Par1, Par2, Par3, Par4)
443 * Defines an recursive array of structures. The size of the array is variable.
444 * <list> ::= {1 <type>} ** 0 ;
445 * Par1: C structure name
446 * Par2: C structure element name
447 * Par3: name of the structure
448 * Par4: will hold the number of element in the array after unpacking
449 *****************************************************************************/
450#define M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100451 {CSN_RECURSIVE_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 +0300452
453/******************************************************************************
454 * M_REC_TARRAY1(Par1, Par2, Par3, Par4)
455 * Same as M_REC_TARRAY but first element always exist:
456 * <list> ::= <type> {1 <type>} ** 0 ;
457 *****************************************************************************/
458#define M_REC_TARRAY_1(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100459 {CSN_RECURSIVE_TARRAY_1, offsetof(_STRUCT, _ElementCountField), {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, sizeof(_MEMBER_TYPE), NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300460
461/******************************************************************************
462 * M_REC_TARRAY2(Par1, Par2, Par3, Par4)
463 * Same as M_REC_TARRAY but with reversed separators :
464 * <lists> ::= <type> { 0 <type> } ** 1 ;
465 *****************************************************************************/
466#define M_REC_TARRAY_2(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100467 {CSN_RECURSIVE_TARRAY_2, offsetof(_STRUCT, _ElementCountField), {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, sizeof(_MEMBER_TYPE), NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300468
469/******************************************************************************
470 * M_TYPE(Par1, Par2, Par3)
471 * Defines a reference to a structure which is described elsewhere
472 * <list> ::= {1 <type>} ** 0 ;
473 * Par1: C structure name
474 * Par2: C structure element name
475 * Par3: type of member
476 *****************************************************************************/
477#define M_TYPE(_STRUCT, _MEMBER, _MEMBER_TYPE)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100478 {CSN_TYPE, 0, {(const void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300479
480/******************************************************************************
481 * M_UNION(Par1, Par2)
482 * Informs the CSN.1 library that a union follows and how many possible choices
483 * there are in the union. The actual value of the choice, which points out the
484 * chosen element of the union is stored in the uint8 variable and is usually
485 * called UnionType. The elements of the union have to be listed directly after
486 * the M_UNION statement.
487 * Par1: C structure name
488 * Par2: number of possible choice in the union
489 *****************************************************************************/
490#define M_UNION(_STRUCT, _COUNT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100491 {CSN_UNION, _COUNT, {0}, offsetof(_STRUCT, UnionType), FALSE, "UnionType", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300492
493/******************************************************************************
494 * M_UNION_LH(Par1, Par2)
495 * Same as M_UNION but masked with background value 0x2B
496 *****************************************************************************/
497#define M_UNION_LH(_STRUCT, _COUNT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100498 {CSN_UNION_LH, _COUNT, {0}, offsetof(_STRUCT, UnionType), FALSE, "UnionType", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300499
500/******************************************************************************
501 * M_CHOICE(Par1, Par2, Par3, Par4)
502 * Similar to the M_UNION. In the M_UNION the selected element of all possible
503 * choices in the union is referred as a sequential numbers, i.e., the first
504 * choice is addressed as choice 0 the second as choice 1, the third as choice
505 * 2 and so on, both in the encoded message and in the variable UnionType which
506 * is the part of the message. In the CSN_CHOICE case, this rule does not
507 * apply. There is free but predefined mapping of the element of the union and
508 * the value which addresses this element.
509 * The value of the address is called a selector.
510 * After unpacking, this value is then converted to the sequential number of the
511 * element in the union and stored in the UnionType variable.
512 * Par1: C structure name
513 * Par2: C structure element name
514 * Par3: address of an array of type CSN_ChoiceElement_t where all possible
515 * values of the selector are provided, together with the selector
516 * length expressed in bits and the address of the CSN_DESCR type
517 * where the element is defined. For every element in the union
518 * there is one line in the Choice variable. These lines have to
519 * appear in the _CHOICE in the same order as the elements in the
520 * union. The element of the union selected in the message through
521 * the _CHOICE parameter is after unpacking translated to the
522 * corresponding sequential number of this element and stored in
523 * the variable pointed out by the _MEMBER
524 * Par4: number of possible choices in the union
525 *****************************************************************************/
526#define M_CHOICE(_STRUCT, _MEMBER, _CHOICE, _ElementCount)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100527 {CSN_CHOICE, _ElementCount, {(const void*)_CHOICE}, offsetof(_STRUCT, _MEMBER), FALSE, #_CHOICE, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300528
529/******************************************************************************
530 * M_FIXED(Par1, Par2, Par3)
531 * Defines a fixed value of type integer which should be fetched from or stored
532 * in the message
533 * Par1: C structure name
534 * Par2: gives the length of the fixed number in bits.
535 * Par3: the value of the number. If the expected value is not present in
536* the message the unpacking procedure is aborted
537 *****************************************************************************/
538#define M_FIXED(_STRUCT, _BITS, _BITVALUE)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100539 {CSN_FIXED, _BITS, {0}, _BITVALUE, FALSE, #_BITVALUE, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300540
541/******************************************************************************
542 * M_SERIALIZE(Par1, Par2, Par3)
543 * Allows using a complete free format of data being encoded or decoded.
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400544 * When the M_SERIALIZE is encounted during encoding or decoding of a message
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300545 * the CSNstream program passes the control over to the specified function
546 * together with all necessary parameters about the current position within
547 * the message being unpacked or packed. When transferring of "serialized"
548 * data to or from the message is finished by the function the CSNstream gets
549 * back control over the data stream and continues to work with the message.
550 *****************************************************************************/
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400551#define M_SERIALIZE(_STRUCT, _MEMBER, _LENGTH_LEN, _SERIALIZEFCN)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100552 {CSN_SERIALIZE, _LENGTH_LEN, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, (void_fn_t)_SERIALIZEFCN}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300553
554#define M_CALLBACK(_STRUCT, _CSNCALLBACKFCN, _PARAM1, _PARAM2)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100555 {CSN_CALLBACK, offsetof(_STRUCT, _PARAM1), {0}, offsetof(_STRUCT, _PARAM2), FALSE, "CallBack_"#_CSNCALLBACKFCN, 0, (void_fn_t)_CSNCALLBACKFCN}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300556
557/******************************************************************************
558 * M_BITMAP(Par1, Par2, Par3)
559 * Defines a type which consists of a bitmap. The size of the bitmap in bits
560 * is fixed and provided by the parameter Par3
561 * Par1: C structure name
562 * Par2: C structure element name
563 * Par3: length of the bitmap expressed in bits
564 *****************************************************************************/
565#define M_BITMAP(_STRUCT, _MEMBER, _BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100566 {CSN_BITMAP, _BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300567
568/* variable length, right aligned bitmap i.e. _ElementCountField = 11 => 00000111 11111111 */
569#define M_VAR_BITMAP(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100570 {CSN_VARIABLE_BITMAP, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300571
572/* variable length, right aligned bitmap filling the rest of message
573 * - when unpacking the _ElementCountField will be set in runtime
574 * - when packing _ElementCountField contains the size of bitmap
575 */
576#define M_VAR_BITMAP_1(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100577 {CSN_VARIABLE_BITMAP_1, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300578
579/* variable length, left aligned bitmap i.e. _ElementCountField = 11 => 11111111 11100000 */
580#define M_LEFT_VAR_BMP(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100581 {CSN_LEFT_ALIGNED_VAR_BMP, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300582
583/* variable length, left aligned bitmap filling the rest of message
584 *- when unpacking the _ElementCountField will be set in runtime
585 * - when packing _ElementCountField contains the size of bitmap
586 */
587#define M_LEFT_VAR_BMP_1(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100588 {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 +0400589
590/* todo: dissect padding bits looking for unexpected extensions */
591#define M_PADDING_BITS(_STRUCT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100592 {CSN_PADDING_BITS, 0, {0}, 0, TRUE, "Padding", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300593
Anders Broman72c102a2020-01-24 14:31:15 +0100594#define M_NULL(_STRUCT, _MEMBER, _SKIP_BITS)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100595 {CSN_NULL, _SKIP_BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300596
597#define M_THIS_EXIST(_STRUCT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100598 {CSN_EXIST, 0, {0}, offsetof(_STRUCT, Exist), FALSE, "Exist", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300599
600#define M_THIS_EXIST_LH(_STRUCT)\
Pau Espin Pedrol900c2e22020-01-24 18:05:00 +0100601 {CSN_EXIST_LH, 0, {0}, offsetof(_STRUCT, Exist), FALSE, "Exist", 0, NULL}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300602
603/* return value 0 if ok else discontionue the unpacking */
604typedef gint16 (*CsnCallBackFcn_t)(void* pv ,...);
605
606#define CSNDESCR(_FuncType) CSNDESCR_##_FuncType
607
608#endif /*_PACKET_CSN1_H_*/