examples: add modem watcher example in python

This commit is contained in:
Aleksander Morgado
2014-04-10 19:05:13 +02:00
parent 79b34b77b4
commit ebff76cfaa
7 changed files with 180 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
SUBDIRS = . build-aux data include libqcdm libwmc libmm-glib src plugins cli vapi introspection uml290 po test docs
SUBDIRS = . build-aux data include libqcdm libwmc libmm-glib src plugins cli vapi introspection uml290 po test examples docs
DISTCHECK_CONFIGURE_FLAGS = \
--with-udev-base-dir="$$dc_install_base" \

View File

@@ -308,6 +308,8 @@ libmm-glib/generated/tests/Makefile
libmm-glib/tests/Makefile
vapi/Makefile
cli/Makefile
examples/Makefile
examples/modem-watcher-python/Makefile
])
AC_OUTPUT

1
examples/Makefile.am Normal file
View File

@@ -0,0 +1 @@
SUBDIRS = modem-watcher-python

View File

@@ -0,0 +1,4 @@
EXTRA_DIST = \
ModemWatcher.py \
modem-watcher-python

View File

@@ -0,0 +1,108 @@
#!/usr/bin/env python3
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2014 Aleksander Morgado <aleksander@aleksander.es>
#
import os
import sys
from gi.repository import GLib, GObject, Gio, ModemManager
"""
The ModemWatcher class is responsible for monitoring ModemManager
"""
class ModemWatcher:
"""
Constructor
"""
def __init__(self):
# Flag for initial logs
self.initializing = True
# Setup DBus monitoring
self.connection = Gio.bus_get_sync (Gio.BusType.SYSTEM, None)
self.manager = ModemManager.Manager.new_sync (self.connection,
Gio.DBusObjectManagerClientFlags.DO_NOT_AUTO_START,
None)
# IDs for added/removed signals
self.object_added_id = 0
self.object_removed_id = 0
# Follow availability of the ModemManager process
self.available = False
self.manager.connect('notify::name-owner', self.on_name_owner)
self.on_name_owner(self.manager, None)
# Finish initialization
self.initializing = False
"""
ModemManager is now available
"""
def set_available(self):
if self.available == False or self.initializing == True:
print('[ModemWatcher] ModemManager service is available in bus')
self.object_added_id = self.manager.connect('object-added', self.on_object_added)
self.object_removed_id = self.manager.connect('object-removed', self.on_object_removed)
self.available = True
# Initial scan
if self.initializing == True:
for obj in self.manager.get_objects():
self.on_object_added(self.manager, obj)
"""
ModemManager is now unavailable
"""
def set_unavailable(self):
if self.available == True or self.initializing == True:
print('[ModemWatcher] ModemManager service not available in bus')
if self.object_added_id:
self.manager.disconnect(self.object_added_id)
self.object_added_id = 0
if self.object_removed_id:
self.manager.disconnect(self.object_removed_id)
self.object_removed_id = 0
self.available = False
"""
Name owner updates
"""
def on_name_owner(self, manager, prop):
if self.manager.get_name_owner():
self.set_available()
else:
self.set_unavailable()
"""
Object added
"""
def on_object_added(self, manager, obj):
modem = obj.get_modem()
print('[ModemWatcher] %s (%s) modem managed by ModemManager [%s]: %s' %
(modem.get_manufacturer(),
modem.get_model(),
modem.get_equipment_identifier(),
obj.get_object_path()))
if modem.get_state() == ModemManager.ModemState.FAILED:
print('[ModemWatcher,%s] ignoring failed modem' %
modem_index(obj.get_object_path()))
"""
Object removed
"""
def on_object_removed(self, manager, obj):
print('[ModemWatcher] modem unmanaged by ModemManager: %s' %
obj.get_object_path())

View File

@@ -0,0 +1,22 @@
The modem-watcher-python program makes use of the 'libmm-glib' library through
GObject Introspection to talk to ModemManager.
The program will just print in stdout whenever:
* ModemManager is found in the bus
* ModemManager is lost in the bus
* A new modem is added to ModemManager
* A modem is removed from ModemManager
The output will look like this:
$ ./modem-watcher-python
[ModemWatcher] ModemManager service is available in bus
[ModemWatcher] Vodafone (Huawei) (K3772) modem managed by ModemManager [861320000017897]: /org/freedesktop/ModemManager1/Modem/0
[ModemWatcher] modem unmanaged by ModemManager: /org/freedesktop/ModemManager1/Modem/0
[ModemWatcher] ModemManager service not available in bus
Note that the program requires ModemManager and libmm-glib to be installed in
the system and the introspection typelibs available in the standard paths.
Have fun!

View File

@@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2014 Aleksander Morgado <aleksander@aleksander.es>
#
import sys, signal
import ModemWatcher
from gi.repository import GLib
main_loop = None
watcher = None
def signal_handler(data):
main_loop.quit()
if __name__ == "__main__":
# Create modem watcher
watcher = ModemWatcher.ModemWatcher()
# Main loop
main_loop = GLib.MainLoop()
GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP, signal_handler, None)
GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM, signal_handler, None)
try:
main_loop.run()
except KeyboardInterrupt:
pass