Compare commits

...

3 Commits

Author SHA1 Message Date
Petteri Aimonen
0cdc623050 Modified nanopb_generator.py to generate includes for other .proto files.
Implementation was suggested by extremeblue99.
Fixes issue 4.
2012-02-15 17:34:48 +02:00
Petteri Aimonen
f6b08404fa Fixed nanopb_generator.py to read the input file in binary mode. 2012-01-30 10:36:17 +02:00
Petteri Aimonen
b36a1a259a Improved documentation on field decoders. 2012-01-23 18:13:26 +02:00
2 changed files with 37 additions and 8 deletions

View File

@@ -376,7 +376,12 @@ Because of memory concerns, the detection of missing required fields is not perf
Each field decoder reads and decodes a single value. For arrays, the decoder is called repeatedly.
You can use the decoders from your callbacks. Just be aware that the pb_field_t passed to the callback is not directly compatible with most of the field decoders. Instead, you must create a new pb_field_t structure and set the data_size according to the data type you pass to *dest*.
You can use the decoders from your callbacks. Just be aware that the pb_field_t passed to the callback is not directly compatible
with the *varint* field decoders. Instead, you must create a new pb_field_t structure and set the data_size according to the data type
you pass to *dest*, e.g. *field.data_size = sizeof(int);*. Other fields in the *pb_field_t* don't matter.
The field decoder interface is a bit messy as a result of the interface required inside the nanopb library.
Eventually they may be replaced by separate wrapper functions with a more friendly interface.
pb_dec_varint
-------------
@@ -403,12 +408,12 @@ pb_dec_fixed32
--------------
Field decoder for PB_LTYPE_FIXED32. ::
bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest);
bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest);
:stream: Input stream to read from. 1-10 bytes will be read.
:stream: Input stream to read from. 4 bytes will be read.
:field: Not used.
:dest: Pointer to destination integer. Must have size of *field->data_size* bytes.
:returns: True on success, false on IO errors or if `pb_decode_varint`_ fails.
: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.
@@ -420,6 +425,11 @@ Field decoder for PB_LTYPE_FIXED64. ::
bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest);
:stream: Input stream to read from. 8 bytes will be read.
:field: Not used.
:dest: Pointer to destination *int64_t*, *uint64_t* or *double*.
:returns: True on success, false on IO errors.
Same as `pb_dec_fixed32`_, except this reads 8 bytes.
pb_dec_bytes
@@ -428,6 +438,9 @@ Field decoder for PB_LTYPE_BYTES. Reads a length-prefixed block of bytes. ::
bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest);
**Note:** This is an internal function that is not useful in decoder callbacks. To read bytes fields in callbacks, use
*stream->bytes_left* and `pb_read`_.
:stream: Input stream to read from.
:field: Field description structure. Only *field->data_size* matters.
:dest: Pointer to a structure similar to pb_bytes_array_t.
@@ -441,6 +454,9 @@ Field decoder for PB_LTYPE_STRING. Reads a length-prefixed string. ::
bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest);
**Note:** This is an internal function that is not useful in decoder callbacks. To read string fields in callbacks, use
*stream->bytes_left* and `pb_read`_.
:stream: Input stream to read from.
:field: Field description structure. Only *field->data_size* matters.
:dest: Pointer to a character array of size *field->data_size*.
@@ -454,6 +470,9 @@ Field decoder for PB_LTYPE_SUBMESSAGE. Calls `pb_decode`_ to perform the actual
bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
**Note:** This is an internal function that is not useful in decoder callbacks. To read submessage fields in callbacks, use
`pb_decode`_ directly.
:stream: Input stream to read from.
:field: Field description structure. Only *field->ptr* matters.
:dest: Pointer to the destination structure.

View File

@@ -347,7 +347,7 @@ def sort_dependencies(messages):
if msgname in message_by_name:
yield message_by_name[msgname]
def generate_header(headername, enums, messages):
def generate_header(dependencies, headername, enums, messages):
'''Generate content for a header file.
Generates strings, which should be concatenated and stored to file.
'''
@@ -359,6 +359,11 @@ def generate_header(headername, enums, messages):
yield '#define _PB_%s_\n' % symbol
yield '#include <pb.h>\n\n'
for dependency in dependencies:
noext = os.path.splitext(dependency)[0]
yield '#include "%s.pb.h"\n' % noext
yield '\n'
yield '/* Enum definitions */\n'
for enum in enums:
yield str(enum) + '\n\n'
@@ -404,7 +409,7 @@ if __name__ == '__main__':
print "Output fill be written to file.pb.h and file.pb.c"
sys.exit(1)
data = open(sys.argv[1]).read()
data = open(sys.argv[1], 'rb').read()
fdesc = descriptor.FileDescriptorSet.FromString(data)
enums, messages = parse_file(fdesc.file[0])
@@ -415,8 +420,13 @@ if __name__ == '__main__':
print "Writing to " + headername + " and " + sourcename
# List of .proto files that should not be included in the C header file
# even if they are mentioned in the source .proto.
excludes = ['nanopb.proto']
dependencies = [d for d in fdesc.file[0].dependency if d not in excludes]
header = open(headername, 'w')
for part in generate_header(headerbasename, enums, messages):
for part in generate_header(dependencies, headerbasename, enums, messages):
header.write(part)
source = open(sourcename, 'w')