aboutsummaryrefslogtreecommitdiffstats
path: root/docs/concepts.rst
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-08-16 17:28:59 +0000
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-08-16 17:28:59 +0000
commit2cefaeaf61f38f1566293e53b4708e9ceff2d945 (patch)
tree83550d3138e34d99b2c09b4a2c5e2418c7e2efb2 /docs/concepts.rst
parent842d52633d650286ce62490362f8dfa356e17800 (diff)
Docs
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@956 e3a754e5-d11d-0410-8d38-ebb782a927b9
Diffstat (limited to 'docs/concepts.rst')
-rw-r--r--docs/concepts.rst105
1 files changed, 95 insertions, 10 deletions
diff --git a/docs/concepts.rst b/docs/concepts.rst
index f2475aec..58620a85 100644
--- a/docs/concepts.rst
+++ b/docs/concepts.rst
@@ -2,12 +2,42 @@
Nanopb: Basic concepts
======================
-The things outlined here are common to both the encoder and the decoder part.
+.. include :: menu.rst
-.. sectnum::
+The things outlined here are common to both the encoder and the decoder part.
.. 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:~$
+
+Compiling .proto files with nanopb options
+------------------------------------------
+Nanopb defines two extensions for message fields described in .proto files: *max_size* and *max_count*.
+These are the maximum size of a string and maximum count of items in an array::
+
+ required string name = 1 [(nanopb).max_size = 40];
+ repeated PhoneNumber phone = 4 [(nanopb).max_count = 5];
+
+To use these extensions, you need to place an import statement in the beginning of the file::
+
+ import "nanopb.proto";
+
+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
+
Streams
=======
@@ -130,21 +160,78 @@ required bytes data = 1 [(nanopb).max_size = 40];
The maximum lengths are checked in runtime. If string/bytes/array exceeds the allocated length, *pb_decode* will return false.
-For more information about callbacks, see the `Encoding` and `Decoding` sections.
+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 you can use for passing data to the callback. The actual behavior of the callback function is different in encoding and decoding modes.
+
+.. _`pb_callback_t`: reference.html#pb-callback-t
+
+Encoding callbacks
+------------------
+::
+
+ bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *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.
+
+The callback may be called multiple times during a single call to `pb_encode`_. It must produce the same amount of data every time.
+
+.. _`pb_encode`: reference.html#pb-encode
+
+This callback writes out a dynamically sized string::
+
+ bool write_string(pb_ostream_t *stream, const pb_field_t *field, const void *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.
+
+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 PhoneNumber {
- required string number = 1 [(nanopb).max_size = 40];
- optional PhoneType type = 2 [default = HOME];
+ 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] = {
{1, PB_HTYPE_REQUIRED | PB_LTYPE_STRING,
@@ -160,8 +247,6 @@ For using the *pb_encode* and *pb_decode* functions, you need an array of pb_fie
PB_LAST_FIELD
};
-For more information about the format, see the `Generated code` section.
-
Return values and error handling
================================