nanopb-0.3.6-linux-x86.tar.gz 这个文件里有很多protocol buffers的例子以及将.proto文件生成.pb.c .pb.h文件
////////////////////////////////////////////////////////////////////
//control_api.proto协议文档
//设置定时喂食任务(APP, 硬件协议)
message SetFeedTimerTaskListReq {
repeated FeedTimerTask rpt_feed_timer_task_list = 1; //定时喂食任务列表
}
message SetFeedTimerTaskListRsp {
}
//喂食任务
message FeedTimerTask {
required int32 ui_time = 1; //投放时间
required int32 ui_feed_weight = 2; //投放量,单位克
required int32 ui_feed_duration = 3; //投放时长,单位分钟
required int32 ui_feed_state = 4; //喂食任务执行状态,参考FeedState
}
//////////////////////////////////////////////////////////////////
//control_api.pb.h
typedef struct _hxy_control_SetFeedTimerTaskListReq {
pb_callback_t rpt_feed_timer_task_list;
/* @@protoc_insertion_point(struct:hxy_control_SetFeedTimerTaskListReq) */
} hxy_control_SetFeedTimerTaskListReq;
typedef struct _hxy_control_FeedTimerTask {
int32_t ui_time;
int32_t ui_feed_weight;
int32_t ui_feed_duration;
int32_t ui_feed_state;
/* @@protoc_insertion_point(struct:hxy_control_FeedTimerTask) */
} hxy_control_FeedTimerTask;
////////////////////////////////////////////////////////////////////////
//control_api.pb.c
const pb_field_t hxy_control_SetFeedTimerTaskListReq_fields[2] = {
PB_FIELD( 1, MESSAGE , REPEATED, CALLBACK, FIRST, hxy_control_SetFeedTimerTaskListReq, rpt_feed_timer_task_list, rpt_feed_timer_task_list, &hxy_control_FeedTimerTask_fields),
PB_LAST_FIELD
};
const pb_field_t hxy_control_FeedTimerTask_fields[5] = {
PB_FIELD( 1, INT32 , REQUIRED, STATIC , FIRST, hxy_control_FeedTimerTask, ui_time, ui_time, 0),
PB_FIELD( 2, INT32 , REQUIRED, STATIC , OTHER, hxy_control_FeedTimerTask, ui_feed_weight, ui_time, 0),
PB_FIELD( 3, INT32 , REQUIRED, STATIC , OTHER, hxy_control_FeedTimerTask, ui_feed_duration, ui_feed_weight, 0),
PB_FIELD( 4, INT32 , REQUIRED, STATIC , OTHER, hxy_control_FeedTimerTask, ui_feed_state, ui_feed_duration, 0),
PB_LAST_FIELD
};
///////////////////////////////////////////////////////////////////////////
解码代码如下:
pb_istream_t stream = pb_istream_from_buffer(buffer, body_length);
hxy_control_SetFeedTimerTaskListReq setfeedtimertaskListreq ={NULL};;
setfeedtimertaskListreq.rpt_feed_timer_task_list.funcs.decode = &decode_setfeedtimertaskListreq;
setfeedtimertaskListreq.rpt_feed_timer_task_list.arg= "wangwenwen\n\0";
if (!pb_decode(&stream, hxy_control_SetFeedTimerTaskListReq_fields, &setfeedtimertaskListreq)){
db("pb_decode ,hxy_control_SetFeedTimerTaskListReq ERROR\0");
return 1;
}
////////
bool decode_setfeedtimertaskListreq(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
db("decode_setfeedtimertaskListreq ::%s \n",*arg);
hxy_control_FeedTimerTask feedtimetask = {};
if (!pb_decode(stream, hxy_control_FeedTimerTask_fields, &feedtimetask))
return false;
db("ui_time == %d\n",feedtimetask.ui_time);
db("ui_feed_weight == %d\n",feedtimetask.ui_feed_weight);
db("ui_feed_duration == %d\n",feedtimetask.ui_feed_duration);
db("ui_feed_state == %d\n",feedtimetask.ui_feed_state);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
阅读(8140) | 评论(0) | 转发(0) |