From 6d5a03f00fc00a4fe9d9e2bd1e6a2abc8ffc5284 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Fri, 4 Nov 2016 12:06:30 -0700 Subject: [PATCH] Unify advertising API (3/3) This patch unifies advertising API. On startup, if VSC multi advertising is avaliable, it will be used, otherwise BleAdvertiserLegacyHciInterfaceImpl will be used, which provides one advertising instance. Test: sl4a tests are passing, all proper tests updated accordingly Change-Id: I899ed51bf4e747e1e3ff1a472a55f060249cbb75 --- jni/com_android_bluetooth_gatt.cpp | 36 +--------- .../bluetooth/btservice/AdapterService.java | 10 --- .../android/bluetooth/gatt/AdvertiseManager.java | 76 +++------------------- src/com/android/bluetooth/gatt/GattService.java | 6 -- 4 files changed, 11 insertions(+), 117 deletions(-) diff --git a/jni/com_android_bluetooth_gatt.cpp b/jni/com_android_bluetooth_gatt.cpp index 6e4ef1da..30806582 100644 --- a/jni/com_android_bluetooth_gatt.cpp +++ b/jni/com_android_bluetooth_gatt.cpp @@ -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 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 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. diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java index 8119bef7..06efeff5 100644 --- a/src/com/android/bluetooth/btservice/AdapterService.java +++ b/src/com/android/bluetooth/btservice/AdapterService.java @@ -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(); diff --git a/src/com/android/bluetooth/gatt/AdvertiseManager.java b/src/com/android/bluetooth/gatt/AdvertiseManager.java index e88d5fa6..83c4e972 100644 --- a/src/com/android/bluetooth/gatt/AdvertiseManager.java +++ b/src/com/android/bluetooth/gatt/AdvertiseManager.java @@ -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); - } - } + gattClientEnableAdvNative(client.advertiserId, false, 0); } 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()) { - gattClientSetParamsNative( + + // 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); - } + gattClientEnableAdvNative(advertiserId, enable, advertiseTimeoutSeconds); } 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); - } + gattClientSetAdvDataNative(client.advertiserId, 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) { diff --git a/src/com/android/bluetooth/gatt/GattService.java b/src/com/android/bluetooth/gatt/GattService.java index c49a23ff..2185d7de 100644 --- a/src/com/android/bluetooth/gatt/GattService.java +++ b/src/com/android/bluetooth/gatt/GattService.java @@ -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 -- 2.11.0