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