1# Nanopb: Migration from older versions 2 3This document details all the breaking changes that have been made to 4nanopb since its initial release. For each change, the rationale and 5required modifications of user applications are explained. Also any 6error indications are included, in order to make it easier to find this 7document. 8 9Nanopb-0.4.4 (2020-11-25) 10------------------------- 11 12### Remove outdated generator/nanopb/options.proto 13 14**Changes:** Back in 2018, it was considered in pull request #241 to 15move nanopb generator options to a separate namespace. For this reason, 16a transitional file was added. It was later abandoned and is now removed 17to avoid confusion. 18 19**Required actions:** Most nanopb users probably never used that transitional 20file at all. If your `.proto` files import it, change to using `generator/proto/nanopb.proto`. 21 22**Error indications:** Errors about missing file `options.proto` when running 23the generator. 24 25Nanopb-0.4.3 (2020-09-21) 26------------------------- 27 28### pb_msgdesc_t struct has new fields 29 30**Changes:** New fields `required_field_count` and 31`largest_tag` were added to `pb_msgdesc_t` 32and existing fields were reordered. 33 34**Required actions:** All `.pb.c` files must be recompiled. 35Regeneration is not needed. 36 37**Error indications:** Messages may fail to encode or decode, or the 38code can crash inside `load_descriptor_values()` in 39`pb_common.c`. 40 41Nanopb-0.4.2 (2020-06-23) 42------------------------- 43 44### Generator now uses Python 3 by default 45 46**Rationale:** Previously `nanopb-generator.py` had hashbang 47of `#!/usr/bin/env python`, which would execute with Python 482 on most systems. Python 2 is now deprecated and many libraries are 49dropping support for it, which makes installing dependencies difficult. 50While `nanopb_generator.py` has worked with Python 3 for 51years now, and overriding the python version was possible with 52virtualenv, that was an extra complication. 53 54**Changes:** Hashbang now uses `#!/usr/bin/env python3`. 55New file `nanopb_generator.py2` can be used to run with 56Python 2, if necessary. 57 58**Required actions:** If possible, just verify Python 3 is installed and 59necessary dependencies are installed for it. For example `pip3 install protobuf grpcio-tools` 60should take care of it. If this is not possible, call `nanopb_generator.py2` from your build 61scripts instead. 62 63**Error indications:** `python3: command not found` if 64Python 3 is not installed. 65`Could not import the Google protobuf Python libraries` if dependencies are only installed for Python 2. 66 67Nanopb-0.4.0 (2019-12-20) 68------------------------- 69 70### New field descriptor format 71 72**Rationale:** Previously information about struct fields was stored as 73an array of `pb_field_t` structures. This was a 74straightforward method, but required allocating space for e.g. 75submessage type and array size for all fields, even though most fields 76are not submessages nor arrays. 77 78**Changes:** Now field information is encoded more efficiently in 79`uint32_t` array in a variable-length format. Old 80`pb_field_t` structure has been removed and it is now a 81typedef for `pb_field_iter_t`. This retains compatibility 82with most old callback definitions. The field definitions in 83`.pb.h` files are now of type `pb_msgdesc_t`. 84 85**Required actions:** If your own code accesses the low-level field 86information in `pb_field_t`, it must be modified to do so 87only through the functions declared in `pb_common.h`. 88 89**Error indications:** `incompatible pointer type` errors 90relating to `pb_field_t` 91 92### Changes to generator default options 93 94**Rationale:** Previously nanopb_generator added a timestamp header to 95generated files and used only basename of files in 96`#include` directives. This is different than what the 97`protoc` C++ backend does. 98 99**Changes:** Now default options are `--no-timestamp` and 100`--no-strip-path`. 101 102**Required actions:** If old behaviour is desired, add 103`--timestamp` and `--strip-path` options to 104`nanopb_generator.py` or on `protoc` command 105line as `--nanopb_out=--timestamp,--strip-path:outdir`. 106 107**Error indications:** Compiler error: cannot find include file 108`mymessage.pb.h` when compiling 109`mymessage.pb.c`. 110 111### Removal of bundled plugin.proto 112 113**Rationale:** Google's Python protobuf library, which is used in 114nanopb generator, has included `plugin_pb2` with it since 115version 3.1.0. It is not necessary to bundle it with nanopb anymore. 116 117**Required actions:** Update `python-protobuf` to version 1183.1.0 or newer. 119 120**Error indications:** `ImportError: No module named compiler.plugin_pb2` 121 122### .options file is now always case-sensitive 123 124**Rationale:** Previously field names in `.options` file 125were case-sensitive on Linux and case-insensitive on Windows. This was 126by accident. Because `.proto` files are case-sensitive, 127`.options` files should be too. 128 129**Changes:** Now field names in `.options` are always 130case-sensitive, and matched by `fnmatchcase()` instead of 131`fnmatch()`. 132 133**Required actions:** If field names in `.options` are not 134capitalized the same as in `.proto`, they must be updated. 135 136### `CHAR_BIT` define is now needed 137 138**Rationale:** To check whether the platform has 8-bit or larger chars, 139the C standard `CHAR_BIT` macro is needed. 140 141**Changes:** `pb.h` now includes `limits.h` for this macro. 142 143**Required actions:** If your platform doesn't have `limits.h` 144available, you can define the macro in `pb_syshdr.h`. There is an 145example in `extra` directory. 146 147**Error indications:** `"Cannot find include file <limits.h>."` or 148`"Undefined identifier: CHAR_BIT."` 149 150### Strings must now always be null-terminated 151 152**Rationale:** Previously `pb_encode()` would accept non-terminated 153strings and assume that they are the full length of the defined array. 154However, `pb_decode()` would reject such messages because null 155terminator wouldn't fit in the array. 156 157**Changes:** `pb_encode()` will now return an error if null terminator 158is missing. Maximum encoded message size calculation is changed 159accordingly so that at most `max_size-1` strings are assumed. New field 160option `max_length` can be used to define the maximum string length, 161instead of the array size. 162 163**Required actions:** If your strings were previously filling the whole 164allocated array, increase the size of the field by 1. 165 166**Error indications:** `pb_encode()` returns error `unterminated string`. 167 168### Removal of per-field default value constants 169 170**Rationale:** Previously nanopb declared a 171`fieldname_default` constant variable for each field with a 172default value, and used these internally to initialize messages. This 173however used unnecessarily large amount of storage for the values. The 174variables were mostly for internal usage, but were available in the 175header file. 176 177**Changes:** Default values are now stored as an encoded protobuf 178message. 179 180**Required actions:** If your code previously used default constants, it 181will have to be adapted to take the default value in some other way, 182such as by defining 183`static const MyMessage msg_default = MyMessage_init_default;` and accessing 184`msg_default.fieldname`. 185 186**Error indications:** Compiler error about `fieldname_default` being undeclared. 187 188### Zero tag in message now raises error by default 189 190**Rationale:** Previously nanopb has allowed messages to be terminated 191by a null byte, which is read as zero tag value. Most other protobuf 192implementations don't support this, so it is not very useful feature. 193It has also been noted that this can complicate debugging issues with 194corrupted messages. 195 196**Changes:** `pb_decode()` now gives error when it 197encounters zero tag value. A new function `pb_decode_ex()` 198supports flag `PB_DECODE_NULLTERMINATED` that supports 199decoding null terminated messages. 200 201**Required actions:** If application uses null termination for messages, 202switch it to use `pb_decode_ex()` and 203`pb_encode_ex()`. If compatibility with 0.3.9.x is needed, 204there are also `pb_decode_nullterminated()` and 205`pb_encode_nullterminated()` macros, which work both in 2060.4.0 and 0.3.9. 207 208**Error indications:** Error message from `pb_decode()`: `zero_tag`. 209 210### Submessages now have has_field in proto3 mode 211 212**Rationale:** Previously nanopb considered proto3 submessages as 213present only when their contents was non-zero. Most other protobuf 214libraries allow explicit null state for submessages. 215 216**Changes:** Submessages now have separate `has_field` in 217proto3 mode also. 218 219**Required actions:** When using submessages in proto3 mode, user code 220must now set `mymsg.has_submsg = true` for each submessage 221that is present. Alternatively, the field option 222`proto3_singular_msgs` can be used to restore the old 223behavior. 224 225**Error indications:** Submessages do not get encoded. 226 227### PB_OLD_CALLBACK_STYLE option has been removed 228 229**Rationale:** Back in 2013, function signature for callbacks was 230changed. The `PB_OLD_CALLBACK_STYLE` option allowed 231compatibility with old code, but complicated code and testing because of 232the different options. 233 234**Changes:** `PB_OLD_CALLBACK_STYLE` option no-longer has 235any effect. 236 237**Required actions:** If `PB_OLD_CALLBACK_STYLE` option 238was in use previously, function signatures must be updated to use double 239pointers (`void**` and `void * const *`). 240 241**Error indications:** Assignment from incompatible pointer type. 242 243### protoc insertion points are no longer included by default 244 245**Rationale:** Protoc allows including comments in form 246`@@protoc_insertion_point` to identify locations for 247other plugins to insert their own extra content. Previously these were 248included by default, but they clutter the generated files and are rarely 249used. 250 251**Changes:** Insertion points are now included only when 252`--protoc-insertion-points` option is passed to the 253generator. 254 255Nanopb-0.3.9.4, 0.4.0 (2019-10-13) 256---------------------------------- 257 258### Fix generation of min/max defines for enum types 259 260**Rationale:** Nanopb generator makes \#defines for enum minimum and 261maximum value. Previously these defines incorrectly had the first and 262last enum value, instead of the actual minimum and maximum. (issue 263#405) 264 265**Changes:** Minimum define now always has the smallest value, and 266maximum define always has the largest value. 267 268**Required actions:** If these defines are used and enum values in 269.proto file are not defined in ascending order, user code behaviour may 270change. Check that user code doesn\'t expect the old, incorrect 271first/last behaviour. 272 273### Fix undefined behavior related to bool fields 274 275**Rationale:** In C99, `bool` variables are not allowed to 276have other values than `true` and `false`. 277Compilers use this fact in optimization, and constructs like 278`int foo = msg.has_field ? 100 : 0;` will give unexpected results 279otherwise. Previously nanopb didn\'t enforce that decoded bool fields 280had valid values. 281 282**Changes:** Bool fields are now handled separately as 283`PB_LTYPE_BOOL`. The `LTYPE` descriptor 284numbers for other field types were renumbered. 285 286**Required actions:** Source code files must be recompiled, but 287regenerating `.pb.h`/`.pb.c` files from 288`.proto` is not required. If user code directly uses the 289nanopb internal field representation (search for 290`PB_LTYPE_VARINT` in source), it may need updating. 291 292Nanopb-0.3.9.1, 0.4.0 (2018-04-14) 293---------------------------------- 294 295### Fix handling of string and bytes default values 296 297**Rationale:** Previously nanopb didn't properly decode special 298character escapes like `\200` emitted by protoc. This caused these 299escapes to end up verbatim in the default values in .pb.c file. 300 301**Changes:** Escapes are now decoded, and e.g. `\200` or `\x80` 302results in {0x80} for bytes field and `"\x80"` for string field. 303 304**Required actions:** If code has previously relied on `\` in default 305value being passed through verbatim, it must now be changed to `\\`. 306 307Nanopb-0.3.8 (2017-03-05) 308------------------------- 309 310### Fully drain substreams before closing 311 312**Rationale:** If the substream functions were called directly and the 313caller did not completely empty the substring before closing it, the 314parent stream would be put into an incorrect state. 315 316**Changes:** `pb_close_string_substream` can now error and returns a 317boolean. 318 319**Required actions:** Add error checking onto any call to 320`pb_close_string_substream`. 321 322### Change oneof format in .pb.c files 323 324**Rationale:** Previously two oneofs in a single message would be 325erroneously handled as part of the same union. 326 327**Changes:** Oneofs fields now use special `PB_DATAOFFSET_UNION` 328offset type in generated .pb.c files to distinguish whether they are the 329first or following field inside an union. 330 331**Required actions:** Regenerate `.pb.c/.pb.h` files with new nanopb 332version if oneofs are used. 333 334Nanopb-0.3.5 (2016-02-13) 335------------------------- 336 337### Add support for platforms without uint8_t 338 339**Rationale:** Some platforms cannot access 8-bit sized values directly, 340and do not define `uint8_t`. Nanopb previously didn\'t support these 341platforms. 342 343**Changes:** References to `uint8_t` were replaced with several 344alternatives, one of them being a new `pb_byte_t` typedef. This in 345turn uses `uint_least8_t` which means the smallest available type. 346 347**Required actions:** If your platform does not have a 348standards-compliant `stdint.h`, it may lack the definition for 349`[u]int_least8_t`. This must be added manually, example can be found 350in `extra/pb_syshdr.h`. 351 352**Error indications:** Compiler error: `"unknown type name 'uint_least8_t'"`. 353 354Nanopb-0.3.2 (2015-01-24) 355------------------------- 356 357### Add support for OneOfs 358 359**Rationale:** Previously nanopb did not support the `oneof` construct 360in `.proto` files. Those fields were generated as regular `optional` 361fields. 362 363**Changes:** OneOfs are now generated as C unions. Callback fields are 364not supported inside oneof and generator gives an error. 365 366**Required actions:** The generator option `no_unions` can be used to 367restore old behaviour and to allow callbacks to be used. To use unions, 368one change is needed: use `which_xxxx` field to detect which field is 369present, instead of `has_xxxx`. Compare the value against 370`MyStruct_myfield_tag`. 371 372**Error indications:** Generator error: `"Callback fields inside of 373oneof are not supported"`. Compiler error: `"Message"` has no member 374named `"has_xxxx"`. 375 376Nanopb-0.3.0 (2014-08-26) 377------------------------- 378 379### Separate field iterator logic to pb_common.c 380 381**Rationale:** Originally, the field iteration logic was simple enough 382to be duplicated in `pb_decode.c` and `pb_encode.c`. New field types 383have made the logic more complex, which required the creation of a new 384file to contain the common functionality. 385 386**Changes:** There is a new file, `pb_common.c`, which must be included 387in builds. 388 389**Required actions:** Add `pb_common.c` to build rules. This file is 390always required. Either `pb_decode.c` or `pb_encode.c` can still be 391left out if some functionality is not needed. 392 393**Error indications:** Linker error: undefined reference to 394`pb_field_iter_begin`, `pb_field_iter_next` or similar. 395 396### Change data type of field counts to pb_size_t 397 398**Rationale:** Often nanopb is used with small arrays, such as 255 items 399or less. Using a full `size_t` field to store the array count wastes 400memory if there are many arrays. There already exists parameters 401`PB_FIELD_16BIT` and `PB_FIELD_32BIT` which tell nanopb what is the 402maximum size of arrays in use. 403 404**Changes:** Generator will now use `pb_size_t` for the array 405`_count` fields. The size of the type will be controlled by the 406`PB_FIELD_16BIT` and `PB_FIELD_32BIT` compilation time options. 407 408**Required actions:** Regenerate all `.pb.h` files. In some cases casts 409to the `pb_size_t` type may need to be added in the user code when 410accessing the `_count` fields. 411 412**Error indications:** Incorrect data at runtime, crashes. But note that 413other changes in the same version already require regenerating the files 414and have better indications of errors, so this is only an issue for 415development versions. 416 417### Renamed some macros and identifiers 418 419**Rationale:** Some names in nanopb core were badly chosen and 420conflicted with ISO C99 reserved names or lacked a prefix. While they 421haven\'t caused trouble so far, it is reasonable to switch to 422non-conflicting names as these are rarely used from user code. 423 424**Changes:** The following identifier names have changed: 425 426 - Macros: 427 - STATIC_ASSERT(x) -> PB_STATIC_ASSERT(x) 428 - UNUSED(x) -> PB_UNUSED(x) 429 - Include guards: 430 - PB_filename -> PB_filename_INCLUDED 431 - Structure forward declaration tags: 432 - _pb_field_t -> pb_field_s 433 - _pb_bytes_array_t -> pb_bytes_array_s 434 - _pb_callback_t -> pb_callback_s 435 - _pb_extension_type_t -> pb_extension_type_s 436 - _pb_extension_t -> pb_extension_s 437 - _pb_istream_t -> pb_istream_s 438 - _pb_ostream_t -> pb_ostream_s 439 440**Required actions:** Regenerate all `.pb.c` files. If you use any of 441the above identifiers in your application code, perform search-replace 442to the new name. 443 444**Error indications:** Compiler errors on lines with the macro/type 445names. 446 447Nanopb-0.2.9 (2014-08-09) 448------------------------- 449 450### Change semantics of generator -e option 451 452**Rationale:** Some compilers do not accept filenames with two dots 453(like in default extension .pb.c). The `-e` option to the generator 454allowed changing the extension, but not skipping the extra dot. 455 456**Changes:** The `-e` option in generator will no longer add the 457prepending dot. The default value has been adjusted accordingly to 458`.pb.c` to keep the default behaviour the same as before. 459 460**Required actions:** Only if using the generator -e option. Add dot 461before the parameter value on the command line. 462 463**Error indications:** File not found when trying to compile generated 464files. 465 466Nanopb-0.2.7 (2014-04-07) 467------------------------- 468 469### Changed pointer-type bytes field datatype 470 471**Rationale:** In the initial pointer encoding support since 472nanopb-0.2.5, the bytes type used a separate `pb_bytes_ptr_t` type to 473represent `bytes` fields. This made it easy to encode data from a 474separate, user-allocated buffer. However, it made the internal logic 475more complex and was inconsistent with the other types. 476 477**Changes:** Dynamically allocated bytes fields now have the 478`pb_bytes_array_t` type, just like statically allocated ones. 479 480**Required actions:** Only if using pointer-type fields with the bytes 481datatype. Change any access to `msg->field.size` to 482`msg->field->size`. Change any allocation to reserve space of amount 483`PB_BYTES_ARRAY_T_ALLOCSIZE(n)`. If the data pointer was begin 484assigned from external source, implement the field using a callback 485function instead. 486 487**Error indications:** Compiler error: unknown type name 488`pb_bytes_ptr_t`. 489 490Nanopb-0.2.4 (2013-11-07) 491------------------------- 492 493### Remove the NANOPB_INTERNALS compilation option 494 495**Rationale:** Having the option in the headers required the functions 496to be non-static, even if the option is not used. This caused errors on 497some static analysis tools. 498 499**Changes:** The `\#ifdef` and associated functions were removed from 500the header. 501 502**Required actions:** Only if the `NANOPB_INTERNALS` option was 503previously used. Actions are as listed under nanopb-0.1.3 and 504nanopb-0.1.6. 505 506**Error indications:** Compiler warning: implicit declaration of 507function `pb_dec_string`, `pb_enc_string`, or similar. 508 509Nanopb-0.2.1 (2013-04-14) 510------------------------- 511 512### Callback function signature 513 514**Rationale:** Previously the auxilary data to field callbacks was 515passed as `void*`. This allowed passing of any data, but made it 516unnecessarily complex to return a pointer from callback. 517 518**Changes:** The callback function parameter was changed to `void**`. 519 520**Required actions:** You can continue using the old callback style by 521defining `PB_OLD_CALLBACK_STYLE`. Recommended action is to: 522 523- Change the callback signatures to contain `void**` for decoders and `void * const *` for encoders. 524- Change the callback function body to use **arg` instead of `arg`. 525 526**Error indications:** Compiler warning: assignment from incompatible 527pointer type, when initializing `funcs.encode` or `funcs.decode`. 528 529Nanopb-0.2.0 (2013-03-02) 530------------------------- 531 532### Reformatted generated .pb.c file using macros 533 534**Rationale:** Previously the generator made a list of C `pb_field_t` 535initializers in the .pb.c file. This led to a need to regenerate all 536.pb.c files after even small changes to the `pb_field_t` definition. 537 538**Changes:** Macros were added to pb.h which allow for cleaner 539definition of the .pb.c contents. By changing the macro definitions, 540changes to the field structure are possible without breaking 541compatibility with old .pb.c files. 542 543**Required actions:** Regenerate all .pb.c files from the .proto 544sources. 545 546**Error indications:** Compiler warning: implicit declaration of 547function `pb_delta_end`. 548 549### Changed pb_type_t definitions 550 551**Rationale:** The `pb_type_t` was previously an enumeration type. 552This caused warnings on some compilers when using bitwise operations to 553set flags inside the values. 554 555**Changes:** The `pb_type_t` was changed to *typedef uint8_t*. The 556values were changed to `#define`. Some value names were changed for 557consistency. 558 559**Required actions:** Only if you directly access the 560`pb_field_t` contents in your own code, something which is 561not usually done. Needed changes: 562 563- Change `PB_HTYPE_ARRAY` to `PB_HTYPE_REPEATED`. 564- Change `PB_HTYPE_CALLBACK` to `PB_ATYPE()` and `PB_ATYPE_CALLBACK`. 565 566**Error indications:** Compiler error: `PB_HTYPE_ARRAY` or 567`PB_HTYPE_CALLBACK` undeclared. 568 569Nanopb-0.1.6 (2012-09-02) 570------------------------- 571 572### Refactored field decoder interface 573 574**Rationale:** Similarly to field encoders in nanopb-0.1.3. 575 576**Changes:** New functions with names `pb_decode_*` were added. 577 578**Required actions:** By defining NANOPB_INTERNALS, you can still keep 579using the old functions. Recommended action is to replace any calls with 580the newer `pb_decode_*` equivalents. 581 582**Error indications:** Compiler warning: implicit declaration of 583function `pb_dec_string`, `pb_dec_varint`, `pb_dec_submessage` or 584similar. 585 586Nanopb-0.1.3 (2012-06-12) 587------------------------- 588 589### Refactored field encoder interface 590 591**Rationale:** The old `pb_enc_*` functions were designed mostly for 592the internal use by the core. Because they are internally accessed 593through function pointers, their signatures had to be common. This led 594to a confusing interface for external users. 595 596**Changes:** New functions with names `pb_encode_*` were added. These 597have easier to use interfaces. The old functions are now only thin 598wrappers for the new interface. 599 600**Required actions:** By defining NANOPB_INTERNALS, you can still keep 601using the old functions. Recommended action is to replace any calls with 602the newer `pb_encode_*` equivalents. 603 604**Error indications:** Compiler warning: implicit declaration of 605function `pb_enc_string`, *pb_enc_varint,`pb_enc_submessage\` or 606similar. 607