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