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