OLD | NEW |
1 import dbus, dbus.mainloop.glib, gobject, logging, re, sys, time, subprocess | 1 import dbus, dbus.mainloop.glib, gobject, logging, re, sys, time, subprocess |
2 | 2 |
3 ssid = sys.argv[1] | 3 ssid = sys.argv[1] |
4 security = sys.argv[2] | 4 security = sys.argv[2] |
5 psk = sys.argv[3] | 5 psk = sys.argv[3] |
6 assoc_timeout = float(sys.argv[4]) | 6 assoc_timeout = float(sys.argv[4]) |
7 config_timeout = float(sys.argv[5]) | 7 config_timeout = float(sys.argv[5]) |
8 reset_timeout = float(sys.argv[6]) if len(sys.argv) > 6 else assoc_timeout | 8 reset_timeout = float(sys.argv[6]) if len(sys.argv) > 6 else assoc_timeout |
9 | 9 |
10 bus_loop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | 10 bus_loop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
(...skipping 16 matching lines...) Expand all Loading... |
27 connection_settings["Passphrase"] = psk | 27 connection_settings["Passphrase"] = psk |
28 | 28 |
29 def DbusSetup(): | 29 def DbusSetup(): |
30 try: | 30 try: |
31 path = manager.GetService((connection_settings)) | 31 path = manager.GetService((connection_settings)) |
32 service = dbus.Interface( | 32 service = dbus.Interface( |
33 bus.get_object("org.chromium.flimflam", path), | 33 bus.get_object("org.chromium.flimflam", path), |
34 "org.chromium.flimflam.Service") | 34 "org.chromium.flimflam.Service") |
35 except Exception, e: | 35 except Exception, e: |
36 print "FAIL(GetService): ssid %s exception %s" %(ssid, e) | 36 print "FAIL(GetService): ssid %s exception %s" %(ssid, e) |
37 sys.exit(1) | 37 ErrExit(1) |
38 | 38 |
39 return (path, service) | 39 return (path, service) |
40 | 40 |
41 | 41 |
42 def ParseProps(props): | 42 def ParseProps(props): |
43 proplist = [] | 43 proplist = [] |
44 if props is not None: | 44 if props is not None: |
45 for p in props: | 45 for p in props: |
46 proplist.append("'%s': '%s'" % (str(p), str(props[p]))) | 46 proplist.append("'%s': '%s'" % (str(p), str(props[p]))) |
47 return '{ %s }' % ', '.join(proplist) | 47 return '{ %s }' % ', '.join(proplist) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 time.sleep(5) | 104 time.sleep(5) |
105 return (None, 'FAIL') | 105 return (None, 'FAIL') |
106 if e.get_dbus_name() == 'org.freedesktop.DBus.Error.UnknownMethod': | 106 if e.get_dbus_name() == 'org.freedesktop.DBus.Error.UnknownMethod': |
107 # We can hope that a ResetService in the next call will solve this | 107 # We can hope that a ResetService in the next call will solve this |
108 connect_quirks['lost_dbus_connect'] = 1 | 108 connect_quirks['lost_dbus_connect'] = 1 |
109 print>>sys.stderr, "Lost the service handle during Connect()!" | 109 print>>sys.stderr, "Lost the service handle during Connect()!" |
110 time.sleep(0.5) | 110 time.sleep(0.5) |
111 return (None, 'FAIL') | 111 return (None, 'FAIL') |
112 # What is this exception? | 112 # What is this exception? |
113 print "FAIL(Connect): ssid %s DBus exception %s" %(ssid, e) | 113 print "FAIL(Connect): ssid %s DBus exception %s" %(ssid, e) |
114 sys.exit(2) | 114 ErrExit(2) |
115 except Exception, e: | 115 except Exception, e: |
116 print "FAIL(Connect): ssid %s exception %s" %(ssid, e) | 116 print "FAIL(Connect): ssid %s exception %s" %(ssid, e) |
117 sys.exit(2) | 117 ErrExit(2) |
118 | 118 |
119 properties = None | 119 properties = None |
120 # wait up to assoc_timeout seconds to associate | 120 # wait up to assoc_timeout seconds to associate |
121 while assoc_time < assoc_timeout: | 121 while assoc_time < assoc_timeout: |
122 properties = service.GetProperties() | 122 try: |
| 123 properties = service.GetProperties() |
| 124 except dbus.exceptions.DBusException, e: |
| 125 connect_quirks['get_prop'] = 1 |
| 126 print>>sys.stderr, "Got exception trying GetProperties()!" |
| 127 return (None, 'DBUSFAIL') |
123 status = properties.get("State", None) | 128 status = properties.get("State", None) |
124 # print>>sys.stderr, "time %3.1f state %s" % (assoc_time, status) | 129 # print>>sys.stderr, "time %3.1f state %s" % (assoc_time, status) |
125 if status == "failure": | 130 if status == "failure": |
126 if assoc_time == init_assoc_time: | 131 if assoc_time == init_assoc_time: |
127 connect_quirks['fast_fail'] = 1 | 132 connect_quirks['fast_fail'] = 1 |
128 print>>sys.stderr, "failure on first try! Sleep 5 seconds" | 133 print>>sys.stderr, "failure on first try! Sleep 5 seconds" |
129 time.sleep(5) | 134 time.sleep(5) |
130 return (properties, 'FAIL') | 135 return (properties, 'FAIL') |
131 if status == "configuration" or status == "ready": | 136 if status == "configuration" or status == "ready": |
132 return (properties, None) | 137 return (properties, None) |
133 time.sleep(.5) | 138 time.sleep(.5) |
134 assoc_time += .5 | 139 assoc_time += .5 |
135 if assoc_time >= assoc_timeout: | 140 if assoc_time >= assoc_timeout: |
136 if properties is None: | 141 if properties is None: |
137 properties = service.GetProperties() | 142 properties = service.GetProperties() |
138 return (properties, 'TIMEOUT') | 143 return (properties, 'TIMEOUT') |
139 sys.exit(4) | 144 ErrExit(4) |
140 | 145 |
141 | 146 |
142 # Open /var/log/messages and seek to the current end | 147 # Open /var/log/messages and seek to the current end |
143 def OpenLogs(*logfiles): | 148 def OpenLogs(*logfiles): |
144 logs = [] | 149 logs = [] |
145 for logfile in logfiles: | 150 for logfile in logfiles: |
146 try: | 151 try: |
147 msgs = open(logfile) | 152 msgs = open(logfile) |
148 msgs.seek(0, 2) | 153 msgs.seek(0, 2) |
149 logs.append({ 'name': logfile, 'file': msgs }) | 154 logs.append({ 'name': logfile, 'file': msgs }) |
150 except Exception, e: | 155 except Exception, e: |
151 # If we cannot open the file, this is not necessarily an error | 156 # If we cannot open the file, this is not necessarily an error |
152 pass | 157 pass |
153 | 158 |
154 return logs | 159 return logs |
155 | 160 |
156 | 161 |
157 def DumpLogs(logs): | 162 def DumpLogs(logs): |
158 for log in logs: | 163 for log in logs: |
159 print>>sys.stderr, "Content of %s during our run:" % log['name'] | 164 print>>sys.stderr, "Content of %s during our run:" % log['name'] |
160 print>>sys.stderr, " ))) ".join(log['file'].readlines()) | 165 print>>sys.stderr, " ))) ".join(log['file'].readlines()) |
161 | 166 |
162 print>>sys.stderr, "iw dev wlan0 scan output: %s" % \ | 167 print>>sys.stderr, "iw dev wlan0 scan output: %s" % \ |
163 subprocess.Popen(["iw", "dev", "wlan0", "scan"], | 168 subprocess.Popen(["iw", "dev", "wlan0", "scan"], |
164 stdout=subprocess.PIPE).communicate()[0] | 169 stdout=subprocess.PIPE).communicate()[0] |
165 | 170 |
| 171 def ErrExit(code): |
| 172 try: |
| 173 service.Disconnect() |
| 174 except: |
| 175 pass |
| 176 DumpLogs(logs) |
| 177 sys.exit(code) |
166 | 178 |
167 logs = OpenLogs('/var/log/messages', '/var/log/hostap.log') | 179 logs = OpenLogs('/var/log/messages', '/var/log/hostap.log') |
168 | 180 |
169 (path, service) = DbusSetup() | 181 (path, service) = DbusSetup() |
170 | 182 |
171 assoc_start = time.time() | 183 assoc_start = time.time() |
172 for attempt in range(5): | 184 for attempt in range(5): |
173 assoc_time = time.time() - assoc_start | 185 assoc_time = time.time() - assoc_start |
174 print>>sys.stderr, "connect attempt #%d %3.1f secs" % (attempt, assoc_time) | 186 print>>sys.stderr, "connect attempt #%d %3.1f secs" % (attempt, assoc_time) |
175 (properties, failure_type) = TryConnect(assoc_time) | 187 (properties, failure_type) = TryConnect(assoc_time) |
176 if failure_type is None or failure_type == 'TIMEOUT': | 188 if failure_type is None or failure_type == 'TIMEOUT': |
177 break | 189 break |
178 if failure_type == 'DBUSFAIL': | 190 if failure_type == 'DBUSFAIL': |
179 (path, service) = DbusSetup() | 191 (path, service) = DbusSetup() |
180 | 192 |
181 assoc_time = time.time() - assoc_start | 193 assoc_time = time.time() - assoc_start |
182 | 194 |
183 if attempt > 0: | 195 if attempt > 0: |
184 connect_quirks['multiple_attempts'] = 1 | 196 connect_quirks['multiple_attempts'] = 1 |
185 | 197 |
186 if failure_type is not None: | 198 if failure_type is not None: |
187 print "%s(assoc): ssid %s assoc %3.1f secs props %s" \ | 199 print "%s(assoc): ssid %s assoc %3.1f secs props %s" \ |
188 %(failure_type, ssid, assoc_time, ParseProps(properties)) | 200 %(failure_type, ssid, assoc_time, ParseProps(properties)) |
189 DumpLogs(logs) | 201 ErrExit(3) |
190 sys.exit(3) | |
191 | 202 |
192 # wait another config_timeout seconds to get an ip address | 203 # wait another config_timeout seconds to get an ip address |
193 config_time = 0 | 204 config_time = 0 |
194 status = properties.get("State", None) | 205 status = properties.get("State", None) |
195 if status != "ready": | 206 if status != "ready": |
196 while config_time < config_timeout: | 207 while config_time < config_timeout: |
197 properties = service.GetProperties() | 208 properties = service.GetProperties() |
198 status = properties.get("State", None) | 209 status = properties.get("State", None) |
199 # print>>sys.stderr, "time %3.1f state %s" % (config_time, status) | 210 # print>>sys.stderr, "time %3.1f state %s" % (config_time, status) |
200 if status == "failure": | 211 if status == "failure": |
201 print "FAIL(config): ssid %s assoc %3.1f config %3.1f secs" \ | 212 print "FAIL(config): ssid %s assoc %3.1f config %3.1f secs" \ |
202 %(ssid, assoc_time, config_time) | 213 %(ssid, assoc_time, config_time) |
203 sys.exit(5) | 214 ErrExit(5) |
204 if status == "ready": | 215 if status == "ready": |
205 break | 216 break |
206 if status != "configuration": | 217 if status != "configuration": |
207 print "FAIL(config): ssid %s assoc %3.1f config %3.1f secs *%s*" \ | 218 print "FAIL(config): ssid %s assoc %3.1f config %3.1f secs *%s*" \ |
208 %(ssid, assoc_time, config_time, status) | 219 %(ssid, assoc_time, config_time, status) |
209 break | 220 ErrExit(4) |
210 time.sleep(.5) | 221 time.sleep(.5) |
211 config_time += .5 | 222 config_time += .5 |
212 if config_time >= config_timeout: | 223 if config_time >= config_timeout: |
213 print "TIMEOUT(config): ssid %s assoc %3.1f config %3.1f secs" \ | 224 print "TIMEOUT(config): ssid %s assoc %3.1f config %3.1f secs" \ |
214 %(ssid, assoc_time, config_time) | 225 %(ssid, assoc_time, config_time) |
215 DumpLogs(logs) | 226 ErrExit(6) |
216 sys.exit(6) | |
217 | 227 |
218 print "OK %3.1f %3.1f %s (assoc and config times in sec, quirks)" \ | 228 print "OK %3.1f %3.1f %s (assoc and config times in sec, quirks)" \ |
219 %(assoc_time, config_time, str(connect_quirks.keys())) | 229 %(assoc_time, config_time, str(connect_quirks.keys())) |
| 230 |
| 231 if connect_quirks: |
| 232 DumpLogs(logs) |
220 sys.exit(0) | 233 sys.exit(0) |
OLD | NEW |