ubus例程server+client交互通信

server.c

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "ubus_server.h"
#include "event.h"

struct event ev;
struct event_base *main_base;

void myfunc(const int fd, const short which, void *arg)
{
	char buf[100] = {0};
	int ret = read(fd,buf,sizeof(buf));
	if (ret > 0)
	{
	//	printf("read buf:%s\n",buf);
	}
	Send_user_message();
}

void QUIT()
{
	exit(0);
}
int main(int argc,char *argv[])
{
	signal(SIGINT,QUIT);
	ubus_thread("/var/run/ubus.sock");
	main_base = event_init();
	event_set(&ev,0,EV_READ|EV_PERSIST,myfunc,main_base);
        event_base_set(main_base, &ev);
        event_add(&ev,NULL);
        event_base_loop(main_base,0);
	return 0;
}

ubus_server.c

#include "ubus_server.h"
#define DBG_UBUS_SERVER 2
static int id;
static struct blob_buf b;
static struct ubus_context *ctx;
enum {
        name,
        age,
        address,
        __EXCUE_MAX
};

static const struct blobmsg_policy get_address_policy[__EXCUE_MAX] = {
  [name] = { .name = "name", .type = BLOBMSG_TYPE_STRING},
  [age] = { .name = "age", .type = BLOBMSG_TYPE_INT8},
  [address] = { .name = "address", .type = BLOBMSG_TYPE_STRING},
};

static int get_address(struct ubus_context *uctx, struct ubus_object *obj,
                      struct ubus_request_data *req, const char *method,
                      struct blob_attr *msg)
{

        struct blob_attr *tb[__EXCUE_MAX];
        char *name_;
        int age_;
        char *address_;
        void *dtable;

        blobmsg_parse(get_address_policy, __EXCUE_MAX, tb, blob_data(msg), blob_len(msg));

        if (!tb[name] || !tb[age] || !tb[address])
                return UBUS_STATUS_INVALID_ARGUMENT;

        name_ = blobmsg_get_string(tb[name]);
        address_ = blobmsg_get_string(tb[address]);
        age_ = blobmsg_get_u8(tb[age]);
        printf("name:%s,address:%s,age:%d\n",name_,address_,age_);
}

static const struct ubus_method address_methods[] = {
        UBUS_METHOD("name", get_address, get_address_policy),
};

static struct ubus_object_type address_object_type =
        UBUS_OBJECT_TYPE("server", address_methods);

static struct ubus_object address_object = {
        .name = "server",
        .type = &address_object_type,
        .methods = address_methods,
        .n_methods = ARRAY_SIZE(address_methods),
        //.subscribe_cb = client_subscribe_cb,
};
static void report_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
	printf("report_cb\n");
}

int Send_user_message()
{
        blobmsg_buf_init(&b);
        blobmsg_add_string(&b, "name","Bruvin");
        blobmsg_add_u8(&b, "age",24);
        blobmsg_add_string(&b,"address","guangdong shenzhen");

        //ubus_invoke(ctx, id, "report", b.head, report_cb, 0, 500);
	
	ubus_notify(ctx,  &address_object, "server", b.head, -1);
}

void *ubus_server_main(void *arg)
{
        int ret;
	char *ubus_socket = (char *)arg;
	for(;;)
	{
       		uloop_init();
        	ctx = ubus_connect(ubus_socket);
        	if (!ctx) {
			printf("ubus_connect failed\n");
               	 	return NULL;
        	}

        	ubus_add_uloop(ctx);

        	ret = ubus_add_object(ctx, &address_object);
        	if (ret)
			printf("ubus_add_object failed\n");
        	uloop_run();
	}

}

void Bru_create_pthread(void *(*func)(void *),void *arg)
{
        pthread_t thread;
        pthread_attr_t attr;
        int ret;
        pthread_attr_init(&attr);

        ret = pthread_create(&thread,&attr,func,arg);
        if (ret!=0)
        {
		printf("pthread_create failed\n");
                exit(1);
        }
}


void ubus_thread(char *arg)
{
	Bru_create_pthread(ubus_server_main,(void *)arg);	
}

ubus_server.h

#ifndef _UBUS_SERVER_H
#define _UBUS_SERVER_H

#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <time.h>
#include "libubus.h"
#include "blobmsg_json.h"

int Send_user_message();
void Bru_create_pthread(void *(*func)(void *),void *arg);
void ubus_thread(char *arg);


#endif

client.c

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "ubus_client.h"
#include "event.h"

struct event ev;
struct event_base *main_base;

void myfunc(const int fd, const short which, void *arg)
{
	char buf[100] = {0};
	int ret = read(fd,buf,sizeof(buf));
	if (ret > 0)
	{
		//printf("read buf:%s\n",buf);
	}
	Send_user_message();
}

void QUIT()
{
	exit(0);
}
int main(int argc,char *argv[])
{
	signal(SIGINT,QUIT);
	init_ubus();
	main_base = event_init();
	event_set(&ev,0,EV_READ|EV_PERSIST,myfunc,main_base);
        event_base_set(main_base, &ev);
        event_add(&ev,NULL);
        event_base_loop(main_base,0);
	return 0;
}

ubus_client.c

#include "ubus_client.h"
#define DBG_UBUS_CLIENT 1
static int id;
struct blob_buf b;
static struct ubus_subscriber event;
static struct ubus_context *ctx;

/*static const struct blobmsg_policy people[] = {
        [name] = { .name = "name", .type = BLOBMSG_TYPE_STRING},
        [age]  = { .name = "age", .type = BLOBMSG_TYPE_INT32 },
        [high] = { .name = "high", .type = BLOBMSG_TYPE_STRING},
        [weight] = { .name = "weight", .type = BLOBMSG_TYPE_STRING},
        [address] = { .anem = "address", .type = BLOBMSG_TYPE_STRING},
};*/

void Bru_create_pthread(void *(*func)(void *),void *arg)
{
        pthread_t thread;
        pthread_attr_t attr;
        int ret;
        pthread_attr_init(&attr);

        ret = pthread_create(&thread,&attr,func,arg);
        if (ret!=0)
        {
                printf("pthread_create failed\n");
                exit(1);
        }
}

enum {
        name,
        age,
        address,
        __EXCUE_MAX
};

static const struct blobmsg_policy get_address_policy[__EXCUE_MAX] = {
  [name] = { .name = "name", .type = BLOBMSG_TYPE_STRING},
  [age] = { .name = "age", .type = BLOBMSG_TYPE_INT8},
  [address] = { .name = "address", .type = BLOBMSG_TYPE_STRING},
};


static int Recv(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req,
                const char *method, struct blob_attr *msg)
{
        printf("recive data from ubus_server\n");
	 struct blob_attr *tb[__EXCUE_MAX];
        char *name_;
        int age_;
        char *address_;
        void *dtable;

        blobmsg_parse(get_address_policy, __EXCUE_MAX, tb, blob_data(msg), blob_len(msg));

        if (!tb[name] || !tb[age] || !tb[address])
                return UBUS_STATUS_INVALID_ARGUMENT;

        name_ = blobmsg_get_string(tb[name]);
        address_ = blobmsg_get_string(tb[address]);
        age_ = blobmsg_get_u8(tb[age]);
        printf("name:%s,address:%s,age:%d\n",name_,address_,age_);

}

void handle_remove(struct ubus_context *ctx,
                          struct ubus_subscriber *obj, uint32_t id)
{
        printf("remove .......\n");
	exit(0);
}

void *client_main(void *arg)
{
        int ret ;
        for(;;)
        {
                event.cb = Recv;
                event.remove_cb = handle_remove;
                ret = ubus_register_subscriber(ctx,&event);
                if (ret)
                {
                        printf("ubus_register_subscriber failed\n");
                        return NULL;
                }
                if (ubus_lookup_id(ctx, "server", &id)) {
                        printf("ubus_loojup_id failed \n");
                        return NULL;
                }
                ret = ubus_subscribe(ctx, &event, id);
                if(ret)
                        printf("Failed to subscribe: %s\n", ubus_strerror(ret));
                uloop_run();
        }
}

static void report_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
        printf("report_cb\n");
}
int Send_user_message()
{

        blobmsg_buf_init(&b);
        blobmsg_add_string(&b, "name","Bruvin");
        blobmsg_add_u8(&b, "age",24);
        blobmsg_add_string(&b,"address","guangdong shenzhen");

        ubus_invoke(ctx, id, "name", b.head, report_cb, 0, 500);
}

void init_ubus(void)
{
        const char *socket = "/var/run/ubus.sock";
        uloop_init();

        signal(SIGPIPE,SIG_IGN);
        ctx = ubus_connect(socket);
        if(!ctx)
        {
                printf("ubus connect failed\n");
                return;
        }
        ubus_add_uloop(ctx);
        Bru_create_pthread(client_main,NULL);
}

ubus_client.h

#ifndef _UBUS_CLIENT_H
#define _UBUS_CLIEN_H

#include <stdio.h>
#include <signal.h>
#include <pthread.h>
#include <time.h>
#include "libubus.h"
#include "blobmsg_json.h"

int Send_user_message();
void Bru_create_pthread(void *(*func)(void *),void *arg);
void init_ubus(void);


#endif

执行:先执行./server 再执行./client

最后就可以通过键盘输入进行交互通信了

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的ubus通信例子: 1. 首先需要在代码中引入相关的头文件: ``` #include <libubox/uloop.h> #include <libubus.h> ``` 2. 初始化ubus连接: ``` struct ubus_context *ctx = NULL; struct blob_buf b = { 0 }; /* Initialize the ubus context */ ctx = ubus_connect(NULL); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } /* Add the message handler */ ubus_add_uloop(ctx); /* Prepare a message buffer */ blob_buf_init(&b, 0); ``` 3. 发送ubus请求: ``` int ret; uint32_t id; /* Prepare the message */ blobmsg_add_string(&b, "message", "Hello, ubus!"); /* Send the message */ ret = ubus_lookup_id(ctx, "my_service", &id); if (ret != UBUS_STATUS_OK) { fprintf(stderr, "Failed to lookup ubus object\n"); goto out; } ret = ubus_invoke(ctx, id, "my_method", b.head, NULL, NULL, 3000); if (ret != UBUS_STATUS_OK) { fprintf(stderr, "Failed to invoke ubus method\n"); } out: blob_buf_free(&b); ``` 4. 注册ubus服务: ``` static int my_method_handler(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { printf("Received ubus message: %s\n", blobmsg_get_string(msg)); return UBUS_STATUS_OK; } static const struct ubus_method my_methods[] = { { .name = "my_method", .handler = my_method_handler }, {} }; static struct ubus_object_type my_object_type = UBUS_OBJECT_TYPE("my_object", my_methods); static struct ubus_object my_object = { .name = "my_service", .type = &my_object_type, .methods = my_methods, .n_methods = ARRAY_SIZE(my_methods), }; ... /* Register the ubus object */ ret = ubus_add_object(ctx, &my_object); if (ret) { fprintf(stderr, "Failed to add ubus object\n"); goto out; } /* Run the event loop */ uloop_run(); out: ubus_free(ctx); ``` 以上代码示例中,我们首先初始化了ubus连接并添加了ubus消息处理器。然后我们发送了一个带有"Hello, ubus!"字符串的ubus请求给名为"my_service"的ubus服务的"my_method"方法。最后,我们注册了一个名为"my_service"的ubus服务,并添加了一个名为"my_method"的ubus方法,该方法会在接收到ubus请求时被调用。最后,我们运行了ubus的事件循环。 希望这个例子能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值