this is a sample for mbed(LPC1768)

Committer:
1
Date:
Thu Nov 19 10:17:55 2015 +0800
Revision:
0:3163adfd2cf1
????????????????????????
1.callback
2.thread
3.auto-connect,time-up

Who changed what in which revision?

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