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