nanopb 的安装与使用

下载与安装:
wget https://koti.kapsi.fi/~jpa/nanopb/download/nanopb-0.3.7-linux-x86.tar.gz
tar -xvf nanopb-0.3.7-linux-x86.tar.gz
sudo cp nanopb-0.3.7-linux-x86/ /opt/ -r
使用方式:
  • 定义协议Message.proto
syntax = "proto2";
message Loginrequst{ 
    required string username = 1;
    required string password = 2;  
}

message Loginrepone{
    required int32 status = 1;
    required int32 errno  = 2; 
}
  • options文件
Loginrequst.username    max_size:16
Loginrequst.password    max_size:16
  • 使用nanopb 生成c 代码
$ls
Message.options  Message.proto
#命令使用方式
$/opt/nanopb-0.3.7-linux-x86/generator-bin/protoc --nanopb_out=. Message.proto
#查看生成文件
$ls
Message.options  Message.pb.c  Message.pb.h  Message.proto
#查看工具链文件
$ls /opt/nanopb-0.3.7-linux-x86/pb* 
/opt/nanopb-0.3.7-linux-x86/pb_common.c  /opt/nanopb-0.3.7-linux-x86/pb_decode.c  /opt/nanopb-0.3.7-linux-x86/pb_encode.c  /opt/nanopb-0.3.7-linux-x86/pb.h
/opt/nanopb-0.3.7-linux-x86/pb_common.h  /opt/nanopb-0.3.7-linux-x86/pb_decode.h  /opt/nanopb-0.3.7-linux-x86/pb_encode.h
#将这些文件与Message.pb.c  Message.pb.h copy到我们的项目中即可,编译时无需链接任何库
  • 编码使用
#include <Message.pb.h>
#include <pb.h>
#include <pb_encode.h>
#include <pb_decode.h>
int test_nanopb(void)
{
    /* This is the buffer where we will store our message. */
       uint8_t buffer[128];
       size_t message_length;
       bool status;
       uint8_t i;

       /* 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.
            */
           Loginrequst  login= Loginrequst_init_zero;

           memset(buffer, 0, 128);
           /* Create a stream that will write to our buffer. */
           pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
           strcpy((char *)&login.username, "devin");
           strcpy((char *)&login.password,"123456");
           /* Now we are ready to encode the message! */
           status = pb_encode(&stream, Loginrequst_fields, &login);
           message_length = stream.bytes_written;
           /* Then just check for any errors.. */
           if (!status)
           {
               printf("Encoding failed: %s\n", 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. */
           Loginrequst login = Loginrequst_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, Loginrequst_fields, &login);

           /* Check for errors... */
           if (!status)
           {
               printf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
               return -1;
           }

           /* Print the data contained in the message. */
           printf("username was [%s].\n", login.username);
           printf("password    was [%s].\n", login.password);
       }

    return 0;
}
int main(int argc,char *argv[])
{
    test_nanopb();
    return 0;
}
  • 执行结果
username        was [devin].
password    was [123456].
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值