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