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