diff options
Diffstat (limited to 'examples/simple')
-rw-r--r-- | examples/simple/Makefile | 1 | ||||
-rw-r--r-- | examples/simple/rules.mk | 24 | ||||
-rw-r--r-- | examples/simple/simple.c | 11 | ||||
-rw-r--r-- | examples/simple/simple.pb.c | 19 | ||||
-rw-r--r-- | examples/simple/simple.pb.h | 51 | ||||
-rw-r--r-- | examples/simple/simple.proto | 2 |
6 files changed, 104 insertions, 4 deletions
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; } |