Quellcode durchsuchen

Add several examples

Wu Han vor 6 Jahren
Ursprung
Commit
0aae6cdb97

+ 15 - 0
examples/encode_decode_file/amessage.pb.c

@@ -0,0 +1,15 @@
+/* Automatically generated nanopb constant definitions */
+/* Generated by nanopb-0.4.0-dev */
+
+#include "amessage.pb.h"
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 40
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+PB_BIND(AMessage, AMessage, AUTO)
+
+
+
+/* @@protoc_insertion_point(eof) */

+ 53 - 0
examples/encode_decode_file/amessage.pb.h

@@ -0,0 +1,53 @@
+/* Automatically generated nanopb header */
+/* Generated by nanopb-0.4.0-dev */
+
+#ifndef PB_AMESSAGE_PB_H_INCLUDED
+#define PB_AMESSAGE_PB_H_INCLUDED
+#include <pb.h>
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 40
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Struct definitions */
+typedef struct _AMessage {
+    int32_t aa;
+    int32_t bb;
+/* @@protoc_insertion_point(struct:AMessage) */
+} AMessage;
+
+
+/* Initializer values for message structs */
+#define AMessage_init_default                    {0, 0}
+#define AMessage_init_zero                       {0, 0}
+
+/* Field tags (for use in manual encoding/decoding) */
+#define AMessage_aa_tag                          1
+#define AMessage_bb_tag                          2
+
+/* Struct field encoding specification for nanopb */
+#define AMessage_FIELDLIST(X, a) \
+X(a, STATIC, SINGULAR, INT32, aa, 1) \
+X(a, STATIC, SINGULAR, INT32, bb, 2)
+#define AMessage_CALLBACK NULL
+#define AMessage_DEFAULT NULL
+
+extern const pb_msgdesc_t AMessage_msg;
+
+/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
+#define AMessage_fields &AMessage_msg
+
+/* Maximum encoded size of messages (where known) */
+#define AMessage_size                            22
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+/* @@protoc_insertion_point(eof) */
+
+#endif

+ 6 - 0
examples/encode_decode_file/amessage.proto

@@ -0,0 +1,6 @@
+syntax = "proto3";
+
+message AMessage {
+  int32 a=1; 
+  int32 b=2;
+}

+ 63 - 0
examples/encode_decode_file/amessage_decode_from_file.c

@@ -0,0 +1,63 @@
+#include <rtthread.h>
+#include <rtdevice.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "amessage.pb.h"
+#include <dfs_posix.h>
+
+static void nanopb_decode_from_file(int argc,char *argv[])
+{
+    /* This is the buffer where we will store our message. */
+    int ret;
+    struct stat buf;
+    ret = stat("/amessage.onnx", &buf);
+    if(ret == 0)
+    {
+        rt_kprintf("amessage.onnx file size = %d\n", buf.st_size);
+    }
+    else
+    {
+        rt_kprintf("amessage.onnx file not fonud\n");
+    }
+
+    size_t message_length = buf.st_size;
+    uint8_t buffer[128];
+    bool status;
+
+    int fd = open("/amessage.onnx", O_RDONLY);
+    if (fd>= 0)
+    {
+        int size = read(fd, buffer, message_length);
+        close(fd);
+        rt_kprintf("Read from file amessage.onnx \n");
+        if (size < 0)
+            return ;
+    }
+
+    {
+        /* Allocate space for the decoded message. */
+        AMessage amessage = AMessage_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, AMessage_fields, &amessage);
+        
+        /* Check for errors... */
+        if (!status)
+        {
+            rt_kprintf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
+            return ;
+        }
+        
+        /* Print the data contained in the message. */
+        rt_kprintf("amessage.aa = %d\n", (int)amessage.aa);
+        rt_kprintf("amessage.bb = %d\n", (int)amessage.bb);
+    }
+
+    return;
+}
+MSH_CMD_EXPORT(nanopb_decode_from_file, nanopb decode from file);

+ 80 - 0
examples/encode_decode_file/amessage_encode_decode.c

@@ -0,0 +1,80 @@
+#include <rtthread.h>
+#include <rtdevice.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "amessage.pb.h"
+
+static void nanopb_encode_decode(int argc,char *argv[])
+{
+    /* 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.
+         */
+        AMessage amessage = AMessage_init_zero;
+        
+        /* Create a stream that will write to our buffer. */
+        pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+        
+        if (argc != 3)
+	    {
+		    rt_kprintf("usage: amessage aa bb\n");
+		    return;
+	    }
+        amessage.aa = atoi(argv[1]);
+		amessage.bb = atoi(argv[2]);
+        
+        /* Now we are ready to encode the message! */
+        status = pb_encode(&stream, AMessage_fields, &amessage);
+        message_length = stream.bytes_written;
+        
+        /* Then just check for any errors.. */
+        if (!status)
+        {
+            rt_kprintf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
+            return;
+        }
+    }
+    
+    /* 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. */
+        AMessage amessage = AMessage_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, AMessage_fields, &amessage);
+        
+        /* Check for errors... */
+        if (!status)
+        {
+            rt_kprintf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
+            return ;
+        }
+        
+        /* Print the data contained in the message. */
+        rt_kprintf("amessage.aa = %d\n", (int)amessage.aa);
+        rt_kprintf("amessage.bb = %d\n", (int)amessage.bb);
+    }
+    
+    return;
+}
+MSH_CMD_EXPORT(nanopb_encode_decode, nanopb encode test);

+ 62 - 0
examples/encode_decode_file/amessage_encode_to_file.c

@@ -0,0 +1,62 @@
+#include <rtthread.h>
+#include <rtdevice.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "amessage.pb.h"
+#include <dfs_posix.h>
+
+static void nanopb_encode_to_file(int argc,char *argv[])
+{
+    /* 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.
+         */
+        AMessage amessage = AMessage_init_zero;
+        
+        /* Create a stream that will write to our buffer. */
+        pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+        
+        if (argc != 3)
+        {
+            rt_kprintf("usage: amessage aa bb\n");
+            return;
+        }
+        amessage.aa = atoi(argv[1]);
+        amessage.bb = atoi(argv[2]);
+        
+        /* Now we are ready to encode the message! */
+        status = pb_encode(&stream, AMessage_fields, &amessage);
+        message_length = stream.bytes_written;
+        
+        /* Then just check for any errors.. */
+        if (!status)
+        {
+            rt_kprintf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
+            return;
+        }
+
+        rt_kprintf("Writing %d serialized bytes\n", message_length); // See the length of message  
+        int fd = open("/amessage.onnx", O_WRONLY | O_CREAT | O_TRUNC);
+        if (fd>= 0)
+        {
+            write(fd, buffer, message_length);
+            close(fd);
+            rt_kprintf("Write done.\n");
+        }
+    }
+
+    return;
+}
+MSH_CMD_EXPORT(nanopb_encode_to_file, nanopb encode to file);

+ 74 - 0
examples/simple/simple.c

@@ -0,0 +1,74 @@
+#include <rtthread.h>
+#include <rtdevice.h>
+#include <stdio.h>
+#include <pb_encode.h>
+#include <pb_decode.h>
+#include "simple.pb.h"
+
+static void nanopb_simple(int argc,char *argv[])
+{
+    /* 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)
+        {
+            rt_kprintf("Encoding failed: %s\n", PB_GET_ERROR(&stream));
+            return ;
+        }
+		rt_kprintf("Successfully encoded\n");
+    }
+    
+    /* 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)
+        {
+            rt_kprintf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
+            return;
+        }
+        
+        /* Print the data contained in the message. */
+        rt_kprintf("Your lucky number was %d!\n", (int)message.lucky_number);
+    }
+    
+    return;
+}
+MSH_CMD_EXPORT(nanopb_simple, nanopb test);

+ 15 - 0
examples/simple/simple.pb.c

@@ -0,0 +1,15 @@
+/* Automatically generated nanopb constant definitions */
+/* Generated by nanopb-0.4.0-dev */
+
+#include "simple.pb.h"
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 40
+#error Regenerate this file with the current version of nanopb generator.
+#endif
+
+PB_BIND(SimpleMessage, SimpleMessage, AUTO)
+
+
+
+/* @@protoc_insertion_point(eof) */

+ 50 - 0
examples/simple/simple.pb.h

@@ -0,0 +1,50 @@
+/* Automatically generated nanopb header */
+/* Generated by nanopb-0.4.0-dev */
+
+#ifndef PB_SIMPLE_PB_H_INCLUDED
+#define PB_SIMPLE_PB_H_INCLUDED
+#include <pb.h>
+
+/* @@protoc_insertion_point(includes) */
+#if PB_PROTO_HEADER_VERSION != 40
+#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;
+
+
+/* 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 */
+#define SimpleMessage_FIELDLIST(X, a) \
+X(a, STATIC, SINGULAR, INT32, lucky_number, 1)
+#define SimpleMessage_CALLBACK NULL
+#define SimpleMessage_DEFAULT NULL
+
+extern const pb_msgdesc_t SimpleMessage_msg;
+
+/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
+#define SimpleMessage_fields &SimpleMessage_msg
+
+/* Maximum encoded size of messages (where known) */
+#define SimpleMessage_size                       11
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+/* @@protoc_insertion_point(eof) */
+
+#endif

+ 8 - 0
examples/simple/simple.proto

@@ -0,0 +1,8 @@
+// A very simple protocol definition, consisting of only
+// one message.
+
+syntax = "proto3";
+
+message SimpleMessage {
+    int32 lucky_number = 1;
+}