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 | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 20 | from . import log, test, util, event_loop, sms |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 21 | |
| 22 | from pydbus import SystemBus, Variant |
| 23 | import time |
| 24 | import pprint |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 25 | import sys |
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 |
| 33 | glib_main_loop = GLib.MainLoop() |
| 34 | glib_main_ctx = glib_main_loop.get_context() |
| 35 | bus = SystemBus() |
| 36 | |
Pau Espin Pedrol | 504a664 | 2017-05-04 11:38:23 +0200 | [diff] [blame] | 37 | I_MODEM = 'org.ofono.Modem' |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 38 | I_NETREG = 'org.ofono.NetworkRegistration' |
| 39 | I_SMS = 'org.ofono.MessageManager' |
| 40 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 41 | # See https://github.com/intgr/ofono/blob/master/doc/network-api.txt#L78 |
| 42 | NETREG_ST_REGISTERED = 'registered' |
| 43 | NETREG_ST_ROAMING = 'roaming' |
| 44 | |
| 45 | NETREG_MAX_REGISTER_ATTEMPTS = 3 |
| 46 | |
Neels Hofmeyr | 035cda8 | 2017-05-05 17:52:45 +0200 | [diff] [blame] | 47 | class DeferredHandling: |
| 48 | defer_queue = [] |
| 49 | |
| 50 | def __init__(self, dbus_iface, handler): |
| 51 | self.handler = handler |
Neels Hofmeyr | 47de6b0 | 2017-05-10 13:24:05 +0200 | [diff] [blame] | 52 | self.subscription_id = dbus_iface.connect(self.receive_signal) |
Neels Hofmeyr | 035cda8 | 2017-05-05 17:52:45 +0200 | [diff] [blame] | 53 | |
| 54 | def receive_signal(self, *args, **kwargs): |
| 55 | DeferredHandling.defer_queue.append((self.handler, args, kwargs)) |
| 56 | |
| 57 | @staticmethod |
| 58 | def handle_queue(): |
| 59 | while DeferredHandling.defer_queue: |
| 60 | handler, args, kwargs = DeferredHandling.defer_queue.pop(0) |
| 61 | handler(*args, **kwargs) |
| 62 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 63 | def defer(handler, *args, **kwargs): |
| 64 | DeferredHandling.defer_queue.append((handler, args, kwargs)) |
| 65 | |
Neels Hofmeyr | 035cda8 | 2017-05-05 17:52:45 +0200 | [diff] [blame] | 66 | def dbus_connect(dbus_iface, handler): |
| 67 | '''This function shall be used instead of directly connecting DBus signals. |
| 68 | It ensures that we don't nest a glib main loop within another, and also |
| 69 | that we receive exceptions raised within the signal handlers. This makes it |
| 70 | so that a signal handler is invoked only after the DBus polling is through |
| 71 | by enlisting signals that should be handled in the |
| 72 | DeferredHandling.defer_queue.''' |
Neels Hofmeyr | 47de6b0 | 2017-05-10 13:24:05 +0200 | [diff] [blame] | 73 | return DeferredHandling(dbus_iface, handler).subscription_id |
Neels Hofmeyr | 035cda8 | 2017-05-05 17:52:45 +0200 | [diff] [blame] | 74 | |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 75 | def poll_glib(): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 76 | global glib_main_ctx |
| 77 | while glib_main_ctx.pending(): |
| 78 | glib_main_ctx.iteration() |
Neels Hofmeyr | 035cda8 | 2017-05-05 17:52:45 +0200 | [diff] [blame] | 79 | DeferredHandling.handle_queue() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 80 | |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 81 | event_loop.register_poll_func(poll_glib) |
| 82 | |
Neels Hofmeyr | 93f5866 | 2017-05-03 16:32:16 +0200 | [diff] [blame] | 83 | def systembus_get(path): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 84 | global bus |
| 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 | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 91 | def _async_result_handler(obj, result, user_data): |
| 92 | '''Generic callback dispatcher called from glib loop when an async method |
| 93 | call has returned. This callback is set up by method dbus_async_call.''' |
| 94 | (result_callback, error_callback, real_user_data) = user_data |
| 95 | try: |
| 96 | ret = obj.call_finish(result) |
| 97 | except Exception as e: |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 98 | if isinstance(e, GLib.Error) and e.code == Gio.IOErrorEnum.CANCELLED: |
| 99 | log.dbg('DBus method cancelled') |
| 100 | return |
| 101 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 102 | if error_callback: |
| 103 | error_callback(obj, e, real_user_data) |
| 104 | else: |
| 105 | result_callback(obj, e, real_user_data) |
| 106 | return |
| 107 | |
| 108 | ret = ret.unpack() |
| 109 | # to be compatible with standard Python behaviour, unbox |
| 110 | # single-element tuples and return None for empty result tuples |
| 111 | if len(ret) == 1: |
| 112 | ret = ret[0] |
| 113 | elif len(ret) == 0: |
| 114 | ret = None |
| 115 | result_callback(obj, ret, real_user_data) |
| 116 | |
| 117 | def dbus_async_call(instance, proxymethod, *proxymethod_args, |
| 118 | result_handler=None, error_handler=None, |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 119 | user_data=None, timeout=30, cancellable=None, |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 120 | **proxymethod_kwargs): |
| 121 | '''pydbus doesn't support asynchronous methods. This method adds support for |
| 122 | it until pydbus implements it''' |
| 123 | |
| 124 | argdiff = len(proxymethod_args) - len(proxymethod._inargs) |
| 125 | if argdiff < 0: |
| 126 | raise TypeError(proxymethod.__qualname__ + " missing {} required positional argument(s)".format(-argdiff)) |
| 127 | elif argdiff > 0: |
| 128 | raise TypeError(proxymethod.__qualname__ + " takes {} positional argument(s) but {} was/were given".format(len(proxymethod._inargs), len(proxymethod_args))) |
| 129 | |
| 130 | timeout = timeout * 1000 |
| 131 | user_data = (result_handler, error_handler, user_data) |
| 132 | |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 133 | # See https://lazka.github.io/pgi-docs/Gio-2.0/classes/DBusProxy.html#Gio.DBusProxy.call |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 134 | ret = instance._bus.con.call( |
| 135 | instance._bus_name, instance._path, |
| 136 | proxymethod._iface_name, proxymethod.__name__, |
| 137 | GLib.Variant(proxymethod._sinargs, proxymethod_args), |
| 138 | GLib.VariantType.new(proxymethod._soutargs), |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 139 | 0, timeout, cancellable, |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 140 | _async_result_handler, user_data) |
| 141 | |
Pau Espin Pedrol | 7423d2e | 2017-08-25 12:58:25 +0200 | [diff] [blame] | 142 | def dbus_call_dismiss_error(log_obj, err_str, method): |
| 143 | try: |
| 144 | method() |
| 145 | except Exception as e: |
| 146 | if isinstance(e, GLib.Error) and err_str in e.domain: |
| 147 | log_obj.log('Dismissed Dbus method error: %r' % e) |
| 148 | return |
| 149 | raise log.Error('dbus_call_dismiss_error raised error %r' % e) |
| 150 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 151 | class ModemDbusInteraction(log.Origin): |
| 152 | '''Work around inconveniences specific to pydbus and ofono. |
| 153 | ofono adds and removes DBus interfaces and notifies about them. |
| 154 | Upon changes we need a fresh pydbus object to benefit from that. |
| 155 | Watching the interfaces change is optional; be sure to call |
| 156 | watch_interfaces() if you'd like to have signals subscribed. |
| 157 | Related: https://github.com/LEW21/pydbus/issues/56 |
| 158 | ''' |
| 159 | |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 160 | modem_path = None |
| 161 | watch_props_subscription = None |
| 162 | _dbus_obj = None |
| 163 | interfaces = None |
| 164 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 165 | def __init__(self, modem_path): |
| 166 | self.modem_path = modem_path |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 167 | super().__init__(log.C_BUS, self.modem_path) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 168 | self.interfaces = set() |
| 169 | |
| 170 | # A dict listing signal handlers to connect, e.g. |
| 171 | # { I_SMS: ( ('IncomingMessage', self._on_incoming_message), ), } |
| 172 | self.required_signals = {} |
| 173 | |
| 174 | # A dict collecting subscription tokens for connected signal handlers. |
| 175 | # { I_SMS: ( token1, token2, ... ), } |
| 176 | self.connected_signals = util.listdict() |
| 177 | |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 178 | def cleanup(self): |
Pau Espin Pedrol | 58ff38d | 2017-06-23 13:10:38 +0200 | [diff] [blame] | 179 | self.set_powered(False) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 180 | self.unwatch_interfaces() |
| 181 | for interface_name in list(self.connected_signals.keys()): |
| 182 | self.remove_signals(interface_name) |
| 183 | |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 184 | def __del__(self): |
| 185 | self.cleanup() |
| 186 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 187 | def get_new_dbus_obj(self): |
| 188 | return systembus_get(self.modem_path) |
| 189 | |
| 190 | def dbus_obj(self): |
| 191 | if self._dbus_obj is None: |
| 192 | self._dbus_obj = self.get_new_dbus_obj() |
| 193 | return self._dbus_obj |
| 194 | |
| 195 | def interface(self, interface_name): |
| 196 | try: |
| 197 | return self.dbus_obj()[interface_name] |
| 198 | except KeyError: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 199 | raise log.Error('Modem interface is not available:', interface_name) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 200 | |
| 201 | def signal(self, interface_name, signal): |
| 202 | return getattr(self.interface(interface_name), signal) |
| 203 | |
| 204 | def watch_interfaces(self): |
| 205 | self.unwatch_interfaces() |
| 206 | # Note: we are watching the properties on a get_new_dbus_obj() that is |
| 207 | # separate from the one used to interact with interfaces. We need to |
| 208 | # refresh the pydbus object to interact with Interfaces that have newly |
| 209 | # appeared, but exchanging the DBus object to watch Interfaces being |
| 210 | # enabled and disabled is racy: we may skip some removals and |
| 211 | # additions. Hence do not exchange this DBus object. We don't even |
| 212 | # need to store the dbus object used for this, we will not touch it |
| 213 | # again. We only store the signal subscription. |
| 214 | self.watch_props_subscription = dbus_connect(self.get_new_dbus_obj().PropertyChanged, |
| 215 | self.on_property_change) |
| 216 | self.on_interfaces_change(self.properties().get('Interfaces')) |
| 217 | |
| 218 | def unwatch_interfaces(self): |
| 219 | if self.watch_props_subscription is None: |
| 220 | return |
| 221 | self.watch_props_subscription.disconnect() |
| 222 | self.watch_props_subscription = None |
| 223 | |
| 224 | def on_property_change(self, name, value): |
| 225 | if name == 'Interfaces': |
| 226 | self.on_interfaces_change(value) |
Pau Espin Pedrol | 7763121 | 2017-09-05 19:04:06 +0200 | [diff] [blame] | 227 | else: |
| 228 | self.dbg('%r.PropertyChanged() -> %s=%s' % (I_MODEM, name, value)) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 229 | |
| 230 | def on_interfaces_change(self, interfaces_now): |
| 231 | # First some logging. |
| 232 | now = set(interfaces_now) |
| 233 | additions = now - self.interfaces |
| 234 | removals = self.interfaces - now |
| 235 | self.interfaces = now |
| 236 | if not (additions or removals): |
| 237 | # nothing changed. |
| 238 | return |
| 239 | |
| 240 | if additions: |
| 241 | self.dbg('interface enabled:', ', '.join(sorted(additions))) |
| 242 | |
| 243 | if removals: |
| 244 | self.dbg('interface disabled:', ', '.join(sorted(removals))) |
| 245 | |
| 246 | # The dbus object is now stale and needs refreshing before we |
| 247 | # access the next interface function. |
| 248 | self._dbus_obj = None |
| 249 | |
| 250 | # If an interface disappeared, disconnect the signal handlers for it. |
| 251 | # Even though we're going to use a fresh dbus object for new |
| 252 | # subscriptions, we will still keep active subscriptions alive on the |
| 253 | # old dbus object which will linger, associated with the respective |
| 254 | # signal subscription. |
| 255 | for removed in removals: |
| 256 | self.remove_signals(removed) |
| 257 | |
| 258 | # Connect signals for added interfaces. |
| 259 | for interface_name in additions: |
| 260 | self.connect_signals(interface_name) |
| 261 | |
| 262 | def remove_signals(self, interface_name): |
| 263 | got = self.connected_signals.pop(interface_name, []) |
| 264 | |
| 265 | if not got: |
| 266 | return |
| 267 | |
| 268 | self.dbg('Disconnecting', len(got), 'signals for', interface_name) |
| 269 | for subscription in got: |
| 270 | subscription.disconnect() |
| 271 | |
| 272 | def connect_signals(self, interface_name): |
| 273 | # If an interface was added, it must not have existed before. For |
| 274 | # paranoia, make sure we have no handlers for those. |
| 275 | self.remove_signals(interface_name) |
| 276 | |
| 277 | want = self.required_signals.get(interface_name, []) |
| 278 | if not want: |
| 279 | return |
| 280 | |
| 281 | self.dbg('Connecting', len(want), 'signals for', interface_name) |
| 282 | for signal, cb in self.required_signals.get(interface_name, []): |
| 283 | subscription = dbus_connect(self.signal(interface_name, signal), cb) |
| 284 | self.connected_signals.add(interface_name, subscription) |
| 285 | |
| 286 | def has_interface(self, *interface_names): |
| 287 | try: |
| 288 | for interface_name in interface_names: |
| 289 | self.dbus_obj()[interface_name] |
| 290 | result = True |
| 291 | except KeyError: |
| 292 | result = False |
| 293 | self.dbg('has_interface(%s) ==' % (', '.join(interface_names)), result) |
| 294 | return result |
| 295 | |
| 296 | def properties(self, iface=I_MODEM): |
| 297 | return self.dbus_obj()[iface].GetProperties() |
| 298 | |
| 299 | def property_is(self, name, val, iface=I_MODEM): |
| 300 | is_val = self.properties(iface).get(name) |
| 301 | self.dbg(name, '==', is_val) |
| 302 | return is_val is not None and is_val == val |
| 303 | |
| 304 | def set_bool(self, name, bool_val, iface=I_MODEM): |
| 305 | # to make sure any pending signals are received before we send out more DBus requests |
| 306 | event_loop.poll() |
| 307 | |
| 308 | val = bool(bool_val) |
| 309 | self.log('Setting', name, val) |
| 310 | self.interface(iface).SetProperty(name, Variant('b', val)) |
| 311 | |
| 312 | event_loop.wait(self, self.property_is, name, bool_val) |
| 313 | |
| 314 | def set_powered(self, powered=True): |
| 315 | self.set_bool('Powered', powered) |
| 316 | |
| 317 | def set_online(self, online=True): |
| 318 | self.set_bool('Online', online) |
| 319 | |
| 320 | def is_powered(self): |
| 321 | return self.property_is('Powered', True) |
| 322 | |
| 323 | def is_online(self): |
| 324 | return self.property_is('Online', True) |
| 325 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 326 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 327 | |
| 328 | class Modem(log.Origin): |
| 329 | 'convenience for ofono Modem interaction' |
| 330 | msisdn = None |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 331 | sms_received_list = None |
Pau Espin Pedrol | cd6ad9d | 2017-08-22 19:10:20 +0200 | [diff] [blame] | 332 | _ki = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 333 | |
| 334 | def __init__(self, conf): |
| 335 | self.conf = conf |
| 336 | self.path = conf.get('path') |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 337 | super().__init__(log.C_TST, self.path) |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 338 | self.sms_received_list = [] |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 339 | self.dbus = ModemDbusInteraction(self.path) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 340 | self.register_attempts = 0 |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 341 | # one Cancellable can handle several concurrent methods. |
| 342 | self.cancellable = Gio.Cancellable.new() |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 343 | self.dbus.required_signals = { |
| 344 | I_SMS: ( ('IncomingMessage', self._on_incoming_message), ), |
Pau Espin Pedrol | 56bf31c | 2017-05-31 12:05:20 +0200 | [diff] [blame] | 345 | I_NETREG: ( ('PropertyChanged', self._on_netreg_property_changed), ), |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 346 | } |
| 347 | self.dbus.watch_interfaces() |
| 348 | |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 349 | def cleanup(self): |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 350 | self.dbg('cleanup') |
| 351 | if self.cancellable: |
| 352 | self.cancellable.cancel() |
| 353 | # Cancel op is applied as a signal coming from glib mainloop, so we |
| 354 | # need to run it and wait for the callbacks to handle cancellations. |
| 355 | poll_glib() |
| 356 | self.cancellable = None |
Neels Hofmeyr | 4d688c2 | 2017-05-29 04:13:58 +0200 | [diff] [blame] | 357 | self.dbus.cleanup() |
| 358 | self.dbus = None |
| 359 | |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 360 | def properties(self, *args, **kwargs): |
| 361 | '''Return a dict of properties on this modem. For the actual arguments, |
| 362 | see ModemDbusInteraction.properties(), which this function calls. The |
| 363 | returned dict is defined by ofono. An example is: |
| 364 | {'Lockdown': False, |
| 365 | 'Powered': True, |
| 366 | 'Model': 'MC7304', |
| 367 | 'Revision': 'SWI9X15C_05.05.66.00 r29972 CARMD-EV-FRMWR1 2015/10/08 08:36:28', |
| 368 | 'Manufacturer': 'Sierra Wireless, Incorporated', |
| 369 | 'Emergency': False, |
| 370 | 'Interfaces': ['org.ofono.SmartMessaging', |
| 371 | 'org.ofono.PushNotification', |
| 372 | 'org.ofono.MessageManager', |
| 373 | 'org.ofono.NetworkRegistration', |
| 374 | 'org.ofono.ConnectionManager', |
| 375 | 'org.ofono.SupplementaryServices', |
| 376 | 'org.ofono.RadioSettings', |
| 377 | 'org.ofono.AllowedAccessPoints', |
| 378 | 'org.ofono.SimManager', |
| 379 | 'org.ofono.LocationReporting', |
| 380 | 'org.ofono.VoiceCallManager'], |
| 381 | 'Serial': '356853054230919', |
| 382 | 'Features': ['sms', 'net', 'gprs', 'ussd', 'rat', 'sim', 'gps'], |
| 383 | 'Type': 'hardware', |
| 384 | 'Online': True} |
| 385 | ''' |
| 386 | return self.dbus.properties(*args, **kwargs) |
| 387 | |
| 388 | def set_powered(self, powered=True): |
| 389 | return self.dbus.set_powered(powered=powered) |
| 390 | |
| 391 | def set_online(self, online=True): |
| 392 | return self.dbus.set_online(online=online) |
| 393 | |
| 394 | def is_powered(self): |
| 395 | return self.dbus.is_powered() |
| 396 | |
| 397 | def is_online(self): |
| 398 | return self.dbus.is_online() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 399 | |
| 400 | def set_msisdn(self, msisdn): |
| 401 | self.msisdn = msisdn |
| 402 | |
| 403 | def imsi(self): |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 404 | imsi = self.conf.get('imsi') |
| 405 | if not imsi: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 406 | raise log.Error('No IMSI') |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 407 | return imsi |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 408 | |
Pau Espin Pedrol | cd6ad9d | 2017-08-22 19:10:20 +0200 | [diff] [blame] | 409 | def set_ki(self, ki): |
| 410 | self._ki = ki |
| 411 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 412 | def ki(self): |
Pau Espin Pedrol | cd6ad9d | 2017-08-22 19:10:20 +0200 | [diff] [blame] | 413 | if self._ki is not None: |
| 414 | return self._ki |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 415 | return self.conf.get('ki') |
| 416 | |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 417 | def auth_algo(self): |
| 418 | return self.conf.get('auth_algo', None) |
| 419 | |
Pau Espin Pedrol | 56bf31c | 2017-05-31 12:05:20 +0200 | [diff] [blame] | 420 | def _on_netreg_property_changed(self, name, value): |
| 421 | self.dbg('%r.PropertyChanged() -> %s=%s' % (I_NETREG, name, value)) |
| 422 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 423 | def is_connected(self, mcc_mnc=None): |
| 424 | netreg = self.dbus.interface(I_NETREG) |
| 425 | prop = netreg.GetProperties() |
| 426 | status = prop.get('Status') |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 427 | self.dbg('status:', status) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 428 | if not (status == NETREG_ST_REGISTERED or status == NETREG_ST_ROAMING): |
| 429 | return False |
| 430 | if mcc_mnc is None: # Any network is fine and we are registered. |
| 431 | return True |
| 432 | mcc = prop.get('MobileCountryCode') |
| 433 | mnc = prop.get('MobileNetworkCode') |
| 434 | if (mcc, mnc) == mcc_mnc: |
| 435 | return True |
| 436 | return False |
| 437 | |
| 438 | def schedule_scan_register(self, mcc_mnc): |
| 439 | if self.register_attempts > NETREG_MAX_REGISTER_ATTEMPTS: |
Pau Espin Pedrol | cc5b5a2 | 2017-06-13 16:55:31 +0200 | [diff] [blame] | 440 | 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] | 441 | self.register_attempts += 1 |
| 442 | netreg = self.dbus.interface(I_NETREG) |
| 443 | self.dbg('Scanning for operators...') |
| 444 | # Scan method can take several seconds, and we don't want to block |
| 445 | # waiting for that. Make it async and try to register when the scan is |
| 446 | # finished. |
| 447 | register_func = self.scan_cb_register_automatic if mcc_mnc is None else self.scan_cb_register |
| 448 | result_handler = lambda obj, result, user_data: defer(register_func, result, user_data) |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 449 | error_handler = lambda obj, e, user_data: defer(self.scan_cb_error_handler, e, mcc_mnc) |
Pau Espin Pedrol | fbecf41 | 2017-06-14 12:14:53 +0200 | [diff] [blame] | 450 | dbus_async_call(netreg, netreg.Scan, timeout=30, cancellable=self.cancellable, |
| 451 | result_handler=result_handler, error_handler=error_handler, |
| 452 | user_data=mcc_mnc) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 453 | |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 454 | def scan_cb_error_handler(self, e, mcc_mnc): |
| 455 | # It was detected that Scan() method can fail for some modems on some |
| 456 | # specific circumstances. For instance it fails with org.ofono.Error.Failed |
| 457 | # if the modem starts to register internally after we started Scan() and |
| 458 | # the registering succeeds while we are still waiting for Scan() to finsih. |
| 459 | # So far the easiest seems to check if we are now registered and |
| 460 | # otherwise schedule a scan again. |
Pau Espin Pedrol | 910f3a1 | 2017-06-13 16:59:19 +0200 | [diff] [blame] | 461 | self.err('Scan() failed, retrying if needed:', e) |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 462 | if not self.is_connected(mcc_mnc): |
| 463 | self.schedule_scan_register(mcc_mnc) |
Pau Espin Pedrol | 910f3a1 | 2017-06-13 16:59:19 +0200 | [diff] [blame] | 464 | else: |
| 465 | self.log('Already registered with network', mcc_mnc) |
Pau Espin Pedrol | 4d63d92 | 2017-06-13 16:23:23 +0200 | [diff] [blame] | 466 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 467 | def scan_cb_register_automatic(self, scanned_operators, mcc_mnc): |
| 468 | self.dbg('scanned operators: ', scanned_operators); |
| 469 | for op_path, op_prop in scanned_operators: |
| 470 | if op_prop.get('Status') == 'current': |
| 471 | mcc = op_prop.get('MobileCountryCode') |
| 472 | mnc = op_prop.get('MobileNetworkCode') |
| 473 | self.log('Already registered with network', (mcc, mnc)) |
| 474 | return |
| 475 | self.log('Registering with the default network') |
| 476 | netreg = self.dbus.interface(I_NETREG) |
Pau Espin Pedrol | 7423d2e | 2017-08-25 12:58:25 +0200 | [diff] [blame] | 477 | dbus_call_dismiss_error(self, 'org.ofono.Error.InProgress', netreg.Register) |
| 478 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 479 | |
| 480 | def scan_cb_register(self, scanned_operators, mcc_mnc): |
| 481 | self.dbg('scanned operators: ', scanned_operators); |
| 482 | matching_op_path = None |
| 483 | for op_path, op_prop in scanned_operators: |
| 484 | mcc = op_prop.get('MobileCountryCode') |
| 485 | mnc = op_prop.get('MobileNetworkCode') |
| 486 | if (mcc, mnc) == mcc_mnc: |
| 487 | if op_prop.get('Status') == 'current': |
| 488 | self.log('Already registered with network', mcc_mnc) |
| 489 | # We discovered the network and we are already registered |
| 490 | # with it. Avoid calling op.Register() in this case (it |
| 491 | # won't act as a NO-OP, it actually returns an error). |
| 492 | return |
| 493 | matching_op_path = op_path |
| 494 | break |
| 495 | if matching_op_path is None: |
| 496 | self.dbg('Failed to find Network Operator', mcc_mnc=mcc_mnc, attempts=self.register_attempts) |
| 497 | self.schedule_scan_register(mcc_mnc) |
| 498 | return |
| 499 | dbus_op = systembus_get(matching_op_path) |
| 500 | self.log('Registering with operator', matching_op_path, mcc_mnc) |
Pau Espin Pedrol | 7423d2e | 2017-08-25 12:58:25 +0200 | [diff] [blame] | 501 | dbus_call_dismiss_error(self, 'org.ofono.Error.InProgress', dbus_op.Register) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 502 | |
| 503 | def power_cycle(self): |
| 504 | 'Power the modem and put it online, power cycle it if it was already on' |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 505 | if self.is_powered(): |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 506 | self.dbg('Power cycling') |
Pau Espin Pedrol | 107f275 | 2017-05-04 11:37:16 +0200 | [diff] [blame] | 507 | self.set_online(False) |
| 508 | self.set_powered(False) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 509 | event_loop.wait(self, lambda: not self.dbus.has_interface(I_NETREG, I_SMS), timeout=10) |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 510 | else: |
| 511 | self.dbg('Powering on') |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 512 | self.set_powered() |
Pau Espin Pedrol | b995576 | 2017-05-02 09:39:27 +0200 | [diff] [blame] | 513 | self.set_online() |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 514 | event_loop.wait(self, self.dbus.has_interface, I_NETREG, I_SMS, timeout=10) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 515 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 516 | def connect(self, mcc_mnc=None): |
| 517 | 'Connect to MCC+MNC' |
| 518 | 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] | 519 | 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 | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 520 | self.power_cycle() |
| 521 | self.register_attempts = 0 |
| 522 | if self.is_connected(mcc_mnc): |
| 523 | self.log('Already registered with', mcc_mnc if mcc_mnc else 'default network') |
| 524 | else: |
| 525 | self.log('Connect to', mcc_mnc if mcc_mnc else 'default network') |
| 526 | self.schedule_scan_register(mcc_mnc) |
| 527 | |
Neels Hofmeyr | 8c7477f | 2017-05-25 04:33:53 +0200 | [diff] [blame] | 528 | def sms_send(self, to_msisdn_or_modem, *tokens): |
| 529 | if isinstance(to_msisdn_or_modem, Modem): |
| 530 | to_msisdn = to_msisdn_or_modem.msisdn |
| 531 | tokens = list(tokens) |
| 532 | tokens.append('to ' + to_msisdn_or_modem.name()) |
| 533 | else: |
| 534 | to_msisdn = str(to_msisdn_or_modem) |
Pau Espin Pedrol | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 535 | msg = sms.Sms(self.msisdn, to_msisdn, 'from ' + self.name(), *tokens) |
| 536 | self.log('sending sms to MSISDN', to_msisdn, sms=msg) |
Neels Hofmeyr | 896f08f | 2017-05-24 20:17:26 +0200 | [diff] [blame] | 537 | mm = self.dbus.interface(I_SMS) |
Pau Espin Pedrol | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 538 | mm.SendMessage(to_msisdn, str(msg)) |
| 539 | return msg |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 540 | |
| 541 | def _on_incoming_message(self, message, info): |
Neels Hofmeyr | 2e41def | 2017-05-06 22:42:57 +0200 | [diff] [blame] | 542 | self.log('Incoming SMS:', repr(message)) |
Neels Hofmeyr | f49c7da | 2017-05-06 22:43:32 +0200 | [diff] [blame] | 543 | self.dbg(info=info) |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 544 | self.sms_received_list.append((message, info)) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 545 | |
Pau Espin Pedrol | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 546 | def sms_was_received(self, sms_obj): |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 547 | for msg, info in self.sms_received_list: |
Pau Espin Pedrol | 996651a | 2017-05-30 15:13:29 +0200 | [diff] [blame] | 548 | if sms_obj.matches(msg): |
Neels Hofmeyr | 2e41def | 2017-05-06 22:42:57 +0200 | [diff] [blame] | 549 | self.log('SMS received as expected:', repr(msg)) |
Neels Hofmeyr | f49c7da | 2017-05-06 22:43:32 +0200 | [diff] [blame] | 550 | self.dbg(info=info) |
Neels Hofmeyr | fec7d16 | 2017-05-02 16:29:09 +0200 | [diff] [blame] | 551 | return True |
| 552 | return False |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 553 | |
Pau Espin Pedrol | ee6e491 | 2017-09-05 18:46:34 +0200 | [diff] [blame] | 554 | def info(self, keys=('Manufacturer', 'Model', 'Revision', 'Serial')): |
Neels Hofmeyr | b801169 | 2017-05-29 03:45:24 +0200 | [diff] [blame] | 555 | props = self.properties() |
| 556 | return ', '.join(['%s: %r'%(k,props.get(k)) for k in keys]) |
| 557 | |
| 558 | def log_info(self, *args, **kwargs): |
| 559 | self.log(self.info(*args, **kwargs)) |
| 560 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 561 | # vim: expandtab tabstop=4 shiftwidth=4 |