aboutsummaryrefslogtreecommitdiff
path: root/examples/network_server
diff options
context:
space:
mode:
authorEtan Cohen <etancohen@google.com>2014-08-20 10:11:11 -0700
committerEtan Cohen <etancohen@google.com>2014-08-20 10:11:11 -0700
commit7ef855e462b9a18b7d330e4b40f350164a6ad9da (patch)
treea43d91fcc4567b75966b901f2c1dbe7581a930e0 /examples/network_server
parent430f996db620d94da8042d70c61e137175687693 (diff)
downloadnanopb-c-7ef855e462b9a18b7d330e4b40f350164a6ad9da.tar.gz
Initial check-in of nanopb-c.android-wear-5.1.1_r1android-wear-5.1.0_r1android-wear-5.0.0_r1android-cts-5.1_r9android-cts-5.1_r8android-cts-5.1_r7android-cts-5.1_r6android-cts-5.1_r5android-cts-5.1_r4android-cts-5.1_r3android-cts-5.1_r28android-cts-5.1_r27android-cts-5.1_r26android-cts-5.1_r25android-cts-5.1_r24android-cts-5.1_r23android-cts-5.1_r22android-cts-5.1_r21android-cts-5.1_r20android-cts-5.1_r2android-cts-5.1_r19android-cts-5.1_r18android-cts-5.1_r17android-cts-5.1_r16android-cts-5.1_r15android-cts-5.1_r14android-cts-5.1_r13android-cts-5.1_r10android-cts-5.1_r1android-cts-5.0_r9android-cts-5.0_r8android-cts-5.0_r7android-cts-5.0_r6android-cts-5.0_r5android-cts-5.0_r4android-cts-5.0_r3android-5.1.1_r9android-5.1.1_r8android-5.1.1_r7android-5.1.1_r6android-5.1.1_r5android-5.1.1_r4android-5.1.1_r38android-5.1.1_r37android-5.1.1_r36android-5.1.1_r35android-5.1.1_r34android-5.1.1_r33android-5.1.1_r30android-5.1.1_r3android-5.1.1_r29android-5.1.1_r28android-5.1.1_r26android-5.1.1_r25android-5.1.1_r24android-5.1.1_r23android-5.1.1_r22android-5.1.1_r20android-5.1.1_r2android-5.1.1_r19android-5.1.1_r18android-5.1.1_r17android-5.1.1_r16android-5.1.1_r15android-5.1.1_r14android-5.1.1_r13android-5.1.1_r12android-5.1.1_r10android-5.1.1_r1android-5.1.0_r5android-5.1.0_r4android-5.1.0_r3android-5.1.0_r1android-5.0.2_r3android-5.0.2_r1android-5.0.1_r1android-5.0.0_r7android-5.0.0_r6android-5.0.0_r5.1android-5.0.0_r5android-5.0.0_r4android-5.0.0_r3android-5.0.0_r2android-5.0.0_r1master-soonglollipop-wear-releaselollipop-releaselollipop-mr1-wfc-releaselollipop-mr1-releaselollipop-mr1-fi-releaselollipop-mr1-devlollipop-mr1-cts-releaselollipop-devlollipop-cts-release
Copied from klp-wireless-dev. Change-Id: I7055708554a8a818edba8ce1da35d589b76c1621
Diffstat (limited to 'examples/network_server')
-rw-r--r--examples/network_server/Makefile17
-rw-r--r--examples/network_server/README.txt60
-rw-r--r--examples/network_server/client.c116
-rw-r--r--examples/network_server/common.c40
-rw-r--r--examples/network_server/common.h9
-rw-r--r--examples/network_server/fileproto.options13
-rw-r--r--examples/network_server/fileproto.proto18
-rw-r--r--examples/network_server/server.c131
8 files changed, 404 insertions, 0 deletions
diff --git a/examples/network_server/Makefile b/examples/network_server/Makefile
new file mode 100644
index 0000000..2c7639a
--- /dev/null
+++ b/examples/network_server/Makefile
@@ -0,0 +1,17 @@
+# Include the nanopb provided Makefile rules
+include ../../extra/nanopb.mk
+
+# Compiler flags to enable all warnings & debug info
+CFLAGS = -ansi -Wall -Werror -g -O0
+CFLAGS += -I$(NANOPB_DIR)
+
+all: server client
+
+.SUFFIXES:
+
+clean:
+ rm -f server client fileproto.pb.c fileproto.pb.h
+
+%: %.c common.c fileproto.pb.c
+ $(CC) $(CFLAGS) -o $@ $^ $(NANOPB_CORE)
+
diff --git a/examples/network_server/README.txt b/examples/network_server/README.txt
new file mode 100644
index 0000000..7bdcbed
--- /dev/null
+++ b/examples/network_server/README.txt
@@ -0,0 +1,60 @@
+Nanopb example "network_server"
+===============================
+
+This example demonstrates the use of nanopb to communicate over network
+connections. It consists of a server that sends file listings, and of
+a client that requests the file list from the server.
+
+Example usage
+-------------
+
+user@host:~/nanopb/examples/network_server$ make # Build the example
+protoc -ofileproto.pb fileproto.proto
+python ../../generator/nanopb_generator.py fileproto.pb
+Writing to fileproto.pb.h and fileproto.pb.c
+cc -ansi -Wall -Werror -I .. -g -O0 -I../.. -o server server.c
+ ../../pb_decode.c ../../pb_encode.c fileproto.pb.c common.c
+cc -ansi -Wall -Werror -I .. -g -O0 -I../.. -o client client.c
+ ../../pb_decode.c ../../pb_encode.c fileproto.pb.c common.c
+
+user@host:~/nanopb/examples/network_server$ ./server & # Start the server on background
+[1] 24462
+
+petteri@oddish:~/nanopb/examples/network_server$ ./client /bin # Request the server to list /bin
+Got connection.
+Listing directory: /bin
+1327119 bzdiff
+1327126 bzless
+1327147 ps
+1327178 ntfsmove
+1327271 mv
+1327187 mount
+1327259 false
+1327266 tempfile
+1327285 zfgrep
+1327165 gzexe
+1327204 nc.openbsd
+1327260 uname
+
+
+Details of implementation
+-------------------------
+fileproto.proto contains the portable Google Protocol Buffers protocol definition.
+It could be used as-is to implement a server or a client in any other language, for
+example Python or Java.
+
+fileproto.options contains the nanopb-specific options for the protocol file. This
+sets the amount of space allocated for file names when decoding messages.
+
+common.c/h contains functions that allow nanopb to read and write directly from
+network socket. This way there is no need to allocate a separate buffer to store
+the message.
+
+server.c contains the code to open a listening socket, to respond to clients and
+to list directory contents.
+
+client.c contains the code to connect to a server, to send a request and to print
+the response message.
+
+The code is implemented using the POSIX socket api, but it should be easy enough
+to port into any other socket api, such as lwip.
diff --git a/examples/network_server/client.c b/examples/network_server/client.c
new file mode 100644
index 0000000..e6e9a2e
--- /dev/null
+++ b/examples/network_server/client.c
@@ -0,0 +1,116 @@
+/* This is a simple TCP client that connects to port 1234 and prints a list
+ * of files in a given directory.
+ *
+ * It directly deserializes and serializes messages from network, minimizing
+ * memory use.
+ *
+ * For flexibility, this example is implemented using posix api.
+ * In a real embedded system you would typically use some other kind of
+ * a communication and filesystem layer.
+ */
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <pb_encode.h>
+#include <pb_decode.h>
+
+#include "fileproto.pb.h"
+#include "common.h"
+
+bool printfile_callback(pb_istream_t *stream, const pb_field_t *field, void **arg)
+{
+ FileInfo fileinfo;
+
+ if (!pb_decode(stream, FileInfo_fields, &fileinfo))
+ return false;
+
+ printf("%-10lld %s\n", (long long)fileinfo.inode, fileinfo.name);
+
+ return true;
+}
+
+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
+ {
+ request.has_path = true;
+ if (strlen(path) + 1 > sizeof(request.path))
+ {
+ fprintf(stderr, "Too long path.\n");
+ return false;
+ }
+
+ 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;
+ }
+
+ if (response.path_error)
+ {
+ fprintf(stderr, "Server reported error.\n");
+ return false;
+ }
+
+ return true;
+}
+
+int main(int argc, char **argv)
+{
+ int sockfd;
+ struct sockaddr_in servaddr;
+ char *path = NULL;
+
+ if (argc > 1)
+ path = argv[1];
+
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+
+ memset(&servaddr, 0, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ servaddr.sin_port = htons(1234);
+
+ if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
+ {
+ perror("connect");
+ return 1;
+ }
+
+ if (!listdir(sockfd, path))
+ return 2;
+
+ close(sockfd);
+
+ return 0;
+}
diff --git a/examples/network_server/common.c b/examples/network_server/common.c
new file mode 100644
index 0000000..04a5aa8
--- /dev/null
+++ b/examples/network_server/common.c
@@ -0,0 +1,40 @@
+/* Simple binding of nanopb streams to TCP sockets.
+ */
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+
+#include "common.h"
+
+static bool write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
+{
+ int fd = (intptr_t)stream->state;
+ return send(fd, buf, count, 0) == count;
+}
+
+static bool read_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
+{
+ int fd = (intptr_t)stream->state;
+ int result;
+
+ result = recv(fd, buf, count, MSG_WAITALL);
+
+ if (result == 0)
+ stream->bytes_left = 0; /* EOF */
+
+ return result == count;
+}
+
+pb_ostream_t pb_ostream_from_socket(int fd)
+{
+ pb_ostream_t stream = {&write_callback, (void*)(intptr_t)fd, SIZE_MAX, 0};
+ return stream;
+}
+
+pb_istream_t pb_istream_from_socket(int fd)
+{
+ pb_istream_t stream = {&read_callback, (void*)(intptr_t)fd, SIZE_MAX};
+ return stream;
+}
diff --git a/examples/network_server/common.h b/examples/network_server/common.h
new file mode 100644
index 0000000..8dab3b7
--- /dev/null
+++ b/examples/network_server/common.h
@@ -0,0 +1,9 @@
+#ifndef _PB_EXAMPLE_COMMON_H_
+#define _PB_EXAMPLE_COMMON_H_
+
+#include <pb.h>
+
+pb_ostream_t pb_ostream_from_socket(int fd);
+pb_istream_t pb_istream_from_socket(int fd);
+
+#endif \ No newline at end of file
diff --git a/examples/network_server/fileproto.options b/examples/network_server/fileproto.options
new file mode 100644
index 0000000..29a2ab0
--- /dev/null
+++ b/examples/network_server/fileproto.options
@@ -0,0 +1,13 @@
+# This file defines the nanopb-specific options for the messages defined
+# in fileproto.proto.
+#
+# If you come from high-level programming background, the hardcoded
+# maximum lengths may disgust you. However, if your microcontroller only
+# has a few kB of ram to begin with, setting reasonable limits for
+# filenames is ok.
+#
+# On the other hand, using the callback interface, it is not necessary
+# to set a limit on the number of files in the response.
+
+ListFilesRequest.path max_size:128
+FileInfo.name max_size:128
diff --git a/examples/network_server/fileproto.proto b/examples/network_server/fileproto.proto
new file mode 100644
index 0000000..3e70c49
--- /dev/null
+++ b/examples/network_server/fileproto.proto
@@ -0,0 +1,18 @@
+// This defines protocol for a simple server that lists files.
+//
+// See also the nanopb-specific options in fileproto.options.
+
+message ListFilesRequest {
+ optional string path = 1 [default = "/"];
+}
+
+message FileInfo {
+ required uint64 inode = 1;
+ required string name = 2;
+}
+
+message ListFilesResponse {
+ optional bool path_error = 1 [default = false];
+ repeated FileInfo file = 2;
+}
+
diff --git a/examples/network_server/server.c b/examples/network_server/server.c
new file mode 100644
index 0000000..9a9c264
--- /dev/null
+++ b/examples/network_server/server.c
@@ -0,0 +1,131 @@
+/* This is a simple TCP server that listens on port 1234 and provides lists
+ * of files to clients, using a protocol defined in file_server.proto.
+ *
+ * It directly deserializes and serializes messages from network, minimizing
+ * memory use.
+ *
+ * For flexibility, this example is implemented using posix api.
+ * In a real embedded system you would typically use some other kind of
+ * a communication and filesystem layer.
+ */
+
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <pb_encode.h>
+#include <pb_decode.h>
+
+#include "fileproto.pb.h"
+#include "common.h"
+
+bool listdir_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
+{
+ DIR *dir = (DIR*) *arg;
+ struct dirent *file;
+ FileInfo fileinfo;
+
+ while ((file = readdir(dir)) != NULL)
+ {
+ fileinfo.inode = file->d_ino;
+ strncpy(fileinfo.name, file->d_name, sizeof(fileinfo.name));
+ fileinfo.name[sizeof(fileinfo.name) - 1] = '\0';
+
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+
+ if (!pb_encode_submessage(stream, FileInfo_fields, &fileinfo))
+ return false;
+ }
+
+ return true;
+}
+
+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;
+
+ if (!pb_decode(&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);
+
+ if (directory == NULL)
+ {
+ perror("opendir");
+
+ 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 (!pb_encode(&output, ListFilesResponse_fields, &response))
+ {
+ printf("Encoding failed.\n");
+ }
+}
+
+int main(int argc, char **argv)
+{
+ int listenfd, connfd;
+ struct sockaddr_in servaddr;
+ int reuse = 1;
+
+ listenfd = socket(AF_INET, SOCK_STREAM, 0);
+
+ setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
+
+ memset(&servaddr, 0, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ servaddr.sin_port = htons(1234);
+ if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0)
+ {
+ perror("bind");
+ return 1;
+ }
+
+ if (listen(listenfd, 5) != 0)
+ {
+ perror("listen");
+ return 1;
+ }
+
+ for(;;)
+ {
+ connfd = accept(listenfd, NULL, NULL);
+
+ if (connfd < 0)
+ {
+ perror("accept");
+ return 1;
+ }
+
+ printf("Got connection.\n");
+
+ handle_connection(connfd);
+
+ printf("Closing connection.\n");
+
+ close(connfd);
+ }
+}