blob blobmsg blobmsg_json使用

Blob :二進制大對象

Blobmsg :二進制對象網絡序列化。

blobmsg_json   用於json對象的序列化

參考鏈接:https://www.cnblogs.com/embedded-linux/p/6792359.html

一 : blob 的使用  (熟悉使用blob的各種api)

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <sys/signal.h>
#include "libubox/uloop.h"
#include "libubox/ustream.h"
#include "libubox/utils.h"
#include "libubus.h"
#include "json-c/json.h"
#include "libubox/blob.h"
#include "libubox/blobmsg.h"
#include "libubox/blobmsg_json.h"
#include "libubox/ulog.h"
#include "libubox/runqueue.h"
#include <libubox/list.h>
#include <libubox/uloop.h>
#include <libubox/ustream.h>
#include "termios.h"
  int main()
{

static struct blob_buf b;
struct blob_attr *attr1,*attr2;
char * str,*str1;
void *ptr;
struct blob_attr * pos;
int rem=0;
blob_buf_init(&b, 0);

//設置blob數據
attr1=blob_put_string(&b, 1, "hello");

attr2=blob_put_u8(&b, 2, 100);

//獲取blob數據
str=blob_get_string(attr1);
int a1=blob_get_u8(attr2);

//打印數據

printf("str=%s\n",str);    
printf("a1=%d\n",a1);

//輸出有效數據地址  和 id
ptr=blob_data(attr1);   // blob_data
printf("ptr=%p\n",ptr);   
printf("id =%d",blob_id(attr1));  //blob_id

printf(" 有效存儲空間大小  blob_len =%d\n",blob_len(attr1));       // blob_len 
printf("完全存儲空間大小  blob_raw_len =%d\n",blob_raw_len(attr1));   // blob_raw_len
printf("blob屬性填補後空間存儲大小  blob_pad_len =%d\n",blob_raw_len(attr1)); //blob_raw_len


rem=12;
__blob_for_each_attr(pos,attr1,rem)  //遍歷
{
  str1=blob_get_string(pos);
  printf("str1 =%s\n",str1);
}
return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章