blob: 3f5b84908054c246dfa369ef2125d4703d0cb52a [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
Alexander Couzensccde5c92017-02-04 03:10:08 +010028extern "C" {
29#include <osmocom/core/bitvec.h>
30}
31
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030032#include <iostream>
33#include <cstdlib>
34#ifndef _PACKET_CSN1_H_
35#define _PACKET_CSN1_H_
36
37
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;
59typedef gint gboolean;
60typedef unsigned char guint8;
61typedef unsigned short guint16;
62typedef unsigned int guint32;
63typedef unsigned long guint64;
64/* CallBack return status */
65typedef gint16 CSN_CallBackStatus_t;
66
67#define CSNCBS_OK 0
68#define CSNCBS_NOT_OK -10
69#define CSNCBS_NOT_TO_US -11
70#define CSNCBS_NOT_COMPLETE -12
71
72#define CSNCBS_REVISION_LIMIT_STOP -20 /* Stop packing/unpacking - revision limit */
73#define CSNCBS_NOT_SUPPORTED_IE -21 /* Handling of the unpacked IE is not supported by MS-software */
74
75
76
77#ifndef ElementsOf
78#define ElementsOf(array) (sizeof(array) / sizeof(array[0]))
79#endif
80
81/* Context holding CSN1 parameters */
82typedef struct
83{
84 gint remaining_bits_len; /* IN to an csn stream operation */
85 gint bit_offset; /* IN/OUT to an csn stream operation */
86 gint direction; /* 0 - decode; 1 - encode */
87} csnStream_t;
88
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +040089typedef gint16 (*StreamSerializeFcn_t)(csnStream_t* ar, bitvec *vector, unsigned& readIndex, void* data);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030090typedef enum
91{
92 CSN_END = 0,
93 CSN_BIT,
94 CSN_UINT,
95 CSN_TYPE,
96 CSN_CHOICE,
97 CSN_UNION,
98 CSN_UNION_LH,
99 CSN_UINT_ARRAY,
100 CSN_TYPE_ARRAY,
101 CSN_BITMAP, /* Bitmap with constant: <bitmap: bit(64)> */
102 CSN_VARIABLE_BITMAP, /* <N: bit (5)> <bitmap: bit(N + offset)> */
103 CSN_VARIABLE_BITMAP_1, /* <bitmap: bit**> i.e. to the end of message (R99) */
104 CSN_LEFT_ALIGNED_VAR_BMP, /* As variable bitmap but the result is left aligned (R99) */
105 CSN_LEFT_ALIGNED_VAR_BMP_1,/* As above only size is to the end of message (R99) */
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400106 CSN_PADDING_BITS, /* Padding bits fill to the end of the buffer */
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300107 CSN_VARIABLE_ARRAY, /* Array with length specified in parameter: <N: bit(4)> <list: octet(N + offset)> */
108 CSN_VARIABLE_TARRAY, /* Type Array with length specified in parameter: <N: bit(x)> <Type>*N */
109 CSN_VARIABLE_TARRAY_OFFSET,/* As above but with offset. The offset is stored as third parameter of CSN_DESCR (descr.value) */
110 CSN_RECURSIVE_ARRAY, /* Recursive way to specify an array of uint: <list> ::= {1 <number: bit(4) <list>|0}; */
111 CSN_RECURSIVE_TARRAY, /* Recursive way to specify an array of type: <list> ::= {1 <type>} ** 0 ; */
112 CSN_RECURSIVE_TARRAY_1, /* same as above but first element always exist:<list> ::= <type> {1 <type>} ** 0 ; */
113 CSN_RECURSIVE_TARRAY_2, /* same as above but with reversed separators :<lists> ::= <type> { 0 <type> } ** 1 ; */
114 CSN_EXIST,
115 CSN_EXIST_LH,
116 CSN_NEXT_EXIST,
117 CSN_NEXT_EXIST_LH,
118 CSN_NULL,
119 CSN_FIXED,
120 CSN_CALLBACK,
121 CSN_UINT_OFFSET, /* unpack will add offset, inverse pack will subtract offset */
122 CSN_UINT_LH, /* Low High extraction of int */
123 CSN_SERIALIZE,
124 CSN_TRAP_ERROR
125} csn_type_t;
126
127/******************************************************************************************
128 * CSN_DESCR structure:
129 *
130 * type:
131 * This is the CSN type. All existing types are specified in the section above.
132 *
133 * i:
134 * Depending on the contents of the type parameter, the parameter "i" may have following meaning:
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400135 * - specifies the number of bits for the CSN_UINT or CSN_UINT_OR_NULL types
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300136 * - the offset for an array size by which the size is incremented
137 * for the CSN_VAR_ARRAY type
138 * - the length of each element of an array for the CSN_REC_ARRAY type
139 * - the number of the elements in an array for the CSN_TYPE_ARRAY type
140 * - the offset to the variable keeping the number of elements of an array for in the CSN_VAR_TARRAY type
141 * - the number of different data types in a union for the CSN_UNION, CSN_UNION_LH, and for the CSN_CHOICE types
142 * - the length in bits of the fixed number defined for the CSN_FIXED type
143 * - the number of lines to skip in the CSN_DESCR type specified for the CSN_NEXT_EXIST, CSN_NEXT_EXIST_LH,
144 * CSN_NEXT_EXIST_OR_NULL, and CSN_NEXT_EXIST_OR_NULL_LH types
145 * - the number of bits in a bitmap for the CSN_BITMAP type
146 * - the value by which the number of bits in a bitmap has to be incremented or decremented for the
147 * CSN_VAR_BITMAP, CSN_LEFT_VAR_BMP, and CSN_LEFT_BMP_1 types
148 * - the offset to param1 for the CSN_CALLBACK type
149 * - ERRORCODE used by the CSN_ERROR type
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400150 * - the bit-length of the LENGTH field in a CSN_SERIALISE type
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300151 *
152 * descr
153 * This parameter has different meaning depending on the value of the type parameter:
154 * - the offset for the CSN_UINT_OFFSET type
155 * - the number of the elements in an array of the CSN_UINT_ARRAY type
156 * - the offset to the parameter where the size of the array has to be stored for the CSN_REC_ARRAY type
157 * - the address of the internal structure, describing the member type (by means of the CSN_DESCR type) in the
158 * CSN_TYPE_ARRAY, CSN_VAR_TARRAY, and CSN_TYPE types
159 * - the address of the variable of type CSN_ChoiceElement_t describing all elements in the CSN_CHOICE type union
160 * - the offset to the variable where the number of bits has to be or is stored for the CSN_VAR_BITMAP,
161 * CSN_LEFT_VAR_BMP, and CSN_LEFT_BMP_1 types
162 * - the function number (case number) for the CSN_CALLBACK and CSN_CALLBACK_NO_ARGS types
163 * - the free text used by the CSN_TRAP_ERROR
164 *
165 * offset
166 * This is an offset to the _MEMBER parameter counting from the beginning of struct
167 * where the unpacked or packed value shall be stored or fetched. The meaning of the _MEMBER parameter
168 * varies depending on the type which is specified and so is the meaning of the offset parameter.
169 * Some types (and corresponding macros) do not have the _MEMBER parameter and then the offset parameter
170 * is not used or is different from the offset to the _MEMBER.
171 * - the fixed value for the CSN_FIXED type
172 * - an offset to the variable UnionType for CSN_UNION and CSN_UNION_LH types
173 * - an offset to the variable Exist for CSN_NEXT_EXIST and CSN_NEXT_EXIST_LH types
174 * - an offset to param2 in the CSN_CALLBACK type
175 *
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400176 * may_be_null
177 * TRUE: if dissection may be attempted at an offset beyond the length of existing data bits
178 * FALSE: othewise
179 *
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300180 * sz
181 * - is the name of the parameter within the descr where their unpacked or packed value shall be stored or fetched.
182 * This paramater is pointed out by the offset parameter in the same CSN_DESCR variable as the sz.
183 * - the free text used by the CSN_TRAP_ERROR (the same as parameter "i")
184 *
185 * serialize
186 * - stores the size of _MEMBER type in case of the M_TYPE_ARRAY and M_VAR_TARRAY,
187 * - the address of the function which is provided by the M_SERIALIZE type.
188 ******************************************************************************************/
189
190
191typedef struct
192{
193 gint16 type;
194 gint16 i;
195 union
196 {
197 void* ptr;
198 guint32 value;
199 } descr;
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +0400200 unsigned offset;
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400201 gboolean may_be_null;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300202 const char* sz;
203 union
204 {
205 StreamSerializeFcn_t fcn;
206 guint32 value;
207 int* hf_ptr;
208 } serialize;
209} CSN_DESCR;
210
211typedef struct
212{
213 guint8 bits;
214 guint8 value;
215 CSN_DESCR descr;
216} CSN_ChoiceElement_t;
217
218void csnStreamInit(csnStream_t* ar,gint BitOffset,gint BitCount);
219
220/******************************************************************************
221* FUNCTION: csnStreamDecoder
222* DESCRIPTION:
223* UnPacks data from bit stream. According to CSN description.
224* ARGS:
225* ar stream will hold the parameters to the pack function
226* ar->remaining_bits_len [IN] Number of bits to unpack [OUT] number of bits left to unpack.
227* ar->bit_offset [IN/OUT] is the current bit where to proceed with the next bit to unpack.
228
229* pDescr CSN description.
230* tvb buffer containing the bit stream to unpack.
231* data unpacked data.
232* ett_csn1 tree
233*
234* RETURNS: int Number of bits left to be unpacked. Negative Error code if failed to unpack all bits
235******************************************************************************/
236
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +0400237gint16 csnStreamDecoder(csnStream_t* ar, const CSN_DESCR* pDescr, bitvec *vector, unsigned& readIndex, void* data);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300238
Ivan Kluchnikov9b06ff02012-06-15 10:13:30 +0400239gint16 csnStreamEncoder(csnStream_t* ar, const CSN_DESCR* pDescr, bitvec *vector, unsigned& readIndex, void* data);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300240
241/* CSN struct macro's */
242#define CSN_DESCR_BEGIN(_STRUCT)\
243 CSN_DESCR CSNDESCR_##_STRUCT[] = {
244
245#define CSN_DESCR_END(_STRUCT)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400246 {CSN_END, 0, {0}, 0, FALSE, "", {(StreamSerializeFcn_t)0}} };
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300247
248/******************************************************************************
249 * CSN_ERROR(Par1, Par2, Par3)
250 * May be called at any time when an abort of packing or unpacking of a message
251 * is desired
252 * Par1: C structure name
253 * Par2: free text which will appear in the error handler
254 * Par3: Error code
255 *****************************************************************************/
256#define CSN_ERROR(_STRUCT, _Text, _ERRCODE)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400257 {CSN_TRAP_ERROR, _ERRCODE, {(void*)_Text}, 0, FALSE, _Text, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300258
259/******************************************************************************
260 * M_BIT(Par1, Par2)
261 * Defines one bit element in the CSN1 syntax.
262 * Par1: C structure name
263 * Par2: C structure element name
264 *****************************************************************************/
265#define M_BIT(_STRUCT, _MEMBER)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400266 {CSN_BIT, 0, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
267
268/******************************************************************************
269 * M_BIT_OR_NULL(Par1, Par2)
270 * Similar to the M_BIT except that not only bit 0 or 1 but also the
271 * end of the message may be encountered when looking for the next element in
272 * the message.
273 * Covers the case {null | 0 | 1}
274 *****************************************************************************/
275 #define M_BIT_OR_NULL(_STRUCT, _MEMBER)\
276 {CSN_BIT, 0, {0}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300277
278/******************************************************************************
279 * M_NEXT_EXIST(Par1, Par2, Par3)
280 * Indicates whether the next element or a group of elements defined in the
281 * structure is present or not.
282 * Par1: C structure name
283 * Par2: C structure element name
284 * Par3: number of lines to skip in the CSN_DESCR type specified if the
285 * element(s) does not exist
286 *****************************************************************************/
287#define M_NEXT_EXIST(_STRUCT, _MEMBER, _NoOfExisting)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400288 {CSN_NEXT_EXIST, _NoOfExisting, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300289
290/******************************************************************************
291 * M_NEXT_EXIST_LH(Par1, Par2, Par3)
292 * similar to the M_NEXT_EXIST except that instead of bit 0/1 which is fetched
293 * from the message in order to find out whether the next element/elements are
294 * present in the message, the logical operation XOR with the background
295 * pattern 0x2B is performed on the read bit before the decision is made.
296 *****************************************************************************/
297#define M_NEXT_EXIST_LH(_STRUCT, _MEMBER, _NoOfExisting)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400298 {CSN_NEXT_EXIST_LH, _NoOfExisting, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300299
300/******************************************************************************
301 * M_NEXT_EXIST_OR_NULL(Par1, Par2, Par3)
302 * Similar to the M_NEXT_EXIST except that not only bit 0 or 1 but also the end
303 * of the message may be encountered when looking for the next element in the
304 * message.
305 * Covers the case {null | 0 | 1 < IE >}
306 *****************************************************************************/
307#define M_NEXT_EXIST_OR_NULL(_STRUCT, _MEMBER, _NoOfExisting)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400308 {CSN_NEXT_EXIST, _NoOfExisting, {(void*)1}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300309
310/******************************************************************************
311 * M_NEXT_EXIST_OR_NULL_LH(Par1, Par2, Par3)
312 * Similar to the M_NEXT_EXIST_LH except that not only bit 0 or 1 but also the
313 * end of the message may be encountered when looking for the next element in
314 * the message.
315 * Covers the case {null | L | H < IE >}
316 *****************************************************************************/
317#define M_NEXT_EXIST_OR_NULL_LH(_STRUCT, _MEMBER, _NoOfExisting)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400318 {CSN_NEXT_EXIST_LH, _NoOfExisting, {(void*)1}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300319
320/******************************************************************************
321 * M_UINT(Par1, Par2, Par3)
322 * Defines an integer number.
323 * Par1: C structure name
324 * Par2: C structure element name
325 * Par3: number of bits used to code the element (between 1 and 32)
326 *****************************************************************************/
327#define M_UINT(_STRUCT, _MEMBER, _BITS)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400328 {CSN_UINT, _BITS, {(void*)1}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
329
330 /******************************************************************************
331 * M_UINT_OR_NULL(Par1, Par2, Par3)
332 * Similar to the M_UINT except that not only the request set of bits but also the
333 * end of the message may be encountered when looking for the next element in
334 * the message.
335 * Covers the case {null | 0 | 1 < IE >}
336 *****************************************************************************/
337 #define M_UINT_OR_NULL(_STRUCT, _MEMBER, _BITS)\
338 {CSN_UINT, _BITS, {0}, offsetof(_STRUCT, _MEMBER), TRUE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300339
340/******************************************************************************
341 * M_UINT(Par1, Par2, Par3)
342 * This macro has the same functionality as M_UINT except that in addition the
343 * logical "exclusive or" operation with the background value "0x2B" is
344 * performed before the final value of the integer number is delivered from the
345 * received CSN.1 message
346 *****************************************************************************/
347#define M_UINT_LH(_STRUCT, _MEMBER, _BITS)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400348 {CSN_UINT_LH, _BITS, {(void*)1}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300349
350/******************************************************************************
351 * M_UINT_OFFSET(Par1, Par2, Par3, Par4)
352 * Defines an integer number.
353 * Par1: C structure name
354 * Par2: C structure element name
355 * Par3: number of bits used to code the element (between 1 and 32)
356 * Par4: value added to the returned integer (offset)
357 *****************************************************************************/
358#define M_UINT_OFFSET(_STRUCT, _MEMBER, _BITS, _OFFSET)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400359 {CSN_UINT_OFFSET, _BITS, {(void*)_OFFSET}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300360
361/******************************************************************************
362 * M_UINT_ARRAY(Par1, Par2, Par3, Par4)
363 * Defines an array of integer numbers. The size of the array is fixed.
364 * Par1: C structure name
365 * Par2: C structure element name
366 * Par3: number of bits used to code the each integer element (between 1 and 32)
367 * Par4: number of elements in the array (fixed integer value)
368 *****************************************************************************/
369#define M_UINT_ARRAY(_STRUCT, _MEMBER, _BITS, _ElementCount)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400370 {CSN_UINT_ARRAY, _BITS, {(void*)_ElementCount}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300371
372/******************************************************************************
373 * M_VAR_UINT_ARRAY(Par1, Par2, Par3, Par4)
374 * Defines an array of integer numbers. The size of the array is variable.
375 * Par1: C structure name
376 * Par2: C structure element name
377 * Par3: number of bits used to code the each integer element (between 1 and 32)
378 * Par4: number of elements in the array supplied by reference to the
379 * structure member holding the length value
380 *****************************************************************************/
381#define M_VAR_UINT_ARRAY(_STRUCT, _MEMBER, _BITS, _ElementCountField)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400382 {CSN_UINT_ARRAY, _BITS, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)1}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300383
384/******************************************************************************
385 * M_VAR_ARRAY(Par1, Par2, Par3, Par4)
386 * Defines an array of 8 bit large integer numbers. The size of the array is variable.
387 * Par1: C structure name
388 * Par2: C structure element name
389 * Par3: name of the structure member holding the size of the array
390 * Par4: offset that is added to the Par3 to get the actual size of the array
391 *****************************************************************************/
392#define M_VAR_ARRAY(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400393 {CSN_VARIABLE_ARRAY, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300394
395/******************************************************************************
396 * M_VAR_TARRAY(Par1, Par2, Par3, Par4)
397 * Similar to M_TYPE_ARRAY except that the size of the array is variable.
398 * Par1: C structure name
399 * Par2: C structure element name
400 * Par3: the type of each element of the array
401 * Par4: name of the structure member holding the size of the array
402 *****************************************************************************/
403#define M_VAR_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400404 {CSN_VARIABLE_TARRAY, offsetof(_STRUCT, _ElementCountField), {(void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300405
406/******************************************************************************
407 * M_VAR_TARRAY_OFFSET(Par1, Par2, Par3, Par4)
408 * Same as M_VAR_TARRAY with offset
409 *****************************************************************************/
410#define M_VAR_TARRAY_OFFSET(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400411 {CSN_VARIABLE_TARRAY_OFFSET, offsetof(_STRUCT, _ElementCountField), {(void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300412
413/******************************************************************************
414 * M_REC_ARRAY(Par1, Par2, Par3, Par4)
415 * similar to the M_VAR_ARRAY. The difference is that the size of the array is
416 * not known in advance and it has to be calculated during unpacking. Its value
417 * is stored in a variable which belongs to the same structure as the array.
418 * A zero element terminates the array. The CSN.1 syntax describes it
419 * recursively as:
420 * <array> ::={1 <element> <array>| 0}
421 *
422 * Par1: C structure name
423 * Par2: C structure element name
424 * Par3: name of the structure member where the calculated the size of the
425 * array will be stored
426 * Par4: length of each element in bits
427 *****************************************************************************/
428#define M_REC_ARRAY(_STRUCT, _MEMBER, _ElementCountField, _BITS)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400429 {CSN_RECURSIVE_ARRAY, _BITS, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300430
431/******************************************************************************
432 * M_VAR_TYPE_ARRAY(Par1, Par2, Par3, Par4)
433 * Defines an array of structures. The size of the array is variable.
434 * Par1: C structure name
435 * Par2: C structure element name
436 * Par3: name of the structure
437 * Par4: number of elements in the array (fixed integer value)
438 *****************************************************************************/
439#define M_TYPE_ARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCount)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400440 {CSN_TYPE_ARRAY, _ElementCount, {(void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300441
442/******************************************************************************
443 * M_REC_TARRAY(Par1, Par2, Par3, Par4)
444 * Defines an recursive array of structures. The size of the array is variable.
445 * <list> ::= {1 <type>} ** 0 ;
446 * Par1: C structure name
447 * Par2: C structure element name
448 * Par3: name of the structure
449 * Par4: will hold the number of element in the array after unpacking
450 *****************************************************************************/
451#define M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400452 {CSN_RECURSIVE_TARRAY, offsetof(_STRUCT, _ElementCountField), {(void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300453
454/******************************************************************************
455 * M_REC_TARRAY1(Par1, Par2, Par3, Par4)
456 * Same as M_REC_TARRAY but first element always exist:
457 * <list> ::= <type> {1 <type>} ** 0 ;
458 *****************************************************************************/
459#define M_REC_TARRAY_1(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400460 {CSN_RECURSIVE_TARRAY_1, offsetof(_STRUCT, _ElementCountField), {(void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300461
462/******************************************************************************
463 * M_REC_TARRAY2(Par1, Par2, Par3, Par4)
464 * Same as M_REC_TARRAY but with reversed separators :
465 * <lists> ::= <type> { 0 <type> } ** 1 ;
466 *****************************************************************************/
467#define M_REC_TARRAY_2(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400468 {CSN_RECURSIVE_TARRAY_2, offsetof(_STRUCT, _ElementCountField), {(void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300469
470/******************************************************************************
471 * M_TYPE(Par1, Par2, Par3)
472 * Defines a reference to a structure which is described elsewhere
473 * <list> ::= {1 <type>} ** 0 ;
474 * Par1: C structure name
475 * Par2: C structure element name
476 * Par3: type of member
477 *****************************************************************************/
478#define M_TYPE(_STRUCT, _MEMBER, _MEMBER_TYPE)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400479 {CSN_TYPE, 0, {(void*)CSNDESCR_##_MEMBER_TYPE}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300480
481/******************************************************************************
482 * M_UNION(Par1, Par2)
483 * Informs the CSN.1 library that a union follows and how many possible choices
484 * there are in the union. The actual value of the choice, which points out the
485 * chosen element of the union is stored in the uint8 variable and is usually
486 * called UnionType. The elements of the union have to be listed directly after
487 * the M_UNION statement.
488 * Par1: C structure name
489 * Par2: number of possible choice in the union
490 *****************************************************************************/
491#define M_UNION(_STRUCT, _COUNT)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400492 {CSN_UNION, _COUNT, {0}, offsetof(_STRUCT, UnionType), FALSE, "UnionType", {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300493
494/******************************************************************************
495 * M_UNION_LH(Par1, Par2)
496 * Same as M_UNION but masked with background value 0x2B
497 *****************************************************************************/
498#define M_UNION_LH(_STRUCT, _COUNT)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400499 {CSN_UNION_LH, _COUNT, {0}, offsetof(_STRUCT, UnionType), FALSE, "UnionType", {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300500
501/******************************************************************************
502 * M_CHOICE(Par1, Par2, Par3, Par4)
503 * Similar to the M_UNION. In the M_UNION the selected element of all possible
504 * choices in the union is referred as a sequential numbers, i.e., the first
505 * choice is addressed as choice 0 the second as choice 1, the third as choice
506 * 2 and so on, both in the encoded message and in the variable UnionType which
507 * is the part of the message. In the CSN_CHOICE case, this rule does not
508 * apply. There is free but predefined mapping of the element of the union and
509 * the value which addresses this element.
510 * The value of the address is called a selector.
511 * After unpacking, this value is then converted to the sequential number of the
512 * element in the union and stored in the UnionType variable.
513 * Par1: C structure name
514 * Par2: C structure element name
515 * Par3: address of an array of type CSN_ChoiceElement_t where all possible
516 * values of the selector are provided, together with the selector
517 * length expressed in bits and the address of the CSN_DESCR type
518 * where the element is defined. For every element in the union
519 * there is one line in the Choice variable. These lines have to
520 * appear in the _CHOICE in the same order as the elements in the
521 * union. The element of the union selected in the message through
522 * the _CHOICE parameter is after unpacking translated to the
523 * corresponding sequential number of this element and stored in
524 * the variable pointed out by the _MEMBER
525 * Par4: number of possible choices in the union
526 *****************************************************************************/
527#define M_CHOICE(_STRUCT, _MEMBER, _CHOICE, _ElementCount)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400528 {CSN_CHOICE, _ElementCount, {(void*)_CHOICE}, offsetof(_STRUCT, _MEMBER), FALSE, #_CHOICE, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300529
530/******************************************************************************
531 * M_FIXED(Par1, Par2, Par3)
532 * Defines a fixed value of type integer which should be fetched from or stored
533 * in the message
534 * Par1: C structure name
535 * Par2: gives the length of the fixed number in bits.
536 * Par3: the value of the number. If the expected value is not present in
537* the message the unpacking procedure is aborted
538 *****************************************************************************/
539#define M_FIXED(_STRUCT, _BITS, _BITVALUE)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400540 {CSN_FIXED, _BITS, {0}, _BITVALUE, FALSE, #_BITVALUE, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300541
542/******************************************************************************
543 * M_SERIALIZE(Par1, Par2, Par3)
544 * Allows using a complete free format of data being encoded or decoded.
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400545 * When the M_SERIALIZE is encounted during encoding or decoding of a message
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300546 * the CSNstream program passes the control over to the specified function
547 * together with all necessary parameters about the current position within
548 * the message being unpacked or packed. When transferring of "serialized"
549 * data to or from the message is finished by the function the CSNstream gets
550 * back control over the data stream and continues to work with the message.
551 *****************************************************************************/
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400552#define M_SERIALIZE(_STRUCT, _MEMBER, _LENGTH_LEN, _SERIALIZEFCN)\
553 {CSN_SERIALIZE, _LENGTH_LEN, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {_SERIALIZEFCN}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300554
555#define M_CALLBACK(_STRUCT, _CSNCALLBACKFCN, _PARAM1, _PARAM2)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400556 {CSN_CALLBACK, offsetof(_STRUCT, _PARAM1), {_CSNCALLBACKFCN}, offsetof(_STRUCT, _PARAM2), FALSE, "CallBack_"#_CSNCALLBACKFCN, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300557
558/******************************************************************************
559 * M_BITMAP(Par1, Par2, Par3)
560 * Defines a type which consists of a bitmap. The size of the bitmap in bits
561 * is fixed and provided by the parameter Par3
562 * Par1: C structure name
563 * Par2: C structure element name
564 * Par3: length of the bitmap expressed in bits
565 *****************************************************************************/
566#define M_BITMAP(_STRUCT, _MEMBER, _BITS)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400567 {CSN_BITMAP, _BITS, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300568
569/* variable length, right aligned bitmap i.e. _ElementCountField = 11 => 00000111 11111111 */
570#define M_VAR_BITMAP(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400571 {CSN_VARIABLE_BITMAP, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300572
573/* variable length, right aligned bitmap filling the rest of message
574 * - when unpacking the _ElementCountField will be set in runtime
575 * - when packing _ElementCountField contains the size of bitmap
576 */
577#define M_VAR_BITMAP_1(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400578 {CSN_VARIABLE_BITMAP_1, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300579
580/* variable length, left aligned bitmap i.e. _ElementCountField = 11 => 11111111 11100000 */
581#define M_LEFT_VAR_BMP(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400582 {CSN_LEFT_ALIGNED_VAR_BMP, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300583
584/* variable length, left aligned bitmap filling the rest of message
585 *- when unpacking the _ElementCountField will be set in runtime
586 * - when packing _ElementCountField contains the size of bitmap
587 */
588#define M_LEFT_VAR_BMP_1(_STRUCT, _MEMBER, _ElementCountField, _OFFSET)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400589 {CSN_LEFT_ALIGNED_VAR_BMP_1, _OFFSET, {(void*)offsetof(_STRUCT, _ElementCountField)}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
590
591/* todo: dissect padding bits looking for unexpected extensions */
592#define M_PADDING_BITS(_STRUCT)\
593 {CSN_PADDING_BITS, 0, {0}, 0, TRUE, "Padding", {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300594
595#define M_NULL(_STRUCT, _MEMBER)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400596 {CSN_NULL, 0, {0}, offsetof(_STRUCT, _MEMBER), FALSE, #_MEMBER, {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300597
598#define M_THIS_EXIST(_STRUCT)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400599 {CSN_EXIST, 0, {0}, offsetof(_STRUCT, Exist), FALSE, "Exist", {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300600
601#define M_THIS_EXIST_LH(_STRUCT)\
Ivan Kluchnikov701d9f82012-10-10 19:43:37 +0400602 {CSN_EXIST_LH, 0, {0}, offsetof(_STRUCT, Exist), FALSE, "Exist", {(StreamSerializeFcn_t)0}}
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300603
604/* return value 0 if ok else discontionue the unpacking */
605typedef gint16 (*CsnCallBackFcn_t)(void* pv ,...);
606
607#define CSNDESCR(_FuncType) CSNDESCR_##_FuncType
608
609#endif /*_PACKET_CSN1_H_*/