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