Nanopb (lightweight version of googles protobuf) test. It is not working as it should yet.

Dependencies:   MODSERIAL mbed

Committer:
Tomas
Date:
Wed Apr 09 11:44:14 2014 +0000
Revision:
3:fd0e1bc80f78
Parent:
0:bada2c7bd577
removed unnecessary libraries

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tomas 0:bada2c7bd577 1 /* Common parts of the nanopb library. Most of these are quite low-level
Tomas 0:bada2c7bd577 2 * stuff. For the high-level interface, see pb_encode.h and pb_decode.h.
Tomas 0:bada2c7bd577 3 */
Tomas 0:bada2c7bd577 4
Tomas 0:bada2c7bd577 5 #ifndef _PB_H_
Tomas 0:bada2c7bd577 6 #define _PB_H_
Tomas 0:bada2c7bd577 7
Tomas 0:bada2c7bd577 8 /*****************************************************************
Tomas 0:bada2c7bd577 9 * Nanopb compilation time options. You can change these here by *
Tomas 0:bada2c7bd577 10 * uncommenting the lines, or on the compiler command line. *
Tomas 0:bada2c7bd577 11 *****************************************************************/
Tomas 0:bada2c7bd577 12
Tomas 0:bada2c7bd577 13 /* Define this if your CPU architecture is big endian, i.e. it
Tomas 0:bada2c7bd577 14 * stores the most-significant byte first. */
Tomas 0:bada2c7bd577 15 /* #define __BIG_ENDIAN__ 1 */
Tomas 0:bada2c7bd577 16
Tomas 0:bada2c7bd577 17 /* Increase the number of required fields that are tracked.
Tomas 0:bada2c7bd577 18 * A compiler warning will tell if you need this. */
Tomas 0:bada2c7bd577 19 /* #define PB_MAX_REQUIRED_FIELDS 256 */
Tomas 0:bada2c7bd577 20
Tomas 0:bada2c7bd577 21 /* Add support for tag numbers > 255 and fields larger than 255 bytes. */
Tomas 0:bada2c7bd577 22 /* #define PB_FIELD_16BIT 1 */
Tomas 0:bada2c7bd577 23
Tomas 0:bada2c7bd577 24 /* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */
Tomas 0:bada2c7bd577 25 /* #define PB_FIELD_32BIT 1 */
Tomas 0:bada2c7bd577 26
Tomas 0:bada2c7bd577 27 /* Disable support for error messages in order to save some code space. */
Tomas 0:bada2c7bd577 28 /* #define PB_NO_ERRMSG 1 */
Tomas 0:bada2c7bd577 29
Tomas 0:bada2c7bd577 30 /* Disable support for custom streams (support only memory buffers). */
Tomas 0:bada2c7bd577 31 /* #define PB_BUFFER_ONLY 1 */
Tomas 0:bada2c7bd577 32
Tomas 0:bada2c7bd577 33 /* Switch back to the old-style callback function signature.
Tomas 0:bada2c7bd577 34 * This was the default until nanopb-0.2.1. */
Tomas 0:bada2c7bd577 35 /* #define PB_OLD_CALLBACK_STYLE */
Tomas 0:bada2c7bd577 36
Tomas 0:bada2c7bd577 37
Tomas 0:bada2c7bd577 38 /******************************************************************
Tomas 0:bada2c7bd577 39 * You usually don't need to change anything below this line. *
Tomas 0:bada2c7bd577 40 * Feel free to look around and use the defined macros, though. *
Tomas 0:bada2c7bd577 41 ******************************************************************/
Tomas 0:bada2c7bd577 42
Tomas 0:bada2c7bd577 43
Tomas 0:bada2c7bd577 44 /* Version of the nanopb library. Just in case you want to check it in
Tomas 0:bada2c7bd577 45 * your own program. */
Tomas 0:bada2c7bd577 46 #define NANOPB_VERSION nanopb-0.2.5
Tomas 0:bada2c7bd577 47
Tomas 0:bada2c7bd577 48 /* Include all the system headers needed by nanopb. You will need the
Tomas 0:bada2c7bd577 49 * definitions of the following:
Tomas 0:bada2c7bd577 50 * - strlen, memcpy, memset functions
Tomas 0:bada2c7bd577 51 * - [u]int8_t, [u]int16_t, [u]int32_t, [u]int64_t
Tomas 0:bada2c7bd577 52 * - size_t
Tomas 0:bada2c7bd577 53 * - bool
Tomas 0:bada2c7bd577 54 *
Tomas 0:bada2c7bd577 55 * If you don't have the standard header files, you can instead provide
Tomas 0:bada2c7bd577 56 * a custom header that defines or includes all this. In that case,
Tomas 0:bada2c7bd577 57 * define PB_SYSTEM_HEADER to the path of this file.
Tomas 0:bada2c7bd577 58 */
Tomas 0:bada2c7bd577 59 #ifdef PB_SYSTEM_HEADER
Tomas 0:bada2c7bd577 60 #include PB_SYSTEM_HEADER
Tomas 0:bada2c7bd577 61 #else
Tomas 0:bada2c7bd577 62 #include <stdint.h>
Tomas 0:bada2c7bd577 63 #include <stddef.h>
Tomas 0:bada2c7bd577 64 #include <stdbool.h>
Tomas 0:bada2c7bd577 65 #include <string.h>
Tomas 0:bada2c7bd577 66 #endif
Tomas 0:bada2c7bd577 67
Tomas 0:bada2c7bd577 68 /* Macro for defining packed structures (compiler dependent).
Tomas 0:bada2c7bd577 69 * This just reduces memory requirements, but is not required.
Tomas 0:bada2c7bd577 70 */
Tomas 0:bada2c7bd577 71 #if defined(__GNUC__) || defined(__clang__)
Tomas 0:bada2c7bd577 72 /* For GCC and clang */
Tomas 0:bada2c7bd577 73 # define PB_PACKED_STRUCT_START
Tomas 0:bada2c7bd577 74 # define PB_PACKED_STRUCT_END
Tomas 0:bada2c7bd577 75 # define pb_packed __attribute__((packed))
Tomas 0:bada2c7bd577 76 #elif defined(__ICCARM__)
Tomas 0:bada2c7bd577 77 /* For IAR ARM compiler */
Tomas 0:bada2c7bd577 78 # define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)")
Tomas 0:bada2c7bd577 79 # define PB_PACKED_STRUCT_END _Pragma("pack(pop)")
Tomas 0:bada2c7bd577 80 # define pb_packed
Tomas 0:bada2c7bd577 81 #elif defined(_MSC_VER) && (_MSC_VER >= 1500)
Tomas 0:bada2c7bd577 82 /* For Microsoft Visual C++ */
Tomas 0:bada2c7bd577 83 # define PB_PACKED_STRUCT_START __pragma(pack(push, 1))
Tomas 0:bada2c7bd577 84 # define PB_PACKED_STRUCT_END __pragma(pack(pop))
Tomas 0:bada2c7bd577 85 # define pb_packed
Tomas 0:bada2c7bd577 86 #else
Tomas 0:bada2c7bd577 87 /* Unknown compiler */
Tomas 0:bada2c7bd577 88 # define PB_PACKED_STRUCT_START
Tomas 0:bada2c7bd577 89 # define PB_PACKED_STRUCT_END
Tomas 0:bada2c7bd577 90 # define pb_packed
Tomas 0:bada2c7bd577 91 #endif
Tomas 0:bada2c7bd577 92
Tomas 0:bada2c7bd577 93 /* Handly macro for suppressing unreferenced-parameter compiler warnings. */
Tomas 0:bada2c7bd577 94 #ifndef UNUSED
Tomas 0:bada2c7bd577 95 #define UNUSED(x) (void)(x)
Tomas 0:bada2c7bd577 96 #endif
Tomas 0:bada2c7bd577 97
Tomas 0:bada2c7bd577 98 /* Compile-time assertion, used for checking compatible compilation options.
Tomas 0:bada2c7bd577 99 * If this fails on your compiler for some reason, use #define STATIC_ASSERT
Tomas 0:bada2c7bd577 100 * to disable it. */
Tomas 0:bada2c7bd577 101 #ifndef STATIC_ASSERT
Tomas 0:bada2c7bd577 102 #define STATIC_ASSERT(COND,MSG) typedef char STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
Tomas 0:bada2c7bd577 103 #define STATIC_ASSERT_MSG(MSG, LINE, COUNTER) STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
Tomas 0:bada2c7bd577 104 #define STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) static_assertion_##MSG##LINE##COUNTER
Tomas 0:bada2c7bd577 105 #endif
Tomas 0:bada2c7bd577 106
Tomas 0:bada2c7bd577 107 /* Number of required fields to keep track of. */
Tomas 0:bada2c7bd577 108 #ifndef PB_MAX_REQUIRED_FIELDS
Tomas 0:bada2c7bd577 109 #define PB_MAX_REQUIRED_FIELDS 64
Tomas 0:bada2c7bd577 110 #endif
Tomas 0:bada2c7bd577 111
Tomas 0:bada2c7bd577 112 #if PB_MAX_REQUIRED_FIELDS < 64
Tomas 0:bada2c7bd577 113 #error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64).
Tomas 0:bada2c7bd577 114 #endif
Tomas 0:bada2c7bd577 115
Tomas 0:bada2c7bd577 116 /* List of possible field types. These are used in the autogenerated code.
Tomas 0:bada2c7bd577 117 * Least-significant 4 bits tell the scalar type
Tomas 0:bada2c7bd577 118 * Most-significant 4 bits specify repeated/required/packed etc.
Tomas 0:bada2c7bd577 119 */
Tomas 0:bada2c7bd577 120
Tomas 0:bada2c7bd577 121 typedef uint8_t pb_type_t;
Tomas 0:bada2c7bd577 122
Tomas 0:bada2c7bd577 123 /**** Field data types ****/
Tomas 0:bada2c7bd577 124
Tomas 0:bada2c7bd577 125 /* Numeric types */
Tomas 0:bada2c7bd577 126 #define PB_LTYPE_VARINT 0x00 /* int32, int64, enum, bool */
Tomas 0:bada2c7bd577 127 #define PB_LTYPE_UVARINT 0x01 /* uint32, uint64 */
Tomas 0:bada2c7bd577 128 #define PB_LTYPE_SVARINT 0x02 /* sint32, sint64 */
Tomas 0:bada2c7bd577 129 #define PB_LTYPE_FIXED32 0x03 /* fixed32, sfixed32, float */
Tomas 0:bada2c7bd577 130 #define PB_LTYPE_FIXED64 0x04 /* fixed64, sfixed64, double */
Tomas 0:bada2c7bd577 131
Tomas 0:bada2c7bd577 132 /* Marker for last packable field type. */
Tomas 0:bada2c7bd577 133 #define PB_LTYPE_LAST_PACKABLE 0x04
Tomas 0:bada2c7bd577 134
Tomas 0:bada2c7bd577 135 /* Byte array with pre-allocated buffer.
Tomas 0:bada2c7bd577 136 * data_size is the length of the allocated PB_BYTES_ARRAY structure. */
Tomas 0:bada2c7bd577 137 #define PB_LTYPE_BYTES 0x05
Tomas 0:bada2c7bd577 138
Tomas 0:bada2c7bd577 139 /* String with pre-allocated buffer.
Tomas 0:bada2c7bd577 140 * data_size is the maximum length. */
Tomas 0:bada2c7bd577 141 #define PB_LTYPE_STRING 0x06
Tomas 0:bada2c7bd577 142
Tomas 0:bada2c7bd577 143 /* Submessage
Tomas 0:bada2c7bd577 144 * submsg_fields is pointer to field descriptions */
Tomas 0:bada2c7bd577 145 #define PB_LTYPE_SUBMESSAGE 0x07
Tomas 0:bada2c7bd577 146
Tomas 0:bada2c7bd577 147 /* Extension pseudo-field
Tomas 0:bada2c7bd577 148 * The field contains a pointer to pb_extension_t */
Tomas 0:bada2c7bd577 149 #define PB_LTYPE_EXTENSION 0x08
Tomas 0:bada2c7bd577 150
Tomas 0:bada2c7bd577 151 /* Number of declared LTYPES */
Tomas 0:bada2c7bd577 152 #define PB_LTYPES_COUNT 9
Tomas 0:bada2c7bd577 153 #define PB_LTYPE_MASK 0x0F
Tomas 0:bada2c7bd577 154
Tomas 0:bada2c7bd577 155 /**** Field repetition rules ****/
Tomas 0:bada2c7bd577 156
Tomas 0:bada2c7bd577 157 #define PB_HTYPE_REQUIRED 0x00
Tomas 0:bada2c7bd577 158 #define PB_HTYPE_OPTIONAL 0x10
Tomas 0:bada2c7bd577 159 #define PB_HTYPE_REPEATED 0x20
Tomas 0:bada2c7bd577 160 #define PB_HTYPE_MASK 0x30
Tomas 0:bada2c7bd577 161
Tomas 0:bada2c7bd577 162 /**** Field allocation types ****/
Tomas 0:bada2c7bd577 163
Tomas 0:bada2c7bd577 164 #define PB_ATYPE_STATIC 0x00
Tomas 0:bada2c7bd577 165 #define PB_ATYPE_POINTER 0x80
Tomas 0:bada2c7bd577 166 #define PB_ATYPE_CALLBACK 0x40
Tomas 0:bada2c7bd577 167 #define PB_ATYPE_MASK 0xC0
Tomas 0:bada2c7bd577 168
Tomas 0:bada2c7bd577 169 #define PB_ATYPE(x) ((x) & PB_ATYPE_MASK)
Tomas 0:bada2c7bd577 170 #define PB_HTYPE(x) ((x) & PB_HTYPE_MASK)
Tomas 0:bada2c7bd577 171 #define PB_LTYPE(x) ((x) & PB_LTYPE_MASK)
Tomas 0:bada2c7bd577 172
Tomas 0:bada2c7bd577 173 /* Data type used for storing sizes of struct fields
Tomas 0:bada2c7bd577 174 * and array counts.
Tomas 0:bada2c7bd577 175 */
Tomas 0:bada2c7bd577 176 #if defined(PB_FIELD_32BIT)
Tomas 0:bada2c7bd577 177 typedef uint32_t pb_size_t;
Tomas 0:bada2c7bd577 178 typedef int32_t pb_ssize_t;
Tomas 0:bada2c7bd577 179 #elif defined(PB_FIELD_16BIT)
Tomas 0:bada2c7bd577 180 typedef uint16_t pb_size_t;
Tomas 0:bada2c7bd577 181 typedef int16_t pb_ssize_t;
Tomas 0:bada2c7bd577 182 #else
Tomas 0:bada2c7bd577 183 typedef uint8_t pb_size_t;
Tomas 0:bada2c7bd577 184 typedef int8_t pb_ssize_t;
Tomas 0:bada2c7bd577 185 #endif
Tomas 0:bada2c7bd577 186
Tomas 0:bada2c7bd577 187 /* This structure is used in auto-generated constants
Tomas 0:bada2c7bd577 188 * to specify struct fields.
Tomas 0:bada2c7bd577 189 * You can change field sizes if you need structures
Tomas 0:bada2c7bd577 190 * larger than 256 bytes or field tags larger than 256.
Tomas 0:bada2c7bd577 191 * The compiler should complain if your .proto has such
Tomas 0:bada2c7bd577 192 * structures. Fix that by defining PB_FIELD_16BIT or
Tomas 0:bada2c7bd577 193 * PB_FIELD_32BIT.
Tomas 0:bada2c7bd577 194 */
Tomas 0:bada2c7bd577 195 PB_PACKED_STRUCT_START
Tomas 0:bada2c7bd577 196 typedef struct _pb_field_t pb_field_t;
Tomas 0:bada2c7bd577 197 struct _pb_field_t {
Tomas 0:bada2c7bd577 198 pb_size_t tag;
Tomas 0:bada2c7bd577 199 pb_type_t type;
Tomas 0:bada2c7bd577 200 pb_size_t data_offset; /* Offset of field data, relative to previous field. */
Tomas 0:bada2c7bd577 201 pb_ssize_t size_offset; /* Offset of array size or has-boolean, relative to data */
Tomas 0:bada2c7bd577 202 pb_size_t data_size; /* Data size in bytes for a single item */
Tomas 0:bada2c7bd577 203 pb_size_t array_size; /* Maximum number of entries in array */
Tomas 0:bada2c7bd577 204
Tomas 0:bada2c7bd577 205 /* Field definitions for submessage
Tomas 0:bada2c7bd577 206 * OR default value for all other non-array, non-callback types
Tomas 0:bada2c7bd577 207 * If null, then field will zeroed. */
Tomas 0:bada2c7bd577 208 const void *ptr;
Tomas 0:bada2c7bd577 209 } pb_packed;
Tomas 0:bada2c7bd577 210 PB_PACKED_STRUCT_END
Tomas 0:bada2c7bd577 211
Tomas 0:bada2c7bd577 212 /* Make sure that the standard integer types are of the expected sizes.
Tomas 0:bada2c7bd577 213 * All kinds of things may break otherwise.. atleast all fixed* types. */
Tomas 0:bada2c7bd577 214 STATIC_ASSERT(sizeof(int8_t) == 1, INT8_T_WRONG_SIZE)
Tomas 0:bada2c7bd577 215 STATIC_ASSERT(sizeof(uint8_t) == 1, UINT8_T_WRONG_SIZE)
Tomas 0:bada2c7bd577 216 STATIC_ASSERT(sizeof(int16_t) == 2, INT16_T_WRONG_SIZE)
Tomas 0:bada2c7bd577 217 STATIC_ASSERT(sizeof(uint16_t) == 2, UINT16_T_WRONG_SIZE)
Tomas 0:bada2c7bd577 218 STATIC_ASSERT(sizeof(int32_t) == 4, INT32_T_WRONG_SIZE)
Tomas 0:bada2c7bd577 219 STATIC_ASSERT(sizeof(uint32_t) == 4, UINT32_T_WRONG_SIZE)
Tomas 0:bada2c7bd577 220 STATIC_ASSERT(sizeof(int64_t) == 8, INT64_T_WRONG_SIZE)
Tomas 0:bada2c7bd577 221 STATIC_ASSERT(sizeof(uint64_t) == 8, UINT64_T_WRONG_SIZE)
Tomas 0:bada2c7bd577 222
Tomas 0:bada2c7bd577 223 /* This structure is used for 'bytes' arrays.
Tomas 0:bada2c7bd577 224 * It has the number of bytes in the beginning, and after that an array.
Tomas 0:bada2c7bd577 225 * Note that actual structs used will have a different length of bytes array.
Tomas 0:bada2c7bd577 226 */
Tomas 0:bada2c7bd577 227 struct _pb_bytes_array_t {
Tomas 0:bada2c7bd577 228 size_t size;
Tomas 0:bada2c7bd577 229 uint8_t bytes[1];
Tomas 0:bada2c7bd577 230 };
Tomas 0:bada2c7bd577 231 typedef struct _pb_bytes_array_t pb_bytes_array_t;
Tomas 0:bada2c7bd577 232
Tomas 0:bada2c7bd577 233 /* Same, except for pointer-type fields. There is no need to variable struct
Tomas 0:bada2c7bd577 234 * length in this case.
Tomas 0:bada2c7bd577 235 */
Tomas 0:bada2c7bd577 236 struct _pb_bytes_ptr_t {
Tomas 0:bada2c7bd577 237 size_t size;
Tomas 0:bada2c7bd577 238 uint8_t *bytes;
Tomas 0:bada2c7bd577 239 };
Tomas 0:bada2c7bd577 240 typedef struct _pb_bytes_ptr_t pb_bytes_ptr_t;
Tomas 0:bada2c7bd577 241
Tomas 0:bada2c7bd577 242 /* This structure is used for giving the callback function.
Tomas 0:bada2c7bd577 243 * It is stored in the message structure and filled in by the method that
Tomas 0:bada2c7bd577 244 * calls pb_decode.
Tomas 0:bada2c7bd577 245 *
Tomas 0:bada2c7bd577 246 * The decoding callback will be given a limited-length stream
Tomas 0:bada2c7bd577 247 * If the wire type was string, the length is the length of the string.
Tomas 0:bada2c7bd577 248 * If the wire type was a varint/fixed32/fixed64, the length is the length
Tomas 0:bada2c7bd577 249 * of the actual value.
Tomas 0:bada2c7bd577 250 * The function may be called multiple times (especially for repeated types,
Tomas 0:bada2c7bd577 251 * but also otherwise if the message happens to contain the field multiple
Tomas 0:bada2c7bd577 252 * times.)
Tomas 0:bada2c7bd577 253 *
Tomas 0:bada2c7bd577 254 * The encoding callback will receive the actual output stream.
Tomas 0:bada2c7bd577 255 * It should write all the data in one call, including the field tag and
Tomas 0:bada2c7bd577 256 * wire type. It can write multiple fields.
Tomas 0:bada2c7bd577 257 *
Tomas 0:bada2c7bd577 258 * The callback can be null if you want to skip a field.
Tomas 0:bada2c7bd577 259 */
Tomas 0:bada2c7bd577 260 typedef struct _pb_istream_t pb_istream_t;
Tomas 0:bada2c7bd577 261 typedef struct _pb_ostream_t pb_ostream_t;
Tomas 0:bada2c7bd577 262 typedef struct _pb_callback_t pb_callback_t;
Tomas 0:bada2c7bd577 263 struct _pb_callback_t {
Tomas 0:bada2c7bd577 264 #ifdef PB_OLD_CALLBACK_STYLE
Tomas 0:bada2c7bd577 265 /* Deprecated since nanopb-0.2.1 */
Tomas 0:bada2c7bd577 266 union {
Tomas 0:bada2c7bd577 267 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
Tomas 0:bada2c7bd577 268 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
Tomas 0:bada2c7bd577 269 } funcs;
Tomas 0:bada2c7bd577 270 #else
Tomas 0:bada2c7bd577 271 /* New function signature, which allows modifying arg contents in callback. */
Tomas 0:bada2c7bd577 272 union {
Tomas 0:bada2c7bd577 273 bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg);
Tomas 0:bada2c7bd577 274 bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
Tomas 0:bada2c7bd577 275 } funcs;
Tomas 0:bada2c7bd577 276 #endif
Tomas 0:bada2c7bd577 277
Tomas 0:bada2c7bd577 278 /* Free arg for use by callback */
Tomas 0:bada2c7bd577 279 void *arg;
Tomas 0:bada2c7bd577 280 };
Tomas 0:bada2c7bd577 281
Tomas 0:bada2c7bd577 282 /* Wire types. Library user needs these only in encoder callbacks. */
Tomas 0:bada2c7bd577 283 typedef enum {
Tomas 0:bada2c7bd577 284 PB_WT_VARINT = 0,
Tomas 0:bada2c7bd577 285 PB_WT_64BIT = 1,
Tomas 0:bada2c7bd577 286 PB_WT_STRING = 2,
Tomas 0:bada2c7bd577 287 PB_WT_32BIT = 5
Tomas 0:bada2c7bd577 288 } pb_wire_type_t;
Tomas 0:bada2c7bd577 289
Tomas 0:bada2c7bd577 290 /* Structure for defining the handling of unknown/extension fields.
Tomas 0:bada2c7bd577 291 * Usually the pb_extension_type_t structure is automatically generated,
Tomas 0:bada2c7bd577 292 * while the pb_extension_t structure is created by the user. However,
Tomas 0:bada2c7bd577 293 * if you want to catch all unknown fields, you can also create a custom
Tomas 0:bada2c7bd577 294 * pb_extension_type_t with your own callback.
Tomas 0:bada2c7bd577 295 */
Tomas 0:bada2c7bd577 296 typedef struct _pb_extension_type_t pb_extension_type_t;
Tomas 0:bada2c7bd577 297 typedef struct _pb_extension_t pb_extension_t;
Tomas 0:bada2c7bd577 298 struct _pb_extension_type_t {
Tomas 0:bada2c7bd577 299 /* Called for each unknown field in the message.
Tomas 0:bada2c7bd577 300 * If you handle the field, read off all of its data and return true.
Tomas 0:bada2c7bd577 301 * If you do not handle the field, do not read anything and return true.
Tomas 0:bada2c7bd577 302 * If you run into an error, return false.
Tomas 0:bada2c7bd577 303 * Set to NULL for default handler.
Tomas 0:bada2c7bd577 304 */
Tomas 0:bada2c7bd577 305 bool (*decode)(pb_istream_t *stream, pb_extension_t *extension,
Tomas 0:bada2c7bd577 306 uint32_t tag, pb_wire_type_t wire_type);
Tomas 0:bada2c7bd577 307
Tomas 0:bada2c7bd577 308 /* Called once after all regular fields have been encoded.
Tomas 0:bada2c7bd577 309 * If you have something to write, do so and return true.
Tomas 0:bada2c7bd577 310 * If you do not have anything to write, just return true.
Tomas 0:bada2c7bd577 311 * If you run into an error, return false.
Tomas 0:bada2c7bd577 312 * Set to NULL for default handler.
Tomas 0:bada2c7bd577 313 */
Tomas 0:bada2c7bd577 314 bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension);
Tomas 0:bada2c7bd577 315
Tomas 0:bada2c7bd577 316 /* Free field for use by the callback. */
Tomas 0:bada2c7bd577 317 const void *arg;
Tomas 0:bada2c7bd577 318 };
Tomas 0:bada2c7bd577 319
Tomas 0:bada2c7bd577 320 struct _pb_extension_t {
Tomas 0:bada2c7bd577 321 /* Type describing the extension field. Usually you'll initialize
Tomas 0:bada2c7bd577 322 * this to a pointer to the automatically generated structure. */
Tomas 0:bada2c7bd577 323 const pb_extension_type_t *type;
Tomas 0:bada2c7bd577 324
Tomas 0:bada2c7bd577 325 /* Destination for the decoded data. This must match the datatype
Tomas 0:bada2c7bd577 326 * of the extension field. */
Tomas 0:bada2c7bd577 327 void *dest;
Tomas 0:bada2c7bd577 328
Tomas 0:bada2c7bd577 329 /* Pointer to the next extension handler, or NULL.
Tomas 0:bada2c7bd577 330 * If this extension does not match a field, the next handler is
Tomas 0:bada2c7bd577 331 * automatically called. */
Tomas 0:bada2c7bd577 332 pb_extension_t *next;
Tomas 0:bada2c7bd577 333 };
Tomas 0:bada2c7bd577 334
Tomas 0:bada2c7bd577 335 /* These macros are used to declare pb_field_t's in the constant array. */
Tomas 0:bada2c7bd577 336 /* Size of a structure member, in bytes. */
Tomas 0:bada2c7bd577 337 #define pb_membersize(st, m) (sizeof ((st*)0)->m)
Tomas 0:bada2c7bd577 338 /* Number of entries in an array. */
Tomas 0:bada2c7bd577 339 #define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0]))
Tomas 0:bada2c7bd577 340 /* Delta from start of one member to the start of another member. */
Tomas 0:bada2c7bd577 341 #define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2))
Tomas 0:bada2c7bd577 342 /* Marks the end of the field list */
Tomas 0:bada2c7bd577 343 #define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0}
Tomas 0:bada2c7bd577 344
Tomas 0:bada2c7bd577 345 /* Macros for filling in the data_offset field */
Tomas 0:bada2c7bd577 346 /* data_offset for first field in a message */
Tomas 0:bada2c7bd577 347 #define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1))
Tomas 0:bada2c7bd577 348 /* data_offset for subsequent fields */
Tomas 0:bada2c7bd577 349 #define PB_DATAOFFSET_OTHER(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2))
Tomas 0:bada2c7bd577 350 /* Choose first/other based on m1 == m2 (deprecated, remains for backwards compatibility) */
Tomas 0:bada2c7bd577 351 #define PB_DATAOFFSET_CHOOSE(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \
Tomas 0:bada2c7bd577 352 ? PB_DATAOFFSET_FIRST(st, m1, m2) \
Tomas 0:bada2c7bd577 353 : PB_DATAOFFSET_OTHER(st, m1, m2))
Tomas 0:bada2c7bd577 354
Tomas 0:bada2c7bd577 355 /* Required fields are the simplest. They just have delta (padding) from
Tomas 0:bada2c7bd577 356 * previous field end, and the size of the field. Pointer is used for
Tomas 0:bada2c7bd577 357 * submessages and default values.
Tomas 0:bada2c7bd577 358 */
Tomas 0:bada2c7bd577 359 #define PB_REQUIRED_STATIC(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 360 {tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \
Tomas 0:bada2c7bd577 361 fd, 0, pb_membersize(st, m), 0, ptr}
Tomas 0:bada2c7bd577 362
Tomas 0:bada2c7bd577 363 /* Optional fields add the delta to the has_ variable. */
Tomas 0:bada2c7bd577 364 #define PB_OPTIONAL_STATIC(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 365 {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
Tomas 0:bada2c7bd577 366 fd, \
Tomas 0:bada2c7bd577 367 pb_delta(st, has_ ## m, m), \
Tomas 0:bada2c7bd577 368 pb_membersize(st, m), 0, ptr}
Tomas 0:bada2c7bd577 369
Tomas 0:bada2c7bd577 370 /* Repeated fields have a _count field and also the maximum number of entries. */
Tomas 0:bada2c7bd577 371 #define PB_REPEATED_STATIC(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 372 {tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype, \
Tomas 0:bada2c7bd577 373 fd, \
Tomas 0:bada2c7bd577 374 pb_delta(st, m ## _count, m), \
Tomas 0:bada2c7bd577 375 pb_membersize(st, m[0]), \
Tomas 0:bada2c7bd577 376 pb_arraysize(st, m), ptr}
Tomas 0:bada2c7bd577 377
Tomas 0:bada2c7bd577 378 /* Allocated fields carry the size of the actual data, not the pointer */
Tomas 0:bada2c7bd577 379 #define PB_REQUIRED_POINTER(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 380 {tag, PB_ATYPE_POINTER | PB_HTYPE_REQUIRED | ltype, \
Tomas 0:bada2c7bd577 381 fd, 0, pb_membersize(st, m[0]), 0, ptr}
Tomas 0:bada2c7bd577 382
Tomas 0:bada2c7bd577 383 /* Optional fields don't need a has_ variable, as information would be redundant */
Tomas 0:bada2c7bd577 384 #define PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 385 {tag, PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype, \
Tomas 0:bada2c7bd577 386 fd, 0, pb_membersize(st, m[0]), 0, ptr}
Tomas 0:bada2c7bd577 387
Tomas 0:bada2c7bd577 388 /* Repeated fields have a _count field and a pointer to array of pointers */
Tomas 0:bada2c7bd577 389 #define PB_REPEATED_POINTER(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 390 {tag, PB_ATYPE_POINTER | PB_HTYPE_REPEATED | ltype, \
Tomas 0:bada2c7bd577 391 fd, pb_delta(st, m ## _count, m), \
Tomas 0:bada2c7bd577 392 pb_membersize(st, m[0]), 0, ptr}
Tomas 0:bada2c7bd577 393
Tomas 0:bada2c7bd577 394 /* Callbacks are much like required fields except with special datatype. */
Tomas 0:bada2c7bd577 395 #define PB_REQUIRED_CALLBACK(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 396 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype, \
Tomas 0:bada2c7bd577 397 fd, 0, pb_membersize(st, m), 0, ptr}
Tomas 0:bada2c7bd577 398
Tomas 0:bada2c7bd577 399 #define PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 400 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
Tomas 0:bada2c7bd577 401 fd, 0, pb_membersize(st, m), 0, ptr}
Tomas 0:bada2c7bd577 402
Tomas 0:bada2c7bd577 403 #define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 404 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \
Tomas 0:bada2c7bd577 405 fd, 0, pb_membersize(st, m), 0, ptr}
Tomas 0:bada2c7bd577 406
Tomas 0:bada2c7bd577 407 /* Optional extensions don't have the has_ field, as that would be redundant. */
Tomas 0:bada2c7bd577 408 #define PB_OPTEXT_STATIC(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 409 {tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
Tomas 0:bada2c7bd577 410 0, \
Tomas 0:bada2c7bd577 411 0, \
Tomas 0:bada2c7bd577 412 pb_membersize(st, m), 0, ptr}
Tomas 0:bada2c7bd577 413
Tomas 0:bada2c7bd577 414 #define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \
Tomas 0:bada2c7bd577 415 {tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
Tomas 0:bada2c7bd577 416 0, 0, pb_membersize(st, m), 0, ptr}
Tomas 0:bada2c7bd577 417
Tomas 0:bada2c7bd577 418 /* The mapping from protobuf types to LTYPEs is done using these macros. */
Tomas 0:bada2c7bd577 419 #define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT
Tomas 0:bada2c7bd577 420 #define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES
Tomas 0:bada2c7bd577 421 #define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64
Tomas 0:bada2c7bd577 422 #define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT
Tomas 0:bada2c7bd577 423 #define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32
Tomas 0:bada2c7bd577 424 #define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64
Tomas 0:bada2c7bd577 425 #define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32
Tomas 0:bada2c7bd577 426 #define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT
Tomas 0:bada2c7bd577 427 #define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT
Tomas 0:bada2c7bd577 428 #define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE
Tomas 0:bada2c7bd577 429 #define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32
Tomas 0:bada2c7bd577 430 #define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64
Tomas 0:bada2c7bd577 431 #define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT
Tomas 0:bada2c7bd577 432 #define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT
Tomas 0:bada2c7bd577 433 #define PB_LTYPE_MAP_STRING PB_LTYPE_STRING
Tomas 0:bada2c7bd577 434 #define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT
Tomas 0:bada2c7bd577 435 #define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT
Tomas 0:bada2c7bd577 436 #define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION
Tomas 0:bada2c7bd577 437
Tomas 0:bada2c7bd577 438 /* This is the actual macro used in field descriptions.
Tomas 0:bada2c7bd577 439 * It takes these arguments:
Tomas 0:bada2c7bd577 440 * - Field tag number
Tomas 0:bada2c7bd577 441 * - Field type: BOOL, BYTES, DOUBLE, ENUM, FIXED32, FIXED64,
Tomas 0:bada2c7bd577 442 * FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64
Tomas 0:bada2c7bd577 443 * SINT32, SINT64, STRING, UINT32, UINT64 or EXTENSION
Tomas 0:bada2c7bd577 444 * - Field rules: REQUIRED, OPTIONAL or REPEATED
Tomas 0:bada2c7bd577 445 * - Allocation: STATIC or CALLBACK
Tomas 0:bada2c7bd577 446 * - Message name
Tomas 0:bada2c7bd577 447 * - Field name
Tomas 0:bada2c7bd577 448 * - Previous field name (or field name again for first field)
Tomas 0:bada2c7bd577 449 * - Pointer to default value or submsg fields.
Tomas 0:bada2c7bd577 450 */
Tomas 0:bada2c7bd577 451
Tomas 0:bada2c7bd577 452 #define PB_FIELD(tag, type, rules, allocation, message, field, prevfield, ptr) \
Tomas 0:bada2c7bd577 453 PB_ ## rules ## _ ## allocation(tag, message, field, \
Tomas 0:bada2c7bd577 454 PB_DATAOFFSET_CHOOSE(message, field, prevfield), \
Tomas 0:bada2c7bd577 455 PB_LTYPE_MAP_ ## type, ptr)
Tomas 0:bada2c7bd577 456
Tomas 0:bada2c7bd577 457 /* This is a new version of the macro used by nanopb generator from
Tomas 0:bada2c7bd577 458 * version 0.2.3 onwards. It avoids the use of a ternary expression in
Tomas 0:bada2c7bd577 459 * the initialization, which confused some compilers.
Tomas 0:bada2c7bd577 460 *
Tomas 0:bada2c7bd577 461 * - Placement: FIRST or OTHER, depending on if this is the first field in structure.
Tomas 0:bada2c7bd577 462 *
Tomas 0:bada2c7bd577 463 */
Tomas 0:bada2c7bd577 464 #define PB_FIELD2(tag, type, rules, allocation, placement, message, field, prevfield, ptr) \
Tomas 0:bada2c7bd577 465 PB_ ## rules ## _ ## allocation(tag, message, field, \
Tomas 0:bada2c7bd577 466 PB_DATAOFFSET_ ## placement(message, field, prevfield), \
Tomas 0:bada2c7bd577 467 PB_LTYPE_MAP_ ## type, ptr)
Tomas 0:bada2c7bd577 468
Tomas 0:bada2c7bd577 469
Tomas 0:bada2c7bd577 470 /* These macros are used for giving out error messages.
Tomas 0:bada2c7bd577 471 * They are mostly a debugging aid; the main error information
Tomas 0:bada2c7bd577 472 * is the true/false return value from functions.
Tomas 0:bada2c7bd577 473 * Some code space can be saved by disabling the error
Tomas 0:bada2c7bd577 474 * messages if not used.
Tomas 0:bada2c7bd577 475 */
Tomas 0:bada2c7bd577 476 #ifdef PB_NO_ERRMSG
Tomas 0:bada2c7bd577 477 #define PB_RETURN_ERROR(stream,msg) return false
Tomas 0:bada2c7bd577 478 #define PB_GET_ERROR(stream) "(errmsg disabled)"
Tomas 0:bada2c7bd577 479 #else
Tomas 0:bada2c7bd577 480 #define PB_RETURN_ERROR(stream,msg) \
Tomas 0:bada2c7bd577 481 do {\
Tomas 0:bada2c7bd577 482 if ((stream)->errmsg == NULL) \
Tomas 0:bada2c7bd577 483 (stream)->errmsg = (msg); \
Tomas 0:bada2c7bd577 484 return false; \
Tomas 0:bada2c7bd577 485 } while(0)
Tomas 0:bada2c7bd577 486 #define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)")
Tomas 0:bada2c7bd577 487 #endif
Tomas 0:bada2c7bd577 488
Tomas 0:bada2c7bd577 489 #endif
Tomas 0:bada2c7bd577 490