summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2014-04-10 19:05:13 +0200
committerAleksander Morgado <aleksander@aleksander.es>2014-04-11 10:31:01 +0200
commitebff76cfaa0e5809ef5883d7588f55dc3e11f828 (patch)
treefeee88d7ba25f780d705cf0f5cf76f3f80ef481c /examples
parent79b34b77b4703eaaf7e007c294325a660a51bc9e (diff)
examples: add modem watcher example in python
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am1
-rw-r--r--examples/modem-watcher-python/Makefile.am4
-rw-r--r--examples/modem-watcher-python/ModemWatcher.py108
-rw-r--r--examples/modem-watcher-python/README22
-rwxr-xr-xexamples/modem-watcher-python/modem-watcher-python42
5 files changed, 177 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
new file mode 100644
index 00000000..26e5407f
--- /dev/null
+++ b/examples/Makefile.am
@@ -0,0 +1 @@
+SUBDIRS = modem-watcher-python
diff --git a/examples/modem-watcher-python/Makefile.am b/examples/modem-watcher-python/Makefile.am
new file mode 100644
index 00000000..52bff807
--- /dev/null
+++ b/examples/modem-watcher-python/Makefile.am
@@ -0,0 +1,4 @@
+
+EXTRA_DIST = \
+ ModemWatcher.py \
+ modem-watcher-python
diff --git a/examples/modem-watcher-python/ModemWatcher.py b/examples/modem-watcher-python/ModemWatcher.py
new file mode 100644
index 00000000..2a9d7390
--- /dev/null
+++ b/examples/modem-watcher-python/ModemWatcher.py
@@ -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())
diff --git a/examples/modem-watcher-python/README b/examples/modem-watcher-python/README
new file mode 100644
index 00000000..f2b58e69
--- /dev/null
+++ b/examples/modem-watcher-python/README
@@ -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! \ No newline at end of file
diff --git a/examples/modem-watcher-python/modem-watcher-python b/examples/modem-watcher-python/modem-watcher-python
new file mode 100755
index 00000000..40998588
--- /dev/null
+++ b/examples/modem-watcher-python/modem-watcher-python
@@ -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