WifiNative.java revision 939177ff615062ec826601d536466875d8457375
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.wifi.BatchedScanSettings; 20import android.net.wifi.RttManager; 21import android.net.wifi.ScanResult; 22import android.net.wifi.WifiLinkLayerStats; 23import android.net.wifi.WifiManager; 24import android.net.wifi.WifiScanner; 25import android.net.wifi.RttManager; 26import android.net.wifi.WpsInfo; 27import android.net.wifi.p2p.WifiP2pConfig; 28import android.net.wifi.p2p.WifiP2pGroup; 29import android.net.wifi.p2p.nsd.WifiP2pServiceInfo; 30import android.os.SystemClock; 31import android.text.TextUtils; 32import android.util.LocalLog; 33import android.util.Log; 34 35import java.util.ArrayList; 36import java.util.List; 37import java.util.Locale; 38 39/** 40 * Native calls for bring up/shut down of the supplicant daemon and for 41 * sending requests to the supplicant daemon 42 * 43 * waitForEvent() is called on the monitor thread for events. All other methods 44 * must be serialized from the framework. 45 * 46 * {@hide} 47 */ 48public class WifiNative { 49 50 private static boolean DBG = false; 51 private final String mTAG; 52 private static final int DEFAULT_GROUP_OWNER_INTENT = 6; 53 54 static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED = 0; 55 static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED = 1; 56 static final int BLUETOOTH_COEXISTENCE_MODE_SENSE = 2; 57 58 static final int SCAN_WITHOUT_CONNECTION_SETUP = 1; 59 static final int SCAN_WITH_CONNECTION_SETUP = 2; 60 61 // Hold this lock before calling supplicant - it is required to 62 // mutually exclude access from Wifi and P2p state machines 63 static final Object mLock = new Object(); 64 65 public final String mInterfaceName; 66 public final String mInterfacePrefix; 67 68 private boolean mSuspendOptEnabled = false; 69 70 private static final int EID_HT_OPERATION = 61; 71 private static final int EID_VHT_OPERATION = 192; 72 private static final int EID_EXTENDED_CAPS = 127; 73 private static final int RTT_RESP_ENABLE_BIT = 70; 74 /* Register native functions */ 75 76 static { 77 /* Native functions are defined in libwifi-service.so */ 78 System.loadLibrary("wifi-service"); 79 registerNatives(); 80 } 81 82 private static native int registerNatives(); 83 84 public native static boolean loadDriver(); 85 86 public native static boolean isDriverLoaded(); 87 88 public native static boolean unloadDriver(); 89 90 public native static boolean startSupplicant(boolean p2pSupported); 91 92 /* Sends a kill signal to supplicant. To be used when we have lost connection 93 or when the supplicant is hung */ 94 public native static boolean killSupplicant(boolean p2pSupported); 95 96 private native boolean connectToSupplicantNative(); 97 98 private native void closeSupplicantConnectionNative(); 99 100 /** 101 * Wait for the supplicant to send an event, returning the event string. 102 * @return the event string sent by the supplicant. 103 */ 104 private native String waitForEventNative(); 105 106 private native boolean doBooleanCommandNative(String command); 107 108 private native int doIntCommandNative(String command); 109 110 private native String doStringCommandNative(String command); 111 112 public WifiNative(String interfaceName) { 113 mInterfaceName = interfaceName; 114 mTAG = "WifiNative-" + interfaceName; 115 if (!interfaceName.equals("p2p0")) { 116 mInterfacePrefix = "IFNAME=" + interfaceName + " "; 117 } else { 118 // commands for p2p0 interface don't need prefix 119 mInterfacePrefix = ""; 120 } 121 } 122 123 void enableVerboseLogging(int verbose) { 124 if (verbose > 0) { 125 DBG = true; 126 } else { 127 DBG = false; 128 } 129 } 130 131 private static final LocalLog mLocalLog = new LocalLog(1024); 132 133 // hold mLock before accessing mCmdIdLock 134 private static int sCmdId; 135 136 public LocalLog getLocalLog() { 137 return mLocalLog; 138 } 139 140 private static int getNewCmdIdLocked() { 141 return sCmdId++; 142 } 143 144 private void localLog(String s) { 145 if (mLocalLog != null) 146 mLocalLog.log(mInterfaceName + ": " + s); 147 } 148 149 public boolean connectToSupplicant() { 150 // No synchronization necessary .. it is implemented in WifiMonitor 151 localLog(mInterfacePrefix + "connectToSupplicant"); 152 return connectToSupplicantNative(); 153 } 154 155 public void closeSupplicantConnection() { 156 localLog(mInterfacePrefix + "closeSupplicantConnection"); 157 closeSupplicantConnectionNative(); 158 } 159 160 public String waitForEvent() { 161 // No synchronization necessary .. it is implemented in WifiMonitor 162 return waitForEventNative(); 163 } 164 165 private boolean doBooleanCommand(String command) { 166 if (DBG) Log.d(mTAG, "doBoolean: " + command); 167 synchronized (mLock) { 168 int cmdId = getNewCmdIdLocked(); 169 String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command; 170 boolean result = doBooleanCommandNative(mInterfacePrefix + command); 171 localLog(toLog + " -> " + result); 172 if (DBG) Log.d(mTAG, command + ": returned " + result); 173 return result; 174 } 175 } 176 177 private int doIntCommand(String command) { 178 if (DBG) Log.d(mTAG, "doInt: " + command); 179 synchronized (mLock) { 180 int cmdId = getNewCmdIdLocked(); 181 String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command; 182 int result = doIntCommandNative(mInterfacePrefix + command); 183 localLog(toLog + " -> " + result); 184 if (DBG) Log.d(mTAG, " returned " + result); 185 return result; 186 } 187 } 188 189 private String doStringCommand(String command) { 190 if (DBG) { 191 //GET_NETWORK commands flood the logs 192 if (!command.startsWith("GET_NETWORK")) { 193 Log.d(mTAG, "doString: [" + command + "]"); 194 } 195 } 196 synchronized (mLock) { 197 int cmdId = getNewCmdIdLocked(); 198 String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command; 199 String result = doStringCommandNative(mInterfacePrefix + command); 200 if (result == null) { 201 if (DBG) Log.d(mTAG, "doStringCommandNative no result"); 202 } else { 203 if (!command.startsWith("STATUS-")) { 204 localLog(toLog + " -> " + result); 205 } 206 if (DBG) Log.d(mTAG, " returned " + result.replace("\n", " ")); 207 } 208 return result; 209 } 210 } 211 212 private String doStringCommandWithoutLogging(String command) { 213 if (DBG) { 214 //GET_NETWORK commands flood the logs 215 if (!command.startsWith("GET_NETWORK")) { 216 Log.d(mTAG, "doString: [" + command + "]"); 217 } 218 } 219 synchronized (mLock) { 220 return doStringCommandNative(mInterfacePrefix + command); 221 } 222 } 223 224 public boolean ping() { 225 String pong = doStringCommand("PING"); 226 return (pong != null && pong.equals("PONG")); 227 } 228 229 public void setSupplicantLogLevel(String level) { 230 doStringCommand("LOG_LEVEL " + level); 231 } 232 233 public String getFreqCapability() { 234 return doStringCommand("GET_CAPABILITY freq"); 235 } 236 237 public boolean scan(int type, String freqList) { 238 if (type == SCAN_WITHOUT_CONNECTION_SETUP) { 239 if (freqList == null) return doBooleanCommand("SCAN TYPE=ONLY"); 240 else return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList); 241 } else if (type == SCAN_WITH_CONNECTION_SETUP) { 242 if (freqList == null) return doBooleanCommand("SCAN"); 243 else return doBooleanCommand("SCAN freq=" + freqList); 244 } else { 245 throw new IllegalArgumentException("Invalid scan type"); 246 } 247 } 248 249 /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta. 250 * 251 * Note that underneath we use a harsh-sounding "terminate" supplicant command 252 * for a graceful stop and a mild-sounding "stop" interface 253 * to kill the process 254 */ 255 public boolean stopSupplicant() { 256 return doBooleanCommand("TERMINATE"); 257 } 258 259 public String listNetworks() { 260 return doStringCommand("LIST_NETWORKS"); 261 } 262 263 public String listNetworks(int last_id) { 264 return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id); 265 } 266 267 public int addNetwork() { 268 return doIntCommand("ADD_NETWORK"); 269 } 270 271 public boolean setNetworkVariable(int netId, String name, String value) { 272 if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false; 273 return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value); 274 } 275 276 public String getNetworkVariable(int netId, String name) { 277 if (TextUtils.isEmpty(name)) return null; 278 279 // GET_NETWORK will likely flood the logs ... 280 return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name); 281 } 282 283 public boolean removeNetwork(int netId) { 284 return doBooleanCommand("REMOVE_NETWORK " + netId); 285 } 286 287 288 private void logDbg(String debug) { 289 long now = SystemClock.elapsedRealtimeNanos(); 290 String ts = String.format("[%,d us] ", now/1000); 291 Log.e("WifiNative: ", ts+debug+ " stack:" 292 + Thread.currentThread().getStackTrace()[2].getMethodName() +" - " 293 + Thread.currentThread().getStackTrace()[3].getMethodName() +" - " 294 + Thread.currentThread().getStackTrace()[4].getMethodName() +" - " 295 + Thread.currentThread().getStackTrace()[5].getMethodName()+" - " 296 + Thread.currentThread().getStackTrace()[6].getMethodName()); 297 298 } 299 public boolean enableNetwork(int netId, boolean disableOthers) { 300 if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId) 301 + " disableOthers=" + disableOthers); 302 if (disableOthers) { 303 return doBooleanCommand("SELECT_NETWORK " + netId); 304 } else { 305 return doBooleanCommand("ENABLE_NETWORK " + netId); 306 } 307 } 308 309 public boolean disableNetwork(int netId) { 310 if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId)); 311 return doBooleanCommand("DISABLE_NETWORK " + netId); 312 } 313 314 public boolean selectNetwork(int netId) { 315 if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId)); 316 return doBooleanCommand("SELECT_NETWORK " + netId); 317 } 318 319 public boolean reconnect() { 320 if (DBG) logDbg("RECONNECT "); 321 return doBooleanCommand("RECONNECT"); 322 } 323 324 public boolean reassociate() { 325 if (DBG) logDbg("REASSOCIATE "); 326 return doBooleanCommand("REASSOCIATE"); 327 } 328 329 public boolean disconnect() { 330 if (DBG) logDbg("DISCONNECT "); 331 return doBooleanCommand("DISCONNECT"); 332 } 333 334 public String status() { 335 return status(false); 336 } 337 338 public String status(boolean noEvents) { 339 if (noEvents) { 340 return doStringCommand("STATUS-NO_EVENTS"); 341 } else { 342 return doStringCommand("STATUS"); 343 } 344 } 345 346 public String getMacAddress() { 347 //Macaddr = XX.XX.XX.XX.XX.XX 348 String ret = doStringCommand("DRIVER MACADDR"); 349 if (!TextUtils.isEmpty(ret)) { 350 String[] tokens = ret.split(" = "); 351 if (tokens.length == 2) return tokens[1]; 352 } 353 return null; 354 } 355 356 357 358 /** 359 * Format of results: 360 * ================= 361 * id=1 362 * bssid=68:7f:74:d7:1b:6e 363 * freq=2412 364 * level=-43 365 * tsf=1344621975160944 366 * age=2623 367 * flags=[WPA2-PSK-CCMP][WPS][ESS] 368 * ssid=zubyb 369 * ==== 370 * 371 * RANGE=ALL gets all scan results 372 * RANGE=ID- gets results from ID 373 * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details 374 * 0 0 1 0 2 375 * WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM | WPA_BSS_MASK_WIFI_DISPLAY 376 * 0 0 0 1 1 -> 9 377 * WPA_BSS_MASK_INTERNETW | WPA_BSS_MASK_P2P_SCAN | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID 378 * 1 0 0 1 9 -> d 379 * WPA_BSS_MASK_FLAGS | WPA_BSS_MASK_IE | WPA_BSS_MASK_AGE | WPA_BSS_MASK_TSF 380 * 1 0 0 0 8 381 * WPA_BSS_MASK_LEVEL | WPA_BSS_MASK_NOISE | WPA_BSS_MASK_QUAL | WPA_BSS_MASK_CAPABILITIES 382 * 0 1 1 1 7 383 * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ | WPA_BSS_MASK_BSSID | WPA_BSS_MASK_ID 384 * 385 * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176) 386 * 387 * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884 388 * wpa_supplicant_ctrl_iface_bss:4315 389 * print_bss_info 390 */ 391 public String scanResults(int sid) { 392 return doStringCommandWithoutLogging("BSS RANGE=" + sid + "- MASK=0x29d87"); 393 } 394 395 public String doCustomCommand(String command) { 396 return doStringCommand(command); 397 } 398 399 /** 400 * Format of result: 401 * id=1016 402 * bssid=00:03:7f:40:84:10 403 * freq=2462 404 * beacon_int=200 405 * capabilities=0x0431 406 * qual=0 407 * noise=0 408 * level=-46 409 * tsf=0000002669008476 410 * age=5 411 * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555... 412 * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20] 413 * ssid=QCA-HS20-R2-TEST 414 * p2p_device_name= 415 * p2p_config_methods=0x0SET_NE 416 * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f... 417 * anqp_network_auth_type=010000 418 * anqp_roaming_consortium=03506f9a05001bc504bd 419 * anqp_ip_addr_type_availability=0c 420 * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2... 421 * anqp_3gpp=000600040132f465 422 * anqp_domain_name=0b65786d61706c652e636f6d 423 * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869... 424 * hs20_wan_metrics=01c40900008001000000000a00 425 * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0... 426 * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d... 427 */ 428 public String scanResult(String bssid) { 429 return doStringCommand("BSS " + bssid); 430 } 431 432 /** 433 * Format of command 434 * DRIVER WLS_BATCHING SET SCANFREQ=x MSCAN=r BESTN=y CHANNEL=<z, w, t> RTT=s 435 * where x is an ascii representation of an integer number of seconds between scans 436 * r is an ascii representation of an integer number of scans per batch 437 * y is an ascii representation of an integer number of the max AP to remember per scan 438 * z, w, t represent a 1..n size list of channel numbers and/or 'A', 'B' values 439 * indicating entire ranges of channels 440 * s is an ascii representation of an integer number of highest-strength AP 441 * for which we'd like approximate distance reported 442 * 443 * The return value is an ascii integer representing a guess of the number of scans 444 * the firmware can remember before it runs out of buffer space or -1 on error 445 */ 446 public String setBatchedScanSettings(BatchedScanSettings settings) { 447 if (settings == null) { 448 return doStringCommand("DRIVER WLS_BATCHING STOP"); 449 } 450 String cmd = "DRIVER WLS_BATCHING SET SCANFREQ=" + settings.scanIntervalSec; 451 cmd += " MSCAN=" + settings.maxScansPerBatch; 452 if (settings.maxApPerScan != BatchedScanSettings.UNSPECIFIED) { 453 cmd += " BESTN=" + settings.maxApPerScan; 454 } 455 if (settings.channelSet != null && !settings.channelSet.isEmpty()) { 456 cmd += " CHANNEL=<"; 457 int i = 0; 458 for (String channel : settings.channelSet) { 459 cmd += (i > 0 ? "," : "") + channel; 460 ++i; 461 } 462 cmd += ">"; 463 } 464 if (settings.maxApForDistance != BatchedScanSettings.UNSPECIFIED) { 465 cmd += " RTT=" + settings.maxApForDistance; 466 } 467 return doStringCommand(cmd); 468 } 469 470 public String getBatchedScanResults() { 471 return doStringCommand("DRIVER WLS_BATCHING GET"); 472 } 473 474 public boolean startDriver() { 475 return doBooleanCommand("DRIVER START"); 476 } 477 478 public boolean stopDriver() { 479 return doBooleanCommand("DRIVER STOP"); 480 } 481 482 483 /** 484 * Start filtering out Multicast V4 packets 485 * @return {@code true} if the operation succeeded, {@code false} otherwise 486 * 487 * Multicast filtering rules work as follows: 488 * 489 * The driver can filter multicast (v4 and/or v6) and broadcast packets when in 490 * a power optimized mode (typically when screen goes off). 491 * 492 * In order to prevent the driver from filtering the multicast/broadcast packets, we have to 493 * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective 494 * 495 * DRIVER RXFILTER-ADD Num 496 * where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6 497 * 498 * and DRIVER RXFILTER-START 499 * In order to stop the usage of these rules, we do 500 * 501 * DRIVER RXFILTER-STOP 502 * DRIVER RXFILTER-REMOVE Num 503 * where Num is as described for RXFILTER-ADD 504 * 505 * The SETSUSPENDOPT driver command overrides the filtering rules 506 */ 507 public boolean startFilteringMulticastV4Packets() { 508 return doBooleanCommand("DRIVER RXFILTER-STOP") 509 && doBooleanCommand("DRIVER RXFILTER-REMOVE 2") 510 && doBooleanCommand("DRIVER RXFILTER-START"); 511 } 512 513 /** 514 * Stop filtering out Multicast V4 packets. 515 * @return {@code true} if the operation succeeded, {@code false} otherwise 516 */ 517 public boolean stopFilteringMulticastV4Packets() { 518 return doBooleanCommand("DRIVER RXFILTER-STOP") 519 && doBooleanCommand("DRIVER RXFILTER-ADD 2") 520 && doBooleanCommand("DRIVER RXFILTER-START"); 521 } 522 523 /** 524 * Start filtering out Multicast V6 packets 525 * @return {@code true} if the operation succeeded, {@code false} otherwise 526 */ 527 public boolean startFilteringMulticastV6Packets() { 528 return doBooleanCommand("DRIVER RXFILTER-STOP") 529 && doBooleanCommand("DRIVER RXFILTER-REMOVE 3") 530 && doBooleanCommand("DRIVER RXFILTER-START"); 531 } 532 533 /** 534 * Stop filtering out Multicast V6 packets. 535 * @return {@code true} if the operation succeeded, {@code false} otherwise 536 */ 537 public boolean stopFilteringMulticastV6Packets() { 538 return doBooleanCommand("DRIVER RXFILTER-STOP") 539 && doBooleanCommand("DRIVER RXFILTER-ADD 3") 540 && doBooleanCommand("DRIVER RXFILTER-START"); 541 } 542 543 /** 544 * Set the operational frequency band 545 * @param band One of 546 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO}, 547 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ}, 548 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ}, 549 * @return {@code true} if the operation succeeded, {@code false} otherwise 550 */ 551 public boolean setBand(int band) { 552 String bandstr; 553 554 if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ) 555 bandstr = "5G"; 556 else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ) 557 bandstr = "2G"; 558 else 559 bandstr = "AUTO"; 560 return doBooleanCommand("SET SETBAND " + bandstr); 561 } 562 563 /** 564 * Sets the bluetooth coexistence mode. 565 * 566 * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED}, 567 * {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or 568 * {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}. 569 * @return Whether the mode was successfully set. 570 */ 571 public boolean setBluetoothCoexistenceMode(int mode) { 572 return doBooleanCommand("DRIVER BTCOEXMODE " + mode); 573 } 574 575 /** 576 * Enable or disable Bluetooth coexistence scan mode. When this mode is on, 577 * some of the low-level scan parameters used by the driver are changed to 578 * reduce interference with A2DP streaming. 579 * 580 * @param isSet whether to enable or disable this mode 581 * @return {@code true} if the command succeeded, {@code false} otherwise. 582 */ 583 public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) { 584 if (setCoexScanMode) { 585 return doBooleanCommand("DRIVER BTCOEXSCAN-START"); 586 } else { 587 return doBooleanCommand("DRIVER BTCOEXSCAN-STOP"); 588 } 589 } 590 591 public void enableSaveConfig() { 592 doBooleanCommand("SET update_config 1"); 593 } 594 595 public boolean saveConfig() { 596 return doBooleanCommand("SAVE_CONFIG"); 597 } 598 599 public boolean addToBlacklist(String bssid) { 600 if (TextUtils.isEmpty(bssid)) return false; 601 return doBooleanCommand("BLACKLIST " + bssid); 602 } 603 604 public boolean clearBlacklist() { 605 return doBooleanCommand("BLACKLIST clear"); 606 } 607 608 public boolean setSuspendOptimizations(boolean enabled) { 609 // if (mSuspendOptEnabled == enabled) return true; 610 mSuspendOptEnabled = enabled; 611 612 Log.e("native", "do suspend " + enabled); 613 if (enabled) { 614 return doBooleanCommand("DRIVER SETSUSPENDMODE 1"); 615 } else { 616 return doBooleanCommand("DRIVER SETSUSPENDMODE 0"); 617 } 618 } 619 620 public boolean setCountryCode(String countryCode) { 621 if (countryCode != null) 622 return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT)); 623 else 624 return doBooleanCommand("DRIVER COUNTRY"); 625 } 626 627 public void enableBackgroundScan(boolean enable) { 628 if (enable) { 629 doBooleanCommand("SET pno 1"); 630 } else { 631 doBooleanCommand("SET pno 0"); 632 } 633 } 634 635 public void enableAutoConnect(boolean enable) { 636 if (enable) { 637 doBooleanCommand("STA_AUTOCONNECT 1"); 638 } else { 639 doBooleanCommand("STA_AUTOCONNECT 0"); 640 } 641 } 642 643 public void setScanInterval(int scanInterval) { 644 doBooleanCommand("SCAN_INTERVAL " + scanInterval); 645 } 646 647 public void startTdls(String macAddr, boolean enable) { 648 if (enable) { 649 doBooleanCommand("TDLS_DISCOVER " + macAddr); 650 doBooleanCommand("TDLS_SETUP " + macAddr); 651 } else { 652 doBooleanCommand("TDLS_TEARDOWN " + macAddr); 653 } 654 } 655 656 /** Example output: 657 * RSSI=-65 658 * LINKSPEED=48 659 * NOISE=9999 660 * FREQUENCY=0 661 */ 662 public String signalPoll() { 663 return doStringCommandWithoutLogging("SIGNAL_POLL"); 664 } 665 666 /** Example outout: 667 * TXGOOD=396 668 * TXBAD=1 669 */ 670 public String pktcntPoll() { 671 return doStringCommand("PKTCNT_POLL"); 672 } 673 674 public void bssFlush() { 675 doBooleanCommand("BSS_FLUSH 0"); 676 } 677 678 public boolean startWpsPbc(String bssid) { 679 if (TextUtils.isEmpty(bssid)) { 680 return doBooleanCommand("WPS_PBC"); 681 } else { 682 return doBooleanCommand("WPS_PBC " + bssid); 683 } 684 } 685 686 public boolean startWpsPbc(String iface, String bssid) { 687 synchronized (mLock) { 688 if (TextUtils.isEmpty(bssid)) { 689 return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC"); 690 } else { 691 return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid); 692 } 693 } 694 } 695 696 public boolean startWpsPinKeypad(String pin) { 697 if (TextUtils.isEmpty(pin)) return false; 698 return doBooleanCommand("WPS_PIN any " + pin); 699 } 700 701 public boolean startWpsPinKeypad(String iface, String pin) { 702 if (TextUtils.isEmpty(pin)) return false; 703 synchronized (mLock) { 704 return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin); 705 } 706 } 707 708 709 public String startWpsPinDisplay(String bssid) { 710 if (TextUtils.isEmpty(bssid)) { 711 return doStringCommand("WPS_PIN any"); 712 } else { 713 return doStringCommand("WPS_PIN " + bssid); 714 } 715 } 716 717 public String startWpsPinDisplay(String iface, String bssid) { 718 synchronized (mLock) { 719 if (TextUtils.isEmpty(bssid)) { 720 return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any"); 721 } else { 722 return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid); 723 } 724 } 725 } 726 727 public boolean setExternalSim(boolean external) { 728 synchronized (mLock) { 729 String value = external ? "1" : "0"; 730 Log.d(TAG, "Setting external_sim to " + value); 731 return doBooleanCommand("SET external_sim " + value); 732 } 733 } 734 735 public boolean simAuthResponse(int id, String response) { 736 synchronized (mLock) { 737 return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-AUTH" + response); 738 } 739 } 740 741 /* Configures an access point connection */ 742 public boolean startWpsRegistrar(String bssid, String pin) { 743 if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false; 744 return doBooleanCommand("WPS_REG " + bssid + " " + pin); 745 } 746 747 public boolean cancelWps() { 748 return doBooleanCommand("WPS_CANCEL"); 749 } 750 751 public boolean setPersistentReconnect(boolean enabled) { 752 int value = (enabled == true) ? 1 : 0; 753 return doBooleanCommand("SET persistent_reconnect " + value); 754 } 755 756 public boolean setDeviceName(String name) { 757 return doBooleanCommand("SET device_name " + name); 758 } 759 760 public boolean setDeviceType(String type) { 761 return doBooleanCommand("SET device_type " + type); 762 } 763 764 public boolean setConfigMethods(String cfg) { 765 return doBooleanCommand("SET config_methods " + cfg); 766 } 767 768 public boolean setManufacturer(String value) { 769 return doBooleanCommand("SET manufacturer " + value); 770 } 771 772 public boolean setModelName(String value) { 773 return doBooleanCommand("SET model_name " + value); 774 } 775 776 public boolean setModelNumber(String value) { 777 return doBooleanCommand("SET model_number " + value); 778 } 779 780 public boolean setSerialNumber(String value) { 781 return doBooleanCommand("SET serial_number " + value); 782 } 783 784 public boolean setP2pSsidPostfix(String postfix) { 785 return doBooleanCommand("SET p2p_ssid_postfix " + postfix); 786 } 787 788 public boolean setP2pGroupIdle(String iface, int time) { 789 synchronized (mLock) { 790 return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time); 791 } 792 } 793 794 public void setPowerSave(boolean enabled) { 795 if (enabled) { 796 doBooleanCommand("SET ps 1"); 797 } else { 798 doBooleanCommand("SET ps 0"); 799 } 800 } 801 802 public boolean setP2pPowerSave(String iface, boolean enabled) { 803 synchronized (mLock) { 804 if (enabled) { 805 return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1"); 806 } else { 807 return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0"); 808 } 809 } 810 } 811 812 public boolean setWfdEnable(boolean enable) { 813 return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0")); 814 } 815 816 public boolean setWfdDeviceInfo(String hex) { 817 return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex); 818 } 819 820 /** 821 * "sta" prioritizes STA connection over P2P and "p2p" prioritizes 822 * P2P connection over STA 823 */ 824 public boolean setConcurrencyPriority(String s) { 825 return doBooleanCommand("P2P_SET conc_pref " + s); 826 } 827 828 public boolean p2pFind() { 829 return doBooleanCommand("P2P_FIND"); 830 } 831 832 public boolean p2pFind(int timeout) { 833 if (timeout <= 0) { 834 return p2pFind(); 835 } 836 return doBooleanCommand("P2P_FIND " + timeout); 837 } 838 839 public boolean p2pStopFind() { 840 return doBooleanCommand("P2P_STOP_FIND"); 841 } 842 843 public boolean p2pListen() { 844 return doBooleanCommand("P2P_LISTEN"); 845 } 846 847 public boolean p2pListen(int timeout) { 848 if (timeout <= 0) { 849 return p2pListen(); 850 } 851 return doBooleanCommand("P2P_LISTEN " + timeout); 852 } 853 854 public boolean p2pExtListen(boolean enable, int period, int interval) { 855 if (enable && interval < period) { 856 return false; 857 } 858 return doBooleanCommand("P2P_EXT_LISTEN" 859 + (enable ? (" " + period + " " + interval) : "")); 860 } 861 862 public boolean p2pSetChannel(int lc, int oc) { 863 if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc); 864 865 if (lc >=1 && lc <= 11) { 866 if (!doBooleanCommand("P2P_SET listen_channel " + lc)) { 867 return false; 868 } 869 } else if (lc != 0) { 870 return false; 871 } 872 873 if (oc >= 1 && oc <= 165 ) { 874 int freq = (oc <= 14 ? 2407 : 5000) + oc * 5; 875 return doBooleanCommand("P2P_SET disallow_freq 1000-" 876 + (freq - 5) + "," + (freq + 5) + "-6000"); 877 } else if (oc == 0) { 878 /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */ 879 return doBooleanCommand("P2P_SET disallow_freq \"\""); 880 } 881 882 return false; 883 } 884 885 public boolean p2pFlush() { 886 return doBooleanCommand("P2P_FLUSH"); 887 } 888 889 /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad] 890 [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */ 891 public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) { 892 if (config == null) return null; 893 List<String> args = new ArrayList<String>(); 894 WpsInfo wps = config.wps; 895 args.add(config.deviceAddress); 896 897 switch (wps.setup) { 898 case WpsInfo.PBC: 899 args.add("pbc"); 900 break; 901 case WpsInfo.DISPLAY: 902 if (TextUtils.isEmpty(wps.pin)) { 903 args.add("pin"); 904 } else { 905 args.add(wps.pin); 906 } 907 args.add("display"); 908 break; 909 case WpsInfo.KEYPAD: 910 args.add(wps.pin); 911 args.add("keypad"); 912 break; 913 case WpsInfo.LABEL: 914 args.add(wps.pin); 915 args.add("label"); 916 default: 917 break; 918 } 919 920 if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) { 921 args.add("persistent"); 922 } 923 924 if (joinExistingGroup) { 925 args.add("join"); 926 } else { 927 //TODO: This can be adapted based on device plugged in state and 928 //device battery state 929 int groupOwnerIntent = config.groupOwnerIntent; 930 if (groupOwnerIntent < 0 || groupOwnerIntent > 15) { 931 groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT; 932 } 933 args.add("go_intent=" + groupOwnerIntent); 934 } 935 936 String command = "P2P_CONNECT "; 937 for (String s : args) command += s + " "; 938 939 return doStringCommand(command); 940 } 941 942 public boolean p2pCancelConnect() { 943 return doBooleanCommand("P2P_CANCEL"); 944 } 945 946 public boolean p2pProvisionDiscovery(WifiP2pConfig config) { 947 if (config == null) return false; 948 949 switch (config.wps.setup) { 950 case WpsInfo.PBC: 951 return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc"); 952 case WpsInfo.DISPLAY: 953 //We are doing display, so provision discovery is keypad 954 return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad"); 955 case WpsInfo.KEYPAD: 956 //We are doing keypad, so provision discovery is display 957 return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display"); 958 default: 959 break; 960 } 961 return false; 962 } 963 964 public boolean p2pGroupAdd(boolean persistent) { 965 if (persistent) { 966 return doBooleanCommand("P2P_GROUP_ADD persistent"); 967 } 968 return doBooleanCommand("P2P_GROUP_ADD"); 969 } 970 971 public boolean p2pGroupAdd(int netId) { 972 return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId); 973 } 974 975 public boolean p2pGroupRemove(String iface) { 976 if (TextUtils.isEmpty(iface)) return false; 977 synchronized (mLock) { 978 return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface); 979 } 980 } 981 982 public boolean p2pReject(String deviceAddress) { 983 return doBooleanCommand("P2P_REJECT " + deviceAddress); 984 } 985 986 /* Invite a peer to a group */ 987 public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) { 988 if (TextUtils.isEmpty(deviceAddress)) return false; 989 990 if (group == null) { 991 return doBooleanCommand("P2P_INVITE peer=" + deviceAddress); 992 } else { 993 return doBooleanCommand("P2P_INVITE group=" + group.getInterface() 994 + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress); 995 } 996 } 997 998 /* Reinvoke a persistent connection */ 999 public boolean p2pReinvoke(int netId, String deviceAddress) { 1000 if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false; 1001 1002 return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress); 1003 } 1004 1005 public String p2pGetSsid(String deviceAddress) { 1006 return p2pGetParam(deviceAddress, "oper_ssid"); 1007 } 1008 1009 public String p2pGetDeviceAddress() { 1010 1011 Log.d(TAG, "p2pGetDeviceAddress"); 1012 1013 String status = null; 1014 1015 /* Explicitly calling the API without IFNAME= prefix to take care of the devices that 1016 don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */ 1017 1018 synchronized (mLock) { 1019 status = doStringCommandNative("STATUS"); 1020 } 1021 1022 String result = ""; 1023 if (status != null) { 1024 String[] tokens = status.split("\n"); 1025 for (String token : tokens) { 1026 if (token.startsWith("p2p_device_address=")) { 1027 String[] nameValue = token.split("="); 1028 if (nameValue.length != 2) 1029 break; 1030 result = nameValue[1]; 1031 } 1032 } 1033 } 1034 1035 Log.d(TAG, "p2pGetDeviceAddress returning " + result); 1036 return result; 1037 } 1038 1039 public int getGroupCapability(String deviceAddress) { 1040 int gc = 0; 1041 if (TextUtils.isEmpty(deviceAddress)) return gc; 1042 String peerInfo = p2pPeer(deviceAddress); 1043 if (TextUtils.isEmpty(peerInfo)) return gc; 1044 1045 String[] tokens = peerInfo.split("\n"); 1046 for (String token : tokens) { 1047 if (token.startsWith("group_capab=")) { 1048 String[] nameValue = token.split("="); 1049 if (nameValue.length != 2) break; 1050 try { 1051 return Integer.decode(nameValue[1]); 1052 } catch(NumberFormatException e) { 1053 return gc; 1054 } 1055 } 1056 } 1057 return gc; 1058 } 1059 1060 public String p2pPeer(String deviceAddress) { 1061 return doStringCommand("P2P_PEER " + deviceAddress); 1062 } 1063 1064 private String p2pGetParam(String deviceAddress, String key) { 1065 if (deviceAddress == null) return null; 1066 1067 String peerInfo = p2pPeer(deviceAddress); 1068 if (peerInfo == null) return null; 1069 String[] tokens= peerInfo.split("\n"); 1070 1071 key += "="; 1072 for (String token : tokens) { 1073 if (token.startsWith(key)) { 1074 String[] nameValue = token.split("="); 1075 if (nameValue.length != 2) break; 1076 return nameValue[1]; 1077 } 1078 } 1079 return null; 1080 } 1081 1082 public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) { 1083 /* 1084 * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump> 1085 * P2P_SERVICE_ADD upnp <version hex> <service> 1086 * 1087 * e.g) 1088 * [Bonjour] 1089 * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.) 1090 * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027 1091 * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript) 1092 * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001 1093 * 09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074 1094 * 1095 * [UPnP] 1096 * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012 1097 * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice 1098 * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp 1099 * -org:device:InternetGatewayDevice:1 1100 * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp 1101 * -org:service:ContentDirectory:2 1102 */ 1103 for (String s : servInfo.getSupplicantQueryList()) { 1104 String command = "P2P_SERVICE_ADD"; 1105 command += (" " + s); 1106 if (!doBooleanCommand(command)) { 1107 return false; 1108 } 1109 } 1110 return true; 1111 } 1112 1113 public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) { 1114 /* 1115 * P2P_SERVICE_DEL bonjour <query hexdump> 1116 * P2P_SERVICE_DEL upnp <version hex> <service> 1117 */ 1118 for (String s : servInfo.getSupplicantQueryList()) { 1119 String command = "P2P_SERVICE_DEL "; 1120 1121 String[] data = s.split(" "); 1122 if (data.length < 2) { 1123 return false; 1124 } 1125 if ("upnp".equals(data[0])) { 1126 command += s; 1127 } else if ("bonjour".equals(data[0])) { 1128 command += data[0]; 1129 command += (" " + data[1]); 1130 } else { 1131 return false; 1132 } 1133 if (!doBooleanCommand(command)) { 1134 return false; 1135 } 1136 } 1137 return true; 1138 } 1139 1140 public boolean p2pServiceFlush() { 1141 return doBooleanCommand("P2P_SERVICE_FLUSH"); 1142 } 1143 1144 public String p2pServDiscReq(String addr, String query) { 1145 String command = "P2P_SERV_DISC_REQ"; 1146 command += (" " + addr); 1147 command += (" " + query); 1148 1149 return doStringCommand(command); 1150 } 1151 1152 public boolean p2pServDiscCancelReq(String id) { 1153 return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id); 1154 } 1155 1156 /* Set the current mode of miracast operation. 1157 * 0 = disabled 1158 * 1 = operating as source 1159 * 2 = operating as sink 1160 */ 1161 public void setMiracastMode(int mode) { 1162 // Note: optional feature on the driver. It is ok for this to fail. 1163 doBooleanCommand("DRIVER MIRACAST " + mode); 1164 } 1165 1166 public boolean fetchAnqp(String bssid, String subtypes) { 1167 return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes); 1168 } 1169 1170 /* WIFI HAL support */ 1171 1172 private static final String TAG = "WifiNative-HAL"; 1173 private static long sWifiHalHandle = 0; /* used by JNI to save wifi_handle */ 1174 private static long[] sWifiIfaceHandles = null; /* used by JNI to save interface handles */ 1175 private static int sWlan0Index = -1; 1176 private static int sP2p0Index = -1; 1177 1178 private static boolean sHalIsStarted = false; 1179 private static boolean sHalFailed = false; 1180 1181 private static native boolean startHalNative(); 1182 private static native void stopHalNative(); 1183 private static native void waitForHalEventNative(); 1184 1185 private static class MonitorThread extends Thread { 1186 public void run() { 1187 Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle)); 1188 waitForHalEventNative(); 1189 } 1190 } 1191 1192 synchronized public static boolean startHal() { 1193 Log.i(TAG, "startHal"); 1194 synchronized (mLock) { 1195 if (sHalFailed) 1196 return false; 1197 if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) { 1198 new MonitorThread().start(); 1199 sHalIsStarted = true; 1200 return true; 1201 } else { 1202 Log.i(TAG, "Could not start hal"); 1203 sHalIsStarted = false; 1204 sHalFailed = true; 1205 return false; 1206 } 1207 } 1208 } 1209 1210 synchronized public static void stopHal() { 1211 stopHalNative(); 1212 } 1213 1214 private static native int getInterfacesNative(); 1215 1216 synchronized public static int getInterfaces() { 1217 synchronized (mLock) { 1218 if (sWifiIfaceHandles == null) { 1219 int num = getInterfacesNative(); 1220 int wifi_num = 0; 1221 for (int i = 0; i < num; i++) { 1222 String name = getInterfaceNameNative(i); 1223 Log.i(TAG, "interface[" + i + "] = " + name); 1224 if (name.equals("wlan0")) { 1225 sWlan0Index = i; 1226 wifi_num++; 1227 } else if (name.equals("p2p0")) { 1228 sP2p0Index = i; 1229 wifi_num++; 1230 } 1231 } 1232 return wifi_num; 1233 } else { 1234 return sWifiIfaceHandles.length; 1235 } 1236 } 1237 } 1238 1239 private static native String getInterfaceNameNative(int index); 1240 synchronized public static String getInterfaceName(int index) { 1241 return getInterfaceNameNative(index); 1242 } 1243 1244 public static class ScanCapabilities { 1245 public int max_scan_cache_size; // in number of scan results?? 1246 public int max_scan_buckets; 1247 public int max_ap_cache_per_scan; 1248 public int max_rssi_sample_size; 1249 public int max_scan_reporting_threshold; // in number of scan results?? 1250 public int max_hotlist_bssids; 1251 public int max_significant_wifi_change_aps; 1252 } 1253 1254 public static boolean getScanCapabilities(ScanCapabilities capabilities) { 1255 return getScanCapabilitiesNative(sWlan0Index, capabilities); 1256 } 1257 1258 private static native boolean getScanCapabilitiesNative( 1259 int iface, ScanCapabilities capabilities); 1260 1261 private static native boolean startScanNative(int iface, int id, ScanSettings settings); 1262 private static native boolean stopScanNative(int iface, int id); 1263 private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush); 1264 private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface); 1265 1266 public static class ChannelSettings { 1267 int frequency; 1268 int dwell_time_ms; 1269 boolean passive; 1270 } 1271 1272 public static class BucketSettings { 1273 int bucket; 1274 int band; 1275 int period_ms; 1276 int report_events; 1277 int num_channels; 1278 ChannelSettings channels[]; 1279 } 1280 1281 public static class ScanSettings { 1282 int base_period_ms; 1283 int max_ap_per_scan; 1284 int report_threshold_percent; 1285 int report_threshold_num_scans; 1286 int num_buckets; 1287 BucketSettings buckets[]; 1288 } 1289 1290 public static interface ScanEventHandler { 1291 void onScanResultsAvailable(); 1292 void onFullScanResult(ScanResult fullScanResult); 1293 void onScanStatus(); 1294 void onScanPaused(WifiScanner.ScanData[] data); 1295 void onScanRestarted(); 1296 } 1297 1298 synchronized static void onScanResultsAvailable(int id) { 1299 if (sScanEventHandler != null) { 1300 sScanEventHandler.onScanResultsAvailable(); 1301 } 1302 } 1303 1304 /* scan status, keep these values in sync with gscan.h */ 1305 private static int WIFI_SCAN_BUFFER_FULL = 0; 1306 private static int WIFI_SCAN_COMPLETE = 1; 1307 1308 synchronized static void onScanStatus(int status) { 1309 Log.i(TAG, "Got a scan status changed event, status = " + status); 1310 1311 if (status == WIFI_SCAN_BUFFER_FULL) { 1312 /* we have a separate event to take care of this */ 1313 } else if (status == WIFI_SCAN_COMPLETE) { 1314 if (sScanEventHandler != null) { 1315 sScanEventHandler.onScanStatus(); 1316 } 1317 } 1318 } 1319 1320 synchronized static void onFullScanResult(int id, ScanResult result, byte bytes[]) { 1321 if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " + 1322 "num = " + bytes.length); 1323 1324 if (sScanEventHandler == null) { 1325 return; 1326 } 1327 1328 int num = 0; 1329 for (int i = 0; i < bytes.length; ) { 1330 int type = bytes[i] & 0xFF; 1331 int len = bytes[i + 1] & 0xFF; 1332 1333 if (i + len + 2 > bytes.length) { 1334 Log.w(TAG, "bad length " + len + " of IE " + type + " from " + result.BSSID); 1335 Log.w(TAG, "ignoring the rest of the IEs"); 1336 break; 1337 } 1338 num++; 1339 i += len + 2; 1340 if (DBG) Log.i(TAG, "bytes[" + i + "] = [" + type + ", " + len + "]" + ", " + 1341 "next = " + i); 1342 } 1343 1344 int secondChanelOffset = 0; 1345 byte channelMode = 0; 1346 byte centerFreqIndex1 = 0; 1347 byte centerFreqIndex2 = 0; 1348 result.is80211McRTTResponder = false; 1349 1350 ScanResult.InformationElement elements[] = new ScanResult.InformationElement[num]; 1351 for (int i = 0, index = 0; i < num; i++) { 1352 int type = bytes[index] & 0xFF; 1353 int len = bytes[index + 1] & 0xFF; 1354 if (DBG) Log.i(TAG, "index = " + index + ", type = " + type + ", len = " + len); 1355 ScanResult.InformationElement elem = new ScanResult.InformationElement(); 1356 elem.id = type; 1357 elem.bytes = new byte[len]; 1358 for (int j = 0; j < len; j++) { 1359 elem.bytes[j] = bytes[index + j + 2]; 1360 } 1361 elements[i] = elem; 1362 int inforStart = index + 2; 1363 index += (len + 2); 1364 1365 if(type == EID_HT_OPERATION) { 1366 secondChanelOffset = bytes[inforStart + 1] & 0x3; 1367 } else if(type == EID_VHT_OPERATION) { 1368 channelMode = bytes[inforStart]; 1369 centerFreqIndex1 = bytes[inforStart + 1]; 1370 centerFreqIndex2 = bytes[inforStart + 2]; 1371 } else if (type == EID_EXTENDED_CAPS) { 1372 int tempIndex = RTT_RESP_ENABLE_BIT / 8; 1373 byte offset = RTT_RESP_ENABLE_BIT % 8; 1374 1375 if(len < tempIndex + 1) { 1376 result.is80211McRTTResponder = false; 1377 } else { 1378 if ((bytes[inforStart + tempIndex] & ((byte)0x1 << offset)) != 0) { 1379 result.is80211McRTTResponder = true; 1380 } else { 1381 result.is80211McRTTResponder = false; 1382 } 1383 } 1384 } 1385 } 1386 //handle RTT related information 1387 if (channelMode != 0) { 1388 // 80 or 160 MHz 1389 result.channelWidth = channelMode + 1; 1390 1391 //convert channel index to frequency in MHz, channel 36 is 5180MHz 1392 result.centerFreq0 = (centerFreqIndex1 - 36) * 5 + 5180; 1393 1394 if(channelMode > 1) { //160MHz 1395 result.centerFreq1 = (centerFreqIndex2 - 36) * 5 + 5180; 1396 } else { 1397 result.centerFreq1 = 0; 1398 } 1399 } else { 1400 //20 or 40 MHz 1401 if (secondChanelOffset != 0) {//40MHz 1402 result.channelWidth = 1; 1403 if (secondChanelOffset == 1) { 1404 result.centerFreq0 = result.frequency + 20; 1405 } else if (secondChanelOffset == 3) { 1406 result.centerFreq0 = result.frequency - 20; 1407 } else { 1408 result.centerFreq0 = 0; 1409 Log.e(TAG, ": Error on secondChanelOffset"); 1410 } 1411 } else { 1412 result.centerFreq0 = 0; 1413 result.centerFreq1 = 0; 1414 } 1415 result.centerFreq1 = 0; 1416 } 1417 if(DBG) { 1418 Log.d(TAG, ":SSID: " + result.SSID + "ChannelWidth is: " + result.channelWidth + 1419 " PrimaryFreq: " + result.frequency +" mCenterfreq0: " + result.centerFreq0 + 1420 " mCenterfreq1: " + result.centerFreq1 + (result.is80211McRTTResponder ? 1421 "Support RTT reponder: " : "Do not support RTT responder")); 1422 } 1423 1424 result.informationElements = elements; 1425 sScanEventHandler.onFullScanResult(result); 1426 } 1427 1428 private static int sScanCmdId = 0; 1429 private static ScanEventHandler sScanEventHandler; 1430 private static ScanSettings sScanSettings; 1431 1432 synchronized public static boolean startScan( 1433 ScanSettings settings, ScanEventHandler eventHandler) { 1434 synchronized (mLock) { 1435 1436 if (sScanCmdId != 0) { 1437 stopScan(); 1438 } else if (sScanSettings != null || sScanEventHandler != null) { 1439 /* current scan is paused; no need to stop it */ 1440 } 1441 1442 sScanCmdId = getNewCmdIdLocked(); 1443 1444 sScanSettings = settings; 1445 sScanEventHandler = eventHandler; 1446 1447 if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) { 1448 sScanEventHandler = null; 1449 sScanSettings = null; 1450 sScanCmdId = 0; 1451 return false; 1452 } 1453 1454 return true; 1455 } 1456 } 1457 1458 synchronized public static void stopScan() { 1459 synchronized (mLock) { 1460 stopScanNative(sWlan0Index, sScanCmdId); 1461 sScanSettings = null; 1462 sScanEventHandler = null; 1463 sScanCmdId = 0; 1464 } 1465 } 1466 1467 synchronized public static void pauseScan() { 1468 synchronized (mLock) { 1469 if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) { 1470 Log.d(TAG, "Pausing scan"); 1471 WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true); 1472 stopScanNative(sWlan0Index, sScanCmdId); 1473 sScanCmdId = 0; 1474 sScanEventHandler.onScanPaused(scanData); 1475 } 1476 } 1477 } 1478 1479 synchronized public static void restartScan() { 1480 synchronized (mLock) { 1481 if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) { 1482 Log.d(TAG, "Restarting scan"); 1483 ScanEventHandler handler = sScanEventHandler; 1484 ScanSettings settings = sScanSettings; 1485 if (startScan(sScanSettings, sScanEventHandler)) { 1486 sScanEventHandler.onScanRestarted(); 1487 } else { 1488 /* we are still paused; don't change state */ 1489 sScanEventHandler = handler; 1490 sScanSettings = settings; 1491 } 1492 } 1493 } 1494 } 1495 1496 synchronized public static WifiScanner.ScanData[] getScanResults(boolean flush) { 1497 synchronized (mLock) { 1498 return getScanResultsNative(sWlan0Index, flush); 1499 } 1500 } 1501 1502 public static interface HotlistEventHandler { 1503 void onHotlistApFound (ScanResult[] result); 1504 void onHotlistApLost (ScanResult[] result); 1505 } 1506 1507 private static int sHotlistCmdId = 0; 1508 private static HotlistEventHandler sHotlistEventHandler; 1509 1510 private native static boolean setHotlistNative(int iface, int id, 1511 WifiScanner.HotlistSettings settings); 1512 private native static boolean resetHotlistNative(int iface, int id); 1513 1514 synchronized public static boolean setHotlist(WifiScanner.HotlistSettings settings, 1515 HotlistEventHandler eventHandler) { 1516 synchronized (mLock) { 1517 if (sHotlistCmdId != 0) { 1518 return false; 1519 } else { 1520 sHotlistCmdId = getNewCmdIdLocked(); 1521 } 1522 1523 sHotlistEventHandler = eventHandler; 1524 if (setHotlistNative(sWlan0Index, sScanCmdId, settings) == false) { 1525 sHotlistEventHandler = null; 1526 return false; 1527 } 1528 1529 return true; 1530 } 1531 } 1532 1533 synchronized public static void resetHotlist() { 1534 synchronized (mLock) { 1535 if (sHotlistCmdId != 0) { 1536 resetHotlistNative(sWlan0Index, sHotlistCmdId); 1537 sHotlistCmdId = 0; 1538 sHotlistEventHandler = null; 1539 } 1540 } 1541 } 1542 1543 synchronized public static void onHotlistApFound(int id, ScanResult[] results) { 1544 synchronized (mLock) { 1545 if (sHotlistCmdId != 0) { 1546 sHotlistEventHandler.onHotlistApFound(results); 1547 } else { 1548 /* this can happen because of race conditions */ 1549 Log.d(TAG, "Ignoring hotlist AP found event"); 1550 } 1551 } 1552 } 1553 1554 synchronized public static void onHotlistApLost(int id, ScanResult[] results) { 1555 synchronized (mLock) { 1556 if (sHotlistCmdId != 0) { 1557 sHotlistEventHandler.onHotlistApLost(results); 1558 } else { 1559 /* this can happen because of race conditions */ 1560 Log.d(TAG, "Ignoring hotlist AP lost event"); 1561 } 1562 } 1563 } 1564 1565 public static interface SignificantWifiChangeEventHandler { 1566 void onChangesFound(ScanResult[] result); 1567 } 1568 1569 private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler; 1570 private static int sSignificantWifiChangeCmdId; 1571 1572 private static native boolean trackSignificantWifiChangeNative( 1573 int iface, int id, WifiScanner.WifiChangeSettings settings); 1574 private static native boolean untrackSignificantWifiChangeNative(int iface, int id); 1575 1576 synchronized public static boolean trackSignificantWifiChange( 1577 WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) { 1578 synchronized (mLock) { 1579 if (sSignificantWifiChangeCmdId != 0) { 1580 return false; 1581 } else { 1582 sSignificantWifiChangeCmdId = getNewCmdIdLocked(); 1583 } 1584 1585 sSignificantWifiChangeHandler = handler; 1586 if (trackSignificantWifiChangeNative(sWlan0Index, sScanCmdId, settings) == false) { 1587 sSignificantWifiChangeHandler = null; 1588 return false; 1589 } 1590 1591 return true; 1592 } 1593 } 1594 1595 synchronized static void untrackSignificantWifiChange() { 1596 synchronized (mLock) { 1597 if (sSignificantWifiChangeCmdId != 0) { 1598 untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId); 1599 sSignificantWifiChangeCmdId = 0; 1600 sSignificantWifiChangeHandler = null; 1601 } 1602 } 1603 } 1604 1605 synchronized static void onSignificantWifiChange(int id, ScanResult[] results) { 1606 synchronized (mLock) { 1607 if (sSignificantWifiChangeCmdId != 0) { 1608 sSignificantWifiChangeHandler.onChangesFound(results); 1609 } else { 1610 /* this can happen because of race conditions */ 1611 Log.d(TAG, "Ignoring significant wifi change"); 1612 } 1613 } 1614 } 1615 1616 synchronized public static WifiLinkLayerStats getWifiLinkLayerStats(String iface) { 1617 // TODO: use correct iface name to Index translation 1618 if (iface == null) return null; 1619 synchronized (mLock) { 1620 if (!sHalIsStarted) 1621 startHal(); 1622 if (sHalIsStarted) 1623 return getWifiLinkLayerStatsNative(sWlan0Index); 1624 } 1625 return null; 1626 } 1627 1628 /* 1629 * NFC-related calls 1630 */ 1631 public String getNfcWpsConfigurationToken(int netId) { 1632 return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId); 1633 } 1634 1635 public String getNfcHandoverRequest() { 1636 return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR"); 1637 } 1638 1639 public String getNfcHandoverSelect() { 1640 return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR"); 1641 } 1642 1643 public boolean initiatorReportNfcHandover(String selectMessage) { 1644 return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage); 1645 } 1646 1647 public boolean responderReportNfcHandover(String requestMessage) { 1648 return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00"); 1649 } 1650 1651 public static native int getSupportedFeatureSetNative(int iface); 1652 synchronized public static int getSupportedFeatureSet() { 1653 return getSupportedFeatureSetNative(sWlan0Index); 1654 } 1655 1656 /* Rtt related commands/events */ 1657 public static interface RttEventHandler { 1658 void onRttResults(RttManager.RttResult[] result); 1659 } 1660 1661 private static RttEventHandler sRttEventHandler; 1662 private static int sRttCmdId; 1663 1664 synchronized private static void onRttResults(int id, RttManager.RttResult[] results) { 1665 if (id == sRttCmdId) { 1666 Log.d(TAG, "Received " + results.length + " rtt results"); 1667 sRttEventHandler.onRttResults(results); 1668 sRttCmdId = 0; 1669 } else { 1670 Log.d(TAG, "Received event for unknown cmd = " + id + ", current id = " + sRttCmdId); 1671 } 1672 } 1673 1674 private static native boolean requestRangeNative( 1675 int iface, int id, RttManager.RttParams[] params); 1676 private static native boolean cancelRangeRequestNative( 1677 int iface, int id, RttManager.RttParams[] params); 1678 1679 synchronized public static boolean requestRtt( 1680 RttManager.RttParams[] params, RttEventHandler handler) { 1681 synchronized (mLock) { 1682 if (sRttCmdId != 0) { 1683 return false; 1684 } else { 1685 sRttCmdId = getNewCmdIdLocked(); 1686 } 1687 sRttEventHandler = handler; 1688 return requestRangeNative(sWlan0Index, sRttCmdId, params); 1689 } 1690 } 1691 1692 synchronized public static boolean cancelRtt(RttManager.RttParams[] params) { 1693 synchronized(mLock) { 1694 if (sRttCmdId == 0) { 1695 return false; 1696 } 1697 1698 if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) { 1699 sRttEventHandler = null; 1700 return true; 1701 } else { 1702 return false; 1703 } 1704 } 1705 } 1706 1707 private static native boolean setScanningMacOuiNative(int iface, byte[] oui); 1708 1709 synchronized public static boolean setScanningMacOui(byte[] oui) { 1710 synchronized (mLock) { 1711 if (startHal()) { 1712 return setScanningMacOuiNative(sWlan0Index, oui); 1713 } else { 1714 return false; 1715 } 1716 } 1717 } 1718 1719 private static native int[] getChannelsForBandNative( 1720 int iface, int band); 1721 1722 synchronized public static int [] getChannelsForBand(int band) { 1723 synchronized (mLock) { 1724 if (startHal()) { 1725 return getChannelsForBandNative(sWlan0Index, band); 1726 } else { 1727 return null; 1728 } 1729 } 1730 } 1731 1732 1733 private static native boolean setDfsFlagNative(int iface, boolean dfsOn); 1734 synchronized public static boolean setDfsFlag(boolean dfsOn) { 1735 synchronized (mLock) { 1736 if (startHal()) { 1737 return setDfsFlagNative(sWlan0Index, dfsOn); 1738 } else { 1739 return false; 1740 } 1741 } 1742 } 1743 1744 private static native boolean toggleInterfaceNative(int on); 1745 synchronized public static boolean toggleInterface(int on) { 1746 synchronized (mLock) { 1747 if (startHal()) { 1748 return toggleInterfaceNative(0); 1749 } else { 1750 1751 return false; 1752 } 1753 } 1754 } 1755 1756 private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface); 1757 synchronized public static RttManager.RttCapabilities getRttCapabilities() { 1758 synchronized (mLock) { 1759 if (startHal()) { 1760 return getRttCapabilitiesNative(sWlan0Index); 1761 } else { 1762 return null; 1763 } 1764 } 1765 } 1766 1767 private static native boolean setCountryCodeHalNative(int iface, String CountryCode); 1768 synchronized public static boolean setCountryCodeHal( String CountryCode) { 1769 synchronized (mLock) { 1770 if (startHal()) { 1771 return setCountryCodeHalNative(sWlan0Index, CountryCode); 1772 } else { 1773 return false; 1774 } 1775 } 1776 } 1777 1778 //--------------------------------------------------------------------------------- 1779 1780 /* Wifi Logger commands/events */ 1781 1782 private static native boolean startLogging(int iface); 1783 1784 public static interface WifiLoggerEventHandler { 1785 void onDataAvailable(char data[], int len); 1786 } 1787 1788 private static WifiLoggerEventHandler sWifiLoggerEventHandler = null; 1789 1790 synchronized private static void onDataAvailable(char data[], int len) { 1791 if (sWifiLoggerEventHandler != null) { 1792 sWifiLoggerEventHandler.onDataAvailable(data, len); 1793 } 1794 } 1795} 1796