Replace PB_MANY_FIELDS with PB_FIELD_16BIT and PB_FIELD_32BIT.
[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 #warning You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64). \
38          The automatic checks against too low value will not be active.
39 #endif
40
41 /* List of possible field types. These are used in the autogenerated code.
42  * Least-significant 4 bits tell the scalar type
43  * Most-significant 4 bits specify repeated/required/packed etc.
44  * 
45  * INT32 and UINT32 are treated the same, as are (U)INT64 and (S)FIXED*
46  * These types are simply casted to correct field type when they are
47  * assigned to the memory pointer.
48  * SINT* is different, though, because it is zig-zag coded.
49  */
50
51 typedef enum {
52     /************************
53      * Field contents types *
54      ************************/
55     
56     /* Numeric types */
57     PB_LTYPE_VARINT = 0x00, /* int32, uint32, int64, uint64, bool, enum */
58     PB_LTYPE_SVARINT = 0x01, /* sint32, sint64 */
59     PB_LTYPE_FIXED32 = 0x02, /* fixed32, sfixed32, float */
60     PB_LTYPE_FIXED64 = 0x03, /* fixed64, sfixed64, double */
61     
62     /* Marker for last packable field type. */
63     PB_LTYPE_LAST_PACKABLE = 0x03,
64     
65     /* Byte array with pre-allocated buffer.
66      * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
67     PB_LTYPE_BYTES = 0x04,
68     
69     /* String with pre-allocated buffer.
70      * data_size is the maximum length. */
71     PB_LTYPE_STRING = 0x05,
72     
73     /* Submessage
74      * submsg_fields is pointer to field descriptions */
75     PB_LTYPE_SUBMESSAGE = 0x06,
76     
77     /* Number of declared LTYPES */
78     PB_LTYPES_COUNT = 7,
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 } pb_packed pb_type_t;
101
102 #define PB_HTYPE(x) ((x) & 0xF0)
103 #define PB_LTYPE(x) ((x) & 0x0F)
104
105 /* This structure is used in auto-generated constants
106  * to specify struct fields.
107  * You can change field sizes if you need structures
108  * larger than 256 bytes or field tags larger than 256.
109  * The compiler should complain if your .proto has such
110  * structures. Fix that by defining PB_FIELD_16BIT or
111  * PB_FIELD_32BIT.
112  */
113 typedef struct _pb_field_t pb_field_t;
114 struct _pb_field_t {
115
116 #if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
117     uint8_t tag;
118     pb_type_t type;
119     uint8_t data_offset; /* Offset of field data, relative to previous field. */
120     int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
121     uint8_t data_size; /* Data size in bytes for a single item */
122     uint8_t array_size; /* Maximum number of entries in array */
123 #elif defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
124     uint16_t tag;
125     pb_type_t type;
126     uint8_t data_offset;
127     int8_t size_offset;
128     uint16_t data_size;
129     uint16_t array_size;
130 #else
131     uint32_t tag;
132     pb_type_t type;
133     uint8_t data_offset;
134     int8_t size_offset;
135     uint32_t data_size;
136     uint32_t array_size;
137 #endif
138     
139     /* Field definitions for submessage
140      * OR default value for all other non-array, non-callback types
141      * If null, then field will zeroed. */
142     const void *ptr;
143 } pb_packed;
144
145 /* This structure is used for 'bytes' arrays.
146  * It has the number of bytes in the beginning, and after that an array.
147  * Note that actual structs used will have a different length of bytes array.
148  */
149 typedef struct {
150     size_t size;
151     uint8_t bytes[1];
152 } pb_bytes_array_t;
153
154 /* This structure is used for giving the callback function.
155  * It is stored in the message structure and filled in by the method that
156  * calls pb_decode.
157  *
158  * The decoding callback will be given a limited-length stream
159  * If the wire type was string, the length is the length of the string.
160  * If the wire type was a varint/fixed32/fixed64, the length is the length
161  * of the actual value.
162  * The function may be called multiple times (especially for repeated types,
163  * but also otherwise if the message happens to contain the field multiple
164  * times.)
165  *
166  * The encoding callback will receive the actual output stream.
167  * It should write all the data in one call, including the field tag and
168  * wire type. It can write multiple fields.
169  *
170  * The callback can be null if you want to skip a field.
171  */
172 typedef struct _pb_istream_t pb_istream_t;
173 typedef struct _pb_ostream_t pb_ostream_t;
174 typedef struct _pb_callback_t pb_callback_t;
175 struct _pb_callback_t {
176     union {
177         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
178         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
179     } funcs;
180     
181     /* Free arg for use by callback */
182     void *arg;
183 };
184
185 /* Wire types. Library user needs these only in encoder callbacks. */
186 typedef enum {
187     PB_WT_VARINT = 0,
188     PB_WT_64BIT  = 1,
189     PB_WT_STRING = 2,
190     PB_WT_32BIT  = 5
191 } pb_wire_type_t;
192
193 /* These macros are used to declare pb_field_t's in the constant array. */
194 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
195 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
196 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
197 #define pb_delta_end(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
198 #define PB_LAST_FIELD {0,0,0,0}
199
200
201 #endif