WifiNative.java revision 2a6d76f0899289cd3b96e3428f02076fdbc0363e
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.app.AlarmManager; 20import android.app.PendingIntent; 21import android.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.Intent; 24import android.content.IntentFilter; 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.WifiManager; 31import android.net.wifi.WifiScanner; 32import android.net.wifi.WifiSsid; 33import android.net.wifi.WifiWakeReasonAndCounts; 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 /* scan status, keep these values in sync with gscan.h */ 1742 private static final int WIFI_SCAN_RESULTS_AVAILABLE = 0; 1743 private static final int WIFI_SCAN_THRESHOLD_NUM_SCANS = 1; 1744 private static final int WIFI_SCAN_THRESHOLD_PERCENT = 2; 1745 private static final int WIFI_SCAN_DISABLED = 3; 1746 1747 // Callback from native 1748 private static void onScanStatus(int id, int event) { 1749 ScanEventHandler handler = sScanEventHandler; 1750 if (event == WIFI_SCAN_RESULTS_AVAILABLE || event == WIFI_SCAN_THRESHOLD_NUM_SCANS 1751 || event == WIFI_SCAN_THRESHOLD_PERCENT) { 1752 if (handler != null) { 1753 // TODO pass event back to framework 1754 handler.onScanStatus(); 1755 } 1756 } 1757 else if (event == WIFI_SCAN_DISABLED) { 1758 // TODO signal that scan has been disabled 1759 } 1760 } 1761 1762 public static WifiSsid createWifiSsid(byte[] rawSsid) { 1763 String ssidHexString = String.valueOf(HexEncoding.encode(rawSsid)); 1764 1765 if (ssidHexString == null) { 1766 return null; 1767 } 1768 1769 WifiSsid wifiSsid = WifiSsid.createFromHex(ssidHexString); 1770 1771 return wifiSsid; 1772 } 1773 1774 public static String ssidConvert(byte[] rawSsid) { 1775 String ssid; 1776 1777 CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); 1778 try { 1779 CharBuffer decoded = decoder.decode(ByteBuffer.wrap(rawSsid)); 1780 ssid = decoded.toString(); 1781 } catch (CharacterCodingException cce) { 1782 ssid = null; 1783 } 1784 1785 if (ssid == null) { 1786 ssid = new String(rawSsid, StandardCharsets.ISO_8859_1); 1787 } 1788 1789 return ssid; 1790 } 1791 1792 // Called from native 1793 public static boolean setSsid(byte[] rawSsid, ScanResult result) { 1794 if (rawSsid == null || rawSsid.length == 0 || result == null) { 1795 return false; 1796 } 1797 1798 result.SSID = ssidConvert(rawSsid); 1799 result.wifiSsid = createWifiSsid(rawSsid); 1800 return true; 1801 } 1802 1803 private static void populateScanResult(ScanResult result, byte bytes[], String dbg) { 1804 if (bytes == null) return; 1805 if (dbg == null) dbg = ""; 1806 1807 InformationElementUtil.HtOperation htOperation = new InformationElementUtil.HtOperation(); 1808 InformationElementUtil.VhtOperation vhtOperation = 1809 new InformationElementUtil.VhtOperation(); 1810 InformationElementUtil.ExtendedCapabilities extendedCaps = 1811 new InformationElementUtil.ExtendedCapabilities(); 1812 1813 ScanResult.InformationElement elements[] = 1814 InformationElementUtil.parseInformationElements(bytes); 1815 for (ScanResult.InformationElement ie : elements) { 1816 if(ie.id == ScanResult.InformationElement.EID_HT_OPERATION) { 1817 htOperation.from(ie); 1818 } else if(ie.id == ScanResult.InformationElement.EID_VHT_OPERATION) { 1819 vhtOperation.from(ie); 1820 } else if (ie.id == ScanResult.InformationElement.EID_EXTENDED_CAPS) { 1821 extendedCaps.from(ie); 1822 } 1823 } 1824 1825 if (extendedCaps.is80211McRTTResponder) { 1826 result.setFlag(ScanResult.FLAG_80211mc_RESPONDER); 1827 } else { 1828 result.clearFlag(ScanResult.FLAG_80211mc_RESPONDER); 1829 } 1830 1831 //handle RTT related information 1832 if (vhtOperation.isValid()) { 1833 result.channelWidth = vhtOperation.getChannelWidth(); 1834 result.centerFreq0 = vhtOperation.getCenterFreq0(); 1835 result.centerFreq1 = vhtOperation.getCenterFreq1(); 1836 } else { 1837 result.channelWidth = htOperation.getChannelWidth(); 1838 result.centerFreq0 = htOperation.getCenterFreq0(result.frequency); 1839 result.centerFreq1 = 0; 1840 } 1841 if(DBG) { 1842 Log.d(TAG, dbg + "SSID: " + result.SSID + " ChannelWidth is: " + result.channelWidth + 1843 " PrimaryFreq: " + result.frequency +" mCenterfreq0: " + result.centerFreq0 + 1844 " mCenterfreq1: " + result.centerFreq1 + (extendedCaps.is80211McRTTResponder ? 1845 "Support RTT reponder: " : "Do not support RTT responder")); 1846 } 1847 1848 result.informationElements = elements; 1849 } 1850 1851 // Callback from native 1852 private static void onFullScanResult(int id, ScanResult result, byte bytes[], 1853 int bucketsScanned) { 1854 if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " + 1855 "num = " + bytes.length); 1856 1857 ScanEventHandler handler = sScanEventHandler; 1858 if (handler != null) { 1859 populateScanResult(result, bytes, " onFullScanResult "); 1860 handler.onFullScanResult(result); 1861 } 1862 } 1863 1864 private static int sScanCmdId = 0; 1865 private static ScanEventHandler sScanEventHandler; 1866 private static ScanSettings sScanSettings; 1867 1868 public boolean startScan(ScanSettings settings, ScanEventHandler eventHandler) { 1869 synchronized (sLock) { 1870 if (isHalStarted()) { 1871 if (sScanCmdId != 0) { 1872 stopScan(); 1873 } else if (sScanSettings != null || sScanEventHandler != null) { 1874 /* current scan is paused; no need to stop it */ 1875 } 1876 1877 sScanCmdId = getNewCmdIdLocked(); 1878 1879 sScanSettings = settings; 1880 sScanEventHandler = eventHandler; 1881 1882 if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) { 1883 sScanEventHandler = null; 1884 sScanSettings = null; 1885 sScanCmdId = 0; 1886 return false; 1887 } 1888 1889 return true; 1890 } else { 1891 return false; 1892 } 1893 } 1894 } 1895 1896 public void stopScan() { 1897 synchronized (sLock) { 1898 if (isHalStarted()) { 1899 if (sScanCmdId != 0) { 1900 stopScanNative(sWlan0Index, sScanCmdId); 1901 } 1902 sScanSettings = null; 1903 sScanEventHandler = null; 1904 sScanCmdId = 0; 1905 } 1906 } 1907 } 1908 1909 public void pauseScan() { 1910 synchronized (sLock) { 1911 if (isHalStarted()) { 1912 if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) { 1913 Log.d(TAG, "Pausing scan"); 1914 WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true); 1915 stopScanNative(sWlan0Index, sScanCmdId); 1916 sScanCmdId = 0; 1917 sScanEventHandler.onScanPaused(scanData); 1918 } 1919 } 1920 } 1921 } 1922 1923 public void restartScan() { 1924 synchronized (sLock) { 1925 if (isHalStarted()) { 1926 if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) { 1927 Log.d(TAG, "Restarting scan"); 1928 ScanEventHandler handler = sScanEventHandler; 1929 ScanSettings settings = sScanSettings; 1930 if (startScan(sScanSettings, sScanEventHandler)) { 1931 sScanEventHandler.onScanRestarted(); 1932 } else { 1933 /* we are still paused; don't change state */ 1934 sScanEventHandler = handler; 1935 sScanSettings = settings; 1936 } 1937 } 1938 } 1939 } 1940 } 1941 1942 public WifiScanner.ScanData[] getScanResults(boolean flush) { 1943 synchronized (sLock) { 1944 WifiScanner.ScanData[] sd = null; 1945 if (isHalStarted()) { 1946 sd = getScanResultsNative(sWlan0Index, flush); 1947 } 1948 1949 if (sd != null) { 1950 return sd; 1951 } else { 1952 return new WifiScanner.ScanData[0]; 1953 } 1954 } 1955 } 1956 1957 public static interface HotlistEventHandler { 1958 void onHotlistApFound (ScanResult[] result); 1959 void onHotlistApLost (ScanResult[] result); 1960 } 1961 1962 private static int sHotlistCmdId = 0; 1963 private static HotlistEventHandler sHotlistEventHandler; 1964 1965 private native static boolean setHotlistNative(int iface, int id, 1966 WifiScanner.HotlistSettings settings); 1967 private native static boolean resetHotlistNative(int iface, int id); 1968 1969 public boolean setHotlist(WifiScanner.HotlistSettings settings, 1970 HotlistEventHandler eventHandler) { 1971 synchronized (sLock) { 1972 if (isHalStarted()) { 1973 if (sHotlistCmdId != 0) { 1974 return false; 1975 } else { 1976 sHotlistCmdId = getNewCmdIdLocked(); 1977 } 1978 1979 sHotlistEventHandler = eventHandler; 1980 if (setHotlistNative(sWlan0Index, sHotlistCmdId, settings) == false) { 1981 sHotlistEventHandler = null; 1982 return false; 1983 } 1984 1985 return true; 1986 } else { 1987 return false; 1988 } 1989 } 1990 } 1991 1992 public void resetHotlist() { 1993 synchronized (sLock) { 1994 if (isHalStarted()) { 1995 if (sHotlistCmdId != 0) { 1996 resetHotlistNative(sWlan0Index, sHotlistCmdId); 1997 sHotlistCmdId = 0; 1998 sHotlistEventHandler = null; 1999 } 2000 } 2001 } 2002 } 2003 2004 // Callback from native 2005 private static void onHotlistApFound(int id, ScanResult[] results) { 2006 HotlistEventHandler handler = sHotlistEventHandler; 2007 if (handler != null) { 2008 handler.onHotlistApFound(results); 2009 } else { 2010 /* this can happen because of race conditions */ 2011 Log.d(TAG, "Ignoring hotlist AP found event"); 2012 } 2013 } 2014 2015 // Callback from native 2016 private static void onHotlistApLost(int id, ScanResult[] results) { 2017 HotlistEventHandler handler = sHotlistEventHandler; 2018 if (handler != null) { 2019 handler.onHotlistApLost(results); 2020 } else { 2021 /* this can happen because of race conditions */ 2022 Log.d(TAG, "Ignoring hotlist AP lost event"); 2023 } 2024 } 2025 2026 public static interface SignificantWifiChangeEventHandler { 2027 void onChangesFound(ScanResult[] result); 2028 } 2029 2030 private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler; 2031 private static int sSignificantWifiChangeCmdId; 2032 2033 private static native boolean trackSignificantWifiChangeNative( 2034 int iface, int id, WifiScanner.WifiChangeSettings settings); 2035 private static native boolean untrackSignificantWifiChangeNative(int iface, int id); 2036 2037 public boolean trackSignificantWifiChange( 2038 WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) { 2039 synchronized (sLock) { 2040 if (isHalStarted()) { 2041 if (sSignificantWifiChangeCmdId != 0) { 2042 return false; 2043 } else { 2044 sSignificantWifiChangeCmdId = getNewCmdIdLocked(); 2045 } 2046 2047 sSignificantWifiChangeHandler = handler; 2048 if (trackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId, 2049 settings) == false) { 2050 sSignificantWifiChangeHandler = null; 2051 return false; 2052 } 2053 2054 return true; 2055 } else { 2056 return false; 2057 } 2058 2059 } 2060 } 2061 2062 public void untrackSignificantWifiChange() { 2063 synchronized (sLock) { 2064 if (isHalStarted()) { 2065 if (sSignificantWifiChangeCmdId != 0) { 2066 untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId); 2067 sSignificantWifiChangeCmdId = 0; 2068 sSignificantWifiChangeHandler = null; 2069 } 2070 } 2071 } 2072 } 2073 2074 // Callback from native 2075 private static void onSignificantWifiChange(int id, ScanResult[] results) { 2076 SignificantWifiChangeEventHandler handler = sSignificantWifiChangeHandler; 2077 if (handler != null) { 2078 handler.onChangesFound(results); 2079 } else { 2080 /* this can happen because of race conditions */ 2081 Log.d(TAG, "Ignoring significant wifi change"); 2082 } 2083 } 2084 2085 public WifiLinkLayerStats getWifiLinkLayerStats(String iface) { 2086 // TODO: use correct iface name to Index translation 2087 if (iface == null) return null; 2088 synchronized (sLock) { 2089 if (isHalStarted()) { 2090 return getWifiLinkLayerStatsNative(sWlan0Index); 2091 } else { 2092 return null; 2093 } 2094 } 2095 } 2096 2097 public void setWifiLinkLayerStats(String iface, int enable) { 2098 if (iface == null) return; 2099 synchronized (sLock) { 2100 if (isHalStarted()) { 2101 setWifiLinkLayerStatsNative(sWlan0Index, enable); 2102 } 2103 } 2104 } 2105 2106 public static native int getSupportedFeatureSetNative(int iface); 2107 public int getSupportedFeatureSet() { 2108 synchronized (sLock) { 2109 if (isHalStarted()) { 2110 return getSupportedFeatureSetNative(sWlan0Index); 2111 } else { 2112 Log.d(TAG, "Failing getSupportedFeatureset because HAL isn't started"); 2113 return 0; 2114 } 2115 } 2116 } 2117 2118 /* Rtt related commands/events */ 2119 public static interface RttEventHandler { 2120 void onRttResults(RttManager.RttResult[] result); 2121 } 2122 2123 private static RttEventHandler sRttEventHandler; 2124 private static int sRttCmdId; 2125 2126 // Callback from native 2127 private static void onRttResults(int id, RttManager.RttResult[] results) { 2128 RttEventHandler handler = sRttEventHandler; 2129 if (handler != null && id == sRttCmdId) { 2130 Log.d(TAG, "Received " + results.length + " rtt results"); 2131 handler.onRttResults(results); 2132 sRttCmdId = 0; 2133 } else { 2134 Log.d(TAG, "RTT Received event for unknown cmd = " + id + 2135 ", current id = " + sRttCmdId); 2136 } 2137 } 2138 2139 private static native boolean requestRangeNative( 2140 int iface, int id, RttManager.RttParams[] params); 2141 private static native boolean cancelRangeRequestNative( 2142 int iface, int id, RttManager.RttParams[] params); 2143 2144 public boolean requestRtt( 2145 RttManager.RttParams[] params, RttEventHandler handler) { 2146 synchronized (sLock) { 2147 if (isHalStarted()) { 2148 if (sRttCmdId != 0) { 2149 Log.v("TAG", "Last one is still under measurement!"); 2150 return false; 2151 } else { 2152 sRttCmdId = getNewCmdIdLocked(); 2153 } 2154 sRttEventHandler = handler; 2155 Log.v(TAG, "native issue RTT request"); 2156 return requestRangeNative(sWlan0Index, sRttCmdId, params); 2157 } else { 2158 return false; 2159 } 2160 } 2161 } 2162 2163 public boolean cancelRtt(RttManager.RttParams[] params) { 2164 synchronized (sLock) { 2165 if (isHalStarted()) { 2166 if (sRttCmdId == 0) { 2167 return false; 2168 } 2169 2170 sRttCmdId = 0; 2171 2172 if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) { 2173 sRttEventHandler = null; 2174 Log.v(TAG, "RTT cancel Request Successfully"); 2175 return true; 2176 } else { 2177 Log.e(TAG, "RTT cancel Request failed"); 2178 return false; 2179 } 2180 } else { 2181 return false; 2182 } 2183 } 2184 } 2185 2186 private static native boolean setScanningMacOuiNative(int iface, byte[] oui); 2187 2188 public boolean setScanningMacOui(byte[] oui) { 2189 synchronized (sLock) { 2190 if (isHalStarted()) { 2191 return setScanningMacOuiNative(sWlan0Index, oui); 2192 } else { 2193 return false; 2194 } 2195 } 2196 } 2197 2198 private static native int[] getChannelsForBandNative( 2199 int iface, int band); 2200 2201 public int [] getChannelsForBand(int band) { 2202 synchronized (sLock) { 2203 if (isHalStarted()) { 2204 return getChannelsForBandNative(sWlan0Index, band); 2205 } else { 2206 return null; 2207 } 2208 } 2209 } 2210 2211 private static native boolean isGetChannelsForBandSupportedNative(); 2212 public boolean isGetChannelsForBandSupported(){ 2213 synchronized (sLock) { 2214 if (isHalStarted()) { 2215 return isGetChannelsForBandSupportedNative(); 2216 } else { 2217 return false; 2218 } 2219 } 2220 } 2221 2222 private static native boolean setDfsFlagNative(int iface, boolean dfsOn); 2223 public boolean setDfsFlag(boolean dfsOn) { 2224 synchronized (sLock) { 2225 if (isHalStarted()) { 2226 return setDfsFlagNative(sWlan0Index, dfsOn); 2227 } else { 2228 return false; 2229 } 2230 } 2231 } 2232 2233 private static native boolean toggleInterfaceNative(int on); 2234 public boolean toggleInterface(int on) { 2235 synchronized (sLock) { 2236 if (isHalStarted()) { 2237 return toggleInterfaceNative(on); 2238 } else { 2239 return false; 2240 } 2241 } 2242 } 2243 2244 private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface); 2245 public RttManager.RttCapabilities getRttCapabilities() { 2246 synchronized (sLock) { 2247 if (isHalStarted()) { 2248 return getRttCapabilitiesNative(sWlan0Index); 2249 } else { 2250 return null; 2251 } 2252 } 2253 } 2254 2255 private static native boolean setCountryCodeHalNative(int iface, String CountryCode); 2256 public boolean setCountryCodeHal(String CountryCode) { 2257 synchronized (sLock) { 2258 if (isHalStarted()) { 2259 return setCountryCodeHalNative(sWlan0Index, CountryCode); 2260 } else { 2261 return false; 2262 } 2263 } 2264 } 2265 2266 /* Rtt related commands/events */ 2267 public abstract class TdlsEventHandler { 2268 abstract public void onTdlsStatus(String macAddr, int status, int reason); 2269 } 2270 2271 private static TdlsEventHandler sTdlsEventHandler; 2272 2273 private static native boolean enableDisableTdlsNative(int iface, boolean enable, 2274 String macAddr); 2275 public boolean enableDisableTdls(boolean enable, String macAdd, TdlsEventHandler tdlsCallBack) { 2276 synchronized (sLock) { 2277 sTdlsEventHandler = tdlsCallBack; 2278 return enableDisableTdlsNative(sWlan0Index, enable, macAdd); 2279 } 2280 } 2281 2282 // Once TDLS per mac and event feature is implemented, this class definition should be 2283 // moved to the right place, like WifiManager etc 2284 public static class TdlsStatus { 2285 int channel; 2286 int global_operating_class; 2287 int state; 2288 int reason; 2289 } 2290 private static native TdlsStatus getTdlsStatusNative(int iface, String macAddr); 2291 public TdlsStatus getTdlsStatus(String macAdd) { 2292 synchronized (sLock) { 2293 if (isHalStarted()) { 2294 return getTdlsStatusNative(sWlan0Index, macAdd); 2295 } else { 2296 return null; 2297 } 2298 } 2299 } 2300 2301 //ToFix: Once TDLS per mac and event feature is implemented, this class definition should be 2302 // moved to the right place, like WifiStateMachine etc 2303 public static class TdlsCapabilities { 2304 /* Maximum TDLS session number can be supported by the Firmware and hardware */ 2305 int maxConcurrentTdlsSessionNumber; 2306 boolean isGlobalTdlsSupported; 2307 boolean isPerMacTdlsSupported; 2308 boolean isOffChannelTdlsSupported; 2309 } 2310 2311 2312 2313 private static native TdlsCapabilities getTdlsCapabilitiesNative(int iface); 2314 public TdlsCapabilities getTdlsCapabilities () { 2315 synchronized (sLock) { 2316 if (isHalStarted()) { 2317 return getTdlsCapabilitiesNative(sWlan0Index); 2318 } else { 2319 return null; 2320 } 2321 } 2322 } 2323 2324 private static boolean onTdlsStatus(String macAddr, int status, int reason) { 2325 TdlsEventHandler handler = sTdlsEventHandler; 2326 if (handler == null) { 2327 return false; 2328 } else { 2329 handler.onTdlsStatus(macAddr, status, reason); 2330 return true; 2331 } 2332 } 2333 2334 //--------------------------------------------------------------------------------- 2335 2336 /* Wifi Logger commands/events */ 2337 2338 public static interface WifiLoggerEventHandler { 2339 void onRingBufferData(RingBufferStatus status, byte[] buffer); 2340 void onWifiAlert(int errorCode, byte[] buffer); 2341 } 2342 2343 private static WifiLoggerEventHandler sWifiLoggerEventHandler = null; 2344 2345 // Callback from native 2346 private static void onRingBufferData(RingBufferStatus status, byte[] buffer) { 2347 WifiLoggerEventHandler handler = sWifiLoggerEventHandler; 2348 if (handler != null) 2349 handler.onRingBufferData(status, buffer); 2350 } 2351 2352 // Callback from native 2353 private static void onWifiAlert(byte[] buffer, int errorCode) { 2354 WifiLoggerEventHandler handler = sWifiLoggerEventHandler; 2355 if (handler != null) 2356 handler.onWifiAlert(errorCode, buffer); 2357 } 2358 2359 private static int sLogCmdId = -1; 2360 private static native boolean setLoggingEventHandlerNative(int iface, int id); 2361 public boolean setLoggingEventHandler(WifiLoggerEventHandler handler) { 2362 synchronized (sLock) { 2363 if (isHalStarted()) { 2364 int oldId = sLogCmdId; 2365 sLogCmdId = getNewCmdIdLocked(); 2366 if (!setLoggingEventHandlerNative(sWlan0Index, sLogCmdId)) { 2367 sLogCmdId = oldId; 2368 return false; 2369 } 2370 sWifiLoggerEventHandler = handler; 2371 return true; 2372 } else { 2373 return false; 2374 } 2375 } 2376 } 2377 2378 private static native boolean startLoggingRingBufferNative(int iface, int verboseLevel, 2379 int flags, int minIntervalSec ,int minDataSize, String ringName); 2380 public boolean startLoggingRingBuffer(int verboseLevel, int flags, int maxInterval, 2381 int minDataSize, String ringName){ 2382 synchronized (sLock) { 2383 if (isHalStarted()) { 2384 return startLoggingRingBufferNative(sWlan0Index, verboseLevel, flags, maxInterval, 2385 minDataSize, ringName); 2386 } else { 2387 return false; 2388 } 2389 } 2390 } 2391 2392 private static native int getSupportedLoggerFeatureSetNative(int iface); 2393 public int getSupportedLoggerFeatureSet() { 2394 synchronized (sLock) { 2395 if (isHalStarted()) { 2396 return getSupportedLoggerFeatureSetNative(sWlan0Index); 2397 } else { 2398 return 0; 2399 } 2400 } 2401 } 2402 2403 private static native boolean resetLogHandlerNative(int iface, int id); 2404 public boolean resetLogHandler() { 2405 synchronized (sLock) { 2406 if (isHalStarted()) { 2407 if (sLogCmdId == -1) { 2408 Log.e(TAG,"Can not reset handler Before set any handler"); 2409 return false; 2410 } 2411 sWifiLoggerEventHandler = null; 2412 if (resetLogHandlerNative(sWlan0Index, sLogCmdId)) { 2413 sLogCmdId = -1; 2414 return true; 2415 } else { 2416 return false; 2417 } 2418 } else { 2419 return false; 2420 } 2421 } 2422 } 2423 2424 private static native String getDriverVersionNative(int iface); 2425 public String getDriverVersion() { 2426 synchronized (sLock) { 2427 if (isHalStarted()) { 2428 return getDriverVersionNative(sWlan0Index); 2429 } else { 2430 return ""; 2431 } 2432 } 2433 } 2434 2435 2436 private static native String getFirmwareVersionNative(int iface); 2437 public String getFirmwareVersion() { 2438 synchronized (sLock) { 2439 if (isHalStarted()) { 2440 return getFirmwareVersionNative(sWlan0Index); 2441 } else { 2442 return ""; 2443 } 2444 } 2445 } 2446 2447 public static class RingBufferStatus{ 2448 String name; 2449 int flag; 2450 int ringBufferId; 2451 int ringBufferByteSize; 2452 int verboseLevel; 2453 int writtenBytes; 2454 int readBytes; 2455 int writtenRecords; 2456 2457 @Override 2458 public String toString() { 2459 return "name: " + name + " flag: " + flag + " ringBufferId: " + ringBufferId + 2460 " ringBufferByteSize: " +ringBufferByteSize + " verboseLevel: " +verboseLevel + 2461 " writtenBytes: " + writtenBytes + " readBytes: " + readBytes + 2462 " writtenRecords: " + writtenRecords; 2463 } 2464 } 2465 2466 private static native RingBufferStatus[] getRingBufferStatusNative(int iface); 2467 public RingBufferStatus[] getRingBufferStatus() { 2468 synchronized (sLock) { 2469 if (isHalStarted()) { 2470 return getRingBufferStatusNative(sWlan0Index); 2471 } else { 2472 return null; 2473 } 2474 } 2475 } 2476 2477 private static native boolean getRingBufferDataNative(int iface, String ringName); 2478 public boolean getRingBufferData(String ringName) { 2479 synchronized (sLock) { 2480 if (isHalStarted()) { 2481 return getRingBufferDataNative(sWlan0Index, ringName); 2482 } else { 2483 return false; 2484 } 2485 } 2486 } 2487 2488 private static byte[] mFwMemoryDump; 2489 // Callback from native 2490 private static void onWifiFwMemoryAvailable(byte[] buffer) { 2491 mFwMemoryDump = buffer; 2492 if (DBG) { 2493 Log.d(TAG, "onWifiFwMemoryAvailable is called and buffer length is: " + 2494 (buffer == null ? 0 : buffer.length)); 2495 } 2496 } 2497 2498 private static native boolean getFwMemoryDumpNative(int iface); 2499 public byte[] getFwMemoryDump() { 2500 synchronized (sLock) { 2501 if (isHalStarted()) { 2502 if(getFwMemoryDumpNative(sWlan0Index)) { 2503 byte[] fwMemoryDump = mFwMemoryDump; 2504 mFwMemoryDump = null; 2505 return fwMemoryDump; 2506 } else { 2507 return null; 2508 } 2509 } 2510 return null; 2511 } 2512 } 2513 2514 //--------------------------------------------------------------------------------- 2515 /* Configure ePNO */ 2516 2517 /* pno flags, keep these values in sync with gscan.h */ 2518 private static int WIFI_PNO_AUTH_CODE_OPEN = 1; // open 2519 private static int WIFI_PNO_AUTH_CODE_PSK = 2; // WPA_PSK or WPA2PSK 2520 private static int WIFI_PNO_AUTH_CODE_EAPOL = 4; // any EAPOL 2521 2522 // Whether directed scan needs to be performed (for hidden SSIDs) 2523 private static int WIFI_PNO_FLAG_DIRECTED_SCAN = 1; 2524 // Whether PNO event shall be triggered if the network is found on A band 2525 private static int WIFI_PNO_FLAG_A_BAND = 2; 2526 // Whether PNO event shall be triggered if the network is found on G band 2527 private static int WIFI_PNO_FLAG_G_BAND = 4; 2528 // Whether strict matching is required (i.e. firmware shall not match on the entire SSID) 2529 private static int WIFI_PNO_FLAG_STRICT_MATCH = 8; 2530 2531 public static class WifiPnoNetwork { 2532 String SSID; 2533 int rssi_threshold; // TODO remove 2534 int flags; 2535 int auth; 2536 String configKey; // kept for reference 2537 2538 WifiPnoNetwork(WifiConfiguration config, int threshold) { 2539 if (config.SSID == null) { 2540 this.SSID = ""; 2541 this.flags = WIFI_PNO_FLAG_DIRECTED_SCAN; 2542 } else { 2543 this.SSID = config.SSID; 2544 } 2545 this.rssi_threshold = threshold; 2546 if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) { 2547 auth |= WIFI_PNO_AUTH_CODE_PSK; 2548 } else if (config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP) || 2549 config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) { 2550 auth |= WIFI_PNO_AUTH_CODE_EAPOL; 2551 } else if (config.wepKeys[0] != null) { 2552 auth |= WIFI_PNO_AUTH_CODE_OPEN; 2553 } else { 2554 auth |= WIFI_PNO_AUTH_CODE_OPEN; 2555 } 2556 2557 flags |= WIFI_PNO_FLAG_A_BAND | WIFI_PNO_FLAG_G_BAND; 2558 configKey = config.configKey(); 2559 } 2560 2561 @Override 2562 public String toString() { 2563 StringBuilder sbuf = new StringBuilder(); 2564 sbuf.append(this.SSID); 2565 sbuf.append(" flags=").append(this.flags); 2566 sbuf.append(" rssi=").append(this.rssi_threshold); 2567 sbuf.append(" auth=").append(this.auth); 2568 return sbuf.toString(); 2569 } 2570 } 2571 2572 public static interface WifiPnoEventHandler { 2573 void onPnoNetworkFound(ScanResult results[]); 2574 } 2575 2576 private static WifiPnoEventHandler sWifiPnoEventHandler; 2577 2578 private static int sPnoCmdId = 0; 2579 2580 private native static boolean setPnoListNative(int iface, int id, WifiPnoNetwork list[]); 2581 2582 public boolean setPnoList(WifiPnoNetwork list[], 2583 WifiPnoEventHandler eventHandler) { 2584 Log.e(TAG, "setPnoList cmd " + sPnoCmdId); 2585 2586 synchronized (sLock) { 2587 if (isHalStarted()) { 2588 2589 sPnoCmdId = getNewCmdIdLocked(); 2590 2591 sWifiPnoEventHandler = eventHandler; 2592 if (setPnoListNative(sWlan0Index, sPnoCmdId, list)) { 2593 return true; 2594 } 2595 } 2596 2597 sWifiPnoEventHandler = null; 2598 return false; 2599 } 2600 } 2601 2602 // Callback from native 2603 private static void onPnoNetworkFound(int id, ScanResult[] results) { 2604 if (results == null) { 2605 Log.e(TAG, "onPnoNetworkFound null results"); 2606 return; 2607 2608 } 2609 Log.d(TAG, "WifiNative.onPnoNetworkFound result " + results.length); 2610 2611 WifiPnoEventHandler handler = sWifiPnoEventHandler; 2612 if (sPnoCmdId != 0 && handler != null) { 2613 for (int i=0; i<results.length; i++) { 2614 Log.e(TAG, "onPnoNetworkFound SSID " + results[i].SSID 2615 + " " + results[i].level + " " + results[i].frequency); 2616 2617 populateScanResult(results[i], results[i].bytes, "onPnoNetworkFound "); 2618 results[i].wifiSsid = WifiSsid.createFromAsciiEncoded(results[i].SSID); 2619 } 2620 2621 handler.onPnoNetworkFound(results); 2622 } else { 2623 /* this can happen because of race conditions */ 2624 Log.d(TAG, "Ignoring Pno Network found event"); 2625 } 2626 } 2627 2628 // TODO remove 2629 public static class WifiLazyRoamParams { 2630 int A_band_boost_threshold; 2631 int A_band_penalty_threshold; 2632 int A_band_boost_factor; 2633 int A_band_penalty_factor; 2634 int A_band_max_boost; 2635 int lazy_roam_hysteresis; 2636 int alert_roam_rssi_trigger; 2637 2638 WifiLazyRoamParams() { 2639 } 2640 2641 @Override 2642 public String toString() { 2643 StringBuilder sbuf = new StringBuilder(); 2644 sbuf.append(" A_band_boost_threshold=").append(this.A_band_boost_threshold); 2645 sbuf.append(" A_band_penalty_threshold=").append(this.A_band_penalty_threshold); 2646 sbuf.append(" A_band_boost_factor=").append(this.A_band_boost_factor); 2647 sbuf.append(" A_band_penalty_factor=").append(this.A_band_penalty_factor); 2648 sbuf.append(" A_band_max_boost=").append(this.A_band_max_boost); 2649 sbuf.append(" lazy_roam_hysteresis=").append(this.lazy_roam_hysteresis); 2650 sbuf.append(" alert_roam_rssi_trigger=").append(this.alert_roam_rssi_trigger); 2651 return sbuf.toString(); 2652 } 2653 } 2654 2655 // TODO remove 2656 public boolean setLazyRoam(boolean enabled, WifiLazyRoamParams params) { 2657 return false; 2658 } 2659 2660 private native static boolean setBssidBlacklistNative(int iface, int id, 2661 String list[]); 2662 2663 public boolean setBssidBlacklist(String list[]) { 2664 int size = 0; 2665 if (list != null) { 2666 size = list.length; 2667 } 2668 Log.e(TAG, "setBssidBlacklist cmd " + sPnoCmdId + " size " + size); 2669 2670 synchronized (sLock) { 2671 if (isHalStarted()) { 2672 sPnoCmdId = getNewCmdIdLocked(); 2673 return setBssidBlacklistNative(sWlan0Index, sPnoCmdId, list); 2674 } else { 2675 return false; 2676 } 2677 } 2678 } 2679 2680 // TODO remove 2681 public boolean setSsidWhitelist(String list[]) { 2682 return false; 2683 } 2684 2685 private native static int startSendingOffloadedPacketNative(int iface, int idx, 2686 byte[] srcMac, byte[] dstMac, byte[] pktData, int period); 2687 2688 public int 2689 startSendingOffloadedPacket(int slot, KeepalivePacketData keepAlivePacket, int period) { 2690 Log.d(TAG, "startSendingOffloadedPacket slot=" + slot + " period=" + period); 2691 2692 String[] macAddrStr = getMacAddress().split(":"); 2693 byte[] srcMac = new byte[6]; 2694 for(int i = 0; i < 6; i++) { 2695 Integer hexVal = Integer.parseInt(macAddrStr[i], 16); 2696 srcMac[i] = hexVal.byteValue(); 2697 } 2698 synchronized (sLock) { 2699 if (isHalStarted()) { 2700 return startSendingOffloadedPacketNative(sWlan0Index, slot, srcMac, 2701 keepAlivePacket.dstMac, keepAlivePacket.data, period); 2702 } else { 2703 return -1; 2704 } 2705 } 2706 } 2707 2708 private native static int stopSendingOffloadedPacketNative(int iface, int idx); 2709 2710 public int 2711 stopSendingOffloadedPacket(int slot) { 2712 Log.d(TAG, "stopSendingOffloadedPacket " + slot); 2713 synchronized (sLock) { 2714 if (isHalStarted()) { 2715 return stopSendingOffloadedPacketNative(sWlan0Index, slot); 2716 } else { 2717 return -1; 2718 } 2719 } 2720 } 2721 2722 public static interface WifiRssiEventHandler { 2723 void onRssiThresholdBreached(byte curRssi); 2724 } 2725 2726 private static WifiRssiEventHandler sWifiRssiEventHandler; 2727 2728 // Callback from native 2729 private static void onRssiThresholdBreached(int id, byte curRssi) { 2730 WifiRssiEventHandler handler = sWifiRssiEventHandler; 2731 if (handler != null) { 2732 handler.onRssiThresholdBreached(curRssi); 2733 } 2734 } 2735 2736 private native static int startRssiMonitoringNative(int iface, int id, 2737 byte maxRssi, byte minRssi); 2738 2739 private static int sRssiMonitorCmdId = 0; 2740 2741 public int startRssiMonitoring(byte maxRssi, byte minRssi, 2742 WifiRssiEventHandler rssiEventHandler) { 2743 Log.d(TAG, "startRssiMonitoring: maxRssi=" + maxRssi + " minRssi=" + minRssi); 2744 synchronized (sLock) { 2745 sWifiRssiEventHandler = rssiEventHandler; 2746 if (isHalStarted()) { 2747 if (sRssiMonitorCmdId != 0) { 2748 stopRssiMonitoring(); 2749 } 2750 2751 sRssiMonitorCmdId = getNewCmdIdLocked(); 2752 Log.d(TAG, "sRssiMonitorCmdId = " + sRssiMonitorCmdId); 2753 int ret = startRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId, 2754 maxRssi, minRssi); 2755 if (ret != 0) { // if not success 2756 sRssiMonitorCmdId = 0; 2757 } 2758 return ret; 2759 } else { 2760 return -1; 2761 } 2762 } 2763 } 2764 2765 private native static int stopRssiMonitoringNative(int iface, int idx); 2766 2767 public int stopRssiMonitoring() { 2768 Log.d(TAG, "stopRssiMonitoring, cmdId " + sRssiMonitorCmdId); 2769 synchronized (sLock) { 2770 if (isHalStarted()) { 2771 int ret = 0; 2772 if (sRssiMonitorCmdId != 0) { 2773 ret = stopRssiMonitoringNative(sWlan0Index, sRssiMonitorCmdId); 2774 } 2775 sRssiMonitorCmdId = 0; 2776 return ret; 2777 } else { 2778 return -1; 2779 } 2780 } 2781 } 2782 2783 private static native WifiWakeReasonAndCounts getWlanWakeReasonCountNative(int iface); 2784} 2785