blob: 18ebec5768d65aa6645f80e946b43985f78fc052 [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
8# it under the terms of the GNU Affero General Public License as
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
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020020from . import log, test
Neels Hofmeyr3531a192017-03-28 14:30:28 +020021
22from pydbus import SystemBus, Variant
23import time
24import pprint
25
26from gi.repository import GLib
27glib_main_loop = GLib.MainLoop()
28glib_main_ctx = glib_main_loop.get_context()
29bus = SystemBus()
30
Pau Espin Pedrol504a6642017-05-04 11:38:23 +020031I_MODEM = 'org.ofono.Modem'
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020032I_NETREG = 'org.ofono.NetworkRegistration'
33I_SMS = 'org.ofono.MessageManager'
34
Neels Hofmeyr3531a192017-03-28 14:30:28 +020035def poll():
36 global glib_main_ctx
37 while glib_main_ctx.pending():
38 glib_main_ctx.iteration()
39
Neels Hofmeyr93f58662017-05-03 16:32:16 +020040def systembus_get(path):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020041 global bus
42 return bus.get('org.ofono', path)
43
44def list_modems():
Neels Hofmeyr93f58662017-05-03 16:32:16 +020045 root = systembus_get('/')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020046 return sorted(root.GetModems())
47
48
49class Modem(log.Origin):
50 'convenience for ofono Modem interaction'
51 msisdn = None
Neels Hofmeyrfec7d162017-05-02 16:29:09 +020052 sms_received_list = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020053
54 def __init__(self, conf):
55 self.conf = conf
56 self.path = conf.get('path')
57 self.set_name(self.path)
58 self.set_log_category(log.C_BUS)
59 self._dbus_obj = None
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020060 self._interfaces = set()
Neels Hofmeyrfec7d162017-05-02 16:29:09 +020061 self.sms_received_list = []
Pau Espin Pedrol107f2752017-05-04 11:37:16 +020062 # init interfaces and connect to signals:
63 self.dbus_obj()
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020064 test.poll()
Neels Hofmeyr3531a192017-03-28 14:30:28 +020065
66 def set_msisdn(self, msisdn):
67 self.msisdn = msisdn
68
69 def imsi(self):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +020070 imsi = self.conf.get('imsi')
71 if not imsi:
72 with self:
73 raise RuntimeError('No IMSI')
74 return imsi
Neels Hofmeyr3531a192017-03-28 14:30:28 +020075
76 def ki(self):
77 return self.conf.get('ki')
78
Pau Espin Pedrol504a6642017-05-04 11:38:23 +020079 def _dbus_set_bool(self, name, bool_val, iface=I_MODEM):
Neels Hofmeyr9dbcb822017-05-02 14:57:57 +020080 # to make sure any pending signals are received before we send out more DBus requests
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020081 test.poll()
Neels Hofmeyr3531a192017-03-28 14:30:28 +020082
Neels Hofmeyr9dbcb822017-05-02 14:57:57 +020083 val = bool(bool_val)
84 self.log('Setting', name, val)
Pau Espin Pedrol504a6642017-05-04 11:38:23 +020085 self.dbus_obj()[iface].SetProperty(name, Variant('b', val))
Neels Hofmeyr9dbcb822017-05-02 14:57:57 +020086
Neels Hofmeyr27d459c2017-05-02 16:30:18 +020087 test.wait(self.property_is, name, bool_val)
88
89 def property_is(self, name, val):
90 is_val = self.properties().get(name)
91 self.dbg(name, '==', is_val)
92 return is_val is not None and is_val == val
Neels Hofmeyr9dbcb822017-05-02 14:57:57 +020093
94 def set_powered(self, on=True):
95 self._dbus_set_bool('Powered', on)
96
Pau Espin Pedrolb9955762017-05-02 09:39:27 +020097 def set_online(self, on=True):
Neels Hofmeyr9dbcb822017-05-02 14:57:57 +020098 self._dbus_set_bool('Online', on)
Pau Espin Pedrolb9955762017-05-02 09:39:27 +020099
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200100 def dbus_obj(self):
101 if self._dbus_obj is not None:
102 return self._dbus_obj
Neels Hofmeyr93f58662017-05-03 16:32:16 +0200103 self._dbus_obj = systembus_get(self.path)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200104 self._dbus_obj.PropertyChanged.connect(self._on_property_change)
105 self._on_interfaces_change(self.properties().get('Interfaces'))
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200106 return self._dbus_obj
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200107
Pau Espin Pedrol504a6642017-05-04 11:38:23 +0200108 def properties(self, iface=I_MODEM):
109 return self.dbus_obj()[iface].GetProperties()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200110
111 def _on_property_change(self, name, value):
112 if name == 'Interfaces':
113 self._on_interfaces_change(value)
114
115 def _on_interfaces_change(self, interfaces_now):
116 now = set(interfaces_now)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200117 additions = now - self._interfaces
118 removals = self._interfaces - now
119 self._interfaces = now
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200120 for iface in removals:
121 with log.Origin('modem.disable(%s)' % iface):
Neels Hofmeyrcf1e20e2017-05-05 18:34:02 +0200122 self._on_interface_disabled(iface)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200123 for iface in additions:
124 with log.Origin('modem.enable(%s)' % iface):
Neels Hofmeyrcf1e20e2017-05-05 18:34:02 +0200125 self._on_interface_enabled(iface)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200126
127 def _on_interface_enabled(self, interface_name):
128 self.dbg('Interface enabled:', interface_name)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200129 if interface_name == I_SMS:
Neels Hofmeyr943f8a22017-05-03 17:37:53 +0200130 retries = 3
Neels Hofmeyr5fe88812017-05-03 17:20:17 +0200131 while True:
Neels Hofmeyr5fe88812017-05-03 17:20:17 +0200132 try:
133 self.dbus_obj()[I_SMS].IncomingMessage.connect(self._on_incoming_message)
134 break
135 except:
136 self.dbg('Interface not yet available:', I_SMS)
137 retries -= 1
138 time.sleep(1)
139 if retries <= 0:
140 self.err('Interface enabled by signal, but not available:', I_SMS)
141 raise
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200142
143 def _on_interface_disabled(self, interface_name):
144 self.dbg('Interface disabled:', interface_name)
145
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200146 def has_interface(self, name):
147 return name in self._interfaces
148
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200149 def connect(self, nitb):
150 'set the modem up to connect to MCC+MNC from NITB config'
151 self.log('connect to', nitb)
Pau Espin Pedrol107f2752017-05-04 11:37:16 +0200152 prepowered = self.properties().get('Powered')
153 if prepowered is not None and prepowered:
154 self.set_online(False)
155 self.set_powered(False)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200156 self.set_powered()
Pau Espin Pedrolb9955762017-05-02 09:39:27 +0200157 self.set_online()
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200158 if not self.has_interface(I_NETREG):
159 self.log('No %r interface, hoping that the modem connects by itself' % I_NETREG)
160 else:
161 self.log('Use of %r interface not implemented yet, hoping that the modem connects by itself' % I_NETREG)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200162
Neels Hofmeyr8d8b03e2017-05-06 18:19:46 +0200163 def sms_send(self, to_msisdn, *tokens):
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200164 if hasattr(to_msisdn, 'msisdn'):
165 to_msisdn = to_msisdn.msisdn
Neels Hofmeyr8d8b03e2017-05-06 18:19:46 +0200166 sms = Sms(self.msisdn, to_msisdn, 'from ' + self.name(), *tokens)
Neels Hofmeyr1ea59ea2017-05-02 14:41:54 +0200167 self.log('sending sms to MSISDN', to_msisdn, sms=sms)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200168 if not self.has_interface(I_SMS):
169 raise RuntimeError('Modem cannot send SMS, interface not active: %r' % I_SMS)
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200170 mm = self.dbus_obj()[I_SMS]
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200171 mm.SendMessage(to_msisdn, str(sms))
172 return sms
173
174 def _on_incoming_message(self, message, info):
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200175 self.log('Incoming SMS:', repr(message), info=info)
176 self.sms_received_list.append((message, info))
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200177
Neels Hofmeyr863cb562017-05-02 16:27:59 +0200178 def sms_was_received(self, sms):
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200179 for msg, info in self.sms_received_list:
180 if sms.matches(msg):
181 self.log('SMS received as expected:', msg=msg, info=info)
182 return True
183 return False
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200184
185class Sms:
186 _last_sms_idx = 0
187 msg = None
188
Neels Hofmeyr8d8b03e2017-05-06 18:19:46 +0200189 def __init__(self, from_msisdn=None, to_msisdn=None, *tokens):
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200190 Sms._last_sms_idx += 1
191 msgs = ['message nr. %d' % Sms._last_sms_idx]
Neels Hofmeyr8d8b03e2017-05-06 18:19:46 +0200192 msgs.extend(tokens)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200193 if from_msisdn:
Neels Hofmeyr8d8b03e2017-05-06 18:19:46 +0200194 msgs.append('from %s' % from_msisdn)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200195 if to_msisdn:
Neels Hofmeyr8d8b03e2017-05-06 18:19:46 +0200196 msgs.append('to %s' % to_msisdn)
197 self.msg = ', '.join(msgs)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200198
199 def __str__(self):
200 return self.msg
201
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200202 def __repr__(self):
203 return repr(self.msg)
204
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200205 def __eq__(self, other):
206 if isinstance(other, Sms):
207 return self.msg == other.msg
208 return inself.msg == other
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200209
Neels Hofmeyrfec7d162017-05-02 16:29:09 +0200210 def matches(self, msg):
211 return self.msg == msg
212
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200213# vim: expandtab tabstop=4 shiftwidth=4