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