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