1
0
Fork 0

ofono: Add some modem detection code to find active modems

This commit is contained in:
Holger Hans Peter Freyther 2012-11-09 14:54:18 +01:00
parent a91e75bac6
commit 8810241b04
2 changed files with 85 additions and 7 deletions

View File

@ -14,8 +14,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import dbus
import sim
class Modem:
class Modem(object):
def __init__(self, bus, name):
self.name = name
self.bus = bus
@ -45,6 +46,31 @@ class Modem:
"""
self.modem.SetProperty("Online", dbus.Boolean(0), timeout = 120)
def is_enabled(self):
"""
Is the modem online?
"""
return bool(self._get_property('Powered'))
def manufacturer(self):
"""
Who is the owner of the mode?
"""
man = self.modem.GetProperties()
try:
return str(man['Manufacturer'])
except:
return None
def _get_property(self, name):
"""
Internal
"""
return self.modem.GetProperties()[name]
def __repr__(self):
return "<Modem('%s')>" % self.name
def get(bus, name):
"""
@ -57,5 +83,53 @@ def getmodems(bus):
Find modems...
"""
obj = dbus.Interface(bus.get_object('org.ofono', '/'), 'org.ofono.Manager')
return obj.GetModems()
return [Modem(bus, str(x[0])) for x in obj.GetModems()]
def detect_modems(bus):
"""
Detect the modems that can be used for the test...
"""
modems = getmodems(bus)
# Filter out the phonesim
modems = filter(lambda x: x.name != '/phonesim', modems)
# Force reloading..
wait = []
on = []
for mod in modems:
mod.disable()
# Enable each modem...
for mod in modems:
if mod.is_enabled():
on.append(mod)
else:
print("Going to enable modem: %s" % mod.name)
mod.enable()
wait.append(mod)
# Now... wait a bit for the modem to do some init
if len(wait) > 0:
import time
time.sleep(3)
for mod in wait:
if mod.is_enabled():
on.append(mod)
# Now filter out the modems without a SIM Card
def modem_vendor(modem):
# Check if the modem vendor was queried
return modem.manufacturer() != None
def sim_present(modem):
s = sim.Sim(bus, modem.name)
print s
return s.imsi() != None
on = filter(modem_vendor, on)
on = filter(sim_present, on)
return on

View File

@ -15,17 +15,21 @@
import dbus
class Sim:
class Sim(object):
def __init__(self, bus, path):
self.bus = bus
self.path = path
self.sim = dbus.Interface(bus.get_object('org.ofono', path), 'org.ofono.SimManager')
def present(self):
return bool(self.sim.GetProperties(timeout=120)['Present'])
def imsi(self):
return str(self.sim.GetProperties(timeout=120)['SubscriberIdentity'])
res = self.sim.GetProperties(['SubscriberIdentity'])
try:
return str(res['SubscriberIdentity'])
except:
return None
def __repr__(self):
return "<Sim(imsi=%s) of '%s'>" % (self.imsi(), self.path)
def get(bus, path):
"""Get the SIM manager"""