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