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
最后就可以通过键盘输入进行交互通信了