Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: DBUS client to talk to ofono |
| 2 | # |
| 3 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU Affero General Public License as |
| 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU Affero General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU Affero General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 20 | from . import log, test |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 21 | |
| 22 | from pydbus import SystemBus, Variant |
| 23 | import time |
| 24 | import pprint |
| 25 | |
| 26 | from gi.repository import GLib |
| 27 | glib_main_loop = GLib.MainLoop() |
| 28 | glib_main_ctx = glib_main_loop.get_context() |
| 29 | bus = SystemBus() |
| 30 | |
Pau Espin Pedrol | 504a664 | 2017-05-04 11:38:23 +0200 | [diff] [blame] | 31 | I_MODEM = 'org.ofono.Modem' |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 32 | I_NETREG = 'org.ofono.NetworkRegistration' |
| 33 | I_SMS = 'org.ofono.MessageManager' |
| 34 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 35 | def poll(): |
| 36 | global glib_main_ctx |
| 37 | while glib_main_ctx.pending(): |
| 38 | glib_main_ctx.iteration() |
| 39 | |
Neels Hofmeyr | 93f5866 | 2017-05-03 16:32:16 +0200 | [diff] [blame] | 40 | def systembus_get(path): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 41 | global bus |
| 42 | return bus.get('org.ofono', path) |
| 43 | |
| 44 | def list_modems(): |
Neels Hofmeyr | 93f5866 | 2017-05-03 16:32:16 +0200 | [diff] [blame] | 45 | root = systembus_get('/') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 46 | return sorted(root.GetModems()) |
| 47 | |
| 48 | |
| 49 | class Modem(log.Origin): |
| 50 | 'convenience for ofono Modem interaction' |
| 51 | msisdn = None |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 52 | sms_received_list = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 53 | |
| 54 | def __init__(self, conf): |
| 55 | self.conf = conf |
| 56 | self.path = conf.get('path') |
| 57 | self.set_name(self.path) |
| 58 | self.set_log_category(log.C_BUS) |
| 59 | self._dbus_obj = None |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 60 | self._interfaces = set() |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 61 | self.sms_received_list = [] |
Pau Espin Pedrol | 107f275 | 2017-05-04 11:37:16 +0200 | [diff] [blame] | 62 | # init interfaces and connect to signals: |
| 63 | self.dbus_obj() |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 64 | test.poll() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 65 | |
| 66 | def set_msisdn(self, msisdn): |
| 67 | self.msisdn = msisdn |
| 68 | |
| 69 | def imsi(self): |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 70 | imsi = self.conf.get('imsi') |
| 71 | if not imsi: |
| 72 | with self: |
| 73 | raise RuntimeError('No IMSI') |
| 74 | return imsi |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 75 | |
| 76 | def ki(self): |
| 77 | return self.conf.get('ki') |
| 78 | |
Pau Espin Pedrol | 504a664 | 2017-05-04 11:38:23 +0200 | [diff] [blame] | 79 | def _dbus_set_bool(self, name, bool_val, iface=I_MODEM): |
Neels Hofmeyr | 9dbcb82 | 2017-05-02 14:57:57 +0200 | [diff] [blame] | 80 | # to make sure any pending signals are received before we send out more DBus requests |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 81 | test.poll() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 82 | |
Neels Hofmeyr | 9dbcb82 | 2017-05-02 14:57:57 +0200 | [diff] [blame] | 83 | val = bool(bool_val) |
| 84 | self.log('Setting', name, val) |
Pau Espin Pedrol | 504a664 | 2017-05-04 11:38:23 +0200 | [diff] [blame] | 85 | self.dbus_obj()[iface].SetProperty(name, Variant('b', val)) |
Neels Hofmeyr | 9dbcb82 | 2017-05-02 14:57:57 +0200 | [diff] [blame] | 86 | |
Neels Hofmeyr | 27d459c | 2017-05-02 16:30:18 +0200 | [diff] [blame] | 87 | test.wait(self.property_is, name, bool_val) |
| 88 | |
| 89 | def property_is(self, name, val): |
| 90 | is_val = self.properties().get(name) |
| 91 | self.dbg(name, '==', is_val) |
| 92 | return is_val is not None and is_val == val |
Neels Hofmeyr | 9dbcb82 | 2017-05-02 14:57:57 +0200 | [diff] [blame] | 93 | |
| 94 | def set_powered(self, on=True): |
| 95 | self._dbus_set_bool('Powered', on) |
| 96 | |
Pau Espin Pedrol | b995576 | 2017-05-02 09:39:27 +0200 | [diff] [blame] | 97 | def set_online(self, on=True): |
Neels Hofmeyr | 9dbcb82 | 2017-05-02 14:57:57 +0200 | [diff] [blame] | 98 | self._dbus_set_bool('Online', on) |
Pau Espin Pedrol | b995576 | 2017-05-02 09:39:27 +0200 | [diff] [blame] | 99 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 100 | def dbus_obj(self): |
| 101 | if self._dbus_obj is not None: |
| 102 | return self._dbus_obj |
Neels Hofmeyr | 93f5866 | 2017-05-03 16:32:16 +0200 | [diff] [blame] | 103 | self._dbus_obj = systembus_get(self.path) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 104 | self._dbus_obj.PropertyChanged.connect(self._on_property_change) |
| 105 | self._on_interfaces_change(self.properties().get('Interfaces')) |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 106 | return self._dbus_obj |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 107 | |
Pau Espin Pedrol | 504a664 | 2017-05-04 11:38:23 +0200 | [diff] [blame] | 108 | def properties(self, iface=I_MODEM): |
| 109 | return self.dbus_obj()[iface].GetProperties() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 110 | |
| 111 | def _on_property_change(self, name, value): |
| 112 | if name == 'Interfaces': |
| 113 | self._on_interfaces_change(value) |
| 114 | |
| 115 | def _on_interfaces_change(self, interfaces_now): |
| 116 | now = set(interfaces_now) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 117 | additions = now - self._interfaces |
| 118 | removals = self._interfaces - now |
| 119 | self._interfaces = now |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 120 | for iface in removals: |
| 121 | with log.Origin('modem.disable(%s)' % iface): |
Neels Hofmeyr | cf1e20e | 2017-05-05 18:34:02 +0200 | [diff] [blame] | 122 | self._on_interface_disabled(iface) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 123 | for iface in additions: |
| 124 | with log.Origin('modem.enable(%s)' % iface): |
Neels Hofmeyr | cf1e20e | 2017-05-05 18:34:02 +0200 | [diff] [blame] | 125 | self._on_interface_enabled(iface) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 126 | |
| 127 | def _on_interface_enabled(self, interface_name): |
| 128 | self.dbg('Interface enabled:', interface_name) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 129 | if interface_name == I_SMS: |
Neels Hofmeyr | 943f8a2 | 2017-05-03 17:37:53 +0200 | [diff] [blame] | 130 | retries = 3 |
Neels Hofmeyr | 5fe8881 | 2017-05-03 17:20:17 +0200 | [diff] [blame] | 131 | while True: |
Neels Hofmeyr | 5fe8881 | 2017-05-03 17:20:17 +0200 | [diff] [blame] | 132 | try: |
| 133 | self.dbus_obj()[I_SMS].IncomingMessage.connect(self._on_incoming_message) |
| 134 | break |
| 135 | except: |
| 136 | self.dbg('Interface not yet available:', I_SMS) |
| 137 | retries -= 1 |
| 138 | time.sleep(1) |
| 139 | if retries <= 0: |
| 140 | self.err('Interface enabled by signal, but not available:', I_SMS) |
| 141 | raise |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 142 | |
| 143 | def _on_interface_disabled(self, interface_name): |
| 144 | self.dbg('Interface disabled:', interface_name) |
| 145 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 146 | def has_interface(self, name): |
| 147 | return name in self._interfaces |
| 148 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 149 | def connect(self, nitb): |
| 150 | 'set the modem up to connect to MCC+MNC from NITB config' |
| 151 | self.log('connect to', nitb) |
Pau Espin Pedrol | 107f275 | 2017-05-04 11:37:16 +0200 | [diff] [blame] | 152 | prepowered = self.properties().get('Powered') |
| 153 | if prepowered is not None and prepowered: |
| 154 | self.set_online(False) |
| 155 | self.set_powered(False) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 156 | self.set_powered() |
Pau Espin Pedrol | b995576 | 2017-05-02 09:39:27 +0200 | [diff] [blame] | 157 | self.set_online() |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 158 | if not self.has_interface(I_NETREG): |
| 159 | self.log('No %r interface, hoping that the modem connects by itself' % I_NETREG) |
| 160 | else: |
| 161 | self.log('Use of %r interface not implemented yet, hoping that the modem connects by itself' % I_NETREG) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 162 | |
Neels Hofmeyr | 8d8b03e | 2017-05-06 18:19:46 +0200 | [diff] [blame] | 163 | def sms_send(self, to_msisdn, *tokens): |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 164 | if hasattr(to_msisdn, 'msisdn'): |
| 165 | to_msisdn = to_msisdn.msisdn |
Neels Hofmeyr | 8d8b03e | 2017-05-06 18:19:46 +0200 | [diff] [blame] | 166 | sms = Sms(self.msisdn, to_msisdn, 'from ' + self.name(), *tokens) |
Neels Hofmeyr | 1ea59ea | 2017-05-02 14:41:54 +0200 | [diff] [blame] | 167 | self.log('sending sms to MSISDN', to_msisdn, sms=sms) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 168 | if not self.has_interface(I_SMS): |
| 169 | raise RuntimeError('Modem cannot send SMS, interface not active: %r' % I_SMS) |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 170 | mm = self.dbus_obj()[I_SMS] |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 171 | mm.SendMessage(to_msisdn, str(sms)) |
| 172 | return sms |
| 173 | |
| 174 | def _on_incoming_message(self, message, info): |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 175 | self.log('Incoming SMS:', repr(message), info=info) |
| 176 | self.sms_received_list.append((message, info)) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 177 | |
Neels Hofmeyr | 863cb56 | 2017-05-02 16:27:59 +0200 | [diff] [blame] | 178 | def sms_was_received(self, sms): |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 179 | for msg, info in self.sms_received_list: |
| 180 | if sms.matches(msg): |
| 181 | self.log('SMS received as expected:', msg=msg, info=info) |
| 182 | return True |
| 183 | return False |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 184 | |
| 185 | class Sms: |
| 186 | _last_sms_idx = 0 |
| 187 | msg = None |
| 188 | |
Neels Hofmeyr | 8d8b03e | 2017-05-06 18:19:46 +0200 | [diff] [blame] | 189 | def __init__(self, from_msisdn=None, to_msisdn=None, *tokens): |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 190 | Sms._last_sms_idx += 1 |
| 191 | msgs = ['message nr. %d' % Sms._last_sms_idx] |
Neels Hofmeyr | 8d8b03e | 2017-05-06 18:19:46 +0200 | [diff] [blame] | 192 | msgs.extend(tokens) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 193 | if from_msisdn: |
Neels Hofmeyr | 8d8b03e | 2017-05-06 18:19:46 +0200 | [diff] [blame] | 194 | msgs.append('from %s' % from_msisdn) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 195 | if to_msisdn: |
Neels Hofmeyr | 8d8b03e | 2017-05-06 18:19:46 +0200 | [diff] [blame] | 196 | msgs.append('to %s' % to_msisdn) |
| 197 | self.msg = ', '.join(msgs) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 198 | |
| 199 | def __str__(self): |
| 200 | return self.msg |
| 201 | |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 202 | def __repr__(self): |
| 203 | return repr(self.msg) |
| 204 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 205 | def __eq__(self, other): |
| 206 | if isinstance(other, Sms): |
| 207 | return self.msg == other.msg |
| 208 | return inself.msg == other |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 209 | |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 210 | def matches(self, msg): |
| 211 | return self.msg == msg |
| 212 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 213 | # vim: expandtab tabstop=4 shiftwidth=4 |