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