test: Make sure test-stk-menu works with python3
[platform/upstream/ofono.git] / test / test-stk-menu
1 #!/usr/bin/python3
2
3 from gi.repository import GLib
4
5 import sys
6 import dbus
7 import dbus.service
8 import dbus.mainloop.glib
9 import signal
10
11 def handler(signum, frame):
12         raise Exception("\nSingle tone is finished!")
13
14 class GoBack(dbus.DBusException):
15         _dbus_error_name = "org.ofono.Error.GoBack"
16
17 class EndSession(dbus.DBusException):
18         _dbus_error_name = "org.ofono.Error.EndSession"
19
20 class Busy(dbus.DBusException):
21         _dbus_error_name = "org.ofono.Error.Busy"
22
23 class StkAgent(dbus.service.Object):
24         exit_on_release = True
25         timeout_id = 0
26         timeout_reply_handler = None
27
28         def set_exit_on_release(self, exit_on_release):
29                 self.exit_on_release = exit_on_release
30
31         def timeout_callback(self):
32                 self.timeout_id = 0
33                 self.timeout_reply_handler()
34                 return False
35
36         def call_added(self, path, properties):
37                 print("call added %s" % (path))
38                 if (self.timeout_id > 0):
39                         GLib.source_remove(self.timeout_id)
40                         self.timeout_callback()
41
42         @dbus.service.method("org.ofono.SimToolkitAgent",
43                                         in_signature="", out_signature="")
44         def Release(self):
45                 print("Release")
46                 if self.exit_on_release:
47                         mainloop.quit()
48
49         @dbus.service.method("org.ofono.SimToolkitAgent",
50                                 in_signature="sya(sy)n", out_signature="y")
51         def RequestSelection(self, title, icon, items, default):
52                 print("Title: (%s)" % (title))
53                 print("Icon: (%d)" % (int(icon)))
54                 index = 0
55                 for item in items:
56                         print("%d. %s (icon: %d)" %
57                                 (index, item[0], int(item[1])))
58                         index += 1
59
60                 print("\nDefault: %d" % (default))
61                 select = input("Enter Selection (t, b):")
62
63                 if select == 'b':
64                         raise GoBack("User wishes to go back")
65                 elif select == 't':
66                         raise EndSession("User wishes to terminate session")
67                 else:
68                         return int(select)
69
70         @dbus.service.method("org.ofono.SimToolkitAgent",
71                                         in_signature="syb", out_signature="",
72                                         async_callbacks=("reply_func",
73                                                                 "error_func"))
74         def DisplayText(self, title, icon, urgent, reply_func, error_func):
75                 print("DisplayText (%s)" % (title))
76                 print("Icon: (%d)" % (int(icon)))
77                 print("Urgent: (%d)" % (urgent))
78                 key = input("Press return to clear ('t' terminates, "
79                                                 "'b' goes back, 'n' busy, "
80                                                 "'w' return and wait):")
81
82                 if key == 'w':
83                         seconds = 60
84                 else:
85                         seconds = 0
86
87                 if key == 'b':
88                         raise GoBack("User wishes to go back")
89                 elif key == 't':
90                         raise EndSession("User wishes to terminate session")
91                 elif key == 'n':
92                         raise Busy("User wishes to simulate busy screen")
93
94                 if (seconds > 0):
95                         print("Waiting for %d seconds" % (seconds))
96
97                 self.timeout_reply_handler = reply_func
98                 self.timeout_id = GLib.timeout_add_seconds(seconds,
99                                                         self.timeout_callback)
100
101         @dbus.service.method("org.ofono.SimToolkitAgent",
102                                 in_signature="sysyyb", out_signature="s")
103         def RequestInput(self, title, icon, default, min_chars, max_chars,
104                                 hide_typing):
105                 print("Title: (%s)" % (title))
106                 print("Icon: (%d)" % (int(icon)))
107                 print("Default: (%s)" % (default))
108                 print("Hide typing: (%s)" % (hide_typing))
109                 print("Enter characters, min: %d, max: %d:" % (min_chars,
110                                                                 max_chars))
111                 userin = input("")
112
113                 return userin
114
115         @dbus.service.method("org.ofono.SimToolkitAgent",
116                                 in_signature="sysyyb", out_signature="s")
117         def RequestDigits(self, title, icon, default, min_chars, max_chars,
118                                 hide_typing):
119                 print("Title: (%s)" % (title))
120                 print("Icon: (%d)" % (int(icon)))
121                 print("Default: (%s)" % (default))
122                 print("Hide typing: (%s)" % (hide_typing))
123                 print("Enter digits, min: %d, max: %d:" % (min_chars,
124                                                                 max_chars))
125                 userin = input("'t' terminates, 'b' goes back:")
126
127                 if userin == 'b':
128                         raise GoBack("User wishes to go back")
129                 elif userin == 't':
130                         raise EndSession("User wishes to terminate session")
131                 else:
132                         return userin
133
134         @dbus.service.method("org.ofono.SimToolkitAgent",
135                                 in_signature="sy", out_signature="s")
136         def RequestKey(self, title, icon):
137                 print("Title: (%s)" % (title))
138                 print("Icon: (%d)" % (int(icon)))
139                 key = input("Enter Key (t, b):")
140
141                 if key == 'b':
142                         raise GoBack("User wishes to go back")
143                 elif key == 't':
144                         raise EndSession("User wishes to terminate session")
145                 else:
146                         return key
147
148         @dbus.service.method("org.ofono.SimToolkitAgent",
149                                 in_signature="sy", out_signature="s")
150         def RequestDigit(self, title, icon):
151                 print("Title: (%s)" % (title))
152                 print("Icon: (%d)" % (int(icon)))
153                 key = input("Enter Digit (t, b):")
154
155                 if key == 'b':
156                         raise GoBack("User wishes to go back")
157                 elif key == 't':
158                         raise EndSession("User wishes to terminate session")
159                 else:
160                         return key
161
162         @dbus.service.method("org.ofono.SimToolkitAgent",
163                                 in_signature="sy", out_signature="s")
164         def RequestQuickDigit(self, title, icon):
165                 print("Title: (%s)" % (title))
166                 print("Icon: (%d)" % (int(icon)))
167                 key = input("Quick digit (0-9, *, #, t, b):")
168
169                 if key == 'b':
170                         raise GoBack("User wishes to go back")
171                 elif key == 't':
172                         raise EndSession("User wishes to terminate session")
173                 else:
174                         return key
175
176         @dbus.service.method("org.ofono.SimToolkitAgent",
177                                 in_signature="sy", out_signature="b")
178         def RequestConfirmation(self, title, icon):
179                 print("Title: (%s)" % (title))
180                 print("Icon: (%d)" % (int(icon)))
181                 key = input("Enter Confirmation (t, b, y, n):")
182
183                 if key == 'b':
184                         raise GoBack("User wishes to go back")
185                 elif key == 't':
186                         raise EndSession("User wishes to terminate session")
187                 elif key == 'y':
188                         return True
189                 else:
190                         return False
191
192         @dbus.service.method("org.ofono.SimToolkitAgent",
193                                 in_signature="sy", out_signature="b")
194         def ConfirmCallSetup(self, info, icon):
195                 print("Information: (%s)" % (info))
196                 print("Icon: (%d)" % (int(icon)))
197                 key = input("Enter Confirmation (t, y, n):")
198
199                 if key == 't':
200                         raise EndSession("User wishes to terminate session")
201                 elif key == 'y':
202                         return True
203                 else:
204                         return False
205
206         @dbus.service.method("org.ofono.SimToolkitAgent",
207                                 in_signature="sys", out_signature="b")
208         def ConfirmLaunchBrowser(self, info, icon, url):
209                 print("Information: (%s)" % (info))
210                 print("Icon: (%d)" % (int(icon)))
211                 print("URL (%s)" % (url))
212                 key = input("Enter Confirmation (y, n):")
213
214                 if key == 'y':
215                         return True
216                 else:
217                         return False
218
219         @dbus.service.method("org.ofono.SimToolkitAgent",
220                                         in_signature="", out_signature="")
221         def Cancel(self):
222                 print("Cancel")
223
224         @dbus.service.method("org.ofono.SimToolkitAgent",
225                                         in_signature="ssy", out_signature="")
226         def PlayTone(self, tone, text, icon):
227                 print("PlayTone: %s" % (tone))
228                 print("Text: %s" % (text))
229                 print("Icon: %d" % (int(icon)))
230
231                 signal.signal(signal.SIGALRM, handler)
232                 signal.alarm(5)
233
234                 try:
235                         key = input("Press return to end before end of"
236                                                          " single tone (t):")
237                         signal.alarm(0)
238
239                         if key == 't':
240                                 raise EndSession("User wishes to terminate"
241                                                                  " session")
242                 except Exception as exc:
243                         print(exc)
244
245         @dbus.service.method("org.ofono.SimToolkitAgent",
246                                         in_signature="ssy", out_signature="",
247                                         async_callbacks=("reply_func",
248                                                                 "error_func"))
249         def LoopTone(self, tone, text, icon, reply_func, error_func):
250                 print("LoopTone: %s" % (tone))
251                 print("Text: %s" % (text))
252                 print("Icon: %d" % (int(icon)))
253                 key = input("Press return to end before timeout "
254                                 "('t' terminates, 'w' return and wait):")
255
256                 if key == 'w':
257                         seconds = 60
258                 else:
259                         seconds = 0
260
261                 if key == 't':
262                         raise EndSession("User wishes to terminate session")
263
264                 if (seconds > 0):
265                         print("Waiting for %d seconds" % (seconds))
266
267                 self.timeout_reply_handler = reply_func
268                 self.timeout_id = GLib.timeout_add_seconds(seconds,
269                                                         self.timeout_callback)
270
271         @dbus.service.method("org.ofono.SimToolkitAgent",
272                                         in_signature="sy", out_signature="")
273         def DisplayActionInformation(self, text, icon):
274                 print("Text: %s" % (text))
275                 print("Icon: %d" % (int(icon)))
276
277         @dbus.service.method("org.ofono.SimToolkitAgent",
278                                         in_signature="sy", out_signature="")
279         def DisplayAction(self, text, icon):
280                 print("Text: (%s)" % (text))
281                 print("Icon: (%d)" % (int(icon)))
282                 key = input("Press 't' to terminate the session ")
283
284                 if key == 't':
285                         raise EndSession("User wishes to terminate session")
286
287         @dbus.service.method("org.ofono.SimToolkitAgent",
288                                         in_signature="sy", out_signature="b")
289         def ConfirmOpenChannel(self, info, icon):
290                 print("Open channel confirmation: (%s)" % (info))
291                 print("Icon: (%d)" % (int(icon)))
292                 key = input("Enter Confirmation (t, y, n):")
293
294                 if key == 't':
295                         raise EndSession("User wishes to terminate session")
296                 elif key == 'y':
297                         return True
298                 else:
299                         return False
300
301 _dbus2py = {
302         dbus.String : str,
303         dbus.UInt32 : int,
304         dbus.Int32 : int,
305         dbus.Int16 : int,
306         dbus.UInt16 : int,
307         dbus.UInt64 : int,
308         dbus.Int64 : int,
309         dbus.Byte : int,
310         dbus.Boolean : bool,
311         dbus.ByteArray : str,
312         dbus.ObjectPath : str
313     }
314
315 def dbus2py(d):
316         t = type(d)
317         if t in _dbus2py:
318                 return _dbus2py[t](d)
319         if t is dbus.Dictionary:
320                 return dict([(dbus2py(k), dbus2py(v)) for k, v in d.items()])
321         if t is dbus.Array and d.signature == "y":
322                 return "".join([chr(b) for b in d])
323         if t is dbus.Array or t is list:
324                 return [dbus2py(v) for v in d]
325         if t is dbus.Struct or t is tuple:
326                 return tuple([dbus2py(v) for v in d])
327         return d
328
329 def pretty(d):
330         d = dbus2py(d)
331         t = type(d)
332
333         if t in (dict, tuple, list) and len(d) > 0:
334                 if t is dict:
335                         d = ", ".join(["%s = %s" % (k, pretty(v))
336                                         for k, v in d.items()])
337                         return "{ %s }" % d
338
339                 d = " ".join([pretty(e) for e in d])
340
341                 if t is tuple:
342                         return "( %s )" % d
343
344         if t is str:
345                 return "%s" % d
346
347         return str(d)
348
349 def property_changed(name, value):
350         print("SimToolKit property: %s changed to '%s'" % (name, pretty(value)))
351
352 if __name__ == '__main__':
353         if len(sys.argv) == 2:
354                 mode = sys.argv[1]
355         else:
356                 mode = 'menu'
357
358         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
359
360         bus = dbus.SystemBus()
361         manager = dbus.Interface(bus.get_object("org.ofono", "/"),
362                                                         "org.ofono.Manager")
363
364         modems = manager.GetModems()
365
366         for path, properties in modems:
367                 if "org.ofono.SimToolkit" in properties["Interfaces"]:
368                         stk = dbus.Interface(bus.get_object('org.ofono', path),
369                                                         'org.ofono.SimToolkit')
370                 if "org.ofono.VoiceCallManager" in properties["Interfaces"]:
371                         vcm = dbus.Interface(bus.get_object('org.ofono', path),
372                                                 'org.ofono.VoiceCallManager')
373
374         stk.connect_to_signal("PropertyChanged", property_changed)
375
376         properties = stk.GetProperties()
377
378         if mode == 'menu':
379                 if "MainMenuTitle" in properties:
380                         print("Main Menu:")
381                         print("%s" % (properties["MainMenuTitle"]))
382                         print("\n")
383
384                 if "MainMenu" in properties:
385                         print("Items:")
386                         index = 0
387                         for item in properties["MainMenu"]:
388                                 print("%d. %s" % (index, item[0]))
389                                 index += 1
390
391                 path = "/test/agent"
392                 agent = StkAgent(bus, path)
393
394                 try:
395                         vcm.connect_to_signal("CallAdded", agent.call_added)
396                 except:
397                         pass
398
399                 select = int(input("Enter Selection: "))
400                 stk.SelectItem(select, path)
401         elif mode == 'agent':
402                 path = "/test/agent"
403                 agent = StkAgent(bus, path)
404
405                 try:
406                         vcm.connect_to_signal("CallAdded", agent.call_added)
407                 except:
408                         pass
409
410                 stk.RegisterAgent(path)
411
412                 print("Default Agent registered - Waiting for STK command...")
413         else:
414                 print("%s [menu|agent]" % (sys.argv[0]))
415                 exit(0)
416
417         mainloop = GLib.MainLoop()
418         mainloop.run()