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