WifiMonitor.java revision f9715cc118c446695b2a82211881c7d6c5e59761
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server.wifi; 18 19import android.net.NetworkInfo; 20import android.net.wifi.SupplicantState; 21import android.net.wifi.WifiEnterpriseConfig; 22import android.net.wifi.WifiManager; 23import android.net.wifi.WifiSsid; 24import android.net.wifi.p2p.WifiP2pConfig; 25import android.net.wifi.p2p.WifiP2pDevice; 26import android.net.wifi.p2p.WifiP2pGroup; 27import android.net.wifi.p2p.WifiP2pProvDiscEvent; 28import android.net.wifi.p2p.nsd.WifiP2pServiceResponse; 29import android.os.Message; 30import android.os.SystemClock; 31import android.text.TextUtils; 32import android.util.Log; 33 34import com.android.server.wifi.p2p.WifiP2pServiceImpl.P2pStatus; 35 36import com.android.internal.util.Protocol; 37import com.android.internal.util.StateMachine; 38 39import java.util.HashMap; 40import java.util.List; 41import java.util.regex.Matcher; 42import java.util.regex.Pattern; 43 44/** 45 * Listens for events from the wpa_supplicant server, and passes them on 46 * to the {@link StateMachine} for handling. Runs in its own thread. 47 * 48 * @hide 49 */ 50public class WifiMonitor { 51 52 private static boolean DBG = false; 53 private static final boolean VDBG = false; 54 private static final String TAG = "WifiMonitor"; 55 56 /** Events we receive from the supplicant daemon */ 57 58 private static final int CONNECTED = 1; 59 private static final int DISCONNECTED = 2; 60 private static final int STATE_CHANGE = 3; 61 private static final int SCAN_RESULTS = 4; 62 private static final int LINK_SPEED = 5; 63 private static final int TERMINATING = 6; 64 private static final int DRIVER_STATE = 7; 65 private static final int EAP_FAILURE = 8; 66 private static final int ASSOC_REJECT = 9; 67 private static final int SSID_TEMP_DISABLE = 10; 68 private static final int SSID_REENABLE = 11; 69 private static final int BSS_ADDED = 12; 70 private static final int BSS_REMOVED = 13; 71 private static final int UNKNOWN = 14; 72 73 /** All events coming from the supplicant start with this prefix */ 74 private static final String EVENT_PREFIX_STR = "CTRL-EVENT-"; 75 private static final int EVENT_PREFIX_LEN_STR = EVENT_PREFIX_STR.length(); 76 77 /** All events coming from the supplicant start with this prefix */ 78 private static final String REQUEST_PREFIX_STR = "CTRL-REQ-"; 79 private static final int REQUEST_PREFIX_LEN_STR = REQUEST_PREFIX_STR.length(); 80 81 82 /** All WPA events coming from the supplicant start with this prefix */ 83 private static final String WPA_EVENT_PREFIX_STR = "WPA:"; 84 private static final String PASSWORD_MAY_BE_INCORRECT_STR = 85 "pre-shared key may be incorrect"; 86 87 /* WPS events */ 88 private static final String WPS_SUCCESS_STR = "WPS-SUCCESS"; 89 90 /* Format: WPS-FAIL msg=%d [config_error=%d] [reason=%d (%s)] */ 91 private static final String WPS_FAIL_STR = "WPS-FAIL"; 92 private static final String WPS_FAIL_PATTERN = 93 "WPS-FAIL msg=\\d+(?: config_error=(\\d+))?(?: reason=(\\d+))?"; 94 95 /* config error code values for config_error=%d */ 96 private static final int CONFIG_MULTIPLE_PBC_DETECTED = 12; 97 private static final int CONFIG_AUTH_FAILURE = 18; 98 99 /* reason code values for reason=%d */ 100 private static final int REASON_TKIP_ONLY_PROHIBITED = 1; 101 private static final int REASON_WEP_PROHIBITED = 2; 102 103 private static final String WPS_OVERLAP_STR = "WPS-OVERLAP-DETECTED"; 104 private static final String WPS_TIMEOUT_STR = "WPS-TIMEOUT"; 105 106 /* Hotspot 2.0 ANQP query events */ 107 private static final String GAS_QUERY_PREFIX_STR = "GAS-QUERY-"; 108 private static final String GAS_QUERY_START_STR = "GAS-QUERY-START"; 109 private static final String GAS_QUERY_DONE_STR = "GAS-QUERY-DONE"; 110 private static final String RX_HS20_ANQP_ICON_STR = "RX-HS20-ANQP-ICON"; 111 private static final int RX_HS20_ANQP_ICON_STR_LEN = RX_HS20_ANQP_ICON_STR.length(); 112 113 /* Hotspot 2.0 events */ 114 private static final String HS20_PREFIX_STR = "HS20-"; 115 private static final String HS20_SUB_REM_STR = "HS20-SUBSCRIPTION-REMEDIATION"; 116 private static final String HS20_DEAUTH_STR = "HS20-DEAUTH-IMMINENT-NOTICE"; 117 118 private static final String IDENTITY_STR = "IDENTITY"; 119 120 private static final String SIM_STR = "SIM"; 121 122 123 //used to debug and detect if we miss an event 124 private static int eventLogCounter = 0; 125 126 /** 127 * Names of events from wpa_supplicant (minus the prefix). In the 128 * format descriptions, * "<code>x</code>" 129 * designates a dynamic value that needs to be parsed out from the event 130 * string 131 */ 132 /** 133 * <pre> 134 * CTRL-EVENT-CONNECTED - Connection to xx:xx:xx:xx:xx:xx completed 135 * </pre> 136 * <code>xx:xx:xx:xx:xx:xx</code> is the BSSID of the associated access point 137 */ 138 private static final String CONNECTED_STR = "CONNECTED"; 139 /** 140 * <pre> 141 * CTRL-EVENT-DISCONNECTED - Disconnect event - remove keys 142 * </pre> 143 */ 144 private static final String DISCONNECTED_STR = "DISCONNECTED"; 145 /** 146 * <pre> 147 * CTRL-EVENT-STATE-CHANGE x 148 * </pre> 149 * <code>x</code> is the numerical value of the new state. 150 */ 151 private static final String STATE_CHANGE_STR = "STATE-CHANGE"; 152 /** 153 * <pre> 154 * CTRL-EVENT-SCAN-RESULTS ready 155 * </pre> 156 */ 157 private static final String SCAN_RESULTS_STR = "SCAN-RESULTS"; 158 159 /** 160 * <pre> 161 * CTRL-EVENT-LINK-SPEED x Mb/s 162 * </pre> 163 * {@code x} is the link speed in Mb/sec. 164 */ 165 private static final String LINK_SPEED_STR = "LINK-SPEED"; 166 /** 167 * <pre> 168 * CTRL-EVENT-TERMINATING - signal x 169 * </pre> 170 * <code>x</code> is the signal that caused termination. 171 */ 172 private static final String TERMINATING_STR = "TERMINATING"; 173 /** 174 * <pre> 175 * CTRL-EVENT-DRIVER-STATE state 176 * </pre> 177 * <code>state</code> can be HANGED 178 */ 179 private static final String DRIVER_STATE_STR = "DRIVER-STATE"; 180 /** 181 * <pre> 182 * CTRL-EVENT-EAP-FAILURE EAP authentication failed 183 * </pre> 184 */ 185 private static final String EAP_FAILURE_STR = "EAP-FAILURE"; 186 187 /** 188 * This indicates an authentication failure on EAP FAILURE event 189 */ 190 private static final String EAP_AUTH_FAILURE_STR = "EAP authentication failed"; 191 192 /** 193 * This indicates an assoc reject event 194 */ 195 private static final String ASSOC_REJECT_STR = "ASSOC-REJECT"; 196 197 /** 198 * This indicates auth or association failure bad enough so as network got disabled 199 * - WPA_PSK auth failure suspecting shared key mismatch 200 * - failed multiple Associations 201 */ 202 private static final String TEMP_DISABLED_STR = "SSID-TEMP-DISABLED"; 203 204 /** 205 * This indicates a previously disabled SSID was reenabled by supplicant 206 */ 207 private static final String REENABLED_STR = "SSID-REENABLED"; 208 209 /** 210 * This indicates supplicant found a given BSS 211 */ 212 private static final String BSS_ADDED_STR = "BSS-ADDED"; 213 214 /** 215 * This indicates supplicant removed a given BSS 216 */ 217 private static final String BSS_REMOVED_STR = "BSS-REMOVED"; 218 219 /** 220 * Regex pattern for extracting an Ethernet-style MAC address from a string. 221 * Matches a strings like the following:<pre> 222 * CTRL-EVENT-CONNECTED - Connection to 00:1e:58:ec:d5:6d completed (reauth) [id=1 id_str=]</pre> 223 */ 224 private static Pattern mConnectedEventPattern = 225 Pattern.compile("((?:[0-9a-f]{2}:){5}[0-9a-f]{2}) .* \\[id=([0-9]+) "); 226 227 /** 228 * Regex pattern for extracting an Ethernet-style MAC address from a string. 229 * Matches a strings like the following:<pre> 230 * CTRL-EVENT-DISCONNECTED - bssid=ac:22:0b:24:70:74 reason=3 locally_generated=1 231 */ 232 private static Pattern mDisconnectedEventPattern = 233 Pattern.compile("((?:[0-9a-f]{2}:){5}[0-9a-f]{2}) +" + 234 "reason=([0-9]+) +locally_generated=([0-1])"); 235 236 /** 237 * Regex pattern for extracting an Ethernet-style MAC address from a string. 238 * Matches a strings like the following:<pre> 239 * CTRL-EVENT-ASSOC-REJECT - bssid=ac:22:0b:24:70:74 status_code=1 240 */ 241 private static Pattern mAssocRejectEventPattern = 242 Pattern.compile("((?:[0-9a-f]{2}:){5}[0-9a-f]{2}) +" + 243 "status_code=([0-9]+)"); 244 245 /** 246 * Regex pattern for extracting an Ethernet-style MAC address from a string. 247 * Matches a strings like the following:<pre> 248 * IFNAME=wlan0 Trying to associate with 6c:f3:7f:ae:87:71 249 */ 250 private static final String TARGET_BSSID_STR = "Trying to associate with "; 251 252 private static Pattern mTargetBSSIDPattern = 253 Pattern.compile("Trying to associate with ((?:[0-9a-f]{2}:){5}[0-9a-f]{2}).*"); 254 255 /** 256 * Regex pattern for extracting an Ethernet-style MAC address from a string. 257 * Matches a strings like the following:<pre> 258 * IFNAME=wlan0 Associated with 6c:f3:7f:ae:87:71 259 */ 260 private static final String ASSOCIATED_WITH_STR = "Associated with "; 261 262 private static Pattern mAssociatedPattern = 263 Pattern.compile("Associated with ((?:[0-9a-f]{2}:){5}[0-9a-f]{2}).*"); 264 265 /** 266 * Regex pattern for extracting SSIDs from request identity string. 267 * Matches a strings like the following:<pre> 268 * CTRL-REQ-SIM-<network id>:GSM-AUTH:<RAND1>:<RAND2>[:<RAND3>] needed for SSID <SSID> 269 * This pattern should find 270 * 0 - id 271 * 1 - Rand1 272 * 2 - Rand2 273 * 3 - Rand3 274 * 4 - SSID 275 */ 276 private static Pattern mRequestGsmAuthPattern = 277 Pattern.compile("SIM-([0-9]*):GSM-AUTH((:[0-9a-f]+)+) needed for SSID (.+)"); 278 279 /** 280 * Regex pattern for extracting SSIDs from request identity string. 281 * Matches a strings like the following:<pre> 282 * CTRL-REQ-IDENTITY-xx:Identity needed for SSID XXXX</pre> 283 */ 284 private static Pattern mRequestIdentityPattern = 285 Pattern.compile("IDENTITY-([0-9]+):Identity needed for SSID (.+)"); 286 287 /** P2P events */ 288 private static final String P2P_EVENT_PREFIX_STR = "P2P"; 289 290 /* P2P-DEVICE-FOUND fa:7b:7a:42:02:13 p2p_dev_addr=fa:7b:7a:42:02:13 pri_dev_type=1-0050F204-1 291 name='p2p-TEST1' config_methods=0x188 dev_capab=0x27 group_capab=0x0 */ 292 private static final String P2P_DEVICE_FOUND_STR = "P2P-DEVICE-FOUND"; 293 294 /* P2P-DEVICE-LOST p2p_dev_addr=42:fc:89:e1:e2:27 */ 295 private static final String P2P_DEVICE_LOST_STR = "P2P-DEVICE-LOST"; 296 297 /* P2P-FIND-STOPPED */ 298 private static final String P2P_FIND_STOPPED_STR = "P2P-FIND-STOPPED"; 299 300 /* P2P-GO-NEG-REQUEST 42:fc:89:a8:96:09 dev_passwd_id=4 */ 301 private static final String P2P_GO_NEG_REQUEST_STR = "P2P-GO-NEG-REQUEST"; 302 303 private static final String P2P_GO_NEG_SUCCESS_STR = "P2P-GO-NEG-SUCCESS"; 304 305 /* P2P-GO-NEG-FAILURE status=x */ 306 private static final String P2P_GO_NEG_FAILURE_STR = "P2P-GO-NEG-FAILURE"; 307 308 private static final String P2P_GROUP_FORMATION_SUCCESS_STR = 309 "P2P-GROUP-FORMATION-SUCCESS"; 310 311 private static final String P2P_GROUP_FORMATION_FAILURE_STR = 312 "P2P-GROUP-FORMATION-FAILURE"; 313 314 /* P2P-GROUP-STARTED p2p-wlan0-0 [client|GO] ssid="DIRECT-W8" freq=2437 315 [psk=2182b2e50e53f260d04f3c7b25ef33c965a3291b9b36b455a82d77fd82ca15bc|passphrase="fKG4jMe3"] 316 go_dev_addr=fa:7b:7a:42:02:13 [PERSISTENT] */ 317 private static final String P2P_GROUP_STARTED_STR = "P2P-GROUP-STARTED"; 318 319 /* P2P-GROUP-REMOVED p2p-wlan0-0 [client|GO] reason=REQUESTED */ 320 private static final String P2P_GROUP_REMOVED_STR = "P2P-GROUP-REMOVED"; 321 322 /* P2P-INVITATION-RECEIVED sa=fa:7b:7a:42:02:13 go_dev_addr=f8:7b:7a:42:02:13 323 bssid=fa:7b:7a:42:82:13 unknown-network */ 324 private static final String P2P_INVITATION_RECEIVED_STR = "P2P-INVITATION-RECEIVED"; 325 326 /* P2P-INVITATION-RESULT status=1 */ 327 private static final String P2P_INVITATION_RESULT_STR = "P2P-INVITATION-RESULT"; 328 329 /* P2P-PROV-DISC-PBC-REQ 42:fc:89:e1:e2:27 p2p_dev_addr=42:fc:89:e1:e2:27 330 pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 331 group_capab=0x0 */ 332 private static final String P2P_PROV_DISC_PBC_REQ_STR = "P2P-PROV-DISC-PBC-REQ"; 333 334 /* P2P-PROV-DISC-PBC-RESP 02:12:47:f2:5a:36 */ 335 private static final String P2P_PROV_DISC_PBC_RSP_STR = "P2P-PROV-DISC-PBC-RESP"; 336 337 /* P2P-PROV-DISC-ENTER-PIN 42:fc:89:e1:e2:27 p2p_dev_addr=42:fc:89:e1:e2:27 338 pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 339 group_capab=0x0 */ 340 private static final String P2P_PROV_DISC_ENTER_PIN_STR = "P2P-PROV-DISC-ENTER-PIN"; 341 /* P2P-PROV-DISC-SHOW-PIN 42:fc:89:e1:e2:27 44490607 p2p_dev_addr=42:fc:89:e1:e2:27 342 pri_dev_type=1-0050F204-1 name='p2p-TEST2' config_methods=0x188 dev_capab=0x27 343 group_capab=0x0 */ 344 private static final String P2P_PROV_DISC_SHOW_PIN_STR = "P2P-PROV-DISC-SHOW-PIN"; 345 /* P2P-PROV-DISC-FAILURE p2p_dev_addr=42:fc:89:e1:e2:27 */ 346 private static final String P2P_PROV_DISC_FAILURE_STR = "P2P-PROV-DISC-FAILURE"; 347 348 /* 349 * Protocol format is as follows.<br> 350 * See the Table.62 in the WiFi Direct specification for the detail. 351 * ______________________________________________________________ 352 * | Length(2byte) | Type(1byte) | TransId(1byte)}| 353 * ______________________________________________________________ 354 * | status(1byte) | vendor specific(variable) | 355 * 356 * P2P-SERV-DISC-RESP 42:fc:89:e1:e2:27 1 0300000101 357 * length=3, service type=0(ALL Service), transaction id=1, 358 * status=1(service protocol type not available)<br> 359 * 360 * P2P-SERV-DISC-RESP 42:fc:89:e1:e2:27 1 0300020201 361 * length=3, service type=2(UPnP), transaction id=2, 362 * status=1(service protocol type not available) 363 * 364 * P2P-SERV-DISC-RESP 42:fc:89:e1:e2:27 1 990002030010757569643a3131323 365 * 2646534652d383537342d353961622d393332322d3333333435363738393034343a3 366 * a75726e3a736368656d61732d75706e702d6f72673a736572766963653a436f6e746 367 * 56e744469726563746f72793a322c757569643a36383539646564652d383537342d3 368 * 53961622d393333322d3132333435363738393031323a3a75706e703a726f6f74646 369 * 576696365 370 * length=153,type=2(UPnP),transaction id=3,status=0 371 * 372 * UPnP Protocol format is as follows. 373 * ______________________________________________________ 374 * | Version (1) | USN (Variable) | 375 * 376 * version=0x10(UPnP1.0) data=usn:uuid:1122de4e-8574-59ab-9322-33345678 377 * 9044::urn:schemas-upnp-org:service:ContentDirectory:2,usn:uuid:6859d 378 * ede-8574-59ab-9332-123456789012::upnp:rootdevice 379 * 380 * P2P-SERV-DISC-RESP 58:17:0c:bc:dd:ca 21 1900010200045f6970 381 * 70c00c000c01094d795072696e746572c027 382 * length=25, type=1(Bonjour),transaction id=2,status=0 383 * 384 * Bonjour Protocol format is as follows. 385 * __________________________________________________________ 386 * |DNS Name(Variable)|DNS Type(1)|Version(1)|RDATA(Variable)| 387 * 388 * DNS Name=_ipp._tcp.local.,DNS type=12(PTR), Version=1, 389 * RDATA=MyPrinter._ipp._tcp.local. 390 * 391 */ 392 private static final String P2P_SERV_DISC_RESP_STR = "P2P-SERV-DISC-RESP"; 393 394 private static final String HOST_AP_EVENT_PREFIX_STR = "AP"; 395 /* AP-STA-CONNECTED 42:fc:89:a8:96:09 dev_addr=02:90:4c:a0:92:54 */ 396 private static final String AP_STA_CONNECTED_STR = "AP-STA-CONNECTED"; 397 /* AP-STA-DISCONNECTED 42:fc:89:a8:96:09 */ 398 private static final String AP_STA_DISCONNECTED_STR = "AP-STA-DISCONNECTED"; 399 400 /* Supplicant events reported to a state machine */ 401 private static final int BASE = Protocol.BASE_WIFI_MONITOR; 402 403 /* Connection to supplicant established */ 404 public static final int SUP_CONNECTION_EVENT = BASE + 1; 405 /* Connection to supplicant lost */ 406 public static final int SUP_DISCONNECTION_EVENT = BASE + 2; 407 /* Network connection completed */ 408 public static final int NETWORK_CONNECTION_EVENT = BASE + 3; 409 /* Network disconnection completed */ 410 public static final int NETWORK_DISCONNECTION_EVENT = BASE + 4; 411 /* Scan results are available */ 412 public static final int SCAN_RESULTS_EVENT = BASE + 5; 413 /* Supplicate state changed */ 414 public static final int SUPPLICANT_STATE_CHANGE_EVENT = BASE + 6; 415 /* Password failure and EAP authentication failure */ 416 public static final int AUTHENTICATION_FAILURE_EVENT = BASE + 7; 417 /* WPS success detected */ 418 public static final int WPS_SUCCESS_EVENT = BASE + 8; 419 /* WPS failure detected */ 420 public static final int WPS_FAIL_EVENT = BASE + 9; 421 /* WPS overlap detected */ 422 public static final int WPS_OVERLAP_EVENT = BASE + 10; 423 /* WPS timeout detected */ 424 public static final int WPS_TIMEOUT_EVENT = BASE + 11; 425 /* Driver was hung */ 426 public static final int DRIVER_HUNG_EVENT = BASE + 12; 427 /* SSID was disabled due to auth failure or excessive 428 * connection failures */ 429 public static final int SSID_TEMP_DISABLED = BASE + 13; 430 /* SSID reenabled by supplicant */ 431 public static final int SSID_REENABLED = BASE + 14; 432 433 /* Request Identity */ 434 public static final int SUP_REQUEST_IDENTITY = BASE + 15; 435 436 /* Request SIM Auth */ 437 public static final int SUP_REQUEST_SIM_AUTH = BASE + 16; 438 439 /* P2P events */ 440 public static final int P2P_DEVICE_FOUND_EVENT = BASE + 21; 441 public static final int P2P_DEVICE_LOST_EVENT = BASE + 22; 442 public static final int P2P_GO_NEGOTIATION_REQUEST_EVENT = BASE + 23; 443 public static final int P2P_GO_NEGOTIATION_SUCCESS_EVENT = BASE + 25; 444 public static final int P2P_GO_NEGOTIATION_FAILURE_EVENT = BASE + 26; 445 public static final int P2P_GROUP_FORMATION_SUCCESS_EVENT = BASE + 27; 446 public static final int P2P_GROUP_FORMATION_FAILURE_EVENT = BASE + 28; 447 public static final int P2P_GROUP_STARTED_EVENT = BASE + 29; 448 public static final int P2P_GROUP_REMOVED_EVENT = BASE + 30; 449 public static final int P2P_INVITATION_RECEIVED_EVENT = BASE + 31; 450 public static final int P2P_INVITATION_RESULT_EVENT = BASE + 32; 451 public static final int P2P_PROV_DISC_PBC_REQ_EVENT = BASE + 33; 452 public static final int P2P_PROV_DISC_PBC_RSP_EVENT = BASE + 34; 453 public static final int P2P_PROV_DISC_ENTER_PIN_EVENT = BASE + 35; 454 public static final int P2P_PROV_DISC_SHOW_PIN_EVENT = BASE + 36; 455 public static final int P2P_FIND_STOPPED_EVENT = BASE + 37; 456 public static final int P2P_SERV_DISC_RESP_EVENT = BASE + 38; 457 public static final int P2P_PROV_DISC_FAILURE_EVENT = BASE + 39; 458 459 /* hostap events */ 460 public static final int AP_STA_DISCONNECTED_EVENT = BASE + 41; 461 public static final int AP_STA_CONNECTED_EVENT = BASE + 42; 462 463 /* Indicates assoc reject event */ 464 public static final int ASSOCIATION_REJECTION_EVENT = BASE + 43; 465 466 /* hotspot 2.0 ANQP events */ 467 public static final int GAS_QUERY_START_EVENT = BASE + 51; 468 public static final int GAS_QUERY_DONE_EVENT = BASE + 52; 469 public static final int RX_HS20_ANQP_ICON_EVENT = BASE + 53; 470 471 /* hotspot 2.0 events */ 472 public static final int HS20_REMEDIATION_EVENT = BASE + 61; 473 public static final int HS20_DEAUTH_EVENT = BASE + 62; 474 475 /** 476 * This indicates a read error on the monitor socket conenction 477 */ 478 private static final String WPA_RECV_ERROR_STR = "recv error"; 479 480 /** 481 * Max errors before we close supplicant connection 482 */ 483 private static final int MAX_RECV_ERRORS = 10; 484 485 private final String mInterfaceName; 486 private final WifiNative mWifiNative; 487 private final StateMachine mStateMachine; 488 private StateMachine mStateMachine2; 489 private boolean mMonitoring; 490 491 // This is a global counter, since it's not monitor specific. However, the existing 492 // implementation forwards all "global" control events like CTRL-EVENT-TERMINATING 493 // to the p2p0 monitor. Is that expected ? It seems a bit surprising. 494 // 495 // TODO: If the p2p0 monitor isn't registered, the behaviour is even more surprising. 496 // The event will be dispatched to all monitors, and each of them will end up incrementing 497 // it in their dispatchXXX method. If we have 5 registered monitors (say), 2 consecutive 498 // recv errors will cause us to disconnect from the supplicant (instead of the intended 10). 499 // 500 // This variable is always accessed and modified under a WifiMonitorSingleton lock. 501 private static int sRecvErrors; 502 503 public WifiMonitor(StateMachine stateMachine, WifiNative wifiNative) { 504 if (DBG) Log.d(TAG, "Creating WifiMonitor"); 505 mWifiNative = wifiNative; 506 mInterfaceName = wifiNative.mInterfaceName; 507 mStateMachine = stateMachine; 508 mStateMachine2 = null; 509 mMonitoring = false; 510 511 WifiMonitorSingleton.sInstance.registerInterfaceMonitor(mInterfaceName, this); 512 } 513 514 void enableVerboseLogging(int verbose) { 515 if (verbose > 0) { 516 DBG = true; 517 } else { 518 DBG = false; 519 } 520 } 521 522 // TODO: temporary hack, should be handle by supplicant manager (new component in future) 523 public void setStateMachine2(StateMachine stateMachine) { 524 mStateMachine2 = stateMachine; 525 } 526 527 public void startMonitoring() { 528 WifiMonitorSingleton.sInstance.startMonitoring(mInterfaceName); 529 } 530 531 public void stopMonitoring() { 532 WifiMonitorSingleton.sInstance.stopMonitoring(mInterfaceName); 533 } 534 535 public void stopSupplicant() { 536 WifiMonitorSingleton.sInstance.stopSupplicant(); 537 } 538 539 public void killSupplicant(boolean p2pSupported) { 540 WifiMonitorSingleton.sInstance.killSupplicant(p2pSupported); 541 } 542 543 private static class WifiMonitorSingleton { 544 private static final WifiMonitorSingleton sInstance = new WifiMonitorSingleton(); 545 546 private final HashMap<String, WifiMonitor> mIfaceMap = new HashMap<String, WifiMonitor>(); 547 private boolean mConnected = false; 548 private WifiNative mWifiNative; 549 550 private WifiMonitorSingleton() { 551 } 552 553 public synchronized void startMonitoring(String iface) { 554 WifiMonitor m = mIfaceMap.get(iface); 555 if (m == null) { 556 Log.e(TAG, "startMonitor called with unknown iface=" + iface); 557 return; 558 } 559 560 Log.d(TAG, "startMonitoring(" + iface + ") with mConnected = " + mConnected); 561 562 if (mConnected) { 563 m.mMonitoring = true; 564 m.mStateMachine.sendMessage(SUP_CONNECTION_EVENT); 565 } else { 566 if (DBG) Log.d(TAG, "connecting to supplicant"); 567 int connectTries = 0; 568 while (true) { 569 if (mWifiNative.connectToSupplicant()) { 570 m.mMonitoring = true; 571 m.mStateMachine.sendMessage(SUP_CONNECTION_EVENT); 572 new MonitorThread(mWifiNative, this).start(); 573 mConnected = true; 574 break; 575 } 576 if (connectTries++ < 5) { 577 try { 578 Thread.sleep(1000); 579 } catch (InterruptedException ignore) { 580 } 581 } else { 582 mIfaceMap.remove(iface); 583 m.mStateMachine.sendMessage(SUP_DISCONNECTION_EVENT); 584 Log.e(TAG, "startMonitoring(" + iface + ") failed!"); 585 break; 586 } 587 } 588 } 589 } 590 591 public synchronized void stopMonitoring(String iface) { 592 WifiMonitor m = mIfaceMap.get(iface); 593 if (DBG) Log.d(TAG, "stopMonitoring(" + iface + ") = " + m.mStateMachine); 594 m.mMonitoring = false; 595 m.mStateMachine.sendMessage(SUP_DISCONNECTION_EVENT); 596 } 597 598 public synchronized void registerInterfaceMonitor(String iface, WifiMonitor m) { 599 if (DBG) Log.d(TAG, "registerInterface(" + iface + "+" + m.mStateMachine + ")"); 600 mIfaceMap.put(iface, m); 601 if (mWifiNative == null) { 602 mWifiNative = m.mWifiNative; 603 } 604 } 605 606 public synchronized void unregisterInterfaceMonitor(String iface) { 607 // REVIEW: When should we call this? If this isn't called, then WifiMonitor 608 // objects will remain in the mIfaceMap; and won't ever get deleted 609 610 WifiMonitor m = mIfaceMap.remove(iface); 611 if (DBG) Log.d(TAG, "unregisterInterface(" + iface + "+" + m.mStateMachine + ")"); 612 } 613 614 public synchronized void stopSupplicant() { 615 mWifiNative.stopSupplicant(); 616 } 617 618 public synchronized void killSupplicant(boolean p2pSupported) { 619 WifiNative.killSupplicant(p2pSupported); 620 mConnected = false; 621 for (WifiMonitor m : mIfaceMap.values()) { 622 m.mMonitoring = false; 623 } 624 } 625 626 private synchronized boolean dispatchEvent(String eventStr) { 627 String iface; 628 if (eventStr.startsWith("IFNAME=")) { 629 int space = eventStr.indexOf(' '); 630 if (space != -1) { 631 iface = eventStr.substring(7, space); 632 if (!mIfaceMap.containsKey(iface) && iface.startsWith("p2p-")) { 633 // p2p interfaces are created dynamically, but we have 634 // only one P2p state machine monitoring all of them; look 635 // for it explicitly, and send messages there .. 636 iface = "p2p0"; 637 } 638 eventStr = eventStr.substring(space + 1); 639 } else { 640 // No point dispatching this event to any interface, the dispatched 641 // event string will begin with "IFNAME=" which dispatchEvent can't really 642 // do anything about. 643 Log.e(TAG, "Dropping malformed event (unparsable iface): " + eventStr); 644 return false; 645 } 646 } else { 647 // events without prefix belong to p2p0 monitor 648 iface = "p2p0"; 649 } 650 651 if (VDBG) Log.d(TAG, "Dispatching event to interface: " + iface); 652 653 WifiMonitor m = mIfaceMap.get(iface); 654 if (m != null) { 655 if (m.mMonitoring) { 656 if (m.dispatchEvent(eventStr, iface)) { 657 mConnected = false; 658 return true; 659 } 660 661 return false; 662 } else { 663 if (DBG) Log.d(TAG, "Dropping event because (" + iface + ") is stopped"); 664 return false; 665 } 666 } else { 667 if (DBG) Log.d(TAG, "Sending to all monitors because there's no matching iface"); 668 boolean done = false; 669 boolean isMonitoring = false; 670 boolean isTerminating = false; 671 if (eventStr.startsWith(EVENT_PREFIX_STR) 672 && eventStr.contains(TERMINATING_STR)) { 673 isTerminating = true; 674 } 675 for (WifiMonitor monitor : mIfaceMap.values()) { 676 if (monitor.mMonitoring) { 677 isMonitoring = true; 678 if (monitor.dispatchEvent(eventStr, iface)) { 679 done = true; 680 } 681 } 682 } 683 684 if (!isMonitoring && isTerminating) { 685 done = true; 686 } 687 688 if (done) { 689 mConnected = false; 690 } 691 692 return done; 693 } 694 } 695 } 696 697 private static class MonitorThread extends Thread { 698 private final WifiNative mWifiNative; 699 private final WifiMonitorSingleton mWifiMonitorSingleton; 700 701 public MonitorThread(WifiNative wifiNative, WifiMonitorSingleton wifiMonitorSingleton) { 702 super("WifiMonitor"); 703 mWifiNative = wifiNative; 704 mWifiMonitorSingleton = wifiMonitorSingleton; 705 } 706 707 public void run() { 708 //noinspection InfiniteLoopStatement 709 for (;;) { 710 String eventStr = mWifiNative.waitForEvent(); 711 712 // Skip logging the common but mostly uninteresting scan-results event 713 if (DBG && eventStr.indexOf(SCAN_RESULTS_STR) == -1) { 714 Log.d(TAG, "Event [" + eventStr + "]"); 715 } 716 717 if (mWifiMonitorSingleton.dispatchEvent(eventStr)) { 718 if (DBG) Log.d(TAG, "Disconnecting from the supplicant, no more events"); 719 break; 720 } 721 } 722 } 723 } 724 725 private void logDbg(String debug) { 726 Log.e(TAG, debug/*+ " stack:" + Thread.currentThread().getStackTrace()[2].getMethodName() 727 +" - "+ Thread.currentThread().getStackTrace()[3].getMethodName() 728 +" - "+ Thread.currentThread().getStackTrace()[4].getMethodName() 729 +" - "+ Thread.currentThread().getStackTrace()[5].getMethodName()*/); 730 } 731 732 /* @return true if the event was supplicant disconnection */ 733 private boolean dispatchEvent(String eventStr, String iface) { 734 735 if (DBG) { 736 // Dont log CTRL-EVENT-BSS-ADDED which are too verbose and not handled 737 if (eventStr != null && !eventStr.contains("CTRL-EVENT-BSS-ADDED")) { 738 logDbg("WifiMonitor:" + iface + " cnt=" + Integer.toString(eventLogCounter) 739 + " dispatchEvent: " + eventStr); 740 } 741 } 742 743 if (!eventStr.startsWith(EVENT_PREFIX_STR)) { 744 if (eventStr.startsWith(WPA_EVENT_PREFIX_STR) && 745 0 < eventStr.indexOf(PASSWORD_MAY_BE_INCORRECT_STR)) { 746 mStateMachine.sendMessage(AUTHENTICATION_FAILURE_EVENT, eventLogCounter); 747 } else if (eventStr.startsWith(WPS_SUCCESS_STR)) { 748 mStateMachine.sendMessage(WPS_SUCCESS_EVENT); 749 } else if (eventStr.startsWith(WPS_FAIL_STR)) { 750 handleWpsFailEvent(eventStr); 751 } else if (eventStr.startsWith(WPS_OVERLAP_STR)) { 752 mStateMachine.sendMessage(WPS_OVERLAP_EVENT); 753 } else if (eventStr.startsWith(WPS_TIMEOUT_STR)) { 754 mStateMachine.sendMessage(WPS_TIMEOUT_EVENT); 755 } else if (eventStr.startsWith(P2P_EVENT_PREFIX_STR)) { 756 handleP2pEvents(eventStr); 757 } else if (eventStr.startsWith(HOST_AP_EVENT_PREFIX_STR)) { 758 handleHostApEvents(eventStr); 759 } else if (eventStr.startsWith(GAS_QUERY_PREFIX_STR)) { 760 handleGasQueryEvents(eventStr); 761 } else if (eventStr.startsWith(RX_HS20_ANQP_ICON_STR)) { 762 if (mStateMachine2 != null) 763 mStateMachine2.sendMessage(RX_HS20_ANQP_ICON_EVENT, 764 eventStr.substring(RX_HS20_ANQP_ICON_STR_LEN + 1)); 765 } else if (eventStr.startsWith(HS20_PREFIX_STR)) { 766 handleHs20Events(eventStr); 767 } else if (eventStr.startsWith(REQUEST_PREFIX_STR)) { 768 handleRequests(eventStr); 769 } else if (eventStr.startsWith(TARGET_BSSID_STR)) { 770 handleTargetBSSIDEvent(eventStr); 771 } else if (eventStr.startsWith(ASSOCIATED_WITH_STR)) { 772 handleAssociatedBSSIDEvent(eventStr); 773 } else { 774 if (DBG) Log.w(TAG, "couldn't identify event type - " + eventStr); 775 } 776 eventLogCounter++; 777 return false; 778 } 779 780 String eventName = eventStr.substring(EVENT_PREFIX_LEN_STR); 781 int nameEnd = eventName.indexOf(' '); 782 if (nameEnd != -1) 783 eventName = eventName.substring(0, nameEnd); 784 if (eventName.length() == 0) { 785 if (DBG) Log.i(TAG, "Received wpa_supplicant event with empty event name"); 786 eventLogCounter++; 787 return false; 788 } 789 /* 790 * Map event name into event enum 791 */ 792 int event; 793 if (eventName.equals(CONNECTED_STR)) 794 event = CONNECTED; 795 else if (eventName.equals(DISCONNECTED_STR)) 796 event = DISCONNECTED; 797 else if (eventName.equals(STATE_CHANGE_STR)) 798 event = STATE_CHANGE; 799 else if (eventName.equals(SCAN_RESULTS_STR)) 800 event = SCAN_RESULTS; 801 else if (eventName.equals(LINK_SPEED_STR)) 802 event = LINK_SPEED; 803 else if (eventName.equals(TERMINATING_STR)) 804 event = TERMINATING; 805 else if (eventName.equals(DRIVER_STATE_STR)) 806 event = DRIVER_STATE; 807 else if (eventName.equals(EAP_FAILURE_STR)) 808 event = EAP_FAILURE; 809 else if (eventName.equals(ASSOC_REJECT_STR)) 810 event = ASSOC_REJECT; 811 else if (eventName.equals(TEMP_DISABLED_STR)) { 812 event = SSID_TEMP_DISABLE; 813 } else if (eventName.equals(REENABLED_STR)) { 814 event = SSID_REENABLE; 815 } else if (eventName.equals(BSS_ADDED_STR)) { 816 event = BSS_ADDED; 817 } else if (eventName.equals(BSS_REMOVED_STR)) { 818 event = BSS_REMOVED; 819 } 820 else 821 event = UNKNOWN; 822 823 String eventData = eventStr; 824 if (event == DRIVER_STATE || event == LINK_SPEED) 825 eventData = eventData.split(" ")[1]; 826 else if (event == STATE_CHANGE || event == EAP_FAILURE) { 827 int ind = eventStr.indexOf(" "); 828 if (ind != -1) { 829 eventData = eventStr.substring(ind + 1); 830 } 831 } else { 832 int ind = eventStr.indexOf(" - "); 833 if (ind != -1) { 834 eventData = eventStr.substring(ind + 3); 835 } 836 } 837 838 if ((event == SSID_TEMP_DISABLE)||(event == SSID_REENABLE)) { 839 String substr = null; 840 int netId = -1; 841 int ind = eventStr.indexOf(" "); 842 if (ind != -1) { 843 substr = eventStr.substring(ind + 1); 844 } 845 if (substr != null) { 846 String status[] = substr.split(" "); 847 for (String key : status) { 848 if (key.regionMatches(0, "id=", 0, 3)) { 849 int idx = 3; 850 netId = 0; 851 while (idx < key.length()) { 852 char c = key.charAt(idx); 853 if ((c >= 0x30) && (c <= 0x39)) { 854 netId *= 10; 855 netId += c - 0x30; 856 idx++; 857 } else { 858 break; 859 } 860 } 861 } 862 } 863 } 864 mStateMachine.sendMessage((event == SSID_TEMP_DISABLE)? 865 SSID_TEMP_DISABLED:SSID_REENABLED, netId, 0, substr); 866 } else if (event == STATE_CHANGE) { 867 handleSupplicantStateChange(eventData); 868 } else if (event == DRIVER_STATE) { 869 handleDriverEvent(eventData); 870 } else if (event == TERMINATING) { 871 /** 872 * Close the supplicant connection if we see 873 * too many recv errors 874 */ 875 if (eventData.startsWith(WPA_RECV_ERROR_STR)) { 876 if (++sRecvErrors > MAX_RECV_ERRORS) { 877 if (DBG) { 878 Log.d(TAG, "too many recv errors, closing connection"); 879 } 880 } else { 881 eventLogCounter++; 882 return false; 883 } 884 } 885 886 // Notify and exit 887 mStateMachine.sendMessage(SUP_DISCONNECTION_EVENT, eventLogCounter); 888 return true; 889 } else if (event == EAP_FAILURE) { 890 if (eventData.startsWith(EAP_AUTH_FAILURE_STR)) { 891 logDbg("WifiMonitor send auth failure (EAP_AUTH_FAILURE) "); 892 mStateMachine.sendMessage(AUTHENTICATION_FAILURE_EVENT, eventLogCounter); 893 } 894 } else if (event == ASSOC_REJECT) { 895 Matcher match = mAssocRejectEventPattern.matcher(eventData); 896 String BSSID = ""; 897 int status = -1; 898 if (!match.find()) { 899 if (DBG) Log.d(TAG, "Assoc Reject: Could not parse assoc reject string"); 900 } else { 901 BSSID = match.group(1); 902 try { 903 status = Integer.parseInt(match.group(2)); 904 } catch (NumberFormatException e) { 905 status = -1; 906 } 907 } 908 mStateMachine.sendMessage(ASSOCIATION_REJECTION_EVENT, eventLogCounter, status, BSSID); 909 } else if (event == BSS_ADDED && !VDBG) { 910 // Ignore that event - it is not handled, and dont log it as it is too verbose 911 } else if (event == BSS_REMOVED && !VDBG) { 912 // Ignore that event - it is not handled, and dont log it as it is too verbose 913 } else { 914 handleEvent(event, eventData); 915 } 916 sRecvErrors = 0; 917 eventLogCounter++; 918 return false; 919 } 920 921 private void handleDriverEvent(String state) { 922 if (state == null) { 923 return; 924 } 925 if (state.equals("HANGED")) { 926 mStateMachine.sendMessage(DRIVER_HUNG_EVENT); 927 } 928 } 929 930 /** 931 * Handle all supplicant events except STATE-CHANGE 932 * @param event the event type 933 * @param remainder the rest of the string following the 934 * event name and " — " 935 */ 936 void handleEvent(int event, String remainder) { 937 if (DBG) { 938 logDbg("handleEvent " + Integer.toString(event) + " " + remainder); 939 } 940 switch (event) { 941 case DISCONNECTED: 942 handleNetworkStateChange(NetworkInfo.DetailedState.DISCONNECTED, remainder); 943 break; 944 945 case CONNECTED: 946 handleNetworkStateChange(NetworkInfo.DetailedState.CONNECTED, remainder); 947 break; 948 949 case SCAN_RESULTS: 950 mStateMachine.sendMessage(SCAN_RESULTS_EVENT); 951 break; 952 953 case UNKNOWN: 954 if (DBG) { 955 logDbg("handleEvent unknown: " + Integer.toString(event) + " " + remainder); 956 } 957 break; 958 default: 959 break; 960 } 961 } 962 963 private void handleTargetBSSIDEvent(String eventStr) { 964 String BSSID = null; 965 Matcher match = mTargetBSSIDPattern.matcher(eventStr); 966 if (match.find()) { 967 BSSID = match.group(1); 968 } else { 969 Log.d(TAG, "didn't find BSSID " + eventStr); 970 } 971 mStateMachine.sendMessage(WifiStateMachine.CMD_TARGET_BSSID, eventLogCounter, 0, BSSID); 972 } 973 974 private void handleAssociatedBSSIDEvent(String eventStr) { 975 String BSSID = null; 976 Matcher match = mAssociatedPattern.matcher(eventStr); 977 if (match.find()) { 978 BSSID = match.group(1); 979 } else { 980 Log.d(TAG, "handleAssociatedBSSIDEvent: didn't find BSSID " + eventStr); 981 } 982 mStateMachine.sendMessage(WifiStateMachine.CMD_ASSOCIATED_BSSID, eventLogCounter, 0, BSSID); 983 } 984 985 986 private void handleWpsFailEvent(String dataString) { 987 final Pattern p = Pattern.compile(WPS_FAIL_PATTERN); 988 Matcher match = p.matcher(dataString); 989 int reason = 0; 990 if (match.find()) { 991 String cfgErrStr = match.group(1); 992 String reasonStr = match.group(2); 993 994 if (reasonStr != null) { 995 int reasonInt = Integer.parseInt(reasonStr); 996 switch(reasonInt) { 997 case REASON_TKIP_ONLY_PROHIBITED: 998 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 999 WifiManager.WPS_TKIP_ONLY_PROHIBITED, 0)); 1000 return; 1001 case REASON_WEP_PROHIBITED: 1002 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 1003 WifiManager.WPS_WEP_PROHIBITED, 0)); 1004 return; 1005 default: 1006 reason = reasonInt; 1007 break; 1008 } 1009 } 1010 if (cfgErrStr != null) { 1011 int cfgErrInt = Integer.parseInt(cfgErrStr); 1012 switch(cfgErrInt) { 1013 case CONFIG_AUTH_FAILURE: 1014 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 1015 WifiManager.WPS_AUTH_FAILURE, 0)); 1016 return; 1017 case CONFIG_MULTIPLE_PBC_DETECTED: 1018 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 1019 WifiManager.WPS_OVERLAP_ERROR, 0)); 1020 return; 1021 default: 1022 if (reason == 0) reason = cfgErrInt; 1023 break; 1024 } 1025 } 1026 } 1027 //For all other errors, return a generic internal error 1028 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 1029 WifiManager.ERROR, reason)); 1030 } 1031 1032 /* <event> status=<err> and the special case of <event> reason=FREQ_CONFLICT */ 1033 private P2pStatus p2pError(String dataString) { 1034 P2pStatus err = P2pStatus.UNKNOWN; 1035 String[] tokens = dataString.split(" "); 1036 if (tokens.length < 2) return err; 1037 String[] nameValue = tokens[1].split("="); 1038 if (nameValue.length != 2) return err; 1039 1040 /* Handle the special case of reason=FREQ+CONFLICT */ 1041 if (nameValue[1].equals("FREQ_CONFLICT")) { 1042 return P2pStatus.NO_COMMON_CHANNEL; 1043 } 1044 try { 1045 err = P2pStatus.valueOf(Integer.parseInt(nameValue[1])); 1046 } catch (NumberFormatException e) { 1047 e.printStackTrace(); 1048 } 1049 return err; 1050 } 1051 1052 /** 1053 * Handle p2p events 1054 */ 1055 private void handleP2pEvents(String dataString) { 1056 if (dataString.startsWith(P2P_DEVICE_FOUND_STR)) { 1057 mStateMachine.sendMessage(P2P_DEVICE_FOUND_EVENT, new WifiP2pDevice(dataString)); 1058 } else if (dataString.startsWith(P2P_DEVICE_LOST_STR)) { 1059 mStateMachine.sendMessage(P2P_DEVICE_LOST_EVENT, new WifiP2pDevice(dataString)); 1060 } else if (dataString.startsWith(P2P_FIND_STOPPED_STR)) { 1061 mStateMachine.sendMessage(P2P_FIND_STOPPED_EVENT); 1062 } else if (dataString.startsWith(P2P_GO_NEG_REQUEST_STR)) { 1063 mStateMachine.sendMessage(P2P_GO_NEGOTIATION_REQUEST_EVENT, 1064 new WifiP2pConfig(dataString)); 1065 } else if (dataString.startsWith(P2P_GO_NEG_SUCCESS_STR)) { 1066 mStateMachine.sendMessage(P2P_GO_NEGOTIATION_SUCCESS_EVENT); 1067 } else if (dataString.startsWith(P2P_GO_NEG_FAILURE_STR)) { 1068 mStateMachine.sendMessage(P2P_GO_NEGOTIATION_FAILURE_EVENT, p2pError(dataString)); 1069 } else if (dataString.startsWith(P2P_GROUP_FORMATION_SUCCESS_STR)) { 1070 mStateMachine.sendMessage(P2P_GROUP_FORMATION_SUCCESS_EVENT); 1071 } else if (dataString.startsWith(P2P_GROUP_FORMATION_FAILURE_STR)) { 1072 mStateMachine.sendMessage(P2P_GROUP_FORMATION_FAILURE_EVENT, p2pError(dataString)); 1073 } else if (dataString.startsWith(P2P_GROUP_STARTED_STR)) { 1074 mStateMachine.sendMessage(P2P_GROUP_STARTED_EVENT, new WifiP2pGroup(dataString)); 1075 } else if (dataString.startsWith(P2P_GROUP_REMOVED_STR)) { 1076 mStateMachine.sendMessage(P2P_GROUP_REMOVED_EVENT, new WifiP2pGroup(dataString)); 1077 } else if (dataString.startsWith(P2P_INVITATION_RECEIVED_STR)) { 1078 mStateMachine.sendMessage(P2P_INVITATION_RECEIVED_EVENT, 1079 new WifiP2pGroup(dataString)); 1080 } else if (dataString.startsWith(P2P_INVITATION_RESULT_STR)) { 1081 mStateMachine.sendMessage(P2P_INVITATION_RESULT_EVENT, p2pError(dataString)); 1082 } else if (dataString.startsWith(P2P_PROV_DISC_PBC_REQ_STR)) { 1083 mStateMachine.sendMessage(P2P_PROV_DISC_PBC_REQ_EVENT, 1084 new WifiP2pProvDiscEvent(dataString)); 1085 } else if (dataString.startsWith(P2P_PROV_DISC_PBC_RSP_STR)) { 1086 mStateMachine.sendMessage(P2P_PROV_DISC_PBC_RSP_EVENT, 1087 new WifiP2pProvDiscEvent(dataString)); 1088 } else if (dataString.startsWith(P2P_PROV_DISC_ENTER_PIN_STR)) { 1089 mStateMachine.sendMessage(P2P_PROV_DISC_ENTER_PIN_EVENT, 1090 new WifiP2pProvDiscEvent(dataString)); 1091 } else if (dataString.startsWith(P2P_PROV_DISC_SHOW_PIN_STR)) { 1092 mStateMachine.sendMessage(P2P_PROV_DISC_SHOW_PIN_EVENT, 1093 new WifiP2pProvDiscEvent(dataString)); 1094 } else if (dataString.startsWith(P2P_PROV_DISC_FAILURE_STR)) { 1095 mStateMachine.sendMessage(P2P_PROV_DISC_FAILURE_EVENT); 1096 } else if (dataString.startsWith(P2P_SERV_DISC_RESP_STR)) { 1097 List<WifiP2pServiceResponse> list = WifiP2pServiceResponse.newInstance(dataString); 1098 if (list != null) { 1099 mStateMachine.sendMessage(P2P_SERV_DISC_RESP_EVENT, list); 1100 } else { 1101 Log.e(TAG, "Null service resp " + dataString); 1102 } 1103 } 1104 } 1105 1106 /** 1107 * Handle hostap events 1108 */ 1109 private void handleHostApEvents(String dataString) { 1110 String[] tokens = dataString.split(" "); 1111 /* AP-STA-CONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=02:90:4c:a0:92:54 */ 1112 if (tokens[0].equals(AP_STA_CONNECTED_STR)) { 1113 mStateMachine.sendMessage(AP_STA_CONNECTED_EVENT, new WifiP2pDevice(dataString)); 1114 /* AP-STA-DISCONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=02:90:4c:a0:92:54 */ 1115 } else if (tokens[0].equals(AP_STA_DISCONNECTED_STR)) { 1116 mStateMachine.sendMessage(AP_STA_DISCONNECTED_EVENT, new WifiP2pDevice(dataString)); 1117 } 1118 } 1119 1120 /** 1121 * Handle ANQP events 1122 */ 1123 private void handleGasQueryEvents(String dataString) { 1124 // hs20 1125 if (mStateMachine2 == null) return; 1126 if (dataString.startsWith(GAS_QUERY_START_STR)) { 1127 mStateMachine2.sendMessage(GAS_QUERY_START_EVENT); 1128 } else if (dataString.startsWith(GAS_QUERY_DONE_STR)) { 1129 String[] dataTokens = dataString.split(" "); 1130 String bssid = null; 1131 int success = 0; 1132 for (String token : dataTokens) { 1133 String[] nameValue = token.split("="); 1134 if (nameValue.length != 2) { 1135 continue; 1136 } 1137 if (nameValue[0].equals("addr")) { 1138 bssid = nameValue[1]; 1139 continue; 1140 } 1141 if (nameValue[0].equals("result")) { 1142 success = nameValue[1].equals("SUCCESS") ? 1 : 0; 1143 continue; 1144 } 1145 } 1146 mStateMachine2.sendMessage(GAS_QUERY_DONE_EVENT, success, 0, bssid); 1147 } else { 1148 if (DBG) Log.d(TAG, "Unknown GAS query event: " + dataString); 1149 } 1150 } 1151 1152 /** 1153 * Handle HS20 events 1154 */ 1155 private void handleHs20Events(String dataString) { 1156 if (mStateMachine2 == null) return; 1157 if (dataString.startsWith(HS20_SUB_REM_STR)) { 1158 // format: HS20-SUBSCRIPTION-REMEDIATION osu_method, url 1159 String[] dataTokens = dataString.split(" "); 1160 int method = -1; 1161 String url = null; 1162 if (dataTokens.length >= 3) { 1163 method = Integer.parseInt(dataTokens[1]); 1164 url = dataTokens[2]; 1165 } 1166 mStateMachine2.sendMessage(HS20_REMEDIATION_EVENT, method, 0, url); 1167 } else if (dataString.startsWith(HS20_DEAUTH_STR)) { 1168 // format: HS20-DEAUTH-IMMINENT-NOTICE code, delay, url 1169 int code = -1; 1170 int delay = -1; 1171 String url = null; 1172 String[] dataTokens = dataString.split(" "); 1173 if (dataTokens.length >= 4) { 1174 code = Integer.parseInt(dataTokens[1]); 1175 delay = Integer.parseInt(dataTokens[2]); 1176 url = dataTokens[3]; 1177 } 1178 mStateMachine2.sendMessage(HS20_DEAUTH_EVENT, code, delay, url); 1179 } else { 1180 if (DBG) Log.d(TAG, "Unknown HS20 event: " + dataString); 1181 } 1182 } 1183 1184 /** 1185 * Handle Supplicant Requests 1186 */ 1187 private void handleRequests(String dataString) { 1188 String SSID = null; 1189 int reason = -2; 1190 String requestName = dataString.substring(REQUEST_PREFIX_LEN_STR); 1191 if (TextUtils.isEmpty(requestName)) { 1192 return; 1193 } 1194 if (requestName.startsWith(IDENTITY_STR)) { 1195 Matcher match = mRequestIdentityPattern.matcher(requestName); 1196 if (match.find()) { 1197 SSID = match.group(2); 1198 try { 1199 reason = Integer.parseInt(match.group(1)); 1200 } catch (NumberFormatException e) { 1201 reason = -1; 1202 } 1203 } else { 1204 Log.e(TAG, "didn't find SSID " + requestName); 1205 } 1206 mStateMachine.sendMessage(SUP_REQUEST_IDENTITY, eventLogCounter, reason, SSID); 1207 } if (requestName.startsWith(SIM_STR)) { 1208 Matcher match = mRequestGsmAuthPattern.matcher(requestName); 1209 if (match.find()) { 1210 WifiStateMachine.SimAuthRequestData data = 1211 new WifiStateMachine.SimAuthRequestData(); 1212 data.networkId = Integer.parseInt(match.group(1)); 1213 data.protocol = WifiEnterpriseConfig.Eap.SIM; 1214 data.ssid = match.group(4); 1215 data.challenges = match.group(2).split(":"); 1216 mStateMachine.sendMessage(SUP_REQUEST_SIM_AUTH, data); 1217 } else { 1218 Log.e(TAG, "couldn't parse SIM auth request - " + requestName); 1219 } 1220 1221 } else { 1222 if (DBG) Log.w(TAG, "couldn't identify request type - " + dataString); 1223 } 1224 } 1225 1226 /** 1227 * Handle the supplicant STATE-CHANGE event 1228 * @param dataString New supplicant state string in the format: 1229 * id=network-id state=new-state 1230 */ 1231 private void handleSupplicantStateChange(String dataString) { 1232 WifiSsid wifiSsid = null; 1233 int index = dataString.lastIndexOf("SSID="); 1234 if (index != -1) { 1235 wifiSsid = WifiSsid.createFromAsciiEncoded( 1236 dataString.substring(index + 5)); 1237 } 1238 String[] dataTokens = dataString.split(" "); 1239 1240 String BSSID = null; 1241 int networkId = -1; 1242 int newState = -1; 1243 for (String token : dataTokens) { 1244 String[] nameValue = token.split("="); 1245 if (nameValue.length != 2) { 1246 continue; 1247 } 1248 1249 if (nameValue[0].equals("BSSID")) { 1250 BSSID = nameValue[1]; 1251 continue; 1252 } 1253 1254 int value; 1255 try { 1256 value = Integer.parseInt(nameValue[1]); 1257 } catch (NumberFormatException e) { 1258 continue; 1259 } 1260 1261 if (nameValue[0].equals("id")) { 1262 networkId = value; 1263 } else if (nameValue[0].equals("state")) { 1264 newState = value; 1265 } 1266 } 1267 1268 if (newState == -1) return; 1269 1270 SupplicantState newSupplicantState = SupplicantState.INVALID; 1271 for (SupplicantState state : SupplicantState.values()) { 1272 if (state.ordinal() == newState) { 1273 newSupplicantState = state; 1274 break; 1275 } 1276 } 1277 if (newSupplicantState == SupplicantState.INVALID) { 1278 Log.w(TAG, "Invalid supplicant state: " + newState); 1279 } 1280 notifySupplicantStateChange(networkId, wifiSsid, BSSID, newSupplicantState); 1281 } 1282 1283 private void handleNetworkStateChange(NetworkInfo.DetailedState newState, String data) { 1284 String BSSID = null; 1285 int networkId = -1; 1286 int reason = 0; 1287 int ind = -1; 1288 int local = 0; 1289 Matcher match; 1290 if (newState == NetworkInfo.DetailedState.CONNECTED) { 1291 match = mConnectedEventPattern.matcher(data); 1292 if (!match.find()) { 1293 if (DBG) Log.d(TAG, "handleNetworkStateChange: Couldnt find BSSID in event string"); 1294 } else { 1295 BSSID = match.group(1); 1296 try { 1297 networkId = Integer.parseInt(match.group(2)); 1298 } catch (NumberFormatException e) { 1299 networkId = -1; 1300 } 1301 } 1302 notifyNetworkStateChange(newState, BSSID, networkId, reason); 1303 } else if (newState == NetworkInfo.DetailedState.DISCONNECTED) { 1304 match = mDisconnectedEventPattern.matcher(data); 1305 if (!match.find()) { 1306 if (DBG) Log.d(TAG, "handleNetworkStateChange: Could not parse disconnect string"); 1307 } else { 1308 BSSID = match.group(1); 1309 try { 1310 reason = Integer.parseInt(match.group(2)); 1311 } catch (NumberFormatException e) { 1312 reason = -1; 1313 } 1314 try { 1315 local = Integer.parseInt(match.group(3)); 1316 } catch (NumberFormatException e) { 1317 local = -1; 1318 } 1319 } 1320 notifyNetworkStateChange(newState, BSSID, local, reason); 1321 } 1322 } 1323 1324 /** 1325 * Send the state machine a notification that the state of Wifi connectivity 1326 * has changed. 1327 * @param newState the new network state 1328 * @param BSSID when the new state is {@link NetworkInfo.DetailedState#CONNECTED}, 1329 * this is the MAC address of the access point. Otherwise, it 1330 * is {@code null}. 1331 * @param netId the configured network on which the state change occurred 1332 */ 1333 void notifyNetworkStateChange(NetworkInfo.DetailedState newState, 1334 String BSSID, int netId, int reason) { 1335 if (newState == NetworkInfo.DetailedState.CONNECTED) { 1336 Message m = mStateMachine.obtainMessage(NETWORK_CONNECTION_EVENT, 1337 netId, reason, BSSID); 1338 mStateMachine.sendMessage(m); 1339 } else { 1340 1341 Message m = mStateMachine.obtainMessage(NETWORK_DISCONNECTION_EVENT, 1342 netId, reason, BSSID); 1343 if (DBG) logDbg("WifiMonitor notify network disconnect: " 1344 + BSSID 1345 + " reason=" + Integer.toString(reason)); 1346 mStateMachine.sendMessage(m); 1347 } 1348 } 1349 1350 /** 1351 * Send the state machine a notification that the state of the supplicant 1352 * has changed. 1353 * @param networkId the configured network on which the state change occurred 1354 * @param wifiSsid network name 1355 * @param BSSID network address 1356 * @param newState the new {@code SupplicantState} 1357 */ 1358 void notifySupplicantStateChange(int networkId, WifiSsid wifiSsid, String BSSID, 1359 SupplicantState newState) { 1360 mStateMachine.sendMessage(mStateMachine.obtainMessage(SUPPLICANT_STATE_CHANGE_EVENT, 1361 eventLogCounter, 0, 1362 new StateChangeResult(networkId, wifiSsid, BSSID, newState))); 1363 } 1364} 1365