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