aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorYu Shan <shanyu@google.com>2018-07-24 17:26:36 -0700
committerYu Shan <shanyu@google.com>2018-07-25 14:51:13 -0400
commit56ebba603b8b913261a40f3f61561bb728e3eaa5 (patch)
tree9aa05df75ba4f5da6459d0192fb5f937a34eaa90 /examples
parent3b20283285e6ae76ba17167d20eaeab0461ff0d9 (diff)
downloadnanopb-c-56ebba603b8b913261a40f3f61561bb728e3eaa5.tar.gz
Upgrade nanopb to 0.3.9.1
Downloaded from https://github.com/nanopb/nanopb/archive/0.3.9.1.tar.gz Also upload generator/google to python-protobuf 3.0.0. Copied from /usr/lib/python2.7/dist-packages/google after run apt-get install python-protobuf. Remove *.so to force it to use python implementation instead of prebuilt cpp implementation. Bug: b/111798740 Test: None Change-Id: I110dc1352b05a372811520efd34ffbda5abc2a02
Diffstat (limited to 'examples')
-rw-r--r--examples/cmake_relpath/CMakeLists.txt18
-rw-r--r--examples/cmake_relpath/README.txt18
-rw-r--r--examples/cmake_relpath/proto/simple.proto11
-rw-r--r--examples/cmake_relpath/proto/sub/unlucky.proto5
-rw-r--r--examples/cmake_relpath/simple.c73
-rw-r--r--examples/cmake_simple/CMakeLists.txt16
-rw-r--r--examples/cmake_simple/README.txt18
-rw-r--r--examples/cmake_simple/simple.c71
-rw-r--r--examples/cmake_simple/simple.proto9
-rw-r--r--examples/network_server/client.c96
-rw-r--r--examples/network_server/fileproto.proto2
-rw-r--r--examples/network_server/server.c89
-rw-r--r--examples/simple/Makefile1
-rw-r--r--examples/simple/rules.mk24
-rw-r--r--examples/simple/simple.c11
-rw-r--r--examples/simple/simple.pb.c19
-rw-r--r--examples/simple/simple.pb.h51
-rw-r--r--examples/simple/simple.proto2
-rw-r--r--examples/using_double_on_avr/doubleproto.proto2
-rw-r--r--examples/using_union_messages/README.txt3
-rw-r--r--examples/using_union_messages/unionproto.proto2
21 files changed, 471 insertions, 70 deletions
diff --git a/examples/cmake_relpath/CMakeLists.txt b/examples/cmake_relpath/CMakeLists.txt
new file mode 100644
index 0000000..e7727d8
--- /dev/null
+++ b/examples/cmake_relpath/CMakeLists.txt
@@ -0,0 +1,18 @@
+cmake_minimum_required(VERSION 2.8)
+project(NANOPB_CMAKE_SIMPLE C)
+
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../extra)
+find_package(Nanopb REQUIRED)
+include_directories(${NANOPB_INCLUDE_DIRS})
+
+nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS RELPATH proto
+ proto/simple.proto proto/sub/unlucky.proto)
+
+include_directories(${CMAKE_CURRENT_BINARY_DIR})
+#add_custom_target(generate_proto_sources DEPENDS ${PROTO_SRCS} ${PROTO_HDRS})
+set_source_files_properties(${PROTO_SRCS} ${PROTO_HDRS}
+ PROPERTIES GENERATED TRUE)
+
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -g -O0")
+
+add_executable(simple simple.c ${PROTO_SRCS} ${PROTO_HDRS})
diff --git a/examples/cmake_relpath/README.txt b/examples/cmake_relpath/README.txt
new file mode 100644
index 0000000..aa0f3f3
--- /dev/null
+++ b/examples/cmake_relpath/README.txt
@@ -0,0 +1,18 @@
+Nanopb example "simple" using CMake
+=======================
+
+This example is the same as the simple nanopb example but built using CMake.
+
+Example usage
+-------------
+
+On Linux, create a build directory and then call cmake:
+
+ nanopb/examples/cmake_simple$ mkdir build
+ nanopb/examples/cmake_simple$ cd build/
+ nanopb/examples/cmake_simple/build$ cmake ..
+ nanopb/examples/cmake_simple/build$ make
+
+After that, you can run it with the command: ./simple
+
+On other platforms supported by CMake, refer to CMake instructions.
diff --git a/examples/cmake_relpath/proto/simple.proto b/examples/cmake_relpath/proto/simple.proto
new file mode 100644
index 0000000..3bf4ad1
--- /dev/null
+++ b/examples/cmake_relpath/proto/simple.proto
@@ -0,0 +1,11 @@
+// A very simple protocol definition, consisting of only
+// one message.
+syntax = "proto2";
+
+import "sub/unlucky.proto";
+
+message SimpleMessage {
+ required int32 lucky_number = 1;
+ required UnluckyNumber unlucky = 2;
+}
+
diff --git a/examples/cmake_relpath/proto/sub/unlucky.proto b/examples/cmake_relpath/proto/sub/unlucky.proto
new file mode 100644
index 0000000..97a42c9
--- /dev/null
+++ b/examples/cmake_relpath/proto/sub/unlucky.proto
@@ -0,0 +1,5 @@
+syntax = "proto2";
+
+message UnluckyNumber {
+ required uint32 number = 1;
+}
diff --git a/examples/cmake_relpath/simple.c b/examples/cmake_relpath/simple.c
new file mode 100644
index 0000000..231886c
--- /dev/null
+++ b/examples/cmake_relpath/simple.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "simple.pb.h"
+
+int main()
+{
+ /* This is the buffer where we will store our message. */
+ uint8_t buffer[128];
+ size_t message_length;
+ bool status;
+
+ /* Encode our message */
+ {
+ /* Allocate space on the stack to store the message data.
+ *
+ * Nanopb generates simple struct definitions for all the messages.
+ * - check out the contents of simple.pb.h!
+ * It is a good idea to always initialize your structures
+ * so that you do not have garbage data from RAM in there.
+ */
+ SimpleMessage message = SimpleMessage_init_zero;
+
+ /* Create a stream that will write to our buffer. */
+ pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+
+ /* Fill in the lucky number */
+ message.lucky_number = 13;
+ message.unlucky.number = 42;
+
+ /* Now we are ready to encode the message! */
+ status = pb_encode(&stream, SimpleMessage_fields, &message);
+ message_length = stream.bytes_written;
+
+ /* Then just check for any errors.. */
+ if (!status)
+ {
+ printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ }
+ }
+
+ /* Now we could transmit the message over network, store it in a file or
+ * wrap it to a pigeon's leg.
+ */
+
+ /* But because we are lazy, we will just decode it immediately. */
+
+ {
+ /* Allocate space for the decoded message. */
+ SimpleMessage message = SimpleMessage_init_zero;
+
+ /* Create a stream that reads from the buffer. */
+ pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
+
+ /* Now we are ready to decode the message. */
+ status = pb_decode(&stream, SimpleMessage_fields, &message);
+
+ /* Check for errors... */
+ if (!status)
+ {
+ printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ }
+
+ /* Print the data contained in the message. */
+ printf("Your lucky number was %d!\n", message.lucky_number);
+ printf("Your unlucky number was %u!\n", message.unlucky.number);
+ }
+
+ return 0;
+}
+
diff --git a/examples/cmake_simple/CMakeLists.txt b/examples/cmake_simple/CMakeLists.txt
new file mode 100644
index 0000000..e5f33a0
--- /dev/null
+++ b/examples/cmake_simple/CMakeLists.txt
@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 2.8)
+project(NANOPB_CMAKE_SIMPLE C)
+
+set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../extra)
+find_package(Nanopb REQUIRED)
+include_directories(${NANOPB_INCLUDE_DIRS})
+
+nanopb_generate_cpp(PROTO_SRCS PROTO_HDRS simple.proto)
+include_directories(${CMAKE_CURRENT_BINARY_DIR})
+#add_custom_target(generate_proto_sources DEPENDS ${PROTO_SRCS} ${PROTO_HDRS})
+set_source_files_properties(${PROTO_SRCS} ${PROTO_HDRS}
+ PROPERTIES GENERATED TRUE)
+
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -g -O0")
+
+add_executable(simple simple.c ${PROTO_SRCS} ${PROTO_HDRS})
diff --git a/examples/cmake_simple/README.txt b/examples/cmake_simple/README.txt
new file mode 100644
index 0000000..aa0f3f3
--- /dev/null
+++ b/examples/cmake_simple/README.txt
@@ -0,0 +1,18 @@
+Nanopb example "simple" using CMake
+=======================
+
+This example is the same as the simple nanopb example but built using CMake.
+
+Example usage
+-------------
+
+On Linux, create a build directory and then call cmake:
+
+ nanopb/examples/cmake_simple$ mkdir build
+ nanopb/examples/cmake_simple$ cd build/
+ nanopb/examples/cmake_simple/build$ cmake ..
+ nanopb/examples/cmake_simple/build$ make
+
+After that, you can run it with the command: ./simple
+
+On other platforms supported by CMake, refer to CMake instructions.
diff --git a/examples/cmake_simple/simple.c b/examples/cmake_simple/simple.c
new file mode 100644
index 0000000..1f6b137
--- /dev/null
+++ b/examples/cmake_simple/simple.c
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "simple.pb.h"
+
+int main()
+{
+ /* This is the buffer where we will store our message. */
+ uint8_t buffer[128];
+ size_t message_length;
+ bool status;
+
+ /* Encode our message */
+ {
+ /* Allocate space on the stack to store the message data.
+ *
+ * Nanopb generates simple struct definitions for all the messages.
+ * - check out the contents of simple.pb.h!
+ * It is a good idea to always initialize your structures
+ * so that you do not have garbage data from RAM in there.
+ */
+ SimpleMessage message = SimpleMessage_init_zero;
+
+ /* Create a stream that will write to our buffer. */
+ pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+
+ /* Fill in the lucky number */
+ message.lucky_number = 13;
+
+ /* Now we are ready to encode the message! */
+ status = pb_encode(&stream, SimpleMessage_fields, &message);
+ message_length = stream.bytes_written;
+
+ /* Then just check for any errors.. */
+ if (!status)
+ {
+ printf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ }
+ }
+
+ /* Now we could transmit the message over network, store it in a file or
+ * wrap it to a pigeon's leg.
+ */
+
+ /* But because we are lazy, we will just decode it immediately. */
+
+ {
+ /* Allocate space for the decoded message. */
+ SimpleMessage message = SimpleMessage_init_zero;
+
+ /* Create a stream that reads from the buffer. */
+ pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
+
+ /* Now we are ready to decode the message. */
+ status = pb_decode(&stream, SimpleMessage_fields, &message);
+
+ /* Check for errors... */
+ if (!status)
+ {
+ printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
+ return 1;
+ }
+
+ /* Print the data contained in the message. */
+ printf("Your lucky number was %d!\n", message.lucky_number);
+ }
+
+ return 0;
+}
+
diff --git a/examples/cmake_simple/simple.proto b/examples/cmake_simple/simple.proto
new file mode 100644
index 0000000..5c73a3b
--- /dev/null
+++ b/examples/cmake_simple/simple.proto
@@ -0,0 +1,9 @@
+// A very simple protocol definition, consisting of only
+// one message.
+
+syntax = "proto2";
+
+message SimpleMessage {
+ required int32 lucky_number = 1;
+}
+
diff --git a/examples/network_server/client.c b/examples/network_server/client.c
index e6e9a2e..ca0c68e 100644
--- a/examples/network_server/client.c
+++ b/examples/network_server/client.c
@@ -23,9 +23,13 @@
#include "fileproto.pb.h"
#include "common.h"
+/* This callback function will be called once for each filename received
+ * from the server. The filenames will be printed out immediately, so that
+ * no memory has to be allocated for them.
+ */
bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
- FileInfo fileinfo;
+ FileInfo fileinfo = {};
if (!pb_decode(stream, FileInfo_fields, &fileinfo))
return false;
@@ -35,51 +39,66 @@ bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void **ar
return true;
}
+/* This function sends a request to socket 'fd' to list the files in
+ * directory given in 'path'. The results received from server will
+ * be printed to stdout.
+ */
bool listdir(int fd, char *path)
{
- ListFilesRequest request;
- ListFilesResponse response;
- pb_istream_t input = pb_istream_from_socket(fd);
- pb_ostream_t output = pb_ostream_from_socket(fd);
- uint8_t zero = 0;
-
- if (path == NULL)
- {
- request.has_path = false;
- }
- else
+ /* Construct and send the request to server */
{
- request.has_path = true;
- if (strlen(path) + 1 > sizeof(request.path))
+ ListFilesRequest request = {};
+ pb_ostream_t output = pb_ostream_from_socket(fd);
+
+ /* In our protocol, path is optional. If it is not given,
+ * the server will list the root directory. */
+ if (path == NULL)
{
- fprintf(stderr, "Too long path.\n");
- return false;
+ request.has_path = false;
+ }
+ else
+ {
+ request.has_path = true;
+ if (strlen(path) + 1 > sizeof(request.path))
+ {
+ fprintf(stderr, "Too long path.\n");
+ return false;
+ }
+
+ strcpy(request.path, path);
}
- strcpy(request.path, path);
- }
-
- if (!pb_encode(&output, ListFilesRequest_fields, &request))
- {
- fprintf(stderr, "Encoding failed.\n");
- return false;
- }
-
- /* We signal the end of request with a 0 tag. */
- pb_write(&output, &zero, 1);
-
- response.file.funcs.decode = &printfile_callback;
-
- if (!pb_decode(&input, ListFilesResponse_fields, &response))
- {
- fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&input));
- return false;
+ /* Encode the request. It is written to the socket immediately
+ * through our custom stream. */
+ if (!pb_encode_delimited(&output, ListFilesRequest_fields, &request))
+ {
+ fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&output));
+ return false;
+ }
}
- if (response.path_error)
+ /* Read back the response from server */
{
- fprintf(stderr, "Server reported error.\n");
- return false;
+ ListFilesResponse response = {};
+ pb_istream_t input = pb_istream_from_socket(fd);
+
+ /* Give a pointer to our callback function, which will handle the
+ * filenames as they arrive. */
+ response.file.funcs.decode = &printfile_callback;
+
+ if (!pb_decode_delimited(&input, ListFilesResponse_fields, &response))
+ {
+ fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&input));
+ return false;
+ }
+
+ /* If the message from server decodes properly, but directory was
+ * not found on server side, we get path_error == true. */
+ if (response.path_error)
+ {
+ fprintf(stderr, "Server reported error.\n");
+ return false;
+ }
}
return true;
@@ -96,6 +115,7 @@ int main(int argc, char **argv)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ /* Connect to server running on localhost:1234 */
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
@@ -107,9 +127,11 @@ int main(int argc, char **argv)
return 1;
}
+ /* Send the directory listing request */
if (!listdir(sockfd, path))
return 2;
+ /* Close connection */
close(sockfd);
return 0;
diff --git a/examples/network_server/fileproto.proto b/examples/network_server/fileproto.proto
index 3e70c49..5640b8d 100644
--- a/examples/network_server/fileproto.proto
+++ b/examples/network_server/fileproto.proto
@@ -2,6 +2,8 @@
//
// See also the nanopb-specific options in fileproto.options.
+syntax = "proto2";
+
message ListFilesRequest {
optional string path = 1 [default = "/"];
}
diff --git a/examples/network_server/server.c b/examples/network_server/server.c
index 9a9c264..f500dcd 100644
--- a/examples/network_server/server.c
+++ b/examples/network_server/server.c
@@ -23,11 +23,16 @@
#include "fileproto.pb.h"
#include "common.h"
+/* This callback function will be called once during the encoding.
+ * It will write out any number of FileInfo entries, without consuming unnecessary memory.
+ * This is accomplished by fetching the filenames one at a time and encoding them
+ * immediately.
+ */
bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
DIR *dir = (DIR*) *arg;
struct dirent *file;
- FileInfo fileinfo;
+ FileInfo fileinfo = {};
while ((file = readdir(dir)) != NULL)
{
@@ -35,53 +40,76 @@ bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, void * cons
strncpy(fileinfo.name, file->d_name, sizeof(fileinfo.name));
fileinfo.name[sizeof(fileinfo.name) - 1] = '\0';
+ /* This encodes the header for the field, based on the constant info
+ * from pb_field_t. */
if (!pb_encode_tag_for_field(stream, field))
return false;
+ /* This encodes the data for the field, based on our FileInfo structure. */
if (!pb_encode_submessage(stream, FileInfo_fields, &fileinfo))
return false;
}
+ /* Because the main program uses pb_encode_delimited(), this callback will be
+ * called twice. Rewind the directory for the next call. */
+ rewinddir(dir);
+
return true;
}
+/* Handle one arriving client connection.
+ * Clients are expected to send a ListFilesRequest, terminated by a '0'.
+ * Server will respond with a ListFilesResponse message.
+ */
void handle_connection(int connfd)
{
- ListFilesRequest request;
- ListFilesResponse response;
- pb_istream_t input = pb_istream_from_socket(connfd);
- pb_ostream_t output = pb_ostream_from_socket(connfd);
- DIR *directory;
+ DIR *directory = NULL;
- if (!pb_decode(&input, ListFilesRequest_fields, &request))
+ /* Decode the message from the client and open the requested directory. */
{
- printf("Decode failed: %s\n", PB_GET_ERROR(&input));
- return;
+ ListFilesRequest request = {};
+ pb_istream_t input = pb_istream_from_socket(connfd);
+
+ if (!pb_decode_delimited(&input, ListFilesRequest_fields, &request))
+ {
+ printf("Decode failed: %s\n", PB_GET_ERROR(&input));
+ return;
+ }
+
+ directory = opendir(request.path);
+ printf("Listing directory: %s\n", request.path);
}
- directory = opendir(request.path);
-
- printf("Listing directory: %s\n", request.path);
-
- if (directory == NULL)
+ /* List the files in the directory and transmit the response to client */
{
- perror("opendir");
+ ListFilesResponse response = {};
+ pb_ostream_t output = pb_ostream_from_socket(connfd);
- response.has_path_error = true;
- response.path_error = true;
- response.file.funcs.encode = NULL;
- }
- else
- {
- response.has_path_error = false;
- response.file.funcs.encode = &listdir_callback;
- response.file.arg = directory;
+ if (directory == NULL)
+ {
+ perror("opendir");
+
+ /* Directory was not found, transmit error status */
+ response.has_path_error = true;
+ response.path_error = true;
+ response.file.funcs.encode = NULL;
+ }
+ else
+ {
+ /* Directory was found, transmit filenames */
+ response.has_path_error = false;
+ response.file.funcs.encode = &listdir_callback;
+ response.file.arg = directory;
+ }
+
+ if (!pb_encode_delimited(&output, ListFilesResponse_fields, &response))
+ {
+ printf("Encoding failed: %s\n", PB_GET_ERROR(&output));
+ }
}
- if (!pb_encode(&output, ListFilesResponse_fields, &response))
- {
- printf("Encoding failed.\n");
- }
+ if (directory != NULL)
+ closedir(directory);
}
int main(int argc, char **argv)
@@ -90,8 +118,8 @@ int main(int argc, char **argv)
struct sockaddr_in servaddr;
int reuse = 1;
+ /* Listen on localhost:1234 for TCP connections */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
-
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
memset(&servaddr, 0, sizeof(servaddr));
@@ -112,6 +140,7 @@ int main(int argc, char **argv)
for(;;)
{
+ /* Wait for a client */
connfd = accept(listenfd, NULL, NULL);
if (connfd < 0)
@@ -128,4 +157,6 @@ int main(int argc, char **argv)
close(connfd);
}
+
+ return 0;
}
diff --git a/examples/simple/Makefile b/examples/simple/Makefile
index 02a4c3f..970a865 100644
--- a/examples/simple/Makefile
+++ b/examples/simple/Makefile
@@ -10,6 +10,7 @@ CSRC = simple.c # The main program
CSRC += simple.pb.c # The compiled protocol definition
CSRC += $(NANOPB_DIR)/pb_encode.c # The nanopb encoder
CSRC += $(NANOPB_DIR)/pb_decode.c # The nanopb decoder
+CSRC += $(NANOPB_DIR)/pb_common.c # The nanopb common parts
# Build rule for the main program
simple: $(CSRC)
diff --git a/examples/simple/rules.mk b/examples/simple/rules.mk
new file mode 100644
index 0000000..1e4221d
--- /dev/null
+++ b/examples/simple/rules.mk
@@ -0,0 +1,24 @@
+NANOPB_DIR := external/nanopb-0.3.9.1-linux-x86
+
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_SRCS += \
+ $(LOCAL_DIR)/simple.c \
+ $(LOCAL_DIR)/simple.pb.c \
+ $(NANOPB_DIR)/pb_encode.c \
+ $(NANOPB_DIR)/pb_decode.c \
+ $(NANOPB_DIR)/pb_common.c
+
+MODULE_CPPFLAGS += -std=c++11
+
+MODULE_INCLUDES += \
+ $(NANOPB_DIR)
+
+$(LOCAL_DIR)/simple.pb.c: $(LOCAL_DIR)/simple.proto
+ $(PROTOC) $(PROTOC_OPTS) --nanopb_out=$(LOCAL_DIR) $(LOCAL_DIR)/simple.proto
+
+include $(NANOPB_DIR)/extra/nanopb.mk
+include make/module.mk
+
diff --git a/examples/simple/simple.c b/examples/simple/simple.c
index 3127230..c16ec52 100644
--- a/examples/simple/simple.c
+++ b/examples/simple/simple.c
@@ -15,8 +15,11 @@ int main()
/* Allocate space on the stack to store the message data.
*
* Nanopb generates simple struct definitions for all the messages.
- * - check out the contents of simple.pb.h! */
- SimpleMessage message;
+ * - check out the contents of simple.pb.h!
+ * It is a good idea to always initialize your structures
+ * so that you do not have garbage data from RAM in there.
+ */
+ SimpleMessage message = SimpleMessage_init_zero;
/* Create a stream that will write to our buffer. */
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
@@ -44,7 +47,7 @@ int main()
{
/* Allocate space for the decoded message. */
- SimpleMessage message;
+ SimpleMessage message = SimpleMessage_init_zero;
/* Create a stream that reads from the buffer. */
pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
@@ -60,7 +63,7 @@ int main()
}
/* Print the data contained in the message. */
- printf("Your lucky number was %d!\n", message.lucky_number);
+ printf("Your lucky number was %d!\n", (int)message.lucky_number);
}
return 0;
diff --git a/examples/simple/simple.pb.c b/examples/simple/simple.pb.c
new file mode 100644
index 0000000..481aa7d
--- /dev/null
+++ b/examples/simple/simple.pb.c
@@ -0,0 +1,19 @@
+/* Automatically generated nanopb constant definitions */
+/* Generated by nanopb-0.3.9.1 at Fri May 25 16:28:54 2018. */
+
+#include "simple.pb.h"
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+
+
+const pb_field_t SimpleMessage_fields[2] = {
+ PB_FIELD( 1, INT32 , REQUIRED, STATIC , FIRST, SimpleMessage, lucky_number, lucky_number, 0),
+ PB_LAST_FIELD
+};
+
+
+/* @@protoc_insertion_point(eof) */
diff --git a/examples/simple/simple.pb.h b/examples/simple/simple.pb.h
new file mode 100644
index 0000000..f4614db
--- /dev/null
+++ b/examples/simple/simple.pb.h
@@ -0,0 +1,51 @@
+/* Automatically generated nanopb header */
+/* Generated by nanopb-0.3.9.1 at Fri May 25 16:28:54 2018. */
+
+#ifndef PB_SIMPLE_PB_H_INCLUDED
+#define PB_SIMPLE_PB_H_INCLUDED
+#include <pb.h>
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 30
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Struct definitions */
+typedef struct _SimpleMessage {
+ int32_t lucky_number;
+/* @@protoc_insertion_point(struct:SimpleMessage) */
+} SimpleMessage;
+
+/* Default values for struct fields */
+
+/* Initializer values for message structs */
+#define SimpleMessage_init_default {0}
+#define SimpleMessage_init_zero {0}
+
+/* Field tags (for use in manual encoding/decoding) */
+#define SimpleMessage_lucky_number_tag 1
+
+/* Struct field encoding specification for nanopb */
+extern const pb_field_t SimpleMessage_fields[2];
+
+/* Maximum encoded size of messages (where known) */
+#define SimpleMessage_size 11
+
+/* Message IDs (where set with "msgid" option) */
+#ifdef PB_MSGID
+
+#define SIMPLE_MESSAGES \
+
+
+#endif
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+/* @@protoc_insertion_point(eof) */
+
+#endif
diff --git a/examples/simple/simple.proto b/examples/simple/simple.proto
index 26e72f4..5c73a3b 100644
--- a/examples/simple/simple.proto
+++ b/examples/simple/simple.proto
@@ -1,6 +1,8 @@
// A very simple protocol definition, consisting of only
// one message.
+syntax = "proto2";
+
message SimpleMessage {
required int32 lucky_number = 1;
}
diff --git a/examples/using_double_on_avr/doubleproto.proto b/examples/using_double_on_avr/doubleproto.proto
index d8b7f2d..72d3f9c 100644
--- a/examples/using_double_on_avr/doubleproto.proto
+++ b/examples/using_double_on_avr/doubleproto.proto
@@ -1,4 +1,6 @@
// A message containing doubles, as used by other applications.
+syntax = "proto2";
+
message DoubleMessage {
required double field1 = 1;
required double field2 = 2;
diff --git a/examples/using_union_messages/README.txt b/examples/using_union_messages/README.txt
index 7a1e75d..d4b7fd2 100644
--- a/examples/using_union_messages/README.txt
+++ b/examples/using_union_messages/README.txt
@@ -17,6 +17,9 @@ we actually want. Similarly when decoding, we can manually read the tag of
the top level message, and only then allocate the memory for the submessage
after we already know its type.
+NOTE: There is a newer protobuf feature called `oneof` that is also supported
+by nanopb. It might be a better option for new code.
+
Example usage
-------------
diff --git a/examples/using_union_messages/unionproto.proto b/examples/using_union_messages/unionproto.proto
index d7c9de2..209df0d 100644
--- a/examples/using_union_messages/unionproto.proto
+++ b/examples/using_union_messages/unionproto.proto
@@ -5,6 +5,8 @@
// but they are commonly implemented by filling out exactly one of
// several optional fields.
+syntax = "proto2";
+
message MsgType1
{
required int32 value = 1;