summaryrefslogtreecommitdiff
path: root/examples/network-scan-python/network-scan-python
blob: 21dd89f4ff6545659284c2c8b6a41e8ac5f3739c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/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) 2019 Aleksander Morgado <aleksander@aleksander.es>
#

import sys
import time

import gi
gi.require_version('ModemManager', '1.0')
from gi.repository import Gio, GLib, GObject, ModemManager


def main():
    """Main routine."""

    # Process input arguments
    if len(sys.argv) != 1:
        sys.stderr.write('error: wrong number of arguments\n')
        sys.stdout.write('usage: network-scan-python\n')
        sys.exit(1)

    # Connection to ModemManager
    connection = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
    manager = ModemManager.Manager.new_sync(
        connection, Gio.DBusObjectManagerClientFlags.DO_NOT_AUTO_START, None)
    if not manager.get_name_owner():
        sys.stderr.write('ModemManager not found in bus\n')
        sys.exit(2)

    # Iterate modems and scan network with each one by one
    for obj in manager.get_objects():
        modem = obj.get_modem()
        modem.enable()
        time.sleep(1)
        modem3gpp = obj.get_modem_3gpp()
        if not modem3gpp:
            sys.stderr.write('%s: skipping unusable modem...\n' %
                             obj.get_object_path())
            continue
        modem3gpp.set_default_timeout(300000)
        print('%s: starting network scan...' % modem3gpp.get_object_path())
        networks = modem3gpp.scan_sync()
        if networks:
            for network in networks:
                print('%s: %s - %s (%s, %s)' % (
                    network.get_operator_code(),
                    network.get_operator_short(),
                    network.get_operator_short(),
                    ModemManager.modem_access_technology_build_string_from_mask(
                        network.get_access_technology()),
                    ModemManager.Modem3gppNetworkAvailability.get_string(
                        network.get_availability())))
        else:
            print('no networks found')


if __name__ == "__main__":
    main()