forked from daurnimator/ldbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.lua
119 lines (91 loc) · 3.87 KB
/
example.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
local ldbus = require "ldbus"
sleep = require"socket".sleep
function sendsignal ( sigvalue )
print ( "Sending signal with value " .. sigvalue )
local conn = assert ( ldbus.bus.get ( "session" ) )
assert ( ldbus.bus.request_name ( conn , "test.signal.source" , { replace_existing = true } ) )
local msg = assert ( ldbus.message.new_signal ( "/test/signal/Object" , "test.signal.Type" , "Test" ) , "Message Null" )
local iter = ldbus.message.iter.new ( )
msg:iter_init_append ( iter )
assert ( iter:append_basic ( sigvalue ) , "Out of Memory" )
local ok , serial = assert ( conn:send ( msg ) )
conn:flush ( )
print ( "Signal Sent" )
end
function query ( param )
print ( "Calling remote method with " .. param )
local conn = assert ( ldbus.bus.get ( "session" ) )
assert ( ldbus.bus.request_name ( conn , "test.signal.source" , { replace_existing = true } ) )
local msg = assert ( ldbus.message.new_method_call ( "test.method.server" , "/test/method/Object" , "test.method.Type" , "Method" ) , "Message Null" )
local iter = ldbus.message.iter.new ( )
msg:iter_init_append ( iter )
assert ( iter:append_basic ( param ) , "Out of Memory" )
local reply = assert ( conn:send_with_reply_and_block ( msg ) )
assert ( reply:iter_init ( iter ) , "Message has no arguments" )
assert ( iter:get_arg_type ( ) == ldbus.types.boolean , "Argument not boolean!" )
local stat = iter:get_basic ( )
assert ( iter:next ( ) )
assert ( iter:get_arg_type ( ) == ldbus.types.uint32 , "Argument not int!" )
local level = iter:get_basic ( )
print( "Got reply: " .. tostring ( stat ) .. ", " .. level )
end
local function reply_to_method_call ( msg , conn )
local stat , level = true , 21614
local iter = ldbus.message.iter.new ( )
assert ( msg:iter_init ( iter ) , "Message has no arguments" )
assert ( iter:get_arg_type ( ) == ldbus.types.string , "Argument is not a string" )
local param = iter:get_basic ( )
print ( "Method called with " .. param )
local reply = assert ( msg:new_method_return ( ) )
reply:iter_init_append ( iter )
assert ( iter:append_basic ( stat , ldbus.types.boolean ) , "Out of Memory" )
assert ( iter:append_basic ( level , ldbus.types.uint32 ) , "Out of Memory" )
local ok , serial = assert ( conn:send ( reply ) , "Out of Memory" )
conn:flush ( )
end
function listen ( )
print ( "Listening for method calls" )
local conn = assert ( ldbus.bus.get ( "session" ) )
assert ( assert ( ldbus.bus.request_name ( conn , "test.method.server" , { replace_existing = true } ) ) == "primary_owner" , "Not Primary Owner" )
while conn:read_write ( 0 ) do
local msg = conn:pop_message ( )
if not msg then
sleep ( 1 )
elseif msg:get_type ( ) == "method_call" then
reply_to_method_call ( msg , conn )
end
end
end
function receive ( )
print ( "Listening for signals" )
local conn = assert ( ldbus.bus.get ( "session" ) )
assert ( assert ( ldbus.bus.request_name ( conn , "test.signal.sink" , { replace_existing = true } ) ) == "primary_owner" , "Not Primary Owner" )
assert ( ldbus.bus.add_match ( conn , "type='signal',interface='test.signal.Type'" ) )
conn:flush ( )
print ( "Match rule sent" )
while conn:read_write ( 0 ) do
local msg = conn:pop_message ( )
if not msg then
sleep ( 1 )
elseif msg:get_type ( ) == "signal" then
local iter = ldbus.message.iter.new ( )
assert ( msg:iter_init ( iter ) , "Message has no parameters" )
assert ( iter:get_arg_type ( ) == ldbus.types.string , "Argument is not a string" )
local sigvalue = iter:get_basic ( )
print ( "Got signal with value " .. sigvalue )
end
end
end
local op = arg [ 1 ]
if op == "send" then
sendsignal ( arg [ 2 ] or "no param" )
elseif op == "receive" then
receive ( )
elseif op == "listen" then
listen ( )
elseif op == "query" then
query ( arg [ 2 ] or "no param" )
else
print ( "Syntax: dbus-example [send|receive|listen|query] [<param>]" )
os.exit ( 1 )
end