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