diff options
Diffstat (limited to 'example')
-rw-r--r-- | example/Makefile | 14 | ||||
-rw-r--r-- | example/client.c | 116 | ||||
-rw-r--r-- | example/common.c | 40 | ||||
-rw-r--r-- | example/common.h | 9 | ||||
-rw-r--r-- | example/fileproto.options | 13 | ||||
-rw-r--r-- | example/fileproto.proto | 18 | ||||
-rw-r--r-- | example/server.c | 131 |
7 files changed, 0 insertions, 341 deletions
diff --git a/example/Makefile b/example/Makefile deleted file mode 100644 index 14dd9fa4..00000000 --- a/example/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -CFLAGS=-ansi -Wall -Werror -I .. -g -O0 -DEPS=../pb_decode.c ../pb_decode.h ../pb_encode.c ../pb_encode.h ../pb.h - -all: server client - -clean: - rm -f server client fileproto.pb.c fileproto.pb.h - -%: %.c $(DEPS) fileproto.pb.h fileproto.pb.c - $(CC) $(CFLAGS) -o $@ $< ../pb_decode.c ../pb_encode.c fileproto.pb.c common.c - -fileproto.pb.c fileproto.pb.h: fileproto.proto ../generator/nanopb_generator.py - protoc -I. -I../generator -I/usr/include -ofileproto.pb $< - python ../generator/nanopb_generator.py fileproto.pb diff --git a/example/client.c b/example/client.c deleted file mode 100644 index e6e9a2e0..00000000 --- a/example/client.c +++ /dev/null @@ -1,116 +0,0 @@ -/* 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/example/common.c b/example/common.c deleted file mode 100644 index 04a5aa85..00000000 --- a/example/common.c +++ /dev/null @@ -1,40 +0,0 @@ -/* 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/example/common.h b/example/common.h deleted file mode 100644 index 8dab3b7c..00000000 --- a/example/common.h +++ /dev/null @@ -1,9 +0,0 @@ -#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/example/fileproto.options b/example/fileproto.options deleted file mode 100644 index 29a2ab0e..00000000 --- a/example/fileproto.options +++ /dev/null @@ -1,13 +0,0 @@ -# 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/example/fileproto.proto b/example/fileproto.proto deleted file mode 100644 index 3e70c492..00000000 --- a/example/fileproto.proto +++ /dev/null @@ -1,18 +0,0 @@ -// 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/example/server.c b/example/server.c deleted file mode 100644 index 9a9c2644..00000000 --- a/example/server.c +++ /dev/null @@ -1,131 +0,0 @@ -/* 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); - } -} |