Christopher Wiley | 2f48d95 | 2013-02-22 09:51:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # |
| 8 | # Call the Simple.Connect API. |
| 9 | # |
| 10 | |
| 11 | import gobject |
| 12 | import dbus |
| 13 | import dbus.mainloop.glib |
| 14 | import mm |
| 15 | import sys |
| 16 | from optparse import OptionParser |
| 17 | |
| 18 | def state_changed(_, new_state, _, modem_path): |
| 19 | """ |
| 20 | _ (old_state): Previous modem state (but unused). |
| 21 | new_state: New modem state. |
| 22 | _ (reason): Reason for transition (but unused). |
| 23 | modem_path: Path of the modem object for this status update. |
| 24 | """ |
| 25 | if new_state == 90: |
| 26 | print "Connection established for", modem_path |
| 27 | else: |
| 28 | print "Connection failed for", modem_path |
| 29 | mainloop.quit() |
| 30 | |
| 31 | usage = '%prog [options]' |
| 32 | |
| 33 | parser = OptionParser(usage=usage) |
| 34 | parser.add_option('--apn', type='string', |
| 35 | dest='apn', |
| 36 | default=None, |
| 37 | help='apn to supply to the connect operation') |
| 38 | parser.add_option('--username', type='string', |
| 39 | dest='username', |
| 40 | default=None, |
| 41 | help='user name to supply to the connect operation') |
| 42 | parser.add_option('--password', type='string', |
| 43 | dest='password', |
| 44 | default=None, |
| 45 | help='password to supply to the connect operation') |
| 46 | parser.add_option('--number', type='string', |
| 47 | dest='number', |
| 48 | default=None, |
| 49 | help='phone number to supply to the connect operation') |
| 50 | parser.add_option('--carrier', type='choice', |
| 51 | dest='carrier', |
| 52 | choices=['tmobile', 'sprint', 'att', 'verizon'], |
| 53 | default=None, |
| 54 | help='Specifies the carrier. Provided as a convenience ' |
| 55 | 'to avoid having to supply an APN or phone number.') |
| 56 | parser.add_option('-w', '--wait', |
| 57 | action='store_true', dest='wait', |
| 58 | default=False, |
| 59 | help='Wait until connection completes or fails ' |
| 60 | '(default: %default)') |
| 61 | |
| 62 | (options, args) = parser.parse_args() |
| 63 | if len(args) > 1: |
| 64 | parser.error("incorrect number of arguments") |
| 65 | |
| 66 | props = dict() |
| 67 | |
| 68 | carriers = dict( |
| 69 | att=dict(number="*99#", apn='broadband'), |
| 70 | tmobile=dict(number="*99#", apn='epc.tmobile.com'), |
| 71 | sprint=dict(number="#777"), |
| 72 | verizon=dict(number="#777"), |
| 73 | ) |
| 74 | |
| 75 | if len(args) < 1: |
| 76 | modem_pattern = '' |
| 77 | else: |
| 78 | modem_pattern = args[0] |
| 79 | |
| 80 | if options.carrier: |
| 81 | try: |
| 82 | props = carriers[options.carrier] |
| 83 | except KeyError: |
| 84 | parser.error("Unknown carrier '%s'." % options.carrier) |
| 85 | sys.exit(1) |
| 86 | |
| 87 | if options.apn: |
| 88 | props['apn'] = options.apn |
| 89 | if options.username: |
| 90 | props['username'] = options.username |
| 91 | if options.password: |
| 92 | props['password'] = options.password |
| 93 | if options.number: |
| 94 | props['number'] = options.number |
| 95 | |
| 96 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 97 | |
| 98 | manager, path = mm.PickOneModem(modem_pattern) |
| 99 | modem = manager.CdmaModem(path) |
| 100 | |
| 101 | print "Connecting", path |
| 102 | |
| 103 | if options.wait: |
| 104 | bus = dbus.SystemBus() |
| 105 | bus.add_signal_receiver(state_changed, |
| 106 | bus_name=manager.provider, |
| 107 | signal_name="StateChanged", |
| 108 | path_keyword="path") |
| 109 | |
| 110 | modem = manager.SimpleModem(path) |
| 111 | try: |
| 112 | modem.Connect(props) |
| 113 | options.wait = False |
| 114 | except dbus.exceptions.DBusException, e: |
| 115 | if (e.get_dbus_name() != |
| 116 | "org.chromium.ModemManager.Error.OperationInitiated"): |
| 117 | raise e |
| 118 | |
| 119 | if options.wait: |
| 120 | mainloop = gobject.MainLoop() |
| 121 | mainloop.run() |