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 |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 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 |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 20 | from . import log, util, sms, process |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 21 | from .event_loop import MainLoop |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 22 | |
| 23 | from pydbus import SystemBus, Variant |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 24 | import os |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 25 | |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 26 | # Required for Gio.Cancellable. |
| 27 | # See https://lazka.github.io/pgi-docs/Gio-2.0/classes/Cancellable.html#Gio.Cancellable |
| 28 | from gi.module import get_introspection_module |
| 29 | Gio = get_introspection_module('Gio') |
| 30 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 31 | from gi.repository import GLib |
Holger Hans Peter Freyther | ae0dae8 | 2019-02-20 08:57:46 +0000 | [diff] [blame] | 32 | bus = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 33 | |
Pau Espin Pedrol | 504a664 | 2017-05-04 11:38:23 +0200 | [diff] [blame] | 34 | I_MODEM = 'org.ofono.Modem' |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 35 | I_NETREG = 'org.ofono.NetworkRegistration' |
| 36 | I_SMS = 'org.ofono.MessageManager' |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 37 | I_CONNMGR = 'org.ofono.ConnectionManager' |
Pau Espin Pedrol | d71edd1 | 2017-10-06 13:53:54 +0200 | [diff] [blame] | 38 | I_CALLMGR = 'org.ofono.VoiceCallManager' |
| 39 | I_CALL = 'org.ofono.VoiceCall' |
Pau Espin Pedrol | 03983aa | 2017-06-12 15:31:27 +0200 | [diff] [blame] | 40 | I_SS = 'org.ofono.SupplementaryServices' |
Pau Espin Pedrol | bfd0b23 | 2018-03-13 18:32:57 +0100 | [diff] [blame] | 41 | I_SIMMGR = 'org.ofono.SimManager' |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 42 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 43 | # See https://github.com/intgr/ofono/blob/master/doc/network-api.txt#L78 |
| 44 | NETREG_ST_REGISTERED = 'registered' |
| 45 | NETREG_ST_ROAMING = 'roaming' |
| 46 | |
| 47 | NETREG_MAX_REGISTER_ATTEMPTS = 3 |
| 48 | |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 49 | class DeferredDBus: |
Neels Hofmeyr | 035cda8 | 2017-05-05 17:52:45 +0200 | [diff] [blame] | 50 | |
| 51 | def __init__(self, dbus_iface, handler): |
| 52 | self.handler = handler |
Neels Hofmeyr | 47de6b0 | 2017-05-10 13:24:05 +0200 | [diff] [blame] | 53 | self.subscription_id = dbus_iface.connect(self.receive_signal) |
Neels Hofmeyr | 035cda8 | 2017-05-05 17:52:45 +0200 | [diff] [blame] | 54 | |
| 55 | def receive_signal(self, *args, **kwargs): |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 56 | MainLoop.defer(self.handler, *args, **kwargs) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 57 | |
Neels Hofmeyr | 035cda8 | 2017-05-05 17:52:45 +0200 | [diff] [blame] | 58 | def dbus_connect(dbus_iface, handler): |
| 59 | '''This function shall be used instead of directly connecting DBus signals. |
| 60 | It ensures that we don't nest a glib main loop within another, and also |
| 61 | that we receive exceptions raised within the signal handlers. This makes it |
| 62 | so that a signal handler is invoked only after the DBus polling is through |
| 63 | by enlisting signals that should be handled in the |
| 64 | DeferredHandling.defer_queue.''' |
Pau Espin Pedrol | bf176e4 | 2018-03-26 19:13:32 +0200 | [diff] [blame] | 65 | return DeferredDBus(dbus_iface, handler).subscription_id |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 66 | |
Neels Hofmeyr | 93f5866 | 2017-05-03 16:32:16 +0200 | [diff] [blame] | 67 | def systembus_get(path): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 68 | global bus |
Holger Hans Peter Freyther | ae0dae8 | 2019-02-20 08:57:46 +0000 | [diff] [blame] | 69 | if not bus: |
| 70 | bus = SystemBus() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 71 | return bus.get('org.ofono', path) |
| 72 | |
| 73 | def list_modems(): |
Neels Hofmeyr | 93f5866 | 2017-05-03 16:32:16 +0200 | [diff] [blame] | 74 | root = systembus_get('/') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 75 | return sorted(root.GetModems()) |
| 76 | |
Pau Espin Pedrol | e25cf04 | 2018-02-23 17:00:09 +0100 | [diff] [blame] | 77 | def get_dbuspath_from_syspath(syspath): |
| 78 | modems = list_modems() |
| 79 | for dbuspath, props in modems: |
| 80 | if props.get('SystemPath', '') == syspath: |
| 81 | return dbuspath |
| 82 | raise ValueError('could not find %s in modem list: %s' % (syspath, modems)) |
| 83 | |
| 84 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 85 | def _async_result_handler(obj, result, user_data): |
| 86 | '''Generic callback dispatcher called from glib loop when an async method |
| 87 | call has returned. This callback is set up by method dbus_async_call.''' |
| 88 | (result_callback, error_callback, real_user_data) = user_data |
| 89 | try: |
| 90 | ret = obj.call_finish(result) |
| 91 | except Exception as e: |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 92 | if isinstance(e, GLib.Error) and e.code == Gio.IOErrorEnum.CANCELLED: |
| 93 | log.dbg('DBus method cancelled') |
| 94 | return |
| 95 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 96 | if error_callback: |
| 97 | error_callback(obj, e, real_user_data) |
| 98 | else: |
| 99 | result_callback(obj, e, real_user_data) |
| 100 | return |
| 101 | |
| 102 | ret = ret.unpack() |
| 103 | # to be compatible with standard Python behaviour, unbox |
| 104 | # single-element tuples and return None for empty result tuples |
| 105 | if len(ret) == 1: |
| 106 | ret = ret[0] |
| 107 | elif len(ret) == 0: |
| 108 | ret = None |
| 109 | result_callback(obj, ret, real_user_data) |
| 110 | |
| 111 | def dbus_async_call(instance, proxymethod, *proxymethod_args, |
| 112 | result_handler=None, error_handler=None, |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 113 | user_data=None, timeout=30, cancellable=None, |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 114 | **proxymethod_kwargs): |
| 115 | '''pydbus doesn't support asynchronous methods. This method adds support for |
| 116 | it until pydbus implements it''' |
| 117 | |
| 118 | argdiff = len(proxymethod_args) - len(proxymethod._inargs) |
| 119 | if argdiff < 0: |
| 120 | raise TypeError(proxymethod.__qualname__ + " missing {} required positional argument(s)".format(-argdiff)) |
| 121 | elif argdiff > 0: |
| 122 | raise TypeError(proxymethod.__qualname__ + " takes {} positional argument(s) but {} was/were given".format(len(proxymethod._inargs), len(proxymethod_args))) |
| 123 | |
| 124 | timeout = timeout * 1000 |
| 125 | user_data = (result_handler, error_handler, user_data) |
| 126 | |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 127 | # See https://lazka.github.io/pgi-docs/Gio-2.0/classes/DBusProxy.html#Gio.DBusProxy.call |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 128 | ret = instance._bus.con.call( |
| 129 | instance._bus_name, instance._path, |
| 130 | proxymethod._iface_name, proxymethod.__name__, |
| 131 | GLib.Variant(proxymethod._sinargs, proxymethod_args), |
| 132 | GLib.VariantType.new(proxymethod._soutargs), |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 133 | 0, timeout, cancellable, |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 134 | _async_result_handler, user_data) |
| 135 | |
Pau Espin Pedrol | 7423d2e | 2017-08-25 12:58:25 +0200 | [diff] [blame] | 136 | def dbus_call_dismiss_error(log_obj, err_str, method): |
| 137 | try: |
| 138 | method() |
Pau Espin Pedrol | 9b67021 | 2017-11-07 17:50:20 +0100 | [diff] [blame] | 139 | except GLib.Error as e: |
| 140 | if Gio.DBusError.is_remote_error(e) and Gio.DBusError.get_remote_error(e) == err_str: |
Pau Espin Pedrol | 7423d2e | 2017-08-25 12:58:25 +0200 | [diff] [blame] | 141 | log_obj.log('Dismissed Dbus method error: %r' % e) |
| 142 | return |
Pau Espin Pedrol | 9b67021 | 2017-11-07 17:50:20 +0100 | [diff] [blame] | 143 | raise e |
Pau Espin Pedrol | 7423d2e | 2017-08-25 12:58:25 +0200 | [diff] [blame] | 144 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 145 | class ModemDbusInteraction(log.Origin): |
| 146 | '''Work around inconveniences specific to pydbus and ofono. |
| 147 | ofono adds and removes DBus interfaces and notifies about them. |
| 148 | Upon changes we need a fresh pydbus object to benefit from that. |
| 149 | Watching the interfaces change is optional; be sure to call |
| 150 | watch_interfaces() if you'd like to have signals subscribed. |
| 151 | Related: https://github.com/LEW21/pydbus/issues/56 |
| 152 | ''' |
| 153 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 154 | modem_path = None |
| 155 | watch_props_subscription = None |
| 156 | _dbus_obj = None |
| 157 | interfaces = None |
| 158 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 159 | def __init__(self, modem_path): |
| 160 | self.modem_path = modem_path |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 161 | super().__init__(log.C_BUS, self.modem_path) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 162 | self.interfaces = set() |
| 163 | |
| 164 | # A dict listing signal handlers to connect, e.g. |
| 165 | # { I_SMS: ( ('IncomingMessage', self._on_incoming_message), ), } |
| 166 | self.required_signals = {} |
| 167 | |
| 168 | # A dict collecting subscription tokens for connected signal handlers. |
| 169 | # { I_SMS: ( token1, token2, ... ), } |
| 170 | self.connected_signals = util.listdict() |
| 171 | |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 172 | def cleanup(self): |
Pau Espin Pedrol | 58ff38d | 2017-06-23 13:10:38 +0200 | [diff] [blame] | 173 | self.set_powered(False) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 174 | self.unwatch_interfaces() |
| 175 | for interface_name in list(self.connected_signals.keys()): |
| 176 | self.remove_signals(interface_name) |
| 177 | |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 178 | def __del__(self): |
| 179 | self.cleanup() |
| 180 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 181 | def get_new_dbus_obj(self): |
| 182 | return systembus_get(self.modem_path) |
| 183 | |
| 184 | def dbus_obj(self): |
| 185 | if self._dbus_obj is None: |
| 186 | self._dbus_obj = self.get_new_dbus_obj() |
| 187 | return self._dbus_obj |
| 188 | |
| 189 | def interface(self, interface_name): |
| 190 | try: |
| 191 | return self.dbus_obj()[interface_name] |
| 192 | except KeyError: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 193 | raise log.Error('Modem interface is not available:', interface_name) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 194 | |
| 195 | def signal(self, interface_name, signal): |
| 196 | return getattr(self.interface(interface_name), signal) |
| 197 | |
| 198 | def watch_interfaces(self): |
| 199 | self.unwatch_interfaces() |
| 200 | # Note: we are watching the properties on a get_new_dbus_obj() that is |
| 201 | # separate from the one used to interact with interfaces. We need to |
| 202 | # refresh the pydbus object to interact with Interfaces that have newly |
| 203 | # appeared, but exchanging the DBus object to watch Interfaces being |
| 204 | # enabled and disabled is racy: we may skip some removals and |
| 205 | # additions. Hence do not exchange this DBus object. We don't even |
| 206 | # need to store the dbus object used for this, we will not touch it |
| 207 | # again. We only store the signal subscription. |
| 208 | self.watch_props_subscription = dbus_connect(self.get_new_dbus_obj().PropertyChanged, |
| 209 | self.on_property_change) |
| 210 | self.on_interfaces_change(self.properties().get('Interfaces')) |
| 211 | |
| 212 | def unwatch_interfaces(self): |
| 213 | if self.watch_props_subscription is None: |
| 214 | return |
| 215 | self.watch_props_subscription.disconnect() |
| 216 | self.watch_props_subscription = None |
| 217 | |
| 218 | def on_property_change(self, name, value): |
| 219 | if name == 'Interfaces': |
| 220 | self.on_interfaces_change(value) |
Pau Espin Pedrol | 7763121 | 2017-09-05 19:04:06 +0200 | [diff] [blame] | 221 | else: |
| 222 | self.dbg('%r.PropertyChanged() -> %s=%s' % (I_MODEM, name, value)) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 223 | |
| 224 | def on_interfaces_change(self, interfaces_now): |
| 225 | # First some logging. |
| 226 | now = set(interfaces_now) |
| 227 | additions = now - self.interfaces |
| 228 | removals = self.interfaces - now |
| 229 | self.interfaces = now |
| 230 | if not (additions or removals): |
| 231 | # nothing changed. |
| 232 | return |
| 233 | |
| 234 | if additions: |
| 235 | self.dbg('interface enabled:', ', '.join(sorted(additions))) |
| 236 | |
| 237 | if removals: |
| 238 | self.dbg('interface disabled:', ', '.join(sorted(removals))) |
| 239 | |
| 240 | # The dbus object is now stale and needs refreshing before we |
| 241 | # access the next interface function. |
| 242 | self._dbus_obj = None |
| 243 | |
| 244 | # If an interface disappeared, disconnect the signal handlers for it. |
| 245 | # Even though we're going to use a fresh dbus object for new |
| 246 | # subscriptions, we will still keep active subscriptions alive on the |
| 247 | # old dbus object which will linger, associated with the respective |
| 248 | # signal subscription. |
| 249 | for removed in removals: |
| 250 | self.remove_signals(removed) |
| 251 | |
| 252 | # Connect signals for added interfaces. |
| 253 | for interface_name in additions: |
| 254 | self.connect_signals(interface_name) |
| 255 | |
| 256 | def remove_signals(self, interface_name): |
| 257 | got = self.connected_signals.pop(interface_name, []) |
| 258 | |
| 259 | if not got: |
| 260 | return |
| 261 | |
| 262 | self.dbg('Disconnecting', len(got), 'signals for', interface_name) |
| 263 | for subscription in got: |
| 264 | subscription.disconnect() |
| 265 | |
| 266 | def connect_signals(self, interface_name): |
| 267 | # If an interface was added, it must not have existed before. For |
| 268 | # paranoia, make sure we have no handlers for those. |
| 269 | self.remove_signals(interface_name) |
| 270 | |
| 271 | want = self.required_signals.get(interface_name, []) |
| 272 | if not want: |
| 273 | return |
| 274 | |
| 275 | self.dbg('Connecting', len(want), 'signals for', interface_name) |
| 276 | for signal, cb in self.required_signals.get(interface_name, []): |
| 277 | subscription = dbus_connect(self.signal(interface_name, signal), cb) |
| 278 | self.connected_signals.add(interface_name, subscription) |
| 279 | |
| 280 | def has_interface(self, *interface_names): |
| 281 | try: |
| 282 | for interface_name in interface_names: |
| 283 | self.dbus_obj()[interface_name] |
| 284 | result = True |
| 285 | except KeyError: |
| 286 | result = False |
| 287 | self.dbg('has_interface(%s) ==' % (', '.join(interface_names)), result) |
| 288 | return result |
| 289 | |
| 290 | def properties(self, iface=I_MODEM): |
| 291 | return self.dbus_obj()[iface].GetProperties() |
| 292 | |
| 293 | def property_is(self, name, val, iface=I_MODEM): |
| 294 | is_val = self.properties(iface).get(name) |
| 295 | self.dbg(name, '==', is_val) |
| 296 | return is_val is not None and is_val == val |
| 297 | |
| 298 | def set_bool(self, name, bool_val, iface=I_MODEM): |
| 299 | # to make sure any pending signals are received before we send out more DBus requests |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 300 | MainLoop.poll() |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 301 | |
| 302 | val = bool(bool_val) |
| 303 | self.log('Setting', name, val) |
| 304 | self.interface(iface).SetProperty(name, Variant('b', val)) |
| 305 | |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 306 | MainLoop.wait(self, self.property_is, name, bool_val) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 307 | |
| 308 | def set_powered(self, powered=True): |
| 309 | self.set_bool('Powered', powered) |
| 310 | |
| 311 | def set_online(self, online=True): |
| 312 | self.set_bool('Online', online) |
| 313 | |
| 314 | def is_powered(self): |
| 315 | return self.property_is('Powered', True) |
| 316 | |
| 317 | def is_online(self): |
| 318 | return self.property_is('Online', True) |
| 319 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 320 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 321 | |
| 322 | class Modem(log.Origin): |
| 323 | 'convenience for ofono Modem interaction' |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 324 | |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 325 | CTX_PROT_IPv4 = 'ip' |
| 326 | CTX_PROT_IPv6 = 'ipv6' |
| 327 | CTX_PROT_IPv46 = 'dual' |
| 328 | |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 329 | def __init__(self, suite_run, conf): |
| 330 | self.suite_run = suite_run |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 331 | self.conf = conf |
Pau Espin Pedrol | e25cf04 | 2018-02-23 17:00:09 +0100 | [diff] [blame] | 332 | self.syspath = conf.get('path') |
| 333 | self.dbuspath = get_dbuspath_from_syspath(self.syspath) |
| 334 | super().__init__(log.C_TST, self.dbuspath) |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 335 | self.dbg('creating from syspath %s' % self.syspath) |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 336 | self.msisdn = None |
| 337 | self._ki = None |
| 338 | self._imsi = None |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 339 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name().strip('/'))) |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 340 | self.sms_received_list = [] |
Pau Espin Pedrol | e25cf04 | 2018-02-23 17:00:09 +0100 | [diff] [blame] | 341 | self.dbus = ModemDbusInteraction(self.dbuspath) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 342 | self.register_attempts = 0 |
Pau Espin Pedrol | d71edd1 | 2017-10-06 13:53:54 +0200 | [diff] [blame] | 343 | self.call_list = [] |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 344 | # one Cancellable can handle several concurrent methods. |
| 345 | self.cancellable = Gio.Cancellable.new() |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 346 | self.dbus.required_signals = { |
| 347 | I_SMS: ( ('IncomingMessage', self._on_incoming_message), ), |
Pau Espin Pedrol | 56bf31c | 2017-05-31 12:05:20 +0200 | [diff] [blame] | 348 | I_NETREG: ( ('PropertyChanged', self._on_netreg_property_changed), ), |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 349 | I_CONNMGR: ( ('PropertyChanged', self._on_connmgr_property_changed), ), |
Pau Espin Pedrol | d71edd1 | 2017-10-06 13:53:54 +0200 | [diff] [blame] | 350 | I_CALLMGR: ( ('PropertyChanged', self._on_callmgr_property_changed), |
| 351 | ('CallAdded', self._on_callmgr_call_added), |
| 352 | ('CallRemoved', self._on_callmgr_call_removed), ), |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 353 | } |
| 354 | self.dbus.watch_interfaces() |
| 355 | |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 356 | def cleanup(self): |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 357 | self.dbg('cleanup') |
| 358 | if self.cancellable: |
Pau Espin Pedrol | 6680ef2 | 2017-09-11 01:24:05 +0200 | [diff] [blame] | 359 | self.cancel_pending_dbus_methods() |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 360 | self.cancellable = None |
Pau Espin Pedrol | 7aef386 | 2017-11-23 12:15:55 +0100 | [diff] [blame] | 361 | if self.is_powered(): |
| 362 | self.power_off() |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 363 | self.dbus.cleanup() |
| 364 | self.dbus = None |
| 365 | |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 366 | def netns(self): |
| 367 | return os.path.basename(self.syspath.rstrip('/')) |
| 368 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 369 | def properties(self, *args, **kwargs): |
| 370 | '''Return a dict of properties on this modem. For the actual arguments, |
| 371 | see ModemDbusInteraction.properties(), which this function calls. The |
| 372 | returned dict is defined by ofono. An example is: |
| 373 | {'Lockdown': False, |
| 374 | 'Powered': True, |
| 375 | 'Model': 'MC7304', |
| 376 | 'Revision': 'SWI9X15C_05.05.66.00 r29972 CARMD-EV-FRMWR1 2015/10/08 08:36:28', |
| 377 | 'Manufacturer': 'Sierra Wireless, Incorporated', |
| 378 | 'Emergency': False, |
| 379 | 'Interfaces': ['org.ofono.SmartMessaging', |
| 380 | 'org.ofono.PushNotification', |
| 381 | 'org.ofono.MessageManager', |
| 382 | 'org.ofono.NetworkRegistration', |
| 383 | 'org.ofono.ConnectionManager', |
| 384 | 'org.ofono.SupplementaryServices', |
| 385 | 'org.ofono.RadioSettings', |
| 386 | 'org.ofono.AllowedAccessPoints', |
| 387 | 'org.ofono.SimManager', |
| 388 | 'org.ofono.LocationReporting', |
| 389 | 'org.ofono.VoiceCallManager'], |
| 390 | 'Serial': '356853054230919', |
| 391 | 'Features': ['sms', 'net', 'gprs', 'ussd', 'rat', 'sim', 'gps'], |
| 392 | 'Type': 'hardware', |
| 393 | 'Online': True} |
| 394 | ''' |
| 395 | return self.dbus.properties(*args, **kwargs) |
| 396 | |
| 397 | def set_powered(self, powered=True): |
| 398 | return self.dbus.set_powered(powered=powered) |
| 399 | |
| 400 | def set_online(self, online=True): |
| 401 | return self.dbus.set_online(online=online) |
| 402 | |
| 403 | def is_powered(self): |
| 404 | return self.dbus.is_powered() |
| 405 | |
| 406 | def is_online(self): |
| 407 | return self.dbus.is_online() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 408 | |
| 409 | def set_msisdn(self, msisdn): |
| 410 | self.msisdn = msisdn |
| 411 | |
| 412 | def imsi(self): |
Pau Espin Pedrol | bfd0b23 | 2018-03-13 18:32:57 +0100 | [diff] [blame] | 413 | if self._imsi is None: |
| 414 | if 'sim' in self.features(): |
| 415 | if not self.is_powered(): |
| 416 | self.set_powered() |
| 417 | # wait for SimManager iface to appear after we power on |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 418 | MainLoop.wait(self, self.dbus.has_interface, I_SIMMGR, timeout=10) |
Pau Espin Pedrol | bfd0b23 | 2018-03-13 18:32:57 +0100 | [diff] [blame] | 419 | simmgr = self.dbus.interface(I_SIMMGR) |
| 420 | # If properties are requested quickly, it may happen that Sim property is still not there. |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 421 | MainLoop.wait(self, lambda: simmgr.GetProperties().get('SubscriberIdentity', None) is not None, timeout=10) |
Pau Espin Pedrol | bfd0b23 | 2018-03-13 18:32:57 +0100 | [diff] [blame] | 422 | props = simmgr.GetProperties() |
| 423 | self.dbg('got SIM properties', props) |
| 424 | self._imsi = props.get('SubscriberIdentity', None) |
| 425 | else: |
| 426 | self._imsi = self.conf.get('imsi') |
| 427 | if self._imsi is None: |
| 428 | raise log.Error('No IMSI') |
| 429 | return self._imsi |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 430 | |
Pau Espin Pedrol | cd6ad9d | 2017-08-22 19:10:20 +0200 | [diff] [blame] | 431 | def set_ki(self, ki): |
| 432 | self._ki = ki |
| 433 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 434 | def ki(self): |
Pau Espin Pedrol | cd6ad9d | 2017-08-22 19:10:20 +0200 | [diff] [blame] | 435 | if self._ki is not None: |
| 436 | return self._ki |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 437 | return self.conf.get('ki') |
| 438 | |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 439 | def auth_algo(self): |
| 440 | return self.conf.get('auth_algo', None) |
| 441 | |
Pau Espin Pedrol | e0f4986 | 2017-11-23 11:37:34 +0100 | [diff] [blame] | 442 | def features(self): |
| 443 | return self.conf.get('features', []) |
| 444 | |
| 445 | def _required_ifaces(self): |
| 446 | req_ifaces = (I_NETREG,) |
| 447 | req_ifaces += (I_SMS,) if 'sms' in self.features() else () |
| 448 | req_ifaces += (I_SS,) if 'ussd' in self.features() else () |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 449 | req_ifaces += (I_CONNMGR,) if 'gprs' in self.features() else () |
Pau Espin Pedrol | bfd0b23 | 2018-03-13 18:32:57 +0100 | [diff] [blame] | 450 | req_ifaces += (I_SIMMGR,) if 'sim' in self.features() else () |
Pau Espin Pedrol | e0f4986 | 2017-11-23 11:37:34 +0100 | [diff] [blame] | 451 | return req_ifaces |
| 452 | |
Pau Espin Pedrol | 56bf31c | 2017-05-31 12:05:20 +0200 | [diff] [blame] | 453 | def _on_netreg_property_changed(self, name, value): |
| 454 | self.dbg('%r.PropertyChanged() -> %s=%s' % (I_NETREG, name, value)) |
| 455 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 456 | def is_connected(self, mcc_mnc=None): |
| 457 | netreg = self.dbus.interface(I_NETREG) |
| 458 | prop = netreg.GetProperties() |
| 459 | status = prop.get('Status') |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 460 | self.dbg('status:', status) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 461 | if not (status == NETREG_ST_REGISTERED or status == NETREG_ST_ROAMING): |
| 462 | return False |
| 463 | if mcc_mnc is None: # Any network is fine and we are registered. |
| 464 | return True |
| 465 | mcc = prop.get('MobileCountryCode') |
| 466 | mnc = prop.get('MobileNetworkCode') |
| 467 | if (mcc, mnc) == mcc_mnc: |
| 468 | return True |
| 469 | return False |
| 470 | |
| 471 | def schedule_scan_register(self, mcc_mnc): |
| 472 | if self.register_attempts > NETREG_MAX_REGISTER_ATTEMPTS: |
Pau Espin Pedrol | cc5b5a2 | 2017-06-13 16:55:31 +0200 | [diff] [blame] | 473 | raise log.Error('Failed to find Network Operator', mcc_mnc=mcc_mnc, attempts=self.register_attempts) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 474 | self.register_attempts += 1 |
| 475 | netreg = self.dbus.interface(I_NETREG) |
| 476 | self.dbg('Scanning for operators...') |
| 477 | # Scan method can take several seconds, and we don't want to block |
| 478 | # waiting for that. Make it async and try to register when the scan is |
| 479 | # finished. |
| 480 | register_func = self.scan_cb_register_automatic if mcc_mnc is None else self.scan_cb_register |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 481 | result_handler = lambda obj, result, user_data: MainLoop.defer(register_func, result, user_data) |
| 482 | error_handler = lambda obj, e, user_data: MainLoop.defer(self.scan_cb_error_handler, e, mcc_mnc) |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 483 | dbus_async_call(netreg, netreg.Scan, timeout=30, cancellable=self.cancellable, |
| 484 | result_handler=result_handler, error_handler=error_handler, |
| 485 | user_data=mcc_mnc) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 486 | |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 487 | def scan_cb_error_handler(self, e, mcc_mnc): |
| 488 | # It was detected that Scan() method can fail for some modems on some |
| 489 | # specific circumstances. For instance it fails with org.ofono.Error.Failed |
| 490 | # if the modem starts to register internally after we started Scan() and |
| 491 | # the registering succeeds while we are still waiting for Scan() to finsih. |
| 492 | # So far the easiest seems to check if we are now registered and |
| 493 | # otherwise schedule a scan again. |
Pau Espin Pedrol | 910f3a1 | 2017-06-13 16:59:19 +0200 | [diff] [blame] | 494 | self.err('Scan() failed, retrying if needed:', e) |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 495 | if not self.is_connected(mcc_mnc): |
| 496 | self.schedule_scan_register(mcc_mnc) |
Pau Espin Pedrol | 910f3a1 | 2017-06-13 16:59:19 +0200 | [diff] [blame] | 497 | else: |
| 498 | self.log('Already registered with network', mcc_mnc) |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 499 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 500 | def scan_cb_register_automatic(self, scanned_operators, mcc_mnc): |
| 501 | self.dbg('scanned operators: ', scanned_operators); |
| 502 | for op_path, op_prop in scanned_operators: |
| 503 | if op_prop.get('Status') == 'current': |
| 504 | mcc = op_prop.get('MobileCountryCode') |
| 505 | mnc = op_prop.get('MobileNetworkCode') |
| 506 | self.log('Already registered with network', (mcc, mnc)) |
| 507 | return |
| 508 | self.log('Registering with the default network') |
| 509 | netreg = self.dbus.interface(I_NETREG) |
Pau Espin Pedrol | 7423d2e | 2017-08-25 12:58:25 +0200 | [diff] [blame] | 510 | dbus_call_dismiss_error(self, 'org.ofono.Error.InProgress', netreg.Register) |
| 511 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 512 | |
| 513 | def scan_cb_register(self, scanned_operators, mcc_mnc): |
| 514 | self.dbg('scanned operators: ', scanned_operators); |
| 515 | matching_op_path = None |
| 516 | for op_path, op_prop in scanned_operators: |
| 517 | mcc = op_prop.get('MobileCountryCode') |
| 518 | mnc = op_prop.get('MobileNetworkCode') |
| 519 | if (mcc, mnc) == mcc_mnc: |
| 520 | if op_prop.get('Status') == 'current': |
| 521 | self.log('Already registered with network', mcc_mnc) |
| 522 | # We discovered the network and we are already registered |
| 523 | # with it. Avoid calling op.Register() in this case (it |
| 524 | # won't act as a NO-OP, it actually returns an error). |
| 525 | return |
| 526 | matching_op_path = op_path |
| 527 | break |
| 528 | if matching_op_path is None: |
| 529 | self.dbg('Failed to find Network Operator', mcc_mnc=mcc_mnc, attempts=self.register_attempts) |
| 530 | self.schedule_scan_register(mcc_mnc) |
| 531 | return |
| 532 | dbus_op = systembus_get(matching_op_path) |
| 533 | self.log('Registering with operator', matching_op_path, mcc_mnc) |
Pau Espin Pedrol | 9f59b82 | 2017-11-07 17:50:52 +0100 | [diff] [blame] | 534 | try: |
| 535 | dbus_call_dismiss_error(self, 'org.ofono.Error.InProgress', dbus_op.Register) |
| 536 | except GLib.Error as e: |
| 537 | if Gio.DBusError.is_remote_error(e) and Gio.DBusError.get_remote_error(e) == 'org.ofono.Error.NotSupported': |
| 538 | self.log('modem does not support manual registering, attempting automatic registering') |
| 539 | self.scan_cb_register_automatic(scanned_operators, mcc_mnc) |
| 540 | return |
| 541 | raise e |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 542 | |
Pau Espin Pedrol | 6680ef2 | 2017-09-11 01:24:05 +0200 | [diff] [blame] | 543 | def cancel_pending_dbus_methods(self): |
| 544 | self.cancellable.cancel() |
| 545 | # Cancel op is applied as a signal coming from glib mainloop, so we |
| 546 | # need to run it and wait for the callbacks to handle cancellations. |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 547 | MainLoop.poll() |
Pau Espin Pedrol | e685c62 | 2017-10-04 18:30:22 +0200 | [diff] [blame] | 548 | # once it has been triggered, create a new one for next operation: |
| 549 | self.cancellable = Gio.Cancellable.new() |
Pau Espin Pedrol | 6680ef2 | 2017-09-11 01:24:05 +0200 | [diff] [blame] | 550 | |
Pau Espin Pedrol | 7aef386 | 2017-11-23 12:15:55 +0100 | [diff] [blame] | 551 | def power_off(self): |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 552 | if self.dbus.has_interface(I_CONNMGR) and self.is_attached(): |
| 553 | self.detach() |
Pau Espin Pedrol | 7aef386 | 2017-11-23 12:15:55 +0100 | [diff] [blame] | 554 | self.set_online(False) |
| 555 | self.set_powered(False) |
| 556 | req_ifaces = self._required_ifaces() |
| 557 | for iface in req_ifaces: |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 558 | MainLoop.wait(self, lambda: not self.dbus.has_interface(iface), timeout=10) |
Pau Espin Pedrol | 7aef386 | 2017-11-23 12:15:55 +0100 | [diff] [blame] | 559 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 560 | def power_cycle(self): |
| 561 | 'Power the modem and put it online, power cycle it if it was already on' |
Pau Espin Pedrol | e0f4986 | 2017-11-23 11:37:34 +0100 | [diff] [blame] | 562 | req_ifaces = self._required_ifaces() |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 563 | if self.is_powered(): |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 564 | self.dbg('Power cycling') |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 565 | MainLoop.sleep(self, 1.0) # workaround for ofono bug OS#3064 |
Pau Espin Pedrol | 7aef386 | 2017-11-23 12:15:55 +0100 | [diff] [blame] | 566 | self.power_off() |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 567 | else: |
| 568 | self.dbg('Powering on') |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 569 | self.set_powered() |
Pau Espin Pedrol | b995576 | 2017-05-02 09:39:27 +0200 | [diff] [blame] | 570 | self.set_online() |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 571 | MainLoop.wait(self, self.dbus.has_interface, *req_ifaces, timeout=10) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 572 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 573 | def connect(self, mcc_mnc=None): |
| 574 | 'Connect to MCC+MNC' |
| 575 | if (mcc_mnc is not None) and (len(mcc_mnc) != 2 or None in mcc_mnc): |
Pau Espin Pedrol | cc5b5a2 | 2017-06-13 16:55:31 +0200 | [diff] [blame] | 576 | raise log.Error('mcc_mnc value is invalid. It should be None or contain both valid mcc and mnc values:', mcc_mnc=mcc_mnc) |
Pau Espin Pedrol | 6680ef2 | 2017-09-11 01:24:05 +0200 | [diff] [blame] | 577 | # if test called connect() before and async scanning has not finished, we need to get rid of it: |
| 578 | self.cancel_pending_dbus_methods() |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 579 | self.power_cycle() |
| 580 | self.register_attempts = 0 |
| 581 | if self.is_connected(mcc_mnc): |
| 582 | self.log('Already registered with', mcc_mnc if mcc_mnc else 'default network') |
| 583 | else: |
| 584 | self.log('Connect to', mcc_mnc if mcc_mnc else 'default network') |
| 585 | self.schedule_scan_register(mcc_mnc) |
| 586 | |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 587 | def is_attached(self): |
| 588 | connmgr = self.dbus.interface(I_CONNMGR) |
| 589 | prop = connmgr.GetProperties() |
| 590 | attached = prop.get('Attached') |
| 591 | self.dbg('attached:', attached) |
| 592 | return attached |
| 593 | |
| 594 | def attach(self, allow_roaming=False): |
| 595 | self.dbg('attach') |
| 596 | if self.is_attached(): |
| 597 | self.detach() |
| 598 | connmgr = self.dbus.interface(I_CONNMGR) |
| 599 | prop = connmgr.SetProperty('RoamingAllowed', Variant('b', allow_roaming)) |
| 600 | prop = connmgr.SetProperty('Powered', Variant('b', True)) |
| 601 | |
| 602 | def detach(self): |
| 603 | self.dbg('detach') |
| 604 | connmgr = self.dbus.interface(I_CONNMGR) |
| 605 | prop = connmgr.SetProperty('RoamingAllowed', Variant('b', False)) |
| 606 | prop = connmgr.SetProperty('Powered', Variant('b', False)) |
| 607 | connmgr.DeactivateAll() |
| 608 | connmgr.ResetContexts() # Requires Powered=false |
| 609 | |
| 610 | def activate_context(self, apn='internet', user='ogt', pwd='', protocol='ip'): |
Pau Espin Pedrol | b05e36a | 2017-12-15 12:39:36 +0100 | [diff] [blame] | 611 | self.dbg('activate_context', apn=apn, user=user, protocol=protocol) |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 612 | |
| 613 | connmgr = self.dbus.interface(I_CONNMGR) |
| 614 | ctx_path = connmgr.AddContext('internet') |
| 615 | |
| 616 | ctx = systembus_get(ctx_path) |
| 617 | ctx.SetProperty('AccessPointName', Variant('s', apn)) |
| 618 | ctx.SetProperty('Username', Variant('s', user)) |
| 619 | ctx.SetProperty('Password', Variant('s', pwd)) |
| 620 | ctx.SetProperty('Protocol', Variant('s', protocol)) |
| 621 | |
| 622 | # Activate can only be called after we are attached |
| 623 | ctx.SetProperty('Active', Variant('b', True)) |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 624 | MainLoop.wait(self, lambda: ctx.GetProperties()['Active'] == True) |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 625 | self.log('context activated', path=ctx_path, apn=apn, user=user, properties=ctx.GetProperties()) |
| 626 | return ctx_path |
| 627 | |
| 628 | def deactivate_context(self, ctx_id): |
Pau Espin Pedrol | 263dd3b | 2018-02-13 16:53:51 +0100 | [diff] [blame] | 629 | self.dbg('deactivate_context', path=ctx_id) |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 630 | ctx = systembus_get(ctx_id) |
| 631 | ctx.SetProperty('Active', Variant('b', False)) |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 632 | MainLoop.wait(self, lambda: ctx.GetProperties()['Active'] == False) |
Pau Espin Pedrol | cdac297 | 2018-02-16 15:14:32 +0100 | [diff] [blame] | 633 | self.dbg('deactivate_context active=false, removing', path=ctx_id) |
| 634 | connmgr = self.dbus.interface(I_CONNMGR) |
| 635 | connmgr.RemoveContext(ctx_id) |
Pau Espin Pedrol | b05aa3c | 2018-02-16 15:03:50 +0100 | [diff] [blame] | 636 | self.log('context deactivated', path=ctx_id) |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 637 | |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 638 | def run_netns_wait(self, name, popen_args): |
| 639 | proc = process.NetNSProcess(name, self.run_dir.new_dir(name), self.netns(), popen_args, |
| 640 | env={}) |
Pau Espin Pedrol | 79df739 | 2018-11-12 18:15:30 +0100 | [diff] [blame] | 641 | proc.launch_sync() |
Pau Espin Pedrol | fd4c144 | 2018-10-25 17:37:23 +0200 | [diff] [blame] | 642 | |
| 643 | def setup_context_data_plane(self, ctx_id): |
| 644 | self.dbg('setup_context_data', path=ctx_id) |
| 645 | ctx = systembus_get(ctx_id) |
| 646 | ctx_settings = ctx.GetProperties().get('Settings', None) |
| 647 | if not ctx_settings: |
| 648 | raise log.Error('%s no Settings found! No way to get iface!' % ctx_id) |
| 649 | iface = ctx_settings.get('Interface', None) |
| 650 | if not iface: |
| 651 | raise log.Error('%s Settings contains no iface! %r' % (ctx_id, repr(ctx_settings))) |
| 652 | self.run_netns_wait('ifup', ('ip', 'link', 'set', 'dev', iface, 'up')) |
| 653 | self.run_netns_wait('dhcp', ('udhcpc', '-q', '-i', iface)) |
| 654 | |
Neels Hofmeyr | 8c7477f | 2017-05-25 04:33:53 +0200 | [diff] [blame] | 655 | def sms_send(self, to_msisdn_or_modem, *tokens): |
| 656 | if isinstance(to_msisdn_or_modem, Modem): |
| 657 | to_msisdn = to_msisdn_or_modem.msisdn |
| 658 | tokens = list(tokens) |
| 659 | tokens.append('to ' + to_msisdn_or_modem.name()) |
| 660 | else: |
| 661 | to_msisdn = str(to_msisdn_or_modem) |
Pau Espin Pedrol | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 662 | msg = sms.Sms(self.msisdn, to_msisdn, 'from ' + self.name(), *tokens) |
| 663 | self.log('sending sms to MSISDN', to_msisdn, sms=msg) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 664 | mm = self.dbus.interface(I_SMS) |
Pau Espin Pedrol | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 665 | mm.SendMessage(to_msisdn, str(msg)) |
| 666 | return msg |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 667 | |
| 668 | def _on_incoming_message(self, message, info): |
Neels Hofmeyr | 2e41def | 2017-05-06 22:42:57 +0200 | [diff] [blame] | 669 | self.log('Incoming SMS:', repr(message)) |
Neels Hofmeyr | f49c7da | 2017-05-06 22:43:32 +0200 | [diff] [blame] | 670 | self.dbg(info=info) |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 671 | self.sms_received_list.append((message, info)) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 672 | |
Pau Espin Pedrol | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 673 | def sms_was_received(self, sms_obj): |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 674 | for msg, info in self.sms_received_list: |
Pau Espin Pedrol | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 675 | if sms_obj.matches(msg): |
Neels Hofmeyr | 2e41def | 2017-05-06 22:42:57 +0200 | [diff] [blame] | 676 | self.log('SMS received as expected:', repr(msg)) |
Neels Hofmeyr | f49c7da | 2017-05-06 22:43:32 +0200 | [diff] [blame] | 677 | self.dbg(info=info) |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 678 | return True |
| 679 | return False |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 680 | |
Pau Espin Pedrol | d71edd1 | 2017-10-06 13:53:54 +0200 | [diff] [blame] | 681 | def call_id_list(self): |
| 682 | self.dbg('call_id_list: %r' % self.call_list) |
| 683 | return self.call_list |
| 684 | |
| 685 | def call_dial(self, to_msisdn_or_modem): |
| 686 | if isinstance(to_msisdn_or_modem, Modem): |
| 687 | to_msisdn = to_msisdn_or_modem.msisdn |
| 688 | else: |
| 689 | to_msisdn = str(to_msisdn_or_modem) |
| 690 | self.dbg('Dialing:', to_msisdn) |
| 691 | cmgr = self.dbus.interface(I_CALLMGR) |
| 692 | call_obj_path = cmgr.Dial(to_msisdn, 'default') |
| 693 | if call_obj_path not in self.call_list: |
| 694 | self.dbg('Adding %s to call list' % call_obj_path) |
| 695 | self.call_list.append(call_obj_path) |
| 696 | else: |
| 697 | self.dbg('Dial returned already existing call') |
| 698 | return call_obj_path |
| 699 | |
| 700 | def _find_call_msisdn_state(self, msisdn, state): |
| 701 | cmgr = self.dbus.interface(I_CALLMGR) |
| 702 | ret = cmgr.GetCalls() |
| 703 | for obj_path, props in ret: |
| 704 | if props['LineIdentification'] == msisdn and props['State'] == state: |
| 705 | return obj_path |
| 706 | return None |
| 707 | |
| 708 | def call_wait_incoming(self, caller_msisdn_or_modem, timeout=60): |
| 709 | if isinstance(caller_msisdn_or_modem, Modem): |
| 710 | caller_msisdn = caller_msisdn_or_modem.msisdn |
| 711 | else: |
| 712 | caller_msisdn = str(caller_msisdn_or_modem) |
| 713 | self.dbg('Waiting for incoming call from:', caller_msisdn) |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 714 | MainLoop.wait(self, lambda: self._find_call_msisdn_state(caller_msisdn, 'incoming') is not None, timeout=timeout) |
Pau Espin Pedrol | d71edd1 | 2017-10-06 13:53:54 +0200 | [diff] [blame] | 715 | return self._find_call_msisdn_state(caller_msisdn, 'incoming') |
| 716 | |
| 717 | def call_answer(self, call_id): |
| 718 | self.dbg('Answer call %s' % call_id) |
| 719 | assert self.call_state(call_id) == 'incoming' |
| 720 | call_dbus_obj = systembus_get(call_id) |
| 721 | call_dbus_obj.Answer() |
Pau Espin Pedrol | 4d7f770 | 2019-02-13 19:30:38 +0100 | [diff] [blame] | 722 | self.dbg('Answered call %s' % call_id) |
Pau Espin Pedrol | d71edd1 | 2017-10-06 13:53:54 +0200 | [diff] [blame] | 723 | |
| 724 | def call_hangup(self, call_id): |
| 725 | self.dbg('Hang up call %s' % call_id) |
| 726 | call_dbus_obj = systembus_get(call_id) |
| 727 | call_dbus_obj.Hangup() |
| 728 | |
| 729 | def call_is_active(self, call_id): |
| 730 | return self.call_state(call_id) == 'active' |
| 731 | |
| 732 | def call_state(self, call_id): |
Pau Espin Pedrol | ccb1bc6 | 2018-04-22 12:58:08 +0200 | [diff] [blame] | 733 | try: |
| 734 | call_dbus_obj = systembus_get(call_id) |
| 735 | props = call_dbus_obj.GetProperties() |
| 736 | state = props.get('State') |
| 737 | except Exception as e: |
| 738 | self.log('asking call state for non existent call') |
| 739 | log.log_exn() |
| 740 | state = 'disconnected' |
Pau Espin Pedrol | 32e9d8c | 2019-02-13 17:40:31 +0100 | [diff] [blame] | 741 | self.dbg('call state: %s' % state, call_id=call_id) |
Pau Espin Pedrol | d71edd1 | 2017-10-06 13:53:54 +0200 | [diff] [blame] | 742 | return state |
| 743 | |
| 744 | def _on_callmgr_call_added(self, obj_path, properties): |
| 745 | self.dbg('%r.CallAdded() -> %s=%r' % (I_CALLMGR, obj_path, repr(properties))) |
| 746 | if obj_path not in self.call_list: |
| 747 | self.call_list.append(obj_path) |
| 748 | else: |
| 749 | self.dbg('Call already exists %r' % obj_path) |
| 750 | |
| 751 | def _on_callmgr_call_removed(self, obj_path): |
| 752 | self.dbg('%r.CallRemoved() -> %s' % (I_CALLMGR, obj_path)) |
| 753 | if obj_path in self.call_list: |
| 754 | self.call_list.remove(obj_path) |
| 755 | else: |
| 756 | self.dbg('Trying to remove non-existing call %r' % obj_path) |
| 757 | |
| 758 | def _on_callmgr_property_changed(self, name, value): |
| 759 | self.dbg('%r.PropertyChanged() -> %s=%s' % (I_CALLMGR, name, value)) |
| 760 | |
Pau Espin Pedrol | de89961 | 2017-11-23 17:18:40 +0100 | [diff] [blame] | 761 | def _on_connmgr_property_changed(self, name, value): |
| 762 | self.dbg('%r.PropertyChanged() -> %s=%s' % (I_CONNMGR, name, value)) |
| 763 | |
Pau Espin Pedrol | ee6e491 | 2017-09-05 18:46:34 +0200 | [diff] [blame] | 764 | def info(self, keys=('Manufacturer', 'Model', 'Revision', 'Serial')): |
Neels Hofmeyr | b801169 | 2017-05-29 03:45:24 +0200 | [diff] [blame] | 765 | props = self.properties() |
| 766 | return ', '.join(['%s: %r'%(k,props.get(k)) for k in keys]) |
| 767 | |
| 768 | def log_info(self, *args, **kwargs): |
| 769 | self.log(self.info(*args, **kwargs)) |
| 770 | |
Pau Espin Pedrol | 03983aa | 2017-06-12 15:31:27 +0200 | [diff] [blame] | 771 | def ussd_send(self, command): |
| 772 | ss = self.dbus.interface(I_SS) |
| 773 | service_type, response = ss.Initiate(command) |
| 774 | return response |
| 775 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 776 | # vim: expandtab tabstop=4 shiftwidth=4 |