test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Thu Sep 01 05:14:03 2016 +0000
Revision:
0:1ad0e04b1bc5
change internal time from 1s to 200ms

Who changed what in which revision?

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