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