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

Commit d79d6f39 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge changes from topic "bluetooth_scanning" am: c2e570e0

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/1937299

Change-Id: Ic9cce43306a651b1df25bf3ad0a6118ba92defc0
parents 9d4048b8 c2e570e0
Loading
Loading
Loading
Loading
+24 −11
Original line number Diff line number Diff line
@@ -210,7 +210,8 @@ 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 +222,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());

  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());
  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(), original_address.get());
}

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

    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());
    // TODO(optedoblivion): Figure out original address for here, use empty
    // for now

    // length of data + '\0'
    char empty_address[18] = "00:00:00:00:00:00";
    ScopedLocalRef<jstring> fake_address(
        sCallbackEnv.get(), sCallbackEnv->NewStringUTF(empty_address));

    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(), fake_address.get());
  }

  void OnTrackAdvFoundLost(AdvertisingTrackInfo track_info) {
@@ -994,8 +1006,9 @@ static void classInitNative(JNIEnv* env, jclass clazz) {
      env->GetMethodID(clazz, "onClientRegistered", "(IIJJ)V");
  method_onScannerRegistered =
      env->GetMethodID(clazz, "onScannerRegistered", "(IIJJ)V");
  method_onScanResult = env->GetMethodID(clazz, "onScanResult",
                                         "(IILjava/lang/String;IIIIII[B)V");
  method_onScanResult =
      env->GetMethodID(clazz, "onScanResult",
                       "(IILjava/lang/String;IIIIII[BLjava/lang/String;)V");
  method_onConnected =
      env->GetMethodID(clazz, "onConnected", "(IIILjava/lang/String;)V");
  method_onDisconnected =
+61 −11
Original line number Diff line number Diff line
@@ -111,6 +111,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 mMatches;
        private final MatchOrigin mOrigin;
        private MatchResult(boolean matches, MatchOrigin origin) {
            this.mMatches = matches;
            this.mOrigin = origin;
        }

        public boolean getMatches() {
            return mMatches;
        }

        public MatchOrigin getMatchOrigin() {
            return mOrigin;
        }
    }

    /**
     * The default floor value for LE batch scan report delays greater than 0
     */
@@ -356,7 +378,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, HexEncoding.decode(test));
                                        127, -54, 0x0, HexEncoding.decode(test),
                                        "DD:34:02:05:5C:4E");
                            }
                            sendEmptyMessageDelayed(0, DateUtils.SECOND_IN_MILLIS);
                        }
@@ -1160,23 +1183,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) {
            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);
@@ -1184,6 +1208,7 @@ public class GattService extends ProfileService {
        for (ScanClient client : mScanManager.getRegularScanQueue()) {
            ScannerMap.App app = mScannerMap.getById(client.scannerId);
            if (app == null) {
                Log.i(TAG, "App is null; skip.");
                continue;
            }

@@ -1195,6 +1220,7 @@ public class GattService extends ProfileService {
            if (settings.getLegacy()) {
                if ((eventType & ET_LEGACY_MASK) == 0) {
                    // If this is legacy scan, but nonlegacy result - skip.
                    Log.i(TAG, "Legacy scan, non legacy result; skip.");
                    continue;
                } else {
                    // Some apps are used to fixed-size advertise data.
@@ -1212,6 +1238,7 @@ public class GattService extends ProfileService {

            if (client.hasDisavowedLocation) {
                if (mLocationDenylistPredicate.test(result)) {
                    Log.i(TAG, "Skipping client for location deny list");
                    continue;
                }
            }
@@ -1232,11 +1259,22 @@ public class GattService extends ProfileService {
                    result = sanitized;
                }
            }
            if (!hasPermission || !matchesFilters(client, result)) {
            MatchResult matchResult = matchesFilters(client, result, originalAddress);
            if (!hasPermission || !matchResult.getMatches()) {
                Log.i(TAG, "Skipping client: permission="
                        + hasPermission + " matches=" + matchResult.getMatches());
                continue;
            }

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

            if ((settings.getCallbackType() & ScanSettings.CALLBACK_TYPE_ALL_MATCHES) == 0) {
                Log.i(TAG, "Skipping client: CALLBACK_TYPE_ALL_MATCHES");
                continue;
            }

@@ -1335,16 +1373,28 @@ 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 or original address
    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)
@@ -1956,7 +2006,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);
            }
        }
+7 −7
Original line number Diff line number Diff line
@@ -181,13 +181,13 @@ 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) {
+5 −0
Original line number Diff line number Diff line
@@ -1809,6 +1809,10 @@ static void bta_dm_inq_results_cb(tBTM_INQ_RESULTS* p_inq, const uint8_t* p_eir,
  uint16_t service_class;

  result.inq_res.bd_addr = p_inq->remote_bd_addr;

  // Pass the original address to GattService#onScanResult
  result.inq_res.original_bda = p_inq->original_bda;

  memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN);
  BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class);
  result.inq_res.is_limited =
@@ -3312,6 +3316,7 @@ static void bta_dm_observe_results_cb(tBTM_INQ_RESULTS* p_inq,
  APPL_TRACE_DEBUG("bta_dm_observe_results_cb");

  result.inq_res.bd_addr = p_inq->remote_bd_addr;
  result.inq_res.original_bda = p_inq->original_bda;
  result.inq_res.rssi = p_inq->rssi;
  result.inq_res.ble_addr_type = p_inq->ble_addr_type;
  result.inq_res.inq_result_type = p_inq->inq_result_type;
+2 −0
Original line number Diff line number Diff line
@@ -434,6 +434,8 @@ typedef struct {
  tBT_DEVICE_TYPE device_type;
  uint8_t flag;
  bool include_rsi; /* true, if ADV contains RSI data */
  RawAddress original_bda; /* original address to pass up to
                              GattService#onScanResult */
} tBTA_DM_INQ_RES;

/* Structure associated with BTA_DM_INQ_CMPL_EVT */
Loading