Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 126755cf authored by Robert Greenwalt's avatar Robert Greenwalt
Browse files

Fix Wifi Batch Scanning

Some parsing bugs, more debug logs, etc.

bug:9301872
Change-Id: I245ab00955e245be81a019750be0f8491aa505fd
parent c333a3f7
Loading
Loading
Loading
Loading
+17 −9
Original line number Diff line number Diff line
@@ -325,11 +325,13 @@ public final class WifiService extends IWifiManager.Stub {
    private class BatchedScanRequest extends DeathRecipient {
        BatchedScanSettings settings;
        int uid;
        int pid;

        BatchedScanRequest(BatchedScanSettings settings, IBinder binder, int uid) {
        BatchedScanRequest(BatchedScanSettings settings, IBinder binder) {
            super(0, null, binder, null);
            this.settings = settings;
            this.uid = uid;
            this.uid = getCallingUid();
            this.pid = getCallingPid();
        }
        public void binderDied() {
            stopBatchedScan(settings, mBinder);
@@ -337,6 +339,10 @@ public final class WifiService extends IWifiManager.Stub {
        public String toString() {
            return "BatchedScanRequest{settings=" + settings + ", binder=" + mBinder + "}";
        }

        public boolean isSameApp() {
            return (this.uid == getCallingUid() && this.pid == getCallingPid());
        }
    }

    private final List<BatchedScanRequest> mBatchedScanners = new ArrayList<BatchedScanRequest>();
@@ -359,7 +365,7 @@ public final class WifiService extends IWifiManager.Stub {
        if (mBatchedScanSupported == false) return false;
        requested = new BatchedScanSettings(requested);
        if (requested.isInvalid()) return false;
        BatchedScanRequest r = new BatchedScanRequest(requested, binder, Binder.getCallingUid());
        BatchedScanRequest r = new BatchedScanRequest(requested, binder);
        synchronized(mBatchedScanners) {
            mBatchedScanners.add(r);
            resolveBatchedScannersLocked();
@@ -393,16 +399,18 @@ public final class WifiService extends IWifiManager.Stub {
    public void stopBatchedScan(BatchedScanSettings settings, IBinder binder) {
        enforceChangePermission();
        if (mBatchedScanSupported == false) return;
        ArrayList<BatchedScanRequest> found = new ArrayList<BatchedScanRequest>();
        synchronized(mBatchedScanners) {
            BatchedScanRequest found = null;
            for (BatchedScanRequest r : mBatchedScanners) {
                if (r.mBinder.equals(binder) && r.settings.equals(settings)) {
                    found = r;
                    break;
                if (r.isSameApp() && (settings == null || settings.equals(r.settings))) {
                    found.add(r);
                    if (settings != null) break;
                }
            }
            for (BatchedScanRequest r : found) {
                mBatchedScanners.remove(r);
            }
            if (found != null) {
                mBatchedScanners.remove(found);
            if (found.size() != 0) {
                resolveBatchedScannersLocked();
            }
        }
+4 −0
Original line number Diff line number Diff line
@@ -818,6 +818,10 @@ public class WifiManager {
    /**
     * End a requested batch scan for this applicaiton.  Note that batched scan may
     * still occur if other apps are using them.
     *
     * @param requested {@link BatchedScanSettings} the scan settings you previously requested
     *        and now wish to stop.  A value of null here will stop all scans requested by the
     *        calling App.
     * @hide
     */
    public void stopBatchedScan(BatchedScanSettings requested) {
+5 −2
Original line number Diff line number Diff line
@@ -138,6 +138,7 @@ public class WifiNative {
            localLog(cmdId + "->" + mInterfacePrefix + command);
            boolean result = doBooleanCommandNative(mInterfacePrefix + command);
            localLog(cmdId + "<-" + result);
            if (DBG) Log.d(mTAG, "   returned " + result);
            return result;
        }
    }
@@ -149,6 +150,7 @@ public class WifiNative {
            localLog(cmdId + "->" + mInterfacePrefix + command);
            int result = doIntCommandNative(mInterfacePrefix + command);
            localLog(cmdId + "<-" + result);
            if (DBG) Log.d(mTAG, "   returned " + result);
            return result;
        }
    }
@@ -160,6 +162,7 @@ public class WifiNative {
            localLog(cmdId + "->" + mInterfacePrefix + command);
            String result = doStringCommandNative(mInterfacePrefix + command);
            localLog(cmdId + "<-" + result);
            if (DBG) Log.d(mTAG, "   returned " + result);
            return result;
        }
    }
@@ -281,7 +284,7 @@ public class WifiNative {

    /**
     * Format of command
     * DRIVER WLS_BATCHING SET SCAN_FRQ=x MSCAN=r BESTN=y CHANNEL=<z, w, t> RTT=s
     * DRIVER WLS_BATCHING SET SCANFREQ=x MSCAN=r BESTN=y CHANNEL=<z, w, t> RTT=s
     * where x is an ascii representation of an integer number of seconds between scans
     *       r is an ascii representation of an integer number of scans per batch
     *       y is an ascii representation of an integer number of the max AP to remember per scan
@@ -295,7 +298,7 @@ public class WifiNative {
     */
    public String setBatchedScanSettings(BatchedScanSettings settings) {
        if (settings == null) return doStringCommand("DRIVER WLS_BATCHING STOP");
        String cmd = "DRIVER WLS_BATCHING SET SCAN_FRQ=" + settings.scanIntervalSec;
        String cmd = "DRIVER WLS_BATCHING SET SCANFREQ=" + settings.scanIntervalSec;
        cmd += " MSCAN=" + settings.maxScansPerBatch;
        if (settings.maxApPerScan != BatchedScanSettings.UNSPECIFIED) {
            cmd += " BESTN=" + settings.maxApPerScan;
+48 −30
Original line number Diff line number Diff line
@@ -859,6 +859,7 @@ public class WifiStateMachine extends StateMachine {
            mExpectedBatchedScans = Integer.parseInt(scansExpected);
            setNextBatchedAlarm(mExpectedBatchedScans);
        } catch (NumberFormatException e) {
            stopBatchedScan();
            loge("Exception parsing WifiNative.setBatchedScanSettings response " + e);
        }
    }
@@ -876,6 +877,11 @@ public class WifiStateMachine extends StateMachine {
    }

    private void handleBatchedScanPollRequest() {
        if (DBG) {
            log("handleBatchedScanPoll Request - mBatchedScanMinPollTime=" +
                    mBatchedScanMinPollTime + " , mBatchedScanSettings=" +
                    mBatchedScanSettings);
        }
        // if there is no appropriate PollTime that's because we either aren't
        // batching or we've already set a time for a poll request
        if (mBatchedScanMinPollTime == 0) return;
@@ -887,7 +893,7 @@ public class WifiStateMachine extends StateMachine {
            // do the poll and reset our timers
            startNextBatchedScan();
        } else {
            mAlarmManager.set(AlarmManager.RTC_WAKEUP, mBatchedScanMinPollTime,
            mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, mBatchedScanMinPollTime,
                    mBatchedScanIntervalIntent);
            mBatchedScanMinPollTime = 0;
        }
@@ -932,7 +938,7 @@ public class WifiStateMachine extends StateMachine {

        // set the alarm to do the next poll.  We set it a little short as we'd rather
        // wake up wearly than miss a scan due to buffer overflow
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + ((secToFull - (mBatchedScanSettings.scanIntervalSec / 2)) * 1000),
                mBatchedScanIntervalIntent);
    }
@@ -964,11 +970,13 @@ public class WifiStateMachine extends StateMachine {
     *   etc
     *   "----"
     */
    private final static boolean DEBUG_PARSE = false;
    private void retrieveBatchedScanData() {
        String rawData = mWifiNative.getBatchedScanResults();
        if (DEBUG_PARSE) log("rawData = " + rawData);
        mBatchedScanMinPollTime = 0;
        if (rawData == null) {
            loge("Unexpected null BatchedScanResults");
        if (rawData == null || rawData.equalsIgnoreCase("OK")) {
            loge("Unexpected BatchedScanResults :" + rawData);
            return;
        }

@@ -978,17 +986,19 @@ public class WifiStateMachine extends StateMachine {
        final String TRUNCATED = "trunc";
        final String AGE = "age=";
        final String DIST = "dist=";
        final String DISTSD = "distsd=";
        final String DISTSD = "distSd=";

        String splitData[] = rawData.split("\n");
        int n = 0;
        if (splitData[n].startsWith(SCANCOUNT)) {
            try {
                scanCount = Integer.parseInt(splitData[n++].substring(SCANCOUNT.length()));
            } catch (NumberFormatException e) {}
            } catch (NumberFormatException e) {
                loge("scancount parseInt Exception from " + splitData[n]);
            }
        } else log("scancount not found");
        if (scanCount == 0) {
            loge("scanCount not found");
            loge("scanCount==0 - aborting");
            return;
        }

@@ -1010,13 +1020,15 @@ public class WifiStateMachine extends StateMachine {

            while (true) {
                while (n < splitData.length) {
                    if (DEBUG_PARSE) logd("parsing " + splitData[n]);
                    if (splitData[n].equals(END_OF_BATCHES)) {
                        if (++n != splitData.length) {
                        if (n+1 != splitData.length) {
                            loge("didn't consume " + (splitData.length-n));
                        }
                        if (mBatchedScanResults.size() > 0) {
                            mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
                            mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
                        }
                        logd("retrieveBatchedScanResults X");
                        return;
                    }
                    if ((splitData[n].equals(END_STR)) || splitData[n].equals(DELIMITER_STR)) {
@@ -1038,51 +1050,56 @@ public class WifiStateMachine extends StateMachine {
                                logd("Found empty batch");
                            }
                        }
                        n++;
                    } else if (splitData[n].equals(BSSID_STR)) {
                        bssid = splitData[n++].substring(BSSID_STR.length());
                    } else if (splitData[n].equals(FREQ_STR)) {
                    } else if (splitData[n].equals(TRUNCATED)) {
                        batchedScanResult.truncated = true;
                    } else if (splitData[n].startsWith(BSSID_STR)) {
                        bssid = splitData[n].substring(BSSID_STR.length());
                    } else if (splitData[n].startsWith(FREQ_STR)) {
                        try {
                            freq = Integer.parseInt(splitData[n++].substring(FREQ_STR.length()));
                            freq = Integer.parseInt(splitData[n].substring(FREQ_STR.length()));
                        } catch (NumberFormatException e) {
                            loge("Invalid freqency: " + splitData[n-1]);
                            loge("Invalid freqency: " + splitData[n]);
                            freq = 0;
                        }
                    } else if (splitData[n].equals(AGE)) {
                    } else if (splitData[n].startsWith(AGE)) {
                        try {
                            tsf = now - Long.parseLong(splitData[n++].substring(AGE.length()));
                            tsf = now - Long.parseLong(splitData[n].substring(AGE.length()));
                        } catch (NumberFormatException e) {
                            loge("Invalid timestamp: " + splitData[n-1]);
                            loge("Invalid timestamp: " + splitData[n]);
                            tsf = 0;
                        }
                    } else if (splitData[n].equals(SSID_STR)) {
                    } else if (splitData[n].startsWith(SSID_STR)) {
                        wifiSsid = WifiSsid.createFromAsciiEncoded(
                                splitData[n++].substring(SSID_STR.length()));
                    } else if (splitData[n].equals(LEVEL_STR)) {
                                splitData[n].substring(SSID_STR.length()));
                    } else if (splitData[n].startsWith(LEVEL_STR)) {
                        try {
                            level = Integer.parseInt(splitData[n++].substring(LEVEL_STR.length()));
                            level = Integer.parseInt(splitData[n].substring(LEVEL_STR.length()));
                            if (level > 0) level -= 256;
                        } catch (NumberFormatException e) {
                            loge("Invalid level: " + splitData[n-1]);
                            loge("Invalid level: " + splitData[n]);
                            level = 0;
                        }
                    } else if (splitData[n].equals(DIST)) {
                    } else if (splitData[n].startsWith(DIST)) {
                        try {
                            dist = Integer.parseInt(splitData[n++].substring(DIST.length()));
                            dist = Integer.parseInt(splitData[n].substring(DIST.length()));
                        } catch (NumberFormatException e) {
                            loge("Invalid distance: " + splitData[n-1]);
                            loge("Invalid distance: " + splitData[n]);
                            dist = ScanResult.UNSPECIFIED;
                        }
                    } else if (splitData[n].equals(DISTSD)) {
                    } else if (splitData[n].startsWith(DISTSD)) {
                        try {
                            distSd = Integer.parseInt(splitData[n++].substring(DISTSD.length()));
                            distSd = Integer.parseInt(splitData[n].substring(DISTSD.length()));
                        } catch (NumberFormatException e) {
                            loge("Invalid distanceSd: " + splitData[n-1]);
                            loge("Invalid distanceSd: " + splitData[n]);
                            distSd = ScanResult.UNSPECIFIED;
                        }
                    } else {
                        loge("Unable to parse batched scan result line: " + splitData[n]);
                    }
                    n++;
                }
                rawData = mWifiNative.getBatchedScanResults();
                if (DEBUG_PARSE) log("reading more data:\n" + rawData);
                if (rawData == null) {
                    loge("Unexpected null BatchedScanResults");
                    return;
@@ -2392,6 +2409,7 @@ public class WifiStateMachine extends StateMachine {
                    break;
                case CMD_POLL_BATCHED_SCAN:
                    handleBatchedScanPollRequest();
                    break;
                case CMD_START_NEXT_BATCHED_SCAN:
                    startNextBatchedScan();
                    break;