openwrt之ubus例子

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



文件目录

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



hello_ubus/Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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/hello_ubus.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/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
}


本文章由作者:佐须之男 整理编辑,原文地址: openwrt之ubus例子
本站的文章和资源来自互联网或者站长的原创,按照 CC BY -NC -SA 3.0 CN协议发布和共享,转载或引用本站文章应遵循相同协议。如果有侵犯版权的资 源请尽快联系站长,我们会在24h内删除有争议的资源。欢迎大家多多交流,期待共同学习进步。

相关推荐