WifiNative.java revision 39c3574fa3be035eafbf86462f3ca1bb859060fa
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.RttManager; 20import android.net.wifi.ScanResult; 21import android.net.wifi.WifiConfiguration; 22import android.net.wifi.WifiEnterpriseConfig; 23import android.net.wifi.WifiLinkLayerStats; 24import android.net.wifi.WifiWakeReasonAndCounts; 25import android.net.wifi.WifiManager; 26import android.net.wifi.WifiScanner; 27import android.net.wifi.WifiSsid; 28import android.net.wifi.WpsInfo; 29import android.net.wifi.p2p.WifiP2pConfig; 30import android.net.wifi.p2p.WifiP2pGroup; 31import android.net.wifi.p2p.nsd.WifiP2pServiceInfo; 32import android.os.SystemClock; 33import android.os.SystemProperties; 34import android.text.TextUtils; 35import android.util.LocalLog; 36import android.util.Log; 37import android.content.Context; 38import android.content.Intent; 39import android.app.AlarmManager; 40import android.app.PendingIntent; 41import android.content.IntentFilter; 42import android.content.BroadcastReceiver; 43import com.android.server.connectivity.KeepalivePacketData; 44import com.android.server.wifi.hotspot2.NetworkDetail; 45import com.android.server.wifi.hotspot2.SupplicantBridge; 46import com.android.server.wifi.hotspot2.Utils; 47import com.android.server.wifi.util.InformationElementUtil; 48 49import libcore.util.HexEncoding; 50 51import java.nio.ByteBuffer; 52import java.nio.CharBuffer; 53import java.nio.charset.CharacterCodingException; 54import java.nio.charset.CharsetDecoder; 55import java.nio.charset.StandardCharsets; 56import java.util.ArrayList; 57import java.util.List; 58import java.util.Locale; 59import java.util.Set; 60 61/** 62 * Native calls for bring up/shut down of the supplicant daemon and for 63 * sending requests to the supplicant daemon 64 * 65 * waitForEvent() is called on the monitor thread for events. All other methods 66 * must be serialized from the framework. 67 * 68 * {@hide} 69 */ 70public class WifiNative { 71 private static boolean DBG = false; 72 73 /** 74 * Hold this lock before calling supplicant or HAL methods 75 * it is required to mutually exclude access to the driver 76 */ 77 public static final Object sLock = new Object(); 78 79 private static final LocalLog sLocalLog = new LocalLog(16384); 80 81 public static LocalLog getLocalLog() { 82 return sLocalLog; 83 } 84 85 /* Register native functions */ 86 static { 87 /* Native functions are defined in libwifi-service.so */ 88 System.loadLibrary("wifi-service"); 89 registerNatives(); 90 } 91 92 private static native int registerNatives(); 93 94 /* 95 * Singleton WifiNative instances 96 */ 97 private static WifiNative wlanNativeInterface = 98 new WifiNative(SystemProperties.get("wifi.interface", "wlan0")); 99 public static WifiNative getWlanNativeInterface() { 100 return wlanNativeInterface; 101 } 102 103 //STOPSHIP: get interface name from native side 104 private static WifiNative p2pNativeInterface = new WifiNative("p2p0"); 105 public static WifiNative getP2pNativeInterface() { 106 return p2pNativeInterface; 107 } 108 109 110 private final String mTAG; 111 private final String mInterfaceName; 112 private final String mInterfacePrefix; 113 114 private Context mContext = null; 115 private PnoMonitor mPnoMonitor = null; 116 public void initContext(Context context) { 117 if (mContext == null && context != null) { 118 mContext = context; 119 mPnoMonitor = new PnoMonitor(); 120 } 121 } 122 123 private WifiNative(String interfaceName) { 124 mInterfaceName = interfaceName; 125 mTAG = "WifiNative-" + interfaceName; 126 127 if (!interfaceName.equals("p2p0")) { 128 mInterfacePrefix = "IFNAME=" + interfaceName + " "; 129 } else { 130 // commands for p2p0 interface don't need prefix 131 mInterfacePrefix = ""; 132 } 133 } 134 135 public String getInterfaceName() { 136 return mInterfaceName; 137 } 138 139 // Note this affects logging on for all interfaces 140 void enableVerboseLogging(int verbose) { 141 if (verbose > 0) { 142 DBG = true; 143 } else { 144 DBG = false; 145 } 146 } 147 148 private void localLog(String s) { 149 if (sLocalLog != null) sLocalLog.log(mInterfaceName + ": " + s); 150 } 151 152 153 154 /* 155 * Driver and Supplicant management 156 */ 157 private native static boolean loadDriverNative(); 158 public boolean loadDriver() { 159 synchronized (sLock) { 160 return loadDriverNative(); 161 } 162 } 163 164 private native static boolean isDriverLoadedNative(); 165 public boolean isDriverLoaded() { 166 synchronized (sLock) { 167 return isDriverLoadedNative(); 168 } 169 } 170 171 private native static boolean unloadDriverNative(); 172 public boolean unloadDriver() { 173 synchronized (sLock) { 174 return unloadDriverNative(); 175 } 176 } 177 178 private native static boolean startSupplicantNative(boolean p2pSupported); 179 public boolean startSupplicant(boolean p2pSupported) { 180 synchronized (sLock) { 181 return startSupplicantNative(p2pSupported); 182 } 183 } 184 185 /* Sends a kill signal to supplicant. To be used when we have lost connection 186 or when the supplicant is hung */ 187 private native static boolean killSupplicantNative(boolean p2pSupported); 188 public boolean killSupplicant(boolean p2pSupported) { 189 synchronized (sLock) { 190 return killSupplicantNative(p2pSupported); 191 } 192 } 193 194 private native static boolean connectToSupplicantNative(); 195 public boolean connectToSupplicant() { 196 synchronized (sLock) { 197 localLog(mInterfacePrefix + "connectToSupplicant"); 198 return connectToSupplicantNative(); 199 } 200 } 201 202 private native static void closeSupplicantConnectionNative(); 203 public void closeSupplicantConnection() { 204 synchronized (sLock) { 205 localLog(mInterfacePrefix + "closeSupplicantConnection"); 206 closeSupplicantConnectionNative(); 207 } 208 } 209 210 /** 211 * Wait for the supplicant to send an event, returning the event string. 212 * @return the event string sent by the supplicant. 213 */ 214 private native static String waitForEventNative(); 215 public String waitForEvent() { 216 // No synchronization necessary .. it is implemented in WifiMonitor 217 return waitForEventNative(); 218 } 219 220 221 /* 222 * Supplicant Command Primitives 223 */ 224 private native boolean doBooleanCommandNative(String command); 225 226 private native int doIntCommandNative(String command); 227 228 private native String doStringCommandNative(String command); 229 230 private boolean doBooleanCommand(String command) { 231 if (DBG) Log.d(mTAG, "doBoolean: " + command); 232 synchronized (sLock) { 233 String toLog = mInterfacePrefix + command; 234 boolean result = doBooleanCommandNative(mInterfacePrefix + command); 235 localLog(toLog + " -> " + result); 236 if (DBG) Log.d(mTAG, command + ": returned " + result); 237 return result; 238 } 239 } 240 241 private boolean doBooleanCommandWithoutLogging(String command) { 242 if (DBG) Log.d(mTAG, "doBooleanCommandWithoutLogging: " + command); 243 synchronized (sLock) { 244 boolean result = doBooleanCommandNative(mInterfacePrefix + command); 245 if (DBG) Log.d(mTAG, command + ": returned " + result); 246 return result; 247 } 248 } 249 250 private int doIntCommand(String command) { 251 if (DBG) Log.d(mTAG, "doInt: " + command); 252 synchronized (sLock) { 253 String toLog = mInterfacePrefix + command; 254 int result = doIntCommandNative(mInterfacePrefix + command); 255 localLog(toLog + " -> " + result); 256 if (DBG) Log.d(mTAG, " returned " + result); 257 return result; 258 } 259 } 260 261 private String doStringCommand(String command) { 262 if (DBG) { 263 //GET_NETWORK commands flood the logs 264 if (!command.startsWith("GET_NETWORK")) { 265 Log.d(mTAG, "doString: [" + command + "]"); 266 } 267 } 268 synchronized (sLock) { 269 String toLog = mInterfacePrefix + command; 270 String result = doStringCommandNative(mInterfacePrefix + command); 271 if (result == null) { 272 if (DBG) Log.d(mTAG, "doStringCommandNative no result"); 273 } else { 274 if (!command.startsWith("STATUS-")) { 275 localLog(toLog + " -> " + result); 276 } 277 if (DBG) Log.d(mTAG, " returned " + result.replace("\n", " ")); 278 } 279 return result; 280 } 281 } 282 283 private String doStringCommandWithoutLogging(String command) { 284 if (DBG) { 285 //GET_NETWORK commands flood the logs 286 if (!command.startsWith("GET_NETWORK")) { 287 Log.d(mTAG, "doString: [" + command + "]"); 288 } 289 } 290 synchronized (sLock) { 291 return doStringCommandNative(mInterfacePrefix + command); 292 } 293 } 294 295 public String doCustomSupplicantCommand(String command) { 296 return doStringCommand(command); 297 } 298 299 /* 300 * Wrappers for supplicant commands 301 */ 302 public boolean ping() { 303 String pong = doStringCommand("PING"); 304 return (pong != null && pong.equals("PONG")); 305 } 306 307 public void setSupplicantLogLevel(String level) { 308 doStringCommand("LOG_LEVEL " + level); 309 } 310 311 public String getFreqCapability() { 312 return doStringCommand("GET_CAPABILITY freq"); 313 } 314 315 316 public static final int SCAN_WITHOUT_CONNECTION_SETUP = 1; 317 public static final int SCAN_WITH_CONNECTION_SETUP = 2; 318 319 public boolean scan(int type, Set<Integer> freqs) { 320 if(freqs == null) { 321 return scan(type, (String)null); 322 } 323 else if (freqs.size() != 0) { 324 StringBuilder freqList = new StringBuilder(); 325 boolean first = true; 326 for (Integer freq : freqs) { 327 if (!first) 328 freqList.append(","); 329 freqList.append(freq.toString()); 330 first = false; 331 } 332 return scan(type, freqList.toString()); 333 } 334 else { 335 return false; 336 } 337 } 338 339 private boolean scan(int type, String freqList) { 340 if (type == SCAN_WITHOUT_CONNECTION_SETUP) { 341 if (freqList == null) return doBooleanCommand("SCAN TYPE=ONLY"); 342 else return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList); 343 } else if (type == SCAN_WITH_CONNECTION_SETUP) { 344 if (freqList == null) return doBooleanCommand("SCAN"); 345 else return doBooleanCommand("SCAN freq=" + freqList); 346 } else { 347 throw new IllegalArgumentException("Invalid scan type"); 348 } 349 } 350 351 /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta. 352 * 353 * Note that underneath we use a harsh-sounding "terminate" supplicant command 354 * for a graceful stop and a mild-sounding "stop" interface 355 * to kill the process 356 */ 357 public boolean stopSupplicant() { 358 return doBooleanCommand("TERMINATE"); 359 } 360 361 public String listNetworks() { 362 return doStringCommand("LIST_NETWORKS"); 363 } 364 365 public String listNetworks(int last_id) { 366 return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id); 367 } 368 369 public int addNetwork() { 370 return doIntCommand("ADD_NETWORK"); 371 } 372 373 public boolean setNetworkVariable(int netId, String name, String value) { 374 if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false; 375 if (name.equals(WifiConfiguration.pskVarName) 376 || name.equals(WifiEnterpriseConfig.PASSWORD_KEY)) { 377 return doBooleanCommandWithoutLogging("SET_NETWORK " + netId + " " + name + " " + value); 378 } else { 379 return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value); 380 } 381 } 382 383 public String getNetworkVariable(int netId, String name) { 384 if (TextUtils.isEmpty(name)) return null; 385 386 // GET_NETWORK will likely flood the logs ... 387 return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name); 388 } 389 390 public boolean removeNetwork(int netId) { 391 return doBooleanCommand("REMOVE_NETWORK " + netId); 392 } 393 394 395 private void logDbg(String debug) { 396 long now = SystemClock.elapsedRealtimeNanos(); 397 String ts = String.format("[%,d us] ", now/1000); 398 Log.e("WifiNative: ", ts+debug+ " stack:" 399 + Thread.currentThread().getStackTrace()[2].getMethodName() +" - " 400 + Thread.currentThread().getStackTrace()[3].getMethodName() +" - " 401 + Thread.currentThread().getStackTrace()[4].getMethodName() +" - " 402 + Thread.currentThread().getStackTrace()[5].getMethodName()+" - " 403 + Thread.currentThread().getStackTrace()[6].getMethodName()); 404 405 } 406 public boolean enableNetwork(int netId, boolean disableOthers) { 407 if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId) 408 + " disableOthers=" + disableOthers); 409 if (disableOthers) { 410 return doBooleanCommand("SELECT_NETWORK " + netId); 411 } else { 412 return doBooleanCommand("ENABLE_NETWORK " + netId); 413 } 414 } 415 416 public boolean disableNetwork(int netId) { 417 if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId)); 418 return doBooleanCommand("DISABLE_NETWORK " + netId); 419 } 420 421 public boolean selectNetwork(int netId) { 422 if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId)); 423 return doBooleanCommand("SELECT_NETWORK " + netId); 424 } 425 426 public boolean reconnect() { 427 if (DBG) logDbg("RECONNECT "); 428 return doBooleanCommand("RECONNECT"); 429 } 430 431 public boolean reassociate() { 432 if (DBG) logDbg("REASSOCIATE "); 433 return doBooleanCommand("REASSOCIATE"); 434 } 435 436 public boolean disconnect() { 437 if (DBG) logDbg("DISCONNECT "); 438 return doBooleanCommand("DISCONNECT"); 439 } 440 441 public String status() { 442 return status(false); 443 } 444 445 public String status(boolean noEvents) { 446 if (noEvents) { 447 return doStringCommand("STATUS-NO_EVENTS"); 448 } else { 449 return doStringCommand("STATUS"); 450 } 451 } 452 453 public String getMacAddress() { 454 //Macaddr = XX.XX.XX.XX.XX.XX 455 String ret = doStringCommand("DRIVER MACADDR"); 456 if (!TextUtils.isEmpty(ret)) { 457 String[] tokens = ret.split(" = "); 458 if (tokens.length == 2) return tokens[1]; 459 } 460 return null; 461 } 462 463 464 465 /** 466 * Format of results: 467 * ================= 468 * id=1 469 * bssid=68:7f:76:d7:1a:6e 470 * freq=2412 471 * level=-44 472 * tsf=1344626243700342 473 * flags=[WPA2-PSK-CCMP][WPS][ESS] 474 * ssid=zfdy 475 * ==== 476 * id=2 477 * bssid=68:5f:74:d7:1a:6f 478 * freq=5180 479 * level=-73 480 * tsf=1344626243700373 481 * flags=[WPA2-PSK-CCMP][WPS][ESS] 482 * ssid=zuby 483 * ==== 484 * 485 * RANGE=ALL gets all scan results 486 * RANGE=ID- gets results from ID 487 * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details 488 * 0 0 1 0 2 489 * WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM | WPA_BSS_MASK_WIFI_DISPLAY 490 * 0 0 0 1 1 -> 9 491 * WPA_BSS_MASK_INTERNETW | WPA_BSS_MASK_P2P_SCAN | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID 492 * 1 0 0 1 9 -> d 493 * WPA_BSS_MASK_FLAGS | WPA_BSS_MASK_IE | WPA_BSS_MASK_AGE | WPA_BSS_MASK_TSF 494 * 1 0 0 0 8 495 * WPA_BSS_MASK_LEVEL | WPA_BSS_MASK_NOISE | WPA_BSS_MASK_QUAL | WPA_BSS_MASK_CAPABILITIES 496 * 0 1 1 1 7 497 * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ | WPA_BSS_MASK_BSSID | WPA_BSS_MASK_ID 498 * 499 * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176) 500 * 501 * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884 502 * wpa_supplicant_ctrl_iface_bss:4315 503 * print_bss_info 504 */ 505 private String getRawScanResults(String range) { 506 return doStringCommandWithoutLogging("BSS RANGE=" + range + " MASK=0x29d87"); 507 } 508 509 private static final String BSS_IE_STR = "ie="; 510 private static final String BSS_ID_STR = "id="; 511 private static final String BSS_BSSID_STR = "bssid="; 512 private static final String BSS_FREQ_STR = "freq="; 513 private static final String BSS_LEVEL_STR = "level="; 514 private static final String BSS_TSF_STR = "tsf="; 515 private static final String BSS_FLAGS_STR = "flags="; 516 private static final String BSS_SSID_STR = "ssid="; 517 private static final String BSS_DELIMITER_STR = "===="; 518 private static final String BSS_END_STR = "####"; 519 520 public ArrayList<ScanDetail> getScanResults() { 521 int next_sid = 0; 522 ArrayList<ScanDetail> results = new ArrayList<>(); 523 while(next_sid >= 0) { 524 String rawResult = getRawScanResults(next_sid+"-"); 525 next_sid = -1; 526 527 if (TextUtils.isEmpty(rawResult)) 528 break; 529 530 String[] lines = rawResult.split("\n"); 531 532 533 // note that all these splits and substrings keep references to the original 534 // huge string buffer while the amount we really want is generally pretty small 535 // so make copies instead (one example b/11087956 wasted 400k of heap here). 536 final int bssidStrLen = BSS_BSSID_STR.length(); 537 final int flagLen = BSS_FLAGS_STR.length(); 538 539 String bssid = ""; 540 int level = 0; 541 int freq = 0; 542 long tsf = 0; 543 String flags = ""; 544 WifiSsid wifiSsid = null; 545 String infoElementsStr = null; 546 List<String> anqpLines = null; 547 548 for (String line : lines) { 549 if (line.startsWith(BSS_ID_STR)) { // Will find the last id line 550 try { 551 next_sid = Integer.parseInt(line.substring(BSS_ID_STR.length())) + 1; 552 } catch (NumberFormatException e) { 553 // Nothing to do 554 } 555 } else if (line.startsWith(BSS_BSSID_STR)) { 556 bssid = new String(line.getBytes(), bssidStrLen, line.length() - bssidStrLen); 557 } else if (line.startsWith(BSS_FREQ_STR)) { 558 try { 559 freq = Integer.parseInt(line.substring(BSS_FREQ_STR.length())); 560 } catch (NumberFormatException e) { 561 freq = 0; 562 } 563 } else if (line.startsWith(BSS_LEVEL_STR)) { 564 try { 565 level = Integer.parseInt(line.substring(BSS_LEVEL_STR.length())); 566 /* some implementations avoid negative values by adding 256 567 * so we need to adjust for that here. 568 */ 569 if (level > 0) level -= 256; 570 } catch (NumberFormatException e) { 571 level = 0; 572 } 573 } else if (line.startsWith(BSS_TSF_STR)) { 574 try { 575 tsf = Long.parseLong(line.substring(BSS_TSF_STR.length())); 576 } catch (NumberFormatException e) { 577 tsf = 0; 578 } 579 } else if (line.startsWith(BSS_FLAGS_STR)) { 580 flags = new String(line.getBytes(), flagLen, line.length() - flagLen); 581 } else if (line.startsWith(BSS_SSID_STR)) { 582 wifiSsid = WifiSsid.createFromAsciiEncoded( 583 line.substring(BSS_SSID_STR.length())); 584 } else if (line.startsWith(BSS_IE_STR)) { 585 infoElementsStr = line; 586 } else if (SupplicantBridge.isAnqpAttribute(line)) { 587 if (anqpLines == null) { 588 anqpLines = new ArrayList<>(); 589 } 590 anqpLines.add(line); 591 } else if (line.startsWith(BSS_DELIMITER_STR) || line.startsWith(BSS_END_STR)) { 592 if (bssid != null) { 593 try { 594 if (infoElementsStr == null) { 595 throw new IllegalArgumentException("Null information element data"); 596 } 597 int seperator = infoElementsStr.indexOf('='); 598 if (seperator < 0) { 599 throw new IllegalArgumentException("No element separator"); 600 } 601 602 ScanResult.InformationElement[] infoElements = 603 InformationElementUtil.parseInformationElements( 604 Utils.hexToBytes(infoElementsStr.substring(seperator + 1))); 605 606 NetworkDetail networkDetail = new NetworkDetail(bssid, 607 infoElements, anqpLines, freq); 608 609 String xssid = (wifiSsid != null) ? wifiSsid.toString() : WifiSsid.NONE; 610 if (!xssid.equals(networkDetail.getTrimmedSSID())) { 611 Log.d(TAG, String.format( 612 "Inconsistent SSID on BSSID '%s': '%s' vs '%s': %s", 613 bssid, xssid, networkDetail.getSSID(), infoElementsStr)); 614 } 615 616 if (networkDetail.hasInterworking()) { 617 Log.d(TAG, "HSNwk: '" + networkDetail); 618 } 619 ScanDetail scan = new ScanDetail(networkDetail, wifiSsid, bssid, flags, 620 level, freq, tsf); 621 scan.getScanResult().informationElements = infoElements; 622 results.add(scan); 623 } catch (IllegalArgumentException iae) { 624 Log.d(TAG, "Failed to parse information elements: " + iae); 625 } 626 } 627 bssid = null; 628 level = 0; 629 freq = 0; 630 tsf = 0; 631 flags = ""; 632 wifiSsid = null; 633 infoElementsStr = null; 634 anqpLines = null; 635 } 636 } 637 } 638 return results; 639 } 640 641 /** 642 * Format of result: 643 * id=1016 644 * bssid=00:03:7f:40:84:10 645 * freq=2462 646 * beacon_int=200 647 * capabilities=0x0431 648 * qual=0 649 * noise=0 650 * level=-46 651 * tsf=0000002669008476 652 * age=5 653 * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555... 654 * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20] 655 * ssid=QCA-HS20-R2-TEST 656 * p2p_device_name= 657 * p2p_config_methods=0x0SET_NE 658 * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f... 659 * anqp_network_auth_type=010000 660 * anqp_roaming_consortium=03506f9a05001bc504bd 661 * anqp_ip_addr_type_availability=0c 662 * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2... 663 * anqp_3gpp=000600040132f465 664 * anqp_domain_name=0b65786d61706c652e636f6d 665 * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869... 666 * hs20_wan_metrics=01c40900008001000000000a00 667 * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0... 668 * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d... 669 */ 670 public String scanResult(String bssid) { 671 return doStringCommand("BSS " + bssid); 672 } 673 674 public boolean startDriver() { 675 return doBooleanCommand("DRIVER START"); 676 } 677 678 public boolean stopDriver() { 679 return doBooleanCommand("DRIVER STOP"); 680 } 681 682 683 /** 684 * Start filtering out Multicast V4 packets 685 * @return {@code true} if the operation succeeded, {@code false} otherwise 686 * 687 * Multicast filtering rules work as follows: 688 * 689 * The driver can filter multicast (v4 and/or v6) and broadcast packets when in 690 * a power optimized mode (typically when screen goes off). 691 * 692 * In order to prevent the driver from filtering the multicast/broadcast packets, we have to 693 * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective 694 * 695 * DRIVER RXFILTER-ADD Num 696 * where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6 697 * 698 * and DRIVER RXFILTER-START 699 * In order to stop the usage of these rules, we do 700 * 701 * DRIVER RXFILTER-STOP 702 * DRIVER RXFILTER-REMOVE Num 703 * where Num is as described for RXFILTER-ADD 704 * 705 * The SETSUSPENDOPT driver command overrides the filtering rules 706 */ 707 public boolean startFilteringMulticastV4Packets() { 708 return doBooleanCommand("DRIVER RXFILTER-STOP") 709 && doBooleanCommand("DRIVER RXFILTER-REMOVE 2") 710 && doBooleanCommand("DRIVER RXFILTER-START"); 711 } 712 713 /** 714 * Stop filtering out Multicast V4 packets. 715 * @return {@code true} if the operation succeeded, {@code false} otherwise 716 */ 717 public boolean stopFilteringMulticastV4Packets() { 718 return doBooleanCommand("DRIVER RXFILTER-STOP") 719 && doBooleanCommand("DRIVER RXFILTER-ADD 2") 720 && doBooleanCommand("DRIVER RXFILTER-START"); 721 } 722 723 /** 724 * Start filtering out Multicast V6 packets 725 * @return {@code true} if the operation succeeded, {@code false} otherwise 726 */ 727 public boolean startFilteringMulticastV6Packets() { 728 return doBooleanCommand("DRIVER RXFILTER-STOP") 729 && doBooleanCommand("DRIVER RXFILTER-REMOVE 3") 730 && doBooleanCommand("DRIVER RXFILTER-START"); 731 } 732 733 /** 734 * Stop filtering out Multicast V6 packets. 735 * @return {@code true} if the operation succeeded, {@code false} otherwise 736 */ 737 public boolean stopFilteringMulticastV6Packets() { 738 return doBooleanCommand("DRIVER RXFILTER-STOP") 739 && doBooleanCommand("DRIVER RXFILTER-ADD 3") 740 && doBooleanCommand("DRIVER RXFILTER-START"); 741 } 742 743 /** 744 * Set the operational frequency band 745 * @param band One of 746 * {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO}, 747 * {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ}, 748 * {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ}, 749 * @return {@code true} if the operation succeeded, {@code false} otherwise 750 */ 751 public boolean setBand(int band) { 752 String bandstr; 753 754 if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ) 755 bandstr = "5G"; 756 else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ) 757 bandstr = "2G"; 758 else 759 bandstr = "AUTO"; 760 return doBooleanCommand("SET SETBAND " + bandstr); 761 } 762 763 public static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED = 0; 764 public static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED = 1; 765 public static final int BLUETOOTH_COEXISTENCE_MODE_SENSE = 2; 766 /** 767 * Sets the bluetooth coexistence mode. 768 * 769 * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED}, 770 * {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or 771 * {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}. 772 * @return Whether the mode was successfully set. 773 */ 774 public boolean setBluetoothCoexistenceMode(int mode) { 775 return doBooleanCommand("DRIVER BTCOEXMODE " + mode); 776 } 777 778 /** 779 * Enable or disable Bluetooth coexistence scan mode. When this mode is on, 780 * some of the low-level scan parameters used by the driver are changed to 781 * reduce interference with A2DP streaming. 782 * 783 * @param isSet whether to enable or disable this mode 784 * @return {@code true} if the command succeeded, {@code false} otherwise. 785 */ 786 public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) { 787 if (setCoexScanMode) { 788 return doBooleanCommand("DRIVER BTCOEXSCAN-START"); 789 } else { 790 return doBooleanCommand("DRIVER BTCOEXSCAN-STOP"); 791 } 792 } 793 794 public void enableSaveConfig() { 795 doBooleanCommand("SET update_config 1"); 796 } 797 798 public boolean saveConfig() { 799 return doBooleanCommand("SAVE_CONFIG"); 800 } 801 802 public boolean addToBlacklist(String bssid) { 803 if (TextUtils.isEmpty(bssid)) return false; 804 return doBooleanCommand("BLACKLIST " + bssid); 805 } 806 807 public boolean clearBlacklist() { 808 return doBooleanCommand("BLACKLIST clear"); 809 } 810 811 public boolean setSuspendOptimizations(boolean enabled) { 812 if (enabled) { 813 return doBooleanCommand("DRIVER SETSUSPENDMODE 1"); 814 } else { 815 return doBooleanCommand("DRIVER SETSUSPENDMODE 0"); 816 } 817 } 818 819 public boolean setCountryCode(String countryCode) { 820 if (countryCode != null) 821 return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT)); 822 else 823 return doBooleanCommand("DRIVER COUNTRY"); 824 } 825 826 //PNO Monitor 827 private class PnoMonitor { 828 private static final int MINIMUM_PNO_GAP = 5 * 1000; 829 private static final String ACTION_TOGGLE_PNO = 830 "com.android.server.Wifi.action.TOGGLE_PNO"; 831 long mLastPnoChangeTimeStamp = -1L; 832 boolean mExpectedPnoState = false; 833 boolean mCurrentPnoState = false;; 834 boolean mWaitForTimer = false; 835 final Object mPnoLock = new Object(); 836 private final AlarmManager mAlarmManager = 837 (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 838 private final PendingIntent mPnoIntent; 839 840 public PnoMonitor() { 841 Intent intent = new Intent(ACTION_TOGGLE_PNO, null); 842 intent.setPackage("android"); 843 mPnoIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); 844 845 mContext.registerReceiver( 846 new BroadcastReceiver() { 847 @Override 848 public void onReceive(Context context, Intent intent) { 849 synchronized(mPnoLock) { 850 if (DBG) Log.d(mTAG, "PNO timer expire, PNO should change to " + 851 mExpectedPnoState); 852 if (mCurrentPnoState != mExpectedPnoState) { 853 if (DBG) Log.d(mTAG, "change PNO from " + mCurrentPnoState + " to " 854 + mExpectedPnoState); 855 boolean ret = setPno(mExpectedPnoState); 856 if (!ret) { 857 Log.e(mTAG, "set PNO failure"); 858 } 859 } else { 860 if (DBG) Log.d(mTAG, "Do not change PNO since current is expected"); 861 } 862 mWaitForTimer = false; 863 } 864 } 865 }, 866 new IntentFilter(ACTION_TOGGLE_PNO)); 867 } 868 869 private boolean setPno(boolean enable) { 870 String cmd = enable ? "SET pno 1" : "SET pno 0"; 871 boolean ret = doBooleanCommand(cmd); 872 mLastPnoChangeTimeStamp = System.currentTimeMillis(); 873 if (ret) { 874 mCurrentPnoState = enable; 875 } 876 return ret; 877 } 878 879 public boolean enableBackgroundScan(boolean enable) { 880 synchronized(mPnoLock) { 881 if (mWaitForTimer) { 882 //already has a timer 883 mExpectedPnoState = enable; 884 if (DBG) Log.d(mTAG, "update expected PNO to " + mExpectedPnoState); 885 } else { 886 if (mCurrentPnoState == enable) { 887 return true; 888 } 889 long timeDifference = System.currentTimeMillis() - mLastPnoChangeTimeStamp; 890 if (timeDifference >= MINIMUM_PNO_GAP) { 891 return setPno(enable); 892 } else { 893 mExpectedPnoState = enable; 894 mWaitForTimer = true; 895 if (DBG) Log.d(mTAG, "start PNO timer with delay:" + timeDifference); 896 mAlarmManager.set(AlarmManager.RTC_WAKEUP, 897 System.currentTimeMillis() + timeDifference, mPnoIntent); 898 } 899 } 900 return true; 901 } 902 } 903 } 904 905 public boolean enableBackgroundScan(boolean enable) { 906 if (mPnoMonitor != null) { 907 return mPnoMonitor.enableBackgroundScan(enable); 908 } else { 909 return false; 910 } 911 } 912 913 public void enableAutoConnect(boolean enable) { 914 if (enable) { 915 doBooleanCommand("STA_AUTOCONNECT 1"); 916 } else { 917 doBooleanCommand("STA_AUTOCONNECT 0"); 918 } 919 } 920 921 public void setScanInterval(int scanInterval) { 922 doBooleanCommand("SCAN_INTERVAL " + scanInterval); 923 } 924 925 public void setHs20(boolean hs20) { 926 if (hs20) { 927 doBooleanCommand("SET HS20 1"); 928 } else { 929 doBooleanCommand("SET HS20 0"); 930 } 931 } 932 933 public void startTdls(String macAddr, boolean enable) { 934 if (enable) { 935 synchronized (sLock) { 936 doBooleanCommand("TDLS_DISCOVER " + macAddr); 937 doBooleanCommand("TDLS_SETUP " + macAddr); 938 } 939 } else { 940 doBooleanCommand("TDLS_TEARDOWN " + macAddr); 941 } 942 } 943 944 /** Example output: 945 * RSSI=-65 946 * LINKSPEED=48 947 * NOISE=9999 948 * FREQUENCY=0 949 */ 950 public String signalPoll() { 951 return doStringCommandWithoutLogging("SIGNAL_POLL"); 952 } 953 954 /** Example outout: 955 * TXGOOD=396 956 * TXBAD=1 957 */ 958 public String pktcntPoll() { 959 return doStringCommand("PKTCNT_POLL"); 960 } 961 962 public void bssFlush() { 963 doBooleanCommand("BSS_FLUSH 0"); 964 } 965 966 public boolean startWpsPbc(String bssid) { 967 if (TextUtils.isEmpty(bssid)) { 968 return doBooleanCommand("WPS_PBC"); 969 } else { 970 return doBooleanCommand("WPS_PBC " + bssid); 971 } 972 } 973 974 public boolean startWpsPbc(String iface, String bssid) { 975 synchronized (sLock) { 976 if (TextUtils.isEmpty(bssid)) { 977 return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC"); 978 } else { 979 return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid); 980 } 981 } 982 } 983 984 public boolean startWpsPinKeypad(String pin) { 985 if (TextUtils.isEmpty(pin)) return false; 986 return doBooleanCommand("WPS_PIN any " + pin); 987 } 988 989 public boolean startWpsPinKeypad(String iface, String pin) { 990 if (TextUtils.isEmpty(pin)) return false; 991 synchronized (sLock) { 992 return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin); 993 } 994 } 995 996 997 public String startWpsPinDisplay(String bssid) { 998 if (TextUtils.isEmpty(bssid)) { 999 return doStringCommand("WPS_PIN any"); 1000 } else { 1001 return doStringCommand("WPS_PIN " + bssid); 1002 } 1003 } 1004 1005 public String startWpsPinDisplay(String iface, String bssid) { 1006 synchronized (sLock) { 1007 if (TextUtils.isEmpty(bssid)) { 1008 return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any"); 1009 } else { 1010 return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid); 1011 } 1012 } 1013 } 1014 1015 public boolean setExternalSim(boolean external) { 1016 String value = external ? "1" : "0"; 1017 Log.d(TAG, "Setting external_sim to " + value); 1018 return doBooleanCommand("SET external_sim " + value); 1019 } 1020 1021 public boolean simAuthResponse(int id, String type, String response) { 1022 // with type = GSM-AUTH, UMTS-AUTH or UMTS-AUTS 1023 return doBooleanCommand("CTRL-RSP-SIM-" + id + ":" + type + response); 1024 } 1025 1026 public boolean simAuthFailedResponse(int id) { 1027 // should be used with type GSM-AUTH 1028 return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-FAIL"); 1029 } 1030 1031 public boolean umtsAuthFailedResponse(int id) { 1032 // should be used with type UMTS-AUTH 1033 return doBooleanCommand("CTRL-RSP-SIM-" + id + ":UMTS-FAIL"); 1034 } 1035 1036 public boolean simIdentityResponse(int id, String response) { 1037 return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response); 1038 } 1039 1040 /* Configures an access point connection */ 1041 public boolean startWpsRegistrar(String bssid, String pin) { 1042 if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false; 1043 return doBooleanCommand("WPS_REG " + bssid + " " + pin); 1044 } 1045 1046 public boolean cancelWps() { 1047 return doBooleanCommand("WPS_CANCEL"); 1048 } 1049 1050 public boolean setPersistentReconnect(boolean enabled) { 1051 int value = (enabled == true) ? 1 : 0; 1052 return doBooleanCommand("SET persistent_reconnect " + value); 1053 } 1054 1055 public boolean setDeviceName(String name) { 1056 return doBooleanCommand("SET device_name " + name); 1057 } 1058 1059 public boolean setDeviceType(String type) { 1060 return doBooleanCommand("SET device_type " + type); 1061 } 1062 1063 public boolean setConfigMethods(String cfg) { 1064 return doBooleanCommand("SET config_methods " + cfg); 1065 } 1066 1067 public boolean setManufacturer(String value) { 1068 return doBooleanCommand("SET manufacturer " + value); 1069 } 1070 1071 public boolean setModelName(String value) { 1072 return doBooleanCommand("SET model_name " + value); 1073 } 1074 1075 public boolean setModelNumber(String value) { 1076 return doBooleanCommand("SET model_number " + value); 1077 } 1078 1079 public boolean setSerialNumber(String value) { 1080 return doBooleanCommand("SET serial_number " + value); 1081 } 1082 1083 public boolean setP2pSsidPostfix(String postfix) { 1084 return doBooleanCommand("SET p2p_ssid_postfix " + postfix); 1085 } 1086 1087 public boolean setP2pGroupIdle(String iface, int time) { 1088 synchronized (sLock) { 1089 return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time); 1090 } 1091 } 1092 1093 public void setPowerSave(boolean enabled) { 1094 if (enabled) { 1095 doBooleanCommand("SET ps 1"); 1096 } else { 1097 doBooleanCommand("SET ps 0"); 1098 } 1099 } 1100 1101 public boolean setP2pPowerSave(String iface, boolean enabled) { 1102 synchronized (sLock) { 1103 if (enabled) { 1104 return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1"); 1105 } else { 1106 return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0"); 1107 } 1108 } 1109 } 1110 1111 public boolean setWfdEnable(boolean enable) { 1112 return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0")); 1113 } 1114 1115 public boolean setWfdDeviceInfo(String hex) { 1116 return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex); 1117 } 1118 1119 /** 1120 * "sta" prioritizes STA connection over P2P and "p2p" prioritizes 1121 * P2P connection over STA 1122 */ 1123 public boolean setConcurrencyPriority(String s) { 1124 return doBooleanCommand("P2P_SET conc_pref " + s); 1125 } 1126 1127 public boolean p2pFind() { 1128 return doBooleanCommand("P2P_FIND"); 1129 } 1130 1131 public boolean p2pFind(int timeout) { 1132 if (timeout <= 0) { 1133 return p2pFind(); 1134 } 1135 return doBooleanCommand("P2P_FIND " + timeout); 1136 } 1137 1138 public boolean p2pStopFind() { 1139 return doBooleanCommand("P2P_STOP_FIND"); 1140 } 1141 1142 public boolean p2pListen() { 1143 return doBooleanCommand("P2P_LISTEN"); 1144 } 1145 1146 public boolean p2pListen(int timeout) { 1147 if (timeout <= 0) { 1148 return p2pListen(); 1149 } 1150 return doBooleanCommand("P2P_LISTEN " + timeout); 1151 } 1152 1153 public boolean p2pExtListen(boolean enable, int period, int interval) { 1154 if (enable && interval < period) { 1155 return false; 1156 } 1157 return doBooleanCommand("P2P_EXT_LISTEN" 1158 + (enable ? (" " + period + " " + interval) : "")); 1159 } 1160 1161 public boolean p2pSetChannel(int lc, int oc) { 1162 if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc); 1163 1164 synchronized (sLock) { 1165 if (lc >=1 && lc <= 11) { 1166 if (!doBooleanCommand("P2P_SET listen_channel " + lc)) { 1167 return false; 1168 } 1169 } else if (lc != 0) { 1170 return false; 1171 } 1172 1173 if (oc >= 1 && oc <= 165 ) { 1174 int freq = (oc <= 14 ? 2407 : 5000) + oc * 5; 1175 return doBooleanCommand("P2P_SET disallow_freq 1000-" 1176 + (freq - 5) + "," + (freq + 5) + "-6000"); 1177 } else if (oc == 0) { 1178 /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */ 1179 return doBooleanCommand("P2P_SET disallow_freq \"\""); 1180 } 1181 } 1182 return false; 1183 } 1184 1185 public boolean p2pFlush() { 1186 return doBooleanCommand("P2P_FLUSH"); 1187 } 1188 1189 private static final int DEFAULT_GROUP_OWNER_INTENT = 6; 1190 /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad] 1191 [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */ 1192 public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) { 1193 if (config == null) return null; 1194 List<String> args = new ArrayList<String>(); 1195 WpsInfo wps = config.wps; 1196 args.add(config.deviceAddress); 1197 1198 switch (wps.setup) { 1199 case WpsInfo.PBC: 1200 args.add("pbc"); 1201 break; 1202 case WpsInfo.DISPLAY: 1203 if (TextUtils.isEmpty(wps.pin)) { 1204 args.add("pin"); 1205 } else { 1206 args.add(wps.pin); 1207 } 1208 args.add("display"); 1209 break; 1210 case WpsInfo.KEYPAD: 1211 args.add(wps.pin); 1212 args.add("keypad"); 1213 break; 1214 case WpsInfo.LABEL: 1215 args.add(wps.pin); 1216 args.add("label"); 1217 default: 1218 break; 1219 } 1220 1221 if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) { 1222 args.add("persistent"); 1223 } 1224 1225 if (joinExistingGroup) { 1226 args.add("join"); 1227 } else { 1228 //TODO: This can be adapted based on device plugged in state and 1229 //device battery state 1230 int groupOwnerIntent = config.groupOwnerIntent; 1231 if (groupOwnerIntent < 0 || groupOwnerIntent > 15) { 1232 groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT; 1233 } 1234 args.add("go_intent=" + groupOwnerIntent); 1235 } 1236 1237 String command = "P2P_CONNECT "; 1238 for (String s : args) command += s + " "; 1239 1240 return doStringCommand(command); 1241 } 1242 1243 public boolean p2pCancelConnect() { 1244 return doBooleanCommand("P2P_CANCEL"); 1245 } 1246 1247 public boolean p2pProvisionDiscovery(WifiP2pConfig config) { 1248 if (config == null) return false; 1249 1250 switch (config.wps.setup) { 1251 case WpsInfo.PBC: 1252 return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc"); 1253 case WpsInfo.DISPLAY: 1254 //We are doing display, so provision discovery is keypad 1255 return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad"); 1256 case WpsInfo.KEYPAD: 1257 //We are doing keypad, so provision discovery is display 1258 return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display"); 1259 default: 1260 break; 1261 } 1262 return false; 1263 } 1264 1265 public boolean p2pGroupAdd(boolean persistent) { 1266 if (persistent) { 1267 return doBooleanCommand("P2P_GROUP_ADD persistent"); 1268 } 1269 return doBooleanCommand("P2P_GROUP_ADD"); 1270 } 1271 1272 public boolean p2pGroupAdd(int netId) { 1273 return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId); 1274 } 1275 1276 public boolean p2pGroupRemove(String iface) { 1277 if (TextUtils.isEmpty(iface)) return false; 1278 synchronized (sLock) { 1279 return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface); 1280 } 1281 } 1282 1283 public boolean p2pReject(String deviceAddress) { 1284 return doBooleanCommand("P2P_REJECT " + deviceAddress); 1285 } 1286 1287 /* Invite a peer to a group */ 1288 public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) { 1289 if (TextUtils.isEmpty(deviceAddress)) return false; 1290 1291 if (group == null) { 1292 return doBooleanCommand("P2P_INVITE peer=" + deviceAddress); 1293 } else { 1294 return doBooleanCommand("P2P_INVITE group=" + group.getInterface() 1295 + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress); 1296 } 1297 } 1298 1299 /* Reinvoke a persistent connection */ 1300 public boolean p2pReinvoke(int netId, String deviceAddress) { 1301 if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false; 1302 1303 return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress); 1304 } 1305 1306 public String p2pGetSsid(String deviceAddress) { 1307 return p2pGetParam(deviceAddress, "oper_ssid"); 1308 } 1309 1310 public String p2pGetDeviceAddress() { 1311 Log.d(TAG, "p2pGetDeviceAddress"); 1312 1313 String status = null; 1314 1315 /* Explicitly calling the API without IFNAME= prefix to take care of the devices that 1316 don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */ 1317 1318 synchronized (sLock) { 1319 status = doStringCommandNative("STATUS"); 1320 } 1321 1322 String result = ""; 1323 if (status != null) { 1324 String[] tokens = status.split("\n"); 1325 for (String token : tokens) { 1326 if (token.startsWith("p2p_device_address=")) { 1327 String[] nameValue = token.split("="); 1328 if (nameValue.length != 2) 1329 break; 1330 result = nameValue[1]; 1331 } 1332 } 1333 } 1334 1335 Log.d(TAG, "p2pGetDeviceAddress returning " + result); 1336 return result; 1337 } 1338 1339 public int getGroupCapability(String deviceAddress) { 1340 int gc = 0; 1341 if (TextUtils.isEmpty(deviceAddress)) return gc; 1342 String peerInfo = p2pPeer(deviceAddress); 1343 if (TextUtils.isEmpty(peerInfo)) return gc; 1344 1345 String[] tokens = peerInfo.split("\n"); 1346 for (String token : tokens) { 1347 if (token.startsWith("group_capab=")) { 1348 String[] nameValue = token.split("="); 1349 if (nameValue.length != 2) break; 1350 try { 1351 return Integer.decode(nameValue[1]); 1352 } catch(NumberFormatException e) { 1353 return gc; 1354 } 1355 } 1356 } 1357 return gc; 1358 } 1359 1360 public String p2pPeer(String deviceAddress) { 1361 return doStringCommand("P2P_PEER " + deviceAddress); 1362 } 1363 1364 private String p2pGetParam(String deviceAddress, String key) { 1365 if (deviceAddress == null) return null; 1366 1367 String peerInfo = p2pPeer(deviceAddress); 1368 if (peerInfo == null) return null; 1369 String[] tokens= peerInfo.split("\n"); 1370 1371 key += "="; 1372 for (String token : tokens) { 1373 if (token.startsWith(key)) { 1374 String[] nameValue = token.split("="); 1375 if (nameValue.length != 2) break; 1376 return nameValue[1]; 1377 } 1378 } 1379 return null; 1380 } 1381 1382 public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) { 1383 /* 1384 * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump> 1385 * P2P_SERVICE_ADD upnp <version hex> <service> 1386 * 1387 * e.g) 1388 * [Bonjour] 1389 * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.) 1390 * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027 1391 * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript) 1392 * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001 1393 * 09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074 1394 * 1395 * [UPnP] 1396 * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012 1397 * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice 1398 * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp 1399 * -org:device:InternetGatewayDevice:1 1400 * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp 1401 * -org:service:ContentDirectory:2 1402 */ 1403 synchronized (sLock) { 1404 for (String s : servInfo.getSupplicantQueryList()) { 1405 String command = "P2P_SERVICE_ADD"; 1406 command += (" " + s); 1407 if (!doBooleanCommand(command)) { 1408 return false; 1409 } 1410 } 1411 } 1412 return true; 1413 } 1414 1415 public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) { 1416 /* 1417 * P2P_SERVICE_DEL bonjour <query hexdump> 1418 * P2P_SERVICE_DEL upnp <version hex> <service> 1419 */ 1420 synchronized (sLock) { 1421 for (String s : servInfo.getSupplicantQueryList()) { 1422 String command = "P2P_SERVICE_DEL "; 1423 1424 String[] data = s.split(" "); 1425 if (data.length < 2) { 1426 return false; 1427 } 1428 if ("upnp".equals(data[0])) { 1429 command += s; 1430 } else if ("bonjour".equals(data[0])) { 1431 command += data[0]; 1432 command += (" " + data[1]); 1433 } else { 1434 return false; 1435 } 1436 if (!doBooleanCommand(command)) { 1437 return false; 1438 } 1439 } 1440 } 1441 return true; 1442 } 1443 1444 public boolean p2pServiceFlush() { 1445 return doBooleanCommand("P2P_SERVICE_FLUSH"); 1446 } 1447 1448 public String p2pServDiscReq(String addr, String query) { 1449 String command = "P2P_SERV_DISC_REQ"; 1450 command += (" " + addr); 1451 command += (" " + query); 1452 1453 return doStringCommand(command); 1454 } 1455 1456 public boolean p2pServDiscCancelReq(String id) { 1457 return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id); 1458 } 1459 1460 /* Set the current mode of miracast operation. 1461 * 0 = disabled 1462 * 1 = operating as source 1463 * 2 = operating as sink 1464 */ 1465 public void setMiracastMode(int mode) { 1466 // Note: optional feature on the driver. It is ok for this to fail. 1467 doBooleanCommand("DRIVER MIRACAST " + mode); 1468 } 1469 1470 public boolean fetchAnqp(String bssid, String subtypes) { 1471 return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes); 1472 } 1473 1474 /* 1475 * NFC-related calls 1476 */ 1477 public String getNfcWpsConfigurationToken(int netId) { 1478 return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId); 1479 } 1480 1481 public String getNfcHandoverRequest() { 1482 return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR"); 1483 } 1484 1485 public String getNfcHandoverSelect() { 1486 return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR"); 1487 } 1488 1489 public boolean initiatorReportNfcHandover(String selectMessage) { 1490 return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage); 1491 } 1492 1493 public boolean responderReportNfcHandover(String requestMessage) { 1494 return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00"); 1495 } 1496 1497 1498 /* kernel logging support */ 1499 private static native byte[] readKernelLogNative(); 1500 1501 synchronized public String readKernelLog() { 1502 byte[] bytes = readKernelLogNative(); 1503 if (bytes != null) { 1504 CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); 1505 try { 1506 CharBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes)); 1507 return decoded.toString(); 1508 } catch (CharacterCodingException cce) { 1509 return new String(bytes, StandardCharsets.ISO_8859_1); 1510 } 1511 } else { 1512 return "*** failed to read kernel log ***"; 1513 } 1514 } 1515 1516 /* WIFI HAL support */ 1517 1518 // HAL command ids 1519 private static int sCmdId = 1; 1520 private static int getNewCmdIdLocked() { 1521 return sCmdId++; 1522 } 1523 1524 private static final String TAG = "WifiNative-HAL"; 1525 private static long sWifiHalHandle = 0; /* used by JNI to save wifi_handle */ 1526 private static long[] sWifiIfaceHandles = null; /* used by JNI to save interface handles */ 1527 public static int sWlan0Index = -1; 1528 private static int sP2p0Index = -1; 1529 private static MonitorThread sThread; 1530 private static final int STOP_HAL_TIMEOUT_MS = 1000; 1531 1532 private static native boolean startHalNative(); 1533 private static native void stopHalNative(); 1534 private static native void waitForHalEventNative(); 1535 1536 private static class MonitorThread extends Thread { 1537 public void run() { 1538 Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle)); 1539 waitForHalEventNative(); 1540 } 1541 } 1542 1543 public boolean startHal() { 1544 String debugLog = "startHal stack: "; 1545 java.lang.StackTraceElement[] elements = Thread.currentThread().getStackTrace(); 1546 for (int i = 2; i < elements.length && i <= 7; i++ ) { 1547 debugLog = debugLog + " - " + elements[i].getMethodName(); 1548 } 1549 1550 sLocalLog.log(debugLog); 1551 1552 synchronized (sLock) { 1553 if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) { 1554 sThread = new MonitorThread(); 1555 sThread.start(); 1556 return true; 1557 } else { 1558 if (DBG) sLocalLog.log("Could not start hal"); 1559 Log.e(TAG, "Could not start hal"); 1560 return false; 1561 } 1562 } 1563 } 1564 1565 public void stopHal() { 1566 synchronized (sLock) { 1567 if (isHalStarted()) { 1568 stopHalNative(); 1569 try { 1570 sThread.join(STOP_HAL_TIMEOUT_MS); 1571 Log.d(TAG, "HAL event thread stopped successfully"); 1572 } catch (InterruptedException e) { 1573 Log.e(TAG, "Could not stop HAL cleanly"); 1574 } 1575 sThread = null; 1576 sWifiHalHandle = 0; 1577 sWifiIfaceHandles = null; 1578 sWlan0Index = -1; 1579 sP2p0Index = -1; 1580 } 1581 } 1582 } 1583 1584 public boolean isHalStarted() { 1585 return (sWifiHalHandle != 0); 1586 } 1587 private static native int getInterfacesNative(); 1588 1589 public int getInterfaces() { 1590 synchronized (sLock) { 1591 if (isHalStarted()) { 1592 if (sWifiIfaceHandles == null) { 1593 int num = getInterfacesNative(); 1594 int wifi_num = 0; 1595 for (int i = 0; i < num; i++) { 1596 String name = getInterfaceNameNative(i); 1597 Log.i(TAG, "interface[" + i + "] = " + name); 1598 if (name.equals("wlan0")) { 1599 sWlan0Index = i; 1600 wifi_num++; 1601 } else if (name.equals("p2p0")) { 1602 sP2p0Index = i; 1603 wifi_num++; 1604 } 1605 } 1606 return wifi_num; 1607 } else { 1608 return sWifiIfaceHandles.length; 1609 } 1610 } else { 1611 return 0; 1612 } 1613 } 1614 } 1615 1616 private static native String getInterfaceNameNative(int index); 1617 public String getInterfaceName(int index) { 1618 synchronized (sLock) { 1619 return getInterfaceNameNative(index); 1620 } 1621 } 1622 1623 // TODO: Change variable names to camel style. 1624 public static class ScanCapabilities { 1625 public int max_scan_cache_size; 1626 public int max_scan_buckets; 1627 public int max_ap_cache_per_scan; 1628 public int max_rssi_sample_size; 1629 public int max_scan_reporting_threshold; 1630 public int max_hotlist_bssids; 1631 public int max_significant_wifi_change_aps; 1632 } 1633 1634 public boolean getScanCapabilities(ScanCapabilities capabilities) { 1635 synchronized (sLock) { 1636 return isHalStarted() && getScanCapabilitiesNative(sWlan0Index, capabilities); 1637 } 1638 } 1639 1640 private static native boolean getScanCapabilitiesNative( 1641 int iface, ScanCapabilities capabilities); 1642 1643 private static native boolean startScanNative(int iface, int id, ScanSettings settings); 1644 private static native boolean stopScanNative(int iface, int id); 1645 private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush); 1646 private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface); 1647 private static native void setWifiLinkLayerStatsNative(int iface, int enable); 1648 1649 public static class ChannelSettings { 1650 int frequency; 1651 int dwell_time_ms; 1652 boolean passive; 1653 } 1654 1655 public static class BucketSettings { 1656 int bucket; 1657 int band; 1658 int period_ms; 1659 int max_period_ms; 1660 int step_count; 1661 int report_events; 1662 int num_channels; 1663 ChannelSettings channels[]; 1664 } 1665 1666 public static class ScanSettings { 1667 int base_period_ms; 1668 int max_ap_per_scan; 1669 int report_threshold_percent; 1670 int report_threshold_num_scans; 1671 int num_buckets; 1672 BucketSettings buckets[]; 1673 } 1674 1675 public static interface ScanEventHandler { 1676 void onScanResultsAvailable(); 1677 void onFullScanResult(ScanResult fullScanResult); 1678 void onScanStatus(); 1679 void onScanPaused(WifiScanner.ScanData[] data); 1680 void onScanRestarted(); 1681 } 1682 1683 // Callback from native 1684 private static void onScanResultsAvailable(int id) { 1685 ScanEventHandler handler = sScanEventHandler; 1686 if (handler != null) { 1687 handler.onScanResultsAvailable(); 1688 } 1689 } 1690 1691 /* scan status, keep these values in sync with gscan.h */ 1692 private static int WIFI_SCAN_BUFFER_FULL = 0; 1693 private static int WIFI_SCAN_COMPLETE = 1; 1694 1695 // Callback from native 1696 private static void onScanStatus(int status) { 1697 ScanEventHandler handler = sScanEventHandler; 1698 if (status == WIFI_SCAN_BUFFER_FULL) { 1699 /* we have a separate event to take care of this */ 1700 } else if (status == WIFI_SCAN_COMPLETE) { 1701 if (handler != null) { 1702 handler.onScanStatus(); 1703 } 1704 } 1705 } 1706 1707 public static WifiSsid createWifiSsid(byte[] rawSsid) { 1708 String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid)); 1709 1710 if (ssidHexString == null) { 1711 return null; 1712 } 1713 1714 WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString); 1715 1716 return wifiSsid; 1717 } 1718 1719 public static String ssidConvert(byte[] rawSsid) { 1720 String ssid; 1721 1722 CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); 1723 try { 1724 CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid)); 1725 ssid = decoded.toString(); 1726 } catch (CharacterCodingException cce) { 1727 ssid = null; 1728 } 1729 1730 if (ssid == null) { 1731 ssid = new String(rawSsid, StandardCharsets.ISO_8859_1); 1732 } 1733 1734 return ssid; 1735 } 1736 1737 // Called from native 1738 public static boolean setSsid(byte[] rawSsid, ScanResult result) { 1739 if (rawSsid == null || rawSsid.length == 0 || result == null) { 1740 return false; 1741 } 1742 1743 result.SSID = ssidConvert(rawSsid); 1744 result.wifiSsid = createWifiSsid(rawSsid); 1745 return true; 1746 } 1747 1748 private static void populateScanResult(ScanResult result, byte bytes[], String dbg) { 1749 if (bytes == null) return; 1750 if (dbg == null) dbg = ""; 1751 1752 InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation(); 1753 InformationElementUtil.VhtOperation vhtOperation = 1754 new InformationElementUtil.VhtOperation(); 1755 InformationElementUtil.ExtendedCapabilities extendedCaps = 1756 new InformationElementUtil.ExtendedCapabilities(); 1757 1758 ScanResult.InformationElement elements[] = 1759 InformationElementUtil.parseInformationElements(bytes); 1760 for (ScanResult.InformationElement ie : elements) { 1761 if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) { 1762 htOperation.from(ie); 1763 } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) { 1764 vhtOperation.from(ie); 1765 } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) { 1766 extendedCaps.from(ie); 1767 } 1768 } 1769 1770 if (extendedCaps.is80211McRTTResponder) { 1771 result.setFlag(ScanResult.FLAG_80211mc_RESPONDER); 1772 } else { 1773 result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER); 1774 } 1775 1776 //handle RTT related information 1777 if (vhtOperation.isValid()) { 1778 result.channelWidth = vhtOperation.getChannelWidth(); 1779 result.centerFreq0 = vhtOperation.getCenterFreq0(); 1780 result.centerFreq1 = vhtOperation.getCenterFreq1(); 1781 } else { 1782 result.channelWidth = htOperation.getChannelWidth(); 1783 result.centerFreq0 = htOperation.getCenterFreq0(result.frequency); 1784 result.centerFreq1 = 0; 1785 } 1786 if(DBG) { 1787 Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth + 1788 " PrimaryFreq: " + result.frequency +" mCenterfreq0: " + result.centerFreq0 + 1789 " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder ? 1790 "Support RTT reponder: " : "Do not support RTT responder")); 1791 } 1792 1793 result.informationElements = elements; 1794 } 1795 1796 // Callback from native 1797 private static void onFullScanResult(int id, ScanResult result, byte bytes[]) { 1798 if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " + 1799 "num = " + bytes.length); 1800 1801 ScanEventHandler handler = sScanEventHandler; 1802 if (handler != null) { 1803 populateScanResult(result, bytes, " onFullScanResult "); 1804 handler.onFullScanResult(result); 1805 } 1806 } 1807 1808 private static int sScanCmdId = 0; 1809 private static ScanEventHandler sScanEventHandler; 1810 private static ScanSettings sScanSettings; 1811 1812 public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) { 1813 synchronized (sLock) { 1814 if (isHalStarted()) { 1815 if (sScanCmdId != 0) { 1816 stopScan(); 1817 } else if (sScanSettings != null || sScanEventHandler != null) { 1818 /* current scan is paused; no need to stop it */ 1819 } 1820 1821 sScanCmdId = getNewCmdIdLocked(); 1822 1823 sScanSettings = settings; 1824 sScanEventHandler = eventHandler; 1825 1826 if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) { 1827 sScanEventHandler = null; 1828 sScanSettings = null; 1829 sScanCmdId = 0; 1830 return false; 1831 } 1832 1833 return true; 1834 } else { 1835 return false; 1836 } 1837 } 1838 } 1839 1840 public void stopScan() { 1841 synchronized (sLock) { 1842 if (isHalStarted()) { 1843 if (sScanCmdId != 0) { 1844 stopScanNative(sWlan0Index, sScanCmdId); 1845 } 1846 sScanSettings = null; 1847 sScanEventHandler = null; 1848 sScanCmdId = 0; 1849 } 1850 } 1851 } 1852 1853 public void pauseScan() { 1854 synchronized (sLock) { 1855 if (isHalStarted()) { 1856 if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) { 1857 Log.d(TAG, "Pausing scan"); 1858 WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true); 1859 stopScanNative(sWlan0Index, sScanCmdId); 1860 sScanCmdId = 0; 1861 sScanEventHandler.onScanPaused(scanData); 1862 } 1863 } 1864 } 1865 } 1866 1867 public void restartScan() { 1868 synchronized (sLock) { 1869 if (isHalStarted()) { 1870 if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) { 1871 Log.d(TAG, "Restarting scan"); 1872 ScanEventHandler handler = sScanEventHandler; 1873 ScanSettings settings = sScanSettings; 1874 if (startScan(sScanSettings, sScanEventHandler)) { 1875 sScanEventHandler.onScanRestarted(); 1876 } else { 1877 /* we are still paused; don't change state */ 1878 sScanEventHandler = handler; 1879 sScanSettings = settings; 1880 } 1881 } 1882 } 1883 } 1884 } 1885 1886 public WifiScanner.ScanData[] getScanResults(boolean flush) { 1887 synchronized (sLock) { 1888 WifiScanner.ScanData[] sd = null; 1889 if (isHalStarted()) { 1890 sd = getScanResultsNative(sWlan0Index, flush); 1891 } 1892 1893 if (sd != null) { 1894 return sd; 1895 } else { 1896 return new WifiScanner.ScanData[0]; 1897 } 1898 } 1899 } 1900 1901 public static interface HotlistEventHandler { 1902 void onHotlistApFound (ScanResult[] result); 1903 void onHotlistApLost (ScanResult[] result); 1904 } 1905 1906 private static int sHotlistCmdId = 0; 1907 private static HotlistEventHandler sHotlistEventHandler; 1908 1909 private native static boolean setHotlistNative(int iface, int id, 1910 WifiScanner.HotlistSettings settings); 1911 private native static boolean resetHotlistNative(int iface, int id); 1912 1913 public boolean setHotlist(WifiScanner.HotlistSettings settings, 1914 HotlistEventHandler eventHandler) { 1915 synchronized (sLock) { 1916 if (isHalStarted()) { 1917 if (sHotlistCmdId != 0) { 1918 return false; 1919 } else { 1920 sHotlistCmdId = getNewCmdIdLocked(); 1921 } 1922 1923 sHotlistEventHandler = eventHandler; 1924 if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) { 1925 sHotlistEventHandler = null; 1926 return false; 1927 } 1928 1929 return true; 1930 } else { 1931 return false; 1932 } 1933 } 1934 } 1935 1936 public void resetHotlist() { 1937 synchronized (sLock) { 1938 if (isHalStarted()) { 1939 if (sHotlistCmdId != 0) { 1940 resetHotlistNative(sWlan0Index, sHotlistCmdId); 1941 sHotlistCmdId = 0; 1942 sHotlistEventHandler = null; 1943 } 1944 } 1945 } 1946 } 1947 1948 // Callback from native 1949 private static void onHotlistApFound(int id, ScanResult[] results) { 1950 HotlistEventHandler handler = sHotlistEventHandler; 1951 if (handler != null) { 1952 handler.onHotlistApFound(results); 1953 } else { 1954 /* this can happen because of race conditions */ 1955 Log.d(TAG, "Ignoring hotlist AP found event"); 1956 } 1957 } 1958 1959 // Callback from native 1960 private static void onHotlistApLost(int id, ScanResult[] results) { 1961 HotlistEventHandler handler = sHotlistEventHandler; 1962 if (handler != null) { 1963 handler.onHotlistApLost(results); 1964 } else { 1965 /* this can happen because of race conditions */ 1966 Log.d(TAG, "Ignoring hotlist AP lost event"); 1967 } 1968 } 1969 1970 public static interface SignificantWifiChangeEventHandler { 1971 void onChangesFound(ScanResult[] result); 1972 } 1973 1974 private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler; 1975 private static int sSignificantWifiChangeCmdId; 1976 1977 private static native boolean trackSignificantWifiChangeNative( 1978 int iface, int id, WifiScanner.WifiChangeSettings settings); 1979 private static native boolean untrackSignificantWifiChangeNative(int iface, int id); 1980 1981 public boolean trackSignificantWifiChange( 1982 WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) { 1983 synchronized (sLock) { 1984 if (isHalStarted()) { 1985 if (sSignificantWifiChangeCmdId != 0) { 1986 return false; 1987 } else { 1988 sSignificantWifiChangeCmdId = getNewCmdIdLocked(); 1989 } 1990 1991 sSignificantWifiChangeHandler = handler; 1992 if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId, 1993 settings) == false) { 1994 sSignificantWifiChangeHandler = null; 1995 return false; 1996 } 1997 1998 return true; 1999 } else { 2000 return false; 2001 } 2002 2003 } 2004 } 2005 2006 public void untrackSignificantWifiChange() { 2007 synchronized (sLock) { 2008 if (isHalStarted()) { 2009 if (sSignificantWifiChangeCmdId != 0) { 2010 untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId); 2011 sSignificantWifiChangeCmdId = 0; 2012 sSignificantWifiChangeHandler = null; 2013 } 2014 } 2015 } 2016 } 2017 2018 // Callback from native 2019 private static void onSignificantWifiChange(int id, ScanResult[] results) { 2020 SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler; 2021 if (handler != null) { 2022 handler.onChangesFound(results); 2023 } else { 2024 /* this can happen because of race conditions */ 2025 Log.d(TAG, "Ignoring significant wifi change"); 2026 } 2027 } 2028 2029 public WifiLinkLayerStats getWifiLinkLayerStats(String iface) { 2030 // TODO: use correct iface name to Index translation 2031 if (iface == null) return null; 2032 synchronized (sLock) { 2033 if (isHalStarted()) { 2034 return getWifiLinkLayerStatsNative(sWlan0Index); 2035 } else { 2036 return null; 2037 } 2038 } 2039 } 2040 2041 public void setWifiLinkLayerStats(String iface, int enable) { 2042 if (iface == null) return; 2043 synchronized (sLock) { 2044 if (isHalStarted()) { 2045 setWifiLinkLayerStatsNative(sWlan0Index, enable); 2046 } 2047 } 2048 } 2049 2050 public static native int getSupportedFeatureSetNative(int iface); 2051 public int getSupportedFeatureSet() { 2052 synchronized (sLock) { 2053 if (isHalStarted()) { 2054 return getSupportedFeatureSetNative(sWlan0Index); 2055 } else { 2056 Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started"); 2057 return 0; 2058 } 2059 } 2060 } 2061 2062 /* Rtt related commands/events */ 2063 public static interface RttEventHandler { 2064 void onRttResults(RttManager.RttResult[] result); 2065 } 2066 2067 private static RttEventHandler sRttEventHandler; 2068 private static int sRttCmdId; 2069 2070 // Callback from native 2071 private static void onRttResults(int id, RttManager.RttResult[] results) { 2072 RttEventHandler handler = sRttEventHandler; 2073 if (handler != null && id == sRttCmdId) { 2074 Log.d(TAG, "Received " + results.length + " rtt results"); 2075 handler.onRttResults(results); 2076 sRttCmdId = 0; 2077 } else { 2078 Log.d(TAG, "RTT Received event for unknown cmd = " + id + 2079 ", current id = " + sRttCmdId); 2080 } 2081 } 2082 2083 private static native boolean requestRangeNative( 2084 int iface, int id, RttManager.RttParams[] params); 2085 private static native boolean cancelRangeRequestNative( 2086 int iface, int id, RttManager.RttParams[] params); 2087 2088 public boolean requestRtt( 2089 RttManager.RttParams[] params, RttEventHandler handler) { 2090 synchronized (sLock) { 2091 if (isHalStarted()) { 2092 if (sRttCmdId != 0) { 2093 Log.v("TAG", "Last one is still under measurement!"); 2094 return false; 2095 } else { 2096 sRttCmdId = getNewCmdIdLocked(); 2097 } 2098 sRttEventHandler = handler; 2099 Log.v(TAG, "native issue RTT request"); 2100 return requestRangeNative(sWlan0Index, sRttCmdId, params); 2101 } else { 2102 return false; 2103 } 2104 } 2105 } 2106 2107 public boolean cancelRtt(RttManager.RttParams[] params) { 2108 synchronized (sLock) { 2109 if (isHalStarted()) { 2110 if (sRttCmdId == 0) { 2111 return false; 2112 } 2113 2114 sRttCmdId = 0; 2115 2116 if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) { 2117 sRttEventHandler = null; 2118 Log.v(TAG, "RTT cancel Request Successfully"); 2119 return true; 2120 } else { 2121 Log.e(TAG, "RTT cancel Request failed"); 2122 return false; 2123 } 2124 } else { 2125 return false; 2126 } 2127 } 2128 } 2129 2130 private static native boolean setScanningMacOuiNative(int iface, byte[] oui); 2131 2132 public boolean setScanningMacOui(byte[] oui) { 2133 synchronized (sLock) { 2134 if (isHalStarted()) { 2135 return setScanningMacOuiNative(sWlan0Index, oui); 2136 } else { 2137 return false; 2138 } 2139 } 2140 } 2141 2142 private static native int[] getChannelsForBandNative( 2143 int iface, int band); 2144 2145 public int [] getChannelsForBand(int band) { 2146 synchronized (sLock) { 2147 if (isHalStarted()) { 2148 return getChannelsForBandNative(sWlan0Index, band); 2149 } else { 2150 return null; 2151 } 2152 } 2153 } 2154 2155 private static native boolean isGetChannelsForBandSupportedNative(); 2156 public boolean isGetChannelsForBandSupported(){ 2157 synchronized (sLock) { 2158 if (isHalStarted()) { 2159 return isGetChannelsForBandSupportedNative(); 2160 } else { 2161 return false; 2162 } 2163 } 2164 } 2165 2166 private static native boolean setDfsFlagNative(int iface, boolean dfsOn); 2167 public boolean setDfsFlag(boolean dfsOn) { 2168 synchronized (sLock) { 2169 if (isHalStarted()) { 2170 return setDfsFlagNative(sWlan0Index, dfsOn); 2171 } else { 2172 return false; 2173 } 2174 } 2175 } 2176 2177 private static native boolean toggleInterfaceNative(int on); 2178 public boolean toggleInterface(int on) { 2179 synchronized (sLock) { 2180 if (isHalStarted()) { 2181 return toggleInterfaceNative(on); 2182 } else { 2183 return false; 2184 } 2185 } 2186 } 2187 2188 private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface); 2189 public RttManager.RttCapabilities getRttCapabilities() { 2190 synchronized (sLock) { 2191 if (isHalStarted()) { 2192 return getRttCapabilitiesNative(sWlan0Index); 2193 } else { 2194 return null; 2195 } 2196 } 2197 } 2198 2199 private static native boolean setCountryCodeHalNative(int iface, String CountryCode); 2200 public boolean setCountryCodeHal(String CountryCode) { 2201 synchronized (sLock) { 2202 if (isHalStarted()) { 2203 return setCountryCodeHalNative(sWlan0Index, CountryCode); 2204 } else { 2205 return false; 2206 } 2207 } 2208 } 2209 2210 /* Rtt related commands/events */ 2211 public abstract class TdlsEventHandler { 2212 abstract public void onTdlsStatus(String macAddr, int status, int reason); 2213 } 2214 2215 private static TdlsEventHandler sTdlsEventHandler; 2216 2217 private static native boolean enableDisableTdlsNative(int iface, boolean enable, 2218 String macAddr); 2219 public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) { 2220 synchronized (sLock) { 2221 sTdlsEventHandler = tdlsCallBack; 2222 return enableDisableTdlsNative(sWlan0Index, enable, macAdd); 2223 } 2224 } 2225 2226 // Once TDLS per mac and event feature is implemented, this class definition should be 2227 // moved to the right place, like WifiManager etc 2228 public static class TdlsStatus { 2229 int channel; 2230 int global_operating_class; 2231 int state; 2232 int reason; 2233 } 2234 private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr); 2235 public TdlsStatus getTdlsStatus(String macAdd) { 2236 synchronized (sLock) { 2237 if (isHalStarted()) { 2238 return getTdlsStatusNative(sWlan0Index, macAdd); 2239 } else { 2240 return null; 2241 } 2242 } 2243 } 2244 2245 //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be 2246 // moved to the right place, like WifiStateMachine etc 2247 public static class TdlsCapabilities { 2248 /* Maximum TDLS session number can be supported by the Firmware and hardware */ 2249 int maxConcurrentTdlsSessionNumber; 2250 boolean isGlobalTdlsSupported; 2251 boolean isPerMacTdlsSupported; 2252 boolean isOffChannelTdlsSupported; 2253 } 2254 2255 2256 2257 private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface); 2258 public TdlsCapabilities getTdlsCapabilities () { 2259 synchronized (sLock) { 2260 if (isHalStarted()) { 2261 return getTdlsCapabilitiesNative(sWlan0Index); 2262 } else { 2263 return null; 2264 } 2265 } 2266 } 2267 2268 private static boolean onTdlsStatus(String macAddr, int status, int reason) { 2269 TdlsEventHandler handler = sTdlsEventHandler; 2270 if (handler == null) { 2271 return false; 2272 } else { 2273 handler.onTdlsStatus(macAddr, status, reason); 2274 return true; 2275 } 2276 } 2277 2278 //--------------------------------------------------------------------------------- 2279 2280 /* Wifi Logger commands/events */ 2281 2282 public static interface WifiLoggerEventHandler { 2283 void onRingBufferData(RingBufferStatus status, byte[] buffer); 2284 void onWifiAlert(int errorCode, byte[] buffer); 2285 } 2286 2287 private static WifiLoggerEventHandler sWifiLoggerEventHandler = null; 2288 2289 // Callback from native 2290 private static void onRingBufferData(RingBufferStatus status, byte[] buffer) { 2291 WifiLoggerEventHandler handler = sWifiLoggerEventHandler; 2292 if (handler != null) 2293 handler.onRingBufferData(status, buffer); 2294 } 2295 2296 // Callback from native 2297 private static void onWifiAlert(byte[] buffer, int errorCode) { 2298 WifiLoggerEventHandler handler = sWifiLoggerEventHandler; 2299 if (handler != null) 2300 handler.onWifiAlert(errorCode, buffer); 2301 } 2302 2303 private static int sLogCmdId = -1; 2304 private static native boolean setLoggingEventHandlerNative(int iface, int id); 2305 public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) { 2306 synchronized (sLock) { 2307 if (isHalStarted()) { 2308 int oldId = sLogCmdId; 2309 sLogCmdId = getNewCmdIdLocked(); 2310 if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) { 2311 sLogCmdId = oldId; 2312 return false; 2313 } 2314 sWifiLoggerEventHandler = handler; 2315 return true; 2316 } else { 2317 return false; 2318 } 2319 } 2320 } 2321 2322 private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel, 2323 int flags, int minIntervalSec ,int minDataSize, String ringName); 2324 public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval, 2325 int minDataSize, String ringName){ 2326 synchronized (sLock) { 2327 if (isHalStarted()) { 2328 return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval, 2329 minDataSize, ringName); 2330 } else { 2331 return false; 2332 } 2333 } 2334 } 2335 2336 private static native int getSupportedLoggerFeatureSetNative(int iface); 2337 public int getSupportedLoggerFeatureSet() { 2338 synchronized (sLock) { 2339 if (isHalStarted()) { 2340 return getSupportedLoggerFeatureSetNative(sWlan0Index); 2341 } else { 2342 return 0; 2343 } 2344 } 2345 } 2346 2347 private static native boolean resetLogHandlerNative(int iface, int id); 2348 public boolean resetLogHandler() { 2349 synchronized (sLock) { 2350 if (isHalStarted()) { 2351 if (sLogCmdId == -1) { 2352 Log.e(TAG,"Can not reset handler Before set any handler"); 2353 return false; 2354 } 2355 sWifiLoggerEventHandler = null; 2356 if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) { 2357 sLogCmdId = -1; 2358 return true; 2359 } else { 2360 return false; 2361 } 2362 } else { 2363 return false; 2364 } 2365 } 2366 } 2367 2368 private static native String getDriverVersionNative(int iface); 2369 public String getDriverVersion() { 2370 synchronized (sLock) { 2371 if (isHalStarted()) { 2372 return getDriverVersionNative(sWlan0Index); 2373 } else { 2374 return ""; 2375 } 2376 } 2377 } 2378 2379 2380 private static native String getFirmwareVersionNative(int iface); 2381 public String getFirmwareVersion() { 2382 synchronized (sLock) { 2383 if (isHalStarted()) { 2384 return getFirmwareVersionNative(sWlan0Index); 2385 } else { 2386 return ""; 2387 } 2388 } 2389 } 2390 2391 public static class RingBufferStatus{ 2392 String name; 2393 int flag; 2394 int ringBufferId; 2395 int ringBufferByteSize; 2396 int verboseLevel; 2397 int writtenBytes; 2398 int readBytes; 2399 int writtenRecords; 2400 2401 @Override 2402 public String toString() { 2403 return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId + 2404 " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel + 2405 " writtenBytes: " + writtenBytes + " readBytes: " + readBytes + 2406 " writtenRecords: " + writtenRecords; 2407 } 2408 } 2409 2410 private static native RingBufferStatus[] getRingBufferStatusNative(int iface); 2411 public RingBufferStatus[] getRingBufferStatus() { 2412 synchronized (sLock) { 2413 if (isHalStarted()) { 2414 return getRingBufferStatusNative(sWlan0Index); 2415 } else { 2416 return null; 2417 } 2418 } 2419 } 2420 2421 private static native boolean getRingBufferDataNative(int iface, String ringName); 2422 public boolean getRingBufferData(String ringName) { 2423 synchronized (sLock) { 2424 if (isHalStarted()) { 2425 return getRingBufferDataNative(sWlan0Index, ringName); 2426 } else { 2427 return false; 2428 } 2429 } 2430 } 2431 2432 private static byte[] mFwMemoryDump; 2433 // Callback from native 2434 private static void onWifiFwMemoryAvailable(byte[] buffer) { 2435 mFwMemoryDump = buffer; 2436 if (DBG) { 2437 Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " + 2438 (buffer == null ? 0 : buffer.length)); 2439 } 2440 } 2441 2442 private static native boolean getFwMemoryDumpNative(int iface); 2443 public byte[] getFwMemoryDump() { 2444 synchronized (sLock) { 2445 if (isHalStarted()) { 2446 if(getFwMemoryDumpNative(sWlan0Index)) { 2447 byte[] fwMemoryDump = mFwMemoryDump; 2448 mFwMemoryDump = null; 2449 return fwMemoryDump; 2450 } else { 2451 return null; 2452 } 2453 } 2454 return null; 2455 } 2456 } 2457 2458 //--------------------------------------------------------------------------------- 2459 /* Configure ePNO */ 2460 2461 /* pno flags, keep these values in sync with gscan.h */ 2462 private static int WIFI_PNO_AUTH_CODE_OPEN = 1; // open 2463 private static int WIFI_PNO_AUTH_CODE_PSK = 2; // WPA_PSK or WPA2PSK 2464 private static int WIFI_PNO_AUTH_CODE_EAPOL = 4; // any EAPOL 2465 2466 // Whether directed scan needs to be performed (for hidden SSIDs) 2467 private static int WIFI_PNO_FLAG_DIRECTED_SCAN = 1; 2468 // Whether PNO event shall be triggered if the network is found on A band 2469 private static int WIFI_PNO_FLAG_A_BAND = 2; 2470 // Whether PNO event shall be triggered if the network is found on G band 2471 private static int WIFI_PNO_FLAG_G_BAND = 4; 2472 // Whether strict matching is required (i.e. firmware shall not match on the entire SSID) 2473 private static int WIFI_PNO_FLAG_STRICT_MATCH = 8; 2474 2475 public static class WifiPnoNetwork { 2476 String SSID; 2477 int rssi_threshold; 2478 int flags; 2479 int auth; 2480 String configKey; // kept for reference 2481 2482 WifiPnoNetwork(WifiConfiguration config, int threshold) { 2483 if (config.SSID == null) { 2484 this.SSID = ""; 2485 this.flags = WIFI_PNO_FLAG_DIRECTED_SCAN; 2486 } else { 2487 this.SSID = config.SSID; 2488 } 2489 this.rssi_threshold = threshold; 2490 if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) { 2491 auth |= WIFI_PNO_AUTH_CODE_PSK; 2492 } else if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) || 2493 config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) { 2494 auth |= WIFI_PNO_AUTH_CODE_EAPOL; 2495 } else if (config.wepKeys[0] != null) { 2496 auth |= WIFI_PNO_AUTH_CODE_OPEN; 2497 } else { 2498 auth |= WIFI_PNO_AUTH_CODE_OPEN; 2499 } 2500 2501 flags |= WIFI_PNO_FLAG_A_BAND | WIFI_PNO_FLAG_G_BAND; 2502 configKey = config.configKey(); 2503 } 2504 2505 @Override 2506 public String toString() { 2507 StringBuilder sbuf = new StringBuilder(); 2508 sbuf.append(this.SSID); 2509 sbuf.append(" flags=").append(this.flags); 2510 sbuf.append(" rssi=").append(this.rssi_threshold); 2511 sbuf.append(" auth=").append(this.auth); 2512 return sbuf.toString(); 2513 } 2514 } 2515 2516 public static interface WifiPnoEventHandler { 2517 void onPnoNetworkFound(ScanResult results[]); 2518 } 2519 2520 private static WifiPnoEventHandler sWifiPnoEventHandler; 2521 2522 private static int sPnoCmdId = 0; 2523 2524 private native static boolean setPnoListNative(int iface, int id, WifiPnoNetwork list[]); 2525 2526 public boolean setPnoList(WifiPnoNetwork list[], 2527 WifiPnoEventHandler eventHandler) { 2528 Log.e(TAG, "setPnoList cmd " + sPnoCmdId); 2529 2530 synchronized (sLock) { 2531 if (isHalStarted()) { 2532 2533 sPnoCmdId = getNewCmdIdLocked(); 2534 2535 sWifiPnoEventHandler = eventHandler; 2536 if (setPnoListNative(sWlan0Index, sPnoCmdId, list)) { 2537 return true; 2538 } 2539 } 2540 2541 sWifiPnoEventHandler = null; 2542 return false; 2543 } 2544 } 2545 2546 // Callback from native 2547 private static void onPnoNetworkFound(int id, ScanResult[] results) { 2548 if (results == null) { 2549 Log.e(TAG, "onPnoNetworkFound null results"); 2550 return; 2551 2552 } 2553 Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length); 2554 2555 WifiPnoEventHandler handler = sWifiPnoEventHandler; 2556 if (sPnoCmdId != 0 && handler != null) { 2557 for (int i=0; i<results.length; i++) { 2558 Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID 2559 + " " + results[i].level + " " + results[i].frequency); 2560 2561 populateScanResult(results[i], results[i].bytes, "onPnoNetworkFound "); 2562 results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID); 2563 } 2564 2565 handler.onPnoNetworkFound(results); 2566 } else { 2567 /* this can happen because of race conditions */ 2568 Log.d(TAG, "Ignoring Pno Network found event"); 2569 } 2570 } 2571 2572 public static class WifiLazyRoamParams { 2573 int A_band_boost_threshold; 2574 int A_band_penalty_threshold; 2575 int A_band_boost_factor; 2576 int A_band_penalty_factor; 2577 int A_band_max_boost; 2578 int lazy_roam_hysteresis; 2579 int alert_roam_rssi_trigger; 2580 2581 WifiLazyRoamParams() { 2582 } 2583 2584 @Override 2585 public String toString() { 2586 StringBuilder sbuf = new StringBuilder(); 2587 sbuf.append(" A_band_boost_threshold=").append(this.A_band_boost_threshold); 2588 sbuf.append(" A_band_penalty_threshold=").append(this.A_band_penalty_threshold); 2589 sbuf.append(" A_band_boost_factor=").append(this.A_band_boost_factor); 2590 sbuf.append(" A_band_penalty_factor=").append(this.A_band_penalty_factor); 2591 sbuf.append(" A_band_max_boost=").append(this.A_band_max_boost); 2592 sbuf.append(" lazy_roam_hysteresis=").append(this.lazy_roam_hysteresis); 2593 sbuf.append(" alert_roam_rssi_trigger=").append(this.alert_roam_rssi_trigger); 2594 return sbuf.toString(); 2595 } 2596 } 2597 2598 private native static boolean setLazyRoamNative(int iface, int id, 2599 boolean enabled, WifiLazyRoamParams param); 2600 2601 public boolean setLazyRoam(boolean enabled, WifiLazyRoamParams params) { 2602 synchronized (sLock) { 2603 if (isHalStarted()) { 2604 sPnoCmdId = getNewCmdIdLocked(); 2605 return setLazyRoamNative(sWlan0Index, sPnoCmdId, enabled, params); 2606 } else { 2607 return false; 2608 } 2609 } 2610 } 2611 2612 private native static boolean setBssidBlacklistNative(int iface, int id, 2613 String list[]); 2614 2615 public boolean setBssidBlacklist(String list[]) { 2616 int size = 0; 2617 if (list != null) { 2618 size = list.length; 2619 } 2620 Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size); 2621 2622 synchronized (sLock) { 2623 if (isHalStarted()) { 2624 sPnoCmdId = getNewCmdIdLocked(); 2625 return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list); 2626 } else { 2627 return false; 2628 } 2629 } 2630 } 2631 2632 private native static boolean setSsidWhitelistNative(int iface, int id, String list[]); 2633 2634 public boolean setSsidWhitelist(String list[]) { 2635 int size = 0; 2636 if (list != null) { 2637 size = list.length; 2638 } 2639 Log.e(TAG, "setSsidWhitelist cmd " + sPnoCmdId + " size " + size); 2640 2641 synchronized (sLock) { 2642 if (isHalStarted()) { 2643 sPnoCmdId = getNewCmdIdLocked(); 2644 2645 return setSsidWhitelistNative(sWlan0Index, sPnoCmdId, list); 2646 } else { 2647 return false; 2648 } 2649 } 2650 } 2651 2652 private native static int startSendingOffloadedPacketNative(int iface, int idx, 2653 byte[] srcMac, byte[] dstMac, byte[] pktData, int period); 2654 2655 public int 2656 startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) { 2657 Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period); 2658 2659 String[] macAddrStr = getMacAddress().split(":"); 2660 byte[] srcMac = new byte[6]; 2661 for(int i = 0; i < 6; i++) { 2662 Integer hexVal = Integer.parseInt(macAddrStr[i], 16); 2663 srcMac[i] = hexVal.byteValue(); 2664 } 2665 synchronized (sLock) { 2666 if (isHalStarted()) { 2667 return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac, 2668 keepAlivePacket.dstMac, keepAlivePacket.data, period); 2669 } else { 2670 return -1; 2671 } 2672 } 2673 } 2674 2675 private native static int stopSendingOffloadedPacketNative(int iface, int idx); 2676 2677 public int 2678 stopSendingOffloadedPacket(int slot) { 2679 Log.d(TAG, "stopSendingOffloadedPacket " + slot); 2680 synchronized (sLock) { 2681 if (isHalStarted()) { 2682 return stopSendingOffloadedPacketNative(sWlan0Index, slot); 2683 } else { 2684 return -1; 2685 } 2686 } 2687 } 2688 2689 public static interface WifiRssiEventHandler { 2690 void onRssiThresholdBreached(byte curRssi); 2691 } 2692 2693 private static WifiRssiEventHandler sWifiRssiEventHandler; 2694 2695 // Callback from native 2696 private static void onRssiThresholdBreached(int id, byte curRssi) { 2697 WifiRssiEventHandler handler = sWifiRssiEventHandler; 2698 if (handler != null) { 2699 handler.onRssiThresholdBreached(curRssi); 2700 } 2701 } 2702 2703 private native static int startRssiMonitoringNative(int iface, int id, 2704 byte maxRssi, byte minRssi); 2705 2706 private static int sRssiMonitorCmdId = 0; 2707 2708 public int startRssiMonitoring(byte maxRssi, byte minRssi, 2709 WifiRssiEventHandler rssiEventHandler) { 2710 Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi); 2711 synchronized (sLock) { 2712 sWifiRssiEventHandler = rssiEventHandler; 2713 if (isHalStarted()) { 2714 if (sRssiMonitorCmdId != 0) { 2715 stopRssiMonitoring(); 2716 } 2717 2718 sRssiMonitorCmdId = getNewCmdIdLocked(); 2719 Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId); 2720 int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId, 2721 maxRssi, minRssi); 2722 if (ret != 0) { // if not success 2723 sRssiMonitorCmdId = 0; 2724 } 2725 return ret; 2726 } else { 2727 return -1; 2728 } 2729 } 2730 } 2731 2732 private native static int stopRssiMonitoringNative(int iface, int idx); 2733 2734 public int stopRssiMonitoring() { 2735 Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId); 2736 synchronized (sLock) { 2737 if (isHalStarted()) { 2738 int ret = 0; 2739 if (sRssiMonitorCmdId != 0) { 2740 ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId); 2741 } 2742 sRssiMonitorCmdId = 0; 2743 return ret; 2744 } else { 2745 return -1; 2746 } 2747 } 2748 } 2749 2750 private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface); 2751} 2752