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

Commit df38dddd authored by Jakub Pawlowski's avatar Jakub Pawlowski Committed by Gerrit Code Review
Browse files

Merge "Unify advertising API (3/3)"

parents 87250c76 2592f4e5
Loading
Loading
Loading
Loading
+3 −33
Original line number Diff line number Diff line
@@ -148,7 +148,6 @@ static jmethodID method_onWriteDescriptor;
static jmethodID method_onNotify;
static jmethodID method_onRegisterForNotifications;
static jmethodID method_onReadRemoteRssi;
static jmethodID method_onAdvertiseCallback;
static jmethodID method_onConfigureMTU;
static jmethodID method_onScanFilterConfig;
static jmethodID method_onScanFilterParamsConfigured;
@@ -831,7 +830,6 @@ static void classInitNative(JNIEnv* env, jclass clazz) {
    method_onRegisterForNotifications = env->GetMethodID(clazz, "onRegisterForNotifications", "(IIII)V");
    method_onReadRemoteRssi = env->GetMethodID(clazz, "onReadRemoteRssi", "(ILjava/lang/String;II)V");
    method_onConfigureMTU = env->GetMethodID(clazz, "onConfigureMTU", "(III)V");
    method_onAdvertiseCallback = env->GetMethodID(clazz, "onAdvertiseCallback", "(I)V");
    method_onScanFilterConfig = env->GetMethodID(clazz, "onScanFilterConfig", "(IIIII)V");
    method_onScanFilterParamsConfigured = env->GetMethodID(clazz, "onScanFilterParamsConfigured", "(IIII)V");
    method_onScanFilterEnableDisabled = env->GetMethodID(clazz, "onScanFilterEnableDisabled", "(III)V");
@@ -1115,32 +1113,6 @@ static void gattClientReadRemoteRssiNative(JNIEnv* env, jobject object, jint cli
    sGattIf->client->read_remote_rssi(clientif, &bda);
}


void btgattc_advertise_cb(uint8_t status)
{
    CallbackEnv sCallbackEnv(__func__);
    if (!sCallbackEnv.valid()) return;
    sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onAdvertiseCallback, status);
}

static void gattAdvertiseNative(JNIEnv *env, jobject object, jboolean start)
{
    if (!sGattIf) return;
    sGattIf->advertiser->Enable(start, base::Bind(&btgattc_advertise_cb));
}

static void gattSetAdvDataNative(JNIEnv *env, jobject object,
                                 jboolean setScanRsp, jbyteArray data)
{
    if (!sGattIf) return;
    jbyte* data_data = env->GetByteArrayElements(data, NULL);
    uint16_t data_len = (uint16_t) env->GetArrayLength(data);
    vector<uint8_t> data_vec(data_data, data_data + data_len);
    env->ReleaseByteArrayElements(data, data_data, JNI_ABORT);

    sGattIf->advertiser->SetData(setScanRsp, std::move(data_vec));
}

static void gattSetScanParametersNative(JNIEnv* env, jobject object,
                                        jint client_if, jint scan_interval_unit,
                                        jint scan_window_unit)
@@ -1388,7 +1360,7 @@ static void gattClientEnableAdvNative(JNIEnv* env, jobject object, jint advertis
{
    if (!sGattIf) return;

    sGattIf->advertiser->MultiAdvEnable(
    sGattIf->advertiser->Enable(
        advertiser_id, enable,
        base::Bind(&ble_advertiser_enable_cb, enable, advertiser_id), timeout_s,
        base::Bind(&ble_advertiser_enable_cb, false, advertiser_id));
@@ -1399,7 +1371,7 @@ static void gattClientSetAdvParamsNative(JNIEnv* env, jobject object, jint adver
{
    if (!sGattIf) return;

    sGattIf->advertiser->MultiAdvSetParameters(
    sGattIf->advertiser->SetParameters(
        advertiser_id, min_interval, max_interval, adv_type, chnl_map, tx_power,
        base::Bind(&ble_advertiser_set_params_cb, advertiser_id));
}
@@ -1413,7 +1385,7 @@ static void gattClientSetAdvDataNative(JNIEnv* env, jobject object, jint adverti
    vector<uint8_t> data_vec(data_data, data_data + data_len);
    env->ReleaseByteArrayElements(data, data_data, JNI_ABORT);

    sGattIf->advertiser->MultiAdvSetInstData(
    sGattIf->advertiser->SetData(
        advertiser_id, set_scan_rsp, std::move(data_vec),
        base::Bind(&ble_advertiser_setadv_data_cb, advertiser_id));
}
@@ -1656,8 +1628,6 @@ static JNINativeMethod sAdvertiseMethods[] = {
    {"gattClientSetParamsNative", "(IIIIII)V", (void *) gattClientSetAdvParamsNative},
    {"gattClientSetAdvDataNative", "(IZ[B)V", (void *) gattClientSetAdvDataNative},
    {"gattClientEnableAdvNative", "(IZI)V", (void *) gattClientEnableAdvNative},
    {"gattSetAdvDataNative", "(Z[B)V", (void *) gattSetAdvDataNative},
    {"gattAdvertiseNative", "(Z)V", (void *) gattAdvertiseNative},
};

// JNI functions defined in ScanManager class.
+0 −10
Original line number Diff line number Diff line
@@ -1420,12 +1420,6 @@ public class AdapterService extends Service {
             return service.isMultiAdvertisementSupported();
         }

         public boolean isPeripheralModeSupported() {
             AdapterService service = getService();
             if (service == null) return false;
             return service.isPeripheralModeSupported();
         }

         public boolean isOffloadedFilteringSupported() {
             AdapterService service = getService();
             if (service == null) return false;
@@ -2281,10 +2275,6 @@ public class AdapterService extends Service {
        return mAdapterProperties.getNumOfOffloadedScanFilterSupported();
    }

    public boolean isPeripheralModeSupported() {
        return getResources().getBoolean(R.bool.config_bluetooth_le_peripheral_mode_supported);
    }

    public int getOffloadedScanResultStorage() {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        return mAdapterProperties.getOffloadedScanResultStorage();
+8 −68
Original line number Diff line number Diff line
@@ -241,16 +241,7 @@ class AdvertiseManager {

        // Returns maximum advertise instances supported by controller.
        int maxAdvertiseInstances() {
            // Note numOfAdvtInstances includes the standard advertising instance.
            // TODO: remove - 1 once the stack is able to include standard instance for multiple
            // advertising.
            if (mAdapterService.isMultiAdvertisementSupported()) {
                return mAdapterService.getNumOfAdvertisementInstancesSupported() - 1;
            }
            if (mAdapterService.isPeripheralModeSupported()) {
                return 1;
            }
            return 0;
            return mAdapterService.getNumOfAdvertisementInstancesSupported();
        }
    }

@@ -284,20 +275,8 @@ class AdvertiseManager {
        private static final int ADVERTISING_EVENT_TYPE_SCANNABLE = 2;
        private static final int ADVERTISING_EVENT_TYPE_NON_CONNECTABLE = 3;

        // TODO: Extract advertising logic into interface as we have multiple implementations now.
        boolean startAdverising(AdvertiseClient client) {
            if (!mAdapterService.isMultiAdvertisementSupported() &&
                    !mAdapterService.isPeripheralModeSupported()) {
                return false;
            }
            if (mAdapterService.isMultiAdvertisementSupported()) {
                return startMultiAdvertising(client);
            }
            return startSingleAdvertising(client);
        }

        boolean startMultiAdvertising(AdvertiseClient client) {
            logd("starting multi advertising");
            logd("starting advertising");
            resetCountDownLatch();
            setAdvertisingParameters(client);
            if (!waitForCallback()) {
@@ -324,32 +303,8 @@ class AdvertiseManager {
            return true;
        }

        boolean startSingleAdvertising(AdvertiseClient client) {
            logd("starting single advertising");
            resetCountDownLatch();
            enableAdvertising(client, true);
            if (!waitForCallback()) {
                return false;
            }
            setAdvertisingData(client, client.advertiseData, false);
            if (client.scanResponse != null) {
                setAdvertisingData(client, client.scanResponse, true);
            }
            return true;
        }

        void stopAdvertising(AdvertiseClient client) {
            if (mAdapterService.isMultiAdvertisementSupported()) {
            gattClientEnableAdvNative(client.advertiserId, false, 0);
            } else {
                gattAdvertiseNative(false);
                try {
                    mService.onAdvertiseInstanceEnabled(
                            AdvertiseCallback.ADVERTISE_SUCCESS, client.advertiserId, false);
                } catch (RemoteException e) {
                        Log.d(TAG, "failed onAdvertiseInstanceDisabled", e);
                }
            }
        }

        private void resetCountDownLatch() {
@@ -371,27 +326,21 @@ class AdvertiseManager {
            int maxAdvertiseUnit = minAdvertiseUnit + ADVERTISING_INTERVAL_DELTA_UNIT;
            int advertiseEventType = getAdvertisingEventType(client);
            int txPowerLevel = getTxPowerLevel(client.settings);
            if (mAdapterService.isMultiAdvertisementSupported()) {

            // if only legacy advertising is supported, the TX power settings wont take effect
            gattClientSetParamsNative(
                        advertiserId,
                        minAdvertiseUnit, maxAdvertiseUnit,
                        advertiseEventType,
                        ADVERTISING_CHANNEL_ALL,
                        txPowerLevel);
            } else {
                //there is no method to set parameters if multi advertising not enabled
            }
        }

        private void enableAdvertising(AdvertiseClient client, boolean enable) {
            int advertiserId = client.advertiserId;
            int advertiseTimeoutSeconds = (int) TimeUnit.MILLISECONDS.toSeconds(
                    client.settings.getTimeout());
            if (mAdapterService.isMultiAdvertisementSupported()) {
            gattClientEnableAdvNative(advertiserId, enable, advertiseTimeoutSeconds);
            } else {
                gattAdvertiseNative(enable);
            }
        }

        private static final int DEVICE_NAME_MAX = 18;
@@ -540,12 +489,7 @@ class AdvertiseManager {
            }

            byte [] data_out = advertiseDataToBytes(data);

            if (mAdapterService.isMultiAdvertisementSupported()) {
            gattClientSetAdvDataNative(client.advertiserId, isScanResponse, data_out);
            } else {
                gattSetAdvDataNative(isScanResponse, data_out);
            }
        }

        // Convert settings tx power level to stack tx power level.
@@ -605,10 +549,6 @@ class AdvertiseManager {

        private native void gattClientEnableAdvNative(int advertiserId,
                boolean enable, int timeout_s);

        private native void gattSetAdvDataNative(boolean setScanRsp, byte[] data);

        private native void gattAdvertiseNative(boolean start);
    }

    private void logd(String s) {
+0 −6
Original line number Diff line number Diff line
@@ -1160,12 +1160,6 @@ public class GattService extends ProfileService {
        }
    }

    // Callback for standard advertising instance.
    void onAdvertiseCallback(int status) {
        if (DBG) Log.d(TAG, "onAdvertiseCallback status=" + status);
        mAdvertiseManager.callbackDone(0 /* advertiser_id */, status);
    }

    // Followings are callbacks for Bluetooth LE Advertise operations.
    // Start advertising flow is
    //     register advertiser         -> onAdvertiserRegistered