消息编解码Nanopb

Google Protocol Buffer 有各种版本的代码包,Python C/C++JAVACOBJ-C.NET等,嵌入式设备中使用的protobuf版本,我们选择的是nanoprobuf

nanopb

1)Google Protocol Buffer 的使用和原理

https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html

2)Nanopb - protocol buffers with small code size

http://jpa.kapsi.fi/nanopb/

To use the nanopb library, you need to do two things:

  1. Compile your .proto files for nanopb, using protoc.
  2. Include pb_encode.c, pb_decode.c and pb_common.c in your project.

The easiest way to get started is to study the project in "examples/simple". It contains a Makefile, which should work directly under most Linux systems. However, for any other kind of build system, see the manual steps in README.txt in that folder.

simple.c

message SimpleMessage {

    required int32 lucky_number = 1;

}
#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
", 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
", PB_GET_ERROR(&stream));
            return 1;
        }
        
        /* Print the data contained in the message. */
        printf("Your lucky number was %d!
", message.lucky_number);
    }
    
    return 0;
}

3) nanopb wiki

https://github.com/nanopb/nanopb

protobuf

ubuntu-16.04 install protobuf-c

https://blog.csdn.net/taosera/article/details/78946625

git clone https://github.com/protobuf-c/protobuf-c.git
./autogen.sh
./configure
make & sudo make install

git clone https://github.com/google/protobuf.git
./autogen.sh
./configure
make && sudo make install

Linux下使用protobuf-c实现自定义协议

https://blog.csdn.net/stayneckwind2/article/details/80293733

protoc-c test.proto --c_out=./


gcc main.c -o main test.pb-c.c -lprotobuf-c -L ./protobuf-c/lib/ -I ./protobuf-c/include/protobuf-c

probuf-c wiki

https://github.com/protocolbuffers/protobuf

https://github.com/protobuf-c/protobuf-c/wiki/Examples 

python protobuf

1. install python protobuf
pip install scons protobuf grpcio-tools

2. download nanopb
git clone https://github.com/nanopb/nanopb/tree/nanopb-0.4.0

3. compile message.proto
python ./nanopb-nanopb-0.4.0/generator/nanopb_generator.py message.proto

3. compile message.proto
protoc --decode=Example message.proto

原文地址:https://www.cnblogs.com/dong1/p/7224745.html