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
14 /* This just reduces memory requirements, but is not required. */
15 #define pb_packed __attribute__((packed))
20 /* Handly macro for suppressing unreferenced-parameter compiler warnings. */
22 #define UNUSED(x) (void)(x)
25 /* Compile-time assertion, used for checking compatible compilation options. */
27 #define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1];
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
36 /* List of possible field types. These are used in the autogenerated code.
37 * Least-significant 4 bits tell the scalar type
38 * Most-significant 4 bits specify repeated/required/packed etc.
40 * INT32 and UINT32 are treated the same, as are (U)INT64 and (S)FIXED*
41 * These types are simply casted to correct field type when they are
42 * assigned to the memory pointer.
43 * SINT* is different, though, because it is zig-zag coded.
47 /************************
48 * Field contents types *
49 ************************/
52 PB_LTYPE_VARINT = 0x00, /* int32, uint32, int64, uint64, bool, enum */
53 PB_LTYPE_SVARINT = 0x01, /* sint32, sint64 */
54 PB_LTYPE_FIXED32 = 0x02, /* fixed32, sfixed32, float */
55 PB_LTYPE_FIXED64 = 0x03, /* fixed64, sfixed64, double */
57 /* Marker for last packable field type. */
58 PB_LTYPE_LAST_PACKABLE = 0x03,
60 /* Byte array with pre-allocated buffer.
61 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
62 PB_LTYPE_BYTES = 0x04,
64 /* String with pre-allocated buffer.
65 * data_size is the maximum length. */
66 PB_LTYPE_STRING = 0x05,
69 * submsg_fields is pointer to field descriptions */
70 PB_LTYPE_SUBMESSAGE = 0x06,
72 /* Number of declared LTYPES */
79 /* Just the basic, write data at data_offset */
80 PB_HTYPE_REQUIRED = 0x00,
82 /* Write true at size_offset */
83 PB_HTYPE_OPTIONAL = 0x10,
85 /* Read to pre-allocated array
86 * Maximum number of entries is array_size,
87 * actual number is stored at size_offset */
88 PB_HTYPE_ARRAY = 0x20,
90 /* Works for all required/optional/repeated fields.
91 * data_offset points to pb_callback_t structure.
92 * LTYPE should be 0 (it is ignored, but sometimes
93 * used to speculatively index an array). */
94 PB_HTYPE_CALLBACK = 0x30
95 } pb_packed pb_type_t;
97 #define PB_HTYPE(x) ((x) & 0xF0)
98 #define PB_LTYPE(x) ((x) & 0x0F)
100 /* This structure is used in auto-generated constants
101 * to specify struct fields.
102 * You can change field sizes here if you need structures
103 * larger than 256 bytes or field tags larger than 256.
104 * The compiler should complain if your .proto has such
105 * structures ("initializer too large for type").
107 typedef struct _pb_field_t pb_field_t;
110 #ifndef PB_MANY_FIELDS
113 uint8_t data_offset; /* Offset of field data, relative to previous field. */
114 int8_t size_offset; /* Offset of array size or has-boolean, relative to data */
115 uint8_t data_size; /* Data size in bytes for a single item */
116 uint8_t array_size; /* Maximum number of entries in array */
126 /* Field definitions for submessage
127 * OR default value for all other non-array, non-callback types
128 * If null, then field will zeroed. */
132 /* This structure is used for 'bytes' arrays.
133 * It has the number of bytes in the beginning, and after that an array.
134 * Note that actual structs used will have a different length of bytes array.
141 /* This structure is used for giving the callback function.
142 * It is stored in the message structure and filled in by the method that
145 * The decoding callback will be given a limited-length stream
146 * If the wire type was string, the length is the length of the string.
147 * If the wire type was a varint/fixed32/fixed64, the length is the length
148 * of the actual value.
149 * The function may be called multiple times (especially for repeated types,
150 * but also otherwise if the message happens to contain the field multiple
153 * The encoding callback will receive the actual output stream.
154 * It should write all the data in one call, including the field tag and
155 * wire type. It can write multiple fields.
157 * The callback can be null if you want to skip a field.
159 typedef struct _pb_istream_t pb_istream_t;
160 typedef struct _pb_ostream_t pb_ostream_t;
161 typedef struct _pb_callback_t pb_callback_t;
162 struct _pb_callback_t {
164 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
165 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
168 /* Free arg for use by callback */
172 /* Wire types. Library user needs these only in encoder callbacks. */
180 /* These macros are used to declare pb_field_t's in the constant array. */
181 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
182 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
183 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
184 #define pb_delta_end(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
185 #define PB_LAST_FIELD {0,0,0,0}