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