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

Commit 76eb4250 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "ble_scanning_pi_results" into sc-v2-dev

* changes:
  BLE Scanning: Handle the filter matching
  BLE Scanning: Pass correct address type for scanning
  BLE Scanning: Receive the original address to onScanResults
parents a8883845 b813fe8e
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -210,7 +210,7 @@ void btgattc_scan_result_cb(uint16_t event_type, uint8_t addr_type,
                            uint8_t secondary_phy, uint8_t advertising_sid,
                            int8_t tx_power, int8_t rssi,
                            uint16_t periodic_adv_int,
                            std::vector<uint8_t> adv_data) {
                            std::vector<uint8_t> adv_data, RawAddress* original_bda) {
  CallbackEnv sCallbackEnv(__func__);
  if (!sCallbackEnv.valid()) return;

@@ -221,10 +221,13 @@ void btgattc_scan_result_cb(uint16_t event_type, uint8_t addr_type,
  sCallbackEnv->SetByteArrayRegion(jb.get(), 0, adv_data.size(),
                                   (jbyte*)adv_data.data());

  ScopedLocalRef<jstring> original_address(sCallbackEnv.get(),
                                  bdaddr2newjstr(sCallbackEnv.get(), original_bda));

  sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onScanResult, event_type,
                               addr_type, address.get(), primary_phy,
                               secondary_phy, advertising_sid, tx_power, rssi,
                               periodic_adv_int, jb.get());
                               periodic_adv_int, jb.get(), original_address.get());
}

void btgattc_open_cb(int conn_id, int status, int clientIf,
@@ -892,10 +895,11 @@ class JniScanningCallbacks : ScanningCallbacks {
    sCallbackEnv->SetByteArrayRegion(jb.get(), 0, adv_data.size(),
                                     (jbyte*)adv_data.data());

    // TODO(optedoblivion): Figure out original address for here, use same address for now
    sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onScanResult, event_type,
                                 addr_type, address.get(), primary_phy,
                                 secondary_phy, advertising_sid, tx_power, rssi,
                                 periodic_adv_int, jb.get());
                                 periodic_adv_int, jb.get(), address.get());
  }

  void OnTrackAdvFoundLost(AdvertisingTrackInfo track_info) {
@@ -971,7 +975,7 @@ static void classInitNative(JNIEnv* env, jclass clazz) {
  method_onScannerRegistered =
      env->GetMethodID(clazz, "onScannerRegistered", "(IIJJ)V");
  method_onScanResult = env->GetMethodID(clazz, "onScanResult",
                                         "(IILjava/lang/String;IIIIII[B)V");
                                         "(IILjava/lang/String;IIIIII[BLjava/lang/String;)V");
  method_onConnected =
      env->GetMethodID(clazz, "onConnected", "(IIILjava/lang/String;)V");
  method_onDisconnected =
+57 −11
Original line number Diff line number Diff line
@@ -109,6 +109,28 @@ public class GattService extends ProfileService {
    private static final int TRUNCATED_RESULT_SIZE = 11;
    private static final int TIME_STAMP_LENGTH = 2;

    private enum MatchOrigin {
        PSEUDO_ADDRESS,
        ORIGINAL_ADDRESS
    }

    private static class MatchResult {
        private final boolean matches;
        private final MatchOrigin origin;
        private MatchResult(boolean matches, MatchOrigin origin) {
            this.matches = matches;
            this.origin = origin;
        }

        public boolean getMatches() {
            return matches;
        }

        public MatchOrigin getMatchOrigin() {
            return origin;
        }
    }

    /**
     * The default floor value for LE batch scan report delays greater than 0
     */
@@ -331,7 +353,8 @@ public class GattService extends ProfileService {
                            }
                            for (String test : TEST_MODE_BEACONS) {
                                onScanResultInternal(0x1b, 0x1, "DD:34:02:05:5C:4D", 1, 0, 0xff,
                                        127, -54, 0x0, HexDump.hexStringToByteArray(test));
                                        127, -54, 0x0, HexDump.hexStringToByteArray(test),
                                        "DD:34:02:05:5C:4E");
                            }
                            sendEmptyMessageDelayed(0, DateUtils.SECOND_IN_MILLIS);
                        }
@@ -1133,23 +1156,24 @@ public class GattService extends ProfileService {

    void onScanResult(int eventType, int addressType, String address, int primaryPhy,
            int secondaryPhy, int advertisingSid, int txPower, int rssi, int periodicAdvInt,
            byte[] advData) {
            byte[] advData, String originalAddress) {
        // When in testing mode, ignore all real-world events
        if (isTestModeEnabled()) return;

        onScanResultInternal(eventType, addressType, address, primaryPhy, secondaryPhy,
                advertisingSid, txPower, rssi, periodicAdvInt, advData);
                advertisingSid, txPower, rssi, periodicAdvInt, advData, originalAddress);
    }

    void onScanResultInternal(int eventType, int addressType, String address, int primaryPhy,
            int secondaryPhy, int advertisingSid, int txPower, int rssi, int periodicAdvInt,
            byte[] advData) {
            byte[] advData, String originalAddress) {
        if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
            Log.d(TAG, "onScanResult() - eventType=0x" + Integer.toHexString(eventType)
                    + ", addressType=" + addressType + ", address=" + address + ", primaryPhy="
                    + primaryPhy + ", secondaryPhy=" + secondaryPhy + ", advertisingSid=0x"
                    + Integer.toHexString(advertisingSid) + ", txPower=" + txPower + ", rssi="
                    + rssi + ", periodicAdvInt=0x" + Integer.toHexString(periodicAdvInt));
                    + rssi + ", periodicAdvInt=0x" + Integer.toHexString(periodicAdvInt)
                    + ", originalAddress=" + originalAddress);
        }

        byte[] legacyAdvData = Arrays.copyOfRange(advData, 0, 62);
@@ -1220,13 +1244,22 @@ public class GattService extends ProfileService {
                }
                continue;
            }
            if (!matchesFilters(client, result)) {

            MatchResult matchResult = matchesFilters(client, result, originalAddress);
            if (!matchResult.getMatches()) {
                if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
                    Log.d(TAG, "result did not match filter for scanner id " + client.scannerId);
                }
                continue;
            }

            if (matchResult.getMatchOrigin() == MatchOrigin.ORIGINAL_ADDRESS) {
                result = new ScanResult(getAnonymousDevice(originalAddress), eventType, primaryPhy,
                        secondaryPhy, advertisingSid, txPower, rssi, periodicAdvInt, scanRecord,
                            SystemClock.elapsedRealtimeNanos());

            }

            if ((settings.getCallbackType() & ScanSettings.CALLBACK_TYPE_ALL_MATCHES) == 0) {
                if (VDBG || mAdapterService.getIsVerboseLoggingEnabledForAll()) {
                    Log.d(TAG, "callback type " + settings.getCallbackType()
@@ -1330,16 +1363,29 @@ public class GattService extends ProfileService {
    }

    // Check if a scan record matches a specific filters.
    private boolean matchesFilters(ScanClient client, ScanResult scanResult) {
    private MatchResult matchesFilters(ScanClient client, ScanResult scanResult) {
        return matchesFilters(client, scanResult, null);
    }


    // Check if a scan record matches a specific filters.
    private MatchResult matchesFilters(ScanClient client, ScanResult scanResult,
            String originalAddress) {
        if (client.filters == null || client.filters.isEmpty()) {
            return true;
            // TODO: Do we really wanna return true here?
            return new MatchResult(true, MatchOrigin.PSEUDO_ADDRESS);
        }
        for (ScanFilter filter : client.filters) {
            // Need to check the filter matches, and the original address without changing the API
            if (filter.matches(scanResult)) {
                return true;
                return new MatchResult(true, MatchOrigin.PSEUDO_ADDRESS);
            }
            if (originalAddress != null
                    && originalAddress.equalsIgnoreCase(filter.getDeviceAddress())) {
                return new MatchResult(true, MatchOrigin.ORIGINAL_ADDRESS);
            }
        return false;
        }
        return new MatchResult(false, MatchOrigin.PSEUDO_ADDRESS);
    }

    void onClientRegistered(int status, int clientIf, long uuidLsb, long uuidMsb)
@@ -1922,7 +1968,7 @@ public class GattService extends ProfileService {
        // Reconstruct the scan results.
        ArrayList<ScanResult> results = new ArrayList<ScanResult>();
        for (ScanResult scanResult : permittedResults) {
            if (matchesFilters(client, scanResult)) {
            if (matchesFilters(client, scanResult).getMatches()) {
                results.add(scanResult);
            }
        }
+6 −7
Original line number Diff line number Diff line
@@ -181,13 +181,12 @@ import java.util.UUID;
            addName(filter.getDeviceName());
        }
        if (filter.getDeviceAddress() != null) {
            byte addressType = (byte) filter.getAddressType();
            // If addressType == iADDRESS_TYPE_PUBLIC (0) then this is the original
            // setDeviceAddress(address) API path which provided DEVICE_TYPE_ALL (2) which might map
            // to the stack value for address type of BTM_BLE_STATIC (2)
            // Additionally, we shouldn't confuse device type with address type.
            addDeviceAddress(filter.getDeviceAddress(),
                    ((addressType == 0) ? DEVICE_TYPE_ALL : addressType), filter.getIrk());
            /*
             * Pass the addres type here.  This address type will be used for the resolving address,
             * however, the host stack will force the type to 0x02 for the APCF filter in
             * btm_ble_adv_filter.cc#BTM_LE_PF_addr_filter(...)
             */
            addDeviceAddress(filter.getDeviceAddress(), (byte) filter.getAddressType(), filter.getIrk());
        }
        if (filter.getServiceUuid() != null) {
            if (filter.getServiceUuidMask() == null) {