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