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 17 package com.android.server.wifi; 18 19 import android.net.NetworkInfo; 20 import android.net.wifi.SupplicantState; 21 import android.net.wifi.WifiEnterpriseConfig; 22 import android.net.wifi.WifiManager; 23 import android.net.wifi.WifiSsid; 24 import android.net.wifi.p2p.WifiP2pConfig; 25 import android.net.wifi.p2p.WifiP2pDevice; 26 import android.net.wifi.p2p.WifiP2pGroup; 27 import android.net.wifi.p2p.WifiP2pProvDiscEvent; 28 import android.net.wifi.p2p.nsd.WifiP2pServiceResponse; 29 import android.os.Message; 30 import android.os.SystemClock; 31 import android.text.TextUtils; 32 import android.util.Log; 33 34 import com.android.server.wifi.p2p.WifiP2pServiceImpl.P2pStatus; 35 36 import com.android.internal.util.Protocol; 37 import com.android.internal.util.StateMachine; 38 39 import java.util.HashMap; 40 import java.util.List; 41 import java.util.regex.Matcher; 42 import 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 */ 50 public 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 for (WifiMonitor monitor : mIfaceMap.values()) { 670 if (monitor.mMonitoring && monitor.dispatchEvent(eventStr, iface)) { 671 done = true; 672 } 673 } 674 675 if (done) { 676 mConnected = false; 677 } 678 679 return done; 680 } 681 } 682 } 683 684 private static class MonitorThread extends Thread { 685 private final WifiNative mWifiNative; 686 private final WifiMonitorSingleton mWifiMonitorSingleton; 687 688 public MonitorThread(WifiNative wifiNative, WifiMonitorSingleton wifiMonitorSingleton) { 689 super("WifiMonitor"); 690 mWifiNative = wifiNative; 691 mWifiMonitorSingleton = wifiMonitorSingleton; 692 } 693 694 public void run() { 695 //noinspection InfiniteLoopStatement 696 for (;;) { 697 String eventStr = mWifiNative.waitForEvent(); 698 699 // Skip logging the common but mostly uninteresting scan-results event 700 if (DBG && eventStr.indexOf(SCAN_RESULTS_STR) == -1) { 701 Log.d(TAG, "Event [" + eventStr + "]"); 702 } 703 704 if (mWifiMonitorSingleton.dispatchEvent(eventStr)) { 705 if (DBG) Log.d(TAG, "Disconnecting from the supplicant, no more events"); 706 break; 707 } 708 } 709 } 710 } 711 712 private void logDbg(String debug) { 713 Log.e(TAG, debug/*+ " stack:" + Thread.currentThread().getStackTrace()[2].getMethodName() 714 +" - "+ Thread.currentThread().getStackTrace()[3].getMethodName() 715 +" - "+ Thread.currentThread().getStackTrace()[4].getMethodName() 716 +" - "+ Thread.currentThread().getStackTrace()[5].getMethodName()*/); 717 } 718 719 /* @return true if the event was supplicant disconnection */ 720 private boolean dispatchEvent(String eventStr, String iface) { 721 722 if (DBG) { 723 // Dont log CTRL-EVENT-BSS-ADDED which are too verbose and not handled 724 if (eventStr != null && !eventStr.contains("CTRL-EVENT-BSS-ADDED")) { 725 logDbg("WifiMonitor:" + iface + " cnt=" + Integer.toString(eventLogCounter) 726 + " dispatchEvent: " + eventStr); 727 } 728 } 729 730 if (!eventStr.startsWith(EVENT_PREFIX_STR)) { 731 if (eventStr.startsWith(WPA_EVENT_PREFIX_STR) && 732 0 < eventStr.indexOf(PASSWORD_MAY_BE_INCORRECT_STR)) { 733 mStateMachine.sendMessage(AUTHENTICATION_FAILURE_EVENT, eventLogCounter); 734 } else if (eventStr.startsWith(WPS_SUCCESS_STR)) { 735 mStateMachine.sendMessage(WPS_SUCCESS_EVENT); 736 } else if (eventStr.startsWith(WPS_FAIL_STR)) { 737 handleWpsFailEvent(eventStr); 738 } else if (eventStr.startsWith(WPS_OVERLAP_STR)) { 739 mStateMachine.sendMessage(WPS_OVERLAP_EVENT); 740 } else if (eventStr.startsWith(WPS_TIMEOUT_STR)) { 741 mStateMachine.sendMessage(WPS_TIMEOUT_EVENT); 742 } else if (eventStr.startsWith(P2P_EVENT_PREFIX_STR)) { 743 handleP2pEvents(eventStr); 744 } else if (eventStr.startsWith(HOST_AP_EVENT_PREFIX_STR)) { 745 handleHostApEvents(eventStr); 746 } else if (eventStr.startsWith(GAS_QUERY_PREFIX_STR)) { 747 handleGasQueryEvents(eventStr); 748 } else if (eventStr.startsWith(RX_HS20_ANQP_ICON_STR)) { 749 if (mStateMachine2 != null) 750 mStateMachine2.sendMessage(RX_HS20_ANQP_ICON_EVENT, 751 eventStr.substring(RX_HS20_ANQP_ICON_STR_LEN + 1)); 752 } else if (eventStr.startsWith(HS20_PREFIX_STR)) { 753 handleHs20Events(eventStr); 754 } else if (eventStr.startsWith(REQUEST_PREFIX_STR)) { 755 handleRequests(eventStr); 756 } else if (eventStr.startsWith(TARGET_BSSID_STR)) { 757 handleTargetBSSIDEvent(eventStr); 758 } else if (eventStr.startsWith(ASSOCIATED_WITH_STR)) { 759 handleAssociatedBSSIDEvent(eventStr); 760 } else { 761 if (DBG) Log.w(TAG, "couldn't identify event type - " + eventStr); 762 } 763 eventLogCounter++; 764 return false; 765 } 766 767 String eventName = eventStr.substring(EVENT_PREFIX_LEN_STR); 768 int nameEnd = eventName.indexOf(' '); 769 if (nameEnd != -1) 770 eventName = eventName.substring(0, nameEnd); 771 if (eventName.length() == 0) { 772 if (DBG) Log.i(TAG, "Received wpa_supplicant event with empty event name"); 773 eventLogCounter++; 774 return false; 775 } 776 /* 777 * Map event name into event enum 778 */ 779 int event; 780 if (eventName.equals(CONNECTED_STR)) 781 event = CONNECTED; 782 else if (eventName.equals(DISCONNECTED_STR)) 783 event = DISCONNECTED; 784 else if (eventName.equals(STATE_CHANGE_STR)) 785 event = STATE_CHANGE; 786 else if (eventName.equals(SCAN_RESULTS_STR)) 787 event = SCAN_RESULTS; 788 else if (eventName.equals(LINK_SPEED_STR)) 789 event = LINK_SPEED; 790 else if (eventName.equals(TERMINATING_STR)) 791 event = TERMINATING; 792 else if (eventName.equals(DRIVER_STATE_STR)) 793 event = DRIVER_STATE; 794 else if (eventName.equals(EAP_FAILURE_STR)) 795 event = EAP_FAILURE; 796 else if (eventName.equals(ASSOC_REJECT_STR)) 797 event = ASSOC_REJECT; 798 else if (eventName.equals(TEMP_DISABLED_STR)) { 799 event = SSID_TEMP_DISABLE; 800 } else if (eventName.equals(REENABLED_STR)) { 801 event = SSID_REENABLE; 802 } else if (eventName.equals(BSS_ADDED_STR)) { 803 event = BSS_ADDED; 804 } else if (eventName.equals(BSS_REMOVED_STR)) { 805 event = BSS_REMOVED; 806 } 807 else 808 event = UNKNOWN; 809 810 String eventData = eventStr; 811 if (event == DRIVER_STATE || event == LINK_SPEED) 812 eventData = eventData.split(" ")[1]; 813 else if (event == STATE_CHANGE || event == EAP_FAILURE) { 814 int ind = eventStr.indexOf(" "); 815 if (ind != -1) { 816 eventData = eventStr.substring(ind + 1); 817 } 818 } else { 819 int ind = eventStr.indexOf(" - "); 820 if (ind != -1) { 821 eventData = eventStr.substring(ind + 3); 822 } 823 } 824 825 if ((event == SSID_TEMP_DISABLE)||(event == SSID_REENABLE)) { 826 String substr = null; 827 int netId = -1; 828 int ind = eventStr.indexOf(" "); 829 if (ind != -1) { 830 substr = eventStr.substring(ind + 1); 831 } 832 if (substr != null) { 833 String status[] = substr.split(" "); 834 for (String key : status) { 835 if (key.regionMatches(0, "id=", 0, 3)) { 836 int idx = 3; 837 netId = 0; 838 while (idx < key.length()) { 839 char c = key.charAt(idx); 840 if ((c >= 0x30) && (c <= 0x39)) { 841 netId *= 10; 842 netId += c - 0x30; 843 idx++; 844 } else { 845 break; 846 } 847 } 848 } 849 } 850 } 851 mStateMachine.sendMessage((event == SSID_TEMP_DISABLE)? 852 SSID_TEMP_DISABLED:SSID_REENABLED, netId, 0, substr); 853 } else if (event == STATE_CHANGE) { 854 handleSupplicantStateChange(eventData); 855 } else if (event == DRIVER_STATE) { 856 handleDriverEvent(eventData); 857 } else if (event == TERMINATING) { 858 /** 859 * Close the supplicant connection if we see 860 * too many recv errors 861 */ 862 if (eventData.startsWith(WPA_RECV_ERROR_STR)) { 863 if (++sRecvErrors > MAX_RECV_ERRORS) { 864 if (DBG) { 865 Log.d(TAG, "too many recv errors, closing connection"); 866 } 867 } else { 868 eventLogCounter++; 869 return false; 870 } 871 } 872 873 // Notify and exit 874 mStateMachine.sendMessage(SUP_DISCONNECTION_EVENT, eventLogCounter); 875 return true; 876 } else if (event == EAP_FAILURE) { 877 if (eventData.startsWith(EAP_AUTH_FAILURE_STR)) { 878 logDbg("WifiMonitor send auth failure (EAP_AUTH_FAILURE) "); 879 mStateMachine.sendMessage(AUTHENTICATION_FAILURE_EVENT, eventLogCounter); 880 } 881 } else if (event == ASSOC_REJECT) { 882 Matcher match = mAssocRejectEventPattern.matcher(eventData); 883 String BSSID = ""; 884 int status = -1; 885 if (!match.find()) { 886 if (DBG) Log.d(TAG, "Assoc Reject: Could not parse assoc reject string"); 887 } else { 888 BSSID = match.group(1); 889 try { 890 status = Integer.parseInt(match.group(2)); 891 } catch (NumberFormatException e) { 892 status = -1; 893 } 894 } 895 mStateMachine.sendMessage(ASSOCIATION_REJECTION_EVENT, eventLogCounter, status, BSSID); 896 } else if (event == BSS_ADDED && !VDBG) { 897 // Ignore that event - it is not handled, and dont log it as it is too verbose 898 } else if (event == BSS_REMOVED && !VDBG) { 899 // Ignore that event - it is not handled, and dont log it as it is too verbose 900 } else { 901 handleEvent(event, eventData); 902 } 903 sRecvErrors = 0; 904 eventLogCounter++; 905 return false; 906 } 907 908 private void handleDriverEvent(String state) { 909 if (state == null) { 910 return; 911 } 912 if (state.equals("HANGED")) { 913 mStateMachine.sendMessage(DRIVER_HUNG_EVENT); 914 } 915 } 916 917 /** 918 * Handle all supplicant events except STATE-CHANGE 919 * @param event the event type 920 * @param remainder the rest of the string following the 921 * event name and " — " 922 */ 923 void handleEvent(int event, String remainder) { 924 if (DBG) { 925 logDbg("handleEvent " + Integer.toString(event) + " " + remainder); 926 } 927 switch (event) { 928 case DISCONNECTED: 929 handleNetworkStateChange(NetworkInfo.DetailedState.DISCONNECTED, remainder); 930 break; 931 932 case CONNECTED: 933 handleNetworkStateChange(NetworkInfo.DetailedState.CONNECTED, remainder); 934 break; 935 936 case SCAN_RESULTS: 937 mStateMachine.sendMessage(SCAN_RESULTS_EVENT); 938 break; 939 940 case UNKNOWN: 941 if (DBG) { 942 logDbg("handleEvent unknown: " + Integer.toString(event) + " " + remainder); 943 } 944 break; 945 default: 946 break; 947 } 948 } 949 950 private void handleTargetBSSIDEvent(String eventStr) { 951 String BSSID = null; 952 Matcher match = mTargetBSSIDPattern.matcher(eventStr); 953 if (match.find()) { 954 BSSID = match.group(1); 955 } else { 956 Log.d(TAG, "didn't find BSSID " + eventStr); 957 } 958 mStateMachine.sendMessage(WifiStateMachine.CMD_TARGET_BSSID, eventLogCounter, 0, BSSID); 959 } 960 961 private void handleAssociatedBSSIDEvent(String eventStr) { 962 String BSSID = null; 963 Matcher match = mAssociatedPattern.matcher(eventStr); 964 if (match.find()) { 965 BSSID = match.group(1); 966 } else { 967 Log.d(TAG, "handleAssociatedBSSIDEvent: didn't find BSSID " + eventStr); 968 } 969 mStateMachine.sendMessage(WifiStateMachine.CMD_ASSOCIATED_BSSID, eventLogCounter, 0, BSSID); 970 } 971 972 973 private void handleWpsFailEvent(String dataString) { 974 final Pattern p = Pattern.compile(WPS_FAIL_PATTERN); 975 Matcher match = p.matcher(dataString); 976 int reason = 0; 977 if (match.find()) { 978 String cfgErrStr = match.group(1); 979 String reasonStr = match.group(2); 980 981 if (reasonStr != null) { 982 int reasonInt = Integer.parseInt(reasonStr); 983 switch(reasonInt) { 984 case REASON_TKIP_ONLY_PROHIBITED: 985 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 986 WifiManager.WPS_TKIP_ONLY_PROHIBITED, 0)); 987 return; 988 case REASON_WEP_PROHIBITED: 989 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 990 WifiManager.WPS_WEP_PROHIBITED, 0)); 991 return; 992 default: 993 reason = reasonInt; 994 break; 995 } 996 } 997 if (cfgErrStr != null) { 998 int cfgErrInt = Integer.parseInt(cfgErrStr); 999 switch(cfgErrInt) { 1000 case CONFIG_AUTH_FAILURE: 1001 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 1002 WifiManager.WPS_AUTH_FAILURE, 0)); 1003 return; 1004 case CONFIG_MULTIPLE_PBC_DETECTED: 1005 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 1006 WifiManager.WPS_OVERLAP_ERROR, 0)); 1007 return; 1008 default: 1009 if (reason == 0) reason = cfgErrInt; 1010 break; 1011 } 1012 } 1013 } 1014 //For all other errors, return a generic internal error 1015 mStateMachine.sendMessage(mStateMachine.obtainMessage(WPS_FAIL_EVENT, 1016 WifiManager.ERROR, reason)); 1017 } 1018 1019 /* <event> status=<err> and the special case of <event> reason=FREQ_CONFLICT */ 1020 private P2pStatus p2pError(String dataString) { 1021 P2pStatus err = P2pStatus.UNKNOWN; 1022 String[] tokens = dataString.split(" "); 1023 if (tokens.length < 2) return err; 1024 String[] nameValue = tokens[1].split("="); 1025 if (nameValue.length != 2) return err; 1026 1027 /* Handle the special case of reason=FREQ+CONFLICT */ 1028 if (nameValue[1].equals("FREQ_CONFLICT")) { 1029 return P2pStatus.NO_COMMON_CHANNEL; 1030 } 1031 try { 1032 err = P2pStatus.valueOf(Integer.parseInt(nameValue[1])); 1033 } catch (NumberFormatException e) { 1034 e.printStackTrace(); 1035 } 1036 return err; 1037 } 1038 1039 /** 1040 * Handle p2p events 1041 */ 1042 private void handleP2pEvents(String dataString) { 1043 if (dataString.startsWith(P2P_DEVICE_FOUND_STR)) { 1044 mStateMachine.sendMessage(P2P_DEVICE_FOUND_EVENT, new WifiP2pDevice(dataString)); 1045 } else if (dataString.startsWith(P2P_DEVICE_LOST_STR)) { 1046 mStateMachine.sendMessage(P2P_DEVICE_LOST_EVENT, new WifiP2pDevice(dataString)); 1047 } else if (dataString.startsWith(P2P_FIND_STOPPED_STR)) { 1048 mStateMachine.sendMessage(P2P_FIND_STOPPED_EVENT); 1049 } else if (dataString.startsWith(P2P_GO_NEG_REQUEST_STR)) { 1050 mStateMachine.sendMessage(P2P_GO_NEGOTIATION_REQUEST_EVENT, 1051 new WifiP2pConfig(dataString)); 1052 } else if (dataString.startsWith(P2P_GO_NEG_SUCCESS_STR)) { 1053 mStateMachine.sendMessage(P2P_GO_NEGOTIATION_SUCCESS_EVENT); 1054 } else if (dataString.startsWith(P2P_GO_NEG_FAILURE_STR)) { 1055 mStateMachine.sendMessage(P2P_GO_NEGOTIATION_FAILURE_EVENT, p2pError(dataString)); 1056 } else if (dataString.startsWith(P2P_GROUP_FORMATION_SUCCESS_STR)) { 1057 mStateMachine.sendMessage(P2P_GROUP_FORMATION_SUCCESS_EVENT); 1058 } else if (dataString.startsWith(P2P_GROUP_FORMATION_FAILURE_STR)) { 1059 mStateMachine.sendMessage(P2P_GROUP_FORMATION_FAILURE_EVENT, p2pError(dataString)); 1060 } else if (dataString.startsWith(P2P_GROUP_STARTED_STR)) { 1061 mStateMachine.sendMessage(P2P_GROUP_STARTED_EVENT, new WifiP2pGroup(dataString)); 1062 } else if (dataString.startsWith(P2P_GROUP_REMOVED_STR)) { 1063 mStateMachine.sendMessage(P2P_GROUP_REMOVED_EVENT, new WifiP2pGroup(dataString)); 1064 } else if (dataString.startsWith(P2P_INVITATION_RECEIVED_STR)) { 1065 mStateMachine.sendMessage(P2P_INVITATION_RECEIVED_EVENT, 1066 new WifiP2pGroup(dataString)); 1067 } else if (dataString.startsWith(P2P_INVITATION_RESULT_STR)) { 1068 mStateMachine.sendMessage(P2P_INVITATION_RESULT_EVENT, p2pError(dataString)); 1069 } else if (dataString.startsWith(P2P_PROV_DISC_PBC_REQ_STR)) { 1070 mStateMachine.sendMessage(P2P_PROV_DISC_PBC_REQ_EVENT, 1071 new WifiP2pProvDiscEvent(dataString)); 1072 } else if (dataString.startsWith(P2P_PROV_DISC_PBC_RSP_STR)) { 1073 mStateMachine.sendMessage(P2P_PROV_DISC_PBC_RSP_EVENT, 1074 new WifiP2pProvDiscEvent(dataString)); 1075 } else if (dataString.startsWith(P2P_PROV_DISC_ENTER_PIN_STR)) { 1076 mStateMachine.sendMessage(P2P_PROV_DISC_ENTER_PIN_EVENT, 1077 new WifiP2pProvDiscEvent(dataString)); 1078 } else if (dataString.startsWith(P2P_PROV_DISC_SHOW_PIN_STR)) { 1079 mStateMachine.sendMessage(P2P_PROV_DISC_SHOW_PIN_EVENT, 1080 new WifiP2pProvDiscEvent(dataString)); 1081 } else if (dataString.startsWith(P2P_PROV_DISC_FAILURE_STR)) { 1082 mStateMachine.sendMessage(P2P_PROV_DISC_FAILURE_EVENT); 1083 } else if (dataString.startsWith(P2P_SERV_DISC_RESP_STR)) { 1084 List<WifiP2pServiceResponse> list = WifiP2pServiceResponse.newInstance(dataString); 1085 if (list != null) { 1086 mStateMachine.sendMessage(P2P_SERV_DISC_RESP_EVENT, list); 1087 } else { 1088 Log.e(TAG, "Null service resp " + dataString); 1089 } 1090 } 1091 } 1092 1093 /** 1094 * Handle hostap events 1095 */ 1096 private void handleHostApEvents(String dataString) { 1097 String[] tokens = dataString.split(" "); 1098 /* AP-STA-CONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=02:90:4c:a0:92:54 */ 1099 if (tokens[0].equals(AP_STA_CONNECTED_STR)) { 1100 mStateMachine.sendMessage(AP_STA_CONNECTED_EVENT, new WifiP2pDevice(dataString)); 1101 /* AP-STA-DISCONNECTED 42:fc:89:a8:96:09 p2p_dev_addr=02:90:4c:a0:92:54 */ 1102 } else if (tokens[0].equals(AP_STA_DISCONNECTED_STR)) { 1103 mStateMachine.sendMessage(AP_STA_DISCONNECTED_EVENT, new WifiP2pDevice(dataString)); 1104 } 1105 } 1106 1107 /** 1108 * Handle ANQP events 1109 */ 1110 private void handleGasQueryEvents(String dataString) { 1111 // hs20 1112 if (mStateMachine2 == null) return; 1113 if (dataString.startsWith(GAS_QUERY_START_STR)) { 1114 mStateMachine2.sendMessage(GAS_QUERY_START_EVENT); 1115 } else if (dataString.startsWith(GAS_QUERY_DONE_STR)) { 1116 String[] dataTokens = dataString.split(" "); 1117 String bssid = null; 1118 int success = 0; 1119 for (String token : dataTokens) { 1120 String[] nameValue = token.split("="); 1121 if (nameValue.length != 2) { 1122 continue; 1123 } 1124 if (nameValue[0].equals("addr")) { 1125 bssid = nameValue[1]; 1126 continue; 1127 } 1128 if (nameValue[0].equals("result")) { 1129 success = nameValue[1].equals("SUCCESS") ? 1 : 0; 1130 continue; 1131 } 1132 } 1133 mStateMachine2.sendMessage(GAS_QUERY_DONE_EVENT, success, 0, bssid); 1134 } else { 1135 if (DBG) Log.d(TAG, "Unknown GAS query event: " + dataString); 1136 } 1137 } 1138 1139 /** 1140 * Handle HS20 events 1141 */ 1142 private void handleHs20Events(String dataString) { 1143 if (mStateMachine2 == null) return; 1144 if (dataString.startsWith(HS20_SUB_REM_STR)) { 1145 // format: HS20-SUBSCRIPTION-REMEDIATION osu_method, url 1146 String[] dataTokens = dataString.split(" "); 1147 int method = -1; 1148 String url = null; 1149 if (dataTokens.length >= 3) { 1150 method = Integer.parseInt(dataTokens[1]); 1151 url = dataTokens[2]; 1152 } 1153 mStateMachine2.sendMessage(HS20_REMEDIATION_EVENT, method, 0, url); 1154 } else if (dataString.startsWith(HS20_DEAUTH_STR)) { 1155 // format: HS20-DEAUTH-IMMINENT-NOTICE code, delay, url 1156 int code = -1; 1157 int delay = -1; 1158 String url = null; 1159 String[] dataTokens = dataString.split(" "); 1160 if (dataTokens.length >= 4) { 1161 code = Integer.parseInt(dataTokens[1]); 1162 delay = Integer.parseInt(dataTokens[2]); 1163 url = dataTokens[3]; 1164 } 1165 mStateMachine2.sendMessage(HS20_DEAUTH_EVENT, code, delay, url); 1166 } else { 1167 if (DBG) Log.d(TAG, "Unknown HS20 event: " + dataString); 1168 } 1169 } 1170 1171 /** 1172 * Handle Supplicant Requests 1173 */ 1174 private void handleRequests(String dataString) { 1175 String SSID = null; 1176 int reason = -2; 1177 String requestName = dataString.substring(REQUEST_PREFIX_LEN_STR); 1178 if (TextUtils.isEmpty(requestName)) { 1179 return; 1180 } 1181 if (requestName.startsWith(IDENTITY_STR)) { 1182 Matcher match = mRequestIdentityPattern.matcher(requestName); 1183 if (match.find()) { 1184 SSID = match.group(2); 1185 try { 1186 reason = Integer.parseInt(match.group(1)); 1187 } catch (NumberFormatException e) { 1188 reason = -1; 1189 } 1190 } else { 1191 Log.e(TAG, "didn't find SSID " + requestName); 1192 } 1193 mStateMachine.sendMessage(SUP_REQUEST_IDENTITY, eventLogCounter, reason, SSID); 1194 } if (requestName.startsWith(SIM_STR)) { 1195 Matcher match = mRequestGsmAuthPattern.matcher(requestName); 1196 if (match.find()) { 1197 WifiStateMachine.SimAuthRequestData data = 1198 new WifiStateMachine.SimAuthRequestData(); 1199 data.networkId = Integer.parseInt(match.group(1)); 1200 data.protocol = WifiEnterpriseConfig.Eap.SIM; 1201 data.ssid = match.group(4); 1202 data.challenges = match.group(2).split(":"); 1203 mStateMachine.sendMessage(SUP_REQUEST_SIM_AUTH, data); 1204 } else { 1205 Log.e(TAG, "couldn't parse SIM auth request - " + requestName); 1206 } 1207 1208 } else { 1209 if (DBG) Log.w(TAG, "couldn't identify request type - " + dataString); 1210 } 1211 } 1212 1213 /** 1214 * Handle the supplicant STATE-CHANGE event 1215 * @param dataString New supplicant state string in the format: 1216 * id=network-id state=new-state 1217 */ 1218 private void handleSupplicantStateChange(String dataString) { 1219 WifiSsid wifiSsid = null; 1220 int index = dataString.lastIndexOf("SSID="); 1221 if (index != -1) { 1222 wifiSsid = WifiSsid.createFromAsciiEncoded( 1223 dataString.substring(index + 5)); 1224 } 1225 String[] dataTokens = dataString.split(" "); 1226 1227 String BSSID = null; 1228 int networkId = -1; 1229 int newState = -1; 1230 for (String token : dataTokens) { 1231 String[] nameValue = token.split("="); 1232 if (nameValue.length != 2) { 1233 continue; 1234 } 1235 1236 if (nameValue[0].equals("BSSID")) { 1237 BSSID = nameValue[1]; 1238 continue; 1239 } 1240 1241 int value; 1242 try { 1243 value = Integer.parseInt(nameValue[1]); 1244 } catch (NumberFormatException e) { 1245 continue; 1246 } 1247 1248 if (nameValue[0].equals("id")) { 1249 networkId = value; 1250 } else if (nameValue[0].equals("state")) { 1251 newState = value; 1252 } 1253 } 1254 1255 if (newState == -1) return; 1256 1257 SupplicantState newSupplicantState = SupplicantState.INVALID; 1258 for (SupplicantState state : SupplicantState.values()) { 1259 if (state.ordinal() == newState) { 1260 newSupplicantState = state; 1261 break; 1262 } 1263 } 1264 if (newSupplicantState == SupplicantState.INVALID) { 1265 Log.w(TAG, "Invalid supplicant state: " + newState); 1266 } 1267 notifySupplicantStateChange(networkId, wifiSsid, BSSID, newSupplicantState); 1268 } 1269 1270 private void handleNetworkStateChange(NetworkInfo.DetailedState newState, String data) { 1271 String BSSID = null; 1272 int networkId = -1; 1273 int reason = 0; 1274 int ind = -1; 1275 int local = 0; 1276 Matcher match; 1277 if (newState == NetworkInfo.DetailedState.CONNECTED) { 1278 match = mConnectedEventPattern.matcher(data); 1279 if (!match.find()) { 1280 if (DBG) Log.d(TAG, "handleNetworkStateChange: Couldnt find BSSID in event string"); 1281 } else { 1282 BSSID = match.group(1); 1283 try { 1284 networkId = Integer.parseInt(match.group(2)); 1285 } catch (NumberFormatException e) { 1286 networkId = -1; 1287 } 1288 } 1289 notifyNetworkStateChange(newState, BSSID, networkId, reason); 1290 } else if (newState == NetworkInfo.DetailedState.DISCONNECTED) { 1291 match = mDisconnectedEventPattern.matcher(data); 1292 if (!match.find()) { 1293 if (DBG) Log.d(TAG, "handleNetworkStateChange: Could not parse disconnect string"); 1294 } else { 1295 BSSID = match.group(1); 1296 try { 1297 reason = Integer.parseInt(match.group(2)); 1298 } catch (NumberFormatException e) { 1299 reason = -1; 1300 } 1301 try { 1302 local = Integer.parseInt(match.group(3)); 1303 } catch (NumberFormatException e) { 1304 local = -1; 1305 } 1306 } 1307 notifyNetworkStateChange(newState, BSSID, local, reason); 1308 } 1309 } 1310 1311 /** 1312 * Send the state machine a notification that the state of Wifi connectivity 1313 * has changed. 1314 * @param newState the new network state 1315 * @param BSSID when the new state is {@link NetworkInfo.DetailedState#CONNECTED}, 1316 * this is the MAC address of the access point. Otherwise, it 1317 * is {@code null}. 1318 * @param netId the configured network on which the state change occurred 1319 */ 1320 void notifyNetworkStateChange(NetworkInfo.DetailedState newState, 1321 String BSSID, int netId, int reason) { 1322 if (newState == NetworkInfo.DetailedState.CONNECTED) { 1323 Message m = mStateMachine.obtainMessage(NETWORK_CONNECTION_EVENT, 1324 netId, reason, BSSID); 1325 mStateMachine.sendMessage(m); 1326 } else { 1327 1328 Message m = mStateMachine.obtainMessage(NETWORK_DISCONNECTION_EVENT, 1329 netId, reason, BSSID); 1330 if (DBG) logDbg("WifiMonitor notify network disconnect: " 1331 + BSSID 1332 + " reason=" + Integer.toString(reason)); 1333 mStateMachine.sendMessage(m); 1334 } 1335 } 1336 1337 /** 1338 * Send the state machine a notification that the state of the supplicant 1339 * has changed. 1340 * @param networkId the configured network on which the state change occurred 1341 * @param wifiSsid network name 1342 * @param BSSID network address 1343 * @param newState the new {@code SupplicantState} 1344 */ 1345 void notifySupplicantStateChange(int networkId, WifiSsid wifiSsid, String BSSID, 1346 SupplicantState newState) { 1347 mStateMachine.sendMessage(mStateMachine.obtainMessage(SUPPLICANT_STATE_CHANGE_EVENT, 1348 eventLogCounter, 0, 1349 new StateChangeResult(networkId, wifiSsid, BSSID, newState))); 1350 } 1351 } 1352