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