openwrt ubus (OpenWrt micro bus 架構)

爲了在OpenWrt中提供守護進程和應用程序間的通信,開發了ubus項目工程。它包含了守護進程、庫以及一些額外的幫助程序。html

核心部分是ubusd守護進程,它提供了其餘守護進程將本身註冊以及發送消息的接口。由於這個,接口經過使用Unix socket來實現,並使用TLV(type-length-value)消息。git

爲了簡化軟件的開發,能夠使用已有的libubus庫來使用ubus(鏈接ubus)。web

每一個守護進程在本身的名稱空間中註冊自有的路徑。每一個路徑能夠提供多個帶有不定數量參數的方法,方法能夠經過消息回覆調用。服務器

代碼在LGPL 2.1受權方法下發布,你能夠經過git在git://nbd.name/luci2/ubus.git或經過http在http://nbd.name/gitweb.cgi?p=luci2/ubus.git;a=summary獲取。 ubus從r28499起被包含在OpenWrt中。socket

ubus命令行工具函數

ubus能夠和ubusd服務器交互(和當前全部已經註冊的服務). 它對研究和調試註冊的命名空間以及編寫腳本很是有用。對於調用帶參數和返回信息的方法,它使用友好的JSON格式。下面是它的命令說明。工具

list
缺省列出全部經過RPC服務器註冊的命名空間:oop

root@uplink:~# ubus list
network
network.device
network.interface.lan
network.interface.loopback
network.interface.wan
root@uplink:~#

若是調用時包含參數-v,將會顯示指定命名空間更多方法參數等信息:spa

root@uplink:~# ubus -v list network.interface.lan
'network.interface.lan' @099f0c8b
    "up": {  }
    "down": {  }
    "status": {  }
    "prepare": {  }
    "add_device": { "name": "String" }
    "remove_device": { "name": "String" }
    "notify_proto": {  }
    "remove": {  }
    "set_data": {  }
root@uplink:~#

call
調用指定命名空間中指定的方法,而且經過消息傳遞給它:命令行

root@uplink:~# ubus call network.interface.wan status
{
    "up": true,
    "pending": false,
    "available": true,
    "autostart": true,
    "uptime": 86017,
    "l3_device": "eth1",
    "device": "eth1",
    "address": [
        {
            "address": "178.25.65.236",
            "mask": 21
        }
    ],
    "route": [
        {
            "target": "0.0.0.0",
            "mask": 0,
            "nexthop": "178.25.71.254"
        }
    ],
    "data": {

    }
}
root@uplink:~#

消息參數必須是有效的JSON字符串,而且攜帶函數所要求的鍵及值:

root@uplink:~# ubus call network.device status '{ "name": "eth0" }'
{
    "type": "Network device",
    "up": true,
    "link": true,
    "mtu": 1500,
    "macaddr": "c6:3d:c7:90:aa:da",
    "txqueuelen": 1000,
    "statistics": {
        "collisions": 0,
        "rx_frame_errors": 0,
        "tx_compressed": 0,
        "multicast": 0,
        "rx_length_errors": 0,
        "tx_dropped": 0,
        "rx_bytes": 0,
        "rx_missed_errors": 0,
        "tx_errors": 0,
        "rx_compressed": 0,
        "rx_over_errors": 0,
        "tx_fifo_errors": 0,
        "rx_crc_errors": 0,
        "rx_packets": 0,
        "tx_heartbeat_errors": 0,
        "rx_dropped": 0,
        "tx_aborted_errors": 0,
        "tx_packets": 184546,
        "rx_errors": 0,
        "tx_bytes": 17409452,
        "tx_window_errors": 0,
        "rx_fifo_errors": 0,
        "tx_carrier_errors": 0
    }
}
root@uplink:~#

listen
設置一個監聽socket並觀察進入的事件:

root@uplink:~# ubus listen &
root@uplink:~# ubus call network.interface.wan down
{ "network.interface": { "action": "ifdown", "interface": "wan" } }
root@uplink:~# ubus call network.interface.wan up
{ "network.interface": { "action": "ifup", "interface": "wan" } }
{ "network.interface": { "action": "ifdown", "interface": "he" } }
{ "network.interface": { "action": "ifdown", "interface": "v6" } }
{ "network.interface": { "action": "ifup", "interface": "he" } }
{ "network.interface": { "action": "ifup", "interface": "v6" } }
root@uplink:~# 

send
發送一個事件提醒:

root@uplink:~# ubus listen &
root@uplink:~# ubus send foo '{ "bar": "baz" }'
{ "foo": { "bar": "baz" } }
root@uplink:~# 

本文章由http://www.wifidog.pro/2015/08/10/openwrt-ubus%E6%9E%B6%E6%9E%84.html整理編輯,轉載請註明出處

相關文章
相關標籤/搜索