xref: /aosp_15_r20/external/nanopb-c/docs/index.rst (revision c8d645cafcee3f91213d30caa0fe303887010b9b)
1*c8d645caSAndroid Build Coastguard Worker=============================================
2*c8d645caSAndroid Build Coastguard WorkerNanopb: Protocol Buffers with small code size
3*c8d645caSAndroid Build Coastguard Worker=============================================
4*c8d645caSAndroid Build Coastguard Worker
5*c8d645caSAndroid Build Coastguard Worker.. include :: menu.rst
6*c8d645caSAndroid Build Coastguard Worker
7*c8d645caSAndroid Build Coastguard WorkerNanopb is an ANSI-C library for encoding and decoding messages in Google's `Protocol Buffers`__ format with minimal requirements for RAM and code space.
8*c8d645caSAndroid Build Coastguard WorkerIt is primarily suitable for 32-bit microcontrollers.
9*c8d645caSAndroid Build Coastguard Worker
10*c8d645caSAndroid Build Coastguard Worker__ https://developers.google.com/protocol-buffers/docs/reference/overview
11*c8d645caSAndroid Build Coastguard Worker
12*c8d645caSAndroid Build Coastguard WorkerOverall structure
13*c8d645caSAndroid Build Coastguard Worker=================
14*c8d645caSAndroid Build Coastguard Worker
15*c8d645caSAndroid Build Coastguard WorkerFor the runtime program, you always need *pb.h* for type declarations.
16*c8d645caSAndroid Build Coastguard WorkerDepending on whether you want to encode, decode, or both, you also need *pb_encode.h/c* or *pb_decode.h/c*.
17*c8d645caSAndroid Build Coastguard Worker
18*c8d645caSAndroid Build Coastguard WorkerThe 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.
19*c8d645caSAndroid Build Coastguard Worker
20*c8d645caSAndroid Build Coastguard Worker.. image:: generator_flow.png
21*c8d645caSAndroid Build Coastguard Worker
22*c8d645caSAndroid Build Coastguard WorkerSo a typical project might include these files:
23*c8d645caSAndroid Build Coastguard Worker
24*c8d645caSAndroid Build Coastguard Worker1) Nanopb runtime library:
25*c8d645caSAndroid Build Coastguard Worker    - pb.h
26*c8d645caSAndroid Build Coastguard Worker    - pb_common.h and pb_common.c (always needed)
27*c8d645caSAndroid Build Coastguard Worker    - pb_decode.h and pb_decode.c (needed for decoding messages)
28*c8d645caSAndroid Build Coastguard Worker    - pb_encode.h and pb_encode.c (needed for encoding messages)
29*c8d645caSAndroid Build Coastguard Worker2) Protocol description (you can have many):
30*c8d645caSAndroid Build Coastguard Worker    - person.proto (just an example)
31*c8d645caSAndroid Build Coastguard Worker    - person.pb.c (autogenerated, contains initializers for const arrays)
32*c8d645caSAndroid Build Coastguard Worker    - person.pb.h (autogenerated, contains type declarations)
33*c8d645caSAndroid Build Coastguard Worker
34*c8d645caSAndroid Build Coastguard WorkerFeatures and limitations
35*c8d645caSAndroid Build Coastguard Worker========================
36*c8d645caSAndroid Build Coastguard Worker
37*c8d645caSAndroid Build Coastguard Worker**Features**
38*c8d645caSAndroid Build Coastguard Worker
39*c8d645caSAndroid Build Coastguard Worker#) Pure C runtime
40*c8d645caSAndroid Build Coastguard Worker#) Small code size (2–10 kB depending on processor, plus any message definitions)
41*c8d645caSAndroid Build Coastguard Worker#) Small ram usage (typically ~300 bytes, plus any message structs)
42*c8d645caSAndroid Build Coastguard Worker#) Allows specifying maximum size for strings and arrays, so that they can be allocated statically.
43*c8d645caSAndroid Build Coastguard Worker#) No malloc needed: everything can be allocated statically or on the stack. Optional malloc support available.
44*c8d645caSAndroid Build Coastguard Worker#) You can use either encoder or decoder alone to cut the code size in half.
45*c8d645caSAndroid Build Coastguard Worker#) Support for most protobuf features, including: all data types, nested submessages, default values, repeated and optional fields, oneofs, packed arrays, extension fields.
46*c8d645caSAndroid Build Coastguard Worker#) Callback mechanism for handling messages larger than can fit in available RAM.
47*c8d645caSAndroid Build Coastguard Worker#) Extensive set of tests.
48*c8d645caSAndroid Build Coastguard Worker
49*c8d645caSAndroid Build Coastguard Worker**Limitations**
50*c8d645caSAndroid Build Coastguard Worker
51*c8d645caSAndroid Build Coastguard Worker#) Some speed has been sacrificed for code size.
52*c8d645caSAndroid Build Coastguard Worker#) Encoding is focused on writing to streams. For memory buffers only it could be made more efficient.
53*c8d645caSAndroid Build Coastguard Worker#) The deprecated Protocol Buffers feature called "groups" is not supported.
54*c8d645caSAndroid Build Coastguard Worker#) Fields in the generated structs are ordered by the tag number, instead of the natural ordering in .proto file.
55*c8d645caSAndroid Build Coastguard Worker#) Unknown fields are not preserved when decoding and re-encoding a message.
56*c8d645caSAndroid Build Coastguard Worker#) Reflection (runtime introspection) is not supported. E.g. you can't request a field by giving its name in a string.
57*c8d645caSAndroid Build Coastguard Worker#) Numeric arrays are always encoded as packed, even if not marked as packed in .proto.
58*c8d645caSAndroid Build Coastguard Worker#) Cyclic references between messages are supported only in callback and malloc mode.
59*c8d645caSAndroid Build Coastguard Worker
60*c8d645caSAndroid Build Coastguard WorkerGetting started
61*c8d645caSAndroid Build Coastguard Worker===============
62*c8d645caSAndroid Build Coastguard Worker
63*c8d645caSAndroid Build Coastguard WorkerFor starters, consider this simple message::
64*c8d645caSAndroid Build Coastguard Worker
65*c8d645caSAndroid Build Coastguard Worker message Example {
66*c8d645caSAndroid Build Coastguard Worker    required int32 value = 1;
67*c8d645caSAndroid Build Coastguard Worker }
68*c8d645caSAndroid Build Coastguard Worker
69*c8d645caSAndroid Build Coastguard WorkerSave this in *message.proto* and compile it::
70*c8d645caSAndroid Build Coastguard Worker
71*c8d645caSAndroid Build Coastguard Worker    user@host:~$ protoc -omessage.pb message.proto
72*c8d645caSAndroid Build Coastguard Worker    user@host:~$ python nanopb/generator/nanopb_generator.py message.pb
73*c8d645caSAndroid Build Coastguard Worker
74*c8d645caSAndroid Build Coastguard WorkerYou should now have in *message.pb.h*::
75*c8d645caSAndroid Build Coastguard Worker
76*c8d645caSAndroid Build Coastguard Worker typedef struct {
77*c8d645caSAndroid Build Coastguard Worker    int32_t value;
78*c8d645caSAndroid Build Coastguard Worker } Example;
79*c8d645caSAndroid Build Coastguard Worker
80*c8d645caSAndroid Build Coastguard Worker extern const pb_field_t Example_fields[2];
81*c8d645caSAndroid Build Coastguard Worker
82*c8d645caSAndroid Build Coastguard WorkerNow in your main program do this to encode a message::
83*c8d645caSAndroid Build Coastguard Worker
84*c8d645caSAndroid Build Coastguard Worker Example mymessage = {42};
85*c8d645caSAndroid Build Coastguard Worker uint8_t buffer[10];
86*c8d645caSAndroid Build Coastguard Worker pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
87*c8d645caSAndroid Build Coastguard Worker pb_encode(&stream, Example_fields, &mymessage);
88*c8d645caSAndroid Build Coastguard Worker
89*c8d645caSAndroid Build Coastguard WorkerAfter that, buffer will contain the encoded message.
90*c8d645caSAndroid Build Coastguard WorkerThe number of bytes in the message is stored in *stream.bytes_written*.
91*c8d645caSAndroid Build Coastguard WorkerYou can feed the message to *protoc --decode=Example message.proto* to verify its validity.
92*c8d645caSAndroid Build Coastguard Worker
93*c8d645caSAndroid Build Coastguard WorkerFor a complete example of the simple case, see *example/simple.c*.
94*c8d645caSAndroid Build Coastguard WorkerFor a more complex example with network interface, see the *example/network_server* subdirectory.
95*c8d645caSAndroid Build Coastguard Worker
96*c8d645caSAndroid Build Coastguard WorkerCompiler requirements
97*c8d645caSAndroid Build Coastguard Worker=====================
98*c8d645caSAndroid Build Coastguard WorkerNanopb should compile with most ansi-C compatible compilers. It however
99*c8d645caSAndroid Build Coastguard Workerrequires a few header files to be available:
100*c8d645caSAndroid Build Coastguard Worker
101*c8d645caSAndroid Build Coastguard Worker#) *string.h*, with these functions: *strlen*, *memcpy*, *memset*
102*c8d645caSAndroid Build Coastguard Worker#) *stdint.h*, for definitions of *int32_t* etc.
103*c8d645caSAndroid Build Coastguard Worker#) *stddef.h*, for definition of *size_t*
104*c8d645caSAndroid Build Coastguard Worker#) *stdbool.h*, for definition of *bool*
105*c8d645caSAndroid Build Coastguard Worker
106*c8d645caSAndroid Build Coastguard WorkerIf these header files do not come with your compiler, you can use the
107*c8d645caSAndroid Build Coastguard Workerfile *extra/pb_syshdr.h* instead. It contains an example of how to provide
108*c8d645caSAndroid Build Coastguard Workerthe dependencies. You may have to edit it a bit to suit your custom platform.
109*c8d645caSAndroid Build Coastguard Worker
110*c8d645caSAndroid Build Coastguard WorkerTo use the pb_syshdr.h, define *PB_SYSTEM_HEADER* as *"pb_syshdr.h"* (including the quotes).
111*c8d645caSAndroid Build Coastguard WorkerSimilarly, you can provide a custom include file, which should provide all the dependencies
112*c8d645caSAndroid Build Coastguard Workerlisted above.
113*c8d645caSAndroid Build Coastguard Worker
114*c8d645caSAndroid Build Coastguard WorkerRunning the test cases
115*c8d645caSAndroid Build Coastguard Worker======================
116*c8d645caSAndroid Build Coastguard WorkerExtensive unittests and test cases are included under the *tests* folder.
117*c8d645caSAndroid Build Coastguard Worker
118*c8d645caSAndroid Build Coastguard WorkerTo build the tests, you will need the `scons`__ build system. The tests should
119*c8d645caSAndroid Build Coastguard Workerbe runnable on most platforms. Windows and Linux builds are regularly tested.
120*c8d645caSAndroid Build Coastguard Worker
121*c8d645caSAndroid Build Coastguard Worker__ http://www.scons.org/
122*c8d645caSAndroid Build Coastguard Worker
123*c8d645caSAndroid Build Coastguard WorkerIn addition to the build system, you will also need a working Google Protocol
124*c8d645caSAndroid Build Coastguard WorkerBuffers *protoc* compiler, and the Python bindings for Protocol Buffers. On
125*c8d645caSAndroid Build Coastguard WorkerDebian-based systems, install the following packages: *protobuf-compiler*,
126*c8d645caSAndroid Build Coastguard Worker*python-protobuf* and *libprotobuf-dev*.
127*c8d645caSAndroid Build Coastguard Worker
128