WifiNative.java revision ad607d99c372160c7d4b38e755e1b47d6419856e
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.WifiLinkLayerStats;
23import android.net.wifi.WifiManager;
24import android.net.wifi.WifiScanner;
25import android.net.wifi.RttManager;
26import android.net.wifi.WpsInfo;
27import android.net.wifi.p2p.WifiP2pConfig;
28import android.net.wifi.p2p.WifiP2pGroup;
29import android.net.wifi.p2p.nsd.WifiP2pServiceInfo;
30import android.os.SystemClock;
31import android.text.TextUtils;
32import android.util.LocalLog;
33import android.util.Log;
34
35import java.util.ArrayList;
36import java.util.List;
37import java.util.Locale;
38
39/**
40 * Native calls for bring up/shut down of the supplicant daemon and for
41 * sending requests to the supplicant daemon
42 *
43 * waitForEvent() is called on the monitor thread for events. All other methods
44 * must be serialized from the framework.
45 *
46 * {@hide}
47 */
48public class WifiNative {
49
50    private static boolean DBG = false;
51    private final String mTAG;
52    private static final int DEFAULT_GROUP_OWNER_INTENT     = 6;
53
54    static final int BLUETOOTH_COEXISTENCE_MODE_ENABLED     = 0;
55    static final int BLUETOOTH_COEXISTENCE_MODE_DISABLED    = 1;
56    static final int BLUETOOTH_COEXISTENCE_MODE_SENSE       = 2;
57
58    static final int SCAN_WITHOUT_CONNECTION_SETUP          = 1;
59    static final int SCAN_WITH_CONNECTION_SETUP             = 2;
60
61    // Hold this lock before calling supplicant - it is required to
62    // mutually exclude access from Wifi and P2p state machines
63    static final Object mLock = new Object();
64
65    public final String mInterfaceName;
66    public final String mInterfacePrefix;
67
68    private boolean mSuspendOptEnabled = false;
69
70    private static final int EID_HT_OPERATION = 61;
71    private static final int EID_VHT_OPERATION = 192;
72    private static final int EID_EXTENDED_CAPS = 127;
73    private static final int RTT_RESP_ENABLE_BIT = 70;
74    /* Register native functions */
75
76    static {
77        /* Native functions are defined in libwifi-service.so */
78        System.loadLibrary("wifi-service");
79        registerNatives();
80    }
81
82    private static native int registerNatives();
83
84    public native static boolean loadDriver();
85
86    public native static boolean isDriverLoaded();
87
88    public native static boolean unloadDriver();
89
90    public native static boolean startSupplicant(boolean p2pSupported);
91
92    /* Sends a kill signal to supplicant. To be used when we have lost connection
93       or when the supplicant is hung */
94    public native static boolean killSupplicant(boolean p2pSupported);
95
96    private native boolean connectToSupplicantNative();
97
98    private native void closeSupplicantConnectionNative();
99
100    /**
101     * Wait for the supplicant to send an event, returning the event string.
102     * @return the event string sent by the supplicant.
103     */
104    private native String waitForEventNative();
105
106    private native boolean doBooleanCommandNative(String command);
107
108    private native int doIntCommandNative(String command);
109
110    private native String doStringCommandNative(String command);
111
112    public WifiNative(String interfaceName) {
113        mInterfaceName = interfaceName;
114        mTAG = "WifiNative-" + interfaceName;
115        if (!interfaceName.equals("p2p0")) {
116            mInterfacePrefix = "IFNAME=" + interfaceName + " ";
117        } else {
118            // commands for p2p0 interface don't need prefix
119            mInterfacePrefix = "";
120        }
121    }
122
123    void enableVerboseLogging(int verbose) {
124        if (verbose > 0) {
125            DBG = true;
126        } else {
127            DBG = false;
128        }
129    }
130
131    private static final LocalLog mLocalLog = new LocalLog(1024);
132
133    // hold mLock before accessing mCmdIdLock
134    private static int sCmdId;
135
136    public LocalLog getLocalLog() {
137        return mLocalLog;
138    }
139
140    private static int getNewCmdIdLocked() {
141        return sCmdId++;
142    }
143
144    private void localLog(String s) {
145        if (mLocalLog != null)
146            mLocalLog.log(mInterfaceName + ": " + s);
147    }
148
149    public boolean connectToSupplicant() {
150        // No synchronization necessary .. it is implemented in WifiMonitor
151        localLog(mInterfacePrefix + "connectToSupplicant");
152        return connectToSupplicantNative();
153    }
154
155    public void closeSupplicantConnection() {
156        localLog(mInterfacePrefix + "closeSupplicantConnection");
157        closeSupplicantConnectionNative();
158    }
159
160    public String waitForEvent() {
161        // No synchronization necessary .. it is implemented in WifiMonitor
162        return waitForEventNative();
163    }
164
165    private boolean doBooleanCommand(String command) {
166        if (DBG) Log.d(mTAG, "doBoolean: " + command);
167        synchronized (mLock) {
168            int cmdId = getNewCmdIdLocked();
169            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
170            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
171            localLog(toLog + " -> " + result);
172            if (DBG) Log.d(mTAG, command + ": returned " + result);
173            return result;
174        }
175    }
176
177    private int doIntCommand(String command) {
178        if (DBG) Log.d(mTAG, "doInt: " + command);
179        synchronized (mLock) {
180            int cmdId = getNewCmdIdLocked();
181            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
182            int result = doIntCommandNative(mInterfacePrefix + command);
183            localLog(toLog + " -> " + result);
184            if (DBG) Log.d(mTAG, "   returned " + result);
185            return result;
186        }
187    }
188
189    private String doStringCommand(String command) {
190        if (DBG) {
191            //GET_NETWORK commands flood the logs
192            if (!command.startsWith("GET_NETWORK")) {
193                Log.d(mTAG, "doString: [" + command + "]");
194            }
195        }
196        synchronized (mLock) {
197            int cmdId = getNewCmdIdLocked();
198            String toLog = Integer.toString(cmdId) + ":" + mInterfacePrefix + command;
199            String result = doStringCommandNative(mInterfacePrefix + command);
200            if (result == null) {
201                if (DBG) Log.d(mTAG, "doStringCommandNative no result");
202            } else {
203                if (!command.startsWith("STATUS-")) {
204                    localLog(toLog + " -> " + result);
205                }
206                if (DBG) Log.d(mTAG, "   returned " + result.replace("\n", " "));
207            }
208            return result;
209        }
210    }
211
212    private String doStringCommandWithoutLogging(String command) {
213        if (DBG) {
214            //GET_NETWORK commands flood the logs
215            if (!command.startsWith("GET_NETWORK")) {
216                Log.d(mTAG, "doString: [" + command + "]");
217            }
218        }
219        synchronized (mLock) {
220            return doStringCommandNative(mInterfacePrefix + command);
221        }
222    }
223
224    public boolean ping() {
225        String pong = doStringCommand("PING");
226        return (pong != null && pong.equals("PONG"));
227    }
228
229    public void setSupplicantLogLevel(String level) {
230        doStringCommand("LOG_LEVEL " + level);
231    }
232
233    public String getFreqCapability() {
234        return doStringCommand("GET_CAPABILITY freq");
235    }
236
237    public boolean scan(int type, String freqList) {
238        if (type == SCAN_WITHOUT_CONNECTION_SETUP) {
239            if (freqList == null) return doBooleanCommand("SCAN TYPE=ONLY");
240            else return doBooleanCommand("SCAN TYPE=ONLY freq=" + freqList);
241        } else if (type == SCAN_WITH_CONNECTION_SETUP) {
242            if (freqList == null) return doBooleanCommand("SCAN");
243            else return doBooleanCommand("SCAN freq=" + freqList);
244        } else {
245            throw new IllegalArgumentException("Invalid scan type");
246        }
247    }
248
249    /* Does a graceful shutdown of supplicant. Is a common stop function for both p2p and sta.
250     *
251     * Note that underneath we use a harsh-sounding "terminate" supplicant command
252     * for a graceful stop and a mild-sounding "stop" interface
253     * to kill the process
254     */
255    public boolean stopSupplicant() {
256        return doBooleanCommand("TERMINATE");
257    }
258
259    public String listNetworks() {
260        return doStringCommand("LIST_NETWORKS");
261    }
262
263    public String listNetworks(int last_id) {
264        return doStringCommand("LIST_NETWORKS LAST_ID=" + last_id);
265    }
266
267    public int addNetwork() {
268        return doIntCommand("ADD_NETWORK");
269    }
270
271    public boolean setNetworkVariable(int netId, String name, String value) {
272        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(value)) return false;
273        return doBooleanCommand("SET_NETWORK " + netId + " " + name + " " + value);
274    }
275
276    public String getNetworkVariable(int netId, String name) {
277        if (TextUtils.isEmpty(name)) return null;
278
279        // GET_NETWORK will likely flood the logs ...
280        return doStringCommandWithoutLogging("GET_NETWORK " + netId + " " + name);
281    }
282
283    public boolean removeNetwork(int netId) {
284        return doBooleanCommand("REMOVE_NETWORK " + netId);
285    }
286
287
288    private void logDbg(String debug) {
289        long now = SystemClock.elapsedRealtimeNanos();
290        String ts = String.format("[%,d us] ", now/1000);
291        Log.e("WifiNative: ", ts+debug+ " stack:"
292                + Thread.currentThread().getStackTrace()[2].getMethodName() +" - "
293                + Thread.currentThread().getStackTrace()[3].getMethodName() +" - "
294                + Thread.currentThread().getStackTrace()[4].getMethodName() +" - "
295                + Thread.currentThread().getStackTrace()[5].getMethodName()+" - "
296                + Thread.currentThread().getStackTrace()[6].getMethodName());
297
298    }
299    public boolean enableNetwork(int netId, boolean disableOthers) {
300        if (DBG) logDbg("enableNetwork nid=" + Integer.toString(netId)
301                + " disableOthers=" + disableOthers);
302        if (disableOthers) {
303            return doBooleanCommand("SELECT_NETWORK " + netId);
304        } else {
305            return doBooleanCommand("ENABLE_NETWORK " + netId);
306        }
307    }
308
309    public boolean disableNetwork(int netId) {
310        if (DBG) logDbg("disableNetwork nid=" + Integer.toString(netId));
311        return doBooleanCommand("DISABLE_NETWORK " + netId);
312    }
313
314    public boolean selectNetwork(int netId) {
315        if (DBG) logDbg("selectNetwork nid=" + Integer.toString(netId));
316        return doBooleanCommand("SELECT_NETWORK " + netId);
317    }
318
319    public boolean reconnect() {
320        if (DBG) logDbg("RECONNECT ");
321        return doBooleanCommand("RECONNECT");
322    }
323
324    public boolean reassociate() {
325        if (DBG) logDbg("REASSOCIATE ");
326        return doBooleanCommand("REASSOCIATE");
327    }
328
329    public boolean disconnect() {
330        if (DBG) logDbg("DISCONNECT ");
331        return doBooleanCommand("DISCONNECT");
332    }
333
334    public String status() {
335        return status(false);
336    }
337
338    public String status(boolean noEvents) {
339        if (noEvents) {
340            return doStringCommand("STATUS-NO_EVENTS");
341        } else {
342            return doStringCommand("STATUS");
343        }
344    }
345
346    public String getMacAddress() {
347        //Macaddr = XX.XX.XX.XX.XX.XX
348        String ret = doStringCommand("DRIVER MACADDR");
349        if (!TextUtils.isEmpty(ret)) {
350            String[] tokens = ret.split(" = ");
351            if (tokens.length == 2) return tokens[1];
352        }
353        return null;
354    }
355
356
357
358    /**
359     * Format of results:
360     * =================
361     * id=1
362     * bssid=68:7f:74:d7:1b:6e
363     * freq=2412
364     * level=-43
365     * tsf=1344621975160944
366     * age=2623
367     * flags=[WPA2-PSK-CCMP][WPS][ESS]
368     * ssid=zubyb
369     * ====
370     *
371     * RANGE=ALL gets all scan results
372     * RANGE=ID- gets results from ID
373     * MASK=<N> see wpa_supplicant/src/common/wpa_ctrl.h for details
374     * 0                         0                        1                       0     2
375     *                           WPA_BSS_MASK_MESH_SCAN | WPA_BSS_MASK_DELIM    | WPA_BSS_MASK_WIFI_DISPLAY
376     * 0                         0                        0                       1     1   -> 9
377     * WPA_BSS_MASK_INTERNETW  | WPA_BSS_MASK_P2P_SCAN  | WPA_BSS_MASK_WPS_SCAN | WPA_BSS_MASK_SSID
378     * 1                         0                        0                       1     9   -> d
379     * WPA_BSS_MASK_FLAGS      | WPA_BSS_MASK_IE        | WPA_BSS_MASK_AGE      | WPA_BSS_MASK_TSF
380     * 1                         0                        0                       0     8
381     * WPA_BSS_MASK_LEVEL      | WPA_BSS_MASK_NOISE     | WPA_BSS_MASK_QUAL     | WPA_BSS_MASK_CAPABILITIES
382     * 0                         1                        1                       1     7
383     * WPA_BSS_MASK_BEACON_INT | WPA_BSS_MASK_FREQ      | WPA_BSS_MASK_BSSID    | WPA_BSS_MASK_ID
384     *
385     * WPA_BSS_MASK_INTERNETW adds ANQP info (ctrl_iface:4151-4176)
386     *
387     * ctrl_iface.c:wpa_supplicant_ctrl_iface_process:7884
388     *  wpa_supplicant_ctrl_iface_bss:4315
389     *  print_bss_info
390     */
391    public String scanResults(int sid) {
392        return doStringCommandWithoutLogging("BSS RANGE=" + sid + "- MASK=0x29d87");
393    }
394
395    public String doCustomCommand(String command) {
396        return doStringCommand(command);
397    }
398
399    /**
400     * Format of result:
401     * id=1016
402     * bssid=00:03:7f:40:84:10
403     * freq=2462
404     * beacon_int=200
405     * capabilities=0x0431
406     * qual=0
407     * noise=0
408     * level=-46
409     * tsf=0000002669008476
410     * age=5
411     * ie=00105143412d485332302d52322d54455354010882848b960c12182403010b0706555...
412     * flags=[WPA2-EAP-CCMP][ESS][P2P][HS20]
413     * ssid=QCA-HS20-R2-TEST
414     * p2p_device_name=
415     * p2p_config_methods=0x0SET_NE
416     * anqp_venue_name=02083d656e6757692d466920416c6c69616e63650a3239383920436f...
417     * anqp_network_auth_type=010000
418     * anqp_roaming_consortium=03506f9a05001bc504bd
419     * anqp_ip_addr_type_availability=0c
420     * anqp_nai_realm=0200300000246d61696c2e6578616d706c652e636f6d3b636973636f2...
421     * anqp_3gpp=000600040132f465
422     * anqp_domain_name=0b65786d61706c652e636f6d
423     * hs20_operator_friendly_name=11656e6757692d466920416c6c69616e63650e636869...
424     * hs20_wan_metrics=01c40900008001000000000a00
425     * hs20_connection_capability=0100000006140001061600000650000106bb010106bb0...
426     * hs20_osu_providers_list=0b5143412d4f53552d425353010901310015656e6757692d...
427     */
428    public String scanResult(String bssid) {
429        return doStringCommand("BSS " + bssid);
430    }
431
432    /**
433     * Format of command
434     * DRIVER WLS_BATCHING SET SCANFREQ=x MSCAN=r BESTN=y CHANNEL=<z, w, t> RTT=s
435     * where x is an ascii representation of an integer number of seconds between scans
436     *       r is an ascii representation of an integer number of scans per batch
437     *       y is an ascii representation of an integer number of the max AP to remember per scan
438     *       z, w, t represent a 1..n size list of channel numbers and/or 'A', 'B' values
439     *           indicating entire ranges of channels
440     *       s is an ascii representation of an integer number of highest-strength AP
441     *           for which we'd like approximate distance reported
442     *
443     * The return value is an ascii integer representing a guess of the number of scans
444     * the firmware can remember before it runs out of buffer space or -1 on error
445     */
446    public String setBatchedScanSettings(BatchedScanSettings settings) {
447        if (settings == null) {
448            return doStringCommand("DRIVER WLS_BATCHING STOP");
449        }
450        String cmd = "DRIVER WLS_BATCHING SET SCANFREQ=" + settings.scanIntervalSec;
451        cmd += " MSCAN=" + settings.maxScansPerBatch;
452        if (settings.maxApPerScan != BatchedScanSettings.UNSPECIFIED) {
453            cmd += " BESTN=" + settings.maxApPerScan;
454        }
455        if (settings.channelSet != null && !settings.channelSet.isEmpty()) {
456            cmd += " CHANNEL=<";
457            int i = 0;
458            for (String channel : settings.channelSet) {
459                cmd += (i > 0 ? "," : "") + channel;
460                ++i;
461            }
462            cmd += ">";
463        }
464        if (settings.maxApForDistance != BatchedScanSettings.UNSPECIFIED) {
465            cmd += " RTT=" + settings.maxApForDistance;
466        }
467        return doStringCommand(cmd);
468    }
469
470    public String getBatchedScanResults() {
471        return doStringCommand("DRIVER WLS_BATCHING GET");
472    }
473
474    public boolean startDriver() {
475        return doBooleanCommand("DRIVER START");
476    }
477
478    public boolean stopDriver() {
479        return doBooleanCommand("DRIVER STOP");
480    }
481
482
483    /**
484     * Start filtering out Multicast V4 packets
485     * @return {@code true} if the operation succeeded, {@code false} otherwise
486     *
487     * Multicast filtering rules work as follows:
488     *
489     * The driver can filter multicast (v4 and/or v6) and broadcast packets when in
490     * a power optimized mode (typically when screen goes off).
491     *
492     * In order to prevent the driver from filtering the multicast/broadcast packets, we have to
493     * add a DRIVER RXFILTER-ADD rule followed by DRIVER RXFILTER-START to make the rule effective
494     *
495     * DRIVER RXFILTER-ADD Num
496     *   where Num = 0 - Unicast, 1 - Broadcast, 2 - Mutil4 or 3 - Multi6
497     *
498     * and DRIVER RXFILTER-START
499     * In order to stop the usage of these rules, we do
500     *
501     * DRIVER RXFILTER-STOP
502     * DRIVER RXFILTER-REMOVE Num
503     *   where Num is as described for RXFILTER-ADD
504     *
505     * The  SETSUSPENDOPT driver command overrides the filtering rules
506     */
507    public boolean startFilteringMulticastV4Packets() {
508        return doBooleanCommand("DRIVER RXFILTER-STOP")
509            && doBooleanCommand("DRIVER RXFILTER-REMOVE 2")
510            && doBooleanCommand("DRIVER RXFILTER-START");
511    }
512
513    /**
514     * Stop filtering out Multicast V4 packets.
515     * @return {@code true} if the operation succeeded, {@code false} otherwise
516     */
517    public boolean stopFilteringMulticastV4Packets() {
518        return doBooleanCommand("DRIVER RXFILTER-STOP")
519            && doBooleanCommand("DRIVER RXFILTER-ADD 2")
520            && doBooleanCommand("DRIVER RXFILTER-START");
521    }
522
523    /**
524     * Start filtering out Multicast V6 packets
525     * @return {@code true} if the operation succeeded, {@code false} otherwise
526     */
527    public boolean startFilteringMulticastV6Packets() {
528        return doBooleanCommand("DRIVER RXFILTER-STOP")
529            && doBooleanCommand("DRIVER RXFILTER-REMOVE 3")
530            && doBooleanCommand("DRIVER RXFILTER-START");
531    }
532
533    /**
534     * Stop filtering out Multicast V6 packets.
535     * @return {@code true} if the operation succeeded, {@code false} otherwise
536     */
537    public boolean stopFilteringMulticastV6Packets() {
538        return doBooleanCommand("DRIVER RXFILTER-STOP")
539            && doBooleanCommand("DRIVER RXFILTER-ADD 3")
540            && doBooleanCommand("DRIVER RXFILTER-START");
541    }
542
543    /**
544     * Set the operational frequency band
545     * @param band One of
546     *     {@link WifiManager#WIFI_FREQUENCY_BAND_AUTO},
547     *     {@link WifiManager#WIFI_FREQUENCY_BAND_5GHZ},
548     *     {@link WifiManager#WIFI_FREQUENCY_BAND_2GHZ},
549     * @return {@code true} if the operation succeeded, {@code false} otherwise
550     */
551    public boolean setBand(int band) {
552        String bandstr;
553
554        if (band == WifiManager.WIFI_FREQUENCY_BAND_5GHZ)
555            bandstr = "5G";
556        else if (band == WifiManager.WIFI_FREQUENCY_BAND_2GHZ)
557            bandstr = "2G";
558        else
559            bandstr = "AUTO";
560        return doBooleanCommand("SET SETBAND " + bandstr);
561    }
562
563    /**
564      * Sets the bluetooth coexistence mode.
565      *
566      * @param mode One of {@link #BLUETOOTH_COEXISTENCE_MODE_DISABLED},
567      *            {@link #BLUETOOTH_COEXISTENCE_MODE_ENABLED}, or
568      *            {@link #BLUETOOTH_COEXISTENCE_MODE_SENSE}.
569      * @return Whether the mode was successfully set.
570      */
571    public boolean setBluetoothCoexistenceMode(int mode) {
572        return doBooleanCommand("DRIVER BTCOEXMODE " + mode);
573    }
574
575    /**
576     * Enable or disable Bluetooth coexistence scan mode. When this mode is on,
577     * some of the low-level scan parameters used by the driver are changed to
578     * reduce interference with A2DP streaming.
579     *
580     * @param isSet whether to enable or disable this mode
581     * @return {@code true} if the command succeeded, {@code false} otherwise.
582     */
583    public boolean setBluetoothCoexistenceScanMode(boolean setCoexScanMode) {
584        if (setCoexScanMode) {
585            return doBooleanCommand("DRIVER BTCOEXSCAN-START");
586        } else {
587            return doBooleanCommand("DRIVER BTCOEXSCAN-STOP");
588        }
589    }
590
591    public void enableSaveConfig() {
592        doBooleanCommand("SET update_config 1");
593    }
594
595    public boolean saveConfig() {
596        return doBooleanCommand("SAVE_CONFIG");
597    }
598
599    public boolean addToBlacklist(String bssid) {
600        if (TextUtils.isEmpty(bssid)) return false;
601        return doBooleanCommand("BLACKLIST " + bssid);
602    }
603
604    public boolean clearBlacklist() {
605        return doBooleanCommand("BLACKLIST clear");
606    }
607
608    public boolean setSuspendOptimizations(boolean enabled) {
609       // if (mSuspendOptEnabled == enabled) return true;
610        mSuspendOptEnabled = enabled;
611
612        Log.e("native", "do suspend " + enabled);
613        if (enabled) {
614            return doBooleanCommand("DRIVER SETSUSPENDMODE 1");
615        } else {
616            return doBooleanCommand("DRIVER SETSUSPENDMODE 0");
617        }
618    }
619
620    public boolean setCountryCode(String countryCode) {
621        if (countryCode != null)
622            return doBooleanCommand("DRIVER COUNTRY " + countryCode.toUpperCase(Locale.ROOT));
623        else
624            return doBooleanCommand("DRIVER COUNTRY");
625    }
626
627    public void enableBackgroundScan(boolean enable) {
628        if (enable) {
629            doBooleanCommand("SET pno 1");
630        } else {
631            doBooleanCommand("SET pno 0");
632        }
633    }
634
635    public void enableAutoConnect(boolean enable) {
636        if (enable) {
637            doBooleanCommand("STA_AUTOCONNECT 1");
638        } else {
639            doBooleanCommand("STA_AUTOCONNECT 0");
640        }
641    }
642
643    public void setScanInterval(int scanInterval) {
644        doBooleanCommand("SCAN_INTERVAL " + scanInterval);
645    }
646
647    public void startTdls(String macAddr, boolean enable) {
648        if (enable) {
649            doBooleanCommand("TDLS_DISCOVER " + macAddr);
650            doBooleanCommand("TDLS_SETUP " + macAddr);
651        } else {
652            doBooleanCommand("TDLS_TEARDOWN " + macAddr);
653        }
654    }
655
656    /** Example output:
657     * RSSI=-65
658     * LINKSPEED=48
659     * NOISE=9999
660     * FREQUENCY=0
661     */
662    public String signalPoll() {
663        return doStringCommandWithoutLogging("SIGNAL_POLL");
664    }
665
666    /** Example outout:
667     * TXGOOD=396
668     * TXBAD=1
669     */
670    public String pktcntPoll() {
671        return doStringCommand("PKTCNT_POLL");
672    }
673
674    public void bssFlush() {
675        doBooleanCommand("BSS_FLUSH 0");
676    }
677
678    public boolean startWpsPbc(String bssid) {
679        if (TextUtils.isEmpty(bssid)) {
680            return doBooleanCommand("WPS_PBC");
681        } else {
682            return doBooleanCommand("WPS_PBC " + bssid);
683        }
684    }
685
686    public boolean startWpsPbc(String iface, String bssid) {
687        synchronized (mLock) {
688            if (TextUtils.isEmpty(bssid)) {
689                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC");
690            } else {
691                return doBooleanCommandNative("IFNAME=" + iface + " WPS_PBC " + bssid);
692            }
693        }
694    }
695
696    public boolean startWpsPinKeypad(String pin) {
697        if (TextUtils.isEmpty(pin)) return false;
698        return doBooleanCommand("WPS_PIN any " + pin);
699    }
700
701    public boolean startWpsPinKeypad(String iface, String pin) {
702        if (TextUtils.isEmpty(pin)) return false;
703        synchronized (mLock) {
704            return doBooleanCommandNative("IFNAME=" + iface + " WPS_PIN any " + pin);
705        }
706    }
707
708
709    public String startWpsPinDisplay(String bssid) {
710        if (TextUtils.isEmpty(bssid)) {
711            return doStringCommand("WPS_PIN any");
712        } else {
713            return doStringCommand("WPS_PIN " + bssid);
714        }
715    }
716
717    public String startWpsPinDisplay(String iface, String bssid) {
718        synchronized (mLock) {
719            if (TextUtils.isEmpty(bssid)) {
720                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN any");
721            } else {
722                return doStringCommandNative("IFNAME=" + iface + " WPS_PIN " + bssid);
723            }
724        }
725    }
726
727    public boolean setExternalSim(boolean external) {
728        synchronized (mLock) {
729            String value = external ? "1" : "0";
730            Log.d(TAG, "Setting external_sim to " + value);
731            return doBooleanCommand("SET external_sim " + value);
732        }
733    }
734
735    public boolean simAuthResponse(int id, String response) {
736        synchronized (mLock) {
737            return doBooleanCommand("CTRL-RSP-SIM-" + id + ":GSM-AUTH" + response);
738        }
739    }
740
741    public boolean simIdentityResponse(int id, String response) {
742        synchronized (mLock) {
743            return doBooleanCommand("CTRL-RSP-IDENTITY-" + id + ":" + response);
744        }
745    }
746
747    /* Configures an access point connection */
748    public boolean startWpsRegistrar(String bssid, String pin) {
749        if (TextUtils.isEmpty(bssid) || TextUtils.isEmpty(pin)) return false;
750        return doBooleanCommand("WPS_REG " + bssid + " " + pin);
751    }
752
753    public boolean cancelWps() {
754        return doBooleanCommand("WPS_CANCEL");
755    }
756
757    public boolean setPersistentReconnect(boolean enabled) {
758        int value = (enabled == true) ? 1 : 0;
759        return doBooleanCommand("SET persistent_reconnect " + value);
760    }
761
762    public boolean setDeviceName(String name) {
763        return doBooleanCommand("SET device_name " + name);
764    }
765
766    public boolean setDeviceType(String type) {
767        return doBooleanCommand("SET device_type " + type);
768    }
769
770    public boolean setConfigMethods(String cfg) {
771        return doBooleanCommand("SET config_methods " + cfg);
772    }
773
774    public boolean setManufacturer(String value) {
775        return doBooleanCommand("SET manufacturer " + value);
776    }
777
778    public boolean setModelName(String value) {
779        return doBooleanCommand("SET model_name " + value);
780    }
781
782    public boolean setModelNumber(String value) {
783        return doBooleanCommand("SET model_number " + value);
784    }
785
786    public boolean setSerialNumber(String value) {
787        return doBooleanCommand("SET serial_number " + value);
788    }
789
790    public boolean setP2pSsidPostfix(String postfix) {
791        return doBooleanCommand("SET p2p_ssid_postfix " + postfix);
792    }
793
794    public boolean setP2pGroupIdle(String iface, int time) {
795        synchronized (mLock) {
796            return doBooleanCommandNative("IFNAME=" + iface + " SET p2p_group_idle " + time);
797        }
798    }
799
800    public void setPowerSave(boolean enabled) {
801        if (enabled) {
802            doBooleanCommand("SET ps 1");
803        } else {
804            doBooleanCommand("SET ps 0");
805        }
806    }
807
808    public boolean setP2pPowerSave(String iface, boolean enabled) {
809        synchronized (mLock) {
810            if (enabled) {
811                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 1");
812            } else {
813                return doBooleanCommandNative("IFNAME=" + iface + " P2P_SET ps 0");
814            }
815        }
816    }
817
818    public boolean setWfdEnable(boolean enable) {
819        return doBooleanCommand("SET wifi_display " + (enable ? "1" : "0"));
820    }
821
822    public boolean setWfdDeviceInfo(String hex) {
823        return doBooleanCommand("WFD_SUBELEM_SET 0 " + hex);
824    }
825
826    /**
827     * "sta" prioritizes STA connection over P2P and "p2p" prioritizes
828     * P2P connection over STA
829     */
830    public boolean setConcurrencyPriority(String s) {
831        return doBooleanCommand("P2P_SET conc_pref " + s);
832    }
833
834    public boolean p2pFind() {
835        return doBooleanCommand("P2P_FIND");
836    }
837
838    public boolean p2pFind(int timeout) {
839        if (timeout <= 0) {
840            return p2pFind();
841        }
842        return doBooleanCommand("P2P_FIND " + timeout);
843    }
844
845    public boolean p2pStopFind() {
846       return doBooleanCommand("P2P_STOP_FIND");
847    }
848
849    public boolean p2pListen() {
850        return doBooleanCommand("P2P_LISTEN");
851    }
852
853    public boolean p2pListen(int timeout) {
854        if (timeout <= 0) {
855            return p2pListen();
856        }
857        return doBooleanCommand("P2P_LISTEN " + timeout);
858    }
859
860    public boolean p2pExtListen(boolean enable, int period, int interval) {
861        if (enable && interval < period) {
862            return false;
863        }
864        return doBooleanCommand("P2P_EXT_LISTEN"
865                    + (enable ? (" " + period + " " + interval) : ""));
866    }
867
868    public boolean p2pSetChannel(int lc, int oc) {
869        if (DBG) Log.d(mTAG, "p2pSetChannel: lc="+lc+", oc="+oc);
870
871        if (lc >=1 && lc <= 11) {
872            if (!doBooleanCommand("P2P_SET listen_channel " + lc)) {
873                return false;
874            }
875        } else if (lc != 0) {
876            return false;
877        }
878
879        if (oc >= 1 && oc <= 165 ) {
880            int freq = (oc <= 14 ? 2407 : 5000) + oc * 5;
881            return doBooleanCommand("P2P_SET disallow_freq 1000-"
882                    + (freq - 5) + "," + (freq + 5) + "-6000");
883        } else if (oc == 0) {
884            /* oc==0 disables "P2P_SET disallow_freq" (enables all freqs) */
885            return doBooleanCommand("P2P_SET disallow_freq \"\"");
886        }
887
888        return false;
889    }
890
891    public boolean p2pFlush() {
892        return doBooleanCommand("P2P_FLUSH");
893    }
894
895    /* p2p_connect <peer device address> <pbc|pin|PIN#> [label|display|keypad]
896        [persistent] [join|auth] [go_intent=<0..15>] [freq=<in MHz>] */
897    public String p2pConnect(WifiP2pConfig config, boolean joinExistingGroup) {
898        if (config == null) return null;
899        List<String> args = new ArrayList<String>();
900        WpsInfo wps = config.wps;
901        args.add(config.deviceAddress);
902
903        switch (wps.setup) {
904            case WpsInfo.PBC:
905                args.add("pbc");
906                break;
907            case WpsInfo.DISPLAY:
908                if (TextUtils.isEmpty(wps.pin)) {
909                    args.add("pin");
910                } else {
911                    args.add(wps.pin);
912                }
913                args.add("display");
914                break;
915            case WpsInfo.KEYPAD:
916                args.add(wps.pin);
917                args.add("keypad");
918                break;
919            case WpsInfo.LABEL:
920                args.add(wps.pin);
921                args.add("label");
922            default:
923                break;
924        }
925
926        if (config.netId == WifiP2pGroup.PERSISTENT_NET_ID) {
927            args.add("persistent");
928        }
929
930        if (joinExistingGroup) {
931            args.add("join");
932        } else {
933            //TODO: This can be adapted based on device plugged in state and
934            //device battery state
935            int groupOwnerIntent = config.groupOwnerIntent;
936            if (groupOwnerIntent < 0 || groupOwnerIntent > 15) {
937                groupOwnerIntent = DEFAULT_GROUP_OWNER_INTENT;
938            }
939            args.add("go_intent=" + groupOwnerIntent);
940        }
941
942        String command = "P2P_CONNECT ";
943        for (String s : args) command += s + " ";
944
945        return doStringCommand(command);
946    }
947
948    public boolean p2pCancelConnect() {
949        return doBooleanCommand("P2P_CANCEL");
950    }
951
952    public boolean p2pProvisionDiscovery(WifiP2pConfig config) {
953        if (config == null) return false;
954
955        switch (config.wps.setup) {
956            case WpsInfo.PBC:
957                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " pbc");
958            case WpsInfo.DISPLAY:
959                //We are doing display, so provision discovery is keypad
960                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " keypad");
961            case WpsInfo.KEYPAD:
962                //We are doing keypad, so provision discovery is display
963                return doBooleanCommand("P2P_PROV_DISC " + config.deviceAddress + " display");
964            default:
965                break;
966        }
967        return false;
968    }
969
970    public boolean p2pGroupAdd(boolean persistent) {
971        if (persistent) {
972            return doBooleanCommand("P2P_GROUP_ADD persistent");
973        }
974        return doBooleanCommand("P2P_GROUP_ADD");
975    }
976
977    public boolean p2pGroupAdd(int netId) {
978        return doBooleanCommand("P2P_GROUP_ADD persistent=" + netId);
979    }
980
981    public boolean p2pGroupRemove(String iface) {
982        if (TextUtils.isEmpty(iface)) return false;
983        synchronized (mLock) {
984            return doBooleanCommandNative("IFNAME=" + iface + " P2P_GROUP_REMOVE " + iface);
985        }
986    }
987
988    public boolean p2pReject(String deviceAddress) {
989        return doBooleanCommand("P2P_REJECT " + deviceAddress);
990    }
991
992    /* Invite a peer to a group */
993    public boolean p2pInvite(WifiP2pGroup group, String deviceAddress) {
994        if (TextUtils.isEmpty(deviceAddress)) return false;
995
996        if (group == null) {
997            return doBooleanCommand("P2P_INVITE peer=" + deviceAddress);
998        } else {
999            return doBooleanCommand("P2P_INVITE group=" + group.getInterface()
1000                    + " peer=" + deviceAddress + " go_dev_addr=" + group.getOwner().deviceAddress);
1001        }
1002    }
1003
1004    /* Reinvoke a persistent connection */
1005    public boolean p2pReinvoke(int netId, String deviceAddress) {
1006        if (TextUtils.isEmpty(deviceAddress) || netId < 0) return false;
1007
1008        return doBooleanCommand("P2P_INVITE persistent=" + netId + " peer=" + deviceAddress);
1009    }
1010
1011    public String p2pGetSsid(String deviceAddress) {
1012        return p2pGetParam(deviceAddress, "oper_ssid");
1013    }
1014
1015    public String p2pGetDeviceAddress() {
1016
1017        Log.d(TAG, "p2pGetDeviceAddress");
1018
1019        String status = null;
1020
1021        /* Explicitly calling the API without IFNAME= prefix to take care of the devices that
1022        don't have p2p0 interface. Supplicant seems to be returning the correct address anyway. */
1023
1024        synchronized (mLock) {
1025            status = doStringCommandNative("STATUS");
1026        }
1027
1028        String result = "";
1029        if (status != null) {
1030            String[] tokens = status.split("\n");
1031            for (String token : tokens) {
1032                if (token.startsWith("p2p_device_address=")) {
1033                    String[] nameValue = token.split("=");
1034                    if (nameValue.length != 2)
1035                        break;
1036                    result = nameValue[1];
1037                }
1038            }
1039        }
1040
1041        Log.d(TAG, "p2pGetDeviceAddress returning " + result);
1042        return result;
1043    }
1044
1045    public int getGroupCapability(String deviceAddress) {
1046        int gc = 0;
1047        if (TextUtils.isEmpty(deviceAddress)) return gc;
1048        String peerInfo = p2pPeer(deviceAddress);
1049        if (TextUtils.isEmpty(peerInfo)) return gc;
1050
1051        String[] tokens = peerInfo.split("\n");
1052        for (String token : tokens) {
1053            if (token.startsWith("group_capab=")) {
1054                String[] nameValue = token.split("=");
1055                if (nameValue.length != 2) break;
1056                try {
1057                    return Integer.decode(nameValue[1]);
1058                } catch(NumberFormatException e) {
1059                    return gc;
1060                }
1061            }
1062        }
1063        return gc;
1064    }
1065
1066    public String p2pPeer(String deviceAddress) {
1067        return doStringCommand("P2P_PEER " + deviceAddress);
1068    }
1069
1070    private String p2pGetParam(String deviceAddress, String key) {
1071        if (deviceAddress == null) return null;
1072
1073        String peerInfo = p2pPeer(deviceAddress);
1074        if (peerInfo == null) return null;
1075        String[] tokens= peerInfo.split("\n");
1076
1077        key += "=";
1078        for (String token : tokens) {
1079            if (token.startsWith(key)) {
1080                String[] nameValue = token.split("=");
1081                if (nameValue.length != 2) break;
1082                return nameValue[1];
1083            }
1084        }
1085        return null;
1086    }
1087
1088    public boolean p2pServiceAdd(WifiP2pServiceInfo servInfo) {
1089        /*
1090         * P2P_SERVICE_ADD bonjour <query hexdump> <RDATA hexdump>
1091         * P2P_SERVICE_ADD upnp <version hex> <service>
1092         *
1093         * e.g)
1094         * [Bonjour]
1095         * # IP Printing over TCP (PTR) (RDATA=MyPrinter._ipp._tcp.local.)
1096         * P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027
1097         * # IP Printing over TCP (TXT) (RDATA=txtvers=1,pdl=application/postscript)
1098         * P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001
1099         *  09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074
1100         *
1101         * [UPnP]
1102         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012
1103         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice
1104         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp
1105         * -org:device:InternetGatewayDevice:1
1106         * P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9322-123456789012::urn:schemas-upnp
1107         * -org:service:ContentDirectory:2
1108         */
1109        for (String s : servInfo.getSupplicantQueryList()) {
1110            String command = "P2P_SERVICE_ADD";
1111            command += (" " + s);
1112            if (!doBooleanCommand(command)) {
1113                return false;
1114            }
1115        }
1116        return true;
1117    }
1118
1119    public boolean p2pServiceDel(WifiP2pServiceInfo servInfo) {
1120        /*
1121         * P2P_SERVICE_DEL bonjour <query hexdump>
1122         * P2P_SERVICE_DEL upnp <version hex> <service>
1123         */
1124        for (String s : servInfo.getSupplicantQueryList()) {
1125            String command = "P2P_SERVICE_DEL ";
1126
1127            String[] data = s.split(" ");
1128            if (data.length < 2) {
1129                return false;
1130            }
1131            if ("upnp".equals(data[0])) {
1132                command += s;
1133            } else if ("bonjour".equals(data[0])) {
1134                command += data[0];
1135                command += (" " + data[1]);
1136            } else {
1137                return false;
1138            }
1139            if (!doBooleanCommand(command)) {
1140                return false;
1141            }
1142        }
1143        return true;
1144    }
1145
1146    public boolean p2pServiceFlush() {
1147        return doBooleanCommand("P2P_SERVICE_FLUSH");
1148    }
1149
1150    public String p2pServDiscReq(String addr, String query) {
1151        String command = "P2P_SERV_DISC_REQ";
1152        command += (" " + addr);
1153        command += (" " + query);
1154
1155        return doStringCommand(command);
1156    }
1157
1158    public boolean p2pServDiscCancelReq(String id) {
1159        return doBooleanCommand("P2P_SERV_DISC_CANCEL_REQ " + id);
1160    }
1161
1162    /* Set the current mode of miracast operation.
1163     *  0 = disabled
1164     *  1 = operating as source
1165     *  2 = operating as sink
1166     */
1167    public void setMiracastMode(int mode) {
1168        // Note: optional feature on the driver. It is ok for this to fail.
1169        doBooleanCommand("DRIVER MIRACAST " + mode);
1170    }
1171
1172    public boolean fetchAnqp(String bssid, String subtypes) {
1173        return doBooleanCommand("ANQP_GET " + bssid + " " + subtypes);
1174    }
1175
1176    /* WIFI HAL support */
1177
1178    private static final String TAG = "WifiNative-HAL";
1179    private static long sWifiHalHandle = 0;  /* used by JNI to save wifi_handle */
1180    private static long[] sWifiIfaceHandles = null;  /* used by JNI to save interface handles */
1181    private static int sWlan0Index = -1;
1182    private static int sP2p0Index = -1;
1183
1184    private static boolean sHalIsStarted = false;
1185    private static boolean sHalFailed = false;
1186
1187    private static native boolean startHalNative();
1188    private static native void stopHalNative();
1189    private static native void waitForHalEventNative();
1190
1191    private static class MonitorThread extends Thread {
1192        public void run() {
1193            Log.i(TAG, "Waiting for HAL events mWifiHalHandle=" + Long.toString(sWifiHalHandle));
1194            waitForHalEventNative();
1195        }
1196    }
1197
1198    synchronized public static boolean startHal() {
1199        Log.i(TAG, "startHal");
1200        synchronized (mLock) {
1201            if (sHalFailed)
1202                return false;
1203            if (startHalNative() && (getInterfaces() != 0) && (sWlan0Index != -1)) {
1204                new MonitorThread().start();
1205                sHalIsStarted = true;
1206                return true;
1207            } else {
1208                Log.i(TAG, "Could not start hal");
1209                sHalIsStarted = false;
1210                sHalFailed = true;
1211                return false;
1212            }
1213        }
1214    }
1215
1216    synchronized public static void stopHal() {
1217        stopHalNative();
1218    }
1219
1220    private static native int getInterfacesNative();
1221
1222    synchronized public static int getInterfaces() {
1223        synchronized (mLock) {
1224            if (sWifiIfaceHandles == null) {
1225                int num = getInterfacesNative();
1226                int wifi_num = 0;
1227                for (int i = 0; i < num; i++) {
1228                    String name = getInterfaceNameNative(i);
1229                    Log.i(TAG, "interface[" + i + "] = " + name);
1230                    if (name.equals("wlan0")) {
1231                        sWlan0Index = i;
1232                        wifi_num++;
1233                    } else if (name.equals("p2p0")) {
1234                        sP2p0Index = i;
1235                        wifi_num++;
1236                    }
1237                }
1238                return wifi_num;
1239            } else {
1240                return sWifiIfaceHandles.length;
1241            }
1242        }
1243    }
1244
1245    private static native String getInterfaceNameNative(int index);
1246    synchronized public static String getInterfaceName(int index) {
1247        return getInterfaceNameNative(index);
1248    }
1249
1250    public static class ScanCapabilities {
1251        public int  max_scan_cache_size;                 // in number of scan results??
1252        public int  max_scan_buckets;
1253        public int  max_ap_cache_per_scan;
1254        public int  max_rssi_sample_size;
1255        public int  max_scan_reporting_threshold;        // in number of scan results??
1256        public int  max_hotlist_bssids;
1257        public int  max_significant_wifi_change_aps;
1258    }
1259
1260    public static boolean getScanCapabilities(ScanCapabilities capabilities) {
1261        return getScanCapabilitiesNative(sWlan0Index, capabilities);
1262    }
1263
1264    private static native boolean getScanCapabilitiesNative(
1265            int iface, ScanCapabilities capabilities);
1266
1267    private static native boolean startScanNative(int iface, int id, ScanSettings settings);
1268    private static native boolean stopScanNative(int iface, int id);
1269    private static native WifiScanner.ScanData[] getScanResultsNative(int iface, boolean flush);
1270    private static native WifiLinkLayerStats getWifiLinkLayerStatsNative(int iface);
1271
1272    public static class ChannelSettings {
1273        int frequency;
1274        int dwell_time_ms;
1275        boolean passive;
1276    }
1277
1278    public static class BucketSettings {
1279        int bucket;
1280        int band;
1281        int period_ms;
1282        int report_events;
1283        int num_channels;
1284        ChannelSettings channels[];
1285    }
1286
1287    public static class ScanSettings {
1288        int base_period_ms;
1289        int max_ap_per_scan;
1290        int report_threshold_percent;
1291        int report_threshold_num_scans;
1292        int num_buckets;
1293        BucketSettings buckets[];
1294    }
1295
1296    public static interface ScanEventHandler {
1297        void onScanResultsAvailable();
1298        void onFullScanResult(ScanResult fullScanResult);
1299        void onScanStatus();
1300        void onScanPaused(WifiScanner.ScanData[] data);
1301        void onScanRestarted();
1302    }
1303
1304    synchronized static void onScanResultsAvailable(int id) {
1305        if (sScanEventHandler  != null) {
1306            sScanEventHandler.onScanResultsAvailable();
1307        }
1308    }
1309
1310    /* scan status, keep these values in sync with gscan.h */
1311    private static int WIFI_SCAN_BUFFER_FULL = 0;
1312    private static int WIFI_SCAN_COMPLETE = 1;
1313
1314    synchronized static void onScanStatus(int status) {
1315        Log.i(TAG, "Got a scan status changed event, status = " + status);
1316
1317        if (status == WIFI_SCAN_BUFFER_FULL) {
1318            /* we have a separate event to take care of this */
1319        } else if (status == WIFI_SCAN_COMPLETE) {
1320            if (sScanEventHandler  != null) {
1321                sScanEventHandler.onScanStatus();
1322            }
1323        }
1324    }
1325
1326    synchronized static void onFullScanResult(int id, ScanResult result, byte bytes[]) {
1327        if (DBG) Log.i(TAG, "Got a full scan results event, ssid = " + result.SSID + ", " +
1328                "num = " + bytes.length);
1329
1330        if (sScanEventHandler == null) {
1331            return;
1332        }
1333
1334        int num = 0;
1335        for (int i = 0; i < bytes.length; ) {
1336            int type  = bytes[i] & 0xFF;
1337            int len = bytes[i + 1] & 0xFF;
1338
1339            if (i + len + 2 > bytes.length) {
1340                Log.w(TAG, "bad length " + len + " of IE " + type + " from " + result.BSSID);
1341                Log.w(TAG, "ignoring the rest of the IEs");
1342                break;
1343            }
1344            num++;
1345            i += len + 2;
1346            if (DBG) Log.i(TAG, "bytes[" + i + "] = [" + type + ", " + len + "]" + ", " +
1347                    "next = " + i);
1348        }
1349
1350        int secondChanelOffset = 0;
1351        byte channelMode = 0;
1352        byte centerFreqIndex1 = 0;
1353        byte centerFreqIndex2 = 0;
1354        result.is80211McRTTResponder = false;
1355
1356        ScanResult.InformationElement elements[] = new ScanResult.InformationElement[num];
1357        for (int i = 0, index = 0; i < num; i++) {
1358            int type  = bytes[index] & 0xFF;
1359            int len = bytes[index + 1] & 0xFF;
1360            if (DBG) Log.i(TAG, "index = " + index + ", type = " + type + ", len = " + len);
1361            ScanResult.InformationElement elem = new ScanResult.InformationElement();
1362            elem.id = type;
1363            elem.bytes = new byte[len];
1364            for (int j = 0; j < len; j++) {
1365                elem.bytes[j] = bytes[index + j + 2];
1366            }
1367            elements[i] = elem;
1368            int inforStart = index + 2;
1369            index += (len + 2);
1370
1371            if(type == EID_HT_OPERATION) {
1372                secondChanelOffset = bytes[inforStart + 1] & 0x3;
1373            } else if(type == EID_VHT_OPERATION) {
1374                channelMode = bytes[inforStart];
1375                centerFreqIndex1 = bytes[inforStart + 1];
1376                centerFreqIndex2 = bytes[inforStart + 2];
1377            } else if (type == EID_EXTENDED_CAPS) {
1378                 int tempIndex = RTT_RESP_ENABLE_BIT / 8;
1379                 byte offset = RTT_RESP_ENABLE_BIT % 8;
1380
1381                 if(len < tempIndex + 1) {
1382                     result.is80211McRTTResponder = false;
1383                 } else {
1384                     if ((bytes[inforStart + tempIndex] & ((byte)0x1 << offset)) != 0) {
1385                         result.is80211McRTTResponder = true;
1386                     } else {
1387                         result.is80211McRTTResponder = false;
1388                     }
1389                 }
1390            }
1391        }
1392        //handle RTT related information
1393        if (channelMode != 0) {
1394            // 80 or 160 MHz
1395            result.channelWidth = channelMode + 1;
1396
1397            //convert channel index to frequency in MHz, channel 36 is 5180MHz
1398            result.centerFreq0 = (centerFreqIndex1 - 36) * 5 + 5180;
1399
1400            if(channelMode > 1) { //160MHz
1401                result.centerFreq1 = (centerFreqIndex2 - 36) * 5 + 5180;
1402            } else {
1403                result.centerFreq1 = 0;
1404            }
1405        } else {
1406            //20 or 40 MHz
1407            if (secondChanelOffset != 0) {//40MHz
1408                result.channelWidth = 1;
1409                if (secondChanelOffset == 1) {
1410                    result.centerFreq0 = result.frequency + 20;
1411                } else if (secondChanelOffset == 3) {
1412                    result.centerFreq0 = result.frequency - 20;
1413                } else {
1414                    result.centerFreq0 = 0;
1415                    Log.e(TAG, ": Error on secondChanelOffset");
1416                }
1417            } else {
1418                result.centerFreq0  = 0;
1419                result.centerFreq1  = 0;
1420            }
1421            result.centerFreq1  = 0;
1422        }
1423        if(DBG) {
1424            Log.d(TAG, ":SSID: " + result.SSID + "ChannelWidth is: " + result.channelWidth +
1425                    " PrimaryFreq: " + result.frequency +" mCenterfreq0: " + result.centerFreq0 +
1426                    " mCenterfreq1: " + result.centerFreq1 + (result.is80211McRTTResponder ?
1427                    "Support RTT reponder: " : "Do not support RTT responder"));
1428        }
1429
1430        result.informationElements = elements;
1431        sScanEventHandler.onFullScanResult(result);
1432    }
1433
1434    private static int sScanCmdId = 0;
1435    private static ScanEventHandler sScanEventHandler;
1436    private static ScanSettings sScanSettings;
1437
1438    synchronized public static boolean startScan(
1439            ScanSettings settings, ScanEventHandler eventHandler) {
1440        synchronized (mLock) {
1441
1442            if (sScanCmdId != 0) {
1443                stopScan();
1444            } else if (sScanSettings != null || sScanEventHandler != null) {
1445                /* current scan is paused; no need to stop it */
1446            }
1447
1448            sScanCmdId = getNewCmdIdLocked();
1449
1450            sScanSettings = settings;
1451            sScanEventHandler = eventHandler;
1452
1453            if (startScanNative(sWlan0Index, sScanCmdId, settings) == false) {
1454                sScanEventHandler = null;
1455                sScanSettings = null;
1456                sScanCmdId = 0;
1457                return false;
1458            }
1459
1460            return true;
1461        }
1462    }
1463
1464    synchronized public static void stopScan() {
1465        synchronized (mLock) {
1466            stopScanNative(sWlan0Index, sScanCmdId);
1467            sScanSettings = null;
1468            sScanEventHandler = null;
1469            sScanCmdId = 0;
1470        }
1471    }
1472
1473    synchronized public static void pauseScan() {
1474        synchronized (mLock) {
1475            if (sScanCmdId != 0 && sScanSettings != null && sScanEventHandler != null) {
1476                Log.d(TAG, "Pausing scan");
1477                WifiScanner.ScanData scanData[] = getScanResultsNative(sWlan0Index, true);
1478                stopScanNative(sWlan0Index, sScanCmdId);
1479                sScanCmdId = 0;
1480                sScanEventHandler.onScanPaused(scanData);
1481            }
1482        }
1483    }
1484
1485    synchronized public static void restartScan() {
1486        synchronized (mLock) {
1487            if (sScanCmdId == 0 && sScanSettings != null && sScanEventHandler != null) {
1488                Log.d(TAG, "Restarting scan");
1489                ScanEventHandler handler = sScanEventHandler;
1490                ScanSettings settings = sScanSettings;
1491                if (startScan(sScanSettings, sScanEventHandler)) {
1492                    sScanEventHandler.onScanRestarted();
1493                } else {
1494                    /* we are still paused; don't change state */
1495                    sScanEventHandler = handler;
1496                    sScanSettings = settings;
1497                }
1498            }
1499        }
1500    }
1501
1502    synchronized public static WifiScanner.ScanData[] getScanResults(boolean flush) {
1503        synchronized (mLock) {
1504            return getScanResultsNative(sWlan0Index, flush);
1505        }
1506    }
1507
1508    public static interface HotlistEventHandler {
1509        void onHotlistApFound (ScanResult[] result);
1510        void onHotlistApLost  (ScanResult[] result);
1511    }
1512
1513    private static int sHotlistCmdId = 0;
1514    private static HotlistEventHandler sHotlistEventHandler;
1515
1516    private native static boolean setHotlistNative(int iface, int id,
1517            WifiScanner.HotlistSettings settings);
1518    private native static boolean resetHotlistNative(int iface, int id);
1519
1520    synchronized public static boolean setHotlist(WifiScanner.HotlistSettings settings,
1521                                    HotlistEventHandler eventHandler) {
1522        synchronized (mLock) {
1523            if (sHotlistCmdId != 0) {
1524                return false;
1525            } else {
1526                sHotlistCmdId = getNewCmdIdLocked();
1527            }
1528
1529            sHotlistEventHandler = eventHandler;
1530            if (setHotlistNative(sWlan0Index, sScanCmdId, settings) == false) {
1531                sHotlistEventHandler = null;
1532                return false;
1533            }
1534
1535            return true;
1536        }
1537    }
1538
1539    synchronized public static void resetHotlist() {
1540        synchronized (mLock) {
1541            if (sHotlistCmdId != 0) {
1542                resetHotlistNative(sWlan0Index, sHotlistCmdId);
1543                sHotlistCmdId = 0;
1544                sHotlistEventHandler = null;
1545            }
1546        }
1547    }
1548
1549    synchronized public static void onHotlistApFound(int id, ScanResult[] results) {
1550        synchronized (mLock) {
1551            if (sHotlistCmdId != 0) {
1552                sHotlistEventHandler.onHotlistApFound(results);
1553            } else {
1554                /* this can happen because of race conditions */
1555                Log.d(TAG, "Ignoring hotlist AP found event");
1556            }
1557        }
1558    }
1559
1560    synchronized public static void onHotlistApLost(int id, ScanResult[] results) {
1561        synchronized (mLock) {
1562            if (sHotlistCmdId != 0) {
1563                sHotlistEventHandler.onHotlistApLost(results);
1564            } else {
1565                /* this can happen because of race conditions */
1566                Log.d(TAG, "Ignoring hotlist AP lost event");
1567            }
1568        }
1569    }
1570
1571    public static interface SignificantWifiChangeEventHandler {
1572        void onChangesFound(ScanResult[] result);
1573    }
1574
1575    private static SignificantWifiChangeEventHandler sSignificantWifiChangeHandler;
1576    private static int sSignificantWifiChangeCmdId;
1577
1578    private static native boolean trackSignificantWifiChangeNative(
1579            int iface, int id, WifiScanner.WifiChangeSettings settings);
1580    private static native boolean untrackSignificantWifiChangeNative(int iface, int id);
1581
1582    synchronized public static boolean trackSignificantWifiChange(
1583            WifiScanner.WifiChangeSettings settings, SignificantWifiChangeEventHandler handler) {
1584        synchronized (mLock) {
1585            if (sSignificantWifiChangeCmdId != 0) {
1586                return false;
1587            } else {
1588                sSignificantWifiChangeCmdId = getNewCmdIdLocked();
1589            }
1590
1591            sSignificantWifiChangeHandler = handler;
1592            if (trackSignificantWifiChangeNative(sWlan0Index, sScanCmdId, settings) == false) {
1593                sSignificantWifiChangeHandler = null;
1594                return false;
1595            }
1596
1597            return true;
1598        }
1599    }
1600
1601    synchronized static void untrackSignificantWifiChange() {
1602        synchronized (mLock) {
1603            if (sSignificantWifiChangeCmdId != 0) {
1604                untrackSignificantWifiChangeNative(sWlan0Index, sSignificantWifiChangeCmdId);
1605                sSignificantWifiChangeCmdId = 0;
1606                sSignificantWifiChangeHandler = null;
1607            }
1608        }
1609    }
1610
1611    synchronized static void onSignificantWifiChange(int id, ScanResult[] results) {
1612        synchronized (mLock) {
1613            if (sSignificantWifiChangeCmdId != 0) {
1614                sSignificantWifiChangeHandler.onChangesFound(results);
1615            } else {
1616                /* this can happen because of race conditions */
1617                Log.d(TAG, "Ignoring significant wifi change");
1618            }
1619        }
1620    }
1621
1622    synchronized public static WifiLinkLayerStats getWifiLinkLayerStats(String iface) {
1623        // TODO: use correct iface name to Index translation
1624        if (iface == null) return null;
1625        synchronized (mLock) {
1626            if (!sHalIsStarted)
1627                startHal();
1628            if (sHalIsStarted)
1629                return getWifiLinkLayerStatsNative(sWlan0Index);
1630        }
1631        return null;
1632    }
1633
1634    /*
1635     * NFC-related calls
1636     */
1637    public String getNfcWpsConfigurationToken(int netId) {
1638        return doStringCommand("WPS_NFC_CONFIG_TOKEN WPS " + netId);
1639    }
1640
1641    public String getNfcHandoverRequest() {
1642        return doStringCommand("NFC_GET_HANDOVER_REQ NDEF P2P-CR");
1643    }
1644
1645    public String getNfcHandoverSelect() {
1646        return doStringCommand("NFC_GET_HANDOVER_SEL NDEF P2P-CR");
1647    }
1648
1649    public boolean initiatorReportNfcHandover(String selectMessage) {
1650        return doBooleanCommand("NFC_REPORT_HANDOVER INIT P2P 00 " + selectMessage);
1651    }
1652
1653    public boolean responderReportNfcHandover(String requestMessage) {
1654        return doBooleanCommand("NFC_REPORT_HANDOVER RESP P2P " + requestMessage + " 00");
1655    }
1656
1657    public static native int getSupportedFeatureSetNative(int iface);
1658    synchronized public static int getSupportedFeatureSet() {
1659        return getSupportedFeatureSetNative(sWlan0Index);
1660    }
1661
1662    /* Rtt related commands/events */
1663    public static interface RttEventHandler {
1664        void onRttResults(RttManager.RttResult[] result);
1665    }
1666
1667    private static RttEventHandler sRttEventHandler;
1668    private static int sRttCmdId;
1669
1670    synchronized private static void onRttResults(int id, RttManager.RttResult[] results) {
1671        if (id == sRttCmdId) {
1672            Log.d(TAG, "Received " + results.length + " rtt results");
1673            sRttEventHandler.onRttResults(results);
1674            sRttCmdId = 0;
1675        } else {
1676            Log.d(TAG, "Received event for unknown cmd = " + id + ", current id = " + sRttCmdId);
1677        }
1678    }
1679
1680    private static native boolean requestRangeNative(
1681            int iface, int id, RttManager.RttParams[] params);
1682    private static native boolean cancelRangeRequestNative(
1683            int iface, int id, RttManager.RttParams[] params);
1684
1685    synchronized public static boolean requestRtt(
1686            RttManager.RttParams[] params, RttEventHandler handler) {
1687        synchronized (mLock) {
1688            if (sRttCmdId != 0) {
1689                return false;
1690            } else {
1691                sRttCmdId = getNewCmdIdLocked();
1692            }
1693            sRttEventHandler = handler;
1694            return requestRangeNative(sWlan0Index, sRttCmdId, params);
1695        }
1696    }
1697
1698    synchronized public static boolean cancelRtt(RttManager.RttParams[] params) {
1699        synchronized(mLock) {
1700            if (sRttCmdId == 0) {
1701                return false;
1702            }
1703
1704            if (cancelRangeRequestNative(sWlan0Index, sRttCmdId, params)) {
1705                sRttEventHandler = null;
1706                return true;
1707            } else {
1708                return false;
1709            }
1710        }
1711    }
1712
1713    private static native boolean setScanningMacOuiNative(int iface, byte[] oui);
1714
1715    synchronized public static boolean setScanningMacOui(byte[] oui) {
1716        synchronized (mLock) {
1717            if (startHal()) {
1718                return setScanningMacOuiNative(sWlan0Index, oui);
1719            } else {
1720                return false;
1721            }
1722        }
1723    }
1724
1725    private static native int[] getChannelsForBandNative(
1726            int iface, int band);
1727
1728    synchronized public static int [] getChannelsForBand(int band) {
1729        synchronized (mLock) {
1730            if (startHal()) {
1731                return getChannelsForBandNative(sWlan0Index, band);
1732            } else {
1733                return null;
1734            }
1735        }
1736    }
1737
1738
1739    private static native boolean setDfsFlagNative(int iface, boolean dfsOn);
1740    synchronized public static boolean setDfsFlag(boolean dfsOn) {
1741        synchronized (mLock) {
1742            if (startHal()) {
1743                return setDfsFlagNative(sWlan0Index, dfsOn);
1744            } else {
1745                return false;
1746            }
1747        }
1748    }
1749
1750    private static native boolean toggleInterfaceNative(int on);
1751    synchronized public static boolean toggleInterface(int on) {
1752        synchronized (mLock) {
1753            if (startHal()) {
1754                return toggleInterfaceNative(0);
1755            } else {
1756
1757                return false;
1758            }
1759        }
1760    }
1761
1762    private static native RttManager.RttCapabilities getRttCapabilitiesNative(int iface);
1763    synchronized public static RttManager.RttCapabilities getRttCapabilities() {
1764        synchronized (mLock) {
1765            if (startHal()) {
1766                return getRttCapabilitiesNative(sWlan0Index);
1767            } else {
1768                return null;
1769            }
1770        }
1771    }
1772
1773    private static native boolean setCountryCodeHalNative(int iface, String CountryCode);
1774    synchronized public static boolean setCountryCodeHal( String CountryCode) {
1775        synchronized (mLock) {
1776            if (startHal()) {
1777                return setCountryCodeHalNative(sWlan0Index, CountryCode);
1778            } else {
1779                return false;
1780            }
1781        }
1782    }
1783
1784    //---------------------------------------------------------------------------------
1785
1786    /* Wifi Logger commands/events */
1787
1788    private static native boolean startLogging(int iface);
1789
1790    public static interface WifiLoggerEventHandler {
1791        void onDataAvailable(char data[], int len);
1792    }
1793
1794    private static WifiLoggerEventHandler sWifiLoggerEventHandler = null;
1795
1796    synchronized private static void onDataAvailable(char data[], int len) {
1797        if (sWifiLoggerEventHandler != null) {
1798            sWifiLoggerEventHandler.onDataAvailable(data, len);
1799        }
1800    }
1801}
1802