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