Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | ''' |
| 4 | Power on and off some modem on ofono, while running the glib main loop in a |
| 5 | thread and receiving modem state changes by dbus signals. |
| 6 | ''' |
| 7 | |
| 8 | from pydbus import SystemBus, Variant |
| 9 | import time |
| 10 | import pprint |
| 11 | |
| 12 | from gi.repository import GLib |
| 13 | glib_main_loop = GLib.MainLoop() |
| 14 | glib_main_ctx = glib_main_loop.get_context() |
| 15 | |
| 16 | def propchanged(*args, **kwargs): |
| 17 | print('-> PROP CHANGED: %r %r' % (args, kwargs)) |
| 18 | |
| 19 | |
| 20 | def pump(): |
| 21 | global glib_main_ctx |
| 22 | print('pump?') |
| 23 | while glib_main_ctx.pending(): |
| 24 | print('* pump') |
| 25 | glib_main_ctx.iteration() |
| 26 | |
| 27 | def wait(condition): |
| 28 | pump() |
| 29 | while not condition(): |
| 30 | time.sleep(.1) |
| 31 | pump() |
| 32 | |
| 33 | bus = SystemBus() |
| 34 | |
| 35 | print('\n- list modems') |
| 36 | root = bus.get("org.ofono", '/') |
| 37 | print(root.Introspect()) |
| 38 | modems = sorted(root.GetModems()) |
| 39 | pprint.pprint(modems) |
| 40 | pump() |
| 41 | |
| 42 | first_modem_path = modems[0][0] |
| 43 | print('\n- first modem %r' % first_modem_path) |
| 44 | modem = bus.get("org.ofono", first_modem_path) |
| 45 | modem.PropertyChanged.connect(propchanged) |
| 46 | |
| 47 | print(modem.Introspect()) |
| 48 | print(modem.GetProperties()) |
| 49 | |
| 50 | print('\n- set Powered = True') |
| 51 | modem.SetProperty('Powered', Variant('b', True)) |
| 52 | print('call returned') |
| 53 | print('- pump dbus events') |
| 54 | pump() |
| 55 | pump() |
| 56 | print('sleep 1') |
| 57 | time.sleep(1) |
| 58 | pump() |
| 59 | |
| 60 | |
| 61 | print('- modem properties:') |
| 62 | print(modem.GetProperties()) |
| 63 | |
| 64 | |
| 65 | print('\n- set Powered = False') |
| 66 | modem.SetProperty('Powered', Variant('b', False)) |
| 67 | print('call returned') |
| 68 | |
| 69 | print(modem.GetProperties()) |
| 70 | |
| 71 | # vim: tabstop=4 shiftwidth=4 expandtab |