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