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

Commit 26b5711c authored by William Escande's avatar William Escande
Browse files

PeriodicScan: Extract NativeInterface

Bug: 295237486
Test: atest BluetoothInstrumentationTests
Change-Id: I239fb55fa7bed55836f25ebb4f308919f26e456e
parent d87d8c13
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2851,7 +2851,7 @@ int register_com_android_bluetooth_gatt(JNIEnv* env) {
      env, "com/android/bluetooth/gatt/AdvertiseManagerNativeInterface",
      sAdvertiseMethods, NELEM(sAdvertiseMethods));
  register_success &= jniRegisterNativeMethods(
      env, "com/android/bluetooth/gatt/PeriodicScanManager",
      env, "com/android/bluetooth/gatt/PeriodicScanNativeInterface",
      sPeriodicScanMethods, NELEM(sPeriodicScanMethods));
  register_success &= jniRegisterNativeMethods(
      env, "com/android/bluetooth/gatt/DistanceMeasurementNativeInterface",
+11 −49
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ public class PeriodicScanManager {
    private static final String TAG = GattServiceConfig.TAG_PREFIX + "SyncManager";

    private final BluetoothAdapter mAdapter;
    private final PeriodicScanNativeInterface mNativeInterface;
    Map<IBinder, SyncInfo> mSyncs = new ConcurrentHashMap<>();
    Map<IBinder, SyncTransferInfo> mSyncTransfers = Collections.synchronizedMap(new HashMap<>());
    static int sTempRegistrationId = -1;
@@ -59,17 +60,18 @@ public class PeriodicScanManager {
            Log.d(TAG, "advertise manager created");
        }
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mNativeInterface = PeriodicScanNativeInterface.getInstance();
    }

    void start() {
        initializeNative();
        mNativeInterface.init(this);
    }

    void cleanup() {
        if (DBG) {
            Log.d(TAG, "cleanup()");
        }
        cleanupNative();
        mNativeInterface.cleanup();
        mSyncs.clear();
        sTempRegistrationId = -1;
    }
@@ -180,15 +182,10 @@ public class PeriodicScanManager {

    void onSyncStarted(int regId, int syncHandle, int sid, int addressType, String address, int phy,
            int interval, int status) throws Exception {
        if (DBG) {
            Log.d(TAG,
                    "onSyncStarted() - regId=" + regId + ", syncHandle=" + syncHandle + ", status="
                            + status);
        }
        Map<IBinder, SyncInfo> syncMap = findAllSync(regId);
        if (syncMap.size() == 0) {
            Log.d(TAG, "onSyncStarted() - no callback found for regId " + regId);
            stopSyncNative(syncHandle);
            mNativeInterface.stopSync(syncHandle);
            return;
        }

@@ -220,10 +217,6 @@ public class PeriodicScanManager {

    void onSyncReport(int syncHandle, int txPower, int rssi, int dataStatus, byte[] data)
            throws Exception {
        if (DBG) {
            Log.d(TAG, "onSyncReport() - syncHandle=" + syncHandle);
        }

        Map<IBinder, SyncInfo> syncMap = findAllSync(syncHandle);
        if (syncMap.isEmpty()) {
            Log.i(TAG, "onSyncReport() - no callback found for syncHandle " + syncHandle);
@@ -239,9 +232,6 @@ public class PeriodicScanManager {
    }

    void onSyncLost(int syncHandle) throws Exception {
        if (DBG) {
            Log.d(TAG, "onSyncLost() - syncHandle=" + syncHandle);
        }
        Map<IBinder, SyncInfo> syncMap = findAllSync(syncHandle);
        if (syncMap.isEmpty()) {
            Log.i(TAG, "onSyncLost() - no callback found for syncHandle " + syncHandle);
@@ -258,12 +248,7 @@ public class PeriodicScanManager {
        }
    }

    void onBigInfoReport(int syncHandle, boolean encrypted)
        throws Exception {
        if (DBG) {
            Log.d(TAG, "onBigInfoReport() - syncHandle=" + syncHandle +
                    " , encrypted=" + encrypted);
        }
    void onBigInfoReport(int syncHandle, boolean encrypted) throws Exception {
        Map<IBinder, SyncInfo> syncMap = findAllSync(syncHandle);
        if (syncMap.isEmpty()) {
            Log.i(TAG, "onBigInfoReport() - no callback found for syncHandle " + syncHandle);
@@ -323,7 +308,7 @@ public class PeriodicScanManager {
        if (DBG) {
            Log.d(TAG, "startSync() - reg_id=" + cbId + ", callback: " + binder);
        }
        startSyncNative(sid, address, skip, timeout, cbId);
        mNativeInterface.startSync(sid, address, skip, timeout, cbId);
    }

    void stopSync(IPeriodicAdvertisingCallback callback) {
@@ -354,14 +339,13 @@ public class PeriodicScanManager {
        Log.d(TAG, "calling stopSyncNative: " + syncHandle.intValue());
        if (syncHandle < 0) {
            Log.i(TAG, "cancelSync() - sync not established yet");
            cancelSyncNative(sync.advSid, sync.address);
            mNativeInterface.cancelSync(sync.advSid, sync.address);
        } else {
            stopSyncNative(syncHandle.intValue());
            mNativeInterface.stopSync(syncHandle.intValue());
        }
    }

    void onSyncTransferredCallback(int paSource, int status, String bda) {
        Log.d(TAG, "onSyncTransferredCallback()");
        Map.Entry<IBinder, SyncTransferInfo> entry = findSyncTransfer(bda);
        if (entry != null) {
            mSyncTransfers.remove(entry);
@@ -384,7 +368,7 @@ public class PeriodicScanManager {
        //check for duplicate transfers
        mSyncTransfers.put(entry.getKey(), new SyncTransferInfo(bda.getAddress(),
                           entry.getValue().callback));
        syncTransferNative(PA_SOURCE_REMOTE, bda.getAddress(), serviceData, syncHandle);
        mNativeInterface.syncTransfer(bda, serviceData, syncHandle);
    }

    void transferSetInfo(BluetoothDevice bda, int serviceData,
@@ -400,28 +384,6 @@ public class PeriodicScanManager {
            throw new IllegalArgumentException("Can't link to periodic scanner death");
        }
        mSyncTransfers.put(binder, new SyncTransferInfo(bda.getAddress(), callback));
        transferSetInfoNative(PA_SOURCE_LOCAL, bda.getAddress(), serviceData, advHandle);
        mNativeInterface.transferSetInfo(bda, serviceData, advHandle);
    }

    static {
        classInitNative();
    }

    private static native void classInitNative();

    private native void initializeNative();

    private native void cleanupNative();

    private native void startSyncNative(int sid, String address, int skip, int timeout, int regId);

    private native void stopSyncNative(int syncHandle);

    private native void cancelSyncNative(int sid, String address);

    private native void syncTransferNative(int paSource, String address, int serviceData,
                                           int syncHandle);

    private native void transferSetInfoNative(int paSource, String address, int serviceData,
                                              int advHandle);
}
+176 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.gatt;

import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

/** NativeInterface for PeriodicScanManager */
public class PeriodicScanNativeInterface {
    private static final String TAG = PeriodicScanNativeInterface.class.getSimpleName();
    private static final boolean DBG = GattServiceConfig.DBG;

    private static final int PA_SOURCE_LOCAL = 1;
    private static final int PA_SOURCE_REMOTE = 2;

    private PeriodicScanManager mManager;

    @GuardedBy("INSTANCE_LOCK")
    private static PeriodicScanNativeInterface sInstance;

    private static final Object INSTANCE_LOCK = new Object();

    private PeriodicScanNativeInterface() {}

    static PeriodicScanNativeInterface getInstance() {
        synchronized (INSTANCE_LOCK) {
            if (sInstance == null) {
                sInstance = new PeriodicScanNativeInterface();
            }
            return sInstance;
        }
    }

    /** Set singleton instance. */
    @VisibleForTesting
    public static void setInstance(PeriodicScanNativeInterface instance) {
        synchronized (INSTANCE_LOCK) {
            sInstance = instance;
        }
    }

    static {
        if (Utils.isInstrumentationTestMode()) {
            Log.w(TAG, "App is instrumented. Skip loading the native");
        } else {
            classInitNative();
        }
    }

    void init(PeriodicScanManager manager) {
        mManager = manager;
        initializeNative();
    }

    void cleanup() {
        cleanupNative();
    }

    void startSync(int sid, String address, int skip, int timeout, int regId) {
        startSyncNative(sid, address, skip, timeout, regId);
    }

    void stopSync(int syncHandle) {
        stopSyncNative(syncHandle);
    }

    void cancelSync(int sid, String address) {
        cancelSyncNative(sid, address);
    }

    void syncTransfer(BluetoothDevice bda, int serviceData, int syncHandle) {
        syncTransferNative(PA_SOURCE_REMOTE, bda.getAddress(), serviceData, syncHandle);
    }

    void transferSetInfo(BluetoothDevice bda, int serviceData, int advHandle) {
        transferSetInfoNative(PA_SOURCE_LOCAL, bda.getAddress(), serviceData, advHandle);
    }

    /**********************************************************************************************/
    /*********************************** callbacks from native ************************************/
    /**********************************************************************************************/

    void onSyncStarted(
            int regId,
            int syncHandle,
            int sid,
            int addressType,
            String address,
            int phy,
            int interval,
            int status)
            throws Exception {
        if (DBG) {
            Log.d(
                    TAG,
                    "onSyncStarted(): "
                            + (" regId=" + regId)
                            + (" syncHandle=" + syncHandle)
                            + (" status=" + status));
        }
        mManager.onSyncStarted(regId, syncHandle, sid, addressType, address, phy, interval, status);
    }

    void onSyncReport(int syncHandle, int txPower, int rssi, int dataStatus, byte[] data)
            throws Exception {
        if (DBG) {
            Log.d(TAG, "onSyncReport(): syncHandle=" + syncHandle);
        }
        mManager.onSyncReport(syncHandle, txPower, rssi, dataStatus, data);
    }

    void onSyncLost(int syncHandle) throws Exception {
        if (DBG) {
            Log.d(TAG, "onSyncLost(): syncHandle=" + syncHandle);
        }
        mManager.onSyncLost(syncHandle);
    }

    void onSyncTransferredCallback(int paSource, int status, String bda) {
        if (DBG) {
            Log.d(TAG, "onSyncTransferredCallback()");
        }
        mManager.onSyncTransferredCallback(paSource, status, bda);
    }

    void onBigInfoReport(int syncHandle, boolean encrypted) throws Exception {
        if (DBG) {
            Log.d(
                    TAG,
                    "onBigInfoReport():"
                            + (" syncHandle=" + syncHandle)
                            + (" encrypted=" + encrypted));
        }
        mManager.onBigInfoReport(syncHandle, encrypted);
    }

    /**********************************************************************************************/
    /******************************************* native *******************************************/
    /**********************************************************************************************/

    private static native void classInitNative();

    private native void initializeNative();

    private native void cleanupNative();

    private native void startSyncNative(int sid, String address, int skip, int timeout, int regId);

    private native void stopSyncNative(int syncHandle);

    private native void cancelSyncNative(int sid, String address);

    private native void syncTransferNative(
            int paSource, String address, int serviceData, int syncHandle);

    private native void transferSetInfoNative(
            int paSource, String address, int serviceData, int advHandle);
}