Support dynamic allocation for string, bytes and message fields.
This is turned on by passing -p to nanopb_generator.py or setting the (nanopb).pointer option for a .proto field. git-svn-id: https://svn.kapsi.fi/jpa/nanopb-dev@1081 e3a754e5-d11d-0410-8d38-ebb782a927b9
This commit is contained in:
committed by
Petteri Aimonen
parent
8e5337e9ef
commit
c66c6b43c4
@@ -1,12 +1,12 @@
|
||||
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
|
||||
TESTS=test_decode1 test_encode1 decode_unittests encode_unittests
|
||||
TESTS=test_decode1 test_encode1 test_decode_callbacks test_encode_callbacks decode_unittests decode_ptr_unittests encode_unittests
|
||||
|
||||
all: breakpoints $(TESTS) run_unittests
|
||||
|
||||
clean:
|
||||
rm -f $(TESTS) person.pb* *.o *.gcda *.gcno
|
||||
rm -f $(TESTS) *.pb.c *.pb.h *.o *.gcda *.gcno
|
||||
|
||||
%.o: %.c
|
||||
%.o: %.c $(DEPS)
|
||||
@@ -16,12 +16,18 @@ pb_encode.o: ../pb_encode.c $(DEPS)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
pb_decode.o: ../pb_decode.c $(DEPS)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
pb_ptr_decode.o: ../pb_decode.c $(DEPS)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
decode_ptr_unittests.o: decode_unittests.c $(DEPS)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
test_decode1: test_decode1.o pb_decode.o person.pb.o
|
||||
test_encode1: test_encode1.o pb_encode.o person.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
|
||||
decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
|
||||
pb_ptr_decode.o decode_ptr_unittests.o: CFLAGS += -DMALLOC_HEADER="<stdlib.h>"
|
||||
decode_ptr_unittests: decode_ptr_unittests.o pb_ptr_decode.o unittestproto.pb.o
|
||||
encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
|
||||
|
||||
%.pb: %.proto
|
||||
@@ -37,10 +43,11 @@ coverage: run_unittests
|
||||
gcov pb_encode.gcda
|
||||
gcov pb_decode.gcda
|
||||
|
||||
run_unittests: decode_unittests encode_unittests test_encode1 test_decode1 test_encode_callbacks test_decode_callbacks
|
||||
run_unittests: decode_unittests decode_ptr_unittests encode_unittests test_encode1 test_decode1 test_encode_callbacks test_decode_callbacks
|
||||
rm -f *.gcda
|
||||
|
||||
./decode_unittests > /dev/null
|
||||
./decode_ptr_unittests > /dev/null
|
||||
./encode_unittests > /dev/null
|
||||
|
||||
[ "`./test_encode1 | ./test_decode1`" = \
|
||||
|
||||
@@ -279,6 +279,30 @@ int main()
|
||||
TEST((s = S("\x08"), !pb_decode(&s, IntegerArray_msg, &dest)))
|
||||
}
|
||||
|
||||
#ifdef MALLOC_HEADER
|
||||
{
|
||||
pb_istream_t s;
|
||||
PointerContainer dest;
|
||||
|
||||
COMMENT("Testing pb_decode with pointer fields")
|
||||
|
||||
memset(&dest, 0, sizeof(dest));
|
||||
TEST((s = S("\x0A\x01\x61\x12\x01\x62\x2A\x01\x65\x32\x01\x66\x3A\x00"
|
||||
"\x42\x01\x63\x4A\x01\x64"),
|
||||
pb_decode(&s, PointerContainer_msg, &dest)))
|
||||
TEST(0 == strcmp(dest.text, "a"))
|
||||
TEST(dest.blob.size == 1 && dest.blob.bytes[0] == 'b')
|
||||
TEST(dest.submsg == NULL)
|
||||
TEST(dest.rtext_count == 1 && (0 == strcmp(dest.rtext[0], "e")))
|
||||
TEST(dest.rblob_count == 1 && dest.rblob[0].size == 1 &&
|
||||
dest.rblob[0].bytes[0] == 'f')
|
||||
TEST(dest.rsubmsg_count == 1)
|
||||
TEST(0 == strcmp(dest.otext, "c"))
|
||||
TEST(dest.oblob.size == 1 && dest.oblob.bytes[0] == 'd')
|
||||
TEST(pb_clean(PointerContainer_msg, &dest));
|
||||
}
|
||||
#endif
|
||||
|
||||
if (status != 0)
|
||||
fprintf(stdout, "\n\nSome tests FAILED!\n");
|
||||
|
||||
|
||||
@@ -269,6 +269,47 @@ int main()
|
||||
TEST(!pb_encode(&s, CallbackContainerContainer_msg, &msg2))
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t buffer[128];
|
||||
pb_ostream_t s;
|
||||
PointerContainer msg;
|
||||
PointerContainer msg2;
|
||||
IntegerArray msg3;
|
||||
|
||||
COMMENT("Test pb_encode with pointer fields.")
|
||||
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
memset(&msg2, 0, sizeof(msg2));
|
||||
msg.text = "a";
|
||||
msg.blob.size = 1;
|
||||
msg.blob.bytes = (uint8_t*)"b";
|
||||
msg.submsg = &msg2;
|
||||
TEST(WRITES(pb_encode(&s, PointerContainer_msg, &msg),
|
||||
"\x0A\x01\x61\x12\x01\x62"))
|
||||
|
||||
memset(&msg3, 0, sizeof(msg3));
|
||||
msg.rtext_count = 1;
|
||||
msg.rtext[0] = "e";
|
||||
msg.rblob_count = 1;
|
||||
msg.rblob[0].size = 1;
|
||||
msg.rblob[0].bytes = (uint8_t*)"f";
|
||||
msg.rsubmsg_count = 1;
|
||||
msg.rsubmsg[0] = &msg3;
|
||||
TEST(WRITES(pb_encode(&s, PointerContainer_msg, &msg),
|
||||
"\x0A\x01\x61\x12\x01\x62"
|
||||
"\x2A\x01\x65\x32\x01\x66\x3A\x00"));
|
||||
|
||||
PointerContainer_set(msg, otext);
|
||||
msg.otext = "c";
|
||||
PointerContainer_set(msg, oblob);
|
||||
msg.oblob.size = 1;
|
||||
msg.oblob.bytes = (uint8_t*)"d";
|
||||
TEST(WRITES(pb_encode(&s, PointerContainer_msg, &msg),
|
||||
"\x0A\x01\x61\x12\x01\x62"
|
||||
"\x2A\x01\x65\x32\x01\x66\x3A\x00"
|
||||
"\x42\x01\x63\x4A\x01\x64"));
|
||||
}
|
||||
|
||||
if (status != 0)
|
||||
fprintf(stdout, "\n\nSome tests FAILED!\n");
|
||||
|
||||
|
||||
@@ -26,3 +26,24 @@ message CallbackContainer {
|
||||
message CallbackContainerContainer {
|
||||
required CallbackContainer submsg = 1;
|
||||
}
|
||||
|
||||
message PointerContainer {
|
||||
required string text = 1 [(nanopb).pointer = true];
|
||||
required bytes blob = 2 [(nanopb).pointer = true];
|
||||
optional PointerContainer submsg = 3 [(nanopb).pointer = true];
|
||||
// This should be rejected:
|
||||
// required int32 data = 4 [(nanopb).pointer = true];
|
||||
repeated string rtext = 5 [(nanopb).pointer = true, (nanopb).max_count = 10];
|
||||
repeated bytes rblob = 6 [(nanopb).pointer = true, (nanopb).max_count = 10];
|
||||
repeated IntegerArray rsubmsg = 7 [(nanopb).pointer = true, (nanopb).max_count = 10];
|
||||
optional string otext = 8 [(nanopb).pointer = true];
|
||||
optional bytes oblob = 9 [(nanopb).pointer = true];
|
||||
}
|
||||
|
||||
message RecursiveRef_A {
|
||||
optional RecursiveRef_B submsg = 1 [(nanopb).pointer = true];
|
||||
}
|
||||
|
||||
message RecursiveRef_B {
|
||||
required RecursiveRef_A submsg = 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user