ubus lua 範例
看了ubus souce程式,也不太清楚ubus用法
不過這個lua範例就很清楚
更新記錄
| item | note | 
|---|---|
| 20170209 | 第一版 | 
目錄
test.lua
此範例來源:ubus-2015-05-25/lua
- github: test.lua
- local: [test.lua] 
- ubus.connect 
 連接ubusd (/var/run/ubus.sock) 並且加到uloop- 1 
 2- ctx = ubus_connect() 
 ubus_add_uloop(ctx);
- conn:add(my_method) 
 使用ubus_add_object(ctx,obj)跟ubusd注冊新的object
- conn:listen(my_event) 
 使用ubus_register_event_handler注冊新的event
 監聽test event(由ubus send送出的event)
 監聽由ubus_send_event的test event- 1 - ubus_register_event_handler(); 
- conn:reply 
 使用ubus_send_reply 回應client
測試ubus server (test.lua)
- test.lua提供下例method - 1 
 2
 3
 4
 5
 6- ~ # ubus list -v 
 'broken' @0521ea88
 "hello1":{}
 'test' @89354d57
 "hello":{"id":"Integer","msg":"String"}
 "hello1":{"id":"Integer","msg":"String"}
- test.lua server log 
 test.lua只listen test,因此若為test1則不會有打印
 可使用conn:reply來回應client- 1 
 2
 3
 4
 5
 6
 7
 8- /opt/dvr_board/bin # ./test.lua 
 Call to function broken hello1
 Call to function 'hello'
 key=id value=123
 key=msg value=str123
 Call to function 'hello1'
 Call to test event
 key=layout value=6x6
- ubus command - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13- ~ # ubus call test hello '{"id":123, "msg":"str123"}' 
 {
 "message": "foo"
 }
 ~ # ubus call test hello1 '{"id":123, "msg":"str123"}'
 {
 "message": "foo1"
 }
 {
 "message": "foo2"
 }
 ~ # ubus send test '{"layout":"6x6"}'
 ~ # ubus send test1 '{"layout":"6x6"}
test_client.lua
此範例來源:ubus-2015-05-25/lua
- conn:objects
 ubus_lua_objects
 ubus_lookup
- conn:call 
 ubus_lookup_id : 找obj id
 ubus_invoke : 指定object執行其方法(method)
- conn:send 
 ubus_send_event
測試ubus client (test_client.lua)
- test_client.lua - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13- /opt/dvr_board/bin # ./test_client.lua 
 namespace=broken
 procedure=hello1
 namespace=test
 procedure=hello
 attribute=id type=5
 attribute=msg type=3
 procedure=hello1
 attribute=id type=5
 attribute=msg type=3
 key=message value=foo
 key=message value=foo1
 key=message value=foo2
- server - 1 
 2
 3
 4
 5- Call to function 'hello' 
 key=msg value=eth0
 Call to function 'hello1'
 Call to test event
 key=foo value=bar
test_client file
這邊使用conn:object查訊obj-id,只是顯示目前在ubusd內有那些obj
若沒有查訊(conn:object)也是可以送訊
使用conn:object查訊obj-id
使用conn:call執行obj的method,此時return value為server msg
使用conn:send來送event及參數
- github: test_client.lua
- local: [test_client.lua]
libusb 介面說明
- libubs介面說明: 
 ubus_register_event_handle: 注册新事件
 ubus_send_event : 发出事件消息
 ubus_connect: 初始化client端context结构,并连接ubusd
- libus-io介面說明: 
 ubus_send_msg: 发送报文 (client上下文對象)
- libubs-obj介面說明 
 ubus_add_object: client端向ubusd server请求增加一个新object
- libusb-req介面說明- ubus_send_reply: 發送回应信息,消息类型UBUS_MSG_DATA- libus-io / ubus_send_msg(,UBUS_MSG_DATA)
 
- ubus_invoke : 指定object執行其方法(method)
 
- ubus_send_reply: 發送回应信息,消息类型UBUS_MSG_DATA