您的位置:首页 > 运维架构

openwrt之ubus例子

2020-01-15 08:28 1121 查看

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/strugglelg/article/details/60883152

 

[code]
root@LEDE:/#   ubus call test_ubus helloworld '{"id":1,"msg":"hi","array":["a","b"]}'
{
"id": 1,
"msg": "hi",
"shuzu": [
"a",
"b"
]
}

文件目录

[code]hello_ubus/
├── files
│   └── etc
│       └── init.d
│           └── hello_ubus
├── Makefile
└── src
├── hello_ubus.c
└── Makefile

hello_ubus/Makefile 

[code]include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=hello_ubus
PKG_RELEASE:=1
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
PKG_INSTALL_DIR:=$(PKG_BUILD_DIR)/ipkg-install

define Package/$(PKG_NAME)
SECTION:=utils
CATEGORY:=Utilities
TITLE:= ubus demo(hello work)
DEPENDS:= +libubus +libubox +ubusd +libuci +libjson-c
endef

define Package/$(PKG_NAME)/description
hello ubus
endef

define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef
TARGET_CFLAGS += \
-I$(STAGING_DIR)/usr/include
define Build/Compile
$(MAKE) -C $(PKG_BUILD_DIR) \
CROSS_COMPILE="$(TARGET_CROSS)" \
CC="$(TARGET_CC)" \
AR="$(TARGET_CROSS)ar" \
LD="$(TARGET_CROSS)ld" \
CFLAGS="$(TARGET_CFLAGS)   $(TARGET_CPPFLAGS)" \
LDFLAGS="$(TARGET_LDFLAGS) -L$(STAGING_DIR)/usr/lib"
endef
define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/hello_ubus $(1)/bin/
$(CP) files/* $(1)/
endef
$(eval $(call BuildPackage,$(PKG_NAME)))

hello_ubus/src/Makefile

[code]OUTPUT = hello_ubus
OBJ = hello_ubus.o
LIBS = -lm -lubus -lubox -lpthread -luci -ljson-c

all: $(OUTPUT)

$(OUTPUT): $(OBJ)
@echo "...................................."
@echo "CC = " $(CC)
@echo "CFLAGS = " $(CFLAGS)
@echo "LDFLAGS = " $(LDFLAGS)
@echo "...................................."
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) $(LDLIBS)

%.o: %.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ -c $^ $(LDLIBS) $(LIBS)

clean:
-rm $(OUTPUT) *.o

hello_ubus/src/hello_ubus.c 

[code]#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>

#include <libubus.h>

#include <libubox/uloop.h>
#include <libubox/list.h>
#include <libubox/blobmsg_json.h>
#include <json-c/json.h>

struct ubus_context *ctx;
struct blob_buf b;

enum {
HELLO_ID,
HELLO_MSG,
HELLO_ARRAY,
__HELLO_MAX,
};

static const struct blobmsg_policy hello_policy[__HELLO_MAX] = {
[HELLO_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
[HELLO_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
[HELLO_ARRAY] = { .name = "array", .type = BLOBMSG_TYPE_ARRAY },
};

#if 0
// define
struct json_object *jobj;

json_object * jobj = json_object_new_object();
json_object *jstring = json_object_new_string("Joys of Programming");
json_object *jint = json_object_new_int(10);
json_object *jboolean = json_object_new_boolean(1);
json_object *jdouble = json_object_new_double(2.14);
json_object *jarray = json_object_new_array();

// alloc
jobj = json_object_new_object();

// fill in
json_object *buf1 = json_object_new_string("c");
json_object *buf2 = json_object_new_string("c++");
json_object *buf3 = json_object_new_string("php");

json_object_array_add(object,buf1);
json_object_array_add(object,buf2);
json_object_array_add(object,buf3);
// json_object_object_add(jobj, "answer", json_object_new_string(answer));

// free
json_object_put(object);
#endif

// ubus call test_ubus helloworld '{"id":1,"msg":"test_msg_hello_world"}'
static int test_hello(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req,
const char *method, struct blob_attr *msg)
{
struct blob_attr *tb[__HELLO_MAX];
int tmp_id;
char *tmp_msg = NULL;
char tmp_array[128];
int len;
struct blob_attr *attr;
void *arr;

blobmsg_parse(hello_policy, __HELLO_MAX, tb, blob_data(msg), blob_len(msg));

blob_buf_init(&b, 0);
if(tb[HELLO_ID])
{
tmp_id = blobmsg_get_u32(tb[HELLO_ID]);
blobmsg_add_u32(&b, "id", tmp_id);
}

if(tb[HELLO_MSG])
{
tmp_msg = blobmsg_get_string(tb[HELLO_MSG]);
blobmsg_add_string(&b, "msg", tmp_msg);
}

if(tb[HELLO_ARRAY] && blobmsg_type(tb[HELLO_ARRAY]) == BLOBMSG_TYPE_ARRAY)
{
arr=blobmsg_open_array(&b, "shuzu");

len = blobmsg_data_len(tb[HELLO_ARRAY]);
__blob_for_each_attr(attr, blobmsg_data(tb[HELLO_ARRAY]), len)
{
if (blobmsg_type(attr) == BLOBMSG_TYPE_STRING)
{
char *tmp = blobmsg_get_string(attr);
blobmsg_add_blob(&b, attr);
printf("array1=%s\n", tmp);
}
}
blobmsg_close_array(&b, arr);
}

printf("tmp_id=%d, tmp_msg=%s, tmp_array=%s\n",tmp_id,tmp_msg,tmp_array);

/*
{
json_object_array_add(array, buf1);
json_object_array_add(array, buf2);
json_object_object_add(json_all, "shuzhu", array);
}
//blobmsg_add_json_element(&b, "", array);
*/
ubus_send_reply(ctx, req, b.head);

return 0;
}

static const struct ubus_method test_methods[] = {
UBUS_METHOD("helloworld", test_hello, hello_policy),
};

static struct ubus_object_type test_object_type =
UBUS_OBJECT_TYPE("test_ubus", test_methods);

static struct ubus_object test_object = {
.name = "test_ubus",
.type = &test_object_type,
.methods = test_methods,
.n_methods = ARRAY_SIZE(test_methods)
};

int ubus_doing()
{
int ret;

ctx = ubus_connect(NULL);
if (!ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
}
ubus_add_uloop(ctx);

ret = ubus_add_object(ctx, &test_object);
if (ret)
fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
}

int main()
{
int ret;

uloop_init();
ubus_doing();
uloop_run();

ubus_free(ctx);
uloop_done();

return 0;
}

files/etc/init.d/hello_ubus

[code]#!/bin/sh /etc/rc.common
START=99
SERVICE_USE_PID=1
USE_PROCD=1
_BIN=/bin/hello_ubus

#. /lib/functions.sh

start_service() {
procd_open_instance

procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param command  $_BIN
procd_set_param respawn

procd_close_instance
}

reload_service() {
restart
}

 

  • 点赞
  • 收藏
  • 分享
  • 文章举报
站内首发文章 ultra seven 发布了9 篇原创文章 · 获赞 8 · 访问量 3037 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: