tizen 2.3 release
[framework/connectivity/bluez.git] / test / test-alert
1 #!/usr/bin/python
2
3 from __future__ import absolute_import, print_function, unicode_literals
4
5 import optparse
6 import os
7 import sys
8 import dbus
9 import dbus.service
10 import dbus.mainloop.glib
11 try:
12   from gi.repository import GObject
13 except ImportError:
14   import gobject as GObject
15
16 BUS_NAME = 'org.bluez'
17 ALERT_INTERFACE = 'org.bluez.Alert1'
18 ALERT_AGENT_INTERFACE = 'org.bluez.AlertAgent1'
19 BLUEZ_OBJECT_PATH = '/org/bluez'
20 TEST_OBJECT_PATH = '/org/bluez/test'
21
22 class AlertAgent(dbus.service.Object):
23         def __init__(self, bus, object_path, alert, mainloop):
24                 dbus.service.Object.__init__(self, bus, object_path)
25                 self.alert = alert
26                 self.mainloop = mainloop
27
28         @dbus.service.method(ALERT_AGENT_INTERFACE, in_signature='',
29                                                         out_signature='')
30         def MuteOnce(self):
31                 print('method MuteOnce() was called')
32                 self.alert.NewAlert('ringer', 1, 'not active')
33
34         @dbus.service.method(ALERT_AGENT_INTERFACE, in_signature='s',
35                                                         out_signature='')
36         def SetRinger(self, mode):
37                 print('method SetRinger(%s) was called' % mode)
38                 self.alert.NewAlert('ringer', 1, mode)
39
40         @dbus.service.method(ALERT_AGENT_INTERFACE, in_signature='',
41                                                         out_signature='')
42         def Release(self):
43                 print('method Release() was called')
44                 self.mainloop.quit()
45
46 def print_command_line(options):
47         if not options.verbose:
48                 return False
49
50         print('-w: ' + str(options.wait))
51
52         if options.times:
53                 print('-t: ' + str(options.times))
54
55         if options.register:
56                 print('-r: ' + options.register)
57         else:
58                 print('-r: ' + str(None))
59
60         if options.new_alert:
61                 print('-n:')
62                 for i in options.new_alert:
63                         print('    ' + i[0] + ', ' + i[1] + ', ' + i[2])
64         else:
65                 print('-n: ' + str(None))
66
67         if options.unread_alert:
68                 print('-u:')
69                 for i in options.unread_alert:
70                         print('    ' + i[0] + ', ' + i[1])
71         else:
72                 print('-u: ' + str(None))
73
74         print()
75
76         return True
77
78 def read_count(param):
79         try:
80                 return int(param)
81         except ValueError:
82                 print('<count> must be integer, not \"%s\"' % param)
83                 sys.exit(1)
84
85 def new_alert(alert, params):
86         if not params:
87                 return False
88
89         for param in params:
90                 category = param[0]
91                 count = read_count(param[1])
92                 description = param[2]
93
94                 alert.NewAlert(category, count, description)
95
96 def unread_alert(alert, params):
97         if not params:
98                 return False
99
100         for param in params:
101                 category = param[0]
102                 count = read_count(param[1])
103
104                 alert.UnreadAlert(category, count)
105
106 option_list = [
107         optparse.make_option('-v', None,
108                         action = 'store_true',
109                         default = False,
110                         dest = 'verbose',
111                         help = 'verbose'),
112
113         optparse.make_option('-w', None,
114                         action = 'store_true',
115                         default = False,
116                         dest = 'wait',
117                         help = 'wait for dbus events'),
118
119         optparse.make_option('-t', None,
120                         action = 'store',
121                         default = 1,
122                         type = "int",
123                         dest = 'times',
124                         help = 'repeat UnreadAlert/NewAlert <times> times',
125                         metavar = '<times>'),
126
127         optparse.make_option('-r', None,
128                         action = 'store',
129                         dest = 'register',
130                         type = 'string',
131                         metavar = '<category>',
132                         help = 'register alert'),
133
134         optparse.make_option('-n', None,
135                         action = 'append',
136                         dest = 'new_alert',
137                         type = 'string',
138                         nargs = 3,
139                         metavar = '<category> <count> <description>',
140                         help = 'send new alert'),
141
142         optparse.make_option('-u', None,
143                         action = 'append',
144                         dest = 'unread_alert',
145                         type = 'string',
146                         nargs = 2,
147                         metavar = '<category> <count>',
148                         help = 'send unread alert'),
149 ]
150
151 parser = optparse.OptionParser(option_list=option_list)
152 parser.disable_interspersed_args()
153 (options, args) = parser.parse_args()
154
155 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
156 bus = dbus.SystemBus()
157 mainloop = GObject.MainLoop()
158 alert = dbus.Interface(bus.get_object(BUS_NAME, BLUEZ_OBJECT_PATH),
159                                                                 ALERT_INTERFACE)
160 alert_agent = AlertAgent(bus, TEST_OBJECT_PATH, alert, mainloop)
161
162 print_command_line(options)
163
164 if not (options.register or options.new_alert or options.unread_alert or
165                                                                 options.wait):
166         parser.print_usage()
167         sys.exit(1)
168
169 if options.register:
170         alert.RegisterAlert(options.register, TEST_OBJECT_PATH)
171
172 times = 0
173 while times < options.times:
174         times += 1
175
176         new_alert(alert, options.new_alert)
177         unread_alert(alert, options.unread_alert)
178
179 if not options.wait:
180         sys.exit(0)
181
182 try:
183         mainloop.run()
184 except:
185         pass