openwrt之ubus例子

2024-02-08 00:32
文章标签 openwrt 例子 ubus

本文主要是介绍openwrt之ubus例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

好一个ic

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 

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例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/689413

相关文章

JavaFX环境的搭建和一个简单的例子

之前在网上搜了很多与javaFX相关的资料,都说要在Eclepse上要安装sdk插件什么的,反正就是乱七八糟的一大片,最后还是没搞成功,所以我在这里写下我搭建javaFX成功的环境给大家做一个参考吧。希望能帮助到你们! 1.首先要保证你的jdk版本能够支持JavaFX的开发,jdk-7u25版本以上的都能支持,最好安装jdk8吧,因为jdk8对支持JavaFX有新的特性了,比如:3D等;

javaScript日期相加减例子

当前时间加上2天 var d = new Date(“2015-7-31”); d.setDate(d.getDate()+2); var addTwo=d.getFullYear()+”年”+(d.getMonth()+1)+”月”+d.getDate()+”日”; “控制台输出===============”+”当前日期加2天:”+addTwo; 使用这种方法,月份也会给你计算.

设计模式大全和详解,含Python代码例子

若有不理解,可以问一下这几个免费的AI网站 https://ai-to.cn/chathttp://m6z.cn/6arKdNhttp://m6z.cn/6b1quhhttp://m6z.cn/6wVAQGhttp://m6z.cn/63vlPw 下面是设计模式的简要介绍和 Python 代码示例,涵盖主要的创建型、结构型和行为型模式。 一、创建型模式 1. 单例模式 (Singleton

JSP 简单表单显示例子

<html><!--http://localhost:8080/test_jsp/input.html --><head><meta http-equiv="Content-Type" content="text/HTML; charset=utf-8"><title>input页面</title></head><body><form action="input.jsp" method

shell循环sleep while例子 条件判断

i=1# 小于5等于时候才执行while [ ${i} -le 5 ]doecho ${i}i=`expr ${i} + 1`# 休眠3秒sleep 3doneecho done 参考 http://c.biancheng.net/cpp/view/2736.html

【ReactJS】通过一个例子学习React组件的生命周期

源代码 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Reac

openwrt的旁路模式无法访问国内网站

防火墙: 常规设置-> 区域:  lan-> wan :编辑 IP 动态伪装:勾选

简单的android Listview使用例子

为了熟悉Listview的使用,做了一个小例子联系一下, 主要步骤: 1. 在MainActivity中,创建一个adapter对象(可以是android自带的ArrayAdapter,也可以是自定义的如SongAdapter) 2. 如果自定义,就要创建ListView的子项,如song_listview_item.xml 3. 创建ListView对象,并用setAdapter方法把a

【 python pymongo】使用pymongo的例子

MongoDB优点 MongoDB是一个为当代web应用而生的noSQL数据库,它有如下优点: 1、文档型存储。可以把关系型数据库的表理解为一个电子表格,列表示字段,每行的记录其实是按照列的字段顺序排列的值得元组。而存储在MongoDB中的文档被存储为键-值对的形式,值却可以是任意类型且可以嵌套。之前在用关系型数据库的时候,我们把产品信息打散到不同的表中,要通过关系表或者使用join拼接成复杂

c:if test=/c:if如何判断空(使用例子)

userName是登录的时候放到session中了 <c:if test="${ not empty userName }">这表示userName判断不为null `<c:if test="${empty userName }"> ` 这表示userName判断为null 使用案例 <c:if test="${ not empty userName }"><ul><li><a