Add section in pb.h for changing compilation settings.
[apps/agl-service-can-low-level.git] / pb.h
1 /* Common parts of the nanopb library. Most of these are quite low-level
2  * stuff. For the high-level interface, see pb_encode.h and pb_decode.h.
3  */
4
5 #ifndef _PB_H_
6 #define _PB_H_
7
8 /*****************************************************************
9  * Nanopb compilation time options. You can change these here by *
10  * uncommenting the lines, or on the compiler command line.      *
11  *****************************************************************/
12
13 /* Define this if your CPU architecture is big endian, i.e. it
14  * stores the most-significant byte first. */
15 /* #define __BIG_ENDIAN__ 1 */
16
17 /* Increase the number of required fields that are tracked.
18  * A compiler warning will tell if you need this. */
19 /* #define PB_MAX_REQUIRED_FIELDS 256 */
20
21 /* Add support for tag numbers > 255 and fields larger than 255 bytes. */
22 /* #define PB_FIELD_16BIT 1 */
23
24 /* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */
25 /* #define PB_FIELD_32BIT 1 */
26
27 /* Disable support for error messages in order to save some code space. */
28 /* #define PB_NO_ERRMSG 1 */
29
30 /* Disable support for custom streams (support only memory buffers). */
31 /* #define PB_BUFFER_ONLY 1 */
32
33 /* Switch back to the old-style callback function signature.
34  * This was the default until nanopb-0.2.1. */
35 /* #define PB_OLD_CALLBACK_STYLE */
36
37
38 /******************************************************************
39  * You usually don't need to change anything below this line.     *
40  * Feel free to look around and use the defined macros, though.   *
41  ******************************************************************/
42
43
44 /* Version of the nanopb library. Just in case you want to check it in
45  * your own program. */
46 #define NANOPB_VERSION nanopb-0.2.2-dev
47
48 /* Include all the system headers needed by nanopb. You will need the
49  * definitions of the following:
50  * - strlen, memcpy, memset functions
51  * - [u]int8_t, [u]int16_t, [u]int32_t, [u]int64_t
52  * - size_t
53  * - bool
54  *
55  * If you don't have the standard header files, you can instead provide
56  * a custom header that defines or includes all this. In that case,
57  * define PB_SYSTEM_HEADER to the path of this file.
58  */
59 #ifdef PB_SYSTEM_HEADER
60 #include PB_SYSTEM_HEADER
61 #else
62 #include <stdint.h>
63 #include <stddef.h>
64 #include <stdbool.h>
65 #include <string.h>
66 #endif
67
68 /* Macro for defining packed structures (compiler dependent).
69  * This just reduces memory requirements, but is not required.
70  */
71 #if defined(__GNUC__) || defined(__clang__)
72     /* For GCC and clang */
73 #   define PB_PACKED_STRUCT_START
74 #   define PB_PACKED_STRUCT_END
75 #   define pb_packed __attribute__((packed))
76 #elif defined(__ICCARM__)
77     /* For IAR ARM compiler */
78 #   define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)")
79 #   define PB_PACKED_STRUCT_END _Pragma("pack(pop)")
80 #   define pb_packed
81 #elif defined(_MSC_VER) && (_MSC_VER >= 1500)
82     /* For Microsoft Visual C++ */
83 #   define PB_PACKED_STRUCT_START __pragma(pack(push, 1))
84 #   define PB_PACKED_STRUCT_END __pragma(pack(pop))
85 #   define pb_packed
86 #else
87     /* Unknown compiler */
88 #   define PB_PACKED_STRUCT_START
89 #   define PB_PACKED_STRUCT_END
90 #   define pb_packed
91 #endif
92
93 /* Handly macro for suppressing unreferenced-parameter compiler warnings. */
94 #ifndef UNUSED
95 #define UNUSED(x) (void)(x)
96 #endif
97
98 /* Compile-time assertion, used for checking compatible compilation options.
99  * If this fails on your compiler for some reason, use #define STATIC_ASSERT
100  * to disable it. */
101 #ifndef STATIC_ASSERT
102 #define STATIC_ASSERT(COND,MSG) typedef char STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
103 #define STATIC_ASSERT_MSG(MSG, LINE, COUNTER) STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
104 #define STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) static_assertion_##MSG##LINE##COUNTER
105 #endif
106
107 /* Number of required fields to keep track of. */
108 #ifndef PB_MAX_REQUIRED_FIELDS
109 #define PB_MAX_REQUIRED_FIELDS 64
110 #endif
111
112 #if PB_MAX_REQUIRED_FIELDS < 64
113 #error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
114 #endif
115
116 /* List of possible field types. These are used in the autogenerated code.
117  * Least-significant 4 bits tell the scalar type
118  * Most-significant 4 bits specify repeated/required/packed etc.
119  * 
120  * INT32 and UINT32 are treated the same, as are (U)INT64 and (S)FIXED*
121  * These types are simply casted to correct field type when they are
122  * assigned to the memory pointer.
123  * SINT* is different, though, because it is zig-zag coded.
124  */
125
126 typedef uint8_t pb_type_t;
127
128 /**** Field data types ****/
129
130 /* Numeric types */
131 #define PB_LTYPE_VARINT 0x00 /* int32, uint32, int64, uint64, bool, enum */
132 #define PB_LTYPE_SVARINT 0x01 /* sint32, sint64 */
133 #define PB_LTYPE_FIXED32 0x02 /* fixed32, sfixed32, float */
134 #define PB_LTYPE_FIXED64 0x03 /* fixed64, sfixed64, double */
135
136 /* Marker for last packable field type. */
137 #define PB_LTYPE_LAST_PACKABLE 0x03
138
139 /* Byte array with pre-allocated buffer.
140  * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
141 #define PB_LTYPE_BYTES 0x04
142
143 /* String with pre-allocated buffer.
144  * data_size is the maximum length. */
145 #define PB_LTYPE_STRING 0x05
146
147 /* Submessage
148  * submsg_fields is pointer to field descriptions */
149 #define PB_LTYPE_SUBMESSAGE 0x06
150
151 /* Number of declared LTYPES */
152 #define PB_LTYPES_COUNT 7
153 #define PB_LTYPE_MASK 0x0F
154
155 /**** Field repetition rules ****/
156
157 #define PB_HTYPE_REQUIRED 0x00
158 #define PB_HTYPE_OPTIONAL 0x10
159 #define PB_HTYPE_REPEATED 0x20
160 #define PB_HTYPE_MASK     0x30
161
162 /**** Field allocation types ****/
163  
164 #define PB_ATYPE_STATIC   0x00
165 #define PB_ATYPE_CALLBACK 0x40
166 #define PB_ATYPE_MASK     0xC0
167
168 #define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
169 #define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
170 #define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
171
172 /* This structure is used in auto-generated constants
173  * to specify struct fields.
174  * You can change field sizes if you need structures
175  * larger than 256 bytes or field tags larger than 256.
176  * The compiler should complain if your .proto has such
177  * structures. Fix that by defining PB_FIELD_16BIT or
178  * PB_FIELD_32BIT.
179  */
180 PB_PACKED_STRUCT_START
181 typedef struct _pb_field_t pb_field_t;
182 struct _pb_field_t {
183
184 #if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
185     uint8_t tag;
186     pb_type_t type;
187     uint8_t data_offset; /* Offset of field data, relative to previous field. */
188     int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
189     uint8_t data_size; /* Data size in bytes for a single item */
190     uint8_t array_size; /* Maximum number of entries in array */
191 #elif defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
192     uint16_t tag;
193     pb_type_t type;
194     uint8_t data_offset;
195     int8_t size_offset;
196     uint16_t data_size;
197     uint16_t array_size;
198 #else
199     uint32_t tag;
200     pb_type_t type;
201     uint8_t data_offset;
202     int8_t size_offset;
203     uint32_t data_size;
204     uint32_t array_size;
205 #endif
206     
207     /* Field definitions for submessage
208      * OR default value for all other non-array, non-callback types
209      * If null, then field will zeroed. */
210     const void *ptr;
211 } pb_packed;
212 PB_PACKED_STRUCT_END
213
214 /* This structure is used for 'bytes' arrays.
215  * It has the number of bytes in the beginning, and after that an array.
216  * Note that actual structs used will have a different length of bytes array.
217  */
218 struct _pb_bytes_array_t {
219     size_t size;
220     uint8_t bytes[1];
221 };
222
223 typedef struct _pb_bytes_array_t pb_bytes_array_t;
224
225 /* This structure is used for giving the callback function.
226  * It is stored in the message structure and filled in by the method that
227  * calls pb_decode.
228  *
229  * The decoding callback will be given a limited-length stream
230  * If the wire type was string, the length is the length of the string.
231  * If the wire type was a varint/fixed32/fixed64, the length is the length
232  * of the actual value.
233  * The function may be called multiple times (especially for repeated types,
234  * but also otherwise if the message happens to contain the field multiple
235  * times.)
236  *
237  * The encoding callback will receive the actual output stream.
238  * It should write all the data in one call, including the field tag and
239  * wire type. It can write multiple fields.
240  *
241  * The callback can be null if you want to skip a field.
242  */
243 typedef struct _pb_istream_t pb_istream_t;
244 typedef struct _pb_ostream_t pb_ostream_t;
245 typedef struct _pb_callback_t pb_callback_t;
246 struct _pb_callback_t {
247 #ifdef PB_OLD_CALLBACK_STYLE
248     /* Deprecated since nanopb-0.2.1 */
249     union {
250         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
251         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
252     } funcs;
253 #else
254     /* New function signature, which allows modifying arg contents in callback. */
255     union {
256         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
257         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
258     } funcs;
259 #endif    
260     
261     /* Free arg for use by callback */
262     void *arg;
263 };
264
265 /* Wire types. Library user needs these only in encoder callbacks. */
266 typedef enum {
267     PB_WT_VARINT = 0,
268     PB_WT_64BIT  = 1,
269     PB_WT_STRING = 2,
270     PB_WT_32BIT  = 5
271 } pb_wire_type_t;
272
273 /* These macros are used to declare pb_field_t's in the constant array. */
274 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
275 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
276 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
277 #define pb_delta_end(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \
278                                   ? offsetof(st, m1) \
279                                   : offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
280 #define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0}
281
282 /* Required fields are the simplest. They just have delta (padding) from
283  * previous field end, and the size of the field. Pointer is used for
284  * submessages and default values.
285  */
286 #define PB_REQUIRED_STATIC(tag, st, m, pm, ltype, ptr) \
287     {tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \
288     pb_delta_end(st, m, pm), 0, pb_membersize(st, m), 0, ptr}
289
290 /* Optional fields add the delta to the has_ variable. */
291 #define PB_OPTIONAL_STATIC(tag, st, m, pm, ltype, ptr) \
292     {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
293     pb_delta_end(st, m, pm), \
294     pb_delta(st, has_ ## m, m), \
295     pb_membersize(st, m), 0, ptr}
296
297 /* Repeated fields have a _count field and also the maximum number of entries. */
298 #define PB_REPEATED_STATIC(tag, st, m, pm, ltype, ptr) \
299     {tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype, \
300     pb_delta_end(st, m, pm), \
301     pb_delta(st, m ## _count, m), \
302     pb_membersize(st, m[0]), \
303     pb_arraysize(st, m), ptr}
304
305 /* Callbacks are much like required fields except with special datatype. */
306 #define PB_REQUIRED_CALLBACK(tag, st, m, pm, ltype, ptr) \
307     {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype, \
308     pb_delta_end(st, m, pm), 0, pb_membersize(st, m), 0, ptr}
309
310 #define PB_OPTIONAL_CALLBACK(tag, st, m, pm, ltype, ptr) \
311     {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
312     pb_delta_end(st, m, pm), 0, pb_membersize(st, m), 0, ptr}
313     
314 #define PB_REPEATED_CALLBACK(tag, st, m, pm, ltype, ptr) \
315     {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \
316     pb_delta_end(st, m, pm), 0, pb_membersize(st, m), 0, ptr}
317
318 /* The mapping from protobuf types to LTYPEs is done using these macros. */
319 #define PB_LTYPE_MAP_BOOL       PB_LTYPE_VARINT
320 #define PB_LTYPE_MAP_BYTES      PB_LTYPE_BYTES
321 #define PB_LTYPE_MAP_DOUBLE     PB_LTYPE_FIXED64
322 #define PB_LTYPE_MAP_ENUM       PB_LTYPE_VARINT
323 #define PB_LTYPE_MAP_FIXED32    PB_LTYPE_FIXED32
324 #define PB_LTYPE_MAP_FIXED64    PB_LTYPE_FIXED64
325 #define PB_LTYPE_MAP_FLOAT      PB_LTYPE_FIXED32
326 #define PB_LTYPE_MAP_INT32      PB_LTYPE_VARINT
327 #define PB_LTYPE_MAP_INT64      PB_LTYPE_VARINT
328 #define PB_LTYPE_MAP_MESSAGE    PB_LTYPE_SUBMESSAGE
329 #define PB_LTYPE_MAP_SFIXED32   PB_LTYPE_FIXED32
330 #define PB_LTYPE_MAP_SFIXED64   PB_LTYPE_FIXED64
331 #define PB_LTYPE_MAP_SINT32     PB_LTYPE_SVARINT
332 #define PB_LTYPE_MAP_SINT64     PB_LTYPE_SVARINT
333 #define PB_LTYPE_MAP_STRING     PB_LTYPE_STRING
334 #define PB_LTYPE_MAP_UINT32     PB_LTYPE_VARINT
335 #define PB_LTYPE_MAP_UINT64     PB_LTYPE_VARINT
336
337 /* This is the actual macro used in field descriptions.
338  * It takes these arguments:
339  * - Field tag number
340  * - Field type:   BOOL, BYTES, DOUBLE, ENUM, FIXED32, FIXED64,
341  *                 FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64
342  *                 SINT32, SINT64, STRING, UINT32 or UINT64
343  * - Field rules:  REQUIRED, OPTIONAL or REPEATED
344  * - Allocation:   STATIC or CALLBACK
345  * - Message name
346  * - Field name
347  * - Previous field name (or field name again for first field)
348  * - Pointer to default value or submsg fields.
349  */
350
351 #define PB_FIELD(tag, type, rules, allocation, message, field, prevfield, ptr) \
352     PB_ ## rules ## _ ## allocation(tag, message, field, prevfield, \
353                                     PB_LTYPE_MAP_ ## type, ptr)
354
355 /* These macros are used for giving out error messages.
356  * They are mostly a debugging aid; the main error information
357  * is the true/false return value from functions.
358  * Some code space can be saved by disabling the error
359  * messages if not used.
360  */
361 #ifdef PB_NO_ERRMSG
362 #define PB_RETURN_ERROR(stream,msg) return false
363 #define PB_GET_ERROR(stream) "(errmsg disabled)"
364 #else
365 #define PB_RETURN_ERROR(stream,msg) \
366     do {\
367         if ((stream)->errmsg == NULL) \
368             (stream)->errmsg = (msg); \
369         return false; \
370     } while(0)
371 #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
372 #endif
373
374 #endif