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