Bug 1217778 - Ensure sBluetoothGattService is not null before accessing GattInterface in GattManager. r=jocelyn

This commit is contained in:
Louis_Chang 2015-12-02 09:31:45 +08:00
parent b6e9471d19
commit 22a3768b77

View File

@ -21,9 +21,18 @@
#define ENSURE_GATT_INTF_IS_READY_VOID(runnable) \
do { \
if (!sBluetoothGattInterface) { \
if (NS_WARN_IF(!sBluetoothGattInterface)) { \
DispatchReplyError(runnable, \
NS_LITERAL_STRING("BluetoothGattInterface is not ready")); \
runnable = nullptr; \
return; \
} \
} while(0)
#define ENSURE_GATT_INTF_IN_ATTR_DISCOVER(client) \
do { \
if (NS_WARN_IF(!sBluetoothGattInterface)) { \
client->NotifyDiscoverCompleted(false); \
return; \
} \
} while(0)
@ -2564,11 +2573,13 @@ BluetoothGattManager::RegisterClientNotification(BluetoothGattStatus aStatus,
if (client->mStartLeScanRunnable) {
// Client just registered, proceed remaining startLeScan request.
ENSURE_GATT_INTF_IS_READY_VOID(client->mStartLeScanRunnable);
sBluetoothGattInterface->Scan(
aClientIf, true /* start */,
new StartLeScanResultHandler(client));
} else if (client->mConnectRunnable) {
// Client just registered, proceed remaining connect request.
ENSURE_GATT_INTF_IS_READY_VOID(client->mConnectRunnable);
sBluetoothGattInterface->Connect(
aClientIf, client->mDeviceAddr, true /* direct connect */,
TRANSPORT_AUTO,
@ -2773,6 +2784,7 @@ BluetoothGattManager::SearchCompleteNotification(int aConnId,
// All services are discovered, continue to search included services of each
// service if existed, otherwise, notify application that discover completed
if (!client->mServices.IsEmpty()) {
ENSURE_GATT_INTF_IN_ATTR_DISCOVER(client);
sBluetoothGattInterface->GetIncludedService(
aConnId,
client->mServices[0], // start from first service
@ -2832,6 +2844,7 @@ BluetoothGattManager::GetCharacteristicNotification(
client->mCharacteristics.AppendElement(attribute);
// Get next characteristic of this service
ENSURE_GATT_INTF_IN_ATTR_DISCOVER(client);
sBluetoothGattInterface->GetCharacteristic(
aConnId,
aServiceId,
@ -2877,6 +2890,7 @@ BluetoothGattManager::GetDescriptorNotification(
client->mDescriptors.AppendElement(aDescriptorId);
// Get next descriptor of this characteristic
ENSURE_GATT_INTF_IN_ATTR_DISCOVER(client);
sBluetoothGattInterface->GetDescriptor(
aConnId,
aServiceId,
@ -2919,6 +2933,7 @@ BluetoothGattManager::GetIncludedServiceNotification(
RefPtr<BluetoothGattClient> client = sClients->ElementAt(index);
MOZ_ASSERT(client->mDiscoverRunnable);
ENSURE_GATT_INTF_IN_ATTR_DISCOVER(client);
if (aStatus == GATT_STATUS_SUCCESS) {
// Save to mIncludedServices for distributing to applications
client->mIncludedServices.AppendElement(aIncludedServId);
@ -3062,6 +3077,14 @@ BluetoothGattManager::ReadCharacteristicNotification(
} else if (!client->mReadCharacteristicState.mAuthRetry &&
(aStatus == GATT_STATUS_INSUFFICIENT_AUTHENTICATION ||
aStatus == GATT_STATUS_INSUFFICIENT_ENCRYPTION)) {
if (NS_WARN_IF(!sBluetoothGattInterface)) {
client->mReadCharacteristicState.Reset();
// Reject the promise
DispatchReplyError(runnable,
NS_LITERAL_STRING("ReadCharacteristicValue failed"));
return;
}
client->mReadCharacteristicState.mAuthRetry = true;
// Retry with another authentication requirement
sBluetoothGattInterface->ReadCharacteristic(
@ -3101,6 +3124,14 @@ BluetoothGattManager::WriteCharacteristicNotification(
} else if (!client->mWriteCharacteristicState.mAuthRetry &&
(aStatus == GATT_STATUS_INSUFFICIENT_AUTHENTICATION ||
aStatus == GATT_STATUS_INSUFFICIENT_ENCRYPTION)) {
if (NS_WARN_IF(!sBluetoothGattInterface)) {
client->mWriteCharacteristicState.Reset();
// Reject the promise
DispatchReplyError(runnable,
NS_LITERAL_STRING("WriteCharacteristicValue failed"));
return;
}
client->mWriteCharacteristicState.mAuthRetry = true;
// Retry with another authentication requirement
sBluetoothGattInterface->WriteCharacteristic(
@ -3156,6 +3187,14 @@ BluetoothGattManager::ReadDescriptorNotification(
} else if (!client->mReadDescriptorState.mAuthRetry &&
(aStatus == GATT_STATUS_INSUFFICIENT_AUTHENTICATION ||
aStatus == GATT_STATUS_INSUFFICIENT_ENCRYPTION)) {
if (NS_WARN_IF(!sBluetoothGattInterface)) {
client->mReadDescriptorState.Reset();
// Reject the promise
DispatchReplyError(runnable,
NS_LITERAL_STRING("ReadDescriptorValue failed"));
return;
}
client->mReadDescriptorState.mAuthRetry = true;
// Retry with another authentication requirement
sBluetoothGattInterface->ReadDescriptor(
@ -3196,6 +3235,14 @@ BluetoothGattManager::WriteDescriptorNotification(
} else if (!client->mWriteDescriptorState.mAuthRetry &&
(aStatus == GATT_STATUS_INSUFFICIENT_AUTHENTICATION ||
aStatus == GATT_STATUS_INSUFFICIENT_ENCRYPTION)) {
if (NS_WARN_IF(!sBluetoothGattInterface)) {
client->mWriteDescriptorState.Reset();
// Reject the promise
DispatchReplyError(runnable,
NS_LITERAL_STRING("WriteDescriptorValue failed"));
return;
}
client->mWriteDescriptorState.mAuthRetry = true;
// Retry with another authentication requirement
sBluetoothGattInterface->WriteDescriptor(
@ -3286,7 +3333,7 @@ BluetoothGattManager::RegisterServerNotification(BluetoothGattStatus aStatus,
server->mIsRegistering = false;
BluetoothService* bs = BluetoothService::Get();
if (!bs || aStatus != GATT_STATUS_SUCCESS) {
if (!bs || aStatus != GATT_STATUS_SUCCESS || !sBluetoothGattInterface) {
nsAutoString appUuidStr;
UuidToString(aAppUuid, appUuidStr);
@ -3655,6 +3702,7 @@ BluetoothGattManager::RequestReadNotification(
MOZ_ASSERT(NS_IsMainThread());
NS_ENSURE_TRUE_VOID(aConnId);
NS_ENSURE_TRUE_VOID(sBluetoothGattInterface);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
@ -3709,6 +3757,7 @@ BluetoothGattManager::RequestWriteNotification(
MOZ_ASSERT(NS_IsMainThread());
NS_ENSURE_TRUE_VOID(aConnId);
NS_ENSURE_TRUE_VOID(sBluetoothGattInterface);
BluetoothService* bs = BluetoothService::Get();
NS_ENSURE_TRUE_VOID(bs);
@ -3810,6 +3859,9 @@ BluetoothGattManager::ProceedDiscoverProcess(
* 3) Both arrays are already empty:
* Discover is done, notify application.
*/
MOZ_ASSERT(aClient->mDiscoverRunnable);
ENSURE_GATT_INTF_IN_ATTR_DISCOVER(aClient);
if (!aClient->mCharacteristics.IsEmpty()) {
sBluetoothGattInterface->GetDescriptor(
aClient->mConnId,