Amxb_Ubus  3.3.1
Ambiorix Ubus API
publisher.lua
Go to the documentation of this file.
1 #!/usr/bin/env lua
2 
3 package.cpath = "/lib/lua/?.so;" .. package.cpath
4 
5 require "ubus"
6 require "uloop"
7 
8 --[[
9  A demo of ubus publisher binding. Should be run before subscriber.lua
10 --]]
11 
12 
13 uloop.init()
14 
15 print("Connect")
16 local conn = ubus.connect()
17 if not conn then
18  error("Failed to connect to ubus")
19 end
20 
21 local ubus_objects = {
22  ["com.broadcom.gpon_md"] = {
23  hello = {
24  function(req, msg)
25  conn:reply(req, {message="foo"});
26  print("Call to function 'hello'")
27  for k, v in pairs(msg) do
28  print("key=" .. k .. " value=" .. tostring(v))
29  end
30  end, {id = ubus.INT32, msg = ubus.STRING }
31  },
32  },
33  test = {
34  hello = {
35  function(req, msg)
36  conn:reply(req, {message="foo"});
37  print("Call to function 'hello'")
38  for k, v in pairs(msg) do
39  print("key=" .. k .. " value=" .. tostring(v))
40  end
41  end, {id = ubus.INT32, msg = ubus.STRING }
42  },
43  hello1 = {
44  function(req)
45  conn:reply(req, {message="foo1"});
46  conn:reply(req, {message="foo2"});
47  print("Call to function 'hello1'")
48  end, {id = ubus.INT32, msg = ubus.STRING }
49  },
50  __subscriber_cb = function( subs )
51  print("total subs: ", subs )
52  end
53  }
54 }
55 
56 print("Add objects")
57 conn:add( ubus_objects )
58 print("Objects added, starting loop")
59 
60 -- start time
61 local timer
62 local counter = 0
63 function t()
64  counter = counter + 1
65  local params = {
66  count = counter,
67  text = "hello world"
68  }
69  conn:notify( ubus_objects.test.__ubusobj, "blabla", params )
70  timer:set(1000)
71 end
72 timer = uloop.timer(t)
73 timer:set(1000)
74 
75 print("Run loop")
76 uloop.run()