blob: 4deff9ef4abba9638699b89c1f9a48240645d04f [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# 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 Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr3531a192017-03-28 14:30:28 +02009# 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 Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr3531a192017-03-28 14:30:28 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr3531a192017-03-28 14:30:28 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Pau Espin Pedrol996651a2017-05-30 15:13:29 +020020from . import log, test, util, event_loop, sms
Neels Hofmeyr3531a192017-03-28 14:30:28 +020021
22from pydbus import SystemBus, Variant
23import time
24import pprint
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020025import sys
Neels Hofmeyr3531a192017-03-28 14:30:28 +020026
27from gi.repository import GLib
28glib_main_loop = GLib.MainLoop()
29glib_main_ctx = glib_main_loop.get_context()
30bus = SystemBus()
31
Pau Espin Pedrol504a6642017-05-04 11:38:23 +020032I_MODEM = 'org.ofono.Modem'
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020033I_NETREG = 'org.ofono.NetworkRegistration'
34I_SMS = 'org.ofono.MessageManager'
35
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020036# See https://github.com/intgr/ofono/blob/master/doc/network-api.txt#L78
37NETREG_ST_REGISTERED = 'registered'
38NETREG_ST_ROAMING = 'roaming'
39
40NETREG_MAX_REGISTER_ATTEMPTS = 3
41
Neels Hofmeyr035cda82017-05-05 17:52:45 +020042class DeferredHandling:
43 defer_queue = []
44
45 def __init__(self, dbus_iface, handler):
46 self.handler = handler
Neels Hofmeyr47de6b02017-05-10 13:24:05 +020047 self.subscription_id = dbus_iface.connect(self.receive_signal)
Neels Hofmeyr035cda82017-05-05 17:52:45 +020048
49 def receive_signal(self, *args, **kwargs):
50 DeferredHandling.defer_queue.append((self.handler, args, kwargs))
51
52 @staticmethod
53 def handle_queue():
54 while DeferredHandling.defer_queue:
55 handler, args, kwargs = DeferredHandling.defer_queue.pop(0)
56 handler(*args, **kwargs)
57
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020058def defer(handler, *args, **kwargs):
59 DeferredHandling.defer_queue.append((handler, args, kwargs))
60
Neels Hofmeyr035cda82017-05-05 17:52:45 +020061def dbus_connect(dbus_iface, handler):
62 '''This function shall be used instead of directly connecting DBus signals.
63 It ensures that we don't nest a glib main loop within another, and also
64 that we receive exceptions raised within the signal handlers. This makes it
65 so that a signal handler is invoked only after the DBus polling is through
66 by enlisting signals that should be handled in the
67 DeferredHandling.defer_queue.'''
Neels Hofmeyr47de6b02017-05-10 13:24:05 +020068 return DeferredHandling(dbus_iface, handler).subscription_id
Neels Hofmeyr035cda82017-05-05 17:52:45 +020069
Pau Espin Pedrol927344b2017-05-22 16:38:49 +020070def poll_glib():
Neels Hofmeyr3531a192017-03-28 14:30:28 +020071 global glib_main_ctx
72 while glib_main_ctx.pending():
73 glib_main_ctx.iteration()
Neels Hofmeyr035cda82017-05-05 17:52:45 +020074 DeferredHandling.handle_queue()
Neels Hofmeyr3531a192017-03-28 14:30:28 +020075
Pau Espin Pedrol927344b2017-05-22 16:38:49 +020076event_loop.register_poll_func(poll_glib)
77
Neels Hofmeyr93f58662017-05-03 16:32:16 +020078def systembus_get(path):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020079 global bus
80 return bus.get('org.ofono', path)
81
82def list_modems():
Neels Hofmeyr93f58662017-05-03 16:32:16 +020083 root = systembus_get('/')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020084 return sorted(root.GetModems())
85
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020086def _async_result_handler(obj, result, user_data):
87 '''Generic callback dispatcher called from glib loop when an async method
88 call has returned. This callback is set up by method dbus_async_call.'''
89 (result_callback, error_callback, real_user_data) = user_data
90 try:
91 ret = obj.call_finish(result)
92 except Exception as e:
93 # return exception as value
94 if error_callback:
95 error_callback(obj, e, real_user_data)
96 else:
97 result_callback(obj, e, real_user_data)
98 return
99
100 ret = ret.unpack()
101 # to be compatible with standard Python behaviour, unbox
102 # single-element tuples and return None for empty result tuples
103 if len(ret) == 1:
104 ret = ret[0]
105 elif len(ret) == 0:
106 ret = None
107 result_callback(obj, ret, real_user_data)
108
109def dbus_async_call(instance, proxymethod, *proxymethod_args,
110 result_handler=None, error_handler=None,
111 user_data=None, timeout=30,
112 **proxymethod_kwargs):
113 '''pydbus doesn't support asynchronous methods. This method adds support for
114 it until pydbus implements it'''
115
116 argdiff = len(proxymethod_args) - len(proxymethod._inargs)
117 if argdiff < 0:
118 raise TypeError(proxymethod.__qualname__ + " missing {} required positional argument(s)".format(-argdiff))
119 elif argdiff > 0:
120 raise TypeError(proxymethod.__qualname__ + " takes {} positional argument(s) but {} was/were given".format(len(proxymethod._inargs), len(proxymethod_args)))
121
122 timeout = timeout * 1000
123 user_data = (result_handler, error_handler, user_data)
124
125 ret = instance._bus.con.call(
126 instance._bus_name, instance._path,
127 proxymethod._iface_name, proxymethod.__name__,
128 GLib.Variant(proxymethod._sinargs, proxymethod_args),
129 GLib.VariantType.new(proxymethod._soutargs),
130 0, timeout, None,
131 _async_result_handler, user_data)
132
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200133class ModemDbusInteraction(log.Origin):
134 '''Work around inconveniences specific to pydbus and ofono.
135 ofono adds and removes DBus interfaces and notifies about them.
136 Upon changes we need a fresh pydbus object to benefit from that.
137 Watching the interfaces change is optional; be sure to call
138 watch_interfaces() if you'd like to have signals subscribed.
139 Related: https://github.com/LEW21/pydbus/issues/56
140 '''
141
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200142 modem_path = None
143 watch_props_subscription = None
144 _dbus_obj = None
145 interfaces = None
146
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200147 def __init__(self, modem_path):
148 self.modem_path = modem_path
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200149 super().__init__(log.C_BUS, self.modem_path)
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200150 self.interfaces = set()
151
152 # A dict listing signal handlers to connect, e.g.
153 # { I_SMS: ( ('IncomingMessage', self._on_incoming_message), ), }
154 self.required_signals = {}
155
156 # A dict collecting subscription tokens for connected signal handlers.
157 # { I_SMS: ( token1, token2, ... ), }
158 self.connected_signals = util.listdict()
159
Neels Hofmeyr4d688c22017-05-29 04:13:58 +0200160 def cleanup(self):
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200161 self.unwatch_interfaces()
162 for interface_name in list(self.connected_signals.keys()):
163 self.remove_signals(interface_name)
164
Neels Hofmeyr4d688c22017-05-29 04:13:58 +0200165 def __del__(self):
166 self.cleanup()
167
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200168 def get_new_dbus_obj(self):
169 return systembus_get(self.modem_path)
170
171 def dbus_obj(self):
172 if self._dbus_obj is None:
173 self._dbus_obj = self.get_new_dbus_obj()
174 return self._dbus_obj
175
176 def interface(self, interface_name):
177 try:
178 return self.dbus_obj()[interface_name]
179 except KeyError:
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200180 raise log.Error('Modem interface is not available:', interface_name)
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200181
182 def signal(self, interface_name, signal):
183 return getattr(self.interface(interface_name), signal)
184
185 def watch_interfaces(self):
186 self.unwatch_interfaces()
187 # Note: we are watching the properties on a get_new_dbus_obj() that is
188 # separate from the one used to interact with interfaces. We need to
189 # refresh the pydbus object to interact with Interfaces that have newly
190 # appeared, but exchanging the DBus object to watch Interfaces being
191 # enabled and disabled is racy: we may skip some removals and
192 # additions. Hence do not exchange this DBus object. We don't even
193 # need to store the dbus object used for this, we will not touch it
194 # again. We only store the signal subscription.
195 self.watch_props_subscription = dbus_connect(self.get_new_dbus_obj().PropertyChanged,
196 self.on_property_change)
197 self.on_interfaces_change(self.properties().get('Interfaces'))
198
199 def unwatch_interfaces(self):
200 if self.watch_props_subscription is None:
201 return
202 self.watch_props_subscription.disconnect()
203 self.watch_props_subscription = None
204
205 def on_property_change(self, name, value):
206 if name == 'Interfaces':
207 self.on_interfaces_change(value)
208
209 def on_interfaces_change(self, interfaces_now):
210 # First some logging.
211 now = set(interfaces_now)
212 additions = now - self.interfaces
213 removals = self.interfaces - now
214 self.interfaces = now
215 if not (additions or removals):
216 # nothing changed.
217 return
218
219 if additions:
220 self.dbg('interface enabled:', ', '.join(sorted(additions)))
221
222 if removals:
223 self.dbg('interface disabled:', ', '.join(sorted(removals)))
224
225 # The dbus object is now stale and needs refreshing before we
226 # access the next interface function.
227 self._dbus_obj = None
228
229 # If an interface disappeared, disconnect the signal handlers for it.
230 # Even though we're going to use a fresh dbus object for new
231 # subscriptions, we will still keep active subscriptions alive on the
232 # old dbus object which will linger, associated with the respective
233 # signal subscription.
234 for removed in removals:
235 self.remove_signals(removed)
236
237 # Connect signals for added interfaces.
238 for interface_name in additions:
239 self.connect_signals(interface_name)
240
241 def remove_signals(self, interface_name):
242 got = self.connected_signals.pop(interface_name, [])
243
244 if not got:
245 return
246
247 self.dbg('Disconnecting', len(got), 'signals for', interface_name)
248 for subscription in got:
249 subscription.disconnect()
250
251 def connect_signals(self, interface_name):
252 # If an interface was added, it must not have existed before. For
253 # paranoia, make sure we have no handlers for those.
254 self.remove_signals(interface_name)
255
256 want = self.required_signals.get(interface_name, [])
257 if not want:
258 return
259
260 self.dbg('Connecting', len(want), 'signals for', interface_name)
261 for signal, cb in self.required_signals.get(interface_name, []):
262 subscription = dbus_connect(self.signal(interface_name, signal), cb)
263 self.connected_signals.add(interface_name, subscription)
264
265 def has_interface(self, *interface_names):
266 try:
267 for interface_name in interface_names:
268 self.dbus_obj()[interface_name]
269 result = True
270 except KeyError:
271 result = False
272 self.dbg('has_interface(%s) ==' % (', '.join(interface_names)), result)
273 return result
274
275 def properties(self, iface=I_MODEM):
276 return self.dbus_obj()[iface].GetProperties()
277
278 def property_is(self, name, val, iface=I_MODEM):
279 is_val = self.properties(iface).get(name)
280 self.dbg(name, '==', is_val)
281 return is_val is not None and is_val == val
282
283 def set_bool(self, name, bool_val, iface=I_MODEM):
284 # to make sure any pending signals are received before we send out more DBus requests
285 event_loop.poll()
286
287 val = bool(bool_val)
288 self.log('Setting', name, val)
289 self.interface(iface).SetProperty(name, Variant('b', val))
290
291 event_loop.wait(self, self.property_is, name, bool_val)
292
293 def set_powered(self, powered=True):
294 self.set_bool('Powered', powered)
295
296 def set_online(self, online=True):
297 self.set_bool('Online', online)
298
299 def is_powered(self):
300 return self.property_is('Powered', True)
301
302 def is_online(self):
303 return self.property_is('Online', True)
304
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200305
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200306
307class Modem(log.Origin):
308 'convenience for ofono Modem interaction'
309 msisdn = None
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200310 sms_received_list = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200311
312 def __init__(self, conf):
313 self.conf = conf
314 self.path = conf.get('path')
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200315 super().__init__(log.C_TST, self.path)
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200316 self.sms_received_list = []
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200317 self.dbus = ModemDbusInteraction(self.path)
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200318 self.register_attempts = 0
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200319 self.dbus.required_signals = {
320 I_SMS: ( ('IncomingMessage', self._on_incoming_message), ),
Pau Espin Pedrol56bf31c2017-05-31 12:05:20 +0200321 I_NETREG: ( ('PropertyChanged', self._on_netreg_property_changed), ),
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200322 }
323 self.dbus.watch_interfaces()
324
Neels Hofmeyr4d688c22017-05-29 04:13:58 +0200325 def cleanup(self):
326 self.dbus.cleanup()
327 self.dbus = None
328
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200329 def properties(self, *args, **kwargs):
330 '''Return a dict of properties on this modem. For the actual arguments,
331 see ModemDbusInteraction.properties(), which this function calls. The
332 returned dict is defined by ofono. An example is:
333 {'Lockdown': False,
334 'Powered': True,
335 'Model': 'MC7304',
336 'Revision': 'SWI9X15C_05.05.66.00 r29972 CARMD-EV-FRMWR1 2015/10/08 08:36:28',
337 'Manufacturer': 'Sierra Wireless, Incorporated',
338 'Emergency': False,
339 'Interfaces': ['org.ofono.SmartMessaging',
340 'org.ofono.PushNotification',
341 'org.ofono.MessageManager',
342 'org.ofono.NetworkRegistration',
343 'org.ofono.ConnectionManager',
344 'org.ofono.SupplementaryServices',
345 'org.ofono.RadioSettings',
346 'org.ofono.AllowedAccessPoints',
347 'org.ofono.SimManager',
348 'org.ofono.LocationReporting',
349 'org.ofono.VoiceCallManager'],
350 'Serial': '356853054230919',
351 'Features': ['sms', 'net', 'gprs', 'ussd', 'rat', 'sim', 'gps'],
352 'Type': 'hardware',
353 'Online': True}
354 '''
355 return self.dbus.properties(*args, **kwargs)
356
357 def set_powered(self, powered=True):
358 return self.dbus.set_powered(powered=powered)
359
360 def set_online(self, online=True):
361 return self.dbus.set_online(online=online)
362
363 def is_powered(self):
364 return self.dbus.is_powered()
365
366 def is_online(self):
367 return self.dbus.is_online()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200368
369 def set_msisdn(self, msisdn):
370 self.msisdn = msisdn
371
372 def imsi(self):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200373 imsi = self.conf.get('imsi')
374 if not imsi:
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200375 raise log.Error('No IMSI')
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200376 return imsi
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200377
378 def ki(self):
379 return self.conf.get('ki')
380
Pau Espin Pedrol56bf31c2017-05-31 12:05:20 +0200381 def _on_netreg_property_changed(self, name, value):
382 self.dbg('%r.PropertyChanged() -> %s=%s' % (I_NETREG, name, value))
383
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200384 def is_connected(self, mcc_mnc=None):
385 netreg = self.dbus.interface(I_NETREG)
386 prop = netreg.GetProperties()
387 status = prop.get('Status')
Pau Espin Pedrol4d63d922017-06-13 16:23:23 +0200388 self.dbg('status:', status)
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200389 if not (status == NETREG_ST_REGISTERED or status == NETREG_ST_ROAMING):
390 return False
391 if mcc_mnc is None: # Any network is fine and we are registered.
392 return True
393 mcc = prop.get('MobileCountryCode')
394 mnc = prop.get('MobileNetworkCode')
395 if (mcc, mnc) == mcc_mnc:
396 return True
397 return False
398
399 def schedule_scan_register(self, mcc_mnc):
400 if self.register_attempts > NETREG_MAX_REGISTER_ATTEMPTS:
Pau Espin Pedrolcc5b5a22017-06-13 16:55:31 +0200401 raise log.Error('Failed to find Network Operator', mcc_mnc=mcc_mnc, attempts=self.register_attempts)
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200402 self.register_attempts += 1
403 netreg = self.dbus.interface(I_NETREG)
404 self.dbg('Scanning for operators...')
405 # Scan method can take several seconds, and we don't want to block
406 # waiting for that. Make it async and try to register when the scan is
407 # finished.
408 register_func = self.scan_cb_register_automatic if mcc_mnc is None else self.scan_cb_register
409 result_handler = lambda obj, result, user_data: defer(register_func, result, user_data)
Pau Espin Pedrol4d63d922017-06-13 16:23:23 +0200410 error_handler = lambda obj, e, user_data: defer(self.scan_cb_error_handler, e, mcc_mnc)
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200411 dbus_async_call(netreg, netreg.Scan, timeout=30, result_handler=result_handler,
412 error_handler=error_handler, user_data=mcc_mnc)
413
Pau Espin Pedrol4d63d922017-06-13 16:23:23 +0200414 def scan_cb_error_handler(self, e, mcc_mnc):
415 # It was detected that Scan() method can fail for some modems on some
416 # specific circumstances. For instance it fails with org.ofono.Error.Failed
417 # if the modem starts to register internally after we started Scan() and
418 # the registering succeeds while we are still waiting for Scan() to finsih.
419 # So far the easiest seems to check if we are now registered and
420 # otherwise schedule a scan again.
Pau Espin Pedrol910f3a12017-06-13 16:59:19 +0200421 self.err('Scan() failed, retrying if needed:', e)
Pau Espin Pedrol4d63d922017-06-13 16:23:23 +0200422 if not self.is_connected(mcc_mnc):
423 self.schedule_scan_register(mcc_mnc)
Pau Espin Pedrol910f3a12017-06-13 16:59:19 +0200424 else:
425 self.log('Already registered with network', mcc_mnc)
Pau Espin Pedrol4d63d922017-06-13 16:23:23 +0200426
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200427 def scan_cb_register_automatic(self, scanned_operators, mcc_mnc):
428 self.dbg('scanned operators: ', scanned_operators);
429 for op_path, op_prop in scanned_operators:
430 if op_prop.get('Status') == 'current':
431 mcc = op_prop.get('MobileCountryCode')
432 mnc = op_prop.get('MobileNetworkCode')
433 self.log('Already registered with network', (mcc, mnc))
434 return
435 self.log('Registering with the default network')
436 netreg = self.dbus.interface(I_NETREG)
437 netreg.Register()
438
439 def scan_cb_register(self, scanned_operators, mcc_mnc):
440 self.dbg('scanned operators: ', scanned_operators);
441 matching_op_path = None
442 for op_path, op_prop in scanned_operators:
443 mcc = op_prop.get('MobileCountryCode')
444 mnc = op_prop.get('MobileNetworkCode')
445 if (mcc, mnc) == mcc_mnc:
446 if op_prop.get('Status') == 'current':
447 self.log('Already registered with network', mcc_mnc)
448 # We discovered the network and we are already registered
449 # with it. Avoid calling op.Register() in this case (it
450 # won't act as a NO-OP, it actually returns an error).
451 return
452 matching_op_path = op_path
453 break
454 if matching_op_path is None:
455 self.dbg('Failed to find Network Operator', mcc_mnc=mcc_mnc, attempts=self.register_attempts)
456 self.schedule_scan_register(mcc_mnc)
457 return
458 dbus_op = systembus_get(matching_op_path)
459 self.log('Registering with operator', matching_op_path, mcc_mnc)
460 dbus_op.Register()
461
462 def power_cycle(self):
463 'Power the modem and put it online, power cycle it if it was already on'
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200464 if self.is_powered():
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200465 self.dbg('Power cycling')
Pau Espin Pedrol107f2752017-05-04 11:37:16 +0200466 self.set_online(False)
467 self.set_powered(False)
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200468 event_loop.wait(self, lambda: not self.dbus.has_interface(I_NETREG, I_SMS), timeout=10)
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200469 else:
470 self.dbg('Powering on')
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200471 self.set_powered()
Pau Espin Pedrolb9955762017-05-02 09:39:27 +0200472 self.set_online()
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200473 event_loop.wait(self, self.dbus.has_interface, I_NETREG, I_SMS, timeout=10)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200474
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200475 def connect(self, mcc_mnc=None):
476 'Connect to MCC+MNC'
477 if (mcc_mnc is not None) and (len(mcc_mnc) != 2 or None in mcc_mnc):
Pau Espin Pedrolcc5b5a22017-06-13 16:55:31 +0200478 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 Pedrol0e57aad2017-05-29 14:25:22 +0200479 self.power_cycle()
480 self.register_attempts = 0
481 if self.is_connected(mcc_mnc):
482 self.log('Already registered with', mcc_mnc if mcc_mnc else 'default network')
483 else:
484 self.log('Connect to', mcc_mnc if mcc_mnc else 'default network')
485 self.schedule_scan_register(mcc_mnc)
486
Neels Hofmeyr8c7477f2017-05-25 04:33:53 +0200487 def sms_send(self, to_msisdn_or_modem, *tokens):
488 if isinstance(to_msisdn_or_modem, Modem):
489 to_msisdn = to_msisdn_or_modem.msisdn
490 tokens = list(tokens)
491 tokens.append('to ' + to_msisdn_or_modem.name())
492 else:
493 to_msisdn = str(to_msisdn_or_modem)
Pau Espin Pedrol996651a2017-05-30 15:13:29 +0200494 msg = sms.Sms(self.msisdn, to_msisdn, 'from ' + self.name(), *tokens)
495 self.log('sending sms to MSISDN', to_msisdn, sms=msg)
Neels Hofmeyr896f08f2017-05-24 20:17:26 +0200496 mm = self.dbus.interface(I_SMS)
Pau Espin Pedrol996651a2017-05-30 15:13:29 +0200497 mm.SendMessage(to_msisdn, str(msg))
498 return msg
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200499
500 def _on_incoming_message(self, message, info):
Neels Hofmeyr2e41def2017-05-06 22:42:57 +0200501 self.log('Incoming SMS:', repr(message))
Neels Hofmeyrf49c7da2017-05-06 22:43:32 +0200502 self.dbg(info=info)
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200503 self.sms_received_list.append((message, info))
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200504
Pau Espin Pedrol996651a2017-05-30 15:13:29 +0200505 def sms_was_received(self, sms_obj):
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200506 for msg, info in self.sms_received_list:
Pau Espin Pedrol996651a2017-05-30 15:13:29 +0200507 if sms_obj.matches(msg):
Neels Hofmeyr2e41def2017-05-06 22:42:57 +0200508 self.log('SMS received as expected:', repr(msg))
Neels Hofmeyrf49c7da2017-05-06 22:43:32 +0200509 self.dbg(info=info)
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200510 return True
511 return False
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200512
Neels Hofmeyrb8011692017-05-29 03:45:24 +0200513 def info(self, keys=('Manufacturer', 'Model', 'Revision')):
514 props = self.properties()
515 return ', '.join(['%s: %r'%(k,props.get(k)) for k in keys])
516
517 def log_info(self, *args, **kwargs):
518 self.log(self.info(*args, **kwargs))
519
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200520# vim: expandtab tabstop=4 shiftwidth=4