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