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