Compare commits
11 Commits
nanopb-0.1
...
nanopb-0.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09ec60cadf | ||
|
|
871e5be9dd | ||
|
|
068de05c51 | ||
|
|
1f8fb1f1ed | ||
|
|
434dcbb2ee | ||
|
|
59788e2aab | ||
|
|
cc29958d34 | ||
|
|
0abb764b18 | ||
|
|
02ecee2de8 | ||
|
|
332a9ee95c | ||
|
|
fc6f56b2bd |
@@ -1,4 +1,10 @@
|
||||
nanopb-0.1.7 (not yet released)
|
||||
nanopb-0.1.8
|
||||
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
|
||||
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)
|
||||
|
||||
@@ -381,7 +381,7 @@ Remove the data for a field from the stream, without actually decoding it::
|
||||
|
||||
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->state* and read the data using `pb_read`_.
|
||||
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.
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
'''Generate header file for nanopb from a ProtoBuf FileDescriptorSet.'''
|
||||
nanopb_version = "nanopb-0.1.7"
|
||||
nanopb_version = "nanopb-0.1.8"
|
||||
|
||||
try:
|
||||
import google.protobuf.descriptor_pb2 as descriptor
|
||||
@@ -73,6 +73,9 @@ class Names:
|
||||
else:
|
||||
raise ValueError("Name parts should be of type str")
|
||||
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, Names) and self.parts == other.parts
|
||||
|
||||
def names_from_type_name(type_name):
|
||||
'''Parse Names() from FieldDescriptorProto type_name'''
|
||||
if type_name[0] != '.':
|
||||
@@ -83,12 +86,15 @@ class Enum:
|
||||
def __init__(self, names, desc, enum_options):
|
||||
'''desc is EnumDescriptorProto'''
|
||||
|
||||
if enum_options.long_names:
|
||||
self.names = names + desc.name
|
||||
else:
|
||||
self.names = names
|
||||
self.options = enum_options
|
||||
self.names = names + desc.name
|
||||
|
||||
self.values = [(self.names + x.name, x.number) for x in desc.value]
|
||||
if enum_options.long_names:
|
||||
self.values = [(self.names + x.name, x.number) for x in desc.value]
|
||||
else:
|
||||
self.values = [(names + x.name, x.number) for x in desc.value]
|
||||
|
||||
self.value_longnames = [self.names + x.name for x in desc.value]
|
||||
|
||||
def __str__(self):
|
||||
result = 'typedef enum _%s {\n' % self.names
|
||||
@@ -389,7 +395,17 @@ def parse_file(fdesc, file_options):
|
||||
message_options = get_nanopb_suboptions(message, file_options)
|
||||
messages.append(Message(names, message, message_options))
|
||||
for enum in message.enum_type:
|
||||
enums.append(Enum(names, enum, message_options))
|
||||
enum_options = get_nanopb_suboptions(enum, message_options)
|
||||
enums.append(Enum(names, enum, enum_options))
|
||||
|
||||
# Fix field default values where enum short names are used.
|
||||
for enum in enums:
|
||||
if not enum.options.long_names:
|
||||
for message in messages:
|
||||
for field in message.fields:
|
||||
if field.default in enum.value_longnames:
|
||||
idx = enum.value_longnames.index(field.default)
|
||||
field.default = enum.values[idx][0]
|
||||
|
||||
return enums, messages
|
||||
|
||||
@@ -477,7 +493,9 @@ def generate_header(dependencies, headername, enums, messages):
|
||||
worst = 0
|
||||
worst_field = ''
|
||||
checks = []
|
||||
checks_msgnames = []
|
||||
for msg in messages:
|
||||
checks_msgnames.append(msg.name)
|
||||
for field in msg.fields:
|
||||
status = field.largest_field_value()
|
||||
if isinstance(status, (str, unicode)):
|
||||
@@ -495,7 +513,8 @@ def generate_header(dependencies, headername, enums, messages):
|
||||
yield '#error Field descriptor for %s is too large. Define PB_FIELD_16BIT to fix this.\n' % worst_field
|
||||
else:
|
||||
assertion = ' && '.join(str(c) + ' < 256' for c in checks)
|
||||
yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_16BIT)\n' % assertion
|
||||
msgs = '_'.join(str(n) for n in checks_msgnames)
|
||||
yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_%s)\n'%(assertion,msgs)
|
||||
yield '#endif\n\n'
|
||||
|
||||
if worst > 65535 or checks:
|
||||
@@ -504,7 +523,8 @@ def generate_header(dependencies, headername, enums, messages):
|
||||
yield '#error Field descriptor for %s is too large. Define PB_FIELD_32BIT to fix this.\n' % worst_field
|
||||
else:
|
||||
assertion = ' && '.join(str(c) + ' < 65536' for c in checks)
|
||||
yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_32BIT)\n' % assertion
|
||||
msgs = '_'.join(str(n) for n in checks_msgnames)
|
||||
yield 'STATIC_ASSERT((%s), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_%s)\n'%(assertion,msgs)
|
||||
yield '#endif\n'
|
||||
|
||||
yield '\n#ifdef __cplusplus\n'
|
||||
|
||||
10
pb.h
10
pb.h
@@ -6,7 +6,7 @@
|
||||
* see pb_encode.h or pb_decode.h
|
||||
*/
|
||||
|
||||
#define NANOPB_VERSION nanopb-0.1.7
|
||||
#define NANOPB_VERSION nanopb-0.1.8
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
@@ -24,9 +24,13 @@
|
||||
#define UNUSED(x) (void)(x)
|
||||
#endif
|
||||
|
||||
/* Compile-time assertion, used for checking compatible compilation options. */
|
||||
/* Compile-time assertion, used for checking compatible compilation options.
|
||||
* If this fails on your compiler for some reason, use #define STATIC_ASSERT
|
||||
* to disable it. */
|
||||
#ifndef STATIC_ASSERT
|
||||
#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1];
|
||||
#define STATIC_ASSERT(COND,MSG) typedef char STATIC_ASSERT_MSG(MSG, __LINE__, __COUNTER__)[(COND)?1:-1];
|
||||
#define STATIC_ASSERT_MSG(MSG, LINE, COUNTER) STATIC_ASSERT_MSG_(MSG, LINE, COUNTER)
|
||||
#define STATIC_ASSERT_MSG_(MSG, LINE, COUNTER) static_assertion_##MSG##LINE##COUNTER
|
||||
#endif
|
||||
|
||||
/* Number of required fields to keep track of
|
||||
|
||||
@@ -80,6 +80,9 @@ pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
|
||||
stream.callback = &buf_read;
|
||||
stream.state = buf;
|
||||
stream.bytes_left = bufsize;
|
||||
#ifndef PB_NO_ERRMSG
|
||||
stream.errmsg = NULL;
|
||||
#endif
|
||||
return stream;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,30 @@
|
||||
CFLAGS=-ansi -Wall -Werror -I .. -g -O0 --coverage
|
||||
LDFLAGS=--coverage
|
||||
DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h missing_fields.pb.h
|
||||
TESTS=test_decode1 test_encode1 decode_unittests encode_unittests test_no_messages
|
||||
CFLAGS=-ansi -Wall -Werror -I .. -g -O0
|
||||
DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h \
|
||||
callbacks2.pb.h callbacks.pb.h unittests.h unittestproto.pb.h \
|
||||
alltypes.pb.h missing_fields.pb.h
|
||||
TESTS= decode_unittests encode_unittests \
|
||||
test_decode1 test_decode2 test_decode3 \
|
||||
test_encode1 test_encode2 test_encode3 \
|
||||
test_decode_callbacks test_encode_callbacks \
|
||||
test_missing_fields test_no_messages \
|
||||
test_multiple_files test_cxxcompile test_options
|
||||
|
||||
# More strict checks for the core part of nanopb
|
||||
CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wlogical-op -Wconversion
|
||||
CC_VERSION=$(shell $(CC) -v 2>&1)
|
||||
CFLAGS_CORE=
|
||||
ifneq (,$(findstring gcc,$(CC_VERSION)))
|
||||
CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wlogical-op -Wconversion
|
||||
CFLAGS+=--coverage
|
||||
LDFLAGS+=--coverage
|
||||
endif
|
||||
ifneq (,$(findstring clang,$(CC_VERSION)))
|
||||
CFLAGS_CORE=-pedantic -Wextra -Wcast-qual -Wconversion
|
||||
endif
|
||||
|
||||
all: breakpoints $(TESTS) run_unittests
|
||||
|
||||
clean:
|
||||
rm -f $(TESTS) person.pb* alltypes.pb* *.o *.gcda *.gcno
|
||||
rm -f $(TESTS) person.pb* alltypes.pb* *.o *.gcda *.gcno *.pb.h *.pb.c
|
||||
|
||||
%.pb.o: %.pb.c %.pb.h
|
||||
$(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
|
||||
@@ -35,6 +50,7 @@ test_decode3: test_decode3.o pb_decode.o alltypes.pb.o
|
||||
test_encode1: test_encode1.o pb_encode.o person.pb.o
|
||||
test_encode2: test_encode2.o pb_encode.o person.pb.o
|
||||
test_encode3: test_encode3.o pb_encode.o alltypes.pb.o
|
||||
test_multiple_files: test_multiple_files.o pb_encode.o callbacks2.pb.o callbacks.pb.o
|
||||
test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.pb.o
|
||||
test_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
|
||||
test_missing_fields: test_missing_fields.o pb_encode.o pb_decode.o missing_fields.pb.o
|
||||
@@ -55,7 +71,7 @@ coverage: run_unittests
|
||||
gcov pb_encode.gcda
|
||||
gcov pb_decode.gcda
|
||||
|
||||
run_unittests: decode_unittests encode_unittests test_cxxcompile test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks test_missing_fields test_options
|
||||
run_unittests: $(TESTS)
|
||||
rm -f *.gcda
|
||||
|
||||
./decode_unittests > /dev/null
|
||||
@@ -82,7 +98,7 @@ run_unittests: decode_unittests encode_unittests test_cxxcompile test_encode1 te
|
||||
|
||||
./test_missing_fields
|
||||
|
||||
test_options: options.pb.h options.expected
|
||||
test_options: options.pb.h options.expected options.pb.o
|
||||
cat options.expected | while read -r p; do \
|
||||
if ! grep -q "$$p" $<; then \
|
||||
echo Expected: "$$p"; \
|
||||
|
||||
9
tests/callbacks2.proto
Normal file
9
tests/callbacks2.proto
Normal file
@@ -0,0 +1,9 @@
|
||||
// Test if including generated header file for this file + implicit include of
|
||||
// callbacks.pb.h still compiles. Used with test_compiles.c.
|
||||
import "callbacks.proto";
|
||||
|
||||
message Callback2Message {
|
||||
required TestMessage tstmsg = 1;
|
||||
required SubMessage submsg = 2;
|
||||
}
|
||||
|
||||
@@ -2,4 +2,5 @@ char filesize\[20\];
|
||||
char msgsize\[30\];
|
||||
char fieldsize\[40\];
|
||||
pb_callback_t int32_callback;
|
||||
[^_]EnumValue1 = 1
|
||||
\sEnumValue1 = 1
|
||||
Message5_EnumValue1
|
||||
|
||||
@@ -36,4 +36,21 @@ enum Enum1
|
||||
{
|
||||
option (nanopb_enumopt).long_names = false;
|
||||
EnumValue1 = 1;
|
||||
EnumValue2 = 2;
|
||||
}
|
||||
|
||||
message EnumTest
|
||||
{
|
||||
required Enum1 field = 1 [default = EnumValue2];
|
||||
}
|
||||
|
||||
// Short enum names inside message
|
||||
message Message5
|
||||
{
|
||||
enum Enum2
|
||||
{
|
||||
option (nanopb_enumopt).long_names = false;
|
||||
EnumValue1 = 1;
|
||||
}
|
||||
required Enum2 field = 1 [default = EnumValue1];
|
||||
}
|
||||
|
||||
13
tests/test_multiple_files.c
Normal file
13
tests/test_multiple_files.c
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Tests if still compile if typedefs are redfefined in STATIC_ASSERTS when
|
||||
* proto file includes another poto file
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <pb_encode.h>
|
||||
#include "callbacks2.pb.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user