ubus c語言例子,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.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

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

#include

#include

#include

#include

#include

#include

#include

#include

#include

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

#!/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

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281551.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281551.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281551.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用Spring訪問Mongodb的方法大全——Spring Data MongoDB查詢指南

1.概述 Spring Data MongoDB 是Spring框架訪問mongodb的神器,借助它可以非常方便的讀寫mongo庫。本文介紹使用Spring Data MongoDB來訪問mongodb數據庫的幾種方法: 使用Query和Criteria類JPA自動生成的查詢方法使用Query 注解基于JSON查詢在開始前&#…

mysqldump導出備份數據庫報Table ‘performance_schema.session_variables‘ doesn‘t exist

今天在bash進行本地數據庫往云端數據庫導數據的時候,在本地導出.sql文件這第一步就出現了錯誤問題,導出sql文件的命令: 1 mysqldump -u 用戶名 -p 數據庫名 > xxx.sql 在做這一步將數據導出的時候報了這么一個錯誤, 1 mysqldu…

在Identity框架中使用RoleBasedAuthorization

本文將介紹在 Identity 框架中如何使用 Sang.AspNetCore.RoleBasedAuthorization[1] 庫。核心介紹Identity 和 jwt 的基本配置我們在這里不再贅述,可以參考最后的項目樣例。核心的代碼主要為 IRolePermission 的實現。internal class MyRolePermission : IRolePermi…

2016年印度公有云服務市場將達13億美元

根據IT咨詢公司Gartner最新調查數據顯示,2016年印度公有云服務市場預計將增長35.9%,達到13億美元。 增長最快的是云系統基礎設施即服務(IaaS),2016年預計將增長45.5%;其次是平臺即服務(PaaS&…

PAT 1042. 字符統計

1042. 字符統計 請編寫程序,找出一段給定文字中出現最頻繁的那個英文字母。 輸入格式: 輸入在一行中給出一個長度不超過1000的字符串。字符串由ASCII碼表中任意可見字符及空格組成,至少包含1個英文字母,以回車結束(回車…

Magicodes.IE 2.7.0-beta發布

2.7.0-beta2022.10.27使用SixLabors.ImageSharp替代System.Drawing,感謝linch90 (見pr#454)2.6.92022.10.26fix: 動態數據源導出到多個sheet的問題 (見#449)2.6.82022.10.18Excel模板導出添加API,以支持通過…

光伏逆變器“領跑”:不止于技術

從無到有,從效率比拼到突破99%,在跟進速度上沒話說的國內光伏逆變器企業難免深陷“價格戰”、同質化的泥潭。隨著“領跑者”計劃躍居光伏主流,嗅到市場紅利的企業再次蜂擁而至。 目前,鑒衡認證發布的第一批光伏并網逆變器“領跑者…

Ubuntu 18.04上Qmmp安裝教程

Qmmp,一個開源的基于Qt的多媒體播放器。它具有多種音頻文件格式支持,DSP效果,視覺效果;輸出系統支持(OSS4(FreeBSD),ALSA(Linux),Pulse Audio,JAC…

android自動跑馬燈,Android-最強跑馬燈

Android--最強跑馬燈Android 跑馬燈已經有很多版本,從最基本的TextView,到重寫TextView使TextView取消焦點限制,還有重寫TextView利用ScrollTo方法寫的,基本都能滿足一般需要。然而在使用過程中,發現一些意外---有時會…

python:軟件目錄結構規范

為什么要設計好目錄結構? “設計項目目錄結構”,就和“代碼編碼風格”一樣,屬于個人風格問題。對于這種風格上的規范,一直都存在兩種態度: 1.一種認為,這種個人風格問題“無關緊要”。理由是能讓程序work就…

開啟智能生活新時代 河北省智慧社區建設從各個擊破

智慧社區作為智慧城市的重要組成部分,是城市智慧落地的觸點,是城市管理、政務服務和市場服務的載體。隨著智慧城市的推廣以及新一代技術的普及,智慧社區的項目必將迎來新一輪的快速發展。2016年智慧社區成為企業業務落地的承載點,…

C# WPF 表格控件的前后臺數據交互?

概述GridControl控件使用我們已經進行了實例講解,這節內容我們列舉一個特殊的應用場景:表格中有一列CheckBox,默認都處于勾選狀態,當用戶通過界面操作后,我們要確保用戶至少選擇了一項,相當于一次數據驗證&…

Java(C#)基礎差異-語法

1、long類型 Java long類型,若賦值大于int型的最大值,或小于int型的最小值,則需要在數字后加L或者l,表示該數值為長整數,如long num2147483650L。 舉例如下: public static void main(String[] args) {/** …

android防止左向右滑出程序,Android——ViewPager禁止左右滑動的實現

目錄1 背景用ViewPagerBottomNavigationView多個Fragment快速搭建的頁面切換架構,一個有四個頁面,因為測試需要,需要屏蔽掉中間的兩個,做法是:設置不可點擊選擇:xml布局文件中,BottomNavigation…

Yii2 的快速配置 api 服務 yii2-fast-api

yii2-fast-api yii2-fast-api是一個Yii2框架的擴展,用于配置完善Yii2,以實現api的快速開發。 此擴展默認的場景是APP的后端接口開發,因此偏向于實用主義,并未完全采用restfull的標準,方便前端開發處理接口數據以及各種…

.NET6打包部署到Windows Service

1.安裝Nuget包安裝以下nuget包支持windows service<PackageReference Include"Microsoft.AspNetCore.Hosting.WindowsServices" Version"6.0.10" /> <PackageReference Include"Microsoft.Extensions.Hosting.WindowsServices" Version…

傳統家電在智能家居變革的五大優勢

而在眾多向智能家居領域轉型變革的企業中&#xff0c;看似落后的傳統家電企業&#xff0c;卻占據著一定的優勢。 產品優勢 傳統家電企業在產品上的優勢主要體現在企業擁有產品本身的設計、技術、生產、制造和營銷渠道&#xff0c;其產品不論是從外觀設計、零件制造還是零件組裝…

《Apache Kafka實戰》讀書筆記-調優Kafka集群

《Apache Kafka實戰》讀書筆記-調優Kafka集群 作者&#xff1a;尹正杰 版權聲明&#xff1a;原創作品&#xff0c;謝絕轉載&#xff01;否則將追究法律責任。 一.確定調優目標 1>.常見的非功能性要求 一.性能&#xff08;performance&#xff09;最重要的非功能性需求之一。…

android emoji unicode編碼表,unicode編碼

unicode編碼app是一款字符查找客戶端應用&#xff0c;通過unicode編碼可以方便尋找特定字符&#xff0c;查看字符表情詳細的描述&#xff0c;并且利用unicode編碼就可以快捷復制任意unicode編碼&#xff0c;提高開發效率&#xff0c;非常的實用&#xff0c;快來下載unicode編碼…

物聯網商機誘人 芯片商大力搭建生態系統

應用需求變化多端的物聯網&#xff0c;雖具備龐大的發展潛力及應用商機&#xff0c;但由于市場過于分散&#xff0c;幾乎沒有殺手應用可言&#xff0c;因此對有意耕耘相關市場的半導體業者而言&#xff0c;如何借力使力&#xff0c;尋找盟友共同搭建出的生態系統&#xff0c;遂…