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