mirror of https://github.com/grpc/grpc.git
parent
47a06ace92
commit
7ec6e8a4de
257 changed files with 7 additions and 23687 deletions
File diff suppressed because it is too large
Load Diff
@ -1,15 +0,0 @@ |
||||
# Copyright 2018 gRPC authors. |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
|
||||
set(_gRPC_NANOPB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/nanopb") |
@ -1,19 +0,0 @@ |
||||
cc_library( |
||||
name = "nanopb", |
||||
srcs = [ |
||||
"pb_common.c", |
||||
"pb_decode.c", |
||||
"pb_encode.c", |
||||
], |
||||
hdrs = [ |
||||
"pb.h", |
||||
"pb_common.h", |
||||
"pb_decode.h", |
||||
"pb_encode.h", |
||||
], |
||||
defines = [ |
||||
"PB_FIELD_32BIT=1", |
||||
], |
||||
visibility = ["//visibility:public"], |
||||
) |
||||
|
@ -1,28 +0,0 @@ |
||||
*.gcda |
||||
*.gcno |
||||
*.gcov |
||||
*.o |
||||
*.pb.c |
||||
*.pb.h |
||||
*.pb |
||||
*.pyc |
||||
*_pb2.py |
||||
*~ |
||||
*.tar.gz |
||||
.sconsign.dblite |
||||
config.log |
||||
.sconf_temp |
||||
tests/build |
||||
julkaisu.txt |
||||
dist |
||||
docs/*.html |
||||
docs/generator_flow.png |
||||
examples/simple/simple |
||||
examples/network_server/client |
||||
examples/network_server/server |
||||
examples/using_double_on_avr/decode_double |
||||
examples/using_double_on_avr/encode_double |
||||
examples/using_double_on_avr/test_conversions |
||||
examples/using_union_messages/decode |
||||
examples/using_union_messages/encode |
||||
generator/nanopb_pb2.pyc |
@ -1,54 +0,0 @@ |
||||
# Travis CI has no ability to handle 3 langauges (c, c++, python) |
||||
# and it overrides $CC/$CXX if language is set to c/c++ (only one, not both). |
||||
# |
||||
# Set language to python since at least the result of that is something useful. |
||||
language: python |
||||
|
||||
python: |
||||
- "2.7" |
||||
- "3.4" |
||||
|
||||
# Manage the C/C++ compiler manually |
||||
env: |
||||
- CC=gcc CXX=g++ |
||||
- CC=gcc-4.8 CXX=g++-4.8 |
||||
- CC=gcc-4.9 CXX=g++-4.9 |
||||
- CC=gcc-5 CXX=g++-5 |
||||
- CC=clang CXX=clang++ |
||||
|
||||
addons: |
||||
apt: |
||||
sources: |
||||
- ubuntu-toolchain-r-test |
||||
packages: |
||||
- gcc-4.8 |
||||
- g++-4.8 |
||||
- gcc-4.9 |
||||
- g++-4.9 |
||||
- gcc-5 |
||||
- g++-5 |
||||
|
||||
|
||||
before_install: |
||||
- export PATH=$HOME/.local/bin:$HOME/protobuf/bin:$PATH |
||||
- export MAKEFLAGS=-j$(grep processor /proc/cpuinfo | wc -l) |
||||
- $CC --version |
||||
- $CXX --version |
||||
- python --version |
||||
- lsb_release -a |
||||
|
||||
# Seems to be issues with concurrent builds |
||||
#cache: |
||||
# directories: |
||||
# - $HOME/protobuf |
||||
|
||||
install: |
||||
- curl -L https://github.com/google/protobuf/releases/download/v3.0.0-beta-1/protobuf-python-3.0.0-alpha-4.tar.gz | tar xzf - |
||||
&& pushd protobuf-3.0.0-alpha-4 |
||||
&& ./configure --prefix=$HOME/protobuf && make && make install |
||||
&& pushd python && python setup.py build && python setup.py install && popd |
||||
&& popd |
||||
|
||||
script: |
||||
- pushd generator/proto && make && popd |
||||
- pushd tests && python2 $(which scons) CC=$CC CXX=$CXX && popd |
@ -1,24 +0,0 @@ |
||||
Petteri Aimonen <jpa@npb.mail.kapsi.fi> |
||||
Michael Poole <mdpoole@troilus.org> |
||||
Daniel Kan <extremeblue99@gmail.com> |
||||
Stan Hu <stanhu@aclimalabs.com> |
||||
dch <david.hotham@blueyonder.co.uk> |
||||
Steffen Siering <steffen siering gmail com> |
||||
Jens Steinhauser <jens.steinhauser@gmail.com> |
||||
Pavel Ilin <ilin.pa@gmail.com> |
||||
Kent Ryhorchuk <kryhorchuk@xeralux.com> |
||||
Martin Donath <scifish@gmail.com> |
||||
Oliver Lee <oliverzlee@gmail.com> |
||||
Michael Haberler <git@mah.priv.at> |
||||
Nicolas Colomer <ncolomer@viadeoteam.com> |
||||
Ivan Kravets <me@ikravets.com> |
||||
Kyle Manna <kyle@kylemanna.com> |
||||
Benjamin Kamath <ben.kamath@synapse.com> |
||||
Andrew Ruder <andrew.ruder@elecsyscorp.com> |
||||
Kenshi Kawaguchi <kenshi@recurse.ca> |
||||
isotes <isotes@gmail.com> |
||||
Maxim Khitrov <max@mxcrypt.com> |
||||
Yaniv Mordekhay <yanivmo@users.noreply.github.com> |
||||
Ming Zhao <mzhao@luminatewireless.com> |
||||
Google, Inc. |
||||
Tom Roeder <tmroeder@google.com> |
@ -1,24 +0,0 @@ |
||||
licenses(["notice"]) |
||||
|
||||
exports_files(["LICENSE.txt"]) |
||||
|
||||
package(default_visibility = ["//visibility:public"]) |
||||
|
||||
cc_library( |
||||
name = "nanopb", |
||||
srcs = [ |
||||
"pb_common.c", |
||||
"pb_decode.c", |
||||
"pb_encode.c", |
||||
], |
||||
hdrs = [ |
||||
"pb.h", |
||||
"pb_common.h", |
||||
"pb_decode.h", |
||||
"pb_encode.h", |
||||
], |
||||
defines = [ |
||||
"PB_FIELD_32BIT=1", |
||||
], |
||||
visibility = ["//visibility:public"], |
||||
) |
@ -1,241 +0,0 @@ |
||||
nanopb-0.3.6 (2016-06-19) |
||||
Protect against corrupted _count fields in pb_release (#205) |
||||
Fix error in STATIC_ASSERT with multiple files (#203) |
||||
Add -D option to specify output directory (#193) |
||||
Generate MIN/MAX/ARRAYSIZE defines for enums (#194) |
||||
Generate comments about uncalculable message sizes (#195) |
||||
Documentation updates (#196, #201) |
||||
Improvements to test cases. |
||||
|
||||
nanopb-0.3.5 (2016-02-13) |
||||
NOTE: If you are using pb_syshdr.h, you will need to add uint_least8_t |
||||
definition. See docs/migration.rst for details. |
||||
|
||||
Fix generator crash with Enum inside Oneof (#188) |
||||
Fix some generator regressions related to .options file path (#172) |
||||
Add support for platforms without uint8_t (#191) |
||||
Allow const parameter to pb_istream_from_buffer (#152) |
||||
Ignore null pointers in pb_release() (#183) |
||||
Add support for anonymous unions (#184) |
||||
Add Python3 support to the generator (#169) |
||||
Add code generator insertion points to generated files (#178) |
||||
Improvements to CMake script (#181) |
||||
Improvements to test cases. |
||||
|
||||
nanopb-0.3.4 (2015-09-26) |
||||
Fix handling of unsigned 8- and 16-bit enums (issue 164) |
||||
Fix generator on systems where python = python3. (issue 155) |
||||
Fix compiler warning on GCC 5.x (issue 171) |
||||
Make the generator better handle imported .protos (issue 165) |
||||
Add packed_enum option to generator. |
||||
Add syntax= line to .proto files (issue 167) |
||||
Add PlatformIO registry manifest file. (pr 156) |
||||
|
||||
nanopb-0.3.3 (2015-04-10) |
||||
Fix missing files in Linux binary package (issue 146) |
||||
Fix generator bug when oneof is first field in a message. (issue 142) |
||||
Fix generator error when long_names:false is combined with Oneofs. (issue 147) |
||||
Fix oneof submessage initialization bug. (issue 149) |
||||
Fix problem with plugin options on Python 2.7.2 and older. (issue 153) |
||||
Fix crash when callback is inside oneof field. (issue 148) |
||||
Switch to .tar.gz format for Mac OS X packages. (issue 154) |
||||
Always define enum long names so that cross-file references work. (issue 118) |
||||
Add msgid generator option. (issue 151) |
||||
Improve comment support in .options files. (issue 145) |
||||
Updates for the CMake rule file, add cmake example. |
||||
Better error messages for syntax errors in .options file |
||||
|
||||
nanopb-0.3.2 (2015-01-24) |
||||
Fix memory leaks with PB_ENABLE_MALLOC with some submessage hierarchies (issue 138) |
||||
Implement support for oneofs (C unions). (issues 131, 141) |
||||
Add int_size option for generator (issue 139) |
||||
Add compilation option to disable struct packing. (issue 136) |
||||
Change PB_RETURN_ERROR() macro to avoid compiler warnings (issue 140) |
||||
Fix build problems with protoc 3.0.0 |
||||
Add support for POINTER type in extensions |
||||
Initialize also extension fields to defaults in pb_decode(). |
||||
Detect too large varint values when decoding. |
||||
|
||||
nanopb-0.3.1 (2014-09-11) |
||||
Fix security issue due to size_t overflows. (issue 132) |
||||
Fix memory leak with duplicated fields and PB_ENABLE_MALLOC |
||||
Fix crash if pb_release() is called twice. |
||||
Fix cyclic message support (issue 130) |
||||
Fix error in generated initializers for repeated pointer fields. |
||||
Improve tests (issues 113, 126) |
||||
|
||||
nanopb-0.3.0 (2014-08-26) |
||||
NOTE: See docs/migration.html or online at |
||||
http://koti.kapsi.fi/~jpa/nanopb/docs/migration.html |
||||
for changes in this version. Most importantly, you need to add |
||||
pb_common.c to the list of files to compile. |
||||
|
||||
Separated field iterator logic to pb_common.c (issue 128) |
||||
Change the _count fields to use pb_size_t datatype (issue 82) |
||||
Added PB_ prefix to macro names (issue 106) |
||||
Added #if version guard to generated files (issue 129) |
||||
Added migration document |
||||
|
||||
nanopb-0.2.9 (2014-08-09) |
||||
NOTE: If you are using the -e option with the generator, you have |
||||
to prepend . to the argument to get the same behaviour as before. |
||||
|
||||
Do not automatically add a dot with generator -e option. (issue 122) |
||||
Fix problem with .options file and extension fields. (issue 125) |
||||
Don't use SIZE_MAX macro, as it is not in C89. (issue 120) |
||||
Generate #defines for initializing message structures. (issue 79) |
||||
Add skip_message option to generator. (issue 121) |
||||
Add PB_PACKED_STRUCT support for Keil MDK-ARM toolchain (issue 119) |
||||
Give better messages about the .options file path. (issue 124) |
||||
Improved tests |
||||
|
||||
nanopb-0.2.8 (2014-05-20) |
||||
Fix security issue with PB_ENABLE_MALLOC. (issue 117) |
||||
Add option to not add timestamps to .pb.h and .pb.c preambles. (issue 115) |
||||
Documentation updates |
||||
Improved tests |
||||
|
||||
nanopb-0.2.7 (2014-04-07) |
||||
Fix bug with default values for extension fields (issue 111) |
||||
Fix some MISRA-C warnings (issue 91) |
||||
Implemented optional malloc() support (issue 80) |
||||
Changed pointer-type bytes field datatype |
||||
Add a "found" field to pb_extension_t (issue 112) |
||||
Add convenience function pb_get_encoded_size() (issue 16) |
||||
|
||||
nanopb-0.2.6 (2014-02-15) |
||||
Fix generator error with bytes callback fields (issue 99) |
||||
Fix warnings about large integer constants (issue 102) |
||||
Add comments to where STATIC_ASSERT is used (issue 96) |
||||
Add warning about unknown field names on .options (issue 105) |
||||
Move descriptor.proto to google/protobuf subdirectory (issue 104) |
||||
Improved tests |
||||
|
||||
nanopb-0.2.5 (2014-01-01) |
||||
Fix a bug with encoding negative values in int32 fields (issue 97) |
||||
Create binary packages of the generator + dependencies (issue 47) |
||||
Add support for pointer-type fields to the encoder (part of issue 80) |
||||
Fixed path in FindNanopb.cmake (issue 94) |
||||
Improved tests |
||||
|
||||
nanopb-0.2.4 (2013-11-07) |
||||
Remove the deprecated NANOPB_INTERNALS functions from public API. |
||||
Document the security model. |
||||
Check array and bytes max sizes when encoding (issue 90) |
||||
Add #defines for maximum encoded message size (issue 89) |
||||
Add #define tags for extension fields (issue 93) |
||||
Fix MISRA C violations (issue 91) |
||||
Clean up pb_field_t definition with typedefs. |
||||
|
||||
nanopb-0.2.3 (2013-09-18) |
||||
Improve compatibility by removing ternary operator from initializations (issue 88) |
||||
Fix build error on Visual C++ (issue 84, patch by Markus Schwarzenberg) |
||||
Don't stop on unsupported extension fields (issue 83) |
||||
Add an example pb_syshdr.h file for non-C99 compilers |
||||
Reorganize tests and examples into subfolders (issue 63) |
||||
Switch from Makefiles to scons for building the tests |
||||
Make the tests buildable on Windows |
||||
|
||||
nanopb-0.2.2 (2013-08-18) |
||||
Add support for extension fields (issue 17) |
||||
Fix unknown fields in empty message (issue 78) |
||||
Include the field tags in the generated .pb.h file. |
||||
Add pb_decode_delimited and pb_encode_delimited wrapper functions (issue 74) |
||||
Add a section in top of pb.h for changing compilation settings (issue 76) |
||||
Documentation improvements (issues 12, 77 and others) |
||||
Improved tests |
||||
|
||||
nanopb-0.2.1 (2013-04-14) |
||||
NOTE: The default callback function signature has changed. |
||||
If you don't want to update your code, define PB_OLD_CALLBACK_STYLE. |
||||
|
||||
Change the callback function to use void** (issue 69) |
||||
Add support for defining the nanopb options in a separate file (issue 12) |
||||
Add support for packed structs in IAR and MSVC (in addition to GCC) (issue 66) |
||||
Implement error message support for the encoder side (issue 7) |
||||
Handle unterminated strings when encoding (issue 68) |
||||
Fix bug with empty strings in repeated string callbacks (issue 73) |
||||
Fix regression in 0.2.0 with optional callback fields (issue 70) |
||||
Fix bugs with empty message types (issues 64, 65) |
||||
Fix some compiler warnings on clang (issue 67) |
||||
Some portability improvements (issues 60, 62) |
||||
Various new generator options |
||||
Improved tests |
||||
|
||||
nanopb-0.2.0 (2013-03-02) |
||||
NOTE: This release requires you to regenerate all .pb.c |
||||
files. Files generated by older versions will not |
||||
compile anymore. |
||||
|
||||
Reformat generated .pb.c files using macros (issue 58) |
||||
Rename PB_HTYPE_ARRAY -> PB_HTYPE_REPEATED |
||||
Separate PB_HTYPE to PB_ATYPE and PB_HTYPE |
||||
Move STATIC_ASSERTs to .pb.c file |
||||
Added CMake file (by Pavel Ilin) |
||||
Add option to give file extension to generator (by Michael Haberler) |
||||
Documentation updates |
||||
|
||||
nanopb-0.1.9 (2013-02-13) |
||||
Fixed error message bugs (issues 52, 56) |
||||
Sanitize #ifndef filename (issue 50) |
||||
Performance improvements |
||||
Add compile-time option PB_BUFFER_ONLY |
||||
Add Java package name to nanopb.proto |
||||
Check for sizeof(double) == 8 (issue 54) |
||||
Added generator option to ignore some fields. (issue 51) |
||||
Added generator option to make message structs packed. (issue 49) |
||||
Add more test cases. |
||||
|
||||
nanopb-0.1.8 (2012-12-13) |
||||
Fix bugs in the enum short names introduced in 0.1.7 (issues 42, 43) |
||||
Fix STATIC_ASSERT macro when using multiple .proto files. (issue 41) |
||||
Fix missing initialization of istream.errmsg |
||||
Make tests/Makefile work for non-gcc compilers (issue 40) |
||||
|
||||
nanopb-0.1.7 (2012-11-11) |
||||
Remove "skip" mode from pb_istream_t callbacks. Example implementation had a bug. (issue 37) |
||||
Add option to use shorter names for enum values (issue 38) |
||||
Improve options support in generator (issues 12, 30) |
||||
Add nanopb version number to generated files (issue 36) |
||||
Add extern "C" to generated headers (issue 35) |
||||
Add names for structs to allow forward declaration (issue 39) |
||||
Add buffer size check in example (issue 34) |
||||
Fix build warnings on MS compilers (issue 33) |
||||
|
||||
nanopb-0.1.6 (2012-09-02) |
||||
Reorganize the field decoder interface (issue 2) |
||||
Improve performance in submessage decoding (issue 28) |
||||
Implement error messages in the decoder side (issue 7) |
||||
Extended testcases (alltypes test is now complete). |
||||
Fix some compiler warnings (issues 25, 26, 27, 32). |
||||
|
||||
nanopb-0.1.5 (2012-08-04) |
||||
Fix bug in decoder with packed arrays (issue 23). |
||||
Extended testcases. |
||||
Fix some compiler warnings. |
||||
|
||||
nanopb-0.1.4 (2012-07-05) |
||||
Add compile-time options for easy-to-use >255 field support. |
||||
Improve the detection of missing required fields. |
||||
Added example on how to handle union messages. |
||||
Fix generator error with .proto without messages. |
||||
Fix problems that stopped the code from compiling with some compilers. |
||||
Fix some compiler warnings. |
||||
|
||||
nanopb-0.1.3 (2012-06-12) |
||||
Refactor the field encoder interface. |
||||
Improve generator error messages (issue 5) |
||||
Add descriptor.proto into the #include exclusion list |
||||
Fix some compiler warnings. |
||||
|
||||
nanopb-0.1.2 (2012-02-15) |
||||
Make the generator to generate include for other .proto files (issue 4). |
||||
Fixed generator not working on Windows (issue 3) |
||||
|
||||
nanopb-0.1.1 (2012-01-14) |
||||
Fixed bug in encoder with 'bytes' fields (issue 1). |
||||
Fixed a bug in the generator that caused a compiler error on sfixed32 and sfixed64 fields. |
||||
Extended testcases. |
||||
|
||||
nanopb-0.1.0 (2012-01-06) |
||||
First stable release. |
@ -1,32 +0,0 @@ |
||||
Contributing to Nanopb development |
||||
================================== |
||||
|
||||
Reporting issues and requesting features |
||||
---------------------------------------- |
||||
|
||||
Feel free to report any issues you see or features you would like |
||||
to see in the future to the Github issue tracker. Using the templates |
||||
below is preferred: |
||||
|
||||
* [Report a bug](https://github.com/nanopb/nanopb/issues/new?body=**Steps%20to%20reproduce%20the%20issue**%0a%0a1.%0a2.%0a3.%0a%0a**What%20happens?**%0A%0A**What%20should%20happen?**&labels=Type-Defect) |
||||
* [Request a feature](https://github.com/nanopb/nanopb/issues/new?body=**What%20should%20the%20feature%20do?**%0A%0A**In%20what%20situation%20would%20the%20feature%20be%20useful?**&labels=Type-Enhancement) |
||||
|
||||
Requesting help |
||||
--------------- |
||||
|
||||
If there is something strange going on, but you do not know if |
||||
it is actually a bug in nanopb, try asking first on the |
||||
[discussion forum](https://groups.google.com/forum/#!forum/nanopb). |
||||
|
||||
Pull requests |
||||
------------- |
||||
|
||||
Pull requests are welcome! |
||||
|
||||
If it is not obvious from the commit message, please indicate the |
||||
same information as you would for an issue report: |
||||
|
||||
* What functionality it fixes/adds. |
||||
* How can the problem be reproduced / when would the feature be useful. |
||||
|
||||
|
@ -1,20 +0,0 @@ |
||||
Copyright (c) 2011 Petteri Aimonen <jpa at nanopb.mail.kapsi.fi> |
||||
|
||||
This software is provided 'as-is', without any express or |
||||
implied warranty. In no event will the authors be held liable |
||||
for any damages arising from the use of this software. |
||||
|
||||
Permission is granted to anyone to use this software for any |
||||
purpose, including commercial applications, and to alter it and |
||||
redistribute it freely, subject to the following restrictions: |
||||
|
||||
1. The origin of this software must not be misrepresented; you |
||||
must not claim that you wrote the original software. If you use |
||||
this software in a product, an acknowledgment in the product |
||||
documentation would be appreciated but is not required. |
||||
|
||||
2. Altered source versions must be plainly marked as such, and |
||||
must not be misrepresented as being the original software. |
||||
|
||||
3. This notice may not be removed or altered from any source |
||||
distribution. |
@ -1,71 +0,0 @@ |
||||
Nanopb - Protocol Buffers for Embedded Systems |
||||
============================================== |
||||
|
||||
[![Build Status](https://travis-ci.org/nanopb/nanopb.svg?branch=master)](https://travis-ci.org/nanopb/nanopb) |
||||
|
||||
Nanopb is a small code-size Protocol Buffers implementation in ansi C. It is |
||||
especially suitable for use in microcontrollers, but fits any memory |
||||
restricted system. |
||||
|
||||
* **Homepage:** http://kapsi.fi/~jpa/nanopb/ |
||||
* **Documentation:** http://kapsi.fi/~jpa/nanopb/docs/ |
||||
* **Downloads:** http://kapsi.fi/~jpa/nanopb/download/ |
||||
* **Forum:** https://groups.google.com/forum/#!forum/nanopb |
||||
|
||||
|
||||
|
||||
Using the nanopb library |
||||
------------------------ |
||||
To use the nanopb library, you need to do two things: |
||||
|
||||
1. Compile your .proto files for nanopb, using protoc. |
||||
2. Include pb_encode.c and pb_decode.c in your project. |
||||
|
||||
The easiest way to get started is to study the project in "examples/simple". |
||||
It contains a Makefile, which should work directly under most Linux systems. |
||||
However, for any other kind of build system, see the manual steps in |
||||
README.txt in that folder. |
||||
|
||||
|
||||
|
||||
Using the Protocol Buffers compiler (protoc) |
||||
-------------------------------------------- |
||||
The nanopb generator is implemented as a plugin for the Google's own protoc |
||||
compiler. This has the advantage that there is no need to reimplement the |
||||
basic parsing of .proto files. However, it does mean that you need the |
||||
Google's protobuf library in order to run the generator. |
||||
|
||||
If you have downloaded a binary package for nanopb (either Windows, Linux or |
||||
Mac OS X version), the 'protoc' binary is included in the 'generator-bin' |
||||
folder. In this case, you are ready to go. Simply run this command: |
||||
|
||||
generator-bin/protoc --nanopb_out=. myprotocol.proto |
||||
|
||||
However, if you are using a git checkout or a plain source distribution, you |
||||
need to provide your own version of protoc and the Google's protobuf library. |
||||
On Linux, the necessary packages are protobuf-compiler and python-protobuf. |
||||
On Windows, you can either build Google's protobuf library from source or use |
||||
one of the binary distributions of it. In either case, if you use a separate |
||||
protoc, you need to manually give the path to nanopb generator: |
||||
|
||||
protoc --plugin=protoc-gen-nanopb=nanopb/generator/protoc-gen-nanopb ... |
||||
|
||||
|
||||
|
||||
Running the tests |
||||
----------------- |
||||
If you want to perform further development of the nanopb core, or to verify |
||||
its functionality using your compiler and platform, you'll want to run the |
||||
test suite. The build rules for the test suite are implemented using Scons, |
||||
so you need to have that installed. To run the tests: |
||||
|
||||
cd tests |
||||
scons |
||||
|
||||
This will show the progress of various test cases. If the output does not |
||||
end in an error, the test cases were successful. |
||||
|
||||
Note: Mac OS X by default aliases 'clang' as 'gcc', while not actually |
||||
supporting the same command line options as gcc does. To run tests on |
||||
Mac OS X, use: "scons CC=clang CXX=clang". Same way can be used to run |
||||
tests with different compilers on any platform. |
@ -1,9 +0,0 @@ |
||||
all: index.html concepts.html reference.html security.html migration.html \
|
||||
generator_flow.png
|
||||
|
||||
%.png: %.svg |
||||
rsvg $< $@
|
||||
|
||||
%.html: %.rst |
||||
rst2html --stylesheet=lsr.css --link-stylesheet $< $@
|
||||
sed -i 's!</head>!<link href="favicon.ico" type="image/x-icon" rel="shortcut icon" />\n</head>!' $@
|
@ -1,392 +0,0 @@ |
||||
====================== |
||||
Nanopb: Basic concepts |
||||
====================== |
||||
|
||||
.. include :: menu.rst |
||||
|
||||
The things outlined here are the underlying concepts of the nanopb design. |
||||
|
||||
.. contents:: |
||||
|
||||
Proto files |
||||
=========== |
||||
All Protocol Buffers implementations use .proto files to describe the message |
||||
format. The point of these files is to be a portable interface description |
||||
language. |
||||
|
||||
Compiling .proto files for nanopb |
||||
--------------------------------- |
||||
Nanopb uses the Google's protoc compiler to parse the .proto file, and then a |
||||
python script to generate the C header and source code from it:: |
||||
|
||||
user@host:~$ protoc -omessage.pb message.proto |
||||
user@host:~$ python ../generator/nanopb_generator.py message.pb |
||||
Writing to message.h and message.c |
||||
user@host:~$ |
||||
|
||||
Modifying generator behaviour |
||||
----------------------------- |
||||
Using generator options, you can set maximum sizes for fields in order to |
||||
allocate them statically. The preferred way to do this is to create an .options |
||||
file with the same name as your .proto file:: |
||||
|
||||
# Foo.proto |
||||
message Foo { |
||||
required string name = 1; |
||||
} |
||||
|
||||
:: |
||||
|
||||
# Foo.options |
||||
Foo.name max_size:16 |
||||
|
||||
For more information on this, see the `Proto file options`_ section in the |
||||
reference manual. |
||||
|
||||
.. _`Proto file options`: reference.html#proto-file-options |
||||
|
||||
Streams |
||||
======= |
||||
|
||||
Nanopb uses streams for accessing the data in encoded format. |
||||
The stream abstraction is very lightweight, and consists of a structure (*pb_ostream_t* or *pb_istream_t*) which contains a pointer to a callback function. |
||||
|
||||
There are a few generic rules for callback functions: |
||||
|
||||
#) Return false on IO errors. The encoding or decoding process will abort immediately. |
||||
#) Use state to store your own data, such as a file descriptor. |
||||
#) *bytes_written* and *bytes_left* are updated by pb_write and pb_read. |
||||
#) Your callback may be used with substreams. In this case *bytes_left*, *bytes_written* and *max_size* have smaller values than the original stream. Don't use these values to calculate pointers. |
||||
#) Always read or write the full requested length of data. For example, POSIX *recv()* needs the *MSG_WAITALL* parameter to accomplish this. |
||||
|
||||
Output streams |
||||
-------------- |
||||
|
||||
:: |
||||
|
||||
struct _pb_ostream_t |
||||
{ |
||||
bool (*callback)(pb_ostream_t *stream, const uint8_t *buf, size_t count); |
||||
void *state; |
||||
size_t max_size; |
||||
size_t bytes_written; |
||||
}; |
||||
|
||||
The *callback* for output stream may be NULL, in which case the stream simply counts the number of bytes written. In this case, *max_size* is ignored. |
||||
|
||||
Otherwise, if *bytes_written* + bytes_to_be_written is larger than *max_size*, pb_write returns false before doing anything else. If you don't want to limit the size of the stream, pass SIZE_MAX. |
||||
|
||||
**Example 1:** |
||||
|
||||
This is the way to get the size of the message without storing it anywhere:: |
||||
|
||||
Person myperson = ...; |
||||
pb_ostream_t sizestream = {0}; |
||||
pb_encode(&sizestream, Person_fields, &myperson); |
||||
printf("Encoded size is %d\n", sizestream.bytes_written); |
||||
|
||||
**Example 2:** |
||||
|
||||
Writing to stdout:: |
||||
|
||||
bool callback(pb_ostream_t *stream, const uint8_t *buf, size_t count) |
||||
{ |
||||
FILE *file = (FILE*) stream->state; |
||||
return fwrite(buf, 1, count, file) == count; |
||||
} |
||||
|
||||
pb_ostream_t stdoutstream = {&callback, stdout, SIZE_MAX, 0}; |
||||
|
||||
Input streams |
||||
------------- |
||||
For input streams, there is one extra rule: |
||||
|
||||
#) You don't need to know the length of the message in advance. After getting EOF error when reading, set bytes_left to 0 and return false. Pb_decode will detect this and if the EOF was in a proper position, it will return true. |
||||
|
||||
Here is the structure:: |
||||
|
||||
struct _pb_istream_t |
||||
{ |
||||
bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count); |
||||
void *state; |
||||
size_t bytes_left; |
||||
}; |
||||
|
||||
The *callback* must always be a function pointer. *Bytes_left* is an upper limit on the number of bytes that will be read. You can use SIZE_MAX if your callback handles EOF as described above. |
||||
|
||||
**Example:** |
||||
|
||||
This function binds an input stream to stdin: |
||||
|
||||
:: |
||||
|
||||
bool callback(pb_istream_t *stream, uint8_t *buf, size_t count) |
||||
{ |
||||
FILE *file = (FILE*)stream->state; |
||||
bool status; |
||||
|
||||
if (buf == NULL) |
||||
{ |
||||
while (count-- && fgetc(file) != EOF); |
||||
return count == 0; |
||||
} |
||||
|
||||
status = (fread(buf, 1, count, file) == count); |
||||
|
||||
if (feof(file)) |
||||
stream->bytes_left = 0; |
||||
|
||||
return status; |
||||
} |
||||
|
||||
pb_istream_t stdinstream = {&callback, stdin, SIZE_MAX}; |
||||
|
||||
Data types |
||||
========== |
||||
|
||||
Most Protocol Buffers datatypes have directly corresponding C datatypes, such as int32 is int32_t, float is float and bool is bool. However, the variable-length datatypes are more complex: |
||||
|
||||
1) Strings, bytes and repeated fields of any type map to callback functions by default. |
||||
2) If there is a special option *(nanopb).max_size* specified in the .proto file, string maps to null-terminated char array and bytes map to a structure containing a char array and a size field. |
||||
3) If *(nanopb).type* is set to *FT_INLINE* and *(nanopb).max_size* is also set, then bytes map to an inline byte array of fixed size. |
||||
3) If there is a special option *(nanopb).max_count* specified on a repeated field, it maps to an array of whatever type is being repeated. Another field will be created for the actual number of entries stored. |
||||
|
||||
=============================================================================== ======================= |
||||
field in .proto autogenerated in .h |
||||
=============================================================================== ======================= |
||||
required string name = 1; pb_callback_t name; |
||||
required string name = 1 [(nanopb).max_size = 40]; char name[40]; |
||||
repeated string name = 1 [(nanopb).max_size = 40]; pb_callback_t name; |
||||
repeated string name = 1 [(nanopb).max_size = 40, (nanopb).max_count = 5]; | size_t name_count; |
||||
| char name[5][40]; |
||||
required bytes data = 1 [(nanopb).max_size = 40]; | typedef struct { |
||||
| size_t size; |
||||
| pb_byte_t bytes[40]; |
||||
| } Person_data_t; |
||||
| Person_data_t data; |
||||
required bytes data = 1 [(nanopb).max_size = 40, (nanopb.type) = FT_INLINE]; | pb_byte_t data[40]; |
||||
=============================================================================== ======================= |
||||
|
||||
The maximum lengths are checked in runtime. If string/bytes/array exceeds the allocated length, *pb_decode* will return false. |
||||
|
||||
Note: for the *bytes* datatype, the field length checking may not be exact. |
||||
The compiler may add some padding to the *pb_bytes_t* structure, and the nanopb runtime doesn't know how much of the structure size is padding. Therefore it uses the whole length of the structure for storing data, which is not very smart but shouldn't cause problems. In practise, this means that if you specify *(nanopb).max_size=5* on a *bytes* field, you may be able to store 6 bytes there. For the *string* field type, the length limit is exact. |
||||
|
||||
Field callbacks |
||||
=============== |
||||
When a field has dynamic length, nanopb cannot statically allocate storage for it. Instead, it allows you to handle the field in whatever way you want, using a callback function. |
||||
|
||||
The `pb_callback_t`_ structure contains a function pointer and a *void* pointer called *arg* you can use for passing data to the callback. If the function pointer is NULL, the field will be skipped. A pointer to the *arg* is passed to the function, so that it can modify it and retrieve the value. |
||||
|
||||
The actual behavior of the callback function is different in encoding and decoding modes. In encoding mode, the callback is called once and should write out everything, including field tags. In decoding mode, the callback is called repeatedly for every data item. |
||||
|
||||
.. _`pb_callback_t`: reference.html#pb-callback-t |
||||
|
||||
Encoding callbacks |
||||
------------------ |
||||
:: |
||||
|
||||
bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg); |
||||
|
||||
When encoding, the callback should write out complete fields, including the wire type and field number tag. It can write as many or as few fields as it likes. For example, if you want to write out an array as *repeated* field, you should do it all in a single call. |
||||
|
||||
Usually you can use `pb_encode_tag_for_field`_ to encode the wire type and tag number of the field. However, if you want to encode a repeated field as a packed array, you must call `pb_encode_tag`_ instead to specify a wire type of *PB_WT_STRING*. |
||||
|
||||
If the callback is used in a submessage, it will be called multiple times during a single call to `pb_encode`_. In this case, it must produce the same amount of data every time. If the callback is directly in the main message, it is called only once. |
||||
|
||||
.. _`pb_encode`: reference.html#pb-encode |
||||
.. _`pb_encode_tag_for_field`: reference.html#pb-encode-tag-for-field |
||||
.. _`pb_encode_tag`: reference.html#pb-encode-tag |
||||
|
||||
This callback writes out a dynamically sized string:: |
||||
|
||||
bool write_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) |
||||
{ |
||||
char *str = get_string_from_somewhere(); |
||||
if (!pb_encode_tag_for_field(stream, field)) |
||||
return false; |
||||
|
||||
return pb_encode_string(stream, (uint8_t*)str, strlen(str)); |
||||
} |
||||
|
||||
Decoding callbacks |
||||
------------------ |
||||
:: |
||||
|
||||
bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg); |
||||
|
||||
When decoding, the callback receives a length-limited substring that reads the contents of a single field. The field tag has already been read. For *string* and *bytes*, the length value has already been parsed, and is available at *stream->bytes_left*. |
||||
|
||||
The callback will be called multiple times for repeated fields. For packed fields, you can either read multiple values until the stream ends, or leave it to `pb_decode`_ to call your function over and over until all values have been read. |
||||
|
||||
.. _`pb_decode`: reference.html#pb-decode |
||||
|
||||
This callback reads multiple integers and prints them:: |
||||
|
||||
bool read_ints(pb_istream_t *stream, const pb_field_t *field, void **arg) |
||||
{ |
||||
while (stream->bytes_left) |
||||
{ |
||||
uint64_t value; |
||||
if (!pb_decode_varint(stream, &value)) |
||||
return false; |
||||
printf("%lld\n", value); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
Field description array |
||||
======================= |
||||
|
||||
For using the *pb_encode* and *pb_decode* functions, you need an array of pb_field_t constants describing the structure you wish to encode. This description is usually autogenerated from .proto file. |
||||
|
||||
For example this submessage in the Person.proto file:: |
||||
|
||||
message Person { |
||||
message PhoneNumber { |
||||
required string number = 1 [(nanopb).max_size = 40]; |
||||
optional PhoneType type = 2 [default = HOME]; |
||||
} |
||||
} |
||||
|
||||
generates this field description array for the structure *Person_PhoneNumber*:: |
||||
|
||||
const pb_field_t Person_PhoneNumber_fields[3] = { |
||||
PB_FIELD( 1, STRING , REQUIRED, STATIC, Person_PhoneNumber, number, number, 0), |
||||
PB_FIELD( 2, ENUM , OPTIONAL, STATIC, Person_PhoneNumber, type, number, &Person_PhoneNumber_type_default), |
||||
PB_LAST_FIELD |
||||
}; |
||||
|
||||
Oneof |
||||
===== |
||||
Protocol Buffers supports `oneof`_ sections. Here is an example of ``oneof`` usage:: |
||||
|
||||
message MsgType1 { |
||||
required int32 value = 1; |
||||
} |
||||
|
||||
message MsgType2 { |
||||
required bool value = 1; |
||||
} |
||||
|
||||
message MsgType3 { |
||||
required int32 value1 = 1; |
||||
required int32 value2 = 2; |
||||
} |
||||
|
||||
message MyMessage { |
||||
required uint32 uid = 1; |
||||
required uint32 pid = 2; |
||||
required uint32 utime = 3; |
||||
|
||||
oneof payload { |
||||
MsgType1 msg1 = 4; |
||||
MsgType2 msg2 = 5; |
||||
MsgType3 msg3 = 6; |
||||
} |
||||
} |
||||
|
||||
Nanopb will generate ``payload`` as a C union and add an additional field ``which_payload``:: |
||||
|
||||
typedef struct _MyMessage { |
||||
uint32_t uid; |
||||
uint32_t pid; |
||||
uint32_t utime; |
||||
pb_size_t which_payload; |
||||
union { |
||||
MsgType1 msg1; |
||||
MsgType2 msg2; |
||||
MsgType3 msg3; |
||||
} payload; |
||||
/* @@protoc_insertion_point(struct:MyMessage) */ |
||||
} MyMessage; |
||||
|
||||
``which_payload`` indicates which of the ``oneof`` fields is actually set. |
||||
The user is expected to set the filed manually using the correct field tag:: |
||||
|
||||
MyMessage msg = MyMessage_init_zero; |
||||
msg.payload.msg2.value = true; |
||||
msg.which_payload = MyMessage_msg2_tag; |
||||
|
||||
Notice that neither ``which_payload`` field nor the unused fileds in ``payload`` |
||||
will consume any space in the resulting encoded message. |
||||
|
||||
.. _`oneof`: https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#oneof_and_oneof_field |
||||
|
||||
Extension fields |
||||
================ |
||||
Protocol Buffers supports a concept of `extension fields`_, which are |
||||
additional fields to a message, but defined outside the actual message. |
||||
The definition can even be in a completely separate .proto file. |
||||
|
||||
The base message is declared as extensible by keyword *extensions* in |
||||
the .proto file:: |
||||
|
||||
message MyMessage { |
||||
.. fields .. |
||||
extensions 100 to 199; |
||||
} |
||||
|
||||
For each extensible message, *nanopb_generator.py* declares an additional |
||||
callback field called *extensions*. The field and associated datatype |
||||
*pb_extension_t* forms a linked list of handlers. When an unknown field is |
||||
encountered, the decoder calls each handler in turn until either one of them |
||||
handles the field, or the list is exhausted. |
||||
|
||||
The actual extensions are declared using the *extend* keyword in the .proto, |
||||
and are in the global namespace:: |
||||
|
||||
extend MyMessage { |
||||
optional int32 myextension = 100; |
||||
} |
||||
|
||||
For each extension, *nanopb_generator.py* creates a constant of type |
||||
*pb_extension_type_t*. To link together the base message and the extension, |
||||
you have to: |
||||
|
||||
1. Allocate storage for your field, matching the datatype in the .proto. |
||||
For example, for a *int32* field, you need a *int32_t* variable to store |
||||
the value. |
||||
2. Create a *pb_extension_t* constant, with pointers to your variable and |
||||
to the generated *pb_extension_type_t*. |
||||
3. Set the *message.extensions* pointer to point to the *pb_extension_t*. |
||||
|
||||
An example of this is available in *tests/test_encode_extensions.c* and |
||||
*tests/test_decode_extensions.c*. |
||||
|
||||
.. _`extension fields`: https://developers.google.com/protocol-buffers/docs/proto#extensions |
||||
|
||||
Message framing |
||||
=============== |
||||
Protocol Buffers does not specify a method of framing the messages for transmission. |
||||
This is something that must be provided by the library user, as there is no one-size-fits-all |
||||
solution. Typical needs for a framing format are to: |
||||
|
||||
1. Encode the message length. |
||||
2. Encode the message type. |
||||
3. Perform any synchronization and error checking that may be needed depending on application. |
||||
|
||||
For example UDP packets already fullfill all the requirements, and TCP streams typically only |
||||
need a way to identify the message length and type. Lower level interfaces such as serial ports |
||||
may need a more robust frame format, such as HDLC (high-level data link control). |
||||
|
||||
Nanopb provides a few helpers to facilitate implementing framing formats: |
||||
|
||||
1. Functions *pb_encode_delimited* and *pb_decode_delimited* prefix the message data with a varint-encoded length. |
||||
2. Union messages and oneofs are supported in order to implement top-level container messages. |
||||
3. Message IDs can be specified using the *(nanopb_msgopt).msgid* option and can then be accessed from the header. |
||||
|
||||
Return values and error handling |
||||
================================ |
||||
|
||||
Most functions in nanopb return bool: *true* means success, *false* means failure. There is also some support for error messages for debugging purposes: the error messages go in *stream->errmsg*. |
||||
|
||||
The error messages help in guessing what is the underlying cause of the error. The most common error conditions are: |
||||
|
||||
1) Running out of memory, i.e. stack overflow. |
||||
2) Invalid field descriptors (would usually mean a bug in the generator). |
||||
3) IO errors in your own stream callbacks. |
||||
4) Errors that happen in your callback functions. |
||||
5) Exceeding the max_size or bytes_left of a stream. |
||||
6) Exceeding the max_size of a string or array field |
||||
7) Invalid protocol buffers binary message. |
Before Width: | Height: | Size: 112 KiB |
@ -1,127 +0,0 @@ |
||||
============================================= |
||||
Nanopb: Protocol Buffers with small code size |
||||
============================================= |
||||
|
||||
.. include :: menu.rst |
||||
|
||||
Nanopb is an ANSI-C library for encoding and decoding messages in Google's `Protocol Buffers`__ format with minimal requirements for RAM and code space. |
||||
It is primarily suitable for 32-bit microcontrollers. |
||||
|
||||
__ https://developers.google.com/protocol-buffers/docs/reference/overview |
||||
|
||||
Overall structure |
||||
================= |
||||
|
||||
For the runtime program, you always need *pb.h* for type declarations. |
||||
Depending on whether you want to encode, decode, or both, you also need *pb_encode.h/c* or *pb_decode.h/c*. |
||||
|
||||
The high-level encoding and decoding functions take an array of *pb_field_t* structures, which describes the fields of a message structure. Usually you want these autogenerated from a *.proto* file. The tool script *nanopb_generator.py* accomplishes this. |
||||
|
||||
.. image:: generator_flow.png |
||||
|
||||
So a typical project might include these files: |
||||
|
||||
1) Nanopb runtime library: |
||||
- pb.h |
||||
- pb_common.h and pb_common.c (always needed) |
||||
- pb_decode.h and pb_decode.c (needed for decoding messages) |
||||
- pb_encode.h and pb_encode.c (needed for encoding messages) |
||||
2) Protocol description (you can have many): |
||||
- person.proto (just an example) |
||||
- person.pb.c (autogenerated, contains initializers for const arrays) |
||||
- person.pb.h (autogenerated, contains type declarations) |
||||
|
||||
Features and limitations |
||||
======================== |
||||
|
||||
**Features** |
||||
|
||||
#) Pure C runtime |
||||
#) Small code size (2–10 kB depending on processor, plus any message definitions) |
||||
#) Small ram usage (typically ~300 bytes, plus any message structs) |
||||
#) Allows specifying maximum size for strings and arrays, so that they can be allocated statically. |
||||
#) No malloc needed: everything can be allocated statically or on the stack. Optional malloc support available. |
||||
#) You can use either encoder or decoder alone to cut the code size in half. |
||||
#) Support for most protobuf features, including: all data types, nested submessages, default values, repeated and optional fields, oneofs, packed arrays, extension fields. |
||||
#) Callback mechanism for handling messages larger than can fit in available RAM. |
||||
#) Extensive set of tests. |
||||
|
||||
**Limitations** |
||||
|
||||
#) Some speed has been sacrificed for code size. |
||||
#) Encoding is focused on writing to streams. For memory buffers only it could be made more efficient. |
||||
#) The deprecated Protocol Buffers feature called "groups" is not supported. |
||||
#) Fields in the generated structs are ordered by the tag number, instead of the natural ordering in .proto file. |
||||
#) Unknown fields are not preserved when decoding and re-encoding a message. |
||||
#) Reflection (runtime introspection) is not supported. E.g. you can't request a field by giving its name in a string. |
||||
#) Numeric arrays are always encoded as packed, even if not marked as packed in .proto. |
||||
#) Cyclic references between messages are supported only in callback and malloc mode. |
||||
|
||||
Getting started |
||||
=============== |
||||
|
||||
For starters, consider this simple message:: |
||||
|
||||
message Example { |
||||
required int32 value = 1; |
||||
} |
||||
|
||||
Save this in *message.proto* and compile it:: |
||||
|
||||
user@host:~$ protoc -omessage.pb message.proto |
||||
user@host:~$ python nanopb/generator/nanopb_generator.py message.pb |
||||
|
||||
You should now have in *message.pb.h*:: |
||||
|
||||
typedef struct { |
||||
int32_t value; |
||||
} Example; |
||||
|
||||
extern const pb_field_t Example_fields[2]; |
||||
|
||||
Now in your main program do this to encode a message:: |
||||
|
||||
Example mymessage = {42}; |
||||
uint8_t buffer[10]; |
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
||||
pb_encode(&stream, Example_fields, &mymessage); |
||||
|
||||
After that, buffer will contain the encoded message. |
||||
The number of bytes in the message is stored in *stream.bytes_written*. |
||||
You can feed the message to *protoc --decode=Example message.proto* to verify its validity. |
||||
|
||||
For a complete example of the simple case, see *example/simple.c*. |
||||
For a more complex example with network interface, see the *example/network_server* subdirectory. |
||||
|
||||
Compiler requirements |
||||
===================== |
||||
Nanopb should compile with most ansi-C compatible compilers. It however |
||||
requires a few header files to be available: |
||||
|
||||
#) *string.h*, with these functions: *strlen*, *memcpy*, *memset* |
||||
#) *stdint.h*, for definitions of *int32_t* etc. |
||||
#) *stddef.h*, for definition of *size_t* |
||||
#) *stdbool.h*, for definition of *bool* |
||||
|
||||
If these header files do not come with your compiler, you can use the |
||||
file *extra/pb_syshdr.h* instead. It contains an example of how to provide |
||||
the dependencies. You may have to edit it a bit to suit your custom platform. |
||||
|
||||
To use the pb_syshdr.h, define *PB_SYSTEM_HEADER* as *"pb_syshdr.h"* (including the quotes). |
||||
Similarly, you can provide a custom include file, which should provide all the dependencies |
||||
listed above. |
||||
|
||||
Running the test cases |
||||
====================== |
||||
Extensive unittests and test cases are included under the *tests* folder. |
||||
|
||||
To build the tests, you will need the `scons`__ build system. The tests should |
||||
be runnable on most platforms. Windows and Linux builds are regularly tested. |
||||
|
||||
__ http://www.scons.org/ |
||||
|
||||
In addition to the build system, you will also need a working Google Protocol |
||||
Buffers *protoc* compiler, and the Python bindings for Protocol Buffers. On |
||||
Debian-based systems, install the following packages: *protobuf-compiler*, |
||||
*python-protobuf* and *libprotobuf-dev*. |
||||
|
Before Width: | Height: | Size: 15 KiB |
Before Width: | Height: | Size: 102 KiB |
Before Width: | Height: | Size: 854 B |
Before Width: | Height: | Size: 2.5 KiB |
@ -1,240 +0,0 @@ |
||||
/* |
||||
Author: Peter Parente |
||||
Date: 2008/01/22 |
||||
Version: 1.0 (modified) |
||||
Copyright: This stylesheet has been placed in the public domain - free to edit and use for all uses. |
||||
*/ |
||||
|
||||
body { |
||||
font: 100% sans-serif; |
||||
background: #ffffff; |
||||
color: black; |
||||
margin: 2em; |
||||
padding: 0em 2em; |
||||
} |
||||
|
||||
p.topic-title { |
||||
font-weight: bold; |
||||
} |
||||
|
||||
table.docinfo { |
||||
text-align: left; |
||||
margin: 2em 0em; |
||||
} |
||||
|
||||
a[href] { |
||||
color: #436976; |
||||
background-color: transparent; |
||||
} |
||||
|
||||
a.toc-backref { |
||||
text-decoration: none; |
||||
} |
||||
|
||||
h1 a[href] { |
||||
color: #003a6b; |
||||
text-decoration: none; |
||||
background-color: transparent; |
||||
} |
||||
|
||||
a.strong { |
||||
font-weight: bold; |
||||
} |
||||
|
||||
img { |
||||
margin: 0; |
||||
border: 0; |
||||
} |
||||
|
||||
p { |
||||
margin: 0.5em 0 1em 0; |
||||
line-height: 1.5em; |
||||
} |
||||
|
||||
p a:visited { |
||||
color: purple; |
||||
background-color: transparent; |
||||
} |
||||
|
||||
p a:active { |
||||
color: red; |
||||
background-color: transparent; |
||||
} |
||||
|
||||
a:hover { |
||||
text-decoration: none; |
||||
} |
||||
|
||||
p img { |
||||
border: 0; |
||||
margin: 0; |
||||
} |
||||
|
||||
p.rubric { |
||||
font-weight: bold; |
||||
font-style: italic; |
||||
} |
||||
|
||||
em { |
||||
font-style: normal; |
||||
font-family: monospace; |
||||
font-weight: bold; |
||||
} |
||||
|
||||
pre { |
||||
border-left: 3px double #aaa; |
||||
padding: 5px 10px; |
||||
background-color: #f6f6f6; |
||||
} |
||||
|
||||
h1.title { |
||||
color: #003a6b; |
||||
font-size: 180%; |
||||
margin-bottom: 0em; |
||||
} |
||||
|
||||
h2.subtitle { |
||||
color: #003a6b; |
||||
border-bottom: 0px; |
||||
} |
||||
|
||||
h1, h2, h3, h4, h5, h6 { |
||||
color: #555; |
||||
background-color: transparent; |
||||
margin: 0em; |
||||
padding-top: 0.5em; |
||||
} |
||||
|
||||
h1 { |
||||
font-size: 150%; |
||||
margin-bottom: 0.5em; |
||||
border-bottom: 2px solid #aaa; |
||||
} |
||||
|
||||
h2 { |
||||
font-size: 130%; |
||||
margin-bottom: 0.5em; |
||||
border-bottom: 1px solid #aaa; |
||||
} |
||||
|
||||
h3 { |
||||
font-size: 120%; |
||||
margin-bottom: 0.5em; |
||||
} |
||||
|
||||
h4 { |
||||
font-size: 110%; |
||||
font-weight: bold; |
||||
margin-bottom: 0.5em; |
||||
} |
||||
|
||||
h5 { |
||||
font-size: 105%; |
||||
font-weight: bold; |
||||
margin-bottom: 0.5em; |
||||
} |
||||
|
||||
h6 { |
||||
font-size: 100%; |
||||
font-weight: bold; |
||||
margin-bottom: 0.5em; |
||||
} |
||||
|
||||
dt { |
||||
font-style: italic; |
||||
} |
||||
|
||||
dd { |
||||
margin-bottom: 1.5em; |
||||
} |
||||
|
||||
div.admonition, div.note, div.tip, div.caution, div.important { |
||||
margin: 2em 2em; |
||||
padding: 0em 1em; |
||||
border-top: 1px solid #aaa; |
||||
border-left: 1px solid #aaa; |
||||
border-bottom: 2px solid #555; |
||||
border-right: 2px solid #555; |
||||
} |
||||
|
||||
div.important { |
||||
background: transparent url('../images/important.png') 10px 2px no-repeat; |
||||
} |
||||
|
||||
div.caution { |
||||
background: transparent url('../images/caution.png') 10px 2px no-repeat; |
||||
} |
||||
|
||||
div.note { |
||||
background: transparent url('../images/note.png') 10px 2px no-repeat; |
||||
} |
||||
|
||||
div.tip { |
||||
background: transparent url('../images/tip.png') 10px 2px no-repeat; |
||||
} |
||||
|
||||
div.admonition-example { |
||||
background: transparent url('../images/tip.png') 10px 2px no-repeat; |
||||
} |
||||
|
||||
div.admonition-critical-example { |
||||
background: transparent url('../images/important.png') 10px 2px no-repeat; |
||||
} |
||||
|
||||
p.admonition-title { |
||||
font-weight: bold; |
||||
border-bottom: 1px solid #aaa; |
||||
padding-left: 30px; |
||||
} |
||||
|
||||
table.docutils { |
||||
text-align: left; |
||||
border: 1px solid gray; |
||||
border-collapse: collapse; |
||||
margin: 1.5em 0em; |
||||
} |
||||
|
||||
table.docutils caption { |
||||
font-style: italic; |
||||
} |
||||
|
||||
table.docutils td, table.docutils th { |
||||
padding: 0.25em 0.5em; |
||||
} |
||||
|
||||
th.field-name { |
||||
text-align: right; |
||||
width: 15em; |
||||
} |
||||
|
||||
table.docutils th { |
||||
font-family: monospace; |
||||
background-color: #f6f6f6; |
||||
vertical-align: middle; |
||||
} |
||||
|
||||
table.field-list { |
||||
border: none; |
||||
} |
||||
|
||||
div.sidebar { |
||||
margin: 2em 2em 2em 0em; |
||||
padding: 0em 1em; |
||||
border-top: 1px solid #aaa; |
||||
border-left: 1px solid #aaa; |
||||
border-bottom: 2px solid #555; |
||||
border-right: 2px solid #555; |
||||
} |
||||
|
||||
p.sidebar-title { |
||||
margin-bottom: 0em; |
||||
color: #003a6b; |
||||
border-bottom: 1px solid #aaa; |
||||
font-weight: bold; |
||||
} |
||||
|
||||
p.sidebar-subtitle { |
||||
margin-top: 0em; |
||||
font-style: italic; |
||||
color: #003a6b; |
||||
} |
@ -1,13 +0,0 @@ |
||||
.. sidebar :: Documentation index |
||||
|
||||
1) `Overview`_ |
||||
2) `Concepts`_ |
||||
3) `API reference`_ |
||||
4) `Security model`_ |
||||
5) `Migration from older versions`_ |
||||
|
||||
.. _`Overview`: index.html |
||||
.. _`Concepts`: concepts.html |
||||
.. _`API reference`: reference.html |
||||
.. _`Security model`: security.html |
||||
.. _`Migration from older versions`: migration.html |
@ -1,276 +0,0 @@ |
||||
===================================== |
||||
Nanopb: Migration from older versions |
||||
===================================== |
||||
|
||||
.. include :: menu.rst |
||||
|
||||
This document details all the breaking changes that have been made to nanopb |
||||
since its initial release. For each change, the rationale and required |
||||
modifications of user applications are explained. Also any error indications |
||||
are included, in order to make it easier to find this document. |
||||
|
||||
.. contents :: |
||||
|
||||
Nanopb-0.3.5 (2016-02-13) |
||||
========================= |
||||
|
||||
Add support for platforms without uint8_t |
||||
----------------------------------------- |
||||
**Rationale:** Some platforms cannot access 8-bit sized values directly, and |
||||
do not define *uint8_t*. Nanopb previously didn't support these platforms. |
||||
|
||||
**Changes:** References to *uint8_t* were replaced with several alternatives, |
||||
one of them being a new *pb_byte_t* typedef. This in turn uses *uint_least8_t* |
||||
which means the smallest available type. |
||||
|
||||
**Required actions:** If your platform does not have a standards-compliant |
||||
*stdint.h*, it may lack the definition for *[u]int_least8_t*. This must be |
||||
added manually, example can be found in *extra/pb_syshdr.h*. |
||||
|
||||
**Error indications:** Compiler error: "unknown type name 'uint_least8_t'". |
||||
|
||||
Nanopb-0.3.2 (2015-01-24) |
||||
========================= |
||||
|
||||
Add support for OneOfs |
||||
---------------------- |
||||
**Rationale:** Previously nanopb did not support the *oneof* construct in |
||||
*.proto* files. Those fields were generated as regular *optional* fields. |
||||
|
||||
**Changes:** OneOfs are now generated as C unions. Callback fields are not |
||||
supported inside oneof and generator gives an error. |
||||
|
||||
**Required actions:** The generator option *no_unions* can be used to restore old |
||||
behaviour and to allow callbacks to be used. To use unions, one change is |
||||
needed: use *which_xxxx* field to detect which field is present, instead |
||||
of *has_xxxx*. Compare the value against *MyStruct_myfield_tag*. |
||||
|
||||
**Error indications:** Generator error: "Callback fields inside of oneof are |
||||
not supported". Compiler error: "Message" has no member named "has_xxxx". |
||||
|
||||
Nanopb-0.3.0 (2014-08-26) |
||||
========================= |
||||
|
||||
Separate field iterator logic to pb_common.c |
||||
-------------------------------------------- |
||||
**Rationale:** Originally, the field iteration logic was simple enough to be |
||||
duplicated in *pb_decode.c* and *pb_encode.c*. New field types have made the |
||||
logic more complex, which required the creation of a new file to contain the |
||||
common functionality. |
||||
|
||||
**Changes:** There is a new file, *pb_common.c*, which must be included in |
||||
builds. |
||||
|
||||
**Required actions:** Add *pb_common.c* to build rules. This file is always |
||||
required. Either *pb_decode.c* or *pb_encode.c* can still be left out if some |
||||
functionality is not needed. |
||||
|
||||
**Error indications:** Linker error: undefined reference to |
||||
*pb_field_iter_begin*, *pb_field_iter_next* or similar. |
||||
|
||||
Change data type of field counts to pb_size_t |
||||
--------------------------------------------- |
||||
**Rationale:** Often nanopb is used with small arrays, such as 255 items or |
||||
less. Using a full *size_t* field to store the array count wastes memory if |
||||
there are many arrays. There already exists parameters *PB_FIELD_16BIT* and |
||||
*PB_FIELD_32BIT* which tell nanopb what is the maximum size of arrays in use. |
||||
|
||||
**Changes:** Generator will now use *pb_size_t* for the array *_count* fields. |
||||
The size of the type will be controlled by the *PB_FIELD_16BIT* and |
||||
*PB_FIELD_32BIT* compilation time options. |
||||
|
||||
**Required actions:** Regenerate all *.pb.h* files. In some cases casts to the |
||||
*pb_size_t* type may need to be added in the user code when accessing the |
||||
*_count* fields. |
||||
|
||||
**Error indications:** Incorrect data at runtime, crashes. But note that other |
||||
changes in the same version already require regenerating the files and have |
||||
better indications of errors, so this is only an issue for development |
||||
versions. |
||||
|
||||
Renamed some macros and identifiers |
||||
----------------------------------- |
||||
**Rationale:** Some names in nanopb core were badly chosen and conflicted with |
||||
ISO C99 reserved names or lacked a prefix. While they haven't caused trouble |
||||
so far, it is reasonable to switch to non-conflicting names as these are rarely |
||||
used from user code. |
||||
|
||||
**Changes:** The following identifier names have changed: |
||||
|
||||
* Macros: |
||||
|
||||
* STATIC_ASSERT(x) -> PB_STATIC_ASSERT(x) |
||||
* UNUSED(x) -> PB_UNUSED(x) |
||||
|
||||
* Include guards: |
||||
|
||||
* _PB_filename_ -> PB_filename_INCLUDED |
||||
|
||||
* Structure forward declaration tags: |
||||
|
||||
* _pb_field_t -> pb_field_s |
||||
* _pb_bytes_array_t -> pb_bytes_array_s |
||||
* _pb_callback_t -> pb_callback_s |
||||
* _pb_extension_type_t -> pb_extension_type_s |
||||
* _pb_extension_t -> pb_extension_s |
||||
* _pb_istream_t -> pb_istream_s |
||||
* _pb_ostream_t -> pb_ostream_s |
||||
|
||||
**Required actions:** Regenerate all *.pb.c* files. If you use any of the above |
||||
identifiers in your application code, perform search-replace to the new name. |
||||
|
||||
**Error indications:** Compiler errors on lines with the macro/type names. |
||||
|
||||
Nanopb-0.2.9 (2014-08-09) |
||||
========================= |
||||
|
||||
Change semantics of generator -e option |
||||
--------------------------------------- |
||||
**Rationale:** Some compilers do not accept filenames with two dots (like |
||||
in default extension .pb.c). The *-e* option to the generator allowed changing |
||||
the extension, but not skipping the extra dot. |
||||
|
||||
**Changes:** The *-e* option in generator will no longer add the prepending |
||||
dot. The default value has been adjusted accordingly to *.pb.c* to keep the |
||||
default behaviour the same as before. |
||||
|
||||
**Required actions:** Only if using the generator -e option. Add dot before |
||||
the parameter value on the command line. |
||||
|
||||
**Error indications:** File not found when trying to compile generated files. |
||||
|
||||
Nanopb-0.2.7 (2014-04-07) |
||||
========================= |
||||
|
||||
Changed pointer-type bytes field datatype |
||||
----------------------------------------- |
||||
**Rationale:** In the initial pointer encoding support since nanopb-0.2.5, |
||||
the bytes type used a separate *pb_bytes_ptr_t* type to represent *bytes* |
||||
fields. This made it easy to encode data from a separate, user-allocated |
||||
buffer. However, it made the internal logic more complex and was inconsistent |
||||
with the other types. |
||||
|
||||
**Changes:** Dynamically allocated bytes fields now have the *pb_bytes_array_t* |
||||
type, just like statically allocated ones. |
||||
|
||||
**Required actions:** Only if using pointer-type fields with the bytes datatype. |
||||
Change any access to *msg->field.size* to *msg->field->size*. Change any |
||||
allocation to reserve space of amount *PB_BYTES_ARRAY_T_ALLOCSIZE(n)*. If the |
||||
data pointer was begin assigned from external source, implement the field using |
||||
a callback function instead. |
||||
|
||||
**Error indications:** Compiler error: unknown type name *pb_bytes_ptr_t*. |
||||
|
||||
Nanopb-0.2.4 (2013-11-07) |
||||
========================= |
||||
|
||||
Remove the NANOPB_INTERNALS compilation option |
||||
---------------------------------------------- |
||||
**Rationale:** Having the option in the headers required the functions to |
||||
be non-static, even if the option is not used. This caused errors on some |
||||
static analysis tools. |
||||
|
||||
**Changes:** The *#ifdef* and associated functions were removed from the |
||||
header. |
||||
|
||||
**Required actions:** Only if the *NANOPB_INTERNALS* option was previously |
||||
used. Actions are as listed under nanopb-0.1.3 and nanopb-0.1.6. |
||||
|
||||
**Error indications:** Compiler warning: implicit declaration of function |
||||
*pb_dec_string*, *pb_enc_string*, or similar. |
||||
|
||||
Nanopb-0.2.1 (2013-04-14) |
||||
========================= |
||||
|
||||
Callback function signature |
||||
--------------------------- |
||||
**Rationale:** Previously the auxilary data to field callbacks was passed |
||||
as *void\**. This allowed passing of any data, but made it unnecessarily |
||||
complex to return a pointer from callback. |
||||
|
||||
**Changes:** The callback function parameter was changed to *void\*\**. |
||||
|
||||
**Required actions:** You can continue using the old callback style by |
||||
defining *PB_OLD_CALLBACK_STYLE*. Recommended action is to: |
||||
|
||||
* Change the callback signatures to contain *void\*\** for decoders and |
||||
*void \* const \** for encoders. |
||||
* Change the callback function body to use *\*arg* instead of *arg*. |
||||
|
||||
**Error indications:** Compiler warning: assignment from incompatible |
||||
pointer type, when initializing *funcs.encode* or *funcs.decode*. |
||||
|
||||
Nanopb-0.2.0 (2013-03-02) |
||||
========================= |
||||
|
||||
Reformatted generated .pb.c file using macros |
||||
--------------------------------------------- |
||||
**Rationale:** Previously the generator made a list of C *pb_field_t* |
||||
initializers in the .pb.c file. This led to a need to regenerate all .pb.c |
||||
files after even small changes to the *pb_field_t* definition. |
||||
|
||||
**Changes:** Macros were added to pb.h which allow for cleaner definition |
||||
of the .pb.c contents. By changing the macro definitions, changes to the |
||||
field structure are possible without breaking compatibility with old .pb.c |
||||
files. |
||||
|
||||
**Required actions:** Regenerate all .pb.c files from the .proto sources. |
||||
|
||||
**Error indications:** Compiler warning: implicit declaration of function |
||||
*pb_delta_end*. |
||||
|
||||
Changed pb_type_t definitions |
||||
----------------------------- |
||||
**Rationale:** The *pb_type_t* was previously an enumeration type. This |
||||
caused warnings on some compilers when using bitwise operations to set flags |
||||
inside the values. |
||||
|
||||
**Changes:** The *pb_type_t* was changed to *typedef uint8_t*. The values |
||||
were changed to *#define*. Some value names were changed for consistency. |
||||
|
||||
**Required actions:** Only if you directly access the `pb_field_t` contents |
||||
in your own code, something which is not usually done. Needed changes: |
||||
|
||||
* Change *PB_HTYPE_ARRAY* to *PB_HTYPE_REPEATED*. |
||||
* Change *PB_HTYPE_CALLBACK* to *PB_ATYPE()* and *PB_ATYPE_CALLBACK*. |
||||
|
||||
**Error indications:** Compiler error: *PB_HTYPE_ARRAY* or *PB_HTYPE_CALLBACK* |
||||
undeclared. |
||||
|
||||
Nanopb-0.1.6 (2012-09-02) |
||||
========================= |
||||
|
||||
Refactored field decoder interface |
||||
---------------------------------- |
||||
**Rationale:** Similarly to field encoders in nanopb-0.1.3. |
||||
|
||||
**Changes:** New functions with names *pb_decode_\** were added. |
||||
|
||||
**Required actions:** By defining NANOPB_INTERNALS, you can still keep using |
||||
the old functions. Recommended action is to replace any calls with the newer |
||||
*pb_decode_\** equivalents. |
||||
|
||||
**Error indications:** Compiler warning: implicit declaration of function |
||||
*pb_dec_string*, *pb_dec_varint*, *pb_dec_submessage* or similar. |
||||
|
||||
Nanopb-0.1.3 (2012-06-12) |
||||
========================= |
||||
|
||||
Refactored field encoder interface |
||||
---------------------------------- |
||||
**Rationale:** The old *pb_enc_\** functions were designed mostly for the |
||||
internal use by the core. Because they are internally accessed through |
||||
function pointers, their signatures had to be common. This led to a confusing |
||||
interface for external users. |
||||
|
||||
**Changes:** New functions with names *pb_encode_\** were added. These have |
||||
easier to use interfaces. The old functions are now only thin wrappers for |
||||
the new interface. |
||||
|
||||
**Required actions:** By defining NANOPB_INTERNALS, you can still keep using |
||||
the old functions. Recommended action is to replace any calls with the newer |
||||
*pb_encode_\** equivalents. |
||||
|
||||
**Error indications:** Compiler warning: implicit declaration of function |
||||
*pb_enc_string*, *pb_enc_varint, *pb_enc_submessage* or similar. |
||||
|
@ -1,770 +0,0 @@ |
||||
===================== |
||||
Nanopb: API reference |
||||
===================== |
||||
|
||||
.. include :: menu.rst |
||||
|
||||
.. contents :: |
||||
|
||||
|
||||
|
||||
|
||||
Compilation options |
||||
=================== |
||||
The following options can be specified in one of two ways: |
||||
|
||||
1. Using the -D switch on the C compiler command line. |
||||
2. By #defining them at the top of pb.h. |
||||
|
||||
You must have the same settings for the nanopb library and all code that |
||||
includes pb.h. |
||||
|
||||
============================ ================================================ |
||||
PB_NO_PACKED_STRUCTS Disable packed structs. Increases RAM usage but |
||||
is necessary on some platforms that do not |
||||
support unaligned memory access. |
||||
PB_ENABLE_MALLOC Set this to enable dynamic allocation support |
||||
in the decoder. |
||||
PB_MAX_REQUIRED_FIELDS Maximum number of required fields to check for |
||||
presence. Default value is 64. Increases stack |
||||
usage 1 byte per every 8 fields. Compiler |
||||
warning will tell if you need this. |
||||
PB_FIELD_16BIT Add support for tag numbers > 255 and fields |
||||
larger than 255 bytes or 255 array entries. |
||||
Increases code size 3 bytes per each field. |
||||
Compiler error will tell if you need this. |
||||
PB_FIELD_32BIT Add support for tag numbers > 65535 and fields |
||||
larger than 65535 bytes or 65535 array entries. |
||||
Increases code size 9 bytes per each field. |
||||
Compiler error will tell if you need this. |
||||
PB_NO_ERRMSG Disables the support for error messages; only |
||||
error information is the true/false return |
||||
value. Decreases the code size by a few hundred |
||||
bytes. |
||||
PB_BUFFER_ONLY Disables the support for custom streams. Only |
||||
supports encoding and decoding with memory |
||||
buffers. Speeds up execution and decreases code |
||||
size slightly. |
||||
PB_OLD_CALLBACK_STYLE Use the old function signature (void\* instead |
||||
of void\*\*) for callback fields. This was the |
||||
default until nanopb-0.2.1. |
||||
PB_SYSTEM_HEADER Replace the standard header files with a single |
||||
header file. It should define all the required |
||||
functions and typedefs listed on the |
||||
`overview page`_. Value must include quotes, |
||||
for example *#define PB_SYSTEM_HEADER "foo.h"*. |
||||
============================ ================================================ |
||||
|
||||
The PB_MAX_REQUIRED_FIELDS, PB_FIELD_16BIT and PB_FIELD_32BIT settings allow |
||||
raising some datatype limits to suit larger messages. Their need is recognized |
||||
automatically by C-preprocessor #if-directives in the generated .pb.h files. |
||||
The default setting is to use the smallest datatypes (least resources used). |
||||
|
||||
.. _`overview page`: index.html#compiler-requirements |
||||
|
||||
|
||||
Proto file options |
||||
================== |
||||
The generator behaviour can be adjusted using these options, defined in the |
||||
'nanopb.proto' file in the generator folder: |
||||
|
||||
============================ ================================================ |
||||
max_size Allocated size for *bytes* and *string* fields. |
||||
max_count Allocated number of entries in arrays |
||||
(*repeated* fields). |
||||
int_size Override the integer type of a field. |
||||
(To use e.g. uint8_t to save RAM.) |
||||
type Type of the generated field. Default value |
||||
is *FT_DEFAULT*, which selects automatically. |
||||
You can use *FT_CALLBACK*, *FT_POINTER*, |
||||
*FT_STATIC*, *FT_IGNORE*, or *FT_INLINE* to |
||||
force a callback field, a dynamically |
||||
allocated field, a static field, to |
||||
completely ignore the field or to |
||||
generate an inline bytes field. |
||||
long_names Prefix the enum name to the enum value in |
||||
definitions, i.e. *EnumName_EnumValue*. Enabled |
||||
by default. |
||||
packed_struct Make the generated structures packed. |
||||
NOTE: This cannot be used on CPUs that break |
||||
on unaligned accesses to variables. |
||||
skip_message Skip the whole message from generation. |
||||
no_unions Generate 'oneof' fields as optional fields |
||||
instead of C unions. |
||||
msgid Specifies a unique id for this message type. |
||||
Can be used by user code as an identifier. |
||||
anonymous_oneof Generate 'oneof' fields as anonymous unions. |
||||
============================ ================================================ |
||||
|
||||
These options can be defined for the .proto files before they are converted |
||||
using the nanopb-generatory.py. There are three ways to define the options: |
||||
|
||||
1. Using a separate .options file. |
||||
This is the preferred way as of nanopb-0.2.1, because it has the best |
||||
compatibility with other protobuf libraries. |
||||
2. Defining the options on the command line of nanopb_generator.py. |
||||
This only makes sense for settings that apply to a whole file. |
||||
3. Defining the options in the .proto file using the nanopb extensions. |
||||
This is the way used in nanopb-0.1, and will remain supported in the |
||||
future. It however sometimes causes trouble when using the .proto file |
||||
with other protobuf libraries. |
||||
|
||||
The effect of the options is the same no matter how they are given. The most |
||||
common purpose is to define maximum size for string fields in order to |
||||
statically allocate them. |
||||
|
||||
Defining the options in a .options file |
||||
--------------------------------------- |
||||
The preferred way to define options is to have a separate file |
||||
'myproto.options' in the same directory as the 'myproto.proto'. :: |
||||
|
||||
# myproto.proto |
||||
message MyMessage { |
||||
required string name = 1; |
||||
repeated int32 ids = 4; |
||||
} |
||||
|
||||
:: |
||||
|
||||
# myproto.options |
||||
MyMessage.name max_size:40 |
||||
MyMessage.ids max_count:5 |
||||
|
||||
The generator will automatically search for this file and read the |
||||
options from it. The file format is as follows: |
||||
|
||||
* Lines starting with '#' or '//' are regarded as comments. |
||||
* Blank lines are ignored. |
||||
* All other lines should start with a field name pattern, followed by one or |
||||
more options. For example: *"MyMessage.myfield max_size:5 max_count:10"*. |
||||
* The field name pattern is matched against a string of form *'Message.field'*. |
||||
For nested messages, the string is *'Message.SubMessage.field'*. |
||||
* The field name pattern may use the notation recognized by Python fnmatch(): |
||||
|
||||
- *\** matches any part of string, like 'Message.\*' for all fields |
||||
- *\?* matches any single character |
||||
- *[seq]* matches any of characters 's', 'e' and 'q' |
||||
- *[!seq]* matches any other character |
||||
|
||||
* The options are written as *'option_name:option_value'* and several options |
||||
can be defined on same line, separated by whitespace. |
||||
* Options defined later in the file override the ones specified earlier, so |
||||
it makes sense to define wildcard options first in the file and more specific |
||||
ones later. |
||||
|
||||
If preferred, the name of the options file can be set using the command line |
||||
switch *-f* to nanopb_generator.py. |
||||
|
||||
Defining the options on command line |
||||
------------------------------------ |
||||
The nanopb_generator.py has a simple command line option *-s OPTION:VALUE*. |
||||
The setting applies to the whole file that is being processed. |
||||
|
||||
Defining the options in the .proto file |
||||
--------------------------------------- |
||||
The .proto file format allows defining custom options for the fields. |
||||
The nanopb library comes with *nanopb.proto* which does exactly that, allowing |
||||
you do define the options directly in the .proto file:: |
||||
|
||||
import "nanopb.proto"; |
||||
|
||||
message MyMessage { |
||||
required string name = 1 [(nanopb).max_size = 40]; |
||||
repeated int32 ids = 4 [(nanopb).max_count = 5]; |
||||
} |
||||
|
||||
A small complication is that you have to set the include path of protoc so that |
||||
nanopb.proto can be found. This file, in turn, requires the file |
||||
*google/protobuf/descriptor.proto*. This is usually installed under |
||||
*/usr/include*. Therefore, to compile a .proto file which uses options, use a |
||||
protoc command similar to:: |
||||
|
||||
protoc -I/usr/include -Inanopb/generator -I. -omessage.pb message.proto |
||||
|
||||
The options can be defined in file, message and field scopes:: |
||||
|
||||
option (nanopb_fileopt).max_size = 20; // File scope |
||||
message Message |
||||
{ |
||||
option (nanopb_msgopt).max_size = 30; // Message scope |
||||
required string fieldsize = 1 [(nanopb).max_size = 40]; // Field scope |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
pb.h |
||||
==== |
||||
|
||||
pb_byte_t |
||||
--------- |
||||
Type used for storing byte-sized data, such as raw binary input and bytes-type fields. :: |
||||
|
||||
typedef uint_least8_t pb_byte_t; |
||||
|
||||
For most platforms this is equivalent to `uint8_t`. Some platforms however do not support |
||||
8-bit variables, and on those platforms 16 or 32 bits need to be used for each byte. |
||||
|
||||
pb_type_t |
||||
--------- |
||||
Type used to store the type of each field, to control the encoder/decoder behaviour. :: |
||||
|
||||
typedef uint_least8_t pb_type_t; |
||||
|
||||
The low-order nibble of the enumeration values defines the function that can be used for encoding and decoding the field data: |
||||
|
||||
=========================== ===== ================================================ |
||||
LTYPE identifier Value Storage format |
||||
=========================== ===== ================================================ |
||||
PB_LTYPE_VARINT 0x00 Integer. |
||||
PB_LTYPE_UVARINT 0x01 Unsigned integer. |
||||
PB_LTYPE_SVARINT 0x02 Integer, zigzag encoded. |
||||
PB_LTYPE_FIXED32 0x03 32-bit integer or floating point. |
||||
PB_LTYPE_FIXED64 0x04 64-bit integer or floating point. |
||||
PB_LTYPE_BYTES 0x05 Structure with *size_t* field and byte array. |
||||
PB_LTYPE_STRING 0x06 Null-terminated string. |
||||
PB_LTYPE_SUBMESSAGE 0x07 Submessage structure. |
||||
PB_LTYPE_EXTENSION 0x08 Point to *pb_extension_t*. |
||||
PB_LTYPE_FIXED_LENGTH_BYTES 0x09 Inline *pb_byte_t* array of fixed size. |
||||
=========================== ===== ================================================ |
||||
|
||||
The bits 4-5 define whether the field is required, optional or repeated: |
||||
|
||||
==================== ===== ================================================ |
||||
HTYPE identifier Value Field handling |
||||
==================== ===== ================================================ |
||||
PB_HTYPE_REQUIRED 0x00 Verify that field exists in decoded message. |
||||
PB_HTYPE_OPTIONAL 0x10 Use separate *has_<field>* boolean to specify |
||||
whether the field is present. |
||||
(Unless it is a callback) |
||||
PB_HTYPE_REPEATED 0x20 A repeated field with preallocated array. |
||||
Separate *<field>_count* for number of items. |
||||
(Unless it is a callback) |
||||
==================== ===== ================================================ |
||||
|
||||
The bits 6-7 define the how the storage for the field is allocated: |
||||
|
||||
==================== ===== ================================================ |
||||
ATYPE identifier Value Allocation method |
||||
==================== ===== ================================================ |
||||
PB_ATYPE_STATIC 0x00 Statically allocated storage in the structure. |
||||
PB_ATYPE_CALLBACK 0x40 A field with dynamic storage size. Struct field |
||||
actually contains a pointer to a callback |
||||
function. |
||||
==================== ===== ================================================ |
||||
|
||||
|
||||
pb_field_t |
||||
---------- |
||||
Describes a single structure field with memory position in relation to others. The descriptions are usually autogenerated. :: |
||||
|
||||
typedef struct pb_field_s pb_field_t; |
||||
struct pb_field_s { |
||||
pb_size_t tag; |
||||
pb_type_t type; |
||||
pb_size_t data_offset; |
||||
pb_ssize_t size_offset; |
||||
pb_size_t data_size; |
||||
pb_size_t array_size; |
||||
const void *ptr; |
||||
} pb_packed; |
||||
|
||||
:tag: Tag number of the field or 0 to terminate a list of fields. |
||||
:type: LTYPE, HTYPE and ATYPE of the field. |
||||
:data_offset: Offset of field data, relative to the end of the previous field. |
||||
:size_offset: Offset of *bool* flag for optional fields or *size_t* count for arrays, relative to field data. |
||||
: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. |
||||
:array_size: Maximum number of entries in an array, if it is an array type. |
||||
:ptr: Pointer to default value for optional fields, or to submessage description for PB_LTYPE_SUBMESSAGE. |
||||
|
||||
The *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*. |
||||
|
||||
pb_bytes_array_t |
||||
---------------- |
||||
An byte array with a field for storing the length:: |
||||
|
||||
typedef struct { |
||||
pb_size_t size; |
||||
pb_byte_t bytes[1]; |
||||
} pb_bytes_array_t; |
||||
|
||||
In an actual array, the length of *bytes* may be different. |
||||
|
||||
pb_callback_t |
||||
------------- |
||||
Part of a message structure, for fields with type PB_HTYPE_CALLBACK:: |
||||
|
||||
typedef struct _pb_callback_t pb_callback_t; |
||||
struct _pb_callback_t { |
||||
union { |
||||
bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg); |
||||
bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg); |
||||
} funcs; |
||||
|
||||
void *arg; |
||||
}; |
||||
|
||||
A pointer to the *arg* is passed to the callback when calling. It can be used to store any information that the callback might need. |
||||
|
||||
Previously 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*. |
||||
|
||||
When 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. |
||||
|
||||
pb_wire_type_t |
||||
-------------- |
||||
Protocol Buffers wire types. These are used with `pb_encode_tag`_. :: |
||||
|
||||
typedef enum { |
||||
PB_WT_VARINT = 0, |
||||
PB_WT_64BIT = 1, |
||||
PB_WT_STRING = 2, |
||||
PB_WT_32BIT = 5 |
||||
} pb_wire_type_t; |
||||
|
||||
pb_extension_type_t |
||||
------------------- |
||||
Defines the handler functions and auxiliary data for a field that extends |
||||
another message. Usually autogenerated by *nanopb_generator.py*:: |
||||
|
||||
typedef struct { |
||||
bool (*decode)(pb_istream_t *stream, pb_extension_t *extension, |
||||
uint32_t tag, pb_wire_type_t wire_type); |
||||
bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension); |
||||
const void *arg; |
||||
} pb_extension_type_t; |
||||
|
||||
In the normal case, the function pointers are *NULL* and the decoder and |
||||
encoder use their internal implementations. The internal implementations |
||||
assume that *arg* points to a *pb_field_t* that describes the field in question. |
||||
|
||||
To implement custom processing of unknown fields, you can provide pointers |
||||
to your own functions. Their functionality is mostly the same as for normal |
||||
callback fields, except that they get called for any unknown field when decoding. |
||||
|
||||
pb_extension_t |
||||
-------------- |
||||
Ties together the extension field type and the storage for the field value:: |
||||
|
||||
typedef struct { |
||||
const pb_extension_type_t *type; |
||||
void *dest; |
||||
pb_extension_t *next; |
||||
bool found; |
||||
} pb_extension_t; |
||||
|
||||
:type: Pointer to the structure that defines the callback functions. |
||||
:dest: Pointer to the variable that stores the field value |
||||
(as used by the default extension callback functions.) |
||||
:next: Pointer to the next extension handler, or *NULL*. |
||||
:found: Decoder sets this to true if the extension was found. |
||||
|
||||
PB_GET_ERROR |
||||
------------ |
||||
Get the current error message from a stream, or a placeholder string if |
||||
there is no error message:: |
||||
|
||||
#define PB_GET_ERROR(stream) (string expression) |
||||
|
||||
This should be used for printing errors, for example:: |
||||
|
||||
if (!pb_decode(...)) |
||||
{ |
||||
printf("Decode failed: %s\n", PB_GET_ERROR(stream)); |
||||
} |
||||
|
||||
The macro only returns pointers to constant strings (in code memory), |
||||
so that there is no need to release the returned pointer. |
||||
|
||||
PB_RETURN_ERROR |
||||
--------------- |
||||
Set the error message and return false:: |
||||
|
||||
#define PB_RETURN_ERROR(stream,msg) (sets error and returns false) |
||||
|
||||
This should be used to handle error conditions inside nanopb functions |
||||
and user callback functions:: |
||||
|
||||
if (error_condition) |
||||
{ |
||||
PB_RETURN_ERROR(stream, "something went wrong"); |
||||
} |
||||
|
||||
The *msg* parameter must be a constant string. |
||||
|
||||
|
||||
|
||||
pb_encode.h |
||||
=========== |
||||
|
||||
pb_ostream_from_buffer |
||||
---------------------- |
||||
Constructs 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. :: |
||||
|
||||
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize); |
||||
|
||||
:buf: Memory buffer to write into. |
||||
:bufsize: Maximum number of bytes to write. |
||||
:returns: An output stream. |
||||
|
||||
After writing, you can check *stream.bytes_written* to find out how much valid data there is in the buffer. |
||||
|
||||
pb_write |
||||
-------- |
||||
Writes data to an output stream. Always use this function, instead of trying to call stream callback manually. :: |
||||
|
||||
bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count); |
||||
|
||||
:stream: Output stream to write to. |
||||
:buf: Pointer to buffer with the data to be written. |
||||
:count: Number of bytes to write. |
||||
:returns: True on success, false if maximum length is exceeded or an IO error happens. |
||||
|
||||
If 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. |
||||
|
||||
pb_encode |
||||
--------- |
||||
Encodes the contents of a structure as a protocol buffers message and writes it to output stream. :: |
||||
|
||||
bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
||||
|
||||
:stream: Output stream to write to. |
||||
:fields: A field description array, usually autogenerated. |
||||
:src_struct: Pointer to the data that will be serialized. |
||||
:returns: True on success, false on IO error, on detectable errors in field description, or if a field encoder returns false. |
||||
|
||||
Normally 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. |
||||
|
||||
pb_encode_delimited |
||||
------------------- |
||||
Calculates the length of the message, encodes it as varint and then encodes the message. :: |
||||
|
||||
bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
||||
|
||||
(parameters are the same as for `pb_encode`_.) |
||||
|
||||
A common way to indicate the message length in Protocol Buffers is to prefix it with a varint. |
||||
This function does this, and it is compatible with *parseDelimitedFrom* in Google's protobuf library. |
||||
|
||||
.. sidebar:: Encoding fields manually |
||||
|
||||
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. |
||||
|
||||
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. |
||||
|
||||
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. |
||||
|
||||
pb_get_encoded_size |
||||
------------------- |
||||
Calculates the length of the encoded message. :: |
||||
|
||||
bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct); |
||||
|
||||
:size: Calculated size of the encoded message. |
||||
:fields: A field description array, usually autogenerated. |
||||
:src_struct: Pointer to the data that will be serialized. |
||||
:returns: True on success, false on detectable errors in field description or if a field encoder returns false. |
||||
|
||||
pb_encode_tag |
||||
------------- |
||||
Starts a field in the Protocol Buffers binary format: encodes the field number and the wire type of the data. :: |
||||
|
||||
bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number); |
||||
|
||||
:stream: Output stream to write to. 1-5 bytes will be written. |
||||
:wiretype: PB_WT_VARINT, PB_WT_64BIT, PB_WT_STRING or PB_WT_32BIT |
||||
:field_number: Identifier for the field, defined in the .proto file. You can get it from field->tag. |
||||
:returns: True on success, false on IO error. |
||||
|
||||
pb_encode_tag_for_field |
||||
----------------------- |
||||
Same as `pb_encode_tag`_, except takes the parameters from a *pb_field_t* structure. :: |
||||
|
||||
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field); |
||||
|
||||
:stream: Output stream to write to. 1-5 bytes will be written. |
||||
:field: Field description structure. Usually autogenerated. |
||||
:returns: True on success, false on IO error or unknown field type. |
||||
|
||||
This 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. |
||||
|
||||
Wire type mapping is as follows: |
||||
|
||||
============================================= ============ |
||||
LTYPEs Wire type |
||||
============================================= ============ |
||||
VARINT, UVARINT, SVARINT PB_WT_VARINT |
||||
FIXED64 PB_WT_64BIT |
||||
STRING, BYTES, SUBMESSAGE, FIXED_LENGTH_BYTES PB_WT_STRING |
||||
FIXED32 PB_WT_32BIT |
||||
============================================= ============ |
||||
|
||||
pb_encode_varint |
||||
---------------- |
||||
Encodes a signed or unsigned integer in the varint_ format. Works for fields of type `bool`, `enum`, `int32`, `int64`, `uint32` and `uint64`:: |
||||
|
||||
bool pb_encode_varint(pb_ostream_t *stream, uint64_t value); |
||||
|
||||
:stream: Output stream to write to. 1-10 bytes will be written. |
||||
:value: Value to encode. Just cast e.g. int32_t directly to uint64_t. |
||||
:returns: True on success, false on IO error. |
||||
|
||||
.. _varint: http://code.google.com/apis/protocolbuffers/docs/encoding.html#varints |
||||
|
||||
pb_encode_svarint |
||||
----------------- |
||||
Encodes a signed integer in the 'zig-zagged' format. Works for fields of type `sint32` and `sint64`:: |
||||
|
||||
bool pb_encode_svarint(pb_ostream_t *stream, int64_t value); |
||||
|
||||
(parameters are the same as for `pb_encode_varint`_ |
||||
|
||||
pb_encode_string |
||||
---------------- |
||||
Writes the length of a string as varint and then contents of the string. Works for fields of type `bytes` and `string`:: |
||||
|
||||
bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size); |
||||
|
||||
:stream: Output stream to write to. |
||||
:buffer: Pointer to string data. |
||||
:size: Number of bytes in the string. Pass `strlen(s)` for strings. |
||||
:returns: True on success, false on IO error. |
||||
|
||||
pb_encode_fixed32 |
||||
----------------- |
||||
Writes 4 bytes to stream and swaps bytes on big-endian architectures. Works for fields of type `fixed32`, `sfixed32` and `float`:: |
||||
|
||||
bool pb_encode_fixed32(pb_ostream_t *stream, const void *value); |
||||
|
||||
:stream: Output stream to write to. |
||||
:value: Pointer to a 4-bytes large C variable, for example `uint32_t foo;`. |
||||
:returns: True on success, false on IO error. |
||||
|
||||
pb_encode_fixed64 |
||||
----------------- |
||||
Writes 8 bytes to stream and swaps bytes on big-endian architecture. Works for fields of type `fixed64`, `sfixed64` and `double`:: |
||||
|
||||
bool pb_encode_fixed64(pb_ostream_t *stream, const void *value); |
||||
|
||||
:stream: Output stream to write to. |
||||
:value: Pointer to a 8-bytes large C variable, for example `uint64_t foo;`. |
||||
:returns: True on success, false on IO error. |
||||
|
||||
pb_encode_submessage |
||||
-------------------- |
||||
Encodes a submessage field, including the size header for it. Works for fields of any message type:: |
||||
|
||||
bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
||||
|
||||
:stream: Output stream to write to. |
||||
:fields: Pointer to the autogenerated field description array for the submessage type, e.g. `MyMessage_fields`. |
||||
:src: Pointer to the structure where submessage data is. |
||||
:returns: True on success, false on IO errors, pb_encode errors or if submessage size changes between calls. |
||||
|
||||
In 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. |
||||
|
||||
If 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. |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
pb_decode.h |
||||
=========== |
||||
|
||||
pb_istream_from_buffer |
||||
---------------------- |
||||
Helper function for creating an input stream that reads data from a memory buffer. :: |
||||
|
||||
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize); |
||||
|
||||
:buf: Pointer to byte array to read from. |
||||
:bufsize: Size of the byte array. |
||||
:returns: An input stream ready to use. |
||||
|
||||
pb_read |
||||
------- |
||||
Read data from input stream. Always use this function, don't try to call the stream callback directly. :: |
||||
|
||||
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count); |
||||
|
||||
:stream: Input stream to read from. |
||||
:buf: Buffer to store the data to, or NULL to just read data without storing it anywhere. |
||||
:count: Number of bytes to read. |
||||
:returns: True on success, false if *stream->bytes_left* is less than *count* or if an IO error occurs. |
||||
|
||||
End of file is signalled by *stream->bytes_left* being zero after pb_read returns false. |
||||
|
||||
pb_decode |
||||
--------- |
||||
Read and decode all fields of a structure. Reads until EOF on input stream. :: |
||||
|
||||
bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
||||
|
||||
:stream: Input stream to read from. |
||||
:fields: A field description array. Usually autogenerated. |
||||
:dest_struct: Pointer to structure where data will be stored. |
||||
: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. |
||||
|
||||
In 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. |
||||
|
||||
In 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. |
||||
|
||||
For optional fields, this function applies the default value and sets *has_<field>* to false if the field is not present. |
||||
|
||||
If *PB_ENABLE_MALLOC* is defined, this function may allocate storage for any pointer type fields. |
||||
In this case, you have to call `pb_release`_ to release the memory after you are done with the message. |
||||
On error return `pb_decode` will release the memory itself. |
||||
|
||||
pb_decode_noinit |
||||
---------------- |
||||
Same as `pb_decode`_, except does not apply the default values to fields. :: |
||||
|
||||
bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
||||
|
||||
(parameters are the same as for `pb_decode`_.) |
||||
|
||||
The 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. |
||||
|
||||
In addition to decoding a single message, this function can be used to merge two messages, so that |
||||
values from previous message will remain if the new message does not contain a field. |
||||
|
||||
This function *will not* release the message even on error return. If you use *PB_ENABLE_MALLOC*, |
||||
you will need to call `pb_release`_ yourself. |
||||
|
||||
pb_decode_delimited |
||||
------------------- |
||||
Same as `pb_decode`_, except that it first reads a varint with the length of the message. :: |
||||
|
||||
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
||||
|
||||
(parameters are the same as for `pb_decode`_.) |
||||
|
||||
A common method to indicate message size in Protocol Buffers is to prefix it with a varint. |
||||
This function is compatible with *writeDelimitedTo* in the Google's Protocol Buffers library. |
||||
|
||||
pb_release |
||||
---------- |
||||
Releases any dynamically allocated fields:: |
||||
|
||||
void pb_release(const pb_field_t fields[], void *dest_struct); |
||||
|
||||
:fields: A field description array. Usually autogenerated. |
||||
:dest_struct: Pointer to structure where data is stored. If NULL, function does nothing. |
||||
|
||||
This function is only available if *PB_ENABLE_MALLOC* is defined. It will release any |
||||
pointer type fields in the structure and set the pointers to NULL. |
||||
|
||||
pb_decode_tag |
||||
------------- |
||||
Decode the tag that comes before field in the protobuf encoding:: |
||||
|
||||
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); |
||||
|
||||
:stream: Input stream to read from. |
||||
:wire_type: Pointer to variable where to store the wire type of the field. |
||||
:tag: Pointer to variable where to store the tag of the field. |
||||
:eof: Pointer to variable where to store end-of-file status. |
||||
:returns: True on success, false on error or EOF. |
||||
|
||||
When the message (stream) ends, this function will return false and set *eof* to true. On other |
||||
errors, *eof* will be set to false. |
||||
|
||||
pb_skip_field |
||||
------------- |
||||
Remove the data for a field from the stream, without actually decoding it:: |
||||
|
||||
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); |
||||
|
||||
:stream: Input stream to read from. |
||||
:wire_type: Type of field to skip. |
||||
:returns: True on success, false on IO error. |
||||
|
||||
.. sidebar:: Decoding fields manually |
||||
|
||||
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. |
||||
|
||||
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. |
||||
|
||||
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`_. |
||||
|
||||
Finally, for decoding submessages in a callback, simply use `pb_decode`_ and pass it the *SubMessage_fields* descriptor array. |
||||
|
||||
pb_decode_varint |
||||
---------------- |
||||
Read and decode a varint_ encoded integer. :: |
||||
|
||||
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); |
||||
|
||||
:stream: Input stream to read from. 1-10 bytes will be read. |
||||
:dest: Storage for the decoded integer. Value is undefined on error. |
||||
:returns: True on success, false if value exceeds uint64_t range or an IO error happens. |
||||
|
||||
pb_decode_svarint |
||||
----------------- |
||||
Similar to `pb_decode_varint`_, except that it performs zigzag-decoding on the value. This corresponds to the Protocol Buffers *sint32* and *sint64* datatypes. :: |
||||
|
||||
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); |
||||
|
||||
(parameters are the same as `pb_decode_varint`_) |
||||
|
||||
pb_decode_fixed32 |
||||
----------------- |
||||
Decode a *fixed32*, *sfixed32* or *float* value. :: |
||||
|
||||
bool pb_decode_fixed32(pb_istream_t *stream, void *dest); |
||||
|
||||
:stream: Input stream to read from. 4 bytes will be read. |
||||
:dest: Pointer to destination *int32_t*, *uint32_t* or *float*. |
||||
:returns: True on success, false on IO errors. |
||||
|
||||
This function reads 4 bytes from the input stream. |
||||
On big endian architectures, it then reverses the order of the bytes. |
||||
Finally, it writes the bytes to *dest*. |
||||
|
||||
pb_decode_fixed64 |
||||
----------------- |
||||
Decode a *fixed64*, *sfixed64* or *double* value. :: |
||||
|
||||
bool pb_decode_fixed64(pb_istream_t *stream, void *dest); |
||||
|
||||
:stream: Input stream to read from. 8 bytes will be read. |
||||
:dest: Pointer to destination *int64_t*, *uint64_t* or *double*. |
||||
:returns: True on success, false on IO errors. |
||||
|
||||
Same as `pb_decode_fixed32`_, except this reads 8 bytes. |
||||
|
||||
pb_make_string_substream |
||||
------------------------ |
||||
Decode the length for a field with wire type *PB_WT_STRING* and create a substream for reading the data. :: |
||||
|
||||
bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
||||
|
||||
:stream: Original input stream to read the length and data from. |
||||
:substream: New substream that has limited length. Filled in by the function. |
||||
:returns: True on success, false if reading the length fails. |
||||
|
||||
This 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. |
||||
|
||||
pb_close_string_substream |
||||
------------------------- |
||||
Close the substream created with `pb_make_string_substream`_. :: |
||||
|
||||
void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
||||
|
||||
:stream: Original input stream to read the length and data from. |
||||
:substream: Substream to close |
||||
|
||||
This function copies back the state from the substream to the parent stream. |
||||
It must be called after done with the substream. |
@ -1,84 +0,0 @@ |
||||
====================== |
||||
Nanopb: Security model |
||||
====================== |
||||
|
||||
.. include :: menu.rst |
||||
|
||||
.. contents :: |
||||
|
||||
|
||||
|
||||
Importance of security in a Protocol Buffers library |
||||
==================================================== |
||||
In the context of protocol buffers, security comes into play when decoding |
||||
untrusted data. Naturally, if the attacker can modify the contents of a |
||||
protocol buffers message, he can feed the application any values possible. |
||||
Therefore the application itself must be prepared to receive untrusted values. |
||||
|
||||
Where nanopb plays a part is preventing the attacker from running arbitrary |
||||
code on the target system. Mostly this means that there must not be any |
||||
possibility to cause buffer overruns, memory corruption or invalid pointers |
||||
by the means of crafting a malicious message. |
||||
|
||||
Division of trusted and untrusted data |
||||
====================================== |
||||
The following data is regarded as **trusted**. It must be under the control of |
||||
the application writer. Malicious data in these structures could cause |
||||
security issues, such as execution of arbitrary code: |
||||
|
||||
1. Callback, pointer and extension fields in message structures given to |
||||
pb_encode() and pb_decode(). These fields are memory pointers, and are |
||||
generated depending on the message definition in the .proto file. |
||||
2. The automatically generated field definitions, i.e. *pb_field_t* lists. |
||||
3. Contents of the *pb_istream_t* and *pb_ostream_t* structures (this does not |
||||
mean the contents of the stream itself, just the stream definition). |
||||
|
||||
The following data is regarded as **untrusted**. Invalid/malicious data in |
||||
these will cause "garbage in, garbage out" behaviour. It will not cause |
||||
buffer overflows, information disclosure or other security problems: |
||||
|
||||
1. All data read from *pb_istream_t*. |
||||
2. All fields in message structures, except: |
||||
|
||||
- callbacks (*pb_callback_t* structures) |
||||
- pointer fields (malloc support) and *_count* fields for pointers |
||||
- extensions (*pb_extension_t* structures) |
||||
|
||||
Invariants |
||||
========== |
||||
The following invariants are maintained during operation, even if the |
||||
untrusted data has been maliciously crafted: |
||||
|
||||
1. Nanopb will never read more than *bytes_left* bytes from *pb_istream_t*. |
||||
2. Nanopb will never write more than *max_size* bytes to *pb_ostream_t*. |
||||
3. Nanopb will never access memory out of bounds of the message structure. |
||||
4. After pb_decode() returns successfully, the message structure will be |
||||
internally consistent: |
||||
|
||||
- The *count* fields of arrays will not exceed the array size. |
||||
- The *size* field of bytes will not exceed the allocated size. |
||||
- All string fields will have null terminator. |
||||
|
||||
5. After pb_encode() returns successfully, the resulting message is a valid |
||||
protocol buffers message. (Except if user-defined callbacks write incorrect |
||||
data.) |
||||
|
||||
Further considerations |
||||
====================== |
||||
Even if the nanopb library is free of any security issues, there are still |
||||
several possible attack vectors that the application author must consider. |
||||
The following list is not comprehensive: |
||||
|
||||
1. Stack usage may depend on the contents of the message. The message |
||||
definition places an upper bound on how much stack will be used. Tests |
||||
should be run with all fields present, to record the maximum possible |
||||
stack usage. |
||||
2. Callbacks can do anything. The code for the callbacks must be carefully |
||||
checked if they are used with untrusted data. |
||||
3. If using stream input, a maximum size should be set in *pb_istream_t* to |
||||
stop a denial of service attack from using an infinite message. |
||||
4. If using network sockets as streams, a timeout should be set to stop |
||||
denial of service attacks. |
||||
5. If using *malloc()* support, some method of limiting memory use should be |
||||
employed. This can be done by defining custom *pb_realloc()* function. |
||||
Nanopb will properly detect and handle failed memory allocations. |
@ -1,16 +0,0 @@ |
||||
cmake_minimum_required(VERSION 2.8) |
||||
project(NANOPB_CMAKE_SIMPLE C) |
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../extra) |
||||
find_package(Nanopb REQUIRED) |
||||
include_directories(${NANOPB_INCLUDE_DIRS}) |
||||
|
||||
nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS simple.proto) |
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR}) |
||||
#add_custom_target(generate_proto_sources DEPENDS ${PROTO_SRCS} ${PROTO_HDRS}) |
||||
set_source_files_properties(${PROTO_SRCS} ${PROTO_HDRS} |
||||
PROPERTIES GENERATED TRUE) |
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -g -O0") |
||||
|
||||
add_executable(simple simple.c ${PROTO_SRCS} ${PROTO_HDRS}) |
@ -1,18 +0,0 @@ |
||||
Nanopb example "simple" using CMake |
||||
======================= |
||||
|
||||
This example is the same as the simple nanopb example but built using CMake. |
||||
|
||||
Example usage |
||||
------------- |
||||
|
||||
On Linux, create a build directory and then call cmake: |
||||
|
||||
nanopb/examples/cmake_simple$ mkdir build |
||||
nanopb/examples/cmake_simple$ cd build/ |
||||
nanopb/examples/cmake_simple/build$ cmake .. |
||||
nanopb/examples/cmake_simple/build$ make |
||||
|
||||
After that, you can run it with the command: ./simple |
||||
|
||||
On other platforms supported by CMake, refer to CMake instructions. |
@ -1,71 +0,0 @@ |
||||
#include <stdio.h> |
||||
#include <pb_encode.h> |
||||
#include <pb_decode.h> |
||||
#include "simple.pb.h" |
||||
|
||||
int main() |
||||
{ |
||||
/* This is the buffer where we will store our message. */ |
||||
uint8_t buffer[128]; |
||||
size_t message_length; |
||||
bool status; |
||||
|
||||
/* Encode our message */ |
||||
{ |
||||
/* Allocate space on the stack to store the message data.
|
||||
* |
||||
* Nanopb generates simple struct definitions for all the messages. |
||||
* - check out the contents of simple.pb.h! |
||||
* It is a good idea to always initialize your structures |
||||
* so that you do not have garbage data from RAM in there. |
||||
*/ |
||||
SimpleMessage message = SimpleMessage_init_zero; |
||||
|
||||
/* Create a stream that will write to our buffer. */ |
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
||||
|
||||
/* Fill in the lucky number */ |
||||
message.lucky_number = 13; |
||||
|
||||
/* Now we are ready to encode the message! */ |
||||
status = pb_encode(&stream, SimpleMessage_fields, &message); |
||||
message_length = stream.bytes_written; |
||||
|
||||
/* Then just check for any errors.. */ |
||||
if (!status) |
||||
{ |
||||
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); |
||||
return 1; |
||||
} |
||||
} |
||||
|
||||
/* Now we could transmit the message over network, store it in a file or
|
||||
* wrap it to a pigeon's leg. |
||||
*/ |
||||
|
||||
/* But because we are lazy, we will just decode it immediately. */ |
||||
|
||||
{ |
||||
/* Allocate space for the decoded message. */ |
||||
SimpleMessage message = SimpleMessage_init_zero; |
||||
|
||||
/* Create a stream that reads from the buffer. */ |
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length); |
||||
|
||||
/* Now we are ready to decode the message. */ |
||||
status = pb_decode(&stream, SimpleMessage_fields, &message); |
||||
|
||||
/* Check for errors... */ |
||||
if (!status) |
||||
{ |
||||
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream)); |
||||
return 1; |
||||
} |
||||
|
||||
/* Print the data contained in the message. */ |
||||
printf("Your lucky number was %d!\n", message.lucky_number); |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
@ -1,9 +0,0 @@ |
||||
// A very simple protocol definition, consisting of only |
||||
// one message. |
||||
|
||||
syntax = "proto2"; |
||||
|
||||
message SimpleMessage { |
||||
required int32 lucky_number = 1; |
||||
} |
||||
|
@ -1,17 +0,0 @@ |
||||
# Include the nanopb provided Makefile rules
|
||||
include ../../extra/nanopb.mk |
||||
|
||||
# Compiler flags to enable all warnings & debug info
|
||||
CFLAGS = -ansi -Wall -Werror -g -O0
|
||||
CFLAGS += -I$(NANOPB_DIR)
|
||||
|
||||
all: server client |
||||
|
||||
.SUFFIXES: |
||||
|
||||
clean: |
||||
rm -f server client fileproto.pb.c fileproto.pb.h
|
||||
|
||||
%: %.c common.c fileproto.pb.c |
||||
$(CC) $(CFLAGS) -o $@ $^ $(NANOPB_CORE)
|
||||
|
@ -1,60 +0,0 @@ |
||||
Nanopb example "network_server" |
||||
=============================== |
||||
|
||||
This example demonstrates the use of nanopb to communicate over network |
||||
connections. It consists of a server that sends file listings, and of |
||||
a client that requests the file list from the server. |
||||
|
||||
Example usage |
||||
------------- |
||||
|
||||
user@host:~/nanopb/examples/network_server$ make # Build the example |
||||
protoc -ofileproto.pb fileproto.proto |
||||
python ../../generator/nanopb_generator.py fileproto.pb |
||||
Writing to fileproto.pb.h and fileproto.pb.c |
||||
cc -ansi -Wall -Werror -I .. -g -O0 -I../.. -o server server.c |
||||
../../pb_decode.c ../../pb_encode.c fileproto.pb.c common.c |
||||
cc -ansi -Wall -Werror -I .. -g -O0 -I../.. -o client client.c |
||||
../../pb_decode.c ../../pb_encode.c fileproto.pb.c common.c |
||||
|
||||
user@host:~/nanopb/examples/network_server$ ./server & # Start the server on background |
||||
[1] 24462 |
||||
|
||||
petteri@oddish:~/nanopb/examples/network_server$ ./client /bin # Request the server to list /bin |
||||
Got connection. |
||||
Listing directory: /bin |
||||
1327119 bzdiff |
||||
1327126 bzless |
||||
1327147 ps |
||||
1327178 ntfsmove |
||||
1327271 mv |
||||
1327187 mount |
||||
1327259 false |
||||
1327266 tempfile |
||||
1327285 zfgrep |
||||
1327165 gzexe |
||||
1327204 nc.openbsd |
||||
1327260 uname |
||||
|
||||
|
||||
Details of implementation |
||||
------------------------- |
||||
fileproto.proto contains the portable Google Protocol Buffers protocol definition. |
||||
It could be used as-is to implement a server or a client in any other language, for |
||||
example Python or Java. |
||||
|
||||
fileproto.options contains the nanopb-specific options for the protocol file. This |
||||
sets the amount of space allocated for file names when decoding messages. |
||||
|
||||
common.c/h contains functions that allow nanopb to read and write directly from |
||||
network socket. This way there is no need to allocate a separate buffer to store |
||||
the message. |
||||
|
||||
server.c contains the code to open a listening socket, to respond to clients and |
||||
to list directory contents. |
||||
|
||||
client.c contains the code to connect to a server, to send a request and to print |
||||
the response message. |
||||
|
||||
The code is implemented using the POSIX socket api, but it should be easy enough |
||||
to port into any other socket api, such as lwip. |
@ -1,142 +0,0 @@ |
||||
/* This is a simple TCP client that connects to port 1234 and prints a list
|
||||
* of files in a given directory. |
||||
* |
||||
* It directly deserializes and serializes messages from network, minimizing |
||||
* memory use. |
||||
*
|
||||
* For flexibility, this example is implemented using posix api. |
||||
* In a real embedded system you would typically use some other kind of |
||||
* a communication and filesystem layer. |
||||
*/ |
||||
|
||||
#include <sys/socket.h> |
||||
#include <sys/types.h> |
||||
#include <netinet/in.h> |
||||
#include <unistd.h> |
||||
#include <dirent.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <pb_encode.h> |
||||
#include <pb_decode.h> |
||||
|
||||
#include "fileproto.pb.h" |
||||
#include "common.h" |
||||
|
||||
/* This callback function will be called once for each filename received
|
||||
* from the server. The filenames will be printed out immediately, so that |
||||
* no memory has to be allocated for them. |
||||
*/ |
||||
bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void **arg) |
||||
{ |
||||
FileInfo fileinfo = {}; |
||||
|
||||
if (!pb_decode(stream, FileInfo_fields, &fileinfo)) |
||||
return false; |
||||
|
||||
printf("%-10lld %s\n", (long long)fileinfo.inode, fileinfo.name); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
/* This function sends a request to socket 'fd' to list the files in
|
||||
* directory given in 'path'. The results received from server will |
||||
* be printed to stdout. |
||||
*/ |
||||
bool listdir(int fd, char *path) |
||||
{ |
||||
/* Construct and send the request to server */ |
||||
{ |
||||
ListFilesRequest request = {}; |
||||
pb_ostream_t output = pb_ostream_from_socket(fd); |
||||
uint8_t zero = 0; |
||||
|
||||
/* In our protocol, path is optional. If it is not given,
|
||||
* the server will list the root directory. */ |
||||
if (path == NULL) |
||||
{ |
||||
request.has_path = false; |
||||
} |
||||
else |
||||
{ |
||||
request.has_path = true; |
||||
if (strlen(path) + 1 > sizeof(request.path)) |
||||
{ |
||||
fprintf(stderr, "Too long path.\n"); |
||||
return false; |
||||
} |
||||
|
||||
strcpy(request.path, path); |
||||
} |
||||
|
||||
/* Encode the request. It is written to the socket immediately
|
||||
* through our custom stream. */ |
||||
if (!pb_encode(&output, ListFilesRequest_fields, &request)) |
||||
{ |
||||
fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&output)); |
||||
return false; |
||||
} |
||||
|
||||
/* We signal the end of request with a 0 tag. */ |
||||
pb_write(&output, &zero, 1); |
||||
} |
||||
|
||||
/* Read back the response from server */ |
||||
{ |
||||
ListFilesResponse response = {}; |
||||
pb_istream_t input = pb_istream_from_socket(fd); |
||||
|
||||
/* Give a pointer to our callback function, which will handle the
|
||||
* filenames as they arrive. */ |
||||
response.file.funcs.decode = &printfile_callback; |
||||
|
||||
if (!pb_decode(&input, ListFilesResponse_fields, &response)) |
||||
{ |
||||
fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&input)); |
||||
return false; |
||||
} |
||||
|
||||
/* If the message from server decodes properly, but directory was
|
||||
* not found on server side, we get path_error == true. */ |
||||
if (response.path_error) |
||||
{ |
||||
fprintf(stderr, "Server reported error.\n"); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
int main(int argc, char **argv) |
||||
{ |
||||
int sockfd; |
||||
struct sockaddr_in servaddr; |
||||
char *path = NULL; |
||||
|
||||
if (argc > 1) |
||||
path = argv[1]; |
||||
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0); |
||||
|
||||
/* Connect to server running on localhost:1234 */ |
||||
memset(&servaddr, 0, sizeof(servaddr)); |
||||
servaddr.sin_family = AF_INET; |
||||
servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
||||
servaddr.sin_port = htons(1234); |
||||
|
||||
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) |
||||
{ |
||||
perror("connect"); |
||||
return 1; |
||||
} |
||||
|
||||
/* Send the directory listing request */ |
||||
if (!listdir(sockfd, path)) |
||||
return 2; |
||||
|
||||
/* Close connection */ |
||||
close(sockfd); |
||||
|
||||
return 0; |
||||
} |
@ -1,40 +0,0 @@ |
||||
/* Simple binding of nanopb streams to TCP sockets.
|
||||
*/ |
||||
|
||||
#include <sys/socket.h> |
||||
#include <sys/types.h> |
||||
#include <pb_encode.h> |
||||
#include <pb_decode.h> |
||||
|
||||
#include "common.h" |
||||
|
||||
static bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count) |
||||
{ |
||||
int fd = (intptr_t)stream->state; |
||||
return send(fd, buf, count, 0) == count; |
||||
} |
||||
|
||||
static bool read_callback(pb_istream_t *stream, uint8_t *buf, size_t count) |
||||
{ |
||||
int fd = (intptr_t)stream->state; |
||||
int result; |
||||
|
||||
result = recv(fd, buf, count, MSG_WAITALL); |
||||
|
||||
if (result == 0) |
||||
stream->bytes_left = 0; /* EOF */ |
||||
|
||||
return result == count; |
||||
} |
||||
|
||||
pb_ostream_t pb_ostream_from_socket(int fd) |
||||
{ |
||||
pb_ostream_t stream = {&write_callback, (void*)(intptr_t)fd, SIZE_MAX, 0}; |
||||
return stream; |
||||
} |
||||
|
||||
pb_istream_t pb_istream_from_socket(int fd) |
||||
{ |
||||
pb_istream_t stream = {&read_callback, (void*)(intptr_t)fd, SIZE_MAX}; |
||||
return stream; |
||||
} |
@ -1,9 +0,0 @@ |
||||
#ifndef _PB_EXAMPLE_COMMON_H_ |
||||
#define _PB_EXAMPLE_COMMON_H_ |
||||
|
||||
#include <pb.h> |
||||
|
||||
pb_ostream_t pb_ostream_from_socket(int fd); |
||||
pb_istream_t pb_istream_from_socket(int fd); |
||||
|
||||
#endif |
@ -1,13 +0,0 @@ |
||||
# This file defines the nanopb-specific options for the messages defined |
||||
# in fileproto.proto. |
||||
# |
||||
# If you come from high-level programming background, the hardcoded |
||||
# maximum lengths may disgust you. However, if your microcontroller only |
||||
# has a few kB of ram to begin with, setting reasonable limits for |
||||
# filenames is ok. |
||||
# |
||||
# On the other hand, using the callback interface, it is not necessary |
||||
# to set a limit on the number of files in the response. |
||||
|
||||
ListFilesRequest.path max_size:128 |
||||
FileInfo.name max_size:128 |
@ -1,20 +0,0 @@ |
||||
// This defines protocol for a simple server that lists files. |
||||
// |
||||
// See also the nanopb-specific options in fileproto.options. |
||||
|
||||
syntax = "proto2"; |
||||
|
||||
message ListFilesRequest { |
||||
optional string path = 1 [default = "/"]; |
||||
} |
||||
|
||||
message FileInfo { |
||||
required uint64 inode = 1; |
||||
required string name = 2; |
||||
} |
||||
|
||||
message ListFilesResponse { |
||||
optional bool path_error = 1 [default = false]; |
||||
repeated FileInfo file = 2; |
||||
} |
||||
|
@ -1,158 +0,0 @@ |
||||
/* This is a simple TCP server that listens on port 1234 and provides lists
|
||||
* of files to clients, using a protocol defined in file_server.proto. |
||||
* |
||||
* It directly deserializes and serializes messages from network, minimizing |
||||
* memory use. |
||||
*
|
||||
* For flexibility, this example is implemented using posix api. |
||||
* In a real embedded system you would typically use some other kind of |
||||
* a communication and filesystem layer. |
||||
*/ |
||||
|
||||
#include <sys/socket.h> |
||||
#include <sys/types.h> |
||||
#include <netinet/in.h> |
||||
#include <unistd.h> |
||||
#include <dirent.h> |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <pb_encode.h> |
||||
#include <pb_decode.h> |
||||
|
||||
#include "fileproto.pb.h" |
||||
#include "common.h" |
||||
|
||||
/* This callback function will be called once during the encoding.
|
||||
* It will write out any number of FileInfo entries, without consuming unnecessary memory. |
||||
* This is accomplished by fetching the filenames one at a time and encoding them |
||||
* immediately. |
||||
*/ |
||||
bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) |
||||
{ |
||||
DIR *dir = (DIR*) *arg; |
||||
struct dirent *file; |
||||
FileInfo fileinfo = {}; |
||||
|
||||
while ((file = readdir(dir)) != NULL) |
||||
{ |
||||
fileinfo.inode = file->d_ino; |
||||
strncpy(fileinfo.name, file->d_name, sizeof(fileinfo.name)); |
||||
fileinfo.name[sizeof(fileinfo.name) - 1] = '\0'; |
||||
|
||||
/* This encodes the header for the field, based on the constant info
|
||||
* from pb_field_t. */ |
||||
if (!pb_encode_tag_for_field(stream, field)) |
||||
return false; |
||||
|
||||
/* This encodes the data for the field, based on our FileInfo structure. */ |
||||
if (!pb_encode_submessage(stream, FileInfo_fields, &fileinfo)) |
||||
return false; |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
/* Handle one arriving client connection.
|
||||
* Clients are expected to send a ListFilesRequest, terminated by a '0'. |
||||
* Server will respond with a ListFilesResponse message. |
||||
*/ |
||||
void handle_connection(int connfd) |
||||
{ |
||||
DIR *directory = NULL; |
||||
|
||||
/* Decode the message from the client and open the requested directory. */ |
||||
{ |
||||
ListFilesRequest request = {}; |
||||
pb_istream_t input = pb_istream_from_socket(connfd); |
||||
|
||||
if (!pb_decode(&input, ListFilesRequest_fields, &request)) |
||||
{ |
||||
printf("Decode failed: %s\n", PB_GET_ERROR(&input)); |
||||
return; |
||||
} |
||||
|
||||
directory = opendir(request.path); |
||||
printf("Listing directory: %s\n", request.path); |
||||
} |
||||
|
||||
/* List the files in the directory and transmit the response to client */ |
||||
{ |
||||
ListFilesResponse response = {}; |
||||
pb_ostream_t output = pb_ostream_from_socket(connfd); |
||||
|
||||
if (directory == NULL) |
||||
{ |
||||
perror("opendir"); |
||||
|
||||
/* Directory was not found, transmit error status */ |
||||
response.has_path_error = true; |
||||
response.path_error = true; |
||||
response.file.funcs.encode = NULL; |
||||
} |
||||
else |
||||
{ |
||||
/* Directory was found, transmit filenames */ |
||||
response.has_path_error = false; |
||||
response.file.funcs.encode = &listdir_callback; |
||||
response.file.arg = directory; |
||||
} |
||||
|
||||
if (!pb_encode(&output, ListFilesResponse_fields, &response)) |
||||
{ |
||||
printf("Encoding failed: %s\n", PB_GET_ERROR(&output)); |
||||
} |
||||
} |
||||
|
||||
if (directory != NULL) |
||||
closedir(directory); |
||||
} |
||||
|
||||
int main(int argc, char **argv) |
||||
{ |
||||
int listenfd, connfd; |
||||
struct sockaddr_in servaddr; |
||||
int reuse = 1; |
||||
|
||||
/* Listen on localhost:1234 for TCP connections */ |
||||
listenfd = socket(AF_INET, SOCK_STREAM, 0); |
||||
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)); |
||||
|
||||
memset(&servaddr, 0, sizeof(servaddr)); |
||||
servaddr.sin_family = AF_INET; |
||||
servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
||||
servaddr.sin_port = htons(1234); |
||||
if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) |
||||
{ |
||||
perror("bind"); |
||||
return 1; |
||||
} |
||||
|
||||
if (listen(listenfd, 5) != 0) |
||||
{ |
||||
perror("listen"); |
||||
return 1; |
||||
} |
||||
|
||||
for(;;) |
||||
{ |
||||
/* Wait for a client */ |
||||
connfd = accept(listenfd, NULL, NULL); |
||||
|
||||
if (connfd < 0) |
||||
{ |
||||
perror("accept"); |
||||
return 1; |
||||
} |
||||
|
||||
printf("Got connection.\n"); |
||||
|
||||
handle_connection(connfd); |
||||
|
||||
printf("Closing connection.\n"); |
||||
|
||||
close(connfd); |
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -1,22 +0,0 @@ |
||||
# Include the nanopb provided Makefile rules
|
||||
include ../../extra/nanopb.mk |
||||
|
||||
# Compiler flags to enable all warnings & debug info
|
||||
CFLAGS = -Wall -Werror -g -O0
|
||||
CFLAGS += -I$(NANOPB_DIR)
|
||||
|
||||
# C source code files that are required
|
||||
CSRC = simple.c # The main program
|
||||
CSRC += simple.pb.c # The compiled protocol definition
|
||||
CSRC += $(NANOPB_DIR)/pb_encode.c # The nanopb encoder
|
||||
CSRC += $(NANOPB_DIR)/pb_decode.c # The nanopb decoder
|
||||
CSRC += $(NANOPB_DIR)/pb_common.c # The nanopb common parts
|
||||
|
||||
# Build rule for the main program
|
||||
simple: $(CSRC) |
||||
$(CC) $(CFLAGS) -osimple $(CSRC)
|
||||
|
||||
# Build rule for the protocol
|
||||
simple.pb.c: simple.proto |
||||
$(PROTOC) $(PROTOC_OPTS) --nanopb_out=. simple.proto
|
||||
|
@ -1,29 +0,0 @@ |
||||
Nanopb example "simple" |
||||
======================= |
||||
|
||||
This example demonstrates the very basic use of nanopb. It encodes and |
||||
decodes a simple message. |
||||
|
||||
The code uses four different API functions: |
||||
|
||||
* pb_ostream_from_buffer() to declare the output buffer that is to be used |
||||
* pb_encode() to encode a message |
||||
* pb_istream_from_buffer() to declare the input buffer that is to be used |
||||
* pb_decode() to decode a message |
||||
|
||||
Example usage |
||||
------------- |
||||
|
||||
On Linux, simply type "make" to build the example. After that, you can |
||||
run it with the command: ./simple |
||||
|
||||
On other platforms, you first have to compile the protocol definition using |
||||
the following command:: |
||||
|
||||
../../generator-bin/protoc --nanopb_out=. simple.proto |
||||
|
||||
After that, add the following four files to your project and compile: |
||||
|
||||
simple.c simple.pb.c pb_encode.c pb_decode.c |
||||
|
||||
|
@ -1,71 +0,0 @@ |
||||
#include <stdio.h> |
||||
#include <pb_encode.h> |
||||
#include <pb_decode.h> |
||||
#include "simple.pb.h" |
||||
|
||||
int main() |
||||
{ |
||||
/* This is the buffer where we will store our message. */ |
||||
uint8_t buffer[128]; |
||||
size_t message_length; |
||||
bool status; |
||||
|
||||
/* Encode our message */ |
||||
{ |
||||
/* Allocate space on the stack to store the message data.
|
||||
* |
||||
* Nanopb generates simple struct definitions for all the messages. |
||||
* - check out the contents of simple.pb.h! |
||||
* It is a good idea to always initialize your structures |
||||
* so that you do not have garbage data from RAM in there. |
||||
*/ |
||||
SimpleMessage message = SimpleMessage_init_zero; |
||||
|
||||
/* Create a stream that will write to our buffer. */ |
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
||||
|
||||
/* Fill in the lucky number */ |
||||
message.lucky_number = 13; |
||||
|
||||
/* Now we are ready to encode the message! */ |
||||
status = pb_encode(&stream, SimpleMessage_fields, &message); |
||||
message_length = stream.bytes_written; |
||||
|
||||
/* Then just check for any errors.. */ |
||||
if (!status) |
||||
{ |
||||
printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); |
||||
return 1; |
||||
} |
||||
} |
||||
|
||||
/* Now we could transmit the message over network, store it in a file or
|
||||
* wrap it to a pigeon's leg. |
||||
*/ |
||||
|
||||
/* But because we are lazy, we will just decode it immediately. */ |
||||
|
||||
{ |
||||
/* Allocate space for the decoded message. */ |
||||
SimpleMessage message = SimpleMessage_init_zero; |
||||
|
||||
/* Create a stream that reads from the buffer. */ |
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length); |
||||
|
||||
/* Now we are ready to decode the message. */ |
||||
status = pb_decode(&stream, SimpleMessage_fields, &message); |
||||
|
||||
/* Check for errors... */ |
||||
if (!status) |
||||
{ |
||||
printf("Decoding failed: %s\n", PB_GET_ERROR(&stream)); |
||||
return 1; |
||||
} |
||||
|
||||
/* Print the data contained in the message. */ |
||||
printf("Your lucky number was %d!\n", message.lucky_number); |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
@ -1,9 +0,0 @@ |
||||
// A very simple protocol definition, consisting of only |
||||
// one message. |
||||
|
||||
syntax = "proto2"; |
||||
|
||||
message SimpleMessage { |
||||
required int32 lucky_number = 1; |
||||
} |
||||
|
@ -1,24 +0,0 @@ |
||||
# Include the nanopb provided Makefile rules
|
||||
include ../../extra/nanopb.mk |
||||
|
||||
# Compiler flags to enable all warnings & debug info
|
||||
CFLAGS = -Wall -Werror -g -O0
|
||||
CFLAGS += -I$(NANOPB_DIR)
|
||||
|
||||
all: run_tests |
||||
|
||||
.SUFFIXES: |
||||
|
||||
clean: |
||||
rm -f test_conversions encode_double decode_double doubleproto.pb.c doubleproto.pb.h
|
||||
|
||||
test_conversions: test_conversions.c double_conversion.c |
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
|
||||
%: %.c double_conversion.c doubleproto.pb.c |
||||
$(CC) $(CFLAGS) -o $@ $^ $(NANOPB_CORE)
|
||||
|
||||
run_tests: test_conversions encode_double decode_double |
||||
./test_conversions
|
||||
./encode_double | ./decode_double
|
||||
|
@ -1,25 +0,0 @@ |
||||
Nanopb example "using_double_on_avr" |
||||
==================================== |
||||
|
||||
Some processors/compilers, such as AVR-GCC, do not support the double |
||||
datatype. Instead, they have sizeof(double) == 4. Because protocol |
||||
binary format uses the double encoding directly, this causes trouble |
||||
if the protocol in .proto requires double fields. |
||||
|
||||
This directory contains a solution to this problem. It uses uint64_t |
||||
to store the raw wire values, because its size is correct on all |
||||
platforms. The file double_conversion.c provides functions that |
||||
convert these values to/from floats, without relying on compiler |
||||
support. |
||||
|
||||
To use this method, you need to make some modifications to your code: |
||||
|
||||
1) Change all 'double' fields into 'fixed64' in the .proto. |
||||
|
||||
2) Whenever writing to a 'double' field, use float_to_double(). |
||||
|
||||
3) Whenever reading a 'double' field, use double_to_float(). |
||||
|
||||
The conversion routines are as accurate as the float datatype can |
||||
be. Furthermore, they should handle all special values (NaN, inf, denormalized |
||||
numbers) correctly. There are testcases in test_conversions.c. |
@ -1,33 +0,0 @@ |
||||
/* Decodes a double value into a float variable.
|
||||
* Used to read double values with AVR code, which doesn't support double directly. |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <pb_decode.h> |
||||
#include "double_conversion.h" |
||||
#include "doubleproto.pb.h" |
||||
|
||||
int main() |
||||
{ |
||||
uint8_t buffer[32]; |
||||
size_t count = fread(buffer, 1, sizeof(buffer), stdin); |
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, count); |
||||
|
||||
AVRDoubleMessage message; |
||||
pb_decode(&stream, AVRDoubleMessage_fields, &message); |
||||
|
||||
float v1 = double_to_float(message.field1); |
||||
float v2 = double_to_float(message.field2); |
||||
|
||||
printf("Values: %f %f\n", v1, v2); |
||||
|
||||
if (v1 == 1234.5678f && |
||||
v2 == 0.00001f) |
||||
{ |
||||
return 0; |
||||
} |
||||
else |
||||
{ |
||||
return 1; |
||||
} |
||||
} |
@ -1,123 +0,0 @@ |
||||
/* Conversion routines for platforms that do not support 'double' directly. */ |
||||
|
||||
#include "double_conversion.h" |
||||
#include <math.h> |
||||
|
||||
typedef union { |
||||
float f; |
||||
uint32_t i; |
||||
} conversion_t; |
||||
|
||||
/* Note: IEE 754 standard specifies float formats as follows:
|
||||
* Single precision: sign, 8-bit exp, 23-bit frac. |
||||
* Double precision: sign, 11-bit exp, 52-bit frac. |
||||
*/ |
||||
|
||||
uint64_t float_to_double(float value) |
||||
{ |
||||
conversion_t in; |
||||
in.f = value; |
||||
uint8_t sign; |
||||
int16_t exponent; |
||||
uint64_t mantissa; |
||||
|
||||
/* Decompose input value */ |
||||
sign = (in.i >> 31) & 1; |
||||
exponent = ((in.i >> 23) & 0xFF) - 127; |
||||
mantissa = in.i & 0x7FFFFF; |
||||
|
||||
if (exponent == 128) |
||||
{ |
||||
/* Special value (NaN etc.) */ |
||||
exponent = 1024; |
||||
} |
||||
else if (exponent == -127) |
||||
{ |
||||
if (!mantissa) |
||||
{ |
||||
/* Zero */ |
||||
exponent = -1023; |
||||
} |
||||
else |
||||
{ |
||||
/* Denormalized */ |
||||
mantissa <<= 1; |
||||
while (!(mantissa & 0x800000)) |
||||
{ |
||||
mantissa <<= 1; |
||||
exponent--; |
||||
} |
||||
mantissa &= 0x7FFFFF; |
||||
} |
||||
} |
||||
|
||||
/* Combine fields */ |
||||
mantissa <<= 29; |
||||
mantissa |= (uint64_t)(exponent + 1023) << 52; |
||||
mantissa |= (uint64_t)sign << 63; |
||||
|
||||
return mantissa; |
||||
} |
||||
|
||||
float double_to_float(uint64_t value) |
||||
{ |
||||
uint8_t sign; |
||||
int16_t exponent; |
||||
uint32_t mantissa; |
||||
conversion_t out; |
||||
|
||||
/* Decompose input value */ |
||||
sign = (value >> 63) & 1; |
||||
exponent = ((value >> 52) & 0x7FF) - 1023; |
||||
mantissa = (value >> 28) & 0xFFFFFF; /* Highest 24 bits */ |
||||
|
||||
/* Figure if value is in range representable by floats. */ |
||||
if (exponent == 1024) |
||||
{ |
||||
/* Special value */ |
||||
exponent = 128; |
||||
} |
||||
else if (exponent > 127) |
||||
{ |
||||
/* Too large */
|
||||
if (sign) |
||||
return -INFINITY; |
||||
else |
||||
return INFINITY; |
||||
} |
||||
else if (exponent < -150) |
||||
{ |
||||
/* Too small */ |
||||
if (sign) |
||||
return -0.0f; |
||||
else |
||||
return 0.0f; |
||||
} |
||||
else if (exponent < -126) |
||||
{ |
||||
/* Denormalized */ |
||||
mantissa |= 0x1000000; |
||||
mantissa >>= (-126 - exponent); |
||||
exponent = -127; |
||||
} |
||||
|
||||
/* Round off mantissa */ |
||||
mantissa = (mantissa + 1) >> 1; |
||||
|
||||
/* Check if mantissa went over 2.0 */ |
||||
if (mantissa & 0x800000) |
||||
{ |
||||
exponent += 1; |
||||
mantissa &= 0x7FFFFF; |
||||
mantissa >>= 1; |
||||
} |
||||
|
||||
/* Combine fields */ |
||||
out.i = mantissa; |
||||
out.i |= (uint32_t)(exponent + 127) << 23; |
||||
out.i |= (uint32_t)sign << 31; |
||||
|
||||
return out.f; |
||||
} |
||||
|
||||
|
@ -1,26 +0,0 @@ |
||||
/* AVR-GCC does not have real double datatype. Instead its double
|
||||
* is equal to float, i.e. 32 bit value. If you need to communicate |
||||
* with other systems that use double in their .proto files, you |
||||
* need to do some conversion. |
||||
* |
||||
* These functions use bitwise operations to mangle floats into doubles |
||||
* and then store them in uint64_t datatype. |
||||
*/ |
||||
|
||||
#ifndef DOUBLE_CONVERSION |
||||
#define DOUBLE_CONVERSION |
||||
|
||||
#include <stdint.h> |
||||
|
||||
/* Convert native 4-byte float into a 8-byte double. */ |
||||
extern uint64_t float_to_double(float value); |
||||
|
||||
/* Convert 8-byte double into native 4-byte float.
|
||||
* Values are rounded to nearest, 0.5 away from zero. |
||||
* Overflowing values are converted to Inf or -Inf. |
||||
*/ |
||||
extern float double_to_float(uint64_t value); |
||||
|
||||
|
||||
#endif |
||||
|
@ -1,15 +0,0 @@ |
||||
// A message containing doubles, as used by other applications. |
||||
syntax = "proto2"; |
||||
|
||||
message DoubleMessage { |
||||
required double field1 = 1; |
||||
required double field2 = 2; |
||||
} |
||||
|
||||
// A message containing doubles, but redefined using uint64_t. |
||||
// For use in AVR code. |
||||
message AVRDoubleMessage { |
||||
required fixed64 field1 = 1; |
||||
required fixed64 field2 = 2; |
||||
} |
||||
|
@ -1,25 +0,0 @@ |
||||
/* Encodes a float value into a double on the wire.
|
||||
* Used to emit doubles from AVR code, which doesn't support double directly. |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <pb_encode.h> |
||||
#include "double_conversion.h" |
||||
#include "doubleproto.pb.h" |
||||
|
||||
int main() |
||||
{ |
||||
AVRDoubleMessage message = { |
||||
float_to_double(1234.5678f), |
||||
float_to_double(0.00001f) |
||||
}; |
||||
|
||||
uint8_t buffer[32]; |
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
||||
|
||||
pb_encode(&stream, AVRDoubleMessage_fields, &message); |
||||
fwrite(buffer, 1, stream.bytes_written, stdout); |
||||
|
||||
return 0; |
||||
} |
||||
|
@ -1,56 +0,0 @@ |
||||
#include "double_conversion.h" |
||||
#include <math.h> |
||||
#include <stdio.h> |
||||
|
||||
static const double testvalues[] = { |
||||
0.0, -0.0, 0.1, -0.1, |
||||
M_PI, -M_PI, 123456.789, -123456.789, |
||||
INFINITY, -INFINITY, NAN, INFINITY - INFINITY, |
||||
1e38, -1e38, 1e39, -1e39, |
||||
1e-38, -1e-38, 1e-39, -1e-39, |
||||
3.14159e-37,-3.14159e-37, 3.14159e-43, -3.14159e-43, |
||||
1e-60, -1e-60, 1e-45, -1e-45, |
||||
0.99999999999999, -0.99999999999999, 127.999999999999, -127.999999999999 |
||||
}; |
||||
|
||||
#define TESTVALUES_COUNT (sizeof(testvalues)/sizeof(testvalues[0])) |
||||
|
||||
int main() |
||||
{ |
||||
int status = 0; |
||||
int i; |
||||
for (i = 0; i < TESTVALUES_COUNT; i++) |
||||
{ |
||||
double orig = testvalues[i]; |
||||
float expected_float = (float)orig; |
||||
double expected_double = (double)expected_float; |
||||
|
||||
float got_float = double_to_float(*(uint64_t*)&orig); |
||||
uint64_t got_double = float_to_double(got_float); |
||||
|
||||
uint32_t e1 = *(uint32_t*)&expected_float; |
||||
uint32_t g1 = *(uint32_t*)&got_float; |
||||
uint64_t e2 = *(uint64_t*)&expected_double; |
||||
uint64_t g2 = got_double; |
||||
|
||||
if (g1 != e1) |
||||
{ |
||||
printf("%3d double_to_float fail: %08x != %08x\n", i, g1, e1); |
||||
status = 1; |
||||
} |
||||
|
||||
if (g2 != e2) |
||||
{ |
||||
printf("%3d float_to_double fail: %016llx != %016llx\n", i, |
||||
(unsigned long long)g2, |
||||
(unsigned long long)e2); |
||||
status = 1; |
||||
} |
||||
} |
||||
|
||||
return status; |
||||
} |
||||
|
||||
|
||||
|
||||
|
@ -1,20 +0,0 @@ |
||||
# Include the nanopb provided Makefile rules
|
||||
include ../../extra/nanopb.mk |
||||
|
||||
# Compiler flags to enable all warnings & debug info
|
||||
CFLAGS = -ansi -Wall -Werror -g -O0
|
||||
CFLAGS += -I$(NANOPB_DIR)
|
||||
|
||||
all: encode decode |
||||
./encode 1 | ./decode
|
||||
./encode 2 | ./decode
|
||||
./encode 3 | ./decode
|
||||
|
||||
.SUFFIXES: |
||||
|
||||
clean: |
||||
rm -f encode unionproto.pb.h unionproto.pb.c
|
||||
|
||||
%: %.c unionproto.pb.c |
||||
$(CC) $(CFLAGS) -o $@ $^ $(NANOPB_CORE)
|
||||
|
@ -1,52 +0,0 @@ |
||||
Nanopb example "using_union_messages" |
||||
===================================== |
||||
|
||||
Union messages is a common technique in Google Protocol Buffers used to |
||||
represent a group of messages, only one of which is passed at a time. |
||||
It is described in Google's documentation: |
||||
https://developers.google.com/protocol-buffers/docs/techniques#union |
||||
|
||||
This directory contains an example on how to encode and decode union messages |
||||
with minimal memory usage. Usually, nanopb would allocate space to store |
||||
all of the possible messages at the same time, even though at most one of |
||||
them will be used at a time. |
||||
|
||||
By using some of the lower level nanopb APIs, we can manually generate the |
||||
top level message, so that we only need to allocate the one submessage that |
||||
we actually want. Similarly when decoding, we can manually read the tag of |
||||
the top level message, and only then allocate the memory for the submessage |
||||
after we already know its type. |
||||
|
||||
|
||||
Example usage |
||||
------------- |
||||
|
||||
Type `make` to run the example. It will build it and run commands like |
||||
following: |
||||
|
||||
./encode 1 | ./decode |
||||
Got MsgType1: 42 |
||||
./encode 2 | ./decode |
||||
Got MsgType2: true |
||||
./encode 3 | ./decode |
||||
Got MsgType3: 3 1415 |
||||
|
||||
This simply demonstrates that the "decode" program has correctly identified |
||||
the type of the received message, and managed to decode it. |
||||
|
||||
|
||||
Details of implementation |
||||
------------------------- |
||||
|
||||
unionproto.proto contains the protocol used in the example. It consists of |
||||
three messages: MsgType1, MsgType2 and MsgType3, which are collected together |
||||
into UnionMessage. |
||||
|
||||
encode.c takes one command line argument, which should be a number 1-3. It |
||||
then fills in and encodes the corresponding message, and writes it to stdout. |
||||
|
||||
decode.c reads a UnionMessage from stdin. Then it calls the function |
||||
decode_unionmessage_type() to determine the type of the message. After that, |
||||
the corresponding message is decoded and the contents of it printed to the |
||||
screen. |
||||
|
@ -1,96 +0,0 @@ |
||||
/* This program reads a message from stdin, detects its type and decodes it.
|
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include <pb_decode.h> |
||||
#include "unionproto.pb.h" |
||||
|
||||
/* This function reads manually the first tag from the stream and finds the
|
||||
* corresponding message type. It doesn't yet decode the actual message. |
||||
* |
||||
* Returns a pointer to the MsgType_fields array, as an identifier for the |
||||
* message type. Returns null if the tag is of unknown type or an error occurs. |
||||
*/ |
||||
const pb_field_t* decode_unionmessage_type(pb_istream_t *stream) |
||||
{ |
||||
pb_wire_type_t wire_type; |
||||
uint32_t tag; |
||||
bool eof; |
||||
|
||||
while (pb_decode_tag(stream, &wire_type, &tag, &eof)) |
||||
{ |
||||
if (wire_type == PB_WT_STRING) |
||||
{ |
||||
const pb_field_t *field; |
||||
for (field = UnionMessage_fields; field->tag != 0; field++) |
||||
{ |
||||
if (field->tag == tag && (field->type & PB_LTYPE_SUBMESSAGE)) |
||||
{ |
||||
/* Found our field. */ |
||||
return field->ptr; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/* Wasn't our field.. */ |
||||
pb_skip_field(stream, wire_type); |
||||
} |
||||
|
||||
return NULL; |
||||
} |
||||
|
||||
bool decode_unionmessage_contents(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct) |
||||
{ |
||||
pb_istream_t substream; |
||||
bool status; |
||||
if (!pb_make_string_substream(stream, &substream)) |
||||
return false; |
||||
|
||||
status = pb_decode(&substream, fields, dest_struct); |
||||
pb_close_string_substream(stream, &substream); |
||||
return status; |
||||
} |
||||
|
||||
int main() |
||||
{ |
||||
/* Read the data into buffer */ |
||||
uint8_t buffer[512]; |
||||
size_t count = fread(buffer, 1, sizeof(buffer), stdin); |
||||
pb_istream_t stream = pb_istream_from_buffer(buffer, count); |
||||
|
||||
const pb_field_t *type = decode_unionmessage_type(&stream); |
||||
bool status = false; |
||||
|
||||
if (type == MsgType1_fields) |
||||
{ |
||||
MsgType1 msg = {}; |
||||
status = decode_unionmessage_contents(&stream, MsgType1_fields, &msg); |
||||
printf("Got MsgType1: %d\n", msg.value); |
||||
} |
||||
else if (type == MsgType2_fields) |
||||
{ |
||||
MsgType2 msg = {}; |
||||
status = decode_unionmessage_contents(&stream, MsgType2_fields, &msg); |
||||
printf("Got MsgType2: %s\n", msg.value ? "true" : "false"); |
||||
} |
||||
else if (type == MsgType3_fields) |
||||
{ |
||||
MsgType3 msg = {}; |
||||
status = decode_unionmessage_contents(&stream, MsgType3_fields, &msg); |
||||
printf("Got MsgType3: %d %d\n", msg.value1, msg.value2);
|
||||
} |
||||
|
||||
if (!status) |
||||
{ |
||||
printf("Decode failed: %s\n", PB_GET_ERROR(&stream)); |
||||
return 1; |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
|
||||
|
@ -1,85 +0,0 @@ |
||||
/* This program takes a command line argument and encodes a message in
|
||||
* one of MsgType1, MsgType2 or MsgType3. |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include <pb_encode.h> |
||||
#include "unionproto.pb.h" |
||||
|
||||
/* This function is the core of the union encoding process. It handles
|
||||
* the top-level pb_field_t array manually, in order to encode a correct |
||||
* field tag before the message. The pointer to MsgType_fields array is |
||||
* used as an unique identifier for the message type. |
||||
*/ |
||||
bool encode_unionmessage(pb_ostream_t *stream, const pb_field_t messagetype[], const void *message) |
||||
{ |
||||
const pb_field_t *field; |
||||
for (field = UnionMessage_fields; field->tag != 0; field++) |
||||
{ |
||||
if (field->ptr == messagetype) |
||||
{ |
||||
/* This is our field, encode the message using it. */ |
||||
if (!pb_encode_tag_for_field(stream, field)) |
||||
return false; |
||||
|
||||
return pb_encode_submessage(stream, messagetype, message); |
||||
} |
||||
} |
||||
|
||||
/* Didn't find the field for messagetype */ |
||||
return false; |
||||
} |
||||
|
||||
int main(int argc, char **argv) |
||||
{ |
||||
if (argc != 2) |
||||
{ |
||||
fprintf(stderr, "Usage: %s (1|2|3)\n", argv[0]); |
||||
return 1; |
||||
} |
||||
|
||||
uint8_t buffer[512]; |
||||
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
||||
|
||||
bool status = false; |
||||
int msgtype = atoi(argv[1]); |
||||
if (msgtype == 1) |
||||
{ |
||||
/* Send message of type 1 */ |
||||
MsgType1 msg = {42}; |
||||
status = encode_unionmessage(&stream, MsgType1_fields, &msg); |
||||
} |
||||
else if (msgtype == 2) |
||||
{ |
||||
/* Send message of type 2 */ |
||||
MsgType2 msg = {true}; |
||||
status = encode_unionmessage(&stream, MsgType2_fields, &msg); |
||||
} |
||||
else if (msgtype == 3) |
||||
{ |
||||
/* Send message of type 3 */ |
||||
MsgType3 msg = {3, 1415}; |
||||
status = encode_unionmessage(&stream, MsgType3_fields, &msg); |
||||
} |
||||
else |
||||
{ |
||||
fprintf(stderr, "Unknown message type: %d\n", msgtype); |
||||
return 2; |
||||
} |
||||
|
||||
if (!status) |
||||
{ |
||||
fprintf(stderr, "Encoding failed!\n"); |
||||
return 3; |
||||
} |
||||
else |
||||
{ |
||||
fwrite(buffer, 1, stream.bytes_written, stdout); |
||||
return 0; /* Success */ |
||||
} |
||||
} |
||||
|
||||
|
@ -1,32 +0,0 @@ |
||||
// This is an example of how to handle 'union' style messages |
||||
// with nanopb, without allocating memory for all the message types. |
||||
// |
||||
// There is no official type in Protocol Buffers for describing unions, |
||||
// but they are commonly implemented by filling out exactly one of |
||||
// several optional fields. |
||||
|
||||
syntax = "proto2"; |
||||
|
||||
message MsgType1 |
||||
{ |
||||
required int32 value = 1; |
||||
} |
||||
|
||||
message MsgType2 |
||||
{ |
||||
required bool value = 1; |
||||
} |
||||
|
||||
message MsgType3 |
||||
{ |
||||
required int32 value1 = 1; |
||||
required int32 value2 = 2; |
||||
} |
||||
|
||||
message UnionMessage |
||||
{ |
||||
optional MsgType1 msg1 = 1; |
||||
optional MsgType2 msg2 = 2; |
||||
optional MsgType3 msg3 = 3; |
||||
} |
||||
|
@ -1,274 +0,0 @@ |
||||
# This is an example script for use with CMake projects for locating and configuring |
||||
# the nanopb library. |
||||
# |
||||
# The following variables can be set and are optional: |
||||
# |
||||
# |
||||
# PROTOBUF_SRC_ROOT_FOLDER - When compiling with MSVC, if this cache variable is set |
||||
# the protobuf-default VS project build locations |
||||
# (vsprojects/Debug & vsprojects/Release) will be searched |
||||
# for libraries and binaries. |
||||
# |
||||
# NANOPB_IMPORT_DIRS - List of additional directories to be searched for |
||||
# imported .proto files. |
||||
# |
||||
# NANOPB_GENERATE_CPP_APPEND_PATH - By default -I will be passed to protoc |
||||
# for each directory where a proto file is referenced. |
||||
# Set to FALSE if you want to disable this behaviour. |
||||
# |
||||
# Defines the following variables: |
||||
# |
||||
# NANOPB_FOUND - Found the nanopb library (source&header files, generator tool, protoc compiler tool) |
||||
# NANOPB_INCLUDE_DIRS - Include directories for Google Protocol Buffers |
||||
# |
||||
# The following cache variables are also available to set or use: |
||||
# PROTOBUF_PROTOC_EXECUTABLE - The protoc compiler |
||||
# NANOPB_GENERATOR_SOURCE_DIR - The nanopb generator source |
||||
# |
||||
# ==================================================================== |
||||
# |
||||
# NANOPB_GENERATE_CPP (public function) |
||||
# SRCS = Variable to define with autogenerated |
||||
# source files |
||||
# HDRS = Variable to define with autogenerated |
||||
# header files |
||||
# ARGN = proto files |
||||
# |
||||
# ==================================================================== |
||||
# Example: |
||||
# |
||||
# set(NANOPB_SRC_ROOT_FOLDER "/path/to/nanopb") |
||||
# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${NANOPB_SRC_ROOT_FOLDER}/cmake) |
||||
# find_package( Nanopb REQUIRED ) |
||||
# include_directories(${NANOPB_INCLUDE_DIRS}) |
||||
# |
||||
# NANOPB_GENERATE_CPP(PROTO_SRCS PROTO_HDRS foo.proto) |
||||
# |
||||
# include_directories(${CMAKE_CURRENT_BINARY_DIR}) |
||||
# add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS}) |
||||
# |
||||
# ==================================================================== |
||||
|
||||
#============================================================================= |
||||
# Copyright 2009 Kitware, Inc. |
||||
# Copyright 2009-2011 Philip Lowman <philip@yhbt.com> |
||||
# Copyright 2008 Esben Mose Hansen, Ange Optimization ApS |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions |
||||
# are met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# |
||||
# * Redistributions in binary form must reproduce the above copyright |
||||
# notice, this list of conditions and the following disclaimer in the |
||||
# documentation and/or other materials provided with the distribution. |
||||
# |
||||
# * Neither the names of Kitware, Inc., the Insight Software Consortium, |
||||
# nor the names of their contributors may be used to endorse or promote |
||||
# products derived from this software without specific prior written |
||||
# permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
# |
||||
#============================================================================= |
||||
# |
||||
# Changes |
||||
# 2013.01.31 - Pavlo Ilin - used Modules/FindProtobuf.cmake from cmake 2.8.10 to |
||||
# write FindNanopb.cmake |
||||
# |
||||
#============================================================================= |
||||
|
||||
|
||||
function(NANOPB_GENERATE_CPP SRCS HDRS) |
||||
if(NOT ARGN) |
||||
return() |
||||
endif() |
||||
|
||||
if(NANOPB_GENERATE_CPP_APPEND_PATH) |
||||
# Create an include path for each file specified |
||||
foreach(FIL ${ARGN}) |
||||
get_filename_component(ABS_FIL ${FIL} ABSOLUTE) |
||||
get_filename_component(ABS_PATH ${ABS_FIL} PATH) |
||||
|
||||
list(FIND _nanobp_include_path ${ABS_PATH} _contains_already) |
||||
if(${_contains_already} EQUAL -1) |
||||
list(APPEND _nanobp_include_path -I ${ABS_PATH}) |
||||
endif() |
||||
endforeach() |
||||
else() |
||||
set(_nanobp_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) |
||||
endif() |
||||
|
||||
if(DEFINED NANOPB_IMPORT_DIRS) |
||||
foreach(DIR ${NANOPB_IMPORT_DIRS}) |
||||
get_filename_component(ABS_PATH ${DIR} ABSOLUTE) |
||||
list(FIND _nanobp_include_path ${ABS_PATH} _contains_already) |
||||
if(${_contains_already} EQUAL -1) |
||||
list(APPEND _nanobp_include_path -I ${ABS_PATH}) |
||||
endif() |
||||
endforeach() |
||||
endif() |
||||
|
||||
set(${SRCS}) |
||||
set(${HDRS}) |
||||
|
||||
set(GENERATOR_PATH ${CMAKE_BINARY_DIR}/nanopb/generator) |
||||
|
||||
set(NANOPB_GENERATOR_EXECUTABLE ${GENERATOR_PATH}/nanopb_generator.py) |
||||
|
||||
set(GENERATOR_CORE_DIR ${GENERATOR_PATH}/proto) |
||||
set(GENERATOR_CORE_SRC |
||||
${GENERATOR_CORE_DIR}/nanopb.proto |
||||
${GENERATOR_CORE_DIR}/plugin.proto) |
||||
|
||||
# Treat the source diretory as immutable. |
||||
# |
||||
# Copy the generator directory to the build directory before |
||||
# compiling python and proto files. Fixes issues when using the |
||||
# same build directory with different python/protobuf versions |
||||
# as the binary build directory is discarded across builds. |
||||
# |
||||
add_custom_command( |
||||
OUTPUT ${NANOPB_GENERATOR_EXECUTABLE} ${GENERATOR_CORE_SRC} |
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory |
||||
ARGS ${NANOPB_GENERATOR_SOURCE_DIR} ${GENERATOR_PATH} |
||||
VERBATIM) |
||||
|
||||
set(GENERATOR_CORE_PYTHON_SRC) |
||||
foreach(FIL ${GENERATOR_CORE_SRC}) |
||||
get_filename_component(ABS_FIL ${FIL} ABSOLUTE) |
||||
get_filename_component(FIL_WE ${FIL} NAME_WE) |
||||
|
||||
set(output "${GENERATOR_CORE_DIR}/${FIL_WE}_pb2.py") |
||||
set(GENERATOR_CORE_PYTHON_SRC ${GENERATOR_CORE_PYTHON_SRC} ${output}) |
||||
add_custom_command( |
||||
OUTPUT ${output} |
||||
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} |
||||
ARGS -I${GENERATOR_PATH}/proto |
||||
--python_out=${GENERATOR_CORE_DIR} ${ABS_FIL} |
||||
DEPENDS ${ABS_FIL} |
||||
VERBATIM) |
||||
endforeach() |
||||
|
||||
foreach(FIL ${ARGN}) |
||||
get_filename_component(ABS_FIL ${FIL} ABSOLUTE) |
||||
get_filename_component(FIL_WE ${FIL} NAME_WE) |
||||
get_filename_component(FIL_DIR ${FIL} PATH) |
||||
set(NANOPB_OPTIONS_FILE ${FIL_DIR}/${FIL_WE}.options) |
||||
set(NANOPB_OPTIONS) |
||||
if(EXISTS ${NANOPB_OPTIONS_FILE}) |
||||
set(NANOPB_OPTIONS -f ${NANOPB_OPTIONS_FILE}) |
||||
endif() |
||||
|
||||
list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c") |
||||
list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h") |
||||
|
||||
add_custom_command( |
||||
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb" |
||||
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} |
||||
ARGS -I${GENERATOR_PATH} -I${GENERATOR_CORE_DIR} |
||||
-I${CMAKE_CURRENT_BINARY_DIR} ${_nanobp_include_path} |
||||
-o${FIL_WE}.pb ${ABS_FIL} |
||||
DEPENDS ${ABS_FIL} ${GENERATOR_CORE_PYTHON_SRC} |
||||
COMMENT "Running C++ protocol buffer compiler on ${FIL}" |
||||
VERBATIM ) |
||||
|
||||
add_custom_command( |
||||
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.c" |
||||
"${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h" |
||||
COMMAND ${PYTHON_EXECUTABLE} |
||||
ARGS ${NANOPB_GENERATOR_EXECUTABLE} ${FIL_WE}.pb ${NANOPB_OPTIONS} |
||||
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb" |
||||
COMMENT "Running nanopb generator on ${FIL_WE}.pb" |
||||
VERBATIM ) |
||||
endforeach() |
||||
|
||||
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) |
||||
set(${SRCS} ${${SRCS}} ${NANOPB_SRCS} PARENT_SCOPE) |
||||
set(${HDRS} ${${HDRS}} ${NANOPB_HDRS} PARENT_SCOPE) |
||||
|
||||
endfunction() |
||||
|
||||
|
||||
|
||||
# |
||||
# Main. |
||||
# |
||||
|
||||
# By default have NANOPB_GENERATE_CPP macro pass -I to protoc |
||||
# for each directory where a proto file is referenced. |
||||
if(NOT DEFINED NANOPB_GENERATE_CPP_APPEND_PATH) |
||||
set(NANOPB_GENERATE_CPP_APPEND_PATH TRUE) |
||||
endif() |
||||
|
||||
# Make a really good guess regarding location of NANOPB_SRC_ROOT_FOLDER |
||||
if(NOT DEFINED NANOPB_SRC_ROOT_FOLDER) |
||||
get_filename_component(NANOPB_SRC_ROOT_FOLDER |
||||
${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE) |
||||
endif() |
||||
|
||||
# Find the include directory |
||||
find_path(NANOPB_INCLUDE_DIRS |
||||
pb.h |
||||
PATHS ${NANOPB_SRC_ROOT_FOLDER} |
||||
) |
||||
mark_as_advanced(NANOPB_INCLUDE_DIRS) |
||||
|
||||
# Find nanopb source files |
||||
set(NANOPB_SRCS) |
||||
set(NANOPB_HDRS) |
||||
list(APPEND _nanopb_srcs pb_decode.c pb_encode.c pb_common.c) |
||||
list(APPEND _nanopb_hdrs pb_decode.h pb_encode.h pb_common.h pb.h) |
||||
|
||||
foreach(FIL ${_nanopb_srcs}) |
||||
find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_SRC_ROOT_FOLDER} ${NANOPB_INCLUDE_DIRS}) |
||||
list(APPEND NANOPB_SRCS "${${FIL}__nano_pb_file}") |
||||
mark_as_advanced(${FIL}__nano_pb_file) |
||||
endforeach() |
||||
|
||||
foreach(FIL ${_nanopb_hdrs}) |
||||
find_file(${FIL}__nano_pb_file NAMES ${FIL} PATHS ${NANOPB_INCLUDE_DIRS}) |
||||
mark_as_advanced(${FIL}__nano_pb_file) |
||||
list(APPEND NANOPB_HDRS "${${FIL}__nano_pb_file}") |
||||
endforeach() |
||||
|
||||
# Find the protoc Executable |
||||
find_program(PROTOBUF_PROTOC_EXECUTABLE |
||||
NAMES protoc |
||||
DOC "The Google Protocol Buffers Compiler" |
||||
PATHS |
||||
${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Release |
||||
${PROTOBUF_SRC_ROOT_FOLDER}/vsprojects/Debug |
||||
) |
||||
mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE) |
||||
|
||||
# Find nanopb generator source dir |
||||
find_path(NANOPB_GENERATOR_SOURCE_DIR |
||||
NAMES nanopb_generator.py |
||||
DOC "nanopb generator source" |
||||
PATHS |
||||
${NANOPB_SRC_ROOT_FOLDER}/generator |
||||
) |
||||
mark_as_advanced(NANOPB_GENERATOR_SOURCE_DIR) |
||||
|
||||
find_package(PythonInterp REQUIRED) |
||||
|
||||
include(FindPackageHandleStandardArgs) |
||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NANOPB DEFAULT_MSG |
||||
NANOPB_INCLUDE_DIRS |
||||
NANOPB_SRCS NANOPB_HDRS |
||||
NANOPB_GENERATOR_SOURCE_DIR |
||||
PROTOBUF_PROTOC_EXECUTABLE |
||||
) |
@ -1,37 +0,0 @@ |
||||
# This is an include file for Makefiles. It provides rules for building
|
||||
# .pb.c and .pb.h files out of .proto, as well the path to nanopb core.
|
||||
|
||||
# Path to the nanopb root directory
|
||||
NANOPB_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))../)
|
||||
|
||||
# Files for the nanopb core
|
||||
NANOPB_CORE = $(NANOPB_DIR)/pb_encode.c $(NANOPB_DIR)/pb_decode.c $(NANOPB_DIR)/pb_common.c
|
||||
|
||||
# Check if we are running on Windows
|
||||
ifdef windir |
||||
WINDOWS = 1
|
||||
endif |
||||
ifdef WINDIR |
||||
WINDOWS = 1
|
||||
endif |
||||
|
||||
# Check whether to use binary version of nanopb_generator or the
|
||||
# system-supplied python interpreter.
|
||||
ifneq "$(wildcard $(NANOPB_DIR)/generator-bin)" "" |
||||
# Binary package
|
||||
PROTOC = $(NANOPB_DIR)/generator-bin/protoc
|
||||
PROTOC_OPTS =
|
||||
else |
||||
# Source only or git checkout
|
||||
PROTOC = protoc
|
||||
ifdef WINDOWS
|
||||
PROTOC_OPTS = --plugin=protoc-gen-nanopb=$(NANOPB_DIR)/generator/protoc-gen-nanopb.bat
|
||||
else
|
||||
PROTOC_OPTS = --plugin=protoc-gen-nanopb=$(NANOPB_DIR)/generator/protoc-gen-nanopb
|
||||
endif
|
||||
endif |
||||
|
||||
# Rule for building .pb.c and .pb.h
|
||||
%.pb.c %.pb.h: %.proto $(wildcard %.options) |
||||
$(PROTOC) $(PROTOC_OPTS) --nanopb_out=. $<
|
||||
|
@ -1,112 +0,0 @@ |
||||
/* This is an example of a header file for platforms/compilers that do
|
||||
* not come with stdint.h/stddef.h/stdbool.h/string.h. To use it, define |
||||
* PB_SYSTEM_HEADER as "pb_syshdr.h", including the quotes, and add the |
||||
* extra folder to your include path. |
||||
* |
||||
* It is very likely that you will need to customize this file to suit |
||||
* your platform. For any compiler that supports C99, this file should |
||||
* not be necessary. |
||||
*/ |
||||
|
||||
#ifndef _PB_SYSHDR_H_ |
||||
#define _PB_SYSHDR_H_ |
||||
|
||||
/* stdint.h subset */ |
||||
#ifdef HAVE_STDINT_H |
||||
#include <stdint.h> |
||||
#else |
||||
/* You will need to modify these to match the word size of your platform. */ |
||||
typedef signed char int8_t; |
||||
typedef unsigned char uint8_t; |
||||
typedef signed short int16_t; |
||||
typedef unsigned short uint16_t; |
||||
typedef signed int int32_t; |
||||
typedef unsigned int uint32_t; |
||||
typedef signed long long int64_t; |
||||
typedef unsigned long long uint64_t; |
||||
|
||||
/* These are ok for most platforms, unless uint8_t is actually not available,
|
||||
* in which case you should give the smallest available type. */ |
||||
typedef int8_t int_least8_t; |
||||
typedef uint8_t uint_least8_t; |
||||
typedef uint8_t uint_fast8_t; |
||||
typedef int16_t int_least16_t; |
||||
typedef uint16_t uint_least16_t; |
||||
#endif |
||||
|
||||
/* stddef.h subset */ |
||||
#ifdef HAVE_STDDEF_H |
||||
#include <stddef.h> |
||||
#else |
||||
|
||||
typedef uint32_t size_t; |
||||
#define offsetof(st, m) ((size_t)(&((st *)0)->m)) |
||||
|
||||
#ifndef NULL |
||||
#define NULL 0 |
||||
#endif |
||||
|
||||
#endif |
||||
|
||||
/* stdbool.h subset */ |
||||
#ifdef HAVE_STDBOOL_H |
||||
#include <stdbool.h> |
||||
#else |
||||
|
||||
#ifndef __cplusplus |
||||
typedef int bool; |
||||
#define false 0 |
||||
#define true 1 |
||||
#endif |
||||
|
||||
#endif |
||||
|
||||
/* stdlib.h subset */ |
||||
#ifdef PB_ENABLE_MALLOC |
||||
#ifdef HAVE_STDLIB_H |
||||
#include <stdlib.h> |
||||
#else |
||||
void *realloc(void *ptr, size_t size); |
||||
void free(void *ptr); |
||||
#endif |
||||
#endif |
||||
|
||||
/* string.h subset */ |
||||
#ifdef HAVE_STRING_H |
||||
#include <string.h> |
||||
#else |
||||
|
||||
/* Implementations are from the Public Domain C Library (PDCLib). */ |
||||
static size_t strlen( const char * s ) |
||||
{ |
||||
size_t rc = 0; |
||||
while ( s[rc] ) |
||||
{ |
||||
++rc; |
||||
} |
||||
return rc; |
||||
} |
||||
|
||||
static void * memcpy( void *s1, const void *s2, size_t n ) |
||||
{ |
||||
char * dest = (char *) s1; |
||||
const char * src = (const char *) s2; |
||||
while ( n-- ) |
||||
{ |
||||
*dest++ = *src++; |
||||
} |
||||
return s1; |
||||
} |
||||
|
||||
static void * memset( void * s, int c, size_t n ) |
||||
{ |
||||
unsigned char * p = (unsigned char *) s; |
||||
while ( n-- ) |
||||
{ |
||||
*p++ = (unsigned char) c; |
||||
} |
||||
return s; |
||||
} |
||||
#endif |
||||
|
||||
#endif |
File diff suppressed because it is too large
Load Diff
@ -1,4 +0,0 @@ |
||||
all: nanopb_pb2.py plugin_pb2.py |
||||
|
||||
%_pb2.py: %.proto |
||||
protoc --python_out=. $<
|
@ -1,714 +0,0 @@ |
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// https://developers.google.com/protocol-buffers/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// Author: kenton@google.com (Kenton Varda) |
||||
// Based on original Protocol Buffers design by |
||||
// Sanjay Ghemawat, Jeff Dean, and others. |
||||
// |
||||
// The messages in this file describe the definitions found in .proto files. |
||||
// A valid .proto file can be translated directly to a FileDescriptorProto |
||||
// without any other information (e.g. without reading its imports). |
||||
|
||||
|
||||
syntax = "proto2"; |
||||
|
||||
package google.protobuf; |
||||
option java_package = "com.google.protobuf"; |
||||
option java_outer_classname = "DescriptorProtos"; |
||||
|
||||
// descriptor.proto must be optimized for speed because reflection-based |
||||
// algorithms don't work during bootstrapping. |
||||
option optimize_for = SPEED; |
||||
|
||||
// The protocol compiler can output a FileDescriptorSet containing the .proto |
||||
// files it parses. |
||||
message FileDescriptorSet { |
||||
repeated FileDescriptorProto file = 1; |
||||
} |
||||
|
||||
// Describes a complete .proto file. |
||||
message FileDescriptorProto { |
||||
optional string name = 1; // file name, relative to root of source tree |
||||
optional string package = 2; // e.g. "foo", "foo.bar", etc. |
||||
|
||||
// Names of files imported by this file. |
||||
repeated string dependency = 3; |
||||
// Indexes of the public imported files in the dependency list above. |
||||
repeated int32 public_dependency = 10; |
||||
// Indexes of the weak imported files in the dependency list. |
||||
// For Google-internal migration only. Do not use. |
||||
repeated int32 weak_dependency = 11; |
||||
|
||||
// All top-level definitions in this file. |
||||
repeated DescriptorProto message_type = 4; |
||||
repeated EnumDescriptorProto enum_type = 5; |
||||
repeated ServiceDescriptorProto service = 6; |
||||
repeated FieldDescriptorProto extension = 7; |
||||
|
||||
optional FileOptions options = 8; |
||||
|
||||
// This field contains optional information about the original source code. |
||||
// You may safely remove this entire field without harming runtime |
||||
// functionality of the descriptors -- the information is needed only by |
||||
// development tools. |
||||
optional SourceCodeInfo source_code_info = 9; |
||||
|
||||
// The syntax of the proto file. |
||||
// The supported values are "proto2" and "proto3". |
||||
optional string syntax = 12; |
||||
} |
||||
|
||||
// Describes a message type. |
||||
message DescriptorProto { |
||||
optional string name = 1; |
||||
|
||||
repeated FieldDescriptorProto field = 2; |
||||
repeated FieldDescriptorProto extension = 6; |
||||
|
||||
repeated DescriptorProto nested_type = 3; |
||||
repeated EnumDescriptorProto enum_type = 4; |
||||
|
||||
message ExtensionRange { |
||||
optional int32 start = 1; |
||||
optional int32 end = 2; |
||||
} |
||||
repeated ExtensionRange extension_range = 5; |
||||
|
||||
repeated OneofDescriptorProto oneof_decl = 8; |
||||
|
||||
optional MessageOptions options = 7; |
||||
} |
||||
|
||||
// Describes a field within a message. |
||||
message FieldDescriptorProto { |
||||
enum Type { |
||||
// 0 is reserved for errors. |
||||
// Order is weird for historical reasons. |
||||
TYPE_DOUBLE = 1; |
||||
TYPE_FLOAT = 2; |
||||
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if |
||||
// negative values are likely. |
||||
TYPE_INT64 = 3; |
||||
TYPE_UINT64 = 4; |
||||
// Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if |
||||
// negative values are likely. |
||||
TYPE_INT32 = 5; |
||||
TYPE_FIXED64 = 6; |
||||
TYPE_FIXED32 = 7; |
||||
TYPE_BOOL = 8; |
||||
TYPE_STRING = 9; |
||||
TYPE_GROUP = 10; // Tag-delimited aggregate. |
||||
TYPE_MESSAGE = 11; // Length-delimited aggregate. |
||||
|
||||
// New in version 2. |
||||
TYPE_BYTES = 12; |
||||
TYPE_UINT32 = 13; |
||||
TYPE_ENUM = 14; |
||||
TYPE_SFIXED32 = 15; |
||||
TYPE_SFIXED64 = 16; |
||||
TYPE_SINT32 = 17; // Uses ZigZag encoding. |
||||
TYPE_SINT64 = 18; // Uses ZigZag encoding. |
||||
}; |
||||
|
||||
enum Label { |
||||
// 0 is reserved for errors |
||||
LABEL_OPTIONAL = 1; |
||||
LABEL_REQUIRED = 2; |
||||
LABEL_REPEATED = 3; |
||||
// TODO(sanjay): Should we add LABEL_MAP? |
||||
}; |
||||
|
||||
optional string name = 1; |
||||
optional int32 number = 3; |
||||
optional Label label = 4; |
||||
|
||||
// If type_name is set, this need not be set. If both this and type_name |
||||
// are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or TYPE_GROUP. |
||||
optional Type type = 5; |
||||
|
||||
// For message and enum types, this is the name of the type. If the name |
||||
// starts with a '.', it is fully-qualified. Otherwise, C++-like scoping |
||||
// rules are used to find the type (i.e. first the nested types within this |
||||
// message are searched, then within the parent, on up to the root |
||||
// namespace). |
||||
optional string type_name = 6; |
||||
|
||||
// For extensions, this is the name of the type being extended. It is |
||||
// resolved in the same manner as type_name. |
||||
optional string extendee = 2; |
||||
|
||||
// For numeric types, contains the original text representation of the value. |
||||
// For booleans, "true" or "false". |
||||
// For strings, contains the default text contents (not escaped in any way). |
||||
// For bytes, contains the C escaped value. All bytes >= 128 are escaped. |
||||
// TODO(kenton): Base-64 encode? |
||||
optional string default_value = 7; |
||||
|
||||
// If set, gives the index of a oneof in the containing type's oneof_decl |
||||
// list. This field is a member of that oneof. Extensions of a oneof should |
||||
// not set this since the oneof to which they belong will be inferred based |
||||
// on the extension range containing the extension's field number. |
||||
optional int32 oneof_index = 9; |
||||
|
||||
optional FieldOptions options = 8; |
||||
} |
||||
|
||||
// Describes a oneof. |
||||
message OneofDescriptorProto { |
||||
optional string name = 1; |
||||
} |
||||
|
||||
// Describes an enum type. |
||||
message EnumDescriptorProto { |
||||
optional string name = 1; |
||||
|
||||
repeated EnumValueDescriptorProto value = 2; |
||||
|
||||
optional EnumOptions options = 3; |
||||
} |
||||
|
||||
// Describes a value within an enum. |
||||
message EnumValueDescriptorProto { |
||||
optional string name = 1; |
||||
optional int32 number = 2; |
||||
|
||||
optional EnumValueOptions options = 3; |
||||
} |
||||
|
||||
// Describes a service. |
||||
message ServiceDescriptorProto { |
||||
optional string name = 1; |
||||
repeated MethodDescriptorProto method = 2; |
||||
|
||||
optional ServiceOptions options = 3; |
||||
} |
||||
|
||||
// Describes a method of a service. |
||||
message MethodDescriptorProto { |
||||
optional string name = 1; |
||||
|
||||
// Input and output type names. These are resolved in the same way as |
||||
// FieldDescriptorProto.type_name, but must refer to a message type. |
||||
optional string input_type = 2; |
||||
optional string output_type = 3; |
||||
|
||||
optional MethodOptions options = 4; |
||||
|
||||
// Identifies if client streams multiple client messages |
||||
optional bool client_streaming = 5 [default=false]; |
||||
// Identifies if server streams multiple server messages |
||||
optional bool server_streaming = 6 [default=false]; |
||||
} |
||||
|
||||
|
||||
// =================================================================== |
||||
// Options |
||||
|
||||
// Each of the definitions above may have "options" attached. These are |
||||
// just annotations which may cause code to be generated slightly differently |
||||
// or may contain hints for code that manipulates protocol messages. |
||||
// |
||||
// Clients may define custom options as extensions of the *Options messages. |
||||
// These extensions may not yet be known at parsing time, so the parser cannot |
||||
// store the values in them. Instead it stores them in a field in the *Options |
||||
// message called uninterpreted_option. This field must have the same name |
||||
// across all *Options messages. We then use this field to populate the |
||||
// extensions when we build a descriptor, at which point all protos have been |
||||
// parsed and so all extensions are known. |
||||
// |
||||
// Extension numbers for custom options may be chosen as follows: |
||||
// * For options which will only be used within a single application or |
||||
// organization, or for experimental options, use field numbers 50000 |
||||
// through 99999. It is up to you to ensure that you do not use the |
||||
// same number for multiple options. |
||||
// * For options which will be published and used publicly by multiple |
||||
// independent entities, e-mail protobuf-global-extension-registry@google.com |
||||
// to reserve extension numbers. Simply provide your project name (e.g. |
||||
// Object-C plugin) and your porject website (if available) -- there's no need |
||||
// to explain how you intend to use them. Usually you only need one extension |
||||
// number. You can declare multiple options with only one extension number by |
||||
// putting them in a sub-message. See the Custom Options section of the docs |
||||
// for examples: |
||||
// https://developers.google.com/protocol-buffers/docs/proto#options |
||||
// If this turns out to be popular, a web service will be set up |
||||
// to automatically assign option numbers. |
||||
|
||||
|
||||
message FileOptions { |
||||
|
||||
// Sets the Java package where classes generated from this .proto will be |
||||
// placed. By default, the proto package is used, but this is often |
||||
// inappropriate because proto packages do not normally start with backwards |
||||
// domain names. |
||||
optional string java_package = 1; |
||||
|
||||
|
||||
// If set, all the classes from the .proto file are wrapped in a single |
||||
// outer class with the given name. This applies to both Proto1 |
||||
// (equivalent to the old "--one_java_file" option) and Proto2 (where |
||||
// a .proto always translates to a single class, but you may want to |
||||
// explicitly choose the class name). |
||||
optional string java_outer_classname = 8; |
||||
|
||||
// If set true, then the Java code generator will generate a separate .java |
||||
// file for each top-level message, enum, and service defined in the .proto |
||||
// file. Thus, these types will *not* be nested inside the outer class |
||||
// named by java_outer_classname. However, the outer class will still be |
||||
// generated to contain the file's getDescriptor() method as well as any |
||||
// top-level extensions defined in the file. |
||||
optional bool java_multiple_files = 10 [default=false]; |
||||
|
||||
// If set true, then the Java code generator will generate equals() and |
||||
// hashCode() methods for all messages defined in the .proto file. |
||||
// - In the full runtime, this is purely a speed optimization, as the |
||||
// AbstractMessage base class includes reflection-based implementations of |
||||
// these methods. |
||||
//- In the lite runtime, setting this option changes the semantics of |
||||
// equals() and hashCode() to more closely match those of the full runtime; |
||||
// the generated methods compute their results based on field values rather |
||||
// than object identity. (Implementations should not assume that hashcodes |
||||
// will be consistent across runtimes or versions of the protocol compiler.) |
||||
optional bool java_generate_equals_and_hash = 20 [default=false]; |
||||
|
||||
// If set true, then the Java2 code generator will generate code that |
||||
// throws an exception whenever an attempt is made to assign a non-UTF-8 |
||||
// byte sequence to a string field. |
||||
// Message reflection will do the same. |
||||
// However, an extension field still accepts non-UTF-8 byte sequences. |
||||
// This option has no effect on when used with the lite runtime. |
||||
optional bool java_string_check_utf8 = 27 [default=false]; |
||||
|
||||
|
||||
// Generated classes can be optimized for speed or code size. |
||||
enum OptimizeMode { |
||||
SPEED = 1; // Generate complete code for parsing, serialization, |
||||
// etc. |
||||
CODE_SIZE = 2; // Use ReflectionOps to implement these methods. |
||||
LITE_RUNTIME = 3; // Generate code using MessageLite and the lite runtime. |
||||
} |
||||
optional OptimizeMode optimize_for = 9 [default=SPEED]; |
||||
|
||||
// Sets the Go package where structs generated from this .proto will be |
||||
// placed. If omitted, the Go package will be derived from the following: |
||||
// - The basename of the package import path, if provided. |
||||
// - Otherwise, the package statement in the .proto file, if present. |
||||
// - Otherwise, the basename of the .proto file, without extension. |
||||
optional string go_package = 11; |
||||
|
||||
|
||||
|
||||
// Should generic services be generated in each language? "Generic" services |
||||
// are not specific to any particular RPC system. They are generated by the |
||||
// main code generators in each language (without additional plugins). |
||||
// Generic services were the only kind of service generation supported by |
||||
// early versions of google.protobuf. |
||||
// |
||||
// Generic services are now considered deprecated in favor of using plugins |
||||
// that generate code specific to your particular RPC system. Therefore, |
||||
// these default to false. Old code which depends on generic services should |
||||
// explicitly set them to true. |
||||
optional bool cc_generic_services = 16 [default=false]; |
||||
optional bool java_generic_services = 17 [default=false]; |
||||
optional bool py_generic_services = 18 [default=false]; |
||||
|
||||
// Is this file deprecated? |
||||
// Depending on the target platform, this can emit Deprecated annotations |
||||
// for everything in the file, or it will be completely ignored; in the very |
||||
// least, this is a formalization for deprecating files. |
||||
optional bool deprecated = 23 [default=false]; |
||||
|
||||
|
||||
// Enables the use of arenas for the proto messages in this file. This applies |
||||
// only to generated classes for C++. |
||||
optional bool cc_enable_arenas = 31 [default=false]; |
||||
|
||||
|
||||
// The parser stores options it doesn't recognize here. See above. |
||||
repeated UninterpretedOption uninterpreted_option = 999; |
||||
|
||||
// Clients can define custom options in extensions of this message. See above. |
||||
extensions 1000 to max; |
||||
} |
||||
|
||||
message MessageOptions { |
||||
// Set true to use the old proto1 MessageSet wire format for extensions. |
||||
// This is provided for backwards-compatibility with the MessageSet wire |
||||
// format. You should not use this for any other reason: It's less |
||||
// efficient, has fewer features, and is more complicated. |
||||
// |
||||
// The message must be defined exactly as follows: |
||||
// message Foo { |
||||
// option message_set_wire_format = true; |
||||
// extensions 4 to max; |
||||
// } |
||||
// Note that the message cannot have any defined fields; MessageSets only |
||||
// have extensions. |
||||
// |
||||
// All extensions of your type must be singular messages; e.g. they cannot |
||||
// be int32s, enums, or repeated messages. |
||||
// |
||||
// Because this is an option, the above two restrictions are not enforced by |
||||
// the protocol compiler. |
||||
optional bool message_set_wire_format = 1 [default=false]; |
||||
|
||||
// Disables the generation of the standard "descriptor()" accessor, which can |
||||
// conflict with a field of the same name. This is meant to make migration |
||||
// from proto1 easier; new code should avoid fields named "descriptor". |
||||
optional bool no_standard_descriptor_accessor = 2 [default=false]; |
||||
|
||||
// Is this message deprecated? |
||||
// Depending on the target platform, this can emit Deprecated annotations |
||||
// for the message, or it will be completely ignored; in the very least, |
||||
// this is a formalization for deprecating messages. |
||||
optional bool deprecated = 3 [default=false]; |
||||
|
||||
// Whether the message is an automatically generated map entry type for the |
||||
// maps field. |
||||
// |
||||
// For maps fields: |
||||
// map<KeyType, ValueType> map_field = 1; |
||||
// The parsed descriptor looks like: |
||||
// message MapFieldEntry { |
||||
// option map_entry = true; |
||||
// optional KeyType key = 1; |
||||
// optional ValueType value = 2; |
||||
// } |
||||
// repeated MapFieldEntry map_field = 1; |
||||
// |
||||
// Implementations may choose not to generate the map_entry=true message, but |
||||
// use a native map in the target language to hold the keys and values. |
||||
// The reflection APIs in such implementions still need to work as |
||||
// if the field is a repeated message field. |
||||
// |
||||
// NOTE: Do not set the option in .proto files. Always use the maps syntax |
||||
// instead. The option should only be implicitly set by the proto compiler |
||||
// parser. |
||||
optional bool map_entry = 7; |
||||
|
||||
// The parser stores options it doesn't recognize here. See above. |
||||
repeated UninterpretedOption uninterpreted_option = 999; |
||||
|
||||
// Clients can define custom options in extensions of this message. See above. |
||||
extensions 1000 to max; |
||||
} |
||||
|
||||
message FieldOptions { |
||||
// The ctype option instructs the C++ code generator to use a different |
||||
// representation of the field than it normally would. See the specific |
||||
// options below. This option is not yet implemented in the open source |
||||
// release -- sorry, we'll try to include it in a future version! |
||||
optional CType ctype = 1 [default = STRING]; |
||||
enum CType { |
||||
// Default mode. |
||||
STRING = 0; |
||||
|
||||
CORD = 1; |
||||
|
||||
STRING_PIECE = 2; |
||||
} |
||||
// The packed option can be enabled for repeated primitive fields to enable |
||||
// a more efficient representation on the wire. Rather than repeatedly |
||||
// writing the tag and type for each element, the entire array is encoded as |
||||
// a single length-delimited blob. |
||||
optional bool packed = 2; |
||||
|
||||
|
||||
|
||||
// Should this field be parsed lazily? Lazy applies only to message-type |
||||
// fields. It means that when the outer message is initially parsed, the |
||||
// inner message's contents will not be parsed but instead stored in encoded |
||||
// form. The inner message will actually be parsed when it is first accessed. |
||||
// |
||||
// This is only a hint. Implementations are free to choose whether to use |
||||
// eager or lazy parsing regardless of the value of this option. However, |
||||
// setting this option true suggests that the protocol author believes that |
||||
// using lazy parsing on this field is worth the additional bookkeeping |
||||
// overhead typically needed to implement it. |
||||
// |
||||
// This option does not affect the public interface of any generated code; |
||||
// all method signatures remain the same. Furthermore, thread-safety of the |
||||
// interface is not affected by this option; const methods remain safe to |
||||
// call from multiple threads concurrently, while non-const methods continue |
||||
// to require exclusive access. |
||||
// |
||||
// |
||||
// Note that implementations may choose not to check required fields within |
||||
// a lazy sub-message. That is, calling IsInitialized() on the outher message |
||||
// may return true even if the inner message has missing required fields. |
||||
// This is necessary because otherwise the inner message would have to be |
||||
// parsed in order to perform the check, defeating the purpose of lazy |
||||
// parsing. An implementation which chooses not to check required fields |
||||
// must be consistent about it. That is, for any particular sub-message, the |
||||
// implementation must either *always* check its required fields, or *never* |
||||
// check its required fields, regardless of whether or not the message has |
||||
// been parsed. |
||||
optional bool lazy = 5 [default=false]; |
||||
|
||||
// Is this field deprecated? |
||||
// Depending on the target platform, this can emit Deprecated annotations |
||||
// for accessors, or it will be completely ignored; in the very least, this |
||||
// is a formalization for deprecating fields. |
||||
optional bool deprecated = 3 [default=false]; |
||||
|
||||
// For Google-internal migration only. Do not use. |
||||
optional bool weak = 10 [default=false]; |
||||
|
||||
|
||||
|
||||
// The parser stores options it doesn't recognize here. See above. |
||||
repeated UninterpretedOption uninterpreted_option = 999; |
||||
|
||||
// Clients can define custom options in extensions of this message. See above. |
||||
extensions 1000 to max; |
||||
} |
||||
|
||||
message EnumOptions { |
||||
|
||||
// Set this option to true to allow mapping different tag names to the same |
||||
// value. |
||||
optional bool allow_alias = 2; |
||||
|
||||
// Is this enum deprecated? |
||||
// Depending on the target platform, this can emit Deprecated annotations |
||||
// for the enum, or it will be completely ignored; in the very least, this |
||||
// is a formalization for deprecating enums. |
||||
optional bool deprecated = 3 [default=false]; |
||||
|
||||
// The parser stores options it doesn't recognize here. See above. |
||||
repeated UninterpretedOption uninterpreted_option = 999; |
||||
|
||||
// Clients can define custom options in extensions of this message. See above. |
||||
extensions 1000 to max; |
||||
} |
||||
|
||||
message EnumValueOptions { |
||||
// Is this enum value deprecated? |
||||
// Depending on the target platform, this can emit Deprecated annotations |
||||
// for the enum value, or it will be completely ignored; in the very least, |
||||
// this is a formalization for deprecating enum values. |
||||
optional bool deprecated = 1 [default=false]; |
||||
|
||||
// The parser stores options it doesn't recognize here. See above. |
||||
repeated UninterpretedOption uninterpreted_option = 999; |
||||
|
||||
// Clients can define custom options in extensions of this message. See above. |
||||
extensions 1000 to max; |
||||
} |
||||
|
||||
message ServiceOptions { |
||||
|
||||
// Note: Field numbers 1 through 32 are reserved for Google's internal RPC |
||||
// framework. We apologize for hoarding these numbers to ourselves, but |
||||
// we were already using them long before we decided to release Protocol |
||||
// Buffers. |
||||
|
||||
// Is this service deprecated? |
||||
// Depending on the target platform, this can emit Deprecated annotations |
||||
// for the service, or it will be completely ignored; in the very least, |
||||
// this is a formalization for deprecating services. |
||||
optional bool deprecated = 33 [default=false]; |
||||
|
||||
// The parser stores options it doesn't recognize here. See above. |
||||
repeated UninterpretedOption uninterpreted_option = 999; |
||||
|
||||
// Clients can define custom options in extensions of this message. See above. |
||||
extensions 1000 to max; |
||||
} |
||||
|
||||
message MethodOptions { |
||||
|
||||
// Note: Field numbers 1 through 32 are reserved for Google's internal RPC |
||||
// framework. We apologize for hoarding these numbers to ourselves, but |
||||
// we were already using them long before we decided to release Protocol |
||||
// Buffers. |
||||
|
||||
// Is this method deprecated? |
||||
// Depending on the target platform, this can emit Deprecated annotations |
||||
// for the method, or it will be completely ignored; in the very least, |
||||
// this is a formalization for deprecating methods. |
||||
optional bool deprecated = 33 [default=false]; |
||||
|
||||
// The parser stores options it doesn't recognize here. See above. |
||||
repeated UninterpretedOption uninterpreted_option = 999; |
||||
|
||||
// Clients can define custom options in extensions of this message. See above. |
||||
extensions 1000 to max; |
||||
} |
||||
|
||||
|
||||
// A message representing a option the parser does not recognize. This only |
||||
// appears in options protos created by the compiler::Parser class. |
||||
// DescriptorPool resolves these when building Descriptor objects. Therefore, |
||||
// options protos in descriptor objects (e.g. returned by Descriptor::options(), |
||||
// or produced by Descriptor::CopyTo()) will never have UninterpretedOptions |
||||
// in them. |
||||
message UninterpretedOption { |
||||
// The name of the uninterpreted option. Each string represents a segment in |
||||
// a dot-separated name. is_extension is true iff a segment represents an |
||||
// extension (denoted with parentheses in options specs in .proto files). |
||||
// E.g.,{ ["foo", false], ["bar.baz", true], ["qux", false] } represents |
||||
// "foo.(bar.baz).qux". |
||||
message NamePart { |
||||
required string name_part = 1; |
||||
required bool is_extension = 2; |
||||
} |
||||
repeated NamePart name = 2; |
||||
|
||||
// The value of the uninterpreted option, in whatever type the tokenizer |
||||
// identified it as during parsing. Exactly one of these should be set. |
||||
optional string identifier_value = 3; |
||||
optional uint64 positive_int_value = 4; |
||||
optional int64 negative_int_value = 5; |
||||
optional double double_value = 6; |
||||
optional bytes string_value = 7; |
||||
optional string aggregate_value = 8; |
||||
} |
||||
|
||||
// =================================================================== |
||||
// Optional source code info |
||||
|
||||
// Encapsulates information about the original source file from which a |
||||
// FileDescriptorProto was generated. |
||||
message SourceCodeInfo { |
||||
// A Location identifies a piece of source code in a .proto file which |
||||
// corresponds to a particular definition. This information is intended |
||||
// to be useful to IDEs, code indexers, documentation generators, and similar |
||||
// tools. |
||||
// |
||||
// For example, say we have a file like: |
||||
// message Foo { |
||||
// optional string foo = 1; |
||||
// } |
||||
// Let's look at just the field definition: |
||||
// optional string foo = 1; |
||||
// ^ ^^ ^^ ^ ^^^ |
||||
// a bc de f ghi |
||||
// We have the following locations: |
||||
// span path represents |
||||
// [a,i) [ 4, 0, 2, 0 ] The whole field definition. |
||||
// [a,b) [ 4, 0, 2, 0, 4 ] The label (optional). |
||||
// [c,d) [ 4, 0, 2, 0, 5 ] The type (string). |
||||
// [e,f) [ 4, 0, 2, 0, 1 ] The name (foo). |
||||
// [g,h) [ 4, 0, 2, 0, 3 ] The number (1). |
||||
// |
||||
// Notes: |
||||
// - A location may refer to a repeated field itself (i.e. not to any |
||||
// particular index within it). This is used whenever a set of elements are |
||||
// logically enclosed in a single code segment. For example, an entire |
||||
// extend block (possibly containing multiple extension definitions) will |
||||
// have an outer location whose path refers to the "extensions" repeated |
||||
// field without an index. |
||||
// - Multiple locations may have the same path. This happens when a single |
||||
// logical declaration is spread out across multiple places. The most |
||||
// obvious example is the "extend" block again -- there may be multiple |
||||
// extend blocks in the same scope, each of which will have the same path. |
||||
// - A location's span is not always a subset of its parent's span. For |
||||
// example, the "extendee" of an extension declaration appears at the |
||||
// beginning of the "extend" block and is shared by all extensions within |
||||
// the block. |
||||
// - Just because a location's span is a subset of some other location's span |
||||
// does not mean that it is a descendent. For example, a "group" defines |
||||
// both a type and a field in a single declaration. Thus, the locations |
||||
// corresponding to the type and field and their components will overlap. |
||||
// - Code which tries to interpret locations should probably be designed to |
||||
// ignore those that it doesn't understand, as more types of locations could |
||||
// be recorded in the future. |
||||
repeated Location location = 1; |
||||
message Location { |
||||
// Identifies which part of the FileDescriptorProto was defined at this |
||||
// location. |
||||
// |
||||
// Each element is a field number or an index. They form a path from |
||||
// the root FileDescriptorProto to the place where the definition. For |
||||
// example, this path: |
||||
// [ 4, 3, 2, 7, 1 ] |
||||
// refers to: |
||||
// file.message_type(3) // 4, 3 |
||||
// .field(7) // 2, 7 |
||||
// .name() // 1 |
||||
// This is because FileDescriptorProto.message_type has field number 4: |
||||
// repeated DescriptorProto message_type = 4; |
||||
// and DescriptorProto.field has field number 2: |
||||
// repeated FieldDescriptorProto field = 2; |
||||
// and FieldDescriptorProto.name has field number 1: |
||||
// optional string name = 1; |
||||
// |
||||
// Thus, the above path gives the location of a field name. If we removed |
||||
// the last element: |
||||
// [ 4, 3, 2, 7 ] |
||||
// this path refers to the whole field declaration (from the beginning |
||||
// of the label to the terminating semicolon). |
||||
repeated int32 path = 1 [packed=true]; |
||||
|
||||
// Always has exactly three or four elements: start line, start column, |
||||
// end line (optional, otherwise assumed same as start line), end column. |
||||
// These are packed into a single field for efficiency. Note that line |
||||
// and column numbers are zero-based -- typically you will want to add |
||||
// 1 to each before displaying to a user. |
||||
repeated int32 span = 2 [packed=true]; |
||||
|
||||
// If this SourceCodeInfo represents a complete declaration, these are any |
||||
// comments appearing before and after the declaration which appear to be |
||||
// attached to the declaration. |
||||
// |
||||
// A series of line comments appearing on consecutive lines, with no other |
||||
// tokens appearing on those lines, will be treated as a single comment. |
||||
// |
||||
// Only the comment content is provided; comment markers (e.g. //) are |
||||
// stripped out. For block comments, leading whitespace and an asterisk |
||||
// will be stripped from the beginning of each line other than the first. |
||||
// Newlines are included in the output. |
||||
// |
||||
// Examples: |
||||
// |
||||
// optional int32 foo = 1; // Comment attached to foo. |
||||
// // Comment attached to bar. |
||||
// optional int32 bar = 2; |
||||
// |
||||
// optional string baz = 3; |
||||
// // Comment attached to baz. |
||||
// // Another line attached to baz. |
||||
// |
||||
// // Comment attached to qux. |
||||
// // |
||||
// // Another line attached to qux. |
||||
// optional double qux = 4; |
||||
// |
||||
// optional string corge = 5; |
||||
// /* Block comment attached |
||||
// * to corge. Leading asterisks |
||||
// * will be removed. */ |
||||
// /* Block comment attached to |
||||
// * grault. */ |
||||
// optional int32 grault = 6; |
||||
optional string leading_comments = 3; |
||||
optional string trailing_comments = 4; |
||||
} |
||||
} |
@ -1,98 +0,0 @@ |
||||
// Custom options for defining: |
||||
// - Maximum size of string/bytes |
||||
// - Maximum number of elements in array |
||||
// |
||||
// These are used by nanopb to generate statically allocable structures |
||||
// for memory-limited environments. |
||||
|
||||
syntax = "proto2"; |
||||
import "google/protobuf/descriptor.proto"; |
||||
|
||||
option java_package = "fi.kapsi.koti.jpa.nanopb"; |
||||
|
||||
enum FieldType { |
||||
FT_DEFAULT = 0; // Automatically decide field type, generate static field if possible. |
||||
FT_CALLBACK = 1; // Always generate a callback field. |
||||
FT_POINTER = 4; // Always generate a dynamically allocated field. |
||||
FT_STATIC = 2; // Generate a static field or raise an exception if not possible. |
||||
FT_IGNORE = 3; // Ignore the field completely. |
||||
FT_INLINE = 5; // Always generate an inline array of fixed size. |
||||
} |
||||
|
||||
enum IntSize { |
||||
IS_DEFAULT = 0; // Default, 32/64bit based on type in .proto |
||||
IS_8 = 8; |
||||
IS_16 = 16; |
||||
IS_32 = 32; |
||||
IS_64 = 64; |
||||
} |
||||
|
||||
// This is the inner options message, which basically defines options for |
||||
// a field. When it is used in message or file scope, it applies to all |
||||
// fields. |
||||
message NanoPBOptions { |
||||
// Allocated size for 'bytes' and 'string' fields. |
||||
optional int32 max_size = 1; |
||||
|
||||
// Allocated number of entries in arrays ('repeated' fields) |
||||
optional int32 max_count = 2; |
||||
|
||||
// Size of integer fields. Can save some memory if you don't need |
||||
// full 32 bits for the value. |
||||
optional IntSize int_size = 7 [default = IS_DEFAULT]; |
||||
|
||||
// Force type of field (callback or static allocation) |
||||
optional FieldType type = 3 [default = FT_DEFAULT]; |
||||
|
||||
// Use long names for enums, i.e. EnumName_EnumValue. |
||||
optional bool long_names = 4 [default = true]; |
||||
|
||||
// Add 'packed' attribute to generated structs. |
||||
// Note: this cannot be used on CPUs that break on unaligned |
||||
// accesses to variables. |
||||
optional bool packed_struct = 5 [default = false]; |
||||
|
||||
// Add 'packed' attribute to generated enums. |
||||
optional bool packed_enum = 10 [default = false]; |
||||
|
||||
// Skip this message |
||||
optional bool skip_message = 6 [default = false]; |
||||
|
||||
// Generate oneof fields as normal optional fields instead of union. |
||||
optional bool no_unions = 8 [default = false]; |
||||
|
||||
// integer type tag for a message |
||||
optional uint32 msgid = 9; |
||||
|
||||
// decode oneof as anonymous union |
||||
optional bool anonymous_oneof = 11 [default = false]; |
||||
} |
||||
|
||||
// Extensions to protoc 'Descriptor' type in order to define options |
||||
// inside a .proto file. |
||||
// |
||||
// Protocol Buffers extension number registry |
||||
// -------------------------------- |
||||
// Project: Nanopb |
||||
// Contact: Petteri Aimonen <jpa@kapsi.fi> |
||||
// Web site: http://kapsi.fi/~jpa/nanopb |
||||
// Extensions: 1010 (all types) |
||||
// -------------------------------- |
||||
|
||||
extend google.protobuf.FileOptions { |
||||
optional NanoPBOptions nanopb_fileopt = 1010; |
||||
} |
||||
|
||||
extend google.protobuf.MessageOptions { |
||||
optional NanoPBOptions nanopb_msgopt = 1010; |
||||
} |
||||
|
||||
extend google.protobuf.EnumOptions { |
||||
optional NanoPBOptions nanopb_enumopt = 1010; |
||||
} |
||||
|
||||
extend google.protobuf.FieldOptions { |
||||
optional NanoPBOptions nanopb = 1010; |
||||
} |
||||
|
||||
|
@ -1,148 +0,0 @@ |
||||
// Protocol Buffers - Google's data interchange format |
||||
// Copyright 2008 Google Inc. All rights reserved. |
||||
// https://developers.google.com/protocol-buffers/ |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// Author: kenton@google.com (Kenton Varda) |
||||
// |
||||
// WARNING: The plugin interface is currently EXPERIMENTAL and is subject to |
||||
// change. |
||||
// |
||||
// protoc (aka the Protocol Compiler) can be extended via plugins. A plugin is |
||||
// just a program that reads a CodeGeneratorRequest from stdin and writes a |
||||
// CodeGeneratorResponse to stdout. |
||||
// |
||||
// Plugins written using C++ can use google/protobuf/compiler/plugin.h instead |
||||
// of dealing with the raw protocol defined here. |
||||
// |
||||
// A plugin executable needs only to be placed somewhere in the path. The |
||||
// plugin should be named "protoc-gen-$NAME", and will then be used when the |
||||
// flag "--${NAME}_out" is passed to protoc. |
||||
|
||||
syntax = "proto2"; |
||||
package google.protobuf.compiler; |
||||
option java_package = "com.google.protobuf.compiler"; |
||||
option java_outer_classname = "PluginProtos"; |
||||
|
||||
import "google/protobuf/descriptor.proto"; |
||||
|
||||
// An encoded CodeGeneratorRequest is written to the plugin's stdin. |
||||
message CodeGeneratorRequest { |
||||
// The .proto files that were explicitly listed on the command-line. The |
||||
// code generator should generate code only for these files. Each file's |
||||
// descriptor will be included in proto_file, below. |
||||
repeated string file_to_generate = 1; |
||||
|
||||
// The generator parameter passed on the command-line. |
||||
optional string parameter = 2; |
||||
|
||||
// FileDescriptorProtos for all files in files_to_generate and everything |
||||
// they import. The files will appear in topological order, so each file |
||||
// appears before any file that imports it. |
||||
// |
||||
// protoc guarantees that all proto_files will be written after |
||||
// the fields above, even though this is not technically guaranteed by the |
||||
// protobuf wire format. This theoretically could allow a plugin to stream |
||||
// in the FileDescriptorProtos and handle them one by one rather than read |
||||
// the entire set into memory at once. However, as of this writing, this |
||||
// is not similarly optimized on protoc's end -- it will store all fields in |
||||
// memory at once before sending them to the plugin. |
||||
repeated FileDescriptorProto proto_file = 15; |
||||
} |
||||
|
||||
// The plugin writes an encoded CodeGeneratorResponse to stdout. |
||||
message CodeGeneratorResponse { |
||||
// Error message. If non-empty, code generation failed. The plugin process |
||||
// should exit with status code zero even if it reports an error in this way. |
||||
// |
||||
// This should be used to indicate errors in .proto files which prevent the |
||||
// code generator from generating correct code. Errors which indicate a |
||||
// problem in protoc itself -- such as the input CodeGeneratorRequest being |
||||
// unparseable -- should be reported by writing a message to stderr and |
||||
// exiting with a non-zero status code. |
||||
optional string error = 1; |
||||
|
||||
// Represents a single generated file. |
||||
message File { |
||||
// The file name, relative to the output directory. The name must not |
||||
// contain "." or ".." components and must be relative, not be absolute (so, |
||||
// the file cannot lie outside the output directory). "/" must be used as |
||||
// the path separator, not "\". |
||||
// |
||||
// If the name is omitted, the content will be appended to the previous |
||||
// file. This allows the generator to break large files into small chunks, |
||||
// and allows the generated text to be streamed back to protoc so that large |
||||
// files need not reside completely in memory at one time. Note that as of |
||||
// this writing protoc does not optimize for this -- it will read the entire |
||||
// CodeGeneratorResponse before writing files to disk. |
||||
optional string name = 1; |
||||
|
||||
// If non-empty, indicates that the named file should already exist, and the |
||||
// content here is to be inserted into that file at a defined insertion |
||||
// point. This feature allows a code generator to extend the output |
||||
// produced by another code generator. The original generator may provide |
||||
// insertion points by placing special annotations in the file that look |
||||
// like: |
||||
// @@protoc_insertion_point(NAME) |
||||
// The annotation can have arbitrary text before and after it on the line, |
||||
// which allows it to be placed in a comment. NAME should be replaced with |
||||
// an identifier naming the point -- this is what other generators will use |
||||
// as the insertion_point. Code inserted at this point will be placed |
||||
// immediately above the line containing the insertion point (thus multiple |
||||
// insertions to the same point will come out in the order they were added). |
||||
// The double-@ is intended to make it unlikely that the generated code |
||||
// could contain things that look like insertion points by accident. |
||||
// |
||||
// For example, the C++ code generator places the following line in the |
||||
// .pb.h files that it generates: |
||||
// // @@protoc_insertion_point(namespace_scope) |
||||
// This line appears within the scope of the file's package namespace, but |
||||
// outside of any particular class. Another plugin can then specify the |
||||
// insertion_point "namespace_scope" to generate additional classes or |
||||
// other declarations that should be placed in this scope. |
||||
// |
||||
// Note that if the line containing the insertion point begins with |
||||
// whitespace, the same whitespace will be added to every line of the |
||||
// inserted text. This is useful for languages like Python, where |
||||
// indentation matters. In these languages, the insertion point comment |
||||
// should be indented the same amount as any inserted code will need to be |
||||
// in order to work correctly in that context. |
||||
// |
||||
// The code generator that generates the initial file and the one which |
||||
// inserts into it must both run as part of a single invocation of protoc. |
||||
// Code generators are executed in the order in which they appear on the |
||||
// command line. |
||||
// |
||||
// If |insertion_point| is present, |name| must also be present. |
||||
optional string insertion_point = 2; |
||||
|
||||
// The file contents. |
||||
optional string content = 15; |
||||
} |
||||
repeated File file = 15; |
||||
} |
@ -1,13 +0,0 @@ |
||||
#!/bin/sh |
||||
|
||||
# This file is used to invoke nanopb_generator.py as a plugin |
||||
# to protoc on Linux and other *nix-style systems. |
||||
# Use it like this: |
||||
# protoc --plugin=nanopb=..../protoc-gen-nanopb --nanopb_out=dir foo.proto |
||||
# |
||||
# Note that if you use the binary package of nanopb, the protoc |
||||
# path is already set up properly and there is no need to give |
||||
# --plugin= on the command line. |
||||
|
||||
MYPATH=$(dirname "$0") |
||||
exec "$MYPATH/nanopb_generator.py" --protoc-plugin |
@ -1,12 +0,0 @@ |
||||
@echo off |
||||
:: This file is used to invoke nanopb_generator.py as a plugin |
||||
:: to protoc on Windows. |
||||
:: Use it like this: |
||||
:: protoc --plugin=nanopb=..../protoc-gen-nanopb.bat --nanopb_out=dir foo.proto |
||||
:: |
||||
:: Note that if you use the binary package of nanopb, the protoc |
||||
:: path is already set up properly and there is no need to give |
||||
:: --plugin= on the command line. |
||||
|
||||
set mydir=%~dp0 |
||||
python "%mydir%\nanopb_generator.py" --protoc-plugin |
@ -1,22 +0,0 @@ |
||||
{ |
||||
"name": "Nanopb", |
||||
"keywords": "protocol buffers, protobuf, google", |
||||
"description": "Nanopb is a plain-C implementation of Google's Protocol Buffers data format. It is targeted at 32 bit microcontrollers, but is also fit for other embedded systems with tight (2-10 kB ROM, <1 kB RAM) memory constraints.", |
||||
"repository": { |
||||
"type": "git", |
||||
"url": "https://github.com/nanopb/nanopb.git" |
||||
}, |
||||
"authors": [{ |
||||
"name": "Petteri Aimonen", |
||||
"email": "jpa@nanopb.mail.kapsi.fi", |
||||
"url": "http://koti.kapsi.fi/jpa/nanopb/" |
||||
}], |
||||
"include": [ |
||||
"*.c", |
||||
"*.cpp", |
||||
"*.h" |
||||
], |
||||
"examples": "examples/*/*.c", |
||||
"frameworks": "*", |
||||
"platforms": "*" |
||||
} |
@ -1,579 +0,0 @@ |
||||
/* Common parts of the nanopb library. Most of these are quite low-level
|
||||
* stuff. For the high-level interface, see pb_encode.h and pb_decode.h. |
||||
*/ |
||||
|
||||
#ifndef PB_H_INCLUDED |
||||
#define PB_H_INCLUDED |
||||
|
||||
/*****************************************************************
|
||||
* Nanopb compilation time options. You can change these here by * |
||||
* uncommenting the lines, or on the compiler command line. * |
||||
*****************************************************************/ |
||||
|
||||
/* Enable support for dynamically allocated fields */ |
||||
/* #define PB_ENABLE_MALLOC 1 */ |
||||
|
||||
/* Define this if your CPU / compiler combination does not support
|
||||
* unaligned memory access to packed structures. */ |
||||
/* #define PB_NO_PACKED_STRUCTS 1 */ |
||||
|
||||
/* Increase the number of required fields that are tracked.
|
||||
* A compiler warning will tell if you need this. */ |
||||
/* #define PB_MAX_REQUIRED_FIELDS 256 */ |
||||
|
||||
/* Add support for tag numbers > 255 and fields larger than 255 bytes. */ |
||||
/* #define PB_FIELD_16BIT 1 */ |
||||
|
||||
/* Add support for tag numbers > 65536 and fields larger than 65536 bytes. */ |
||||
/* #define PB_FIELD_32BIT 1 */
|
||||
|
||||
/* Disable support for error messages in order to save some code space. */ |
||||
/* #define PB_NO_ERRMSG 1 */ |
||||
|
||||
/* Disable support for custom streams (support only memory buffers). */ |
||||
/* #define PB_BUFFER_ONLY 1 */ |
||||
|
||||
/* Switch back to the old-style callback function signature.
|
||||
* This was the default until nanopb-0.2.1. */ |
||||
/* #define PB_OLD_CALLBACK_STYLE */ |
||||
|
||||
|
||||
/******************************************************************
|
||||
* You usually don't need to change anything below this line. * |
||||
* Feel free to look around and use the defined macros, though. * |
||||
******************************************************************/ |
||||
|
||||
|
||||
/* Version of the nanopb library. Just in case you want to check it in
|
||||
* your own program. */ |
||||
#define NANOPB_VERSION nanopb-0.3.7-dev |
||||
|
||||
/* Include all the system headers needed by nanopb. You will need the
|
||||
* definitions of the following: |
||||
* - strlen, memcpy, memset functions |
||||
* - [u]int_least8_t, uint_fast8_t, [u]int_least16_t, [u]int32_t, [u]int64_t |
||||
* - size_t |
||||
* - bool |
||||
* |
||||
* If you don't have the standard header files, you can instead provide |
||||
* a custom header that defines or includes all this. In that case, |
||||
* define PB_SYSTEM_HEADER to the path of this file. |
||||
*/ |
||||
#ifdef PB_SYSTEM_HEADER |
||||
#include PB_SYSTEM_HEADER |
||||
#else |
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
#include <stdbool.h> |
||||
#include <string.h> |
||||
|
||||
#ifdef PB_ENABLE_MALLOC |
||||
#include <stdlib.h> |
||||
#endif |
||||
#endif |
||||
|
||||
/* Macro for defining packed structures (compiler dependent).
|
||||
* This just reduces memory requirements, but is not required. |
||||
*/ |
||||
#if defined(PB_NO_PACKED_STRUCTS) |
||||
/* Disable struct packing */ |
||||
# define PB_PACKED_STRUCT_START |
||||
# define PB_PACKED_STRUCT_END |
||||
# define pb_packed |
||||
#elif defined(__GNUC__) || defined(__clang__) |
||||
/* For GCC and clang */ |
||||
# define PB_PACKED_STRUCT_START |
||||
# define PB_PACKED_STRUCT_END |
||||
# define pb_packed __attribute__((packed)) |
||||
#elif defined(__ICCARM__) || defined(__CC_ARM) |
||||
/* For IAR ARM and Keil MDK-ARM compilers */ |
||||
# define PB_PACKED_STRUCT_START _Pragma("pack(push, 1)") |
||||
# define PB_PACKED_STRUCT_END _Pragma("pack(pop)") |
||||
# define pb_packed |
||||
#elif defined(_MSC_VER) && (_MSC_VER >= 1500) |
||||
/* For Microsoft Visual C++ */ |
||||
# define PB_PACKED_STRUCT_START __pragma(pack(push, 1)) |
||||
# define PB_PACKED_STRUCT_END __pragma(pack(pop)) |
||||
# define pb_packed |
||||
#else |
||||
/* Unknown compiler */ |
||||
# define PB_PACKED_STRUCT_START |
||||
# define PB_PACKED_STRUCT_END |
||||
# define pb_packed |
||||
#endif |
||||
|
||||
/* Handly macro for suppressing unreferenced-parameter compiler warnings. */ |
||||
#ifndef PB_UNUSED |
||||
#define PB_UNUSED(x) (void)(x) |
||||
#endif |
||||
|
||||
/* Compile-time assertion, used for checking compatible compilation options.
|
||||
* If this does not work properly on your compiler, use |
||||
* #define PB_NO_STATIC_ASSERT to disable it. |
||||
* |
||||
* But before doing that, check carefully the error message / place where it |
||||
* comes from to see if the error has a real cause. Unfortunately the error |
||||
* message is not always very clear to read, but you can see the reason better |
||||
* in the place where the PB_STATIC_ASSERT macro was called. |
||||
*/ |
||||
#ifndef PB_NO_STATIC_ASSERT |
||||
#ifndef PB_STATIC_ASSERT |
||||
#define PB_STATIC_ASSERT(COND,MSG) typedef char PB_STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1]; |
||||
#define PB_STATIC_ASSERT_MSG(MSG, LINE, COUNTER) PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) |
||||
#define PB_STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) pb_static_assertion_##MSG##LINE##COUNTER |
||||
#endif |
||||
#else |
||||
#define PB_STATIC_ASSERT(COND,MSG) |
||||
#endif |
||||
|
||||
/* Number of required fields to keep track of. */ |
||||
#ifndef PB_MAX_REQUIRED_FIELDS |
||||
#define PB_MAX_REQUIRED_FIELDS 64 |
||||
#endif |
||||
|
||||
#if PB_MAX_REQUIRED_FIELDS < 64 |
||||
#error You should not lower PB_MAX_REQUIRED_FIELDS from the default value (64). |
||||
#endif |
||||
|
||||
/* List of possible field types. These are used in the autogenerated code.
|
||||
* Least-significant 4 bits tell the scalar type |
||||
* Most-significant 4 bits specify repeated/required/packed etc. |
||||
*/ |
||||
|
||||
typedef uint_least8_t pb_type_t; |
||||
|
||||
/**** Field data types ****/ |
||||
|
||||
/* Numeric types */ |
||||
#define PB_LTYPE_VARINT 0x00 /* int32, int64, enum, bool */ |
||||
#define PB_LTYPE_UVARINT 0x01 /* uint32, uint64 */ |
||||
#define PB_LTYPE_SVARINT 0x02 /* sint32, sint64 */ |
||||
#define PB_LTYPE_FIXED32 0x03 /* fixed32, sfixed32, float */ |
||||
#define PB_LTYPE_FIXED64 0x04 /* fixed64, sfixed64, double */ |
||||
|
||||
/* Marker for last packable field type. */ |
||||
#define PB_LTYPE_LAST_PACKABLE 0x04 |
||||
|
||||
/* Byte array with pre-allocated buffer.
|
||||
* data_size is the length of the allocated PB_BYTES_ARRAY structure. */ |
||||
#define PB_LTYPE_BYTES 0x05 |
||||
|
||||
/* String with pre-allocated buffer.
|
||||
* data_size is the maximum length. */ |
||||
#define PB_LTYPE_STRING 0x06 |
||||
|
||||
/* Submessage
|
||||
* submsg_fields is pointer to field descriptions */ |
||||
#define PB_LTYPE_SUBMESSAGE 0x07 |
||||
|
||||
/* Extension pseudo-field
|
||||
* The field contains a pointer to pb_extension_t */ |
||||
#define PB_LTYPE_EXTENSION 0x08 |
||||
|
||||
/* Byte array with inline, pre-allocated byffer.
|
||||
* data_size is the length of the inline, allocated buffer. |
||||
* This differs from PB_LTYPE_BYTES by defining the element as |
||||
* pb_byte_t[data_size] rather than pb_bytes_array_t. */ |
||||
#define PB_LTYPE_FIXED_LENGTH_BYTES 0x09 |
||||
|
||||
/* Number of declared LTYPES */ |
||||
#define PB_LTYPES_COUNT 0x0A |
||||
#define PB_LTYPE_MASK 0x0F |
||||
|
||||
/**** Field repetition rules ****/ |
||||
|
||||
#define PB_HTYPE_REQUIRED 0x00 |
||||
#define PB_HTYPE_OPTIONAL 0x10 |
||||
#define PB_HTYPE_REPEATED 0x20 |
||||
#define PB_HTYPE_ONEOF 0x30 |
||||
#define PB_HTYPE_MASK 0x30 |
||||
|
||||
/**** Field allocation types ****/ |
||||
|
||||
#define PB_ATYPE_STATIC 0x00 |
||||
#define PB_ATYPE_POINTER 0x80 |
||||
#define PB_ATYPE_CALLBACK 0x40 |
||||
#define PB_ATYPE_MASK 0xC0 |
||||
|
||||
#define PB_ATYPE(x) ((x) & PB_ATYPE_MASK) |
||||
#define PB_HTYPE(x) ((x) & PB_HTYPE_MASK) |
||||
#define PB_LTYPE(x) ((x) & PB_LTYPE_MASK) |
||||
|
||||
/* Data type used for storing sizes of struct fields
|
||||
* and array counts. |
||||
*/ |
||||
#if defined(PB_FIELD_32BIT) |
||||
typedef uint32_t pb_size_t; |
||||
typedef int32_t pb_ssize_t; |
||||
#elif defined(PB_FIELD_16BIT) |
||||
typedef uint_least16_t pb_size_t; |
||||
typedef int_least16_t pb_ssize_t; |
||||
#else |
||||
typedef uint_least8_t pb_size_t; |
||||
typedef int_least8_t pb_ssize_t; |
||||
#endif |
||||
#define PB_SIZE_MAX ((pb_size_t)-1) |
||||
|
||||
/* Data type for storing encoded data and other byte streams.
|
||||
* This typedef exists to support platforms where uint8_t does not exist. |
||||
* You can regard it as equivalent on uint8_t on other platforms. |
||||
*/ |
||||
typedef uint_least8_t pb_byte_t; |
||||
|
||||
/* This structure is used in auto-generated constants
|
||||
* to specify struct fields. |
||||
* You can change field sizes if you need structures |
||||
* larger than 256 bytes or field tags larger than 256. |
||||
* The compiler should complain if your .proto has such |
||||
* structures. Fix that by defining PB_FIELD_16BIT or |
||||
* PB_FIELD_32BIT. |
||||
*/ |
||||
PB_PACKED_STRUCT_START |
||||
typedef struct pb_field_s pb_field_t; |
||||
struct pb_field_s { |
||||
pb_size_t tag; |
||||
pb_type_t type; |
||||
pb_size_t data_offset; /* Offset of field data, relative to previous field. */ |
||||
pb_ssize_t size_offset; /* Offset of array size or has-boolean, relative to data */ |
||||
pb_size_t data_size; /* Data size in bytes for a single item */ |
||||
pb_size_t array_size; /* Maximum number of entries in array */ |
||||
|
||||
/* Field definitions for submessage
|
||||
* OR default value for all other non-array, non-callback types |
||||
* If null, then field will zeroed. */ |
||||
const void *ptr; |
||||
} pb_packed; |
||||
PB_PACKED_STRUCT_END |
||||
|
||||
/* Make sure that the standard integer types are of the expected sizes.
|
||||
* Otherwise fixed32/fixed64 fields can break. |
||||
* |
||||
* If you get errors here, it probably means that your stdint.h is not |
||||
* correct for your platform. |
||||
*/ |
||||
PB_STATIC_ASSERT(sizeof(int64_t) == 2 * sizeof(int32_t), INT64_T_WRONG_SIZE) |
||||
PB_STATIC_ASSERT(sizeof(uint64_t) == 2 * sizeof(uint32_t), UINT64_T_WRONG_SIZE) |
||||
|
||||
/* This structure is used for 'bytes' arrays.
|
||||
* It has the number of bytes in the beginning, and after that an array. |
||||
* Note that actual structs used will have a different length of bytes array. |
||||
*/ |
||||
#define PB_BYTES_ARRAY_T(n) struct { pb_size_t size; pb_byte_t bytes[n]; } |
||||
#define PB_BYTES_ARRAY_T_ALLOCSIZE(n) ((size_t)n + offsetof(pb_bytes_array_t, bytes)) |
||||
|
||||
struct pb_bytes_array_s { |
||||
pb_size_t size; |
||||
pb_byte_t bytes[1]; |
||||
}; |
||||
typedef struct pb_bytes_array_s pb_bytes_array_t; |
||||
|
||||
/* This structure is used for giving the callback function.
|
||||
* It is stored in the message structure and filled in by the method that |
||||
* calls pb_decode. |
||||
* |
||||
* The decoding callback will be given a limited-length stream |
||||
* If the wire type was string, the length is the length of the string. |
||||
* If the wire type was a varint/fixed32/fixed64, the length is the length |
||||
* of the actual value. |
||||
* The function may be called multiple times (especially for repeated types, |
||||
* but also otherwise if the message happens to contain the field multiple |
||||
* times.) |
||||
* |
||||
* The encoding callback will receive the actual output stream. |
||||
* It should write all the data in one call, including the field tag and |
||||
* wire type. It can write multiple fields. |
||||
* |
||||
* The callback can be null if you want to skip a field. |
||||
*/ |
||||
typedef struct pb_istream_s pb_istream_t; |
||||
typedef struct pb_ostream_s pb_ostream_t; |
||||
typedef struct pb_callback_s pb_callback_t; |
||||
struct pb_callback_s { |
||||
#ifdef PB_OLD_CALLBACK_STYLE |
||||
/* Deprecated since nanopb-0.2.1 */ |
||||
union { |
||||
bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg); |
||||
bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg); |
||||
} funcs; |
||||
#else |
||||
/* New function signature, which allows modifying arg contents in callback. */ |
||||
union { |
||||
bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void **arg); |
||||
bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg); |
||||
} funcs; |
||||
#endif |
||||
|
||||
/* Free arg for use by callback */ |
||||
void *arg; |
||||
}; |
||||
|
||||
/* Wire types. Library user needs these only in encoder callbacks. */ |
||||
typedef enum { |
||||
PB_WT_VARINT = 0, |
||||
PB_WT_64BIT = 1, |
||||
PB_WT_STRING = 2, |
||||
PB_WT_32BIT = 5 |
||||
} pb_wire_type_t; |
||||
|
||||
/* Structure for defining the handling of unknown/extension fields.
|
||||
* Usually the pb_extension_type_t structure is automatically generated, |
||||
* while the pb_extension_t structure is created by the user. However, |
||||
* if you want to catch all unknown fields, you can also create a custom |
||||
* pb_extension_type_t with your own callback. |
||||
*/ |
||||
typedef struct pb_extension_type_s pb_extension_type_t; |
||||
typedef struct pb_extension_s pb_extension_t; |
||||
struct pb_extension_type_s { |
||||
/* Called for each unknown field in the message.
|
||||
* If you handle the field, read off all of its data and return true. |
||||
* If you do not handle the field, do not read anything and return true. |
||||
* If you run into an error, return false. |
||||
* Set to NULL for default handler. |
||||
*/ |
||||
bool (*decode)(pb_istream_t *stream, pb_extension_t *extension, |
||||
uint32_t tag, pb_wire_type_t wire_type); |
||||
|
||||
/* Called once after all regular fields have been encoded.
|
||||
* If you have something to write, do so and return true. |
||||
* If you do not have anything to write, just return true. |
||||
* If you run into an error, return false. |
||||
* Set to NULL for default handler. |
||||
*/ |
||||
bool (*encode)(pb_ostream_t *stream, const pb_extension_t *extension); |
||||
|
||||
/* Free field for use by the callback. */ |
||||
const void *arg; |
||||
}; |
||||
|
||||
struct pb_extension_s { |
||||
/* Type describing the extension field. Usually you'll initialize
|
||||
* this to a pointer to the automatically generated structure. */ |
||||
const pb_extension_type_t *type; |
||||
|
||||
/* Destination for the decoded data. This must match the datatype
|
||||
* of the extension field. */ |
||||
void *dest; |
||||
|
||||
/* Pointer to the next extension handler, or NULL.
|
||||
* If this extension does not match a field, the next handler is |
||||
* automatically called. */ |
||||
pb_extension_t *next; |
||||
|
||||
/* The decoder sets this to true if the extension was found.
|
||||
* Ignored for encoding. */ |
||||
bool found; |
||||
}; |
||||
|
||||
/* Memory allocation functions to use. You can define pb_realloc and
|
||||
* pb_free to custom functions if you want. */ |
||||
#ifdef PB_ENABLE_MALLOC |
||||
# ifndef pb_realloc |
||||
# define pb_realloc(ptr, size) realloc(ptr, size) |
||||
# endif |
||||
# ifndef pb_free |
||||
# define pb_free(ptr) free(ptr) |
||||
# endif |
||||
#endif |
||||
|
||||
/* This is used to inform about need to regenerate .pb.h/.pb.c files. */ |
||||
#define PB_PROTO_HEADER_VERSION 30 |
||||
|
||||
/* These macros are used to declare pb_field_t's in the constant array. */ |
||||
/* Size of a structure member, in bytes. */ |
||||
#define pb_membersize(st, m) (sizeof ((st*)0)->m) |
||||
/* Number of entries in an array. */ |
||||
#define pb_arraysize(st, m) (pb_membersize(st, m) / pb_membersize(st, m[0])) |
||||
/* Delta from start of one member to the start of another member. */ |
||||
#define pb_delta(st, m1, m2) ((int)offsetof(st, m1) - (int)offsetof(st, m2)) |
||||
/* Marks the end of the field list */ |
||||
#define PB_LAST_FIELD {0,(pb_type_t) 0,0,0,0,0,0} |
||||
|
||||
/* Macros for filling in the data_offset field */ |
||||
/* data_offset for first field in a message */ |
||||
#define PB_DATAOFFSET_FIRST(st, m1, m2) (offsetof(st, m1)) |
||||
/* data_offset for subsequent fields */ |
||||
#define PB_DATAOFFSET_OTHER(st, m1, m2) (offsetof(st, m1) - offsetof(st, m2) - pb_membersize(st, m2)) |
||||
/* Choose first/other based on m1 == m2 (deprecated, remains for backwards compatibility) */ |
||||
#define PB_DATAOFFSET_CHOOSE(st, m1, m2) (int)(offsetof(st, m1) == offsetof(st, m2) \ |
||||
? PB_DATAOFFSET_FIRST(st, m1, m2) \
|
||||
: PB_DATAOFFSET_OTHER(st, m1, m2)) |
||||
|
||||
/* Required fields are the simplest. They just have delta (padding) from
|
||||
* previous field end, and the size of the field. Pointer is used for |
||||
* submessages and default values. |
||||
*/ |
||||
#define PB_REQUIRED_STATIC(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | ltype, \
|
||||
fd, 0, pb_membersize(st, m), 0, ptr} |
||||
|
||||
/* Optional fields add the delta to the has_ variable. */ |
||||
#define PB_OPTIONAL_STATIC(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
|
||||
fd, \
|
||||
pb_delta(st, has_ ## m, m), \
|
||||
pb_membersize(st, m), 0, ptr} |
||||
|
||||
/* Repeated fields have a _count field and also the maximum number of entries. */ |
||||
#define PB_REPEATED_STATIC(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_STATIC | PB_HTYPE_REPEATED | ltype, \
|
||||
fd, \
|
||||
pb_delta(st, m ## _count, m), \
|
||||
pb_membersize(st, m[0]), \
|
||||
pb_arraysize(st, m), ptr} |
||||
|
||||
#define PB_REQUIRED_INLINE(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_STATIC | PB_HTYPE_REQUIRED | PB_LTYPE_FIXED_LENGTH_BYTES, \
|
||||
fd, 0, pb_membersize(st, m), 0, ptr} |
||||
|
||||
/* Optional fields add the delta to the has_ variable. */ |
||||
#define PB_OPTIONAL_INLINE(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | PB_LTYPE_FIXED_LENGTH_BYTES, \
|
||||
fd, \
|
||||
pb_delta(st, has_ ## m, m), \
|
||||
pb_membersize(st, m), 0, ptr} |
||||
|
||||
/* INLINE does not support REPEATED fields. */ |
||||
|
||||
/* Allocated fields carry the size of the actual data, not the pointer */ |
||||
#define PB_REQUIRED_POINTER(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_POINTER | PB_HTYPE_REQUIRED | ltype, \
|
||||
fd, 0, pb_membersize(st, m[0]), 0, ptr} |
||||
|
||||
/* Optional fields don't need a has_ variable, as information would be redundant */ |
||||
#define PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_POINTER | PB_HTYPE_OPTIONAL | ltype, \
|
||||
fd, 0, pb_membersize(st, m[0]), 0, ptr} |
||||
|
||||
/* Repeated fields have a _count field and a pointer to array of pointers */ |
||||
#define PB_REPEATED_POINTER(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_POINTER | PB_HTYPE_REPEATED | ltype, \
|
||||
fd, pb_delta(st, m ## _count, m), \
|
||||
pb_membersize(st, m[0]), 0, ptr} |
||||
|
||||
/* Callbacks are much like required fields except with special datatype. */ |
||||
#define PB_REQUIRED_CALLBACK(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_CALLBACK | PB_HTYPE_REQUIRED | ltype, \
|
||||
fd, 0, pb_membersize(st, m), 0, ptr} |
||||
|
||||
#define PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_CALLBACK | PB_HTYPE_OPTIONAL | ltype, \
|
||||
fd, 0, pb_membersize(st, m), 0, ptr} |
||||
|
||||
#define PB_REPEATED_CALLBACK(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_CALLBACK | PB_HTYPE_REPEATED | ltype, \
|
||||
fd, 0, pb_membersize(st, m), 0, ptr} |
||||
|
||||
/* Optional extensions don't have the has_ field, as that would be redundant. */ |
||||
#define PB_OPTEXT_STATIC(tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_STATIC | PB_HTYPE_OPTIONAL | ltype, \
|
||||
0, \
|
||||
0, \
|
||||
pb_membersize(st, m), 0, ptr} |
||||
|
||||
#define PB_OPTEXT_POINTER(tag, st, m, fd, ltype, ptr) \ |
||||
PB_OPTIONAL_POINTER(tag, st, m, fd, ltype, ptr) |
||||
|
||||
/* INLINE does not support OPTEXT. */ |
||||
|
||||
#define PB_OPTEXT_CALLBACK(tag, st, m, fd, ltype, ptr) \ |
||||
PB_OPTIONAL_CALLBACK(tag, st, m, fd, ltype, ptr) |
||||
|
||||
/* The mapping from protobuf types to LTYPEs is done using these macros. */ |
||||
#define PB_LTYPE_MAP_BOOL PB_LTYPE_VARINT |
||||
#define PB_LTYPE_MAP_BYTES PB_LTYPE_BYTES |
||||
#define PB_LTYPE_MAP_DOUBLE PB_LTYPE_FIXED64 |
||||
#define PB_LTYPE_MAP_ENUM PB_LTYPE_VARINT |
||||
#define PB_LTYPE_MAP_UENUM PB_LTYPE_UVARINT |
||||
#define PB_LTYPE_MAP_FIXED32 PB_LTYPE_FIXED32 |
||||
#define PB_LTYPE_MAP_FIXED64 PB_LTYPE_FIXED64 |
||||
#define PB_LTYPE_MAP_FLOAT PB_LTYPE_FIXED32 |
||||
#define PB_LTYPE_MAP_INT32 PB_LTYPE_VARINT |
||||
#define PB_LTYPE_MAP_INT64 PB_LTYPE_VARINT |
||||
#define PB_LTYPE_MAP_MESSAGE PB_LTYPE_SUBMESSAGE |
||||
#define PB_LTYPE_MAP_SFIXED32 PB_LTYPE_FIXED32 |
||||
#define PB_LTYPE_MAP_SFIXED64 PB_LTYPE_FIXED64 |
||||
#define PB_LTYPE_MAP_SINT32 PB_LTYPE_SVARINT |
||||
#define PB_LTYPE_MAP_SINT64 PB_LTYPE_SVARINT |
||||
#define PB_LTYPE_MAP_STRING PB_LTYPE_STRING |
||||
#define PB_LTYPE_MAP_UINT32 PB_LTYPE_UVARINT |
||||
#define PB_LTYPE_MAP_UINT64 PB_LTYPE_UVARINT |
||||
#define PB_LTYPE_MAP_EXTENSION PB_LTYPE_EXTENSION |
||||
|
||||
/* This is the actual macro used in field descriptions.
|
||||
* It takes these arguments: |
||||
* - Field tag number |
||||
* - Field type: BOOL, BYTES, DOUBLE, ENUM, UENUM, FIXED32, FIXED64, |
||||
* FLOAT, INT32, INT64, MESSAGE, SFIXED32, SFIXED64 |
||||
* SINT32, SINT64, STRING, UINT32, UINT64 or EXTENSION |
||||
* - Field rules: REQUIRED, OPTIONAL or REPEATED |
||||
* - Allocation: STATIC, INLINE, or CALLBACK |
||||
* - Placement: FIRST or OTHER, depending on if this is the first field in structure. |
||||
* - Message name |
||||
* - Field name |
||||
* - Previous field name (or field name again for first field) |
||||
* - Pointer to default value or submsg fields. |
||||
*/ |
||||
|
||||
#define PB_FIELD(tag, type, rules, allocation, placement, message, field, prevfield, ptr) \ |
||||
PB_ ## rules ## _ ## allocation(tag, message, field, \
|
||||
PB_DATAOFFSET_ ## placement(message, field, prevfield), \
|
||||
PB_LTYPE_MAP_ ## type, ptr) |
||||
|
||||
/* Field description for oneof fields. This requires taking into account the
|
||||
* union name also, that's why a separate set of macros is needed. |
||||
*/ |
||||
#define PB_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype, \
|
||||
fd, pb_delta(st, which_ ## u, u.m), \
|
||||
pb_membersize(st, u.m), 0, ptr} |
||||
|
||||
#define PB_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype, \
|
||||
fd, pb_delta(st, which_ ## u, u.m), \
|
||||
pb_membersize(st, u.m[0]), 0, ptr} |
||||
|
||||
/* INLINE does not support ONEOF. */ |
||||
|
||||
#define PB_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \ |
||||
PB_ONEOF_ ## allocation(union_name, tag, message, field, \
|
||||
PB_DATAOFFSET_ ## placement(message, union_name.field, prevfield), \
|
||||
PB_LTYPE_MAP_ ## type, ptr) |
||||
|
||||
#define PB_ANONYMOUS_ONEOF_STATIC(u, tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_STATIC | PB_HTYPE_ONEOF | ltype, \
|
||||
fd, pb_delta(st, which_ ## u, m), \
|
||||
pb_membersize(st, m), 0, ptr} |
||||
|
||||
#define PB_ANONYMOUS_ONEOF_POINTER(u, tag, st, m, fd, ltype, ptr) \ |
||||
{tag, PB_ATYPE_POINTER | PB_HTYPE_ONEOF | ltype, \
|
||||
fd, pb_delta(st, which_ ## u, m), \
|
||||
pb_membersize(st, m[0]), 0, ptr} |
||||
|
||||
#define PB_ANONYMOUS_ONEOF_FIELD(union_name, tag, type, rules, allocation, placement, message, field, prevfield, ptr) \ |
||||
PB_ANONYMOUS_ONEOF_ ## allocation(union_name, tag, message, field, \
|
||||
PB_DATAOFFSET_ ## placement(message, field, prevfield), \
|
||||
PB_LTYPE_MAP_ ## type, ptr) |
||||
|
||||
/* These macros are used for giving out error messages.
|
||||
* They are mostly a debugging aid; the main error information |
||||
* is the true/false return value from functions. |
||||
* Some code space can be saved by disabling the error |
||||
* messages if not used. |
||||
* |
||||
* PB_SET_ERROR() sets the error message if none has been set yet. |
||||
* msg must be a constant string literal. |
||||
* PB_GET_ERROR() always returns a pointer to a string. |
||||
* PB_RETURN_ERROR() sets the error and returns false from current |
||||
* function. |
||||
*/ |
||||
#ifdef PB_NO_ERRMSG |
||||
#define PB_SET_ERROR(stream, msg) PB_UNUSED(stream) |
||||
#define PB_GET_ERROR(stream) "(errmsg disabled)" |
||||
#else |
||||
#define PB_SET_ERROR(stream, msg) (stream->errmsg = (stream)->errmsg ? (stream)->errmsg : (msg)) |
||||
#define PB_GET_ERROR(stream) ((stream)->errmsg ? (stream)->errmsg : "(none)") |
||||
#endif |
||||
|
||||
#define PB_RETURN_ERROR(stream, msg) return PB_SET_ERROR(stream, msg), false |
||||
|
||||
#endif |
@ -1,97 +0,0 @@ |
||||
/* pb_common.c: Common support functions for pb_encode.c and pb_decode.c.
|
||||
* |
||||
* 2014 Petteri Aimonen <jpa@kapsi.fi> |
||||
*/ |
||||
|
||||
#include "pb_common.h" |
||||
|
||||
bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct) |
||||
{ |
||||
iter->start = fields; |
||||
iter->pos = fields; |
||||
iter->required_field_index = 0; |
||||
iter->dest_struct = dest_struct; |
||||
iter->pData = (char*)dest_struct + iter->pos->data_offset; |
||||
iter->pSize = (char*)iter->pData + iter->pos->size_offset; |
||||
|
||||
return (iter->pos->tag != 0); |
||||
} |
||||
|
||||
bool pb_field_iter_next(pb_field_iter_t *iter) |
||||
{ |
||||
const pb_field_t *prev_field = iter->pos; |
||||
|
||||
if (prev_field->tag == 0) |
||||
{ |
||||
/* Handle empty message types, where the first field is already the terminator.
|
||||
* In other cases, the iter->pos never points to the terminator. */ |
||||
return false; |
||||
} |
||||
|
||||
iter->pos++; |
||||
|
||||
if (iter->pos->tag == 0) |
||||
{ |
||||
/* Wrapped back to beginning, reinitialize */ |
||||
(void)pb_field_iter_begin(iter, iter->start, iter->dest_struct); |
||||
return false; |
||||
} |
||||
else |
||||
{ |
||||
/* Increment the pointers based on previous field size */ |
||||
size_t prev_size = prev_field->data_size; |
||||
|
||||
if (PB_HTYPE(prev_field->type) == PB_HTYPE_ONEOF && |
||||
PB_HTYPE(iter->pos->type) == PB_HTYPE_ONEOF) |
||||
{ |
||||
/* Don't advance pointers inside unions */ |
||||
prev_size = 0; |
||||
iter->pData = (char*)iter->pData - prev_field->data_offset; |
||||
} |
||||
else if (PB_ATYPE(prev_field->type) == PB_ATYPE_STATIC && |
||||
PB_HTYPE(prev_field->type) == PB_HTYPE_REPEATED) |
||||
{ |
||||
/* In static arrays, the data_size tells the size of a single entry and
|
||||
* array_size is the number of entries */ |
||||
prev_size *= prev_field->array_size; |
||||
} |
||||
else if (PB_ATYPE(prev_field->type) == PB_ATYPE_POINTER) |
||||
{ |
||||
/* Pointer fields always have a constant size in the main structure.
|
||||
* The data_size only applies to the dynamically allocated area. */ |
||||
prev_size = sizeof(void*); |
||||
} |
||||
|
||||
if (PB_HTYPE(prev_field->type) == PB_HTYPE_REQUIRED) |
||||
{ |
||||
/* Count the required fields, in order to check their presence in the
|
||||
* decoder. */ |
||||
iter->required_field_index++; |
||||
} |
||||
|
||||
iter->pData = (char*)iter->pData + prev_size + iter->pos->data_offset; |
||||
iter->pSize = (char*)iter->pData + iter->pos->size_offset; |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag) |
||||
{ |
||||
const pb_field_t *start = iter->pos; |
||||
|
||||
do { |
||||
if (iter->pos->tag == tag && |
||||
PB_LTYPE(iter->pos->type) != PB_LTYPE_EXTENSION) |
||||
{ |
||||
/* Found the wanted field */ |
||||
return true; |
||||
} |
||||
|
||||
(void)pb_field_iter_next(iter); |
||||
} while (iter->pos != start); |
||||
|
||||
/* Searched all the way back to start, and found nothing. */ |
||||
return false; |
||||
} |
||||
|
||||
|
@ -1,42 +0,0 @@ |
||||
/* pb_common.h: Common support functions for pb_encode.c and pb_decode.c.
|
||||
* These functions are rarely needed by applications directly. |
||||
*/ |
||||
|
||||
#ifndef PB_COMMON_H_INCLUDED |
||||
#define PB_COMMON_H_INCLUDED |
||||
|
||||
#include "pb.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/* Iterator for pb_field_t list */ |
||||
struct pb_field_iter_s { |
||||
const pb_field_t *start; /* Start of the pb_field_t array */ |
||||
const pb_field_t *pos; /* Current position of the iterator */ |
||||
unsigned required_field_index; /* Zero-based index that counts only the required fields */ |
||||
void *dest_struct; /* Pointer to start of the structure */ |
||||
void *pData; /* Pointer to current field value */ |
||||
void *pSize; /* Pointer to count/has field */ |
||||
}; |
||||
typedef struct pb_field_iter_s pb_field_iter_t; |
||||
|
||||
/* Initialize the field iterator structure to beginning.
|
||||
* Returns false if the message type is empty. */ |
||||
bool pb_field_iter_begin(pb_field_iter_t *iter, const pb_field_t *fields, void *dest_struct); |
||||
|
||||
/* Advance the iterator to the next field.
|
||||
* Returns false when the iterator wraps back to the first field. */ |
||||
bool pb_field_iter_next(pb_field_iter_t *iter); |
||||
|
||||
/* Advance the iterator until it points at a field with the given tag.
|
||||
* Returns false if no such field exists. */ |
||||
bool pb_field_iter_find(pb_field_iter_t *iter, uint32_t tag); |
||||
|
||||
#ifdef __cplusplus |
||||
} /* extern "C" */ |
||||
#endif |
||||
|
||||
#endif |
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,149 +0,0 @@ |
||||
/* pb_decode.h: Functions to decode protocol buffers. Depends on pb_decode.c.
|
||||
* The main function is pb_decode. You also need an input stream, and the |
||||
* field descriptions created by nanopb_generator.py. |
||||
*/ |
||||
|
||||
#ifndef PB_DECODE_H_INCLUDED |
||||
#define PB_DECODE_H_INCLUDED |
||||
|
||||
#include "pb.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/* Structure for defining custom input streams. You will need to provide
|
||||
* a callback function to read the bytes from your storage, which can be |
||||
* for example a file or a network socket. |
||||
*
|
||||
* The callback must conform to these rules: |
||||
* |
||||
* 1) Return false on IO errors. This will cause decoding to abort. |
||||
* 2) You can use state to store your own data (e.g. buffer pointer), |
||||
* and rely on pb_read to verify that no-body reads past bytes_left. |
||||
* 3) Your callback may be used with substreams, in which case bytes_left |
||||
* is different than from the main stream. Don't use bytes_left to compute |
||||
* any pointers. |
||||
*/ |
||||
struct pb_istream_s |
||||
{ |
||||
#ifdef PB_BUFFER_ONLY |
||||
/* Callback pointer is not used in buffer-only configuration.
|
||||
* Having an int pointer here allows binary compatibility but |
||||
* gives an error if someone tries to assign callback function. |
||||
*/ |
||||
int *callback; |
||||
#else |
||||
bool (*callback)(pb_istream_t *stream, pb_byte_t *buf, size_t count); |
||||
#endif |
||||
|
||||
void *state; /* Free field for use by callback implementation */ |
||||
size_t bytes_left; |
||||
|
||||
#ifndef PB_NO_ERRMSG |
||||
const char *errmsg; |
||||
#endif |
||||
}; |
||||
|
||||
/***************************
|
||||
* Main decoding functions * |
||||
***************************/ |
||||
|
||||
/* Decode a single protocol buffers message from input stream into a C structure.
|
||||
* Returns true on success, false on any failure. |
||||
* The actual struct pointed to by dest must match the description in fields. |
||||
* Callback fields of the destination structure must be initialized by caller. |
||||
* All other fields will be initialized by this function. |
||||
* |
||||
* Example usage: |
||||
* MyMessage msg = {}; |
||||
* uint8_t buffer[64]; |
||||
* pb_istream_t stream; |
||||
*
|
||||
* // ... read some data into buffer ...
|
||||
* |
||||
* stream = pb_istream_from_buffer(buffer, count); |
||||
* pb_decode(&stream, MyMessage_fields, &msg); |
||||
*/ |
||||
bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
||||
|
||||
/* Same as pb_decode, except does not initialize the destination structure
|
||||
* to default values. This is slightly faster if you need no default values |
||||
* and just do memset(struct, 0, sizeof(struct)) yourself. |
||||
* |
||||
* This can also be used for 'merging' two messages, i.e. update only the |
||||
* fields that exist in the new message. |
||||
* |
||||
* Note: If this function returns with an error, it will not release any |
||||
* dynamically allocated fields. You will need to call pb_release() yourself. |
||||
*/ |
||||
bool pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
||||
|
||||
/* Same as pb_decode, except expects the stream to start with the message size
|
||||
* encoded as varint. Corresponds to parseDelimitedFrom() in Google's |
||||
* protobuf API. |
||||
*/ |
||||
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct); |
||||
|
||||
#ifdef PB_ENABLE_MALLOC |
||||
/* Release any allocated pointer fields. If you use dynamic allocation, you should
|
||||
* call this for any successfully decoded message when you are done with it. If |
||||
* pb_decode() returns with an error, the message is already released. |
||||
*/ |
||||
void pb_release(const pb_field_t fields[], void *dest_struct); |
||||
#endif |
||||
|
||||
|
||||
/**************************************
|
||||
* Functions for manipulating streams * |
||||
**************************************/ |
||||
|
||||
/* Create an input stream for reading from a memory buffer.
|
||||
* |
||||
* Alternatively, you can use a custom stream that reads directly from e.g. |
||||
* a file or a network socket. |
||||
*/ |
||||
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t bufsize); |
||||
|
||||
/* Function to read from a pb_istream_t. You can use this if you need to
|
||||
* read some custom header data, or to read data in field callbacks. |
||||
*/ |
||||
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count); |
||||
|
||||
|
||||
/************************************************
|
||||
* Helper functions for writing field callbacks * |
||||
************************************************/ |
||||
|
||||
/* Decode the tag for the next field in the stream. Gives the wire type and
|
||||
* field tag. At end of the message, returns false and sets eof to true. */ |
||||
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof); |
||||
|
||||
/* Skip the field payload data, given the wire type. */ |
||||
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type); |
||||
|
||||
/* Decode an integer in the varint format. This works for bool, enum, int32,
|
||||
* int64, uint32 and uint64 field types. */ |
||||
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest); |
||||
|
||||
/* Decode an integer in the zig-zagged svarint format. This works for sint32
|
||||
* and sint64. */ |
||||
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest); |
||||
|
||||
/* Decode a fixed32, sfixed32 or float value. You need to pass a pointer to
|
||||
* a 4-byte wide C variable. */ |
||||
bool pb_decode_fixed32(pb_istream_t *stream, void *dest); |
||||
|
||||
/* Decode a fixed64, sfixed64 or double value. You need to pass a pointer to
|
||||
* a 8-byte wide C variable. */ |
||||
bool pb_decode_fixed64(pb_istream_t *stream, void *dest); |
||||
|
||||
/* Make a limited-length substream for reading a PB_WT_STRING field. */ |
||||
bool pb_make_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
||||
void pb_close_string_substream(pb_istream_t *stream, pb_istream_t *substream); |
||||
|
||||
#ifdef __cplusplus |
||||
} /* extern "C" */ |
||||
#endif |
||||
|
||||
#endif |
@ -1,696 +0,0 @@ |
||||
/* pb_encode.c -- encode a protobuf using minimal resources
|
||||
* |
||||
* 2011 Petteri Aimonen <jpa@kapsi.fi> |
||||
*/ |
||||
|
||||
#include "pb.h" |
||||
#include "pb_encode.h" |
||||
#include "pb_common.h" |
||||
|
||||
/* Use the GCC warn_unused_result attribute to check that all return values
|
||||
* are propagated correctly. On other compilers and gcc before 3.4.0 just |
||||
* ignore the annotation. |
||||
*/ |
||||
#if !defined(__GNUC__) || ( __GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ < 4) |
||||
#define checkreturn |
||||
#else |
||||
#define checkreturn __attribute__((warn_unused_result)) |
||||
#endif |
||||
|
||||
/**************************************
|
||||
* Declarations internal to this file * |
||||
**************************************/ |
||||
typedef bool (*pb_encoder_t)(pb_ostream_t *stream, const pb_field_t *field, const void *src) checkreturn; |
||||
|
||||
static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count); |
||||
static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field, const void *pData, size_t count, pb_encoder_t func); |
||||
static bool checkreturn encode_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData); |
||||
static bool checkreturn default_extension_encoder(pb_ostream_t *stream, const pb_extension_t *extension); |
||||
static bool checkreturn encode_extension_field(pb_ostream_t *stream, const pb_field_t *field, const void *pData); |
||||
static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
||||
static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
||||
static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
||||
static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
||||
static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
||||
static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
||||
static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
||||
static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src); |
||||
|
||||
/* --- Function pointers to field encoders ---
|
||||
* Order in the array must match pb_action_t LTYPE numbering. |
||||
*/ |
||||
static const pb_encoder_t PB_ENCODERS[PB_LTYPES_COUNT] = { |
||||
&pb_enc_varint, |
||||
&pb_enc_uvarint, |
||||
&pb_enc_svarint, |
||||
&pb_enc_fixed32, |
||||
&pb_enc_fixed64, |
||||
|
||||
&pb_enc_bytes, |
||||
&pb_enc_string, |
||||
&pb_enc_submessage, |
||||
NULL, /* extensions */ |
||||
&pb_enc_bytes /* PB_LTYPE_FIXED_LENGTH_BYTES */ |
||||
}; |
||||
|
||||
/*******************************
|
||||
* pb_ostream_t implementation * |
||||
*******************************/ |
||||
|
||||
static bool checkreturn buf_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count) |
||||
{ |
||||
pb_byte_t *dest = (pb_byte_t*)stream->state; |
||||
stream->state = dest + count; |
||||
|
||||
while (count--) |
||||
*dest++ = *buf++; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize) |
||||
{ |
||||
pb_ostream_t stream; |
||||
#ifdef PB_BUFFER_ONLY |
||||
stream.callback = (void*)1; /* Just a marker value */ |
||||
#else |
||||
stream.callback = &buf_write; |
||||
#endif |
||||
stream.state = buf; |
||||
stream.max_size = bufsize; |
||||
stream.bytes_written = 0; |
||||
#ifndef PB_NO_ERRMSG |
||||
stream.errmsg = NULL; |
||||
#endif |
||||
return stream; |
||||
} |
||||
|
||||
bool checkreturn pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count) |
||||
{ |
||||
if (stream->callback != NULL) |
||||
{ |
||||
if (stream->bytes_written + count > stream->max_size) |
||||
PB_RETURN_ERROR(stream, "stream full"); |
||||
|
||||
#ifdef PB_BUFFER_ONLY |
||||
if (!buf_write(stream, buf, count)) |
||||
PB_RETURN_ERROR(stream, "io error"); |
||||
#else |
||||
if (!stream->callback(stream, buf, count)) |
||||
PB_RETURN_ERROR(stream, "io error"); |
||||
#endif |
||||
} |
||||
|
||||
stream->bytes_written += count; |
||||
return true; |
||||
} |
||||
|
||||
/*************************
|
||||
* Encode a single field * |
||||
*************************/ |
||||
|
||||
/* Encode a static array. Handles the size calculations and possible packing. */ |
||||
static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *field, |
||||
const void *pData, size_t count, pb_encoder_t func) |
||||
{ |
||||
size_t i; |
||||
const void *p; |
||||
size_t size; |
||||
|
||||
if (count == 0) |
||||
return true; |
||||
|
||||
if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size) |
||||
PB_RETURN_ERROR(stream, "array max size exceeded"); |
||||
|
||||
/* We always pack arrays if the datatype allows it. */ |
||||
if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE) |
||||
{ |
||||
if (!pb_encode_tag(stream, PB_WT_STRING, field->tag)) |
||||
return false; |
||||
|
||||
/* Determine the total size of packed array. */ |
||||
if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32) |
||||
{ |
||||
size = 4 * count; |
||||
} |
||||
else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64) |
||||
{ |
||||
size = 8 * count; |
||||
} |
||||
else |
||||
{
|
||||
pb_ostream_t sizestream = PB_OSTREAM_SIZING; |
||||
p = pData; |
||||
for (i = 0; i < count; i++) |
||||
{ |
||||
if (!func(&sizestream, field, p)) |
||||
return false; |
||||
p = (const char*)p + field->data_size; |
||||
} |
||||
size = sizestream.bytes_written; |
||||
} |
||||
|
||||
if (!pb_encode_varint(stream, (uint64_t)size)) |
||||
return false; |
||||
|
||||
if (stream->callback == NULL) |
||||
return pb_write(stream, NULL, size); /* Just sizing.. */ |
||||
|
||||
/* Write the data */ |
||||
p = pData; |
||||
for (i = 0; i < count; i++) |
||||
{ |
||||
if (!func(stream, field, p)) |
||||
return false; |
||||
p = (const char*)p + field->data_size; |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
p = pData; |
||||
for (i = 0; i < count; i++) |
||||
{ |
||||
if (!pb_encode_tag_for_field(stream, field)) |
||||
return false; |
||||
|
||||
/* Normally the data is stored directly in the array entries, but
|
||||
* for pointer-type string and bytes fields, the array entries are |
||||
* actually pointers themselves also. So we have to dereference once |
||||
* more to get to the actual data. */ |
||||
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER && |
||||
(PB_LTYPE(field->type) == PB_LTYPE_STRING || |
||||
PB_LTYPE(field->type) == PB_LTYPE_BYTES)) |
||||
{ |
||||
if (!func(stream, field, *(const void* const*)p)) |
||||
return false;
|
||||
} |
||||
else |
||||
{ |
||||
if (!func(stream, field, p)) |
||||
return false; |
||||
} |
||||
p = (const char*)p + field->data_size; |
||||
} |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
/* Encode a field with static or pointer allocation, i.e. one whose data
|
||||
* is available to the encoder directly. */ |
||||
static bool checkreturn encode_basic_field(pb_ostream_t *stream, |
||||
const pb_field_t *field, const void *pData) |
||||
{ |
||||
pb_encoder_t func; |
||||
const void *pSize; |
||||
bool implicit_has = true; |
||||
|
||||
func = PB_ENCODERS[PB_LTYPE(field->type)]; |
||||
|
||||
if (field->size_offset) |
||||
pSize = (const char*)pData + field->size_offset; |
||||
else |
||||
pSize = &implicit_has; |
||||
|
||||
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) |
||||
{ |
||||
/* pData is a pointer to the field, which contains pointer to
|
||||
* the data. If the 2nd pointer is NULL, it is interpreted as if |
||||
* the has_field was false. |
||||
*/ |
||||
|
||||
pData = *(const void* const*)pData; |
||||
implicit_has = (pData != NULL); |
||||
} |
||||
|
||||
switch (PB_HTYPE(field->type)) |
||||
{ |
||||
case PB_HTYPE_REQUIRED: |
||||
if (!pData) |
||||
PB_RETURN_ERROR(stream, "missing required field"); |
||||
if (!pb_encode_tag_for_field(stream, field)) |
||||
return false; |
||||
if (!func(stream, field, pData)) |
||||
return false; |
||||
break; |
||||
|
||||
case PB_HTYPE_OPTIONAL: |
||||
if (*(const bool*)pSize) |
||||
{ |
||||
if (!pb_encode_tag_for_field(stream, field)) |
||||
return false; |
||||
|
||||
if (!func(stream, field, pData)) |
||||
return false; |
||||
} |
||||
break; |
||||
|
||||
case PB_HTYPE_REPEATED: |
||||
if (!encode_array(stream, field, pData, *(const pb_size_t*)pSize, func)) |
||||
return false; |
||||
break; |
||||
|
||||
case PB_HTYPE_ONEOF: |
||||
if (*(const pb_size_t*)pSize == field->tag) |
||||
{ |
||||
if (!pb_encode_tag_for_field(stream, field)) |
||||
return false; |
||||
|
||||
if (!func(stream, field, pData)) |
||||
return false; |
||||
} |
||||
break; |
||||
|
||||
default: |
||||
PB_RETURN_ERROR(stream, "invalid field type"); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
/* Encode a field with callback semantics. This means that a user function is
|
||||
* called to provide and encode the actual data. */ |
||||
static bool checkreturn encode_callback_field(pb_ostream_t *stream, |
||||
const pb_field_t *field, const void *pData) |
||||
{ |
||||
const pb_callback_t *callback = (const pb_callback_t*)pData; |
||||
|
||||
#ifdef PB_OLD_CALLBACK_STYLE |
||||
const void *arg = callback->arg; |
||||
#else |
||||
void * const *arg = &(callback->arg); |
||||
#endif |
||||
|
||||
if (callback->funcs.encode != NULL) |
||||
{ |
||||
if (!callback->funcs.encode(stream, field, arg)) |
||||
PB_RETURN_ERROR(stream, "callback error"); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
/* Encode a single field of any callback or static type. */ |
||||
static bool checkreturn encode_field(pb_ostream_t *stream, |
||||
const pb_field_t *field, const void *pData) |
||||
{ |
||||
switch (PB_ATYPE(field->type)) |
||||
{ |
||||
case PB_ATYPE_STATIC: |
||||
case PB_ATYPE_POINTER: |
||||
return encode_basic_field(stream, field, pData); |
||||
|
||||
case PB_ATYPE_CALLBACK: |
||||
return encode_callback_field(stream, field, pData); |
||||
|
||||
default: |
||||
PB_RETURN_ERROR(stream, "invalid field type"); |
||||
} |
||||
} |
||||
|
||||
/* Default handler for extension fields. Expects to have a pb_field_t
|
||||
* pointer in the extension->type->arg field. */ |
||||
static bool checkreturn default_extension_encoder(pb_ostream_t *stream, |
||||
const pb_extension_t *extension) |
||||
{ |
||||
const pb_field_t *field = (const pb_field_t*)extension->type->arg; |
||||
|
||||
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) |
||||
{ |
||||
/* For pointer extensions, the pointer is stored directly
|
||||
* in the extension structure. This avoids having an extra |
||||
* indirection. */ |
||||
return encode_field(stream, field, &extension->dest); |
||||
} |
||||
else |
||||
{ |
||||
return encode_field(stream, field, extension->dest); |
||||
} |
||||
} |
||||
|
||||
/* Walk through all the registered extensions and give them a chance
|
||||
* to encode themselves. */ |
||||
static bool checkreturn encode_extension_field(pb_ostream_t *stream, |
||||
const pb_field_t *field, const void *pData) |
||||
{ |
||||
const pb_extension_t *extension = *(const pb_extension_t* const *)pData; |
||||
PB_UNUSED(field); |
||||
|
||||
while (extension) |
||||
{ |
||||
bool status; |
||||
if (extension->type->encode) |
||||
status = extension->type->encode(stream, extension); |
||||
else |
||||
status = default_extension_encoder(stream, extension); |
||||
|
||||
if (!status) |
||||
return false; |
||||
|
||||
extension = extension->next; |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
/*********************
|
||||
* Encode all fields * |
||||
*********************/ |
||||
|
||||
static void *remove_const(const void *p) |
||||
{ |
||||
/* Note: this casts away const, in order to use the common field iterator
|
||||
* logic for both encoding and decoding. */ |
||||
union { |
||||
void *p1; |
||||
const void *p2; |
||||
} t; |
||||
t.p2 = p; |
||||
return t.p1; |
||||
} |
||||
|
||||
bool checkreturn pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) |
||||
{ |
||||
pb_field_iter_t iter; |
||||
if (!pb_field_iter_begin(&iter, fields, remove_const(src_struct))) |
||||
return true; /* Empty message type */ |
||||
|
||||
do { |
||||
if (PB_LTYPE(iter.pos->type) == PB_LTYPE_EXTENSION) |
||||
{ |
||||
/* Special case for the extension field placeholder */ |
||||
if (!encode_extension_field(stream, iter.pos, iter.pData)) |
||||
return false; |
||||
} |
||||
else |
||||
{ |
||||
/* Regular field */ |
||||
if (!encode_field(stream, iter.pos, iter.pData)) |
||||
return false; |
||||
} |
||||
} while (pb_field_iter_next(&iter)); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) |
||||
{ |
||||
return pb_encode_submessage(stream, fields, src_struct); |
||||
} |
||||
|
||||
bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct) |
||||
{ |
||||
pb_ostream_t stream = PB_OSTREAM_SIZING; |
||||
|
||||
if (!pb_encode(&stream, fields, src_struct)) |
||||
return false; |
||||
|
||||
*size = stream.bytes_written; |
||||
return true; |
||||
} |
||||
|
||||
/********************
|
||||
* Helper functions * |
||||
********************/ |
||||
bool checkreturn pb_encode_varint(pb_ostream_t *stream, uint64_t value) |
||||
{ |
||||
pb_byte_t buffer[10]; |
||||
size_t i = 0; |
||||
|
||||
if (value <= 0x7F) |
||||
{ |
||||
pb_byte_t v = (pb_byte_t)value; |
||||
return pb_write(stream, &v, 1); |
||||
} |
||||
|
||||
while (value) |
||||
{ |
||||
buffer[i] = (pb_byte_t)((value & 0x7F) | 0x80); |
||||
value >>= 7; |
||||
i++; |
||||
} |
||||
buffer[i-1] &= 0x7F; /* Unset top bit on last byte */ |
||||
|
||||
return pb_write(stream, buffer, i); |
||||
} |
||||
|
||||
bool checkreturn pb_encode_svarint(pb_ostream_t *stream, int64_t value) |
||||
{ |
||||
uint64_t zigzagged; |
||||
if (value < 0) |
||||
zigzagged = ~((uint64_t)value << 1); |
||||
else |
||||
zigzagged = (uint64_t)value << 1; |
||||
|
||||
return pb_encode_varint(stream, zigzagged); |
||||
} |
||||
|
||||
bool checkreturn pb_encode_fixed32(pb_ostream_t *stream, const void *value) |
||||
{ |
||||
uint32_t val = *(const uint32_t*)value; |
||||
pb_byte_t bytes[4]; |
||||
bytes[0] = (pb_byte_t)(val & 0xFF); |
||||
bytes[1] = (pb_byte_t)((val >> 8) & 0xFF); |
||||
bytes[2] = (pb_byte_t)((val >> 16) & 0xFF); |
||||
bytes[3] = (pb_byte_t)((val >> 24) & 0xFF); |
||||
return pb_write(stream, bytes, 4); |
||||
} |
||||
|
||||
bool checkreturn pb_encode_fixed64(pb_ostream_t *stream, const void *value) |
||||
{ |
||||
uint64_t val = *(const uint64_t*)value; |
||||
pb_byte_t bytes[8]; |
||||
bytes[0] = (pb_byte_t)(val & 0xFF); |
||||
bytes[1] = (pb_byte_t)((val >> 8) & 0xFF); |
||||
bytes[2] = (pb_byte_t)((val >> 16) & 0xFF); |
||||
bytes[3] = (pb_byte_t)((val >> 24) & 0xFF); |
||||
bytes[4] = (pb_byte_t)((val >> 32) & 0xFF); |
||||
bytes[5] = (pb_byte_t)((val >> 40) & 0xFF); |
||||
bytes[6] = (pb_byte_t)((val >> 48) & 0xFF); |
||||
bytes[7] = (pb_byte_t)((val >> 56) & 0xFF); |
||||
return pb_write(stream, bytes, 8); |
||||
} |
||||
|
||||
bool checkreturn pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number) |
||||
{ |
||||
uint64_t tag = ((uint64_t)field_number << 3) | wiretype; |
||||
return pb_encode_varint(stream, tag); |
||||
} |
||||
|
||||
bool checkreturn pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field) |
||||
{ |
||||
pb_wire_type_t wiretype; |
||||
switch (PB_LTYPE(field->type)) |
||||
{ |
||||
case PB_LTYPE_VARINT: |
||||
case PB_LTYPE_UVARINT: |
||||
case PB_LTYPE_SVARINT: |
||||
wiretype = PB_WT_VARINT; |
||||
break; |
||||
|
||||
case PB_LTYPE_FIXED32: |
||||
wiretype = PB_WT_32BIT; |
||||
break; |
||||
|
||||
case PB_LTYPE_FIXED64: |
||||
wiretype = PB_WT_64BIT; |
||||
break; |
||||
|
||||
case PB_LTYPE_BYTES: |
||||
case PB_LTYPE_STRING: |
||||
case PB_LTYPE_SUBMESSAGE: |
||||
case PB_LTYPE_FIXED_LENGTH_BYTES: |
||||
wiretype = PB_WT_STRING; |
||||
break; |
||||
|
||||
default: |
||||
PB_RETURN_ERROR(stream, "invalid field type"); |
||||
} |
||||
|
||||
return pb_encode_tag(stream, wiretype, field->tag); |
||||
} |
||||
|
||||
bool checkreturn pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size) |
||||
{ |
||||
if (!pb_encode_varint(stream, (uint64_t)size)) |
||||
return false; |
||||
|
||||
return pb_write(stream, buffer, size); |
||||
} |
||||
|
||||
bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) |
||||
{ |
||||
/* First calculate the message size using a non-writing substream. */ |
||||
pb_ostream_t substream = PB_OSTREAM_SIZING; |
||||
size_t size; |
||||
bool status; |
||||
|
||||
if (!pb_encode(&substream, fields, src_struct)) |
||||
{ |
||||
#ifndef PB_NO_ERRMSG |
||||
stream->errmsg = substream.errmsg; |
||||
#endif |
||||
return false; |
||||
} |
||||
|
||||
size = substream.bytes_written; |
||||
|
||||
if (!pb_encode_varint(stream, (uint64_t)size)) |
||||
return false; |
||||
|
||||
if (stream->callback == NULL) |
||||
return pb_write(stream, NULL, size); /* Just sizing */ |
||||
|
||||
if (stream->bytes_written + size > stream->max_size) |
||||
PB_RETURN_ERROR(stream, "stream full"); |
||||
|
||||
/* Use a substream to verify that a callback doesn't write more than
|
||||
* what it did the first time. */ |
||||
substream.callback = stream->callback; |
||||
substream.state = stream->state; |
||||
substream.max_size = size; |
||||
substream.bytes_written = 0; |
||||
#ifndef PB_NO_ERRMSG |
||||
substream.errmsg = NULL; |
||||
#endif |
||||
|
||||
status = pb_encode(&substream, fields, src_struct); |
||||
|
||||
stream->bytes_written += substream.bytes_written; |
||||
stream->state = substream.state; |
||||
#ifndef PB_NO_ERRMSG |
||||
stream->errmsg = substream.errmsg; |
||||
#endif |
||||
|
||||
if (substream.bytes_written != size) |
||||
PB_RETURN_ERROR(stream, "submsg size changed"); |
||||
|
||||
return status; |
||||
} |
||||
|
||||
/* Field encoders */ |
||||
|
||||
static bool checkreturn pb_enc_varint(pb_ostream_t *stream, const pb_field_t *field, const void *src) |
||||
{ |
||||
int64_t value = 0; |
||||
|
||||
if (field->data_size == sizeof(int_least8_t)) |
||||
value = *(const int_least8_t*)src; |
||||
else if (field->data_size == sizeof(int_least16_t)) |
||||
value = *(const int_least16_t*)src; |
||||
else if (field->data_size == sizeof(int32_t)) |
||||
value = *(const int32_t*)src; |
||||
else if (field->data_size == sizeof(int64_t)) |
||||
value = *(const int64_t*)src; |
||||
else |
||||
PB_RETURN_ERROR(stream, "invalid data_size"); |
||||
|
||||
return pb_encode_varint(stream, (uint64_t)value); |
||||
} |
||||
|
||||
static bool checkreturn pb_enc_uvarint(pb_ostream_t *stream, const pb_field_t *field, const void *src) |
||||
{ |
||||
uint64_t value = 0; |
||||
|
||||
if (field->data_size == sizeof(uint_least8_t)) |
||||
value = *(const uint_least8_t*)src; |
||||
else if (field->data_size == sizeof(uint_least16_t)) |
||||
value = *(const uint_least16_t*)src; |
||||
else if (field->data_size == sizeof(uint32_t)) |
||||
value = *(const uint32_t*)src; |
||||
else if (field->data_size == sizeof(uint64_t)) |
||||
value = *(const uint64_t*)src; |
||||
else |
||||
PB_RETURN_ERROR(stream, "invalid data_size"); |
||||
|
||||
return pb_encode_varint(stream, value); |
||||
} |
||||
|
||||
static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src) |
||||
{ |
||||
int64_t value = 0; |
||||
|
||||
if (field->data_size == sizeof(int_least8_t)) |
||||
value = *(const int_least8_t*)src; |
||||
else if (field->data_size == sizeof(int_least16_t)) |
||||
value = *(const int_least16_t*)src; |
||||
else if (field->data_size == sizeof(int32_t)) |
||||
value = *(const int32_t*)src; |
||||
else if (field->data_size == sizeof(int64_t)) |
||||
value = *(const int64_t*)src; |
||||
else |
||||
PB_RETURN_ERROR(stream, "invalid data_size"); |
||||
|
||||
return pb_encode_svarint(stream, value); |
||||
} |
||||
|
||||
static bool checkreturn pb_enc_fixed64(pb_ostream_t *stream, const pb_field_t *field, const void *src) |
||||
{ |
||||
PB_UNUSED(field); |
||||
return pb_encode_fixed64(stream, src); |
||||
} |
||||
|
||||
static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src) |
||||
{ |
||||
PB_UNUSED(field); |
||||
return pb_encode_fixed32(stream, src); |
||||
} |
||||
|
||||
static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src) |
||||
{ |
||||
const pb_bytes_array_t *bytes = NULL; |
||||
|
||||
if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES) |
||||
return pb_encode_string(stream, (const pb_byte_t*)src, field->data_size); |
||||
|
||||
bytes = (const pb_bytes_array_t*)src; |
||||
|
||||
if (src == NULL) |
||||
{ |
||||
/* Treat null pointer as an empty bytes field */ |
||||
return pb_encode_string(stream, NULL, 0); |
||||
} |
||||
|
||||
if (PB_ATYPE(field->type) == PB_ATYPE_STATIC && |
||||
PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size) |
||||
{ |
||||
PB_RETURN_ERROR(stream, "bytes size exceeded"); |
||||
} |
||||
|
||||
return pb_encode_string(stream, bytes->bytes, bytes->size); |
||||
} |
||||
|
||||
static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src) |
||||
{ |
||||
size_t size = 0; |
||||
size_t max_size = field->data_size; |
||||
const char *p = (const char*)src; |
||||
|
||||
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER) |
||||
max_size = (size_t)-1; |
||||
|
||||
if (src == NULL) |
||||
{ |
||||
size = 0; /* Treat null pointer as an empty string */ |
||||
} |
||||
else |
||||
{ |
||||
/* strnlen() is not always available, so just use a loop */ |
||||
while (size < max_size && *p != '\0') |
||||
{ |
||||
size++; |
||||
p++; |
||||
} |
||||
} |
||||
|
||||
return pb_encode_string(stream, (const pb_byte_t*)src, size); |
||||
} |
||||
|
||||
static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src) |
||||
{ |
||||
if (field->ptr == NULL) |
||||
PB_RETURN_ERROR(stream, "invalid field descriptor"); |
||||
|
||||
return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src); |
||||
} |
||||
|
@ -1,154 +0,0 @@ |
||||
/* pb_encode.h: Functions to encode protocol buffers. Depends on pb_encode.c.
|
||||
* The main function is pb_encode. You also need an output stream, and the |
||||
* field descriptions created by nanopb_generator.py. |
||||
*/ |
||||
|
||||
#ifndef PB_ENCODE_H_INCLUDED |
||||
#define PB_ENCODE_H_INCLUDED |
||||
|
||||
#include "pb.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/* Structure for defining custom output streams. You will need to provide
|
||||
* a callback function to write the bytes to your storage, which can be |
||||
* for example a file or a network socket. |
||||
* |
||||
* The callback must conform to these rules: |
||||
* |
||||
* 1) Return false on IO errors. This will cause encoding to abort. |
||||
* 2) You can use state to store your own data (e.g. buffer pointer). |
||||
* 3) pb_write will update bytes_written after your callback runs. |
||||
* 4) Substreams will modify max_size and bytes_written. Don't use them |
||||
* to calculate any pointers. |
||||
*/ |
||||
struct pb_ostream_s |
||||
{ |
||||
#ifdef PB_BUFFER_ONLY |
||||
/* Callback pointer is not used in buffer-only configuration.
|
||||
* Having an int pointer here allows binary compatibility but |
||||
* gives an error if someone tries to assign callback function. |
||||
* Also, NULL pointer marks a 'sizing stream' that does not |
||||
* write anything. |
||||
*/ |
||||
int *callback; |
||||
#else |
||||
bool (*callback)(pb_ostream_t *stream, const pb_byte_t *buf, size_t count); |
||||
#endif |
||||
void *state; /* Free field for use by callback implementation. */ |
||||
size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */ |
||||
size_t bytes_written; /* Number of bytes written so far. */ |
||||
|
||||
#ifndef PB_NO_ERRMSG |
||||
const char *errmsg; |
||||
#endif |
||||
}; |
||||
|
||||
/***************************
|
||||
* Main encoding functions * |
||||
***************************/ |
||||
|
||||
/* Encode a single protocol buffers message from C structure into a stream.
|
||||
* Returns true on success, false on any failure. |
||||
* The actual struct pointed to by src_struct must match the description in fields. |
||||
* All required fields in the struct are assumed to have been filled in. |
||||
* |
||||
* Example usage: |
||||
* MyMessage msg = {}; |
||||
* uint8_t buffer[64]; |
||||
* pb_ostream_t stream; |
||||
* |
||||
* msg.field1 = 42; |
||||
* stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
||||
* pb_encode(&stream, MyMessage_fields, &msg); |
||||
*/ |
||||
bool pb_encode(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
||||
|
||||
/* Same as pb_encode, but prepends the length of the message as a varint.
|
||||
* Corresponds to writeDelimitedTo() in Google's protobuf API. |
||||
*/ |
||||
bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
||||
|
||||
/* Encode the message to get the size of the encoded data, but do not store
|
||||
* the data. */ |
||||
bool pb_get_encoded_size(size_t *size, const pb_field_t fields[], const void *src_struct); |
||||
|
||||
/**************************************
|
||||
* Functions for manipulating streams * |
||||
**************************************/ |
||||
|
||||
/* Create an output stream for writing into a memory buffer.
|
||||
* The number of bytes written can be found in stream.bytes_written after |
||||
* encoding the message. |
||||
* |
||||
* Alternatively, you can use a custom stream that writes directly to e.g. |
||||
* a file or a network socket. |
||||
*/ |
||||
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize); |
||||
|
||||
/* Pseudo-stream for measuring the size of a message without actually storing
|
||||
* the encoded data. |
||||
*
|
||||
* Example usage: |
||||
* MyMessage msg = {}; |
||||
* pb_ostream_t stream = PB_OSTREAM_SIZING; |
||||
* pb_encode(&stream, MyMessage_fields, &msg); |
||||
* printf("Message size is %d\n", stream.bytes_written); |
||||
*/ |
||||
#ifndef PB_NO_ERRMSG |
||||
#define PB_OSTREAM_SIZING {0,0,0,0,0} |
||||
#else |
||||
#define PB_OSTREAM_SIZING {0,0,0,0} |
||||
#endif |
||||
|
||||
/* Function to write into a pb_ostream_t stream. You can use this if you need
|
||||
* to append or prepend some custom headers to the message. |
||||
*/ |
||||
bool pb_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count); |
||||
|
||||
|
||||
/************************************************
|
||||
* Helper functions for writing field callbacks * |
||||
************************************************/ |
||||
|
||||
/* Encode field header based on type and field number defined in the field
|
||||
* structure. Call this from the callback before writing out field contents. */ |
||||
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_t *field); |
||||
|
||||
/* Encode field header by manually specifing wire type. You need to use this
|
||||
* if you want to write out packed arrays from a callback field. */ |
||||
bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number); |
||||
|
||||
/* Encode an integer in the varint format.
|
||||
* This works for bool, enum, int32, int64, uint32 and uint64 field types. */ |
||||
bool pb_encode_varint(pb_ostream_t *stream, uint64_t value); |
||||
|
||||
/* Encode an integer in the zig-zagged svarint format.
|
||||
* This works for sint32 and sint64. */ |
||||
bool pb_encode_svarint(pb_ostream_t *stream, int64_t value); |
||||
|
||||
/* Encode a string or bytes type field. For strings, pass strlen(s) as size. */ |
||||
bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size); |
||||
|
||||
/* Encode a fixed32, sfixed32 or float value.
|
||||
* You need to pass a pointer to a 4-byte wide C variable. */ |
||||
bool pb_encode_fixed32(pb_ostream_t *stream, const void *value); |
||||
|
||||
/* Encode a fixed64, sfixed64 or double value.
|
||||
* You need to pass a pointer to a 8-byte wide C variable. */ |
||||
bool pb_encode_fixed64(pb_ostream_t *stream, const void *value); |
||||
|
||||
/* Encode a submessage field.
|
||||
* You need to pass the pb_field_t array and pointer to struct, just like |
||||
* with pb_encode(). This internally encodes the submessage twice, first to |
||||
* calculate message size and then to actually write it out. |
||||
*/ |
||||
bool pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct); |
||||
|
||||
#ifdef __cplusplus |
||||
} /* extern "C" */ |
||||
#endif |
||||
|
||||
#endif |
@ -1,21 +0,0 @@ |
||||
all: |
||||
scons
|
||||
|
||||
clean: |
||||
scons -c
|
||||
|
||||
coverage: |
||||
rm -rf build coverage
|
||||
|
||||
# LCOV does not like the newer gcov format
|
||||
scons CC=gcc-4.6 CXX=gcc-4.6
|
||||
|
||||
# Collect the data
|
||||
mkdir build/coverage
|
||||
lcov --base-directory . --directory build/ --gcov-tool gcov-4.6 -c -o build/coverage/nanopb.info
|
||||
|
||||
# Remove the test code from results
|
||||
lcov -r build/coverage/nanopb.info '*tests*' -o build/coverage/nanopb.info
|
||||
|
||||
# Generate HTML
|
||||
genhtml -o build/coverage build/coverage/nanopb.info
|
@ -1,155 +0,0 @@ |
||||
Help(''' |
||||
Type 'scons' to build and run all the available test cases. |
||||
It will automatically detect your platform and C compiler and |
||||
build appropriately. |
||||
|
||||
You can modify the behavious using following options: |
||||
CC Name of C compiler |
||||
CXX Name of C++ compiler |
||||
CCFLAGS Flags to pass to the C compiler |
||||
CXXFLAGS Flags to pass to the C++ compiler |
||||
|
||||
For example, for a clang build, use: |
||||
scons CC=clang CXX=clang++ |
||||
''') |
||||
|
||||
import os |
||||
env = Environment(ENV = os.environ, tools = ['default', 'nanopb']) |
||||
|
||||
# Allow overriding the compiler with scons CC=??? |
||||
if 'CC' in ARGUMENTS: env.Replace(CC = ARGUMENTS['CC']) |
||||
if 'CXX' in ARGUMENTS: env.Replace(CXX = ARGUMENTS['CXX']) |
||||
if 'CCFLAGS' in ARGUMENTS: env.Append(CCFLAGS = ARGUMENTS['CCFLAGS']) |
||||
if 'CXXFLAGS' in ARGUMENTS: env.Append(CXXFLAGS = ARGUMENTS['CXXFLAGS']) |
||||
|
||||
# Add the builders defined in site_init.py |
||||
add_nanopb_builders(env) |
||||
|
||||
# Path to the files shared by tests, and to the nanopb core. |
||||
env.Append(CPPPATH = ["#../", "$COMMON"]) |
||||
|
||||
# Path for finding nanopb.proto |
||||
env.Append(PROTOCPATH = '#../generator') |
||||
|
||||
# Check the compilation environment, unless we are just cleaning up. |
||||
if not env.GetOption('clean'): |
||||
def check_ccflags(context, flags, linkflags = ''): |
||||
'''Check if given CCFLAGS are supported''' |
||||
context.Message('Checking support for CCFLAGS="%s"... ' % flags) |
||||
oldflags = context.env['CCFLAGS'] |
||||
oldlinkflags = context.env['CCFLAGS'] |
||||
context.env.Append(CCFLAGS = flags) |
||||
context.env.Append(LINKFLAGS = linkflags) |
||||
result = context.TryCompile("int main() {return 0;}", '.c') |
||||
context.env.Replace(CCFLAGS = oldflags) |
||||
context.env.Replace(LINKFLAGS = oldlinkflags) |
||||
context.Result(result) |
||||
return result |
||||
|
||||
conf = Configure(env, custom_tests = {'CheckCCFLAGS': check_ccflags}) |
||||
|
||||
# If the platform doesn't support C99, use our own header file instead. |
||||
stdbool = conf.CheckCHeader('stdbool.h') |
||||
stdint = conf.CheckCHeader('stdint.h') |
||||
stddef = conf.CheckCHeader('stddef.h') |
||||
string = conf.CheckCHeader('string.h') |
||||
stdlib = conf.CheckCHeader('stdlib.h') |
||||
if not stdbool or not stdint or not stddef or not string: |
||||
conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'}) |
||||
conf.env.Append(CPPPATH = "#../extra") |
||||
conf.env.Append(SYSHDR = '\\"pb_syshdr.h\\"') |
||||
|
||||
if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1}) |
||||
if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1}) |
||||
if stddef: conf.env.Append(CPPDEFINES = {'HAVE_STDDEF_H': 1}) |
||||
if string: conf.env.Append(CPPDEFINES = {'HAVE_STRING_H': 1}) |
||||
if stdlib: conf.env.Append(CPPDEFINES = {'HAVE_STDLIB_H': 1}) |
||||
|
||||
# Check if we can use pkg-config to find protobuf include path |
||||
status, output = conf.TryAction('pkg-config protobuf --variable=includedir > $TARGET') |
||||
if status: |
||||
conf.env.Append(PROTOCPATH = output.strip()) |
||||
else: |
||||
conf.env.Append(PROTOCPATH = '/usr/include') |
||||
|
||||
# Check protoc version |
||||
status, output = conf.TryAction('$PROTOC --version > $TARGET') |
||||
if status: |
||||
conf.env['PROTOC_VERSION'] = output |
||||
|
||||
# Check if libmudflap is available (only with GCC) |
||||
if 'gcc' in env['CC']: |
||||
if conf.CheckLib('mudflap'): |
||||
conf.env.Append(CCFLAGS = '-fmudflap') |
||||
conf.env.Append(LINKFLAGS = '-fmudflap') |
||||
|
||||
# Check if we can use extra strict warning flags (only with GCC) |
||||
extra = '-Wcast-qual -Wlogical-op -Wconversion' |
||||
extra += ' -fstrict-aliasing -Wstrict-aliasing=1' |
||||
extra += ' -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls' |
||||
extra += ' -Wstack-protector ' |
||||
if 'gcc' in env['CC']: |
||||
if conf.CheckCCFLAGS(extra): |
||||
conf.env.Append(CORECFLAGS = extra) |
||||
|
||||
# Check if we can use undefined behaviour sanitizer (only with clang) |
||||
extra = '-fsanitize=undefined ' |
||||
if 'clang' in env['CC']: |
||||
if conf.CheckCCFLAGS(extra, linkflags = extra): |
||||
conf.env.Append(CORECFLAGS = extra) |
||||
conf.env.Append(LINKFLAGS = extra) |
||||
|
||||
# End the config stuff |
||||
env = conf.Finish() |
||||
|
||||
# Initialize the CCFLAGS according to the compiler |
||||
if 'gcc' in env['CC']: |
||||
# GNU Compiler Collection |
||||
|
||||
# Debug info, warnings as errors |
||||
env.Append(CFLAGS = '-ansi -pedantic -g -Wall -Werror -fprofile-arcs -ftest-coverage ') |
||||
env.Append(CORECFLAGS = '-Wextra') |
||||
env.Append(LINKFLAGS = '-g --coverage') |
||||
|
||||
# We currently need uint64_t anyway, even though ANSI C90 otherwise.. |
||||
env.Append(CFLAGS = '-Wno-long-long') |
||||
elif 'clang' in env['CC']: |
||||
# CLang |
||||
env.Append(CFLAGS = '-ansi -g -Wall -Werror') |
||||
env.Append(CORECFLAGS = ' -Wextra -Wcast-qual -Wconversion') |
||||
elif 'cl' in env['CC']: |
||||
# Microsoft Visual C++ |
||||
|
||||
# Debug info on, warning level 2 for tests, warnings as errors |
||||
env.Append(CFLAGS = '/Zi /W2 /WX') |
||||
env.Append(LINKFLAGS = '/DEBUG') |
||||
|
||||
# More strict checks on the nanopb core |
||||
env.Append(CORECFLAGS = '/W4') |
||||
elif 'tcc' in env['CC']: |
||||
# Tiny C Compiler |
||||
env.Append(CFLAGS = '-Wall -Werror -g') |
||||
|
||||
env.SetDefault(CORECFLAGS = '') |
||||
|
||||
if 'clang' in env['CXX']: |
||||
env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers') |
||||
elif 'g++' in env['CXX'] or 'gcc' in env['CXX']: |
||||
env.Append(CXXFLAGS = '-g -Wall -Werror -Wextra -Wno-missing-field-initializers') |
||||
elif 'cl' in env['CXX']: |
||||
env.Append(CXXFLAGS = '/Zi /W2 /WX') |
||||
|
||||
# Now include the SConscript files from all subdirectories |
||||
import os.path |
||||
env['VARIANT_DIR'] = 'build' |
||||
env['BUILD'] = '#' + env['VARIANT_DIR'] |
||||
env['COMMON'] = '#' + env['VARIANT_DIR'] + '/common' |
||||
|
||||
# Include common/SConscript first to make sure its exports are available |
||||
# to other SConscripts. |
||||
SConscript("common/SConscript", exports = 'env', variant_dir = env['VARIANT_DIR'] + '/common') |
||||
|
||||
for subdir in Glob('*/SConscript') + Glob('regression/*/SConscript'): |
||||
if str(subdir).startswith("common"): continue |
||||
SConscript(subdir, exports = 'env', variant_dir = env['VARIANT_DIR'] + '/' + os.path.dirname(str(subdir))) |
||||
|
@ -1,35 +0,0 @@ |
||||
# Build and run a test that encodes and decodes a message that contains |
||||
# all of the Protocol Buffers data types. |
||||
|
||||
Import("env") |
||||
|
||||
env.NanopbProto(["alltypes", "alltypes.options"]) |
||||
enc = env.Program(["encode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_encode.o", "$COMMON/pb_common.o"]) |
||||
dec = env.Program(["decode_alltypes.c", "alltypes.pb.c", "$COMMON/pb_decode.o", "$COMMON/pb_common.o"]) |
||||
|
||||
# Test the round-trip from nanopb encoder to nanopb decoder |
||||
env.RunTest(enc) |
||||
env.RunTest([dec, "encode_alltypes.output"]) |
||||
|
||||
# Re-encode the data using protoc, and check that the results from nanopb |
||||
# match byte-per-byte to the protoc output. |
||||
env.Decode("encode_alltypes.output.decoded", |
||||
["encode_alltypes.output", "alltypes.proto"], |
||||
MESSAGE='AllTypes') |
||||
env.Encode("encode_alltypes.output.recoded", |
||||
["encode_alltypes.output.decoded", "alltypes.proto"], |
||||
MESSAGE='AllTypes') |
||||
env.Compare(["encode_alltypes.output", "encode_alltypes.output.recoded"]) |
||||
|
||||
# Do the same checks with the optional fields present. |
||||
env.RunTest("optionals.output", enc, ARGS = ['1']) |
||||
env.RunTest("optionals.decout", [dec, "optionals.output"], ARGS = ['1']) |
||||
env.Decode("optionals.output.decoded", |
||||
["optionals.output", "alltypes.proto"], |
||||
MESSAGE='AllTypes') |
||||
env.Encode("optionals.output.recoded", |
||||
["optionals.output.decoded", "alltypes.proto"], |
||||
MESSAGE='AllTypes') |
||||
env.Compare(["optionals.output", "optionals.output.recoded"]) |
||||
|
||||
|
@ -1,3 +0,0 @@ |
||||
* max_size:16 |
||||
* max_count:5 |
||||
|
@ -1,123 +0,0 @@ |
||||
syntax = "proto2"; |
||||
// package name placeholder |
||||
|
||||
message SubMessage { |
||||
required string substuff1 = 1 [default = "1"]; |
||||
required int32 substuff2 = 2 [default = 2]; |
||||
optional fixed32 substuff3 = 3 [default = 3]; |
||||
} |
||||
|
||||
message EmptyMessage { |
||||
|
||||
} |
||||
|
||||
enum HugeEnum { |
||||
Negative = -2147483647; /* protoc doesn't accept -2147483648 here */ |
||||
Positive = 2147483647; |
||||
} |
||||
|
||||
message Limits { |
||||
required int32 int32_min = 1 [default = 2147483647]; |
||||
required int32 int32_max = 2 [default = -2147483647]; |
||||
required uint32 uint32_min = 3 [default = 4294967295]; |
||||
required uint32 uint32_max = 4 [default = 0]; |
||||
required int64 int64_min = 5 [default = 9223372036854775807]; |
||||
required int64 int64_max = 6 [default = -9223372036854775807]; |
||||
required uint64 uint64_min = 7 [default = 18446744073709551615]; |
||||
required uint64 uint64_max = 8 [default = 0]; |
||||
required HugeEnum enum_min = 9 [default = Positive]; |
||||
required HugeEnum enum_max = 10 [default = Negative]; |
||||
} |
||||
|
||||
enum MyEnum { |
||||
Zero = 0; |
||||
First = 1; |
||||
Second = 2; |
||||
Truth = 42; |
||||
} |
||||
|
||||
message AllTypes { |
||||
required int32 req_int32 = 1; |
||||
required int64 req_int64 = 2; |
||||
required uint32 req_uint32 = 3; |
||||
required uint64 req_uint64 = 4; |
||||
required sint32 req_sint32 = 5; |
||||
required sint64 req_sint64 = 6; |
||||
required bool req_bool = 7; |
||||
|
||||
required fixed32 req_fixed32 = 8; |
||||
required sfixed32 req_sfixed32= 9; |
||||
required float req_float = 10; |
||||
|
||||
required fixed64 req_fixed64 = 11; |
||||
required sfixed64 req_sfixed64= 12; |
||||
required double req_double = 13; |
||||
|
||||
required string req_string = 14; |
||||
required bytes req_bytes = 15; |
||||
required SubMessage req_submsg = 16; |
||||
required MyEnum req_enum = 17; |
||||
required EmptyMessage req_emptymsg = 18; |
||||
|
||||
|
||||
repeated int32 rep_int32 = 21 [packed = true]; |
||||
repeated int64 rep_int64 = 22 [packed = true]; |
||||
repeated uint32 rep_uint32 = 23 [packed = true]; |
||||
repeated uint64 rep_uint64 = 24 [packed = true]; |
||||
repeated sint32 rep_sint32 = 25 [packed = true]; |
||||
repeated sint64 rep_sint64 = 26 [packed = true]; |
||||
repeated bool rep_bool = 27 [packed = true]; |
||||
|
||||
repeated fixed32 rep_fixed32 = 28 [packed = true]; |
||||
repeated sfixed32 rep_sfixed32= 29 [packed = true]; |
||||
repeated float rep_float = 30 [packed = true]; |
||||
|
||||
repeated fixed64 rep_fixed64 = 31 [packed = true]; |
||||
repeated sfixed64 rep_sfixed64= 32 [packed = true]; |
||||
repeated double rep_double = 33 [packed = true]; |
||||
|
||||
repeated string rep_string = 34; |
||||
repeated bytes rep_bytes = 35; |
||||
repeated SubMessage rep_submsg = 36; |
||||
repeated MyEnum rep_enum = 37 [packed = true]; |
||||
repeated EmptyMessage rep_emptymsg = 38; |
||||
|
||||
optional int32 opt_int32 = 41 [default = 4041]; |
||||
optional int64 opt_int64 = 42 [default = 4042]; |
||||
optional uint32 opt_uint32 = 43 [default = 4043]; |
||||
optional uint64 opt_uint64 = 44 [default = 4044]; |
||||
optional sint32 opt_sint32 = 45 [default = 4045]; |
||||
optional sint64 opt_sint64 = 46 [default = 4046]; |
||||
optional bool opt_bool = 47 [default = false]; |
||||
|
||||
optional fixed32 opt_fixed32 = 48 [default = 4048]; |
||||
optional sfixed32 opt_sfixed32= 49 [default = 4049]; |
||||
optional float opt_float = 50 [default = 4050]; |
||||
|
||||
optional fixed64 opt_fixed64 = 51 [default = 4051]; |
||||
optional sfixed64 opt_sfixed64= 52 [default = 4052]; |
||||
optional double opt_double = 53 [default = 4053]; |
||||
|
||||
optional string opt_string = 54 [default = "4054"]; |
||||
optional bytes opt_bytes = 55 [default = "4055"]; |
||||
optional SubMessage opt_submsg = 56; |
||||
optional MyEnum opt_enum = 57 [default = Second]; |
||||
optional EmptyMessage opt_emptymsg = 58; |
||||
|
||||
oneof oneof |
||||
{ |
||||
SubMessage oneof_msg1 = 59; |
||||
EmptyMessage oneof_msg2 = 60; |
||||
} |
||||
|
||||
// Check that extreme integer values are handled correctly |
||||
required Limits req_limits = 98; |
||||
|
||||
// Just to make sure that the size of the fields has been calculated |
||||
// properly, i.e. otherwise a bug in last field might not be detected. |
||||
required int32 end = 99; |
||||
|
||||
|
||||
extensions 200 to 255; |
||||
} |
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue