Fix STATIC_ASSERT macro when using multiple .proto files.
[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.1.8-dev
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 #ifndef STATIC_ASSERT
29 #define STATIC_ASSERT(COND,MSG) typedef char STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
30 #define STATIC_ASSERT_MSG(MSG, LINE, COUNTER) STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
31 #define STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) static_assertion_##MSG##LINE##COUNTER
32 #endif
33
34 /* Number of required fields to keep track of
35  * (change here or on compiler command line). */
36 #ifndef PB_MAX_REQUIRED_FIELDS
37 #define PB_MAX_REQUIRED_FIELDS 64
38 #endif
39
40 #if PB_MAX_REQUIRED_FIELDS < 64
41 #error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
42 #endif
43
44 /* List of possible field types. These are used in the autogenerated code.
45  * Least-significant 4 bits tell the scalar type
46  * Most-significant 4 bits specify repeated/required/packed etc.
47  * 
48  * INT32 and UINT32 are treated the same, as are (U)INT64 and (S)FIXED*
49  * These types are simply casted to correct field type when they are
50  * assigned to the memory pointer.
51  * SINT* is different, though, because it is zig-zag coded.
52  */
53
54 typedef enum {
55     /************************
56      * Field contents types *
57      ************************/
58     
59     /* Numeric types */
60     PB_LTYPE_VARINT = 0x00, /* int32, uint32, int64, uint64, bool, enum */
61     PB_LTYPE_SVARINT = 0x01, /* sint32, sint64 */
62     PB_LTYPE_FIXED32 = 0x02, /* fixed32, sfixed32, float */
63     PB_LTYPE_FIXED64 = 0x03, /* fixed64, sfixed64, double */
64     
65     /* Marker for last packable field type. */
66     PB_LTYPE_LAST_PACKABLE = 0x03,
67     
68     /* Byte array with pre-allocated buffer.
69      * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
70     PB_LTYPE_BYTES = 0x04,
71     
72     /* String with pre-allocated buffer.
73      * data_size is the maximum length. */
74     PB_LTYPE_STRING = 0x05,
75     
76     /* Submessage
77      * submsg_fields is pointer to field descriptions */
78     PB_LTYPE_SUBMESSAGE = 0x06,
79     
80     /* Number of declared LTYPES */
81     PB_LTYPES_COUNT = 7,
82     PB_LTYPE_MASK = 0x0F,
83     
84     /******************
85      * Modifier flags *
86      ******************/
87     
88     /* Just the basic, write data at data_offset */
89     PB_HTYPE_REQUIRED = 0x00,
90     
91     /* Write true at size_offset */
92     PB_HTYPE_OPTIONAL = 0x10,
93     
94     /* Read to pre-allocated array
95      * Maximum number of entries is array_size,
96      * actual number is stored at size_offset */
97     PB_HTYPE_ARRAY = 0x20,
98     
99     /* Works for all required/optional/repeated fields.
100      * data_offset points to pb_callback_t structure.
101      * LTYPE should be 0 (it is ignored, but sometimes
102      * used to speculatively index an array). */
103     PB_HTYPE_CALLBACK = 0x30,
104     
105     PB_HTYPE_MASK = 0xF0
106 } pb_packed pb_type_t;
107
108 #define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
109 #define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
110
111 /* This structure is used in auto-generated constants
112  * to specify struct fields.
113  * You can change field sizes if you need structures
114  * larger than 256 bytes or field tags larger than 256.
115  * The compiler should complain if your .proto has such
116  * structures. Fix that by defining PB_FIELD_16BIT or
117  * PB_FIELD_32BIT.
118  */
119 typedef struct _pb_field_t pb_field_t;
120 struct _pb_field_t {
121
122 #if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
123     uint8_t tag;
124     pb_type_t type;
125     uint8_t data_offset; /* Offset of field data, relative to previous field. */
126     int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
127     uint8_t data_size; /* Data size in bytes for a single item */
128     uint8_t array_size; /* Maximum number of entries in array */
129 #elif defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
130     uint16_t tag;
131     pb_type_t type;
132     uint8_t data_offset;
133     int8_t size_offset;
134     uint16_t data_size;
135     uint16_t array_size;
136 #else
137     uint32_t tag;
138     pb_type_t type;
139     uint8_t data_offset;
140     int8_t size_offset;
141     uint32_t data_size;
142     uint32_t array_size;
143 #endif
144     
145     /* Field definitions for submessage
146      * OR default value for all other non-array, non-callback types
147      * If null, then field will zeroed. */
148     const void *ptr;
149 } pb_packed;
150
151 /* This structure is used for 'bytes' arrays.
152  * It has the number of bytes in the beginning, and after that an array.
153  * Note that actual structs used will have a different length of bytes array.
154  */
155 struct _pb_bytes_array_t {
156     size_t size;
157     uint8_t bytes[1];
158 };
159
160 typedef struct _pb_bytes_array_t pb_bytes_array_t;
161
162 /* This structure is used for giving the callback function.
163  * It is stored in the message structure and filled in by the method that
164  * calls pb_decode.
165  *
166  * The decoding callback will be given a limited-length stream
167  * If the wire type was string, the length is the length of the string.
168  * If the wire type was a varint/fixed32/fixed64, the length is the length
169  * of the actual value.
170  * The function may be called multiple times (especially for repeated types,
171  * but also otherwise if the message happens to contain the field multiple
172  * times.)
173  *
174  * The encoding callback will receive the actual output stream.
175  * It should write all the data in one call, including the field tag and
176  * wire type. It can write multiple fields.
177  *
178  * The callback can be null if you want to skip a field.
179  */
180 typedef struct _pb_istream_t pb_istream_t;
181 typedef struct _pb_ostream_t pb_ostream_t;
182 typedef struct _pb_callback_t pb_callback_t;
183 struct _pb_callback_t {
184     union {
185         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
186         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
187     } funcs;
188     
189     /* Free arg for use by callback */
190     void *arg;
191 };
192
193 /* Wire types. Library user needs these only in encoder callbacks. */
194 typedef enum {
195     PB_WT_VARINT = 0,
196     PB_WT_64BIT  = 1,
197     PB_WT_STRING = 2,
198     PB_WT_32BIT  = 5
199 } pb_wire_type_t;
200
201 /* These macros are used to declare pb_field_t's in the constant array. */
202 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
203 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
204 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
205 #define pb_delta_end(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
206 #define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0}
207
208 /* These macros are used for giving out error messages.
209  * They are mostly a debugging aid; the main error information
210  * is the true/false return value from functions.
211  * Some code space can be saved by disabling the error
212  * messages if not used.
213  */
214 #ifdef PB_NO_ERRMSG
215 #define PB_RETURN_ERROR(stream,msg) return false
216 #define PB_GET_ERROR(stream) "(errmsg disabled)"
217 #else
218 #define PB_RETURN_ERROR(stream,msg) \
219     do {\
220         if ((stream)->errmsg == NULL) \
221             (stream)->errmsg = (msg); \
222         return false; \
223     } while(0)
224 #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
225 #endif
226
227 #endif