本文主要是介绍openwrt之ubus例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
好一个ic
{
"id": 1,
"msg": "hi",
"shuzu": [
"a",
"b"
]
}
文件目录
hello_ubus/
├── files
│ └── etc
│ └── init.d
│ └── hello_ubus
├── Makefile
└── src
├── hello_ubus.c
└── Makefile
hello_ubus/Makefile
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/kernel.mkPKG_NAME:=hello_ubus
PKG_RELEASE:=1
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
PKG_INSTALL_DIR:=$(PKG_BUILD_DIR)/ipkg-installdefine Package/$(PKG_NAME)SECTION:=utilsCATEGORY:=UtilitiesTITLE:= ubus demo(hello work)DEPENDS:= +libubus +libubox +ubusd +libuci +libjson-c
endefdefine Package/$(PKG_NAME)/descriptionhello ubus
endefdefine Build/Preparemkdir -p $(PKG_BUILD_DIR)$(CP) ./src/* $(PKG_BUILD_DIR)/
endefTARGET_CFLAGS += \-I$(STAGING_DIR)/usr/includedefine 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"
endefdefine 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
OUTPUT = hello_ubus
OBJ = hello_ubus.o
LIBS = -lm -lubus -lubox -lpthread -luci -ljson-call: $(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
#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();// allocjobj = json_object_new_object();// fill injson_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));// freejson_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
#!/bin/sh /etc/rc.common
START=99
SERVICE_USE_PID=1
USE_PROCD=1
_BIN=/bin/hello_ubus#. /lib/functions.shstart_service() {procd_open_instanceprocd_set_param stdout 1procd_set_param stderr 1procd_set_param command $_BINprocd_set_param respawnprocd_close_instance
}reload_service() {restart
}
这篇关于openwrt之ubus例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!