1*c8d645caSAndroid Build Coastguard Worker===================== 2*c8d645caSAndroid Build Coastguard WorkerNanopb: API reference 3*c8d645caSAndroid Build Coastguard Worker===================== 4*c8d645caSAndroid Build Coastguard Worker 5*c8d645caSAndroid Build Coastguard Worker.. include :: menu.rst 6*c8d645caSAndroid Build Coastguard Worker 7*c8d645caSAndroid Build Coastguard Worker.. contents :: 8*c8d645caSAndroid Build Coastguard Worker 9*c8d645caSAndroid Build Coastguard Worker 10*c8d645caSAndroid Build Coastguard Worker 11*c8d645caSAndroid Build Coastguard Worker 12*c8d645caSAndroid Build Coastguard WorkerCompilation options 13*c8d645caSAndroid Build Coastguard Worker=================== 14*c8d645caSAndroid Build Coastguard WorkerThe following options can be specified in one of two ways: 15*c8d645caSAndroid Build Coastguard Worker 16*c8d645caSAndroid Build Coastguard Worker1. Using the -D switch on the C compiler command line. 17*c8d645caSAndroid Build Coastguard Worker2. By #defining them at the top of pb.h. 18*c8d645caSAndroid Build Coastguard Worker 19*c8d645caSAndroid Build Coastguard WorkerYou must have the same settings for the nanopb library and all code that 20*c8d645caSAndroid Build Coastguard Workerincludes pb.h. 21*c8d645caSAndroid Build Coastguard Worker 22*c8d645caSAndroid Build Coastguard Worker============================ ================================================ 23*c8d645caSAndroid Build Coastguard WorkerPB_NO_PACKED_STRUCTS Disable packed structs. Increases RAM usage but 24*c8d645caSAndroid Build Coastguard Worker is necessary on some platforms that do not 25*c8d645caSAndroid Build Coastguard Worker support unaligned memory access. 26*c8d645caSAndroid Build Coastguard WorkerPB_ENABLE_MALLOC Set this to enable dynamic allocation support 27*c8d645caSAndroid Build Coastguard Worker in the decoder. 28*c8d645caSAndroid Build Coastguard WorkerPB_MAX_REQUIRED_FIELDS Maximum number of required fields to check for 29*c8d645caSAndroid Build Coastguard Worker presence. Default value is 64. Increases stack 30*c8d645caSAndroid Build Coastguard Worker usage 1 byte per every 8 fields. Compiler 31*c8d645caSAndroid Build Coastguard Worker warning will tell if you need this. 32*c8d645caSAndroid Build Coastguard WorkerPB_FIELD_16BIT Add support for tag numbers > 255 and fields 33*c8d645caSAndroid Build Coastguard Worker larger than 255 bytes or 255 array entries. 34*c8d645caSAndroid Build Coastguard Worker Increases code size 3 bytes per each field. 35*c8d645caSAndroid Build Coastguard Worker Compiler error will tell if you need this. 36*c8d645caSAndroid Build Coastguard WorkerPB_FIELD_32BIT Add support for tag numbers > 65535 and fields 37*c8d645caSAndroid Build Coastguard Worker larger than 65535 bytes or 65535 array entries. 38*c8d645caSAndroid Build Coastguard Worker Increases code size 9 bytes per each field. 39*c8d645caSAndroid Build Coastguard Worker Compiler error will tell if you need this. 40*c8d645caSAndroid Build Coastguard WorkerPB_NO_ERRMSG Disables the support for error messages; only 41*c8d645caSAndroid Build Coastguard Worker error information is the true/false return 42*c8d645caSAndroid Build Coastguard Worker value. Decreases the code size by a few hundred 43*c8d645caSAndroid Build Coastguard Worker bytes. 44*c8d645caSAndroid Build Coastguard WorkerPB_BUFFER_ONLY Disables the support for custom streams. Only 45*c8d645caSAndroid Build Coastguard Worker supports encoding and decoding with memory 46*c8d645caSAndroid Build Coastguard Worker buffers. Speeds up execution and decreases code 47*c8d645caSAndroid Build Coastguard Worker size slightly. 48*c8d645caSAndroid Build Coastguard WorkerPB_OLD_CALLBACK_STYLE Use the old function signature (void\* instead 49*c8d645caSAndroid Build Coastguard Worker of void\*\*) for callback fields. This was the 50*c8d645caSAndroid Build Coastguard Worker default until nanopb-0.2.1. 51*c8d645caSAndroid Build Coastguard WorkerPB_SYSTEM_HEADER Replace the standard header files with a single 52*c8d645caSAndroid Build Coastguard Worker header file. It should define all the required 53*c8d645caSAndroid Build Coastguard Worker functions and typedefs listed on the 54*c8d645caSAndroid Build Coastguard Worker `overview page`_. Value must include quotes, 55*c8d645caSAndroid Build Coastguard Worker for example *#define PB_SYSTEM_HEADER "foo.h"*. 56*c8d645caSAndroid Build Coastguard WorkerPB_WITHOUT_64BIT Disable 64-bit support, for old compilers or 57*c8d645caSAndroid Build Coastguard Worker for a slight speedup on 8-bit platforms. 58*c8d645caSAndroid Build Coastguard WorkerPB_ENCODE_ARRAYS_UNPACKED Don't encode scalar arrays as packed. 59*c8d645caSAndroid Build Coastguard Worker This is only to be used when the decoder on the 60*c8d645caSAndroid Build Coastguard Worker receiving side cannot process packed scalar 61*c8d645caSAndroid Build Coastguard Worker arrays. Such example is older protobuf.js. 62*c8d645caSAndroid Build Coastguard Worker============================ ================================================ 63*c8d645caSAndroid Build Coastguard Worker 64*c8d645caSAndroid Build Coastguard WorkerThe PB_MAX_REQUIRED_FIELDS, PB_FIELD_16BIT and PB_FIELD_32BIT settings allow 65*c8d645caSAndroid Build Coastguard Workerraising some datatype limits to suit larger messages. Their need is recognized 66*c8d645caSAndroid Build Coastguard Workerautomatically by C-preprocessor #if-directives in the generated .pb.h files. 67*c8d645caSAndroid Build Coastguard WorkerThe default setting is to use the smallest datatypes (least resources used). 68*c8d645caSAndroid Build Coastguard Worker 69*c8d645caSAndroid Build Coastguard Worker.. _`overview page`: index.html#compiler-requirements 70*c8d645caSAndroid Build Coastguard Worker 71*c8d645caSAndroid Build Coastguard Worker 72*c8d645caSAndroid Build Coastguard WorkerProto file options 73*c8d645caSAndroid Build Coastguard Worker================== 74*c8d645caSAndroid Build Coastguard WorkerThe generator behaviour can be adjusted using these options, defined in the 75*c8d645caSAndroid Build Coastguard Worker'nanopb.proto' file in the generator folder: 76*c8d645caSAndroid Build Coastguard Worker 77*c8d645caSAndroid Build Coastguard Worker============================ ================================================ 78*c8d645caSAndroid Build Coastguard Workermax_size Allocated size for *bytes* and *string* fields. 79*c8d645caSAndroid Build Coastguard Workermax_count Allocated number of entries in arrays 80*c8d645caSAndroid Build Coastguard Worker (*repeated* fields). 81*c8d645caSAndroid Build Coastguard Workerint_size Override the integer type of a field. 82*c8d645caSAndroid Build Coastguard Worker (To use e.g. uint8_t to save RAM.) 83*c8d645caSAndroid Build Coastguard Workertype Type of the generated field. Default value 84*c8d645caSAndroid Build Coastguard Worker is *FT_DEFAULT*, which selects automatically. 85*c8d645caSAndroid Build Coastguard Worker You can use *FT_CALLBACK*, *FT_POINTER*, 86*c8d645caSAndroid Build Coastguard Worker *FT_STATIC* or *FT_IGNORE* to 87*c8d645caSAndroid Build Coastguard Worker force a callback field, a dynamically 88*c8d645caSAndroid Build Coastguard Worker allocated field, a static field or to 89*c8d645caSAndroid Build Coastguard Worker completely ignore the field. 90*c8d645caSAndroid Build Coastguard Workerlong_names Prefix the enum name to the enum value in 91*c8d645caSAndroid Build Coastguard Worker definitions, i.e. *EnumName_EnumValue*. Enabled 92*c8d645caSAndroid Build Coastguard Worker by default. 93*c8d645caSAndroid Build Coastguard Workerpacked_struct Make the generated structures packed. 94*c8d645caSAndroid Build Coastguard Worker NOTE: This cannot be used on CPUs that break 95*c8d645caSAndroid Build Coastguard Worker on unaligned accesses to variables. 96*c8d645caSAndroid Build Coastguard Workerskip_message Skip the whole message from generation. 97*c8d645caSAndroid Build Coastguard Workerno_unions Generate 'oneof' fields as optional fields 98*c8d645caSAndroid Build Coastguard Worker instead of C unions. 99*c8d645caSAndroid Build Coastguard Workermsgid Specifies a unique id for this message type. 100*c8d645caSAndroid Build Coastguard Worker Can be used by user code as an identifier. 101*c8d645caSAndroid Build Coastguard Workeranonymous_oneof Generate 'oneof' fields as anonymous unions. 102*c8d645caSAndroid Build Coastguard Workerfixed_length Generate 'bytes' fields with constant length 103*c8d645caSAndroid Build Coastguard Worker (max_size must also be defined). 104*c8d645caSAndroid Build Coastguard Workerfixed_count Generate arrays with constant length 105*c8d645caSAndroid Build Coastguard Worker (max_count must also be defined). 106*c8d645caSAndroid Build Coastguard Worker============================ ================================================ 107*c8d645caSAndroid Build Coastguard Worker 108*c8d645caSAndroid Build Coastguard WorkerThese options can be defined for the .proto files before they are converted 109*c8d645caSAndroid Build Coastguard Workerusing the nanopb-generatory.py. There are three ways to define the options: 110*c8d645caSAndroid Build Coastguard Worker 111*c8d645caSAndroid Build Coastguard Worker1. Using a separate .options file. 112*c8d645caSAndroid Build Coastguard Worker This is the preferred way as of nanopb-0.2.1, because it has the best 113*c8d645caSAndroid Build Coastguard Worker compatibility with other protobuf libraries. 114*c8d645caSAndroid Build Coastguard Worker2. Defining the options on the command line of nanopb_generator.py. 115*c8d645caSAndroid Build Coastguard Worker This only makes sense for settings that apply to a whole file. 116*c8d645caSAndroid Build Coastguard Worker3. Defining the options in the .proto file using the nanopb extensions. 117*c8d645caSAndroid Build Coastguard Worker This is the way used in nanopb-0.1, and will remain supported in the 118*c8d645caSAndroid Build Coastguard Worker future. It however sometimes causes trouble when using the .proto file 119*c8d645caSAndroid Build Coastguard Worker with other protobuf libraries. 120*c8d645caSAndroid Build Coastguard Worker 121*c8d645caSAndroid Build Coastguard WorkerThe effect of the options is the same no matter how they are given. The most 122*c8d645caSAndroid Build Coastguard Workercommon purpose is to define maximum size for string fields in order to 123*c8d645caSAndroid Build Coastguard Workerstatically allocate them. 124*c8d645caSAndroid Build Coastguard Worker 125*c8d645caSAndroid Build Coastguard WorkerDefining the options in a .options file 126*c8d645caSAndroid Build Coastguard Worker--------------------------------------- 127*c8d645caSAndroid Build Coastguard WorkerThe preferred way to define options is to have a separate file 128*c8d645caSAndroid Build Coastguard Worker'myproto.options' in the same directory as the 'myproto.proto'. :: 129*c8d645caSAndroid Build Coastguard Worker 130*c8d645caSAndroid Build Coastguard Worker # myproto.proto 131*c8d645caSAndroid Build Coastguard Worker message MyMessage { 132*c8d645caSAndroid Build Coastguard Worker required string name = 1; 133*c8d645caSAndroid Build Coastguard Worker repeated int32 ids = 4; 134*c8d645caSAndroid Build Coastguard Worker } 135*c8d645caSAndroid Build Coastguard Worker 136*c8d645caSAndroid Build Coastguard Worker:: 137*c8d645caSAndroid Build Coastguard Worker 138*c8d645caSAndroid Build Coastguard Worker # myproto.options 139*c8d645caSAndroid Build Coastguard Worker MyMessage.name max_size:40 140*c8d645caSAndroid Build Coastguard Worker MyMessage.ids max_count:5 141*c8d645caSAndroid Build Coastguard Worker 142*c8d645caSAndroid Build Coastguard WorkerThe generator will automatically search for this file and read the 143*c8d645caSAndroid Build Coastguard Workeroptions from it. The file format is as follows: 144*c8d645caSAndroid Build Coastguard Worker 145*c8d645caSAndroid Build Coastguard Worker* Lines starting with '#' or '//' are regarded as comments. 146*c8d645caSAndroid Build Coastguard Worker* Blank lines are ignored. 147*c8d645caSAndroid Build Coastguard Worker* All other lines should start with a field name pattern, followed by one or 148*c8d645caSAndroid Build Coastguard Worker more options. For example: *"MyMessage.myfield max_size:5 max_count:10"*. 149*c8d645caSAndroid Build Coastguard Worker* The field name pattern is matched against a string of form *'Message.field'*. 150*c8d645caSAndroid Build Coastguard Worker For nested messages, the string is *'Message.SubMessage.field'*. 151*c8d645caSAndroid Build Coastguard Worker* The field name pattern may use the notation recognized by Python fnmatch(): 152*c8d645caSAndroid Build Coastguard Worker 153*c8d645caSAndroid Build Coastguard Worker - *\** matches any part of string, like 'Message.\*' for all fields 154*c8d645caSAndroid Build Coastguard Worker - *\?* matches any single character 155*c8d645caSAndroid Build Coastguard Worker - *[seq]* matches any of characters 's', 'e' and 'q' 156*c8d645caSAndroid Build Coastguard Worker - *[!seq]* matches any other character 157*c8d645caSAndroid Build Coastguard Worker 158*c8d645caSAndroid Build Coastguard Worker* The options are written as *'option_name:option_value'* and several options 159*c8d645caSAndroid Build Coastguard Worker can be defined on same line, separated by whitespace. 160*c8d645caSAndroid Build Coastguard Worker* Options defined later in the file override the ones specified earlier, so 161*c8d645caSAndroid Build Coastguard Worker it makes sense to define wildcard options first in the file and more specific 162*c8d645caSAndroid Build Coastguard Worker ones later. 163*c8d645caSAndroid Build Coastguard Worker 164*c8d645caSAndroid Build Coastguard WorkerTo debug problems in applying the options, you can use the *-v* option for the 165*c8d645caSAndroid Build Coastguard Workerplugin. Plugin options are specified in front of the output path: 166*c8d645caSAndroid Build Coastguard Worker 167*c8d645caSAndroid Build Coastguard Worker protoc ... --nanopb_out=-v:. message.proto 168*c8d645caSAndroid Build Coastguard Worker 169*c8d645caSAndroid Build Coastguard WorkerProtoc doesn't currently pass include path into plugins. Therefore if your 170*c8d645caSAndroid Build Coastguard Worker*.proto* is in a subdirectory, nanopb may have trouble finding the associated 171*c8d645caSAndroid Build Coastguard Worker*.options* file. A workaround is to specify include path separately to the 172*c8d645caSAndroid Build Coastguard Workernanopb plugin, like: 173*c8d645caSAndroid Build Coastguard Worker 174*c8d645caSAndroid Build Coastguard Worker protoc -Isubdir --nanopb_out=-Isubdir:. message.proto 175*c8d645caSAndroid Build Coastguard Worker 176*c8d645caSAndroid Build Coastguard WorkerIf preferred, the name of the options file can be set using plugin argument 177*c8d645caSAndroid Build Coastguard Worker*-f*. 178*c8d645caSAndroid Build Coastguard Worker 179*c8d645caSAndroid Build Coastguard WorkerDefining the options on command line 180*c8d645caSAndroid Build Coastguard Worker------------------------------------ 181*c8d645caSAndroid Build Coastguard WorkerThe nanopb_generator.py has a simple command line option *-s OPTION:VALUE*. 182*c8d645caSAndroid Build Coastguard WorkerThe setting applies to the whole file that is being processed. 183*c8d645caSAndroid Build Coastguard Worker 184*c8d645caSAndroid Build Coastguard WorkerDefining the options in the .proto file 185*c8d645caSAndroid Build Coastguard Worker--------------------------------------- 186*c8d645caSAndroid Build Coastguard WorkerThe .proto file format allows defining custom options for the fields. 187*c8d645caSAndroid Build Coastguard WorkerThe nanopb library comes with *nanopb.proto* which does exactly that, allowing 188*c8d645caSAndroid Build Coastguard Workeryou do define the options directly in the .proto file:: 189*c8d645caSAndroid Build Coastguard Worker 190*c8d645caSAndroid Build Coastguard Worker import "nanopb.proto"; 191*c8d645caSAndroid Build Coastguard Worker 192*c8d645caSAndroid Build Coastguard Worker message MyMessage { 193*c8d645caSAndroid Build Coastguard Worker required string name = 1 [(nanopb).max_size = 40]; 194*c8d645caSAndroid Build Coastguard Worker repeated int32 ids = 4 [(nanopb).max_count = 5]; 195*c8d645caSAndroid Build Coastguard Worker } 196*c8d645caSAndroid Build Coastguard Worker 197*c8d645caSAndroid Build Coastguard WorkerA small complication is that you have to set the include path of protoc so that 198*c8d645caSAndroid Build Coastguard Workernanopb.proto can be found. This file, in turn, requires the file 199*c8d645caSAndroid Build Coastguard Worker*google/protobuf/descriptor.proto*. This is usually installed under 200*c8d645caSAndroid Build Coastguard Worker*/usr/include*. Therefore, to compile a .proto file which uses options, use a 201*c8d645caSAndroid Build Coastguard Workerprotoc command similar to:: 202*c8d645caSAndroid Build Coastguard Worker 203*c8d645caSAndroid Build Coastguard Worker protoc -I/usr/include -Inanopb/generator -I. --nanopb_out=. message.proto 204*c8d645caSAndroid Build Coastguard Worker 205*c8d645caSAndroid Build Coastguard WorkerThe options can be defined in file, message and field scopes:: 206*c8d645caSAndroid Build Coastguard Worker 207*c8d645caSAndroid Build Coastguard Worker option (nanopb_fileopt).max_size = 20; // File scope 208*c8d645caSAndroid Build Coastguard Worker message Message 209*c8d645caSAndroid Build Coastguard Worker { 210*c8d645caSAndroid Build Coastguard Worker option (nanopb_msgopt).max_size = 30; // Message scope 211*c8d645caSAndroid Build Coastguard Worker required string fieldsize = 1 [(nanopb).max_size = 40]; // Field scope 212*c8d645caSAndroid Build Coastguard Worker } 213*c8d645caSAndroid Build Coastguard Worker 214*c8d645caSAndroid Build Coastguard Worker 215*c8d645caSAndroid Build Coastguard Workerpb.h 216*c8d645caSAndroid Build Coastguard Worker==== 217*c8d645caSAndroid Build Coastguard Worker 218*c8d645caSAndroid Build Coastguard Workerpb_byte_t 219*c8d645caSAndroid Build Coastguard Worker--------- 220*c8d645caSAndroid Build Coastguard WorkerType used for storing byte-sized data, such as raw binary input and bytes-type fields. :: 221*c8d645caSAndroid Build Coastguard Worker 222*c8d645caSAndroid Build Coastguard Worker typedef uint_least8_t pb_byte_t; 223*c8d645caSAndroid Build Coastguard Worker 224*c8d645caSAndroid Build Coastguard WorkerFor most platforms this is equivalent to `uint8_t`. Some platforms however do not support 225*c8d645caSAndroid Build Coastguard Worker8-bit variables, and on those platforms 16 or 32 bits need to be used for each byte. 226*c8d645caSAndroid Build Coastguard Worker 227*c8d645caSAndroid Build Coastguard Workerpb_type_t 228*c8d645caSAndroid Build Coastguard Worker--------- 229*c8d645caSAndroid Build Coastguard WorkerType used to store the type of each field, to control the encoder/decoder behaviour. :: 230*c8d645caSAndroid Build Coastguard Worker 231*c8d645caSAndroid Build Coastguard Worker typedef uint_least8_t pb_type_t; 232*c8d645caSAndroid Build Coastguard Worker 233*c8d645caSAndroid Build Coastguard WorkerThe low-order nibble of the enumeration values defines the function that can be used for encoding and decoding the field data: 234*c8d645caSAndroid Build Coastguard Worker 235*c8d645caSAndroid Build Coastguard Worker=========================== ===== ================================================ 236*c8d645caSAndroid Build Coastguard WorkerLTYPE identifier Value Storage format 237*c8d645caSAndroid Build Coastguard Worker=========================== ===== ================================================ 238*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_VARINT 0x00 Integer. 239*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_UVARINT 0x01 Unsigned integer. 240*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_SVARINT 0x02 Integer, zigzag encoded. 241*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_FIXED32 0x03 32-bit integer or floating point. 242*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_FIXED64 0x04 64-bit integer or floating point. 243*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_BYTES 0x05 Structure with *size_t* field and byte array. 244*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_STRING 0x06 Null-terminated string. 245*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_SUBMESSAGE 0x07 Submessage structure. 246*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_EXTENSION 0x08 Point to *pb_extension_t*. 247*c8d645caSAndroid Build Coastguard WorkerPB_LTYPE_FIXED_LENGTH_BYTES 0x09 Inline *pb_byte_t* array of fixed size. 248*c8d645caSAndroid Build Coastguard Worker=========================== ===== ================================================ 249*c8d645caSAndroid Build Coastguard Worker 250*c8d645caSAndroid Build Coastguard WorkerThe bits 4-5 define whether the field is required, optional or repeated: 251*c8d645caSAndroid Build Coastguard Worker 252*c8d645caSAndroid Build Coastguard Worker==================== ===== ================================================ 253*c8d645caSAndroid Build Coastguard WorkerHTYPE identifier Value Field handling 254*c8d645caSAndroid Build Coastguard Worker==================== ===== ================================================ 255*c8d645caSAndroid Build Coastguard WorkerPB_HTYPE_REQUIRED 0x00 Verify that field exists in decoded message. 256*c8d645caSAndroid Build Coastguard WorkerPB_HTYPE_OPTIONAL 0x10 Use separate *has_<field>* boolean to specify 257*c8d645caSAndroid Build Coastguard Worker whether the field is present. 258*c8d645caSAndroid Build Coastguard Worker (Unless it is a callback) 259*c8d645caSAndroid Build Coastguard WorkerPB_HTYPE_REPEATED 0x20 A repeated field with preallocated array. 260*c8d645caSAndroid Build Coastguard Worker Separate *<field>_count* for number of items. 261*c8d645caSAndroid Build Coastguard Worker (Unless it is a callback) 262*c8d645caSAndroid Build Coastguard Worker==================== ===== ================================================ 263*c8d645caSAndroid Build Coastguard Worker 264*c8d645caSAndroid Build Coastguard WorkerThe bits 6-7 define the how the storage for the field is allocated: 265*c8d645caSAndroid Build Coastguard Worker 266*c8d645caSAndroid Build Coastguard Worker==================== ===== ================================================ 267*c8d645caSAndroid Build Coastguard WorkerATYPE identifier Value Allocation method 268*c8d645caSAndroid Build Coastguard Worker==================== ===== ================================================ 269*c8d645caSAndroid Build Coastguard WorkerPB_ATYPE_STATIC 0x00 Statically allocated storage in the structure. 270*c8d645caSAndroid Build Coastguard WorkerPB_ATYPE_CALLBACK 0x40 A field with dynamic storage size. Struct field 271*c8d645caSAndroid Build Coastguard Worker actually contains a pointer to a callback 272*c8d645caSAndroid Build Coastguard Worker function. 273*c8d645caSAndroid Build Coastguard Worker==================== ===== ================================================ 274*c8d645caSAndroid Build Coastguard Worker 275*c8d645caSAndroid Build Coastguard Worker 276*c8d645caSAndroid Build Coastguard Workerpb_field_t 277*c8d645caSAndroid Build Coastguard Worker---------- 278*c8d645caSAndroid Build Coastguard WorkerDescribes a single structure field with memory position in relation to others. The descriptions are usually autogenerated. :: 279*c8d645caSAndroid Build Coastguard Worker 280*c8d645caSAndroid Build Coastguard Worker typedef struct pb_field_s pb_field_t; 281*c8d645caSAndroid Build Coastguard Worker struct pb_field_s { 282*c8d645caSAndroid Build Coastguard Worker pb_size_t tag; 283*c8d645caSAndroid Build Coastguard Worker pb_type_t type; 284*c8d645caSAndroid Build Coastguard Worker pb_size_t data_offset; 285*c8d645caSAndroid Build Coastguard Worker pb_ssize_t size_offset; 286*c8d645caSAndroid Build Coastguard Worker pb_size_t data_size; 287*c8d645caSAndroid Build Coastguard Worker pb_size_t array_size; 288*c8d645caSAndroid Build Coastguard Worker const void *ptr; 289*c8d645caSAndroid Build Coastguard Worker } pb_packed; 290*c8d645caSAndroid Build Coastguard Worker 291*c8d645caSAndroid Build Coastguard Worker:tag: Tag number of the field or 0 to terminate a list of fields. 292*c8d645caSAndroid Build Coastguard Worker:type: LTYPE, HTYPE and ATYPE of the field. 293*c8d645caSAndroid Build Coastguard Worker:data_offset: Offset of field data, relative to the end of the previous field. 294*c8d645caSAndroid Build Coastguard Worker:size_offset: Offset of *bool* flag for optional fields or *size_t* count for arrays, relative to field data. 295*c8d645caSAndroid Build Coastguard Worker:data_size: Size of a single data entry, in bytes. For PB_LTYPE_BYTES, the size of the byte array inside the containing structure. For PB_HTYPE_CALLBACK, size of the C data type if known. 296*c8d645caSAndroid Build Coastguard Worker:array_size: Maximum number of entries in an array, if it is an array type. 297*c8d645caSAndroid Build Coastguard Worker:ptr: Pointer to default value for optional fields, or to submessage description for PB_LTYPE_SUBMESSAGE. 298*c8d645caSAndroid Build Coastguard Worker 299*c8d645caSAndroid Build Coastguard WorkerThe *uint8_t* datatypes limit the maximum size of a single item to 255 bytes and arrays to 255 items. Compiler will give error if the values are too large. The types can be changed to larger ones by defining *PB_FIELD_16BIT*. 300*c8d645caSAndroid Build Coastguard Worker 301*c8d645caSAndroid Build Coastguard Workerpb_bytes_array_t 302*c8d645caSAndroid Build Coastguard Worker---------------- 303*c8d645caSAndroid Build Coastguard WorkerAn byte array with a field for storing the length:: 304*c8d645caSAndroid Build Coastguard Worker 305*c8d645caSAndroid Build Coastguard Worker typedef struct { 306*c8d645caSAndroid Build Coastguard Worker pb_size_t size; 307*c8d645caSAndroid Build Coastguard Worker pb_byte_t bytes[1]; 308*c8d645caSAndroid Build Coastguard Worker } pb_bytes_array_t; 309*c8d645caSAndroid Build Coastguard Worker 310*c8d645caSAndroid Build Coastguard WorkerIn an actual array, the length of *bytes* may be different. 311*c8d645caSAndroid Build Coastguard Worker 312*c8d645caSAndroid Build Coastguard Workerpb_callback_t 313*c8d645caSAndroid Build Coastguard Worker------------- 314*c8d645caSAndroid Build Coastguard WorkerPart of a message structure, for fields with type PB_HTYPE_CALLBACK:: 315*c8d645caSAndroid Build Coastguard Worker 316*c8d645caSAndroid Build Coastguard Worker typedef struct _pb_callback_t pb_callback_t; 317*c8d645caSAndroid Build Coastguard Worker struct _pb_callback_t { 318*c8d645caSAndroid Build Coastguard Worker union { 319*c8d645caSAndroid Build Coastguard Worker bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg); 320*c8d645caSAndroid Build Coastguard Worker bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg); 321*c8d645caSAndroid Build Coastguard Worker } funcs; 322*c8d645caSAndroid Build Coastguard Worker 323*c8d645caSAndroid Build Coastguard Worker void *arg; 324*c8d645caSAndroid Build Coastguard Worker }; 325*c8d645caSAndroid Build Coastguard Worker 326*c8d645caSAndroid Build Coastguard WorkerA pointer to the *arg* is passed to the callback when calling. It can be used to store any information that the callback might need. 327*c8d645caSAndroid Build Coastguard Worker 328*c8d645caSAndroid Build Coastguard WorkerPreviously the function received just the value of *arg* instead of a pointer to it. This old behaviour can be enabled by defining *PB_OLD_CALLBACK_STYLE*. 329*c8d645caSAndroid Build Coastguard Worker 330*c8d645caSAndroid Build Coastguard WorkerWhen calling `pb_encode`_, *funcs.encode* is used, and similarly when calling `pb_decode`_, *funcs.decode* is used. The function pointers are stored in the same memory location but are of incompatible types. You can set the function pointer to NULL to skip the field. 331*c8d645caSAndroid Build Coastguard Worker 332*c8d645caSAndroid Build Coastguard Workerpb_wire_type_t 333*c8d645caSAndroid Build Coastguard Worker-------------- 334*c8d645caSAndroid Build Coastguard WorkerProtocol Buffers wire types. These are used with `pb_encode_tag`_. :: 335*c8d645caSAndroid Build Coastguard Worker 336*c8d645caSAndroid Build Coastguard Worker typedef enum { 337*c8d645caSAndroid Build Coastguard Worker PB_WT_VARINT = 0, 338*c8d645caSAndroid Build Coastguard Worker PB_WT_64BIT = 1, 339*c8d645caSAndroid Build Coastguard Worker PB_WT_STRING = 2, 340*c8d645caSAndroid Build Coastguard Worker PB_WT_32BIT = 5 341*c8d645caSAndroid Build Coastguard Worker } pb_wire_type_t; 342*c8d645caSAndroid Build Coastguard Worker 343*c8d645caSAndroid Build Coastguard Workerpb_extension_type_t 344*c8d645caSAndroid Build Coastguard Worker------------------- 345*c8d645caSAndroid Build Coastguard WorkerDefines the handler functions and auxiliary data for a field that extends 346*c8d645caSAndroid Build Coastguard Workeranother message. Usually autogenerated by *nanopb_generator.py*:: 347*c8d645caSAndroid Build Coastguard Worker 348*c8d645caSAndroid Build Coastguard Worker typedef struct { 349*c8d645caSAndroid Build Coastguard Worker bool (*decode)(pb_istream_t *stream, pb_extension_t *extension, 350*c8d645caSAndroid Build Coastguard Worker uint32_t tag, pb_wire_type_t wire_type); 351*c8d645caSAndroid Build Coastguard Worker bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension); 352*c8d645caSAndroid Build Coastguard Worker const void *arg; 353*c8d645caSAndroid Build Coastguard Worker } pb_extension_type_t; 354*c8d645caSAndroid Build Coastguard Worker 355*c8d645caSAndroid Build Coastguard WorkerIn the normal case, the function pointers are *NULL* and the decoder and 356*c8d645caSAndroid Build Coastguard Workerencoder use their internal implementations. The internal implementations 357*c8d645caSAndroid Build Coastguard Workerassume that *arg* points to a *pb_field_t* that describes the field in question. 358*c8d645caSAndroid Build Coastguard Worker 359*c8d645caSAndroid Build Coastguard WorkerTo implement custom processing of unknown fields, you can provide pointers 360*c8d645caSAndroid Build Coastguard Workerto your own functions. Their functionality is mostly the same as for normal 361*c8d645caSAndroid Build Coastguard Workercallback fields, except that they get called for any unknown field when decoding. 362*c8d645caSAndroid Build Coastguard Worker 363*c8d645caSAndroid Build Coastguard Workerpb_extension_t 364*c8d645caSAndroid Build Coastguard Worker-------------- 365*c8d645caSAndroid Build Coastguard WorkerTies together the extension field type and the storage for the field value:: 366*c8d645caSAndroid Build Coastguard Worker 367*c8d645caSAndroid Build Coastguard Worker typedef struct { 368*c8d645caSAndroid Build Coastguard Worker const pb_extension_type_t *type; 369*c8d645caSAndroid Build Coastguard Worker void *dest; 370*c8d645caSAndroid Build Coastguard Worker pb_extension_t *next; 371*c8d645caSAndroid Build Coastguard Worker bool found; 372*c8d645caSAndroid Build Coastguard Worker } pb_extension_t; 373*c8d645caSAndroid Build Coastguard Worker 374*c8d645caSAndroid Build Coastguard Worker:type: Pointer to the structure that defines the callback functions. 375*c8d645caSAndroid Build Coastguard Worker:dest: Pointer to the variable that stores the field value 376*c8d645caSAndroid Build Coastguard Worker (as used by the default extension callback functions.) 377*c8d645caSAndroid Build Coastguard Worker:next: Pointer to the next extension handler, or *NULL*. 378*c8d645caSAndroid Build Coastguard Worker:found: Decoder sets this to true if the extension was found. 379*c8d645caSAndroid Build Coastguard Worker 380*c8d645caSAndroid Build Coastguard WorkerPB_GET_ERROR 381*c8d645caSAndroid Build Coastguard Worker------------ 382*c8d645caSAndroid Build Coastguard WorkerGet the current error message from a stream, or a placeholder string if 383*c8d645caSAndroid Build Coastguard Workerthere is no error message:: 384*c8d645caSAndroid Build Coastguard Worker 385*c8d645caSAndroid Build Coastguard Worker #define PB_GET_ERROR(stream) (string expression) 386*c8d645caSAndroid Build Coastguard Worker 387*c8d645caSAndroid Build Coastguard WorkerThis should be used for printing errors, for example:: 388*c8d645caSAndroid Build Coastguard Worker 389*c8d645caSAndroid Build Coastguard Worker if (!pb_decode(...)) 390*c8d645caSAndroid Build Coastguard Worker { 391*c8d645caSAndroid Build Coastguard Worker printf("Decode failed: %s\n", PB_GET_ERROR(stream)); 392*c8d645caSAndroid Build Coastguard Worker } 393*c8d645caSAndroid Build Coastguard Worker 394*c8d645caSAndroid Build Coastguard WorkerThe macro only returns pointers to constant strings (in code memory), 395*c8d645caSAndroid Build Coastguard Workerso that there is no need to release the returned pointer. 396*c8d645caSAndroid Build Coastguard Worker 397*c8d645caSAndroid Build Coastguard WorkerPB_RETURN_ERROR 398*c8d645caSAndroid Build Coastguard Worker--------------- 399*c8d645caSAndroid Build Coastguard WorkerSet the error message and return false:: 400*c8d645caSAndroid Build Coastguard Worker 401*c8d645caSAndroid Build Coastguard Worker #define PB_RETURN_ERROR(stream,msg) (sets error and returns false) 402*c8d645caSAndroid Build Coastguard Worker 403*c8d645caSAndroid Build Coastguard WorkerThis should be used to handle error conditions inside nanopb functions 404*c8d645caSAndroid Build Coastguard Workerand user callback functions:: 405*c8d645caSAndroid Build Coastguard Worker 406*c8d645caSAndroid Build Coastguard Worker if (error_condition) 407*c8d645caSAndroid Build Coastguard Worker { 408*c8d645caSAndroid Build Coastguard Worker PB_RETURN_ERROR(stream, "something went wrong"); 409*c8d645caSAndroid Build Coastguard Worker } 410*c8d645caSAndroid Build Coastguard Worker 411*c8d645caSAndroid Build Coastguard WorkerThe *msg* parameter must be a constant string. 412*c8d645caSAndroid Build Coastguard Worker 413*c8d645caSAndroid Build Coastguard Worker 414*c8d645caSAndroid Build Coastguard Worker 415*c8d645caSAndroid Build Coastguard Workerpb_encode.h 416*c8d645caSAndroid Build Coastguard Worker=========== 417*c8d645caSAndroid Build Coastguard Worker 418*c8d645caSAndroid Build Coastguard Workerpb_ostream_from_buffer 419*c8d645caSAndroid Build Coastguard Worker---------------------- 420*c8d645caSAndroid Build Coastguard WorkerConstructs an output stream for writing into a memory buffer. This is just a helper function, it doesn't do anything you couldn't do yourself in a callback function. It uses an internal callback that stores the pointer in stream *state* field. :: 421*c8d645caSAndroid Build Coastguard Worker 422*c8d645caSAndroid Build Coastguard Worker pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize); 423*c8d645caSAndroid Build Coastguard Worker 424*c8d645caSAndroid Build Coastguard Worker:buf: Memory buffer to write into. 425*c8d645caSAndroid Build Coastguard Worker:bufsize: Maximum number of bytes to write. 426*c8d645caSAndroid Build Coastguard Worker:returns: An output stream. 427*c8d645caSAndroid Build Coastguard Worker 428*c8d645caSAndroid Build Coastguard WorkerAfter writing, you can check *stream.bytes_written* to find out how much valid data there is in the buffer. 429*c8d645caSAndroid Build Coastguard Worker 430*c8d645caSAndroid Build Coastguard Workerpb_write 431*c8d645caSAndroid Build Coastguard Worker-------- 432*c8d645caSAndroid Build Coastguard WorkerWrites data to an output stream. Always use this function, instead of trying to call stream callback manually. :: 433*c8d645caSAndroid Build Coastguard Worker 434*c8d645caSAndroid Build Coastguard Worker bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count); 435*c8d645caSAndroid Build Coastguard Worker 436*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 437*c8d645caSAndroid Build Coastguard Worker:buf: Pointer to buffer with the data to be written. 438*c8d645caSAndroid Build Coastguard Worker:count: Number of bytes to write. 439*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false if maximum length is exceeded or an IO error happens. 440*c8d645caSAndroid Build Coastguard Worker 441*c8d645caSAndroid Build Coastguard WorkerIf an error happens, *bytes_written* is not incremented. Depending on the callback used, calling pb_write again after it has failed once may be dangerous. Nanopb itself never does this, instead it returns the error to user application. The builtin pb_ostream_from_buffer is safe to call again after failed write. 442*c8d645caSAndroid Build Coastguard Worker 443*c8d645caSAndroid Build Coastguard Workerpb_encode 444*c8d645caSAndroid Build Coastguard Worker--------- 445*c8d645caSAndroid Build Coastguard WorkerEncodes the contents of a structure as a protocol buffers message and writes it to output stream. :: 446*c8d645caSAndroid Build Coastguard Worker 447*c8d645caSAndroid Build Coastguard Worker bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); 448*c8d645caSAndroid Build Coastguard Worker 449*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 450*c8d645caSAndroid Build Coastguard Worker:fields: A field description array, usually autogenerated. 451*c8d645caSAndroid Build Coastguard Worker:src_struct: Pointer to the data that will be serialized. 452*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error, on detectable errors in field description, or if a field encoder returns false. 453*c8d645caSAndroid Build Coastguard Worker 454*c8d645caSAndroid Build Coastguard WorkerNormally pb_encode simply walks through the fields description array and serializes each field in turn. However, submessages must be serialized twice: first to calculate their size and then to actually write them to output. This causes some constraints for callback fields, which must return the same data on every call. 455*c8d645caSAndroid Build Coastguard Worker 456*c8d645caSAndroid Build Coastguard Workerpb_encode_delimited 457*c8d645caSAndroid Build Coastguard Worker------------------- 458*c8d645caSAndroid Build Coastguard WorkerCalculates the length of the message, encodes it as varint and then encodes the message. :: 459*c8d645caSAndroid Build Coastguard Worker 460*c8d645caSAndroid Build Coastguard Worker bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); 461*c8d645caSAndroid Build Coastguard Worker 462*c8d645caSAndroid Build Coastguard Worker(parameters are the same as for `pb_encode`_.) 463*c8d645caSAndroid Build Coastguard Worker 464*c8d645caSAndroid Build Coastguard WorkerA common way to indicate the message length in Protocol Buffers is to prefix it with a varint. 465*c8d645caSAndroid Build Coastguard WorkerThis function does this, and it is compatible with *parseDelimitedFrom* in Google's protobuf library. 466*c8d645caSAndroid Build Coastguard Worker 467*c8d645caSAndroid Build Coastguard Worker.. sidebar:: Encoding fields manually 468*c8d645caSAndroid Build Coastguard Worker 469*c8d645caSAndroid Build Coastguard Worker The functions with names *pb_encode_\** are used when dealing with callback fields. The typical reason for using callbacks is to have an array of unlimited size. In that case, `pb_encode`_ will call your callback function, which in turn will call *pb_encode_\** functions repeatedly to write out values. 470*c8d645caSAndroid Build Coastguard Worker 471*c8d645caSAndroid Build Coastguard Worker The tag of a field must be encoded separately with `pb_encode_tag_for_field`_. After that, you can call exactly one of the content-writing functions to encode the payload of the field. For repeated fields, you can repeat this process multiple times. 472*c8d645caSAndroid Build Coastguard Worker 473*c8d645caSAndroid Build Coastguard Worker Writing packed arrays is a little bit more involved: you need to use `pb_encode_tag` and specify `PB_WT_STRING` as the wire type. Then you need to know exactly how much data you are going to write, and use `pb_encode_varint`_ to write out the number of bytes before writing the actual data. Substreams can be used to determine the number of bytes beforehand; see `pb_encode_submessage`_ source code for an example. 474*c8d645caSAndroid Build Coastguard Worker 475*c8d645caSAndroid Build Coastguard Workerpb_get_encoded_size 476*c8d645caSAndroid Build Coastguard Worker------------------- 477*c8d645caSAndroid Build Coastguard WorkerCalculates the length of the encoded message. :: 478*c8d645caSAndroid Build Coastguard Worker 479*c8d645caSAndroid Build Coastguard Worker bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct); 480*c8d645caSAndroid Build Coastguard Worker 481*c8d645caSAndroid Build Coastguard Worker:size: Calculated size of the encoded message. 482*c8d645caSAndroid Build Coastguard Worker:fields: A field description array, usually autogenerated. 483*c8d645caSAndroid Build Coastguard Worker:src_struct: Pointer to the data that will be serialized. 484*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on detectable errors in field description or if a field encoder returns false. 485*c8d645caSAndroid Build Coastguard Worker 486*c8d645caSAndroid Build Coastguard Workerpb_encode_tag 487*c8d645caSAndroid Build Coastguard Worker------------- 488*c8d645caSAndroid Build Coastguard WorkerStarts a field in the Protocol Buffers binary format: encodes the field number and the wire type of the data. :: 489*c8d645caSAndroid Build Coastguard Worker 490*c8d645caSAndroid Build Coastguard Worker bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number); 491*c8d645caSAndroid Build Coastguard Worker 492*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 1-5 bytes will be written. 493*c8d645caSAndroid Build Coastguard Worker:wiretype: PB_WT_VARINT, PB_WT_64BIT, PB_WT_STRING or PB_WT_32BIT 494*c8d645caSAndroid Build Coastguard Worker:field_number: Identifier for the field, defined in the .proto file. You can get it from field->tag. 495*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error. 496*c8d645caSAndroid Build Coastguard Worker 497*c8d645caSAndroid Build Coastguard Workerpb_encode_tag_for_field 498*c8d645caSAndroid Build Coastguard Worker----------------------- 499*c8d645caSAndroid Build Coastguard WorkerSame as `pb_encode_tag`_, except takes the parameters from a *pb_field_t* structure. :: 500*c8d645caSAndroid Build Coastguard Worker 501*c8d645caSAndroid Build Coastguard Worker bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field); 502*c8d645caSAndroid Build Coastguard Worker 503*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 1-5 bytes will be written. 504*c8d645caSAndroid Build Coastguard Worker:field: Field description structure. Usually autogenerated. 505*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error or unknown field type. 506*c8d645caSAndroid Build Coastguard Worker 507*c8d645caSAndroid Build Coastguard WorkerThis function only considers the LTYPE of the field. You can use it from your field callbacks, because the source generator writes correct LTYPE also for callback type fields. 508*c8d645caSAndroid Build Coastguard Worker 509*c8d645caSAndroid Build Coastguard WorkerWire type mapping is as follows: 510*c8d645caSAndroid Build Coastguard Worker 511*c8d645caSAndroid Build Coastguard Worker============================================= ============ 512*c8d645caSAndroid Build Coastguard WorkerLTYPEs Wire type 513*c8d645caSAndroid Build Coastguard Worker============================================= ============ 514*c8d645caSAndroid Build Coastguard WorkerVARINT, UVARINT, SVARINT PB_WT_VARINT 515*c8d645caSAndroid Build Coastguard WorkerFIXED64 PB_WT_64BIT 516*c8d645caSAndroid Build Coastguard WorkerSTRING, BYTES, SUBMESSAGE, FIXED_LENGTH_BYTES PB_WT_STRING 517*c8d645caSAndroid Build Coastguard WorkerFIXED32 PB_WT_32BIT 518*c8d645caSAndroid Build Coastguard Worker============================================= ============ 519*c8d645caSAndroid Build Coastguard Worker 520*c8d645caSAndroid Build Coastguard Workerpb_encode_varint 521*c8d645caSAndroid Build Coastguard Worker---------------- 522*c8d645caSAndroid Build Coastguard WorkerEncodes a signed or unsigned integer in the varint_ format. Works for fields of type `bool`, `enum`, `int32`, `int64`, `uint32` and `uint64`:: 523*c8d645caSAndroid Build Coastguard Worker 524*c8d645caSAndroid Build Coastguard Worker bool pb_encode_varint(pb_ostream_t *stream, uint64_t value); 525*c8d645caSAndroid Build Coastguard Worker 526*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 1-10 bytes will be written. 527*c8d645caSAndroid Build Coastguard Worker:value: Value to encode. Just cast e.g. int32_t directly to uint64_t. 528*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error. 529*c8d645caSAndroid Build Coastguard Worker 530*c8d645caSAndroid Build Coastguard Worker.. _varint: http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints 531*c8d645caSAndroid Build Coastguard Worker 532*c8d645caSAndroid Build Coastguard Workerpb_encode_svarint 533*c8d645caSAndroid Build Coastguard Worker----------------- 534*c8d645caSAndroid Build Coastguard WorkerEncodes a signed integer in the 'zig-zagged' format. Works for fields of type `sint32` and `sint64`:: 535*c8d645caSAndroid Build Coastguard Worker 536*c8d645caSAndroid Build Coastguard Worker bool pb_encode_svarint(pb_ostream_t *stream, int64_t value); 537*c8d645caSAndroid Build Coastguard Worker 538*c8d645caSAndroid Build Coastguard Worker(parameters are the same as for `pb_encode_varint`_ 539*c8d645caSAndroid Build Coastguard Worker 540*c8d645caSAndroid Build Coastguard Workerpb_encode_string 541*c8d645caSAndroid Build Coastguard Worker---------------- 542*c8d645caSAndroid Build Coastguard WorkerWrites the length of a string as varint and then contents of the string. Works for fields of type `bytes` and `string`:: 543*c8d645caSAndroid Build Coastguard Worker 544*c8d645caSAndroid Build Coastguard Worker bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size); 545*c8d645caSAndroid Build Coastguard Worker 546*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 547*c8d645caSAndroid Build Coastguard Worker:buffer: Pointer to string data. 548*c8d645caSAndroid Build Coastguard Worker:size: Number of bytes in the string. Pass `strlen(s)` for strings. 549*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error. 550*c8d645caSAndroid Build Coastguard Worker 551*c8d645caSAndroid Build Coastguard Workerpb_encode_fixed32 552*c8d645caSAndroid Build Coastguard Worker----------------- 553*c8d645caSAndroid Build Coastguard WorkerWrites 4 bytes to stream and swaps bytes on big-endian architectures. Works for fields of type `fixed32`, `sfixed32` and `float`:: 554*c8d645caSAndroid Build Coastguard Worker 555*c8d645caSAndroid Build Coastguard Worker bool pb_encode_fixed32(pb_ostream_t *stream, const void *value); 556*c8d645caSAndroid Build Coastguard Worker 557*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 558*c8d645caSAndroid Build Coastguard Worker:value: Pointer to a 4-bytes large C variable, for example `uint32_t foo;`. 559*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error. 560*c8d645caSAndroid Build Coastguard Worker 561*c8d645caSAndroid Build Coastguard Workerpb_encode_fixed64 562*c8d645caSAndroid Build Coastguard Worker----------------- 563*c8d645caSAndroid Build Coastguard WorkerWrites 8 bytes to stream and swaps bytes on big-endian architecture. Works for fields of type `fixed64`, `sfixed64` and `double`:: 564*c8d645caSAndroid Build Coastguard Worker 565*c8d645caSAndroid Build Coastguard Worker bool pb_encode_fixed64(pb_ostream_t *stream, const void *value); 566*c8d645caSAndroid Build Coastguard Worker 567*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 568*c8d645caSAndroid Build Coastguard Worker:value: Pointer to a 8-bytes large C variable, for example `uint64_t foo;`. 569*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error. 570*c8d645caSAndroid Build Coastguard Worker 571*c8d645caSAndroid Build Coastguard Workerpb_encode_submessage 572*c8d645caSAndroid Build Coastguard Worker-------------------- 573*c8d645caSAndroid Build Coastguard WorkerEncodes a submessage field, including the size header for it. Works for fields of any message type:: 574*c8d645caSAndroid Build Coastguard Worker 575*c8d645caSAndroid Build Coastguard Worker bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); 576*c8d645caSAndroid Build Coastguard Worker 577*c8d645caSAndroid Build Coastguard Worker:stream: Output stream to write to. 578*c8d645caSAndroid Build Coastguard Worker:fields: Pointer to the autogenerated field description array for the submessage type, e.g. `MyMessage_fields`. 579*c8d645caSAndroid Build Coastguard Worker:src: Pointer to the structure where submessage data is. 580*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO errors, pb_encode errors or if submessage size changes between calls. 581*c8d645caSAndroid Build Coastguard Worker 582*c8d645caSAndroid Build Coastguard WorkerIn Protocol Buffers format, the submessage size must be written before the submessage contents. Therefore, this function has to encode the submessage twice in order to know the size beforehand. 583*c8d645caSAndroid Build Coastguard Worker 584*c8d645caSAndroid Build Coastguard WorkerIf the submessage contains callback fields, the callback function might misbehave and write out a different amount of data on the second call. This situation is recognized and *false* is returned, but garbage will be written to the output before the problem is detected. 585*c8d645caSAndroid Build Coastguard Worker 586*c8d645caSAndroid Build Coastguard Worker 587*c8d645caSAndroid Build Coastguard Worker 588*c8d645caSAndroid Build Coastguard Worker 589*c8d645caSAndroid Build Coastguard Worker 590*c8d645caSAndroid Build Coastguard Worker 591*c8d645caSAndroid Build Coastguard Worker 592*c8d645caSAndroid Build Coastguard Worker 593*c8d645caSAndroid Build Coastguard Worker 594*c8d645caSAndroid Build Coastguard Worker 595*c8d645caSAndroid Build Coastguard Worker 596*c8d645caSAndroid Build Coastguard Worker 597*c8d645caSAndroid Build Coastguard Workerpb_decode.h 598*c8d645caSAndroid Build Coastguard Worker=========== 599*c8d645caSAndroid Build Coastguard Worker 600*c8d645caSAndroid Build Coastguard Workerpb_istream_from_buffer 601*c8d645caSAndroid Build Coastguard Worker---------------------- 602*c8d645caSAndroid Build Coastguard WorkerHelper function for creating an input stream that reads data from a memory buffer. :: 603*c8d645caSAndroid Build Coastguard Worker 604*c8d645caSAndroid Build Coastguard Worker pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize); 605*c8d645caSAndroid Build Coastguard Worker 606*c8d645caSAndroid Build Coastguard Worker:buf: Pointer to byte array to read from. 607*c8d645caSAndroid Build Coastguard Worker:bufsize: Size of the byte array. 608*c8d645caSAndroid Build Coastguard Worker:returns: An input stream ready to use. 609*c8d645caSAndroid Build Coastguard Worker 610*c8d645caSAndroid Build Coastguard Workerpb_read 611*c8d645caSAndroid Build Coastguard Worker------- 612*c8d645caSAndroid Build Coastguard WorkerRead data from input stream. Always use this function, don't try to call the stream callback directly. :: 613*c8d645caSAndroid Build Coastguard Worker 614*c8d645caSAndroid Build Coastguard Worker bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count); 615*c8d645caSAndroid Build Coastguard Worker 616*c8d645caSAndroid Build Coastguard Worker:stream: Input stream to read from. 617*c8d645caSAndroid Build Coastguard Worker:buf: Buffer to store the data to, or NULL to just read data without storing it anywhere. 618*c8d645caSAndroid Build Coastguard Worker:count: Number of bytes to read. 619*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false if *stream->bytes_left* is less than *count* or if an IO error occurs. 620*c8d645caSAndroid Build Coastguard Worker 621*c8d645caSAndroid Build Coastguard WorkerEnd of file is signalled by *stream->bytes_left* being zero after pb_read returns false. 622*c8d645caSAndroid Build Coastguard Worker 623*c8d645caSAndroid Build Coastguard Workerpb_decode 624*c8d645caSAndroid Build Coastguard Worker--------- 625*c8d645caSAndroid Build Coastguard WorkerRead and decode all fields of a structure. Reads until EOF on input stream. :: 626*c8d645caSAndroid Build Coastguard Worker 627*c8d645caSAndroid Build Coastguard Worker bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 628*c8d645caSAndroid Build Coastguard Worker 629*c8d645caSAndroid Build Coastguard Worker:stream: Input stream to read from. 630*c8d645caSAndroid Build Coastguard Worker:fields: A field description array. Usually autogenerated. 631*c8d645caSAndroid Build Coastguard Worker:dest_struct: Pointer to structure where data will be stored. 632*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error, on detectable errors in field description, if a field encoder returns false or if a required field is missing. 633*c8d645caSAndroid Build Coastguard Worker 634*c8d645caSAndroid Build Coastguard WorkerIn Protocol Buffers binary format, EOF is only allowed between fields. If it happens anywhere else, pb_decode will return *false*. If pb_decode returns false, you cannot trust any of the data in the structure. 635*c8d645caSAndroid Build Coastguard Worker 636*c8d645caSAndroid Build Coastguard WorkerIn addition to EOF, the pb_decode implementation supports terminating a message with a 0 byte. This is compatible with the official Protocol Buffers because 0 is never a valid field tag. 637*c8d645caSAndroid Build Coastguard Worker 638*c8d645caSAndroid Build Coastguard WorkerFor optional fields, this function applies the default value and sets *has_<field>* to false if the field is not present. 639*c8d645caSAndroid Build Coastguard Worker 640*c8d645caSAndroid Build Coastguard WorkerIf *PB_ENABLE_MALLOC* is defined, this function may allocate storage for any pointer type fields. 641*c8d645caSAndroid Build Coastguard WorkerIn this case, you have to call `pb_release`_ to release the memory after you are done with the message. 642*c8d645caSAndroid Build Coastguard WorkerOn error return `pb_decode` will release the memory itself. 643*c8d645caSAndroid Build Coastguard Worker 644*c8d645caSAndroid Build Coastguard Workerpb_decode_noinit 645*c8d645caSAndroid Build Coastguard Worker---------------- 646*c8d645caSAndroid Build Coastguard WorkerSame as `pb_decode`_, except does not apply the default values to fields. :: 647*c8d645caSAndroid Build Coastguard Worker 648*c8d645caSAndroid Build Coastguard Worker bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 649*c8d645caSAndroid Build Coastguard Worker 650*c8d645caSAndroid Build Coastguard Worker(parameters are the same as for `pb_decode`_.) 651*c8d645caSAndroid Build Coastguard Worker 652*c8d645caSAndroid Build Coastguard WorkerThe destination structure should be filled with zeros before calling this function. Doing a *memset* manually can be slightly faster than using `pb_decode`_ if you don't need any default values. 653*c8d645caSAndroid Build Coastguard Worker 654*c8d645caSAndroid Build Coastguard WorkerIn addition to decoding a single message, this function can be used to merge two messages, so that 655*c8d645caSAndroid Build Coastguard Workervalues from previous message will remain if the new message does not contain a field. 656*c8d645caSAndroid Build Coastguard Worker 657*c8d645caSAndroid Build Coastguard WorkerThis function *will not* release the message even on error return. If you use *PB_ENABLE_MALLOC*, 658*c8d645caSAndroid Build Coastguard Workeryou will need to call `pb_release`_ yourself. 659*c8d645caSAndroid Build Coastguard Worker 660*c8d645caSAndroid Build Coastguard Workerpb_decode_delimited 661*c8d645caSAndroid Build Coastguard Worker------------------- 662*c8d645caSAndroid Build Coastguard WorkerSame as `pb_decode`_, except that it first reads a varint with the length of the message. :: 663*c8d645caSAndroid Build Coastguard Worker 664*c8d645caSAndroid Build Coastguard Worker bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); 665*c8d645caSAndroid Build Coastguard Worker 666*c8d645caSAndroid Build Coastguard Worker(parameters are the same as for `pb_decode`_.) 667*c8d645caSAndroid Build Coastguard Worker 668*c8d645caSAndroid Build Coastguard WorkerA common method to indicate message size in Protocol Buffers is to prefix it with a varint. 669*c8d645caSAndroid Build Coastguard WorkerThis function is compatible with *writeDelimitedTo* in the Google's Protocol Buffers library. 670*c8d645caSAndroid Build Coastguard Worker 671*c8d645caSAndroid Build Coastguard Workerpb_release 672*c8d645caSAndroid Build Coastguard Worker---------- 673*c8d645caSAndroid Build Coastguard WorkerReleases any dynamically allocated fields:: 674*c8d645caSAndroid Build Coastguard Worker 675*c8d645caSAndroid Build Coastguard Worker void pb_release(const pb_field_t fields[], void *dest_struct); 676*c8d645caSAndroid Build Coastguard Worker 677*c8d645caSAndroid Build Coastguard Worker:fields: A field description array. Usually autogenerated. 678*c8d645caSAndroid Build Coastguard Worker:dest_struct: Pointer to structure where data is stored. If NULL, function does nothing. 679*c8d645caSAndroid Build Coastguard Worker 680*c8d645caSAndroid Build Coastguard WorkerThis function is only available if *PB_ENABLE_MALLOC* is defined. It will release any 681*c8d645caSAndroid Build Coastguard Workerpointer type fields in the structure and set the pointers to NULL. 682*c8d645caSAndroid Build Coastguard Worker 683*c8d645caSAndroid Build Coastguard Workerpb_decode_tag 684*c8d645caSAndroid Build Coastguard Worker------------- 685*c8d645caSAndroid Build Coastguard WorkerDecode the tag that comes before field in the protobuf encoding:: 686*c8d645caSAndroid Build Coastguard Worker 687*c8d645caSAndroid Build Coastguard Worker bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); 688*c8d645caSAndroid Build Coastguard Worker 689*c8d645caSAndroid Build Coastguard Worker:stream: Input stream to read from. 690*c8d645caSAndroid Build Coastguard Worker:wire_type: Pointer to variable where to store the wire type of the field. 691*c8d645caSAndroid Build Coastguard Worker:tag: Pointer to variable where to store the tag of the field. 692*c8d645caSAndroid Build Coastguard Worker:eof: Pointer to variable where to store end-of-file status. 693*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on error or EOF. 694*c8d645caSAndroid Build Coastguard Worker 695*c8d645caSAndroid Build Coastguard WorkerWhen the message (stream) ends, this function will return false and set *eof* to true. On other 696*c8d645caSAndroid Build Coastguard Workererrors, *eof* will be set to false. 697*c8d645caSAndroid Build Coastguard Worker 698*c8d645caSAndroid Build Coastguard Workerpb_skip_field 699*c8d645caSAndroid Build Coastguard Worker------------- 700*c8d645caSAndroid Build Coastguard WorkerRemove the data for a field from the stream, without actually decoding it:: 701*c8d645caSAndroid Build Coastguard Worker 702*c8d645caSAndroid Build Coastguard Worker bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); 703*c8d645caSAndroid Build Coastguard Worker 704*c8d645caSAndroid Build Coastguard Worker:stream: Input stream to read from. 705*c8d645caSAndroid Build Coastguard Worker:wire_type: Type of field to skip. 706*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO error. 707*c8d645caSAndroid Build Coastguard Worker 708*c8d645caSAndroid Build Coastguard Worker.. sidebar:: Decoding fields manually 709*c8d645caSAndroid Build Coastguard Worker 710*c8d645caSAndroid Build Coastguard Worker The functions with names beginning with *pb_decode_* are used when dealing with callback fields. The typical reason for using callbacks is to have an array of unlimited size. In that case, `pb_decode`_ will call your callback function repeatedly, which can then store the values into e.g. filesystem in the order received in. 711*c8d645caSAndroid Build Coastguard Worker 712*c8d645caSAndroid Build Coastguard Worker For decoding numeric (including enumerated and boolean) values, use `pb_decode_varint`_, `pb_decode_svarint`_, `pb_decode_fixed32`_ and `pb_decode_fixed64`_. They take a pointer to a 32- or 64-bit C variable, which you may then cast to smaller datatype for storage. 713*c8d645caSAndroid Build Coastguard Worker 714*c8d645caSAndroid Build Coastguard Worker For decoding strings and bytes fields, the length has already been decoded. You can therefore check the total length in *stream->bytes_left* and read the data using `pb_read`_. 715*c8d645caSAndroid Build Coastguard Worker 716*c8d645caSAndroid Build Coastguard Worker Finally, for decoding submessages in a callback, simply use `pb_decode`_ and pass it the *SubMessage_fields* descriptor array. 717*c8d645caSAndroid Build Coastguard Worker 718*c8d645caSAndroid Build Coastguard Workerpb_decode_varint 719*c8d645caSAndroid Build Coastguard Worker---------------- 720*c8d645caSAndroid Build Coastguard WorkerRead and decode a varint_ encoded integer. :: 721*c8d645caSAndroid Build Coastguard Worker 722*c8d645caSAndroid Build Coastguard Worker bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); 723*c8d645caSAndroid Build Coastguard Worker 724*c8d645caSAndroid Build Coastguard Worker:stream: Input stream to read from. 1-10 bytes will be read. 725*c8d645caSAndroid Build Coastguard Worker:dest: Storage for the decoded integer. Value is undefined on error. 726*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false if value exceeds uint64_t range or an IO error happens. 727*c8d645caSAndroid Build Coastguard Worker 728*c8d645caSAndroid Build Coastguard Workerpb_decode_svarint 729*c8d645caSAndroid Build Coastguard Worker----------------- 730*c8d645caSAndroid Build Coastguard WorkerSimilar to `pb_decode_varint`_, except that it performs zigzag-decoding on the value. This corresponds to the Protocol Buffers *sint32* and *sint64* datatypes. :: 731*c8d645caSAndroid Build Coastguard Worker 732*c8d645caSAndroid Build Coastguard Worker bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); 733*c8d645caSAndroid Build Coastguard Worker 734*c8d645caSAndroid Build Coastguard Worker(parameters are the same as `pb_decode_varint`_) 735*c8d645caSAndroid Build Coastguard Worker 736*c8d645caSAndroid Build Coastguard Workerpb_decode_fixed32 737*c8d645caSAndroid Build Coastguard Worker----------------- 738*c8d645caSAndroid Build Coastguard WorkerDecode a *fixed32*, *sfixed32* or *float* value. :: 739*c8d645caSAndroid Build Coastguard Worker 740*c8d645caSAndroid Build Coastguard Worker bool pb_decode_fixed32(pb_istream_t *stream, void *dest); 741*c8d645caSAndroid Build Coastguard Worker 742*c8d645caSAndroid Build Coastguard Worker:stream: Input stream to read from. 4 bytes will be read. 743*c8d645caSAndroid Build Coastguard Worker:dest: Pointer to destination *int32_t*, *uint32_t* or *float*. 744*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO errors. 745*c8d645caSAndroid Build Coastguard Worker 746*c8d645caSAndroid Build Coastguard WorkerThis function reads 4 bytes from the input stream. 747*c8d645caSAndroid Build Coastguard WorkerOn big endian architectures, it then reverses the order of the bytes. 748*c8d645caSAndroid Build Coastguard WorkerFinally, it writes the bytes to *dest*. 749*c8d645caSAndroid Build Coastguard Worker 750*c8d645caSAndroid Build Coastguard Workerpb_decode_fixed64 751*c8d645caSAndroid Build Coastguard Worker----------------- 752*c8d645caSAndroid Build Coastguard WorkerDecode a *fixed64*, *sfixed64* or *double* value. :: 753*c8d645caSAndroid Build Coastguard Worker 754*c8d645caSAndroid Build Coastguard Worker bool pb_decode_fixed64(pb_istream_t *stream, void *dest); 755*c8d645caSAndroid Build Coastguard Worker 756*c8d645caSAndroid Build Coastguard Worker:stream: Input stream to read from. 8 bytes will be read. 757*c8d645caSAndroid Build Coastguard Worker:dest: Pointer to destination *int64_t*, *uint64_t* or *double*. 758*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false on IO errors. 759*c8d645caSAndroid Build Coastguard Worker 760*c8d645caSAndroid Build Coastguard WorkerSame as `pb_decode_fixed32`_, except this reads 8 bytes. 761*c8d645caSAndroid Build Coastguard Worker 762*c8d645caSAndroid Build Coastguard Workerpb_make_string_substream 763*c8d645caSAndroid Build Coastguard Worker------------------------ 764*c8d645caSAndroid Build Coastguard WorkerDecode the length for a field with wire type *PB_WT_STRING* and create a substream for reading the data. :: 765*c8d645caSAndroid Build Coastguard Worker 766*c8d645caSAndroid Build Coastguard Worker bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); 767*c8d645caSAndroid Build Coastguard Worker 768*c8d645caSAndroid Build Coastguard Worker:stream: Original input stream to read the length and data from. 769*c8d645caSAndroid Build Coastguard Worker:substream: New substream that has limited length. Filled in by the function. 770*c8d645caSAndroid Build Coastguard Worker:returns: True on success, false if reading the length fails. 771*c8d645caSAndroid Build Coastguard Worker 772*c8d645caSAndroid Build Coastguard WorkerThis function uses `pb_decode_varint`_ to read an integer from the stream. This is interpreted as a number of bytes, and the substream is set up so that its `bytes_left` is initially the same as the length, and its callback function and state the same as the parent stream. 773*c8d645caSAndroid Build Coastguard Worker 774*c8d645caSAndroid Build Coastguard Workerpb_close_string_substream 775*c8d645caSAndroid Build Coastguard Worker------------------------- 776*c8d645caSAndroid Build Coastguard WorkerClose the substream created with `pb_make_string_substream`_. :: 777*c8d645caSAndroid Build Coastguard Worker 778*c8d645caSAndroid Build Coastguard Worker void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); 779*c8d645caSAndroid Build Coastguard Worker 780*c8d645caSAndroid Build Coastguard Worker:stream: Original input stream to read the length and data from. 781*c8d645caSAndroid Build Coastguard Worker:substream: Substream to close 782*c8d645caSAndroid Build Coastguard Worker 783*c8d645caSAndroid Build Coastguard WorkerThis function copies back the state from the substream to the parent stream. 784*c8d645caSAndroid Build Coastguard WorkerIt must be called after done with the substream. 785