First version of header generator
[apps/agl-service-can-low-level.git] / pb.h
1 #ifndef _PB_H_
2 #define _PB_H_
3
4 #include <stdint.h>
5 #include <stddef.h>
6 #include <stdbool.h>
7
8 #ifdef __GNUC__
9 /* This just reduces memory requirements, but is not required. */
10 #define pb_packed __attribute__((packed))
11 #else
12 #define pb_packed
13 #endif
14
15 /* Lightweight output stream. */
16 typedef struct _pb_ostream_t pb_ostream_t;
17 struct _pb_ostream_t
18 {
19     bool (*callback)(pb_ostream_t *stream, const uint8_t *buf, size_t count);
20     void *state; /* Free field for use by callback implementation */
21     size_t bytes_written;
22 };
23
24 /*static inline bool pb_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
25 {
26     bool status = stream->callback(stream, buf, count);
27     stream->bytes_written += count;
28     return status;
29 }*/
30
31 /* List of possible field types
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_FIXED = 0x02, /* fixed32, sfixed32, fixed64, sfixed64, float, double */
50     
51     /* Marker for last packable field type. */
52     PB_LTYPE_LAST_PACKABLE = 0x02,
53     
54     /* Byte array with pre-allocated buffer.
55      * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
56     PB_LTYPE_BYTES = 0x03,
57     
58     /* String with pre-allocated buffer.
59      * data_size is the maximum length. */
60     PB_LTYPE_STRING = 0x04,
61     
62     /* Submessage
63      * submsg_fields is pointer to field descriptions */
64     PB_LTYPE_SUBMESSAGE = 0x05,
65     
66     /* Number of declared LTYPES */
67     PB_LTYPES_COUNT = 6,
68     
69     /******************
70      * Modifier flags *
71      ******************/
72     
73     /* Just the basic, write data at data_offset */
74     PB_HTYPE_REQUIRED = 0x00,
75     
76     /* Write true at size_offset */
77     PB_HTYPE_OPTIONAL = 0x10,
78     
79     /* Read to pre-allocated array
80      * Maximum number of entries is array_size,
81      * actual number is stored at size_offset */
82     PB_HTYPE_ARRAY = 0x20,
83     
84     /* Works for all required/optional/repeated fields.
85      * data_offset points to pb_callback_t structure.
86      * LTYPE is ignored. */
87     PB_HTYPE_CALLBACK = 0x30
88 } pb_packed pb_type_t;
89
90 #define PB_HTYPE(x) ((x) & 0xF0)
91 #define PB_LTYPE(x) ((x) & 0x0F)
92
93 /* This structure is used in auto-generated constants
94  * to specify struct fields.
95  * You can change field sizes here if you need structures
96  * larger than 256 bytes or field tags larger than 256.
97  * The compiler should complain if your .proto has such
98  * structures ("initializer too large for type").
99  */
100 typedef struct _pb_field_t pb_field_t;
101 struct _pb_field_t {
102     uint8_t tag;
103     pb_type_t type;
104     uint8_t data_offset; /* Offset of field data, relative to previous field. */
105     int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
106     uint8_t data_size; /* Data size in bytes for a single item */
107     uint8_t array_size; /* Maximum number of entries in array */
108     
109     /* Field definitions for submessage
110      * OR default value for all other non-array, non-callback types
111      * If null, then field will zeroed. */
112     const void *ptr;
113 } pb_packed;
114
115 /* This structure is used for 'bytes' arrays.
116  * It has the number of bytes in the beginning, and after that an array. */
117 #define PB_BYTES_ARRAY(buffersize) \
118 struct { \
119     size_t size; \
120     uint8_t bytes[buffersize]; \
121 }
122
123 typedef PB_BYTES_ARRAY() pb_bytes_array_t;
124
125 /* This structure is used for giving the callback function.
126  * It is stored in the message structure and filled in by the method that
127  * calls pb_decode.
128  *
129  * The decoding callback will be given a limited-length stream
130  * If the wire type was string, the length is the length of the string.
131  * If the wire type was a varint/fixed32/fixed64, the length is the length
132  * of the actual value.
133  * The function may be called multiple times (especially for repeated types,
134  * but also otherwise if the message happens to contain the field multiple
135  * times.)
136  *
137  * The encoding callback will receive the actual output stream.
138  * It should write all the data in one call, including the field tag and
139  * wire type. It can write multiple fields.
140  */
141 typedef struct _pb_istream_t pb_istream_t;
142 typedef struct _pb_callback_t pb_callback_t;
143 struct _pb_callback_t {
144     union {
145         bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
146         bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void *arg);
147     } funcs;
148     
149     /* Free arg for use by callback */
150     void *arg;
151 };
152
153 /* These macros are used to declare pb_field_t's in the constant array. */
154 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
155 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
156 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
157 #define PB_LAST_FIELD {0,0,0,0}
158
159
160 #endif