~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to scripts/mm-test

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-17 13:54:25 UTC
  • mto: This revision was merged to the branch mainline in revision 2130.
  • Revision ID: zygmunt.krynicki@canonical.com-20130517135425-cxcenxx5t0qrtbxd
checkbox-ng: add CheckBoxNG sub-project

CheckBoxNG (or lowercase as checkbox-ng, pypi:checkbox-ng) is a clean
implementation of CheckBox on top of PlainBox. It provides a new
executable, 'checkbox' that has some of the same commands that were
previously implemented in the plainbox package.

In particular CheckBoxNG comes with the 'checkbox sru' command
(the same one as in plainbox). Later on this sub-command will be removed
from plainbox.

CheckBoxNG depends on plainbox >= 0.3

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details:
 
13
#
 
14
# Copyright (C) 2008 Novell, Inc.
 
15
# Copyright (C) 2009 Red Hat, Inc.
 
16
#
 
17
 
 
18
import sys, dbus, time, os, string, subprocess, socket
 
19
 
 
20
DBUS_INTERFACE_PROPERTIES='org.freedesktop.DBus.Properties'
 
21
MM_DBUS_SERVICE='org.freedesktop.ModemManager'
 
22
MM_DBUS_PATH='/org/freedesktop/ModemManager'
 
23
MM_DBUS_INTERFACE='org.freedesktop.ModemManager'
 
24
MM_DBUS_INTERFACE_MODEM='org.freedesktop.ModemManager.Modem'
 
25
MM_DBUS_INTERFACE_MODEM_CDMA='org.freedesktop.ModemManager.Modem.Cdma'
 
26
MM_DBUS_INTERFACE_MODEM_GSM_CARD='org.freedesktop.ModemManager.Modem.Gsm.Card'
 
27
MM_DBUS_INTERFACE_MODEM_GSM_NETWORK='org.freedesktop.ModemManager.Modem.Gsm.Network'
 
28
MM_DBUS_INTERFACE_MODEM_SIMPLE='org.freedesktop.ModemManager.Modem.Simple'
 
29
 
 
30
def get_cdma_band_class(band_class):
 
31
    if band_class == 1:
 
32
        return "800MHz"
 
33
    elif band_class == 2:
 
34
        return "1900MHz"
 
35
    else:
 
36
        return "Unknown"
 
37
 
 
38
def get_reg_state(state):
 
39
    if state == 1:
 
40
        return "registered (roaming unknown)"
 
41
    elif state == 2:
 
42
        return "registered on home network"
 
43
    elif state == 3:
 
44
        return "registered on roaming network"
 
45
    else:
 
46
        return "unknown"
 
47
 
 
48
def cdma_inspect(proxy, dump_private):
 
49
    cdma = dbus.Interface(proxy, dbus_interface=MM_DBUS_INTERFACE_MODEM_CDMA)
 
50
 
 
51
    esn = "<private>"
 
52
    if dump_private:
 
53
        try:
 
54
            esn = cdma.GetEsn()
 
55
        except dbus.exceptions.DBusException:
 
56
            esn = "<unavailable>"
 
57
 
 
58
    print("")
 
59
    print("ESN: %s" % esn)
 
60
 
 
61
    try:
 
62
        (cdma_1x_state, evdo_state) = cdma.GetRegistrationState()
 
63
        print("1x State:   %s" % get_reg_state (cdma_1x_state))
 
64
        print("EVDO State: %s" % get_reg_state (evdo_state))
 
65
    except dbus.exceptions.DBusException as e:
 
66
        print("Error reading registration state: %s" % e)
 
67
 
 
68
    try:
 
69
        quality = cdma.GetSignalQuality()
 
70
        print("Signal quality: %d" % quality)
 
71
    except dbus.exceptions.DBusException as e:
 
72
        print("Error reading signal quality: %s" % e)
 
73
 
 
74
    try:
 
75
        info = cdma.GetServingSystem()
 
76
        print("Class: %s" % get_cdma_band_class(info[0]))
 
77
        print("Band:  %s" % info[1])
 
78
        print("SID:   %d" % info[2])
 
79
    except dbus.exceptions.DBusException as e:
 
80
        print("Error reading serving system: %s" % e)
 
81
 
 
82
def cdma_connect(proxy, user, password):
 
83
    # Modem.Simple interface
 
84
    simple = dbus.Interface(proxy, dbus_interface=MM_DBUS_INTERFACE_MODEM_SIMPLE)
 
85
    try:
 
86
        simple.Connect({'number':"#777"}, timeout=92)
 
87
        print("\nConnected!")
 
88
        return True
 
89
    except Exception as e:
 
90
        print("Error connecting: %s" % e)
 
91
    return False
 
92
 
 
93
 
 
94
def get_gsm_network_mode(modem):
 
95
    mode = modem.GetNetworkMode()
 
96
    if mode == 0x0:
 
97
        mode = "Unknown"
 
98
    elif mode == 0x1:
 
99
        mode = "Any"
 
100
    elif mode == 0x2:
 
101
        mode = "GPRS"
 
102
    elif mode == 0x4:
 
103
        mode = "EDGE"
 
104
    elif mode == 0x8:
 
105
        mode = "UMTS"
 
106
    elif mode == 0x10:
 
107
        mode = "HSDPA"
 
108
    elif mode == 0x20:
 
109
        mode = "2G Preferred"
 
110
    elif mode == 0x40:
 
111
        mode = "3G Preferred"
 
112
    elif mode == 0x80:
 
113
        mode = "2G Only"
 
114
    elif mode == 0x100:
 
115
        mode = "3G Only"
 
116
    elif mode == 0x200:
 
117
        mode = "HSUPA"
 
118
    elif mode == 0x400:
 
119
        mode = "HSPA"
 
120
    else:
 
121
        mode = "(Unknown)"
 
122
 
 
123
    print("Mode: %s" % mode)
 
124
 
 
125
def get_gsm_band(modem):
 
126
    band = modem.GetBand()
 
127
    if band == 0x0:
 
128
        band = "Unknown"
 
129
    elif band == 0x1:
 
130
        band = "Any"
 
131
    elif band == 0x2:
 
132
        band = "EGSM (900 MHz)"
 
133
    elif band == 0x4:
 
134
        band = "DCS (1800 MHz)"
 
135
    elif band == 0x8:
 
136
        band = "PCS (1900 MHz)"
 
137
    elif band == 0x10:
 
138
        band = "G850 (850 MHz)"
 
139
    elif band == 0x20:
 
140
        band = "U2100 (WCSMA 2100 MHZ, Class I)"
 
141
    elif band == 0x40:
 
142
        band = "U1700 (WCDMA 3GPP UMTS1800 MHz, Class III)"
 
143
    elif band == 0x80:
 
144
        band = "17IV (WCDMA 3GPP AWS 1700/2100 MHz, Class IV)"
 
145
    elif band == 0x100:
 
146
        band = "U800 (WCDMA 3GPP UMTS800 MHz, Class VI)"
 
147
    elif band == 0x200:
 
148
        band = "U850 (WCDMA 3GPP UMT850 MHz, Class V)"
 
149
    elif band == 0x400:
 
150
        band = "U900 (WCDMA 3GPP UMTS900 MHz, Class VIII)"
 
151
    elif band == 0x800:
 
152
        band = "U17IX (WCDMA 3GPP UMTS MHz, Class IX)"
 
153
    else:
 
154
        band = "(invalid)"
 
155
 
 
156
    print("Band: %s" % band)
 
157
 
 
158
 
 
159
def gsm_inspect(proxy, dump_private, do_scan):
 
160
    # Gsm.Card interface
 
161
    card = dbus.Interface(proxy, dbus_interface=MM_DBUS_INTERFACE_MODEM_GSM_CARD)
 
162
 
 
163
    imei = "<private>"
 
164
    imsi = "<private>"
 
165
    if dump_private:
 
166
        try:
 
167
            imei = card.GetImei()
 
168
        except dbus.exceptions.DBusException:
 
169
            imei = "<unavailable>"
 
170
        try:
 
171
            imsi = card.GetImsi()
 
172
        except dbus.exceptions.DBusException:
 
173
            imsi = "<unavailable>"
 
174
 
 
175
    print("IMEI: %s" % imei)
 
176
    print("IMSI: %s" % imsi)
 
177
 
 
178
    # Gsm.Network interface
 
179
    net = dbus.Interface(proxy, dbus_interface=MM_DBUS_INTERFACE_MODEM_GSM_NETWORK)
 
180
    try:
 
181
        quality = net.GetSignalQuality()
 
182
        print("Signal quality: %d" % quality)
 
183
    except dbus.exceptions.DBusException as e:
 
184
        print("Error reading signal quality: %s" % e)
 
185
 
 
186
    if not do_scan:
 
187
        return
 
188
 
 
189
    print("Scanning...")
 
190
    try:
 
191
        results = net.Scan(timeout=120)
 
192
    except dbus.exceptions.DBusException as e:
 
193
        print("Error scanning: %s" % e)
 
194
        results = {}
 
195
 
 
196
    for r in results:
 
197
        status = r['status']
 
198
        if status == "1":
 
199
            status = "available"
 
200
        elif status == "2":
 
201
            status = "current"
 
202
        elif status == "3":
 
203
            status = "forbidden"
 
204
        else:
 
205
            status = "(Unknown)"
 
206
 
 
207
        access_tech = ""
 
208
        try:
 
209
            access_tech_num = r['access-tech']
 
210
            if access_tech_num == "0":
 
211
                access_tech = "(GSM)"
 
212
            elif access_tech_num == "1":
 
213
                access_tech = "(Compact GSM)"
 
214
            elif access_tech_num == "2":
 
215
                access_tech = "(UMTS)"
 
216
            elif access_tech_num == "3":
 
217
                access_tech = "(EDGE)"
 
218
            elif access_tech_num == "4":
 
219
                access_tech = "(HSDPA)"
 
220
            elif access_tech_num == "5":
 
221
                access_tech = "(HSUPA)"
 
222
            elif access_tech_num == "6":
 
223
                access_tech = "(HSPA)"
 
224
        except KeyError:
 
225
            pass
 
226
 
 
227
        if 'operator-long' in r and len(r['operator-long']):
 
228
            print("%s: %s %s" % (r['operator-long'], status, access_tech))
 
229
        elif 'operator-short' in r and len(r['operator-short']):
 
230
            print("%s: %s %s" % (r['operator-short'], status, access_tech))
 
231
        else:
 
232
            print("%s: %s %s" % (r['operator-num'], status, access_tech))
 
233
 
 
234
def gsm_connect(proxy, apn, user, password):
 
235
    # Modem.Simple interface
 
236
    simple = dbus.Interface(proxy, dbus_interface=MM_DBUS_INTERFACE_MODEM_SIMPLE)
 
237
    try:
 
238
        opts = {'number':"*99#"}
 
239
        if apn is not None:
 
240
            opts['apn'] = apn
 
241
        if user is not None:
 
242
            opts['username'] = user
 
243
        if password is not None:
 
244
            opts['password'] = password
 
245
        simple.Connect(opts, timeout=120)
 
246
        print("\nConnected!")
 
247
        return True
 
248
    except Exception as e:
 
249
        print("Error connecting: %s" % e)
 
250
    return False
 
251
 
 
252
def pppd_find():
 
253
    paths = ["/usr/local/sbin/pppd", "/usr/sbin/pppd", "/sbin/pppd"]
 
254
    for p in paths:
 
255
        if os.path.exists(p):
 
256
            return p
 
257
    return None
 
258
 
 
259
def ppp_start(device, user, password, tmpfile):
 
260
    path = pppd_find()
 
261
    if not path:
 
262
        return None
 
263
 
 
264
    args = [path]
 
265
    args += ["nodetach"]
 
266
    args += ["lock"]
 
267
    args += ["nodefaultroute"]
 
268
    args += ["debug"]
 
269
    if user:
 
270
        args += ["user"]
 
271
        args += [user]
 
272
    args += ["noipdefault"]
 
273
    args += ["115200"]
 
274
    args += ["noauth"]
 
275
    args += ["crtscts"]
 
276
    args += ["modem"]
 
277
    args += ["usepeerdns"]
 
278
    args += ["ipparam"]
 
279
 
 
280
    ipparam = ""
 
281
    if user:
 
282
        ipparam += user
 
283
    ipparam += "+"
 
284
    if password:
 
285
        ipparam += password
 
286
    ipparam += "+"
 
287
    ipparam += tmpfile
 
288
    args += [ipparam]
 
289
 
 
290
    args += ["plugin"]
 
291
    args += ["mm-test-pppd-plugin.so"]
 
292
 
 
293
    args += [device]
 
294
 
 
295
    return subprocess.Popen(args, close_fds=True, cwd="/", env={})
 
296
 
 
297
def ppp_wait(p, tmpfile):
 
298
    i = 0
 
299
    while p.poll() == None and i < 30:
 
300
        time.sleep(1)
 
301
        if os.path.exists(tmpfile):
 
302
            f = open(tmpfile, 'r')
 
303
            stuff = f.read(500)
 
304
            idx = string.find(stuff, "DONE")
 
305
            f.close()
 
306
            if idx >= 0:
 
307
                return True
 
308
        i += 1
 
309
    return False
 
310
 
 
311
def ppp_stop(p):
 
312
    import signal
 
313
    p.send_signal(signal.SIGTERM)
 
314
    p.wait()
 
315
 
 
316
def ntop_helper(ip):
 
317
    ip = socket.ntohl(ip)
 
318
    n1 = ip >> 24 & 0xFF
 
319
    n2 = ip >> 16 & 0xFF
 
320
    n3 = ip >> 8 & 0xFF
 
321
    n4 = ip & 0xFF
 
322
    a = "%c%c%c%c" % (n1, n2, n3, n4)
 
323
    return socket.inet_ntop(socket.AF_INET, a)
 
324
 
 
325
def static_start(iface, modem):
 
326
    (addr_num, dns1_num, dns2_num, dns3_num) = modem.GetIP4Config()
 
327
    addr = ntop_helper(addr_num)
 
328
    dns1 = ntop_helper(dns1_num)
 
329
    dns2 = ntop_helper(dns2_num)
 
330
    configure_iface(iface, addr, 0, dns1, dns2)
 
331
 
 
332
def down_iface(iface):
 
333
    ip = ["ip", "addr", "flush", "dev", iface]
 
334
    print(" ".join(ip))
 
335
    subprocess.call(ip)
 
336
    ip = ["ip", "link", "set", iface, "down"]
 
337
    print(" ".join(ip))
 
338
    subprocess.call(ip)
 
339
 
 
340
def configure_iface(iface, addr, gw, dns1, dns2):
 
341
    print("\n\n******************************")
 
342
    print("iface: %s" % iface)
 
343
    print("addr:  %s" % addr)
 
344
    print("gw:    %s" % gw)
 
345
    print("dns1:  %s" % dns1)
 
346
    print("dns2:  %s" % dns2)
 
347
 
 
348
    ifconfig = ["ifconfig", iface, "%s/32" % addr]
 
349
    if gw != 0:
 
350
        ifconfig += ["pointopoint", gw]
 
351
    print(" ".join(ifconfig))
 
352
    print("\n******************************\n")
 
353
 
 
354
    subprocess.call(ifconfig)
 
355
 
 
356
def file_configure_iface(tmpfile):
 
357
    addr = None
 
358
    gw = None
 
359
    iface = None
 
360
    dns1 = None
 
361
    dns2 = None
 
362
 
 
363
    f = open(tmpfile, 'r')
 
364
    lines = f.readlines()
 
365
    for l in lines:
 
366
        if l.startswith("addr"):
 
367
            addr = l[len("addr"):].strip()
 
368
        if l.startswith("gateway"):
 
369
            gw = l[len("gateway"):].strip()
 
370
        if l.startswith("iface"):
 
371
            iface = l[len("iface"):].strip()
 
372
        if l.startswith("dns1"):
 
373
            dns1 = l[len("dns1"):].strip()
 
374
        if l.startswith("dns2"):
 
375
            dns2 = l[len("dns2"):].strip()
 
376
    f.close()
 
377
 
 
378
    configure_iface(iface, addr, gw, dns1, dns2)
 
379
    return iface
 
380
 
 
381
def try_ping(iface):
 
382
    cmd = ["ping", "-I", iface, "-c", "4", "-i", "3", "-w", "20", "4.2.2.1"]
 
383
    print(" ".join(cmd))
 
384
    retcode = subprocess.call(cmd)
 
385
    if retcode != 0:
 
386
        print("PING: failed")
 
387
    else:
 
388
        print("PING: success")
 
389
 
 
390
 
 
391
dump_private = False
 
392
connect = False
 
393
apn = None
 
394
user = None
 
395
password = None
 
396
do_ip = False
 
397
do_scan = True
 
398
x = 1
 
399
while x < len(sys.argv):
 
400
    if sys.argv[x] == "--private":
 
401
        dump_private = True
 
402
    elif sys.argv[x] == "--connect":
 
403
        connect = True
 
404
    elif (sys.argv[x] == "--user" or sys.argv[x] == "--username"):
 
405
        x += 1
 
406
        user = sys.argv[x]
 
407
    elif sys.argv[x] == "--apn":
 
408
        x += 1
 
409
        apn = sys.argv[x]
 
410
    elif sys.argv[x] == "--password":
 
411
        x += 1
 
412
        password = sys.argv[x]
 
413
    elif sys.argv[x] == "--ip":
 
414
        do_ip = True
 
415
        if os.geteuid() != 0:
 
416
            print("You probably want to be root to use --ip")
 
417
            sys.exit(1)
 
418
    elif sys.argv[x] == "--no-scan":
 
419
        do_scan = False
 
420
    x += 1
 
421
 
 
422
bus = dbus.SystemBus()
 
423
 
 
424
# Get available modems:
 
425
manager_proxy = bus.get_object('org.freedesktop.ModemManager', '/org/freedesktop/ModemManager')
 
426
manager_iface = dbus.Interface(manager_proxy, dbus_interface='org.freedesktop.ModemManager')
 
427
modems = manager_iface.EnumerateDevices()
 
428
 
 
429
if not modems:
 
430
    print("No modems found")
 
431
    sys.exit(1)
 
432
 
 
433
for m in modems:
 
434
    connect_success = False
 
435
    data_device = None
 
436
 
 
437
    proxy = bus.get_object(MM_DBUS_SERVICE, m)
 
438
 
 
439
    # Properties
 
440
    props_iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
 
441
 
 
442
    type = props_iface.Get(MM_DBUS_INTERFACE_MODEM, 'Type')
 
443
    if type == 1:
 
444
        print("GSM modem")
 
445
    elif type == 2:
 
446
        print("CDMA modem")
 
447
    else:
 
448
        print("Invalid modem type: %d" % type)
 
449
 
 
450
    print("Driver: '%s'" % (props_iface.Get(MM_DBUS_INTERFACE_MODEM, 'Driver')))
 
451
    print("Modem device: '%s'" % (props_iface.Get(MM_DBUS_INTERFACE_MODEM, 'MasterDevice')))
 
452
    data_device = props_iface.Get(MM_DBUS_INTERFACE_MODEM, 'Device')
 
453
    print("Data device: '%s'" % data_device)
 
454
 
 
455
    # Modem interface
 
456
    modem = dbus.Interface(proxy, dbus_interface=MM_DBUS_INTERFACE_MODEM)
 
457
 
 
458
    try:
 
459
        modem.Enable(True)
 
460
    except dbus.exceptions.DBusException as e:
 
461
        print("Error enabling modem: %s" % e)
 
462
        sys.exit(1)
 
463
 
 
464
    info = modem.GetInfo()
 
465
    print("Vendor:  %s" % info[0])
 
466
    print("Model:   %s" % info[1])
 
467
    print("Version: %s" % info[2])
 
468
 
 
469
    if type == 1:
 
470
        gsm_inspect(proxy, dump_private, do_scan)
 
471
        if connect == True:
 
472
            connect_success = gsm_connect(proxy, apn, user, password)
 
473
    elif type == 2:
 
474
        cdma_inspect(proxy, dump_private)
 
475
        if connect == True:
 
476
            connect_success = cdma_connect(proxy, user, password)
 
477
    print()
 
478
 
 
479
    if connect_success and do_ip:
 
480
        tmpfile = "/tmp/mm-test-%d.tmp" % os.getpid()
 
481
        success = False
 
482
        try:
 
483
            ip_method = props_iface.Get(MM_DBUS_INTERFACE_MODEM, 'IpMethod')
 
484
            if ip_method == 0:
 
485
                # ppp
 
486
                p = ppp_start(data_device, user, password, tmpfile)
 
487
                if ppp_wait(p, tmpfile):
 
488
                    data_device = file_configure_iface(tmpfile)
 
489
                    success = True
 
490
            elif ip_method == 1:
 
491
                # static
 
492
                static_start(data_device, modem)
 
493
                success = True
 
494
            elif ip_method == 2:
 
495
                # dhcp
 
496
                pass
 
497
        except Exception as e:
 
498
            print("Error setting up IP: %s" % e)
 
499
 
 
500
        if success:
 
501
            try_ping(data_device)
 
502
            print("Waiting for 30s...")
 
503
            time.sleep(30)
 
504
 
 
505
        print("Disconnecting...")
 
506
        try:
 
507
            if ip_method == 0:
 
508
                ppp_stop(p)
 
509
                try:
 
510
                    os.remove(tmpfile)
 
511
                except:
 
512
                    pass
 
513
            elif ip_method == 1:
 
514
                # static
 
515
                down_iface(data_device)
 
516
            elif ip_method == 2:
 
517
                # dhcp
 
518
                down_iface(data_device)
 
519
 
 
520
            modem.Disconnect()
 
521
        except Exception as e:
 
522
            print("Error tearing down IP: %s" % e)
 
523
 
 
524
    time.sleep(5)
 
525
 
 
526
    modem.Enable(False)
 
527