blob: aad06b37d72c3558a11e2ad21baa3bcbb7affb2f [file] [log] [blame]
Christopher Wiley2f48d952013-02-22 09:51:47 -08001#!/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
11import gobject
12import dbus
13import dbus.mainloop.glib
14import mm
15import sys
16from optparse import OptionParser
17
18def 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
31usage = '%prog [options]'
32
33parser = OptionParser(usage=usage)
34parser.add_option('--apn', type='string',
35 dest='apn',
36 default=None,
37 help='apn to supply to the connect operation')
38parser.add_option('--username', type='string',
39 dest='username',
40 default=None,
41 help='user name to supply to the connect operation')
42parser.add_option('--password', type='string',
43 dest='password',
44 default=None,
45 help='password to supply to the connect operation')
46parser.add_option('--number', type='string',
47 dest='number',
48 default=None,
49 help='phone number to supply to the connect operation')
50parser.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.')
56parser.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()
63if len(args) > 1:
64 parser.error("incorrect number of arguments")
65
66props = dict()
67
68carriers = 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
75if len(args) < 1:
76 modem_pattern = ''
77else:
78 modem_pattern = args[0]
79
80if 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
87if options.apn:
88 props['apn'] = options.apn
89if options.username:
90 props['username'] = options.username
91if options.password:
92 props['password'] = options.password
93if options.number:
94 props['number'] = options.number
95
96dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
97
98manager, path = mm.PickOneModem(modem_pattern)
99modem = manager.CdmaModem(path)
100
101print "Connecting", path
102
103if 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
110modem = manager.SimpleModem(path)
111try:
112 modem.Connect(props)
113 options.wait = False
114except dbus.exceptions.DBusException, e:
115 if (e.get_dbus_name() !=
116 "org.chromium.ModemManager.Error.OperationInitiated"):
117 raise e
118
119if options.wait:
120 mainloop = gobject.MainLoop()
121 mainloop.run()