diff --git a/dom/bluetooth/bluedroid/BluetoothGattManager.cpp b/dom/bluetooth/bluedroid/BluetoothGattManager.cpp index bf10cb55dca7..4f58bcca59d3 100644 --- a/dom/bluetooth/bluedroid/BluetoothGattManager.cpp +++ b/dom/bluetooth/bluedroid/BluetoothGattManager.cpp @@ -6,7 +6,6 @@ #include "BluetoothGattManager.h" -#include "BluetoothHashKeys.h" #include "BluetoothInterface.h" #include "BluetoothReplyRunnable.h" #include "BluetoothService.h" @@ -138,29 +137,25 @@ class mozilla::dom::bluetooth::BluetoothGattClient final : public nsISupports public: NS_DECL_ISUPPORTS - BluetoothGattClient(const BluetoothUuid& aAppUuid, - const BluetoothAddress& aDeviceAddr) - : mAppUuid(aAppUuid) - , mDeviceAddr(aDeviceAddr) - , mClientIf(0) - , mConnId(0) + BluetoothGattClient(const nsAString& aAppUuid, const nsAString& aDeviceAddr) + : mAppUuid(aAppUuid) + , mDeviceAddr(aDeviceAddr) + , mClientIf(0) + , mConnId(0) { } void NotifyDiscoverCompleted(bool aSuccess) { - MOZ_ASSERT(mAppUuid != BluetoothUuid::ZERO); + MOZ_ASSERT(!mAppUuid.IsEmpty()); MOZ_ASSERT(mDiscoverRunnable); BluetoothService* bs = BluetoothService::Get(); NS_ENSURE_TRUE_VOID(bs); - nsAutoString appUuidStr; - UuidToString(mAppUuid, appUuidStr); - // Notify application to clear the cache values of // service/characteristic/descriptor. bs->DistributeSignal(NS_LITERAL_STRING("DiscoverCompleted"), - appUuidStr, + mAppUuid, BluetoothValue(aSuccess)); // Resolve/Reject the Promise. @@ -179,8 +174,8 @@ public: mDiscoverRunnable = nullptr; } - BluetoothUuid mAppUuid; - BluetoothAddress mDeviceAddr; + nsString mAppUuid; + nsString mDeviceAddr; int mClientIf; int mConnId; RefPtr mStartLeScanRunnable; @@ -258,7 +253,7 @@ struct BluetoothGattServerAddDescriptorState { memset(&mServiceHandle, 0, sizeof(mServiceHandle)); memset(&mCharacteristicHandle, 0, sizeof(mCharacteristicHandle)); - mDescriptorUuid.Clear(); + memset(&mDescriptorUuid, 0, sizeof(mDescriptorUuid)); mRunnable = nullptr; } }; @@ -268,13 +263,13 @@ class BluetoothGattServer final : public nsISupports public: NS_DECL_ISUPPORTS - BluetoothGattServer(const BluetoothUuid& aAppUuid) - : mAppUuid(aAppUuid) - , mServerIf(0) - , mIsRegistering(false) + BluetoothGattServer(const nsAString& aAppUuid) + : mAppUuid(aAppUuid) + , mServerIf(0) + , mIsRegistering(false) { } - BluetoothUuid mAppUuid; + nsString mAppUuid; int mServerIf; /* @@ -302,7 +297,7 @@ public: RefPtr mSendIndicationRunnable; // Map connection id from device address - nsDataHashtable mConnectionMap; + nsDataHashtable mConnectionMap; private: ~BluetoothGattServer() { } @@ -314,15 +309,15 @@ class UuidComparator { public: bool Equals(const RefPtr& aClient, - const BluetoothUuid& aAppUuid) const + const nsAString& aAppUuid) const { - return aClient->mAppUuid == aAppUuid; + return aClient->mAppUuid.Equals(aAppUuid); } bool Equals(const RefPtr& aServer, - const BluetoothUuid& aAppUuid) const + const nsAString& aAppUuid) const { - return aServer->mAppUuid == aAppUuid; + return aServer->mAppUuid.Equals(aAppUuid); } }; @@ -659,13 +654,10 @@ public: BluetoothService* bs = BluetoothService::Get(); NS_ENSURE_TRUE_VOID(bs); - nsAutoString appUuidStr; - UuidToString(mClient->mAppUuid, appUuidStr); - // Notify BluetoothGatt for client disconnected bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, + mClient->mAppUuid, BluetoothValue(false)); // Disconnected // Reject the connect request @@ -698,12 +690,9 @@ public: BluetoothService* bs = BluetoothService::Get(); NS_ENSURE_TRUE_VOID(bs); - nsAutoString appUuidStr; - UuidToString(mClient->mAppUuid, appUuidStr); - // Notify BluetoothGatt to clear the clientIf bs->DistributeSignal(NS_LITERAL_STRING("ClientUnregistered"), - appUuidStr); + mClient->mAppUuid); // Resolve the unregister request DispatchReplySuccess(mClient->mUnregisterClientRunnable); @@ -837,14 +826,14 @@ BluetoothGattManager::StartLeScan(const nsTArray& aServiceUuids, ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - if (NS_WARN_IF(NS_FAILED(GenerateUuid(appUuid)))) { + nsString appUuidStr; + if (NS_WARN_IF(NS_FAILED(GenerateUuid(appUuidStr))) || appUuidStr.IsEmpty()) { DispatchReplyError(aRunnable, NS_LITERAL_STRING("start LE scan failed")); return; } - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(appUuidStr, 0 /* Start */, UuidComparator()); // Reject the startLeScan request if the clientIf is being used. if (NS_WARN_IF(index != sClients->NoIndex)) { @@ -854,11 +843,13 @@ BluetoothGattManager::StartLeScan(const nsTArray& aServiceUuids, } index = sClients->Length(); - sClients->AppendElement(new BluetoothGattClient(appUuid, - BluetoothAddress::ANY)); + sClients->AppendElement(new BluetoothGattClient(appUuidStr, EmptyString())); RefPtr client = sClients->ElementAt(index); client->mStartLeScanRunnable = aRunnable; + BluetoothUuid appUuid; + StringToUuid(appUuidStr, appUuid); + // 'startLeScan' will be proceeded after client registered sBluetoothGattInterface->RegisterClient( appUuid, new RegisterClientResultHandler(client)); @@ -873,10 +864,7 @@ BluetoothGattManager::StopLeScan(const nsAString& aScanUuid, ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid scanUuid; - StringToUuid(aScanUuid, scanUuid); - - size_t index = sClients->IndexOf(scanUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aScanUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { // Reject the stop LE scan request DispatchReplyError(aRunnable, NS_LITERAL_STRING("StopLeScan failed")); @@ -909,13 +897,10 @@ public: BluetoothService* bs = BluetoothService::Get(); NS_ENSURE_TRUE_VOID(bs); - nsAutoString appUuidStr; - UuidToString(mClient->mAppUuid, appUuidStr); - // Notify BluetoothGatt for client disconnected bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, + mClient->mAppUuid, BluetoothValue(false)); // Disconnected // Reject the connect request @@ -938,9 +923,6 @@ BluetoothGattManager::Connect(const nsAString& aAppUuid, ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - BluetoothAddress deviceAddr; nsresult rv = StringToAddress(aDeviceAddr, deviceAddr); if (NS_FAILED(rv)) { @@ -958,10 +940,10 @@ BluetoothGattManager::Connect(const nsAString& aAppUuid, return; } - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (index == sClients->NoIndex) { index = sClients->Length(); - sClients->AppendElement(new BluetoothGattClient(appUuid, deviceAddr)); + sClients->AppendElement(new BluetoothGattClient(aAppUuid, aDeviceAddr)); } RefPtr client = sClients->ElementAt(index); @@ -974,9 +956,12 @@ BluetoothGattManager::Connect(const nsAString& aAppUuid, TRANSPORT_AUTO, new ConnectResultHandler(client)); } else { + BluetoothUuid uuid; + StringToUuid(aAppUuid, uuid); + // connect will be proceeded after client registered sBluetoothGattInterface->RegisterClient( - appUuid, new RegisterClientResultHandler(client)); + uuid, new RegisterClientResultHandler(client)); } } @@ -999,13 +984,10 @@ public: BluetoothService* bs = BluetoothService::Get(); NS_ENSURE_TRUE_VOID(bs); - nsAutoString appUuidStr; - UuidToString(mClient->mAppUuid, appUuidStr); - // Notify BluetoothGatt that the client remains connected bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, + mClient->mAppUuid, BluetoothValue(true)); // Connected // Reject the disconnect request @@ -1045,10 +1027,7 @@ BluetoothGattManager::Disconnect(const nsAString& aAppUuid, return; } - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1095,10 +1074,7 @@ BluetoothGattManager::Discover(const nsAString& aAppUuid, ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1241,10 +1217,7 @@ BluetoothGattManager::RegisterNotifications( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1260,10 +1233,17 @@ BluetoothGattManager::RegisterNotifications( return; } + BluetoothAddress deviceAddr; + nsresult rv = StringToAddress(client->mDeviceAddr, deviceAddr); + if (NS_FAILED(rv)) { + DispatchReplyError(aRunnable, STATUS_PARM_INVALID); + return; + } + client->mRegisterNotificationsRunnable = aRunnable; sBluetoothGattInterface->RegisterNotification( - client->mClientIf, client->mDeviceAddr, aServId, aCharId, + client->mClientIf, deviceAddr, aServId, aCharId, new RegisterNotificationsResultHandler(client)); } @@ -1320,10 +1300,7 @@ BluetoothGattManager::DeregisterNotifications( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1338,10 +1315,17 @@ BluetoothGattManager::DeregisterNotifications( return; } + BluetoothAddress deviceAddr; + nsresult rv = StringToAddress(client->mDeviceAddr, deviceAddr); + if (NS_FAILED(rv)) { + DispatchReplyError(aRunnable, STATUS_PARM_INVALID); + return; + } + client->mDeregisterNotificationsRunnable = aRunnable; sBluetoothGattInterface->DeregisterNotification( - client->mClientIf, client->mDeviceAddr, aServId, aCharId, + client->mClientIf, deviceAddr, aServId, aCharId, new DeregisterNotificationsResultHandler(client)); } @@ -1386,10 +1370,7 @@ BluetoothGattManager::ReadCharacteristicValue( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1469,10 +1450,7 @@ BluetoothGattManager::WriteCharacteristicValue( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1554,10 +1532,7 @@ BluetoothGattManager::ReadDescriptorValue( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1638,10 +1613,7 @@ BluetoothGattManager::WriteDescriptorValue( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sClients->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sClients->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1746,10 +1718,13 @@ public: (int)aStatus); MOZ_ASSERT(mServer->mConnectPeripheralRunnable); + nsAutoString addressStr; + AddressToString(mDeviceAddr, addressStr); + DispatchReplyError(mServer->mConnectPeripheralRunnable, NS_LITERAL_STRING("ConnectPeripheral failed")); mServer->mConnectPeripheralRunnable = nullptr; - mServer->mConnectionMap.Remove(mDeviceAddr); + mServer->mConnectionMap.Remove(addressStr); } private: @@ -1768,9 +1743,6 @@ BluetoothGattManager::ConnectPeripheral( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - BluetoothAddress address; nsresult rv = StringToAddress(aAddress, address); if (NS_FAILED(rv)) { @@ -1778,10 +1750,10 @@ BluetoothGattManager::ConnectPeripheral( return; } - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (index == sServers->NoIndex) { index = sServers->Length(); - sServers->AppendElement(new BluetoothGattServer(appUuid)); + sServers->AppendElement(new BluetoothGattServer(aAppUuid)); } RefPtr server = (*sServers)[index]; @@ -1804,13 +1776,13 @@ BluetoothGattManager::ConnectPeripheral( } int connId = 0; - if (server->mConnectionMap.Get(address, &connId)) { + if (server->mConnectionMap.Get(aAddress, &connId)) { MOZ_ASSERT(connId > 0); DispatchReplySuccess(aRunnable); return; } - server->mConnectionMap.Put(address, 0); + server->mConnectionMap.Put(aAddress, 0); server->mConnectPeripheralRunnable = aRunnable; if (server->mServerIf > 0) { @@ -1823,9 +1795,12 @@ BluetoothGattManager::ConnectPeripheral( } else if (!server->mIsRegistering) { /* avoid triggering another registration * procedure if there is an on-going one * already */ + BluetoothUuid uuid; + StringToUuid(aAppUuid, uuid); + // connect will be proceeded after server registered sBluetoothGattInterface->RegisterServer( - appUuid, new RegisterServerResultHandler(server)); + uuid, new RegisterServerResultHandler(server)); } } @@ -1866,9 +1841,6 @@ BluetoothGattManager::DisconnectPeripheral( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - BluetoothAddress address; nsresult rv = StringToAddress(aAddress, address); if (NS_FAILED(rv)) { @@ -1876,7 +1848,7 @@ BluetoothGattManager::DisconnectPeripheral( return; } - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sServers->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -1898,7 +1870,7 @@ BluetoothGattManager::DisconnectPeripheral( // Resolve the request if the device is not connected. int connId = 0; - if (!server->mConnectionMap.Get(address, &connId)) { + if (!server->mConnectionMap.Get(aAddress, &connId)) { DispatchReplySuccess(aRunnable); return; } @@ -1927,12 +1899,9 @@ public: BluetoothService* bs = BluetoothService::Get(); NS_ENSURE_TRUE_VOID(bs); - nsAutoString appUuidStr; - UuidToString(mServer->mAppUuid, appUuidStr); - // Notify BluetoothGattServer to clear the serverIf bs->DistributeSignal(NS_LITERAL_STRING("ServerUnregistered"), - appUuidStr); + mServer->mAppUuid); // Resolve the unregister request if (mServer->mUnregisterServerRunnable) { @@ -2020,13 +1989,10 @@ BluetoothGattManager::ServerAddService( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (index == sServers->NoIndex) { index = sServers->Length(); - sServers->AppendElement(new BluetoothGattServer(appUuid)); + sServers->AppendElement(new BluetoothGattServer(aAppUuid)); } RefPtr server = sServers->ElementAt(index); @@ -2047,9 +2013,12 @@ BluetoothGattManager::ServerAddService( } else if (!server->mIsRegistering) { /* avoid triggering another registration * procedure if there is an on-going one * already */ + BluetoothUuid uuid; + StringToUuid(aAppUuid, uuid); + // add service will be proceeded after server registered sBluetoothGattInterface->RegisterServer( - appUuid, new RegisterServerResultHandler(server)); + uuid, new RegisterServerResultHandler(server)); } } @@ -2092,10 +2061,7 @@ BluetoothGattManager::ServerAddIncludedService( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sServers->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -2163,10 +2129,7 @@ BluetoothGattManager::ServerAddCharacteristic( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sServers->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -2236,10 +2199,7 @@ BluetoothGattManager::ServerAddDescriptor( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sServers->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -2308,10 +2268,7 @@ BluetoothGattManager::ServerRemoveService( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sServers->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -2375,10 +2332,7 @@ BluetoothGattManager::ServerStartService( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sServers->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -2443,10 +2397,7 @@ BluetoothGattManager::ServerStopService( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (NS_WARN_IF(index == sServers->NoIndex)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; @@ -2520,10 +2471,7 @@ BluetoothGattManager::ServerSendResponse(const nsAString& aAppUuid, ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); if (index == sServers->NoIndex) { DispatchReplyError(aRunnable, STATUS_NOT_READY); return; @@ -2535,15 +2483,8 @@ BluetoothGattManager::ServerSendResponse(const nsAString& aAppUuid, return; } - BluetoothAddress address; - nsresult rv = StringToAddress(aAddress, address); - if (NS_FAILED(rv)) { - DispatchReplyError(aRunnable, STATUS_PARM_INVALID); - return; - } - int connId = 0; - server->mConnectionMap.Get(address, &connId); + server->mConnectionMap.Get(aAddress, &connId); if (!connId) { DispatchReplyError(aRunnable, STATUS_NOT_READY); return; @@ -2606,10 +2547,7 @@ BluetoothGattManager::ServerSendIndication( ENSURE_GATT_INTF_IS_READY_VOID(aRunnable); - BluetoothUuid appUuid; - StringToUuid(aAppUuid, appUuid); - - size_t index = sServers->IndexOf(appUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); // Reject the request if the server has not been registered yet. if (index == sServers->NoIndex) { DispatchReplyError(aRunnable, STATUS_NOT_READY); @@ -2628,15 +2566,8 @@ BluetoothGattManager::ServerSendIndication( return; } - BluetoothAddress address; - nsresult rv = StringToAddress(aAddress, address); - if (NS_FAILED(rv)) { - DispatchReplyError(aRunnable, STATUS_PARM_INVALID); - return; - } - int connId = 0; - if (!server->mConnectionMap.Get(address, &connId)) { + if (!server->mConnectionMap.Get(aAddress, &connId)) { DispatchReplyError(aRunnable, STATUS_PARM_INVALID); return; } @@ -2667,7 +2598,10 @@ BluetoothGattManager::RegisterClientNotification(BluetoothGattStatus aStatus, { MOZ_ASSERT(NS_IsMainThread()); - size_t index = sClients->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); + nsString uuid; + UuidToString(aAppUuid, uuid); + + size_t index = sClients->IndexOf(uuid, 0 /* Start */, UuidComparator()); NS_ENSURE_TRUE_VOID(index != sClients->NoIndex); RefPtr client = sClients->ElementAt(index); @@ -2675,17 +2609,14 @@ BluetoothGattManager::RegisterClientNotification(BluetoothGattStatus aStatus, BluetoothService* bs = BluetoothService::Get(); NS_ENSURE_TRUE_VOID(bs); - nsAutoString appUuidStr; - UuidToString(aAppUuid, appUuidStr); - if (aStatus != GATT_STATUS_SUCCESS) { BT_LOGD("RegisterClient failed: clientIf = %d, status = %d, appUuid = %s", - aClientIf, aStatus, NS_ConvertUTF16toUTF8(appUuidStr).get()); + aClientIf, aStatus, NS_ConvertUTF16toUTF8(uuid).get()); // Notify BluetoothGatt for client disconnected bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, BluetoothValue(false)); // Disconnected + uuid, BluetoothValue(false)); // Disconnected if (client->mStartLeScanRunnable) { // Reject the LE scan request @@ -2710,7 +2641,7 @@ BluetoothGattManager::RegisterClientNotification(BluetoothGattStatus aStatus, // Notify BluetoothGatt to update the clientIf bs->DistributeSignal( NS_LITERAL_STRING("ClientRegistered"), - appUuidStr, BluetoothValue(uint32_t(aClientIf))); + uuid, BluetoothValue(uint32_t(aClientIf))); if (client->mStartLeScanRunnable) { // Client just registered, proceed remaining startLeScan request. @@ -2719,8 +2650,24 @@ BluetoothGattManager::RegisterClientNotification(BluetoothGattStatus aStatus, new StartLeScanResultHandler(client)); } else if (client->mConnectRunnable) { // Client just registered, proceed remaining connect request. + BluetoothAddress address; + nsresult rv = StringToAddress(client->mDeviceAddr, address); + if (NS_FAILED(rv)) { + // Notify BluetoothGatt for client disconnected + bs->DistributeSignal( + NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), + client->mAppUuid, + BluetoothValue(false)); // Disconnected + + // Reject the connect request + DispatchReplyError(client->mConnectRunnable, + NS_LITERAL_STRING("Connect failed")); + client->mConnectRunnable = nullptr; + return; + } + sBluetoothGattInterface->Connect( - aClientIf, client->mDeviceAddr, true /* direct connect */, + aClientIf, address, true /* direct connect */, TRANSPORT_AUTO, new ConnectResultHandler(client)); } @@ -2809,9 +2756,6 @@ BluetoothGattManager::ConnectNotification(int aConnId, RefPtr client = sClients->ElementAt(index); - nsAutoString appUuidStr; - UuidToString(client->mAppUuid, appUuidStr); - if (aStatus != GATT_STATUS_SUCCESS) { BT_LOGD("Connect failed: clientIf = %d, connId = %d, status = %d", aClientIf, aConnId, aStatus); @@ -2819,7 +2763,7 @@ BluetoothGattManager::ConnectNotification(int aConnId, // Notify BluetoothGatt that the client remains disconnected bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, + client->mAppUuid, BluetoothValue(false)); // Disconnected // Reject the connect request @@ -2837,7 +2781,7 @@ BluetoothGattManager::ConnectNotification(int aConnId, // Notify BluetoothGatt for client connected bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, + client->mAppUuid, BluetoothValue(true)); // Connected // Resolve the connect request @@ -2865,14 +2809,11 @@ BluetoothGattManager::DisconnectNotification( RefPtr client = sClients->ElementAt(index); - nsAutoString appUuidStr; - UuidToString(client->mAppUuid, appUuidStr); - if (aStatus != GATT_STATUS_SUCCESS) { // Notify BluetoothGatt that the client remains connected bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, + client->mAppUuid, BluetoothValue(true)); // Connected // Reject the disconnect request @@ -2890,7 +2831,7 @@ BluetoothGattManager::DisconnectNotification( // Notify BluetoothGatt for client disconnected bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, + client->mAppUuid, BluetoothValue(false)); // Disconnected // Resolve the disconnect request @@ -2921,12 +2862,9 @@ BluetoothGattManager::SearchCompleteNotification(int aConnId, return; } - nsAutoString appUuidStr; - UuidToString(client->mAppUuid, appUuidStr); - // Notify BluetoothGatt to create all services bs->DistributeSignal(NS_LITERAL_STRING("ServicesDiscovered"), - appUuidStr, + client->mAppUuid, BluetoothValue(client->mServices)); // All services are discovered, continue to search included services of each @@ -3004,11 +2942,8 @@ BluetoothGattManager::GetCharacteristicNotification( AppendNamedValue(values, "serviceId", aServiceId); AppendNamedValue(values, "characteristics", client->mCharacteristics); - nsAutoString appUuidStr; - UuidToString(client->mAppUuid, appUuidStr); - bs->DistributeSignal(NS_LITERAL_STRING("CharacteristicsDiscovered"), - appUuidStr, + client->mAppUuid, BluetoothValue(values)); ProceedDiscoverProcess(client, aServiceId); @@ -3054,11 +2989,8 @@ BluetoothGattManager::GetDescriptorNotification( AppendNamedValue(values, "characteristicId", aCharId); AppendNamedValue(values, "descriptors", client->mDescriptors); - nsAutoString appUuidStr; - UuidToString(client->mAppUuid, appUuidStr); - bs->DistributeSignal(NS_LITERAL_STRING("DescriptorsDiscovered"), - appUuidStr, + client->mAppUuid, BluetoothValue(values)); client->mDescriptors.Clear(); @@ -3102,11 +3034,8 @@ BluetoothGattManager::GetIncludedServiceNotification( AppendNamedValue(values, "serviceId", aServiceId); AppendNamedValue(values, "includedServices", client->mIncludedServices); - nsAutoString appUuidStr; - UuidToString(client->mAppUuid, appUuidStr); - bs->DistributeSignal(NS_LITERAL_STRING("IncludedServicesDiscovered"), - appUuidStr, + client->mAppUuid, BluetoothValue(values)); client->mIncludedServices.Clear(); @@ -3177,11 +3106,8 @@ BluetoothGattManager::NotifyNotification( ids.AppendElement(BluetoothNamedValue(NS_LITERAL_STRING("charId"), aNotifyParam.mCharId)); - nsAutoString appUuidStr; - UuidToString(client->mAppUuid, appUuidStr); - bs->DistributeSignal(NS_LITERAL_STRING(GATT_CHARACTERISTIC_CHANGED_ID), - appUuidStr, + client->mAppUuid, BluetoothValue(ids)); } @@ -3224,11 +3150,8 @@ BluetoothGattManager::ReadCharacteristicNotification( ids.AppendElement(BluetoothNamedValue(NS_LITERAL_STRING("charId"), aReadParam.mCharId)); - nsAutoString appUuidStr; - UuidToString(client->mAppUuid, appUuidStr); - bs->DistributeSignal(NS_LITERAL_STRING(GATT_CHARACTERISTIC_CHANGED_ID), - appUuidStr, + client->mAppUuid, BluetoothValue(ids)); // Resolve the promise @@ -3452,10 +3375,10 @@ BluetoothGattManager::RegisterServerNotification(BluetoothGattStatus aStatus, { MOZ_ASSERT(NS_IsMainThread()); - nsAutoString appUuidStr; - UuidToString(aAppUuid, appUuidStr); + nsString uuid; + UuidToString(aAppUuid, uuid); - size_t index = sServers->IndexOf(aAppUuid, 0 /* Start */, UuidComparator()); + size_t index = sServers->IndexOf(uuid, 0 /* Start */, UuidComparator()); NS_ENSURE_TRUE_VOID(index != sServers->NoIndex); RefPtr server = (*sServers)[index]; @@ -3465,7 +3388,7 @@ BluetoothGattManager::RegisterServerNotification(BluetoothGattStatus aStatus, BluetoothService* bs = BluetoothService::Get(); if (!bs || aStatus != GATT_STATUS_SUCCESS) { BT_LOGD("RegisterServer failed: serverIf = %d, status = %d, appUuid = %s", - aServerIf, aStatus, NS_ConvertUTF16toUTF8(appUuidStr).get()); + aServerIf, aStatus, NS_ConvertUTF16toUTF8(uuid).get()); if (server->mConnectPeripheralRunnable) { // Reject the connect peripheral request @@ -3494,11 +3417,19 @@ BluetoothGattManager::RegisterServerNotification(BluetoothGattStatus aStatus, // Notify BluetoothGattServer to update the serverIf bs->DistributeSignal( NS_LITERAL_STRING("ServerRegistered"), - appUuidStr, BluetoothValue(uint32_t(aServerIf))); + uuid, BluetoothValue(uint32_t(aServerIf))); if (server->mConnectPeripheralRunnable) { // Only one entry exists in the map during first connect peripheral request - const BluetoothAddress& deviceAddr = server->mConnectionMap.Iter().Key(); + BluetoothAddress deviceAddr; + nsresult rv = StringToAddress(server->mConnectionMap.Iter().Key(), deviceAddr); + if (NS_FAILED(rv)) { + DispatchReplyError(server->mConnectPeripheralRunnable, + NS_LITERAL_STRING("ConnectPeripheral failed")); + server->mConnectPeripheralRunnable = nullptr; + server->mConnectionMap.Remove(server->mConnectionMap.Iter().Key()); + return; + } sBluetoothGattInterface->ConnectPeripheral( aServerIf, deviceAddr, true /* direct connect */, TRANSPORT_AUTO, @@ -3531,26 +3462,23 @@ BluetoothGattManager::ConnectionNotification(int aConnId, RefPtr server = (*sServers)[index]; + nsAutoString addressStr; + AddressToString(aBdAddr, addressStr); + // Update the connection map based on the connection status if (aConnected) { - server->mConnectionMap.Put(aBdAddr, aConnId); + server->mConnectionMap.Put(addressStr, aConnId); } else { - server->mConnectionMap.Remove(aBdAddr); + server->mConnectionMap.Remove(addressStr); } - nsAutoString bdAddrStr; - AddressToString(aBdAddr, bdAddrStr); - - nsAutoString appUuidStr; - UuidToString(server->mAppUuid, appUuidStr); - // Notify BluetoothGattServer that connection status changed InfallibleTArray props; AppendNamedValue(props, "Connected", aConnected); - AppendNamedValue(props, "Address", bdAddrStr); + AppendNamedValue(props, "Address", addressStr); bs->DistributeSignal( NS_LITERAL_STRING(GATT_CONNECTION_STATE_CHANGED_ID), - appUuidStr, + server->mAppUuid, BluetoothValue(props)); // Resolve or reject connect/disconnect peripheral requests @@ -3598,15 +3526,12 @@ BluetoothGattManager::ServiceAddedNotification( return; } - nsAutoString appUuidStr; - UuidToString(server->mAppUuid, appUuidStr); - // Notify BluetoothGattServer to update service handle InfallibleTArray props; AppendNamedValue(props, "ServiceId", aServiceId); AppendNamedValue(props, "ServiceHandle", aServiceHandle); bs->DistributeSignal(NS_LITERAL_STRING("ServiceHandleUpdated"), - appUuidStr, + server->mAppUuid, BluetoothValue(props)); if (server->mAddServiceState.mRunnable) { @@ -3673,16 +3598,13 @@ BluetoothGattManager::CharacteristicAddedNotification( return; } - nsAutoString appUuidStr; - UuidToString(server->mAppUuid, appUuidStr); - // Notify BluetoothGattServer to update characteristic handle InfallibleTArray props; AppendNamedValue(props, "CharacteristicUuid", aCharId); AppendNamedValue(props, "ServiceHandle", aServiceHandle); AppendNamedValue(props, "CharacteristicHandle", aCharacteristicHandle); bs->DistributeSignal(NS_LITERAL_STRING("CharacteristicHandleUpdated"), - appUuidStr, + server->mAppUuid, BluetoothValue(props)); if (server->mAddCharacteristicRunnable) { @@ -3719,9 +3641,6 @@ BluetoothGattManager::DescriptorAddedNotification( return; } - nsAutoString appUuidStr; - UuidToString(server->mAppUuid, appUuidStr); - // Notify BluetoothGattServer to update descriptor handle InfallibleTArray props; AppendNamedValue(props, "CharacteristicUuid", aCharId); @@ -3730,7 +3649,7 @@ BluetoothGattManager::DescriptorAddedNotification( server->mAddDescriptorState.mCharacteristicHandle); AppendNamedValue(props, "DescriptorHandle", aDescriptorHandle); bs->DistributeSignal(NS_LITERAL_STRING("DescriptorHandleUpdated"), - appUuidStr, + server->mAppUuid, BluetoothValue(props)); if (server->mAddDescriptorState.mRunnable) { @@ -3863,23 +3782,20 @@ BluetoothGattManager::RequestReadNotification( return; } - nsAutoString bdAddrStr; - AddressToString(aBdAddr, bdAddrStr); - - nsAutoString appUuidStr; - UuidToString(server->mAppUuid, appUuidStr); + nsAutoString addressStr; + AddressToString(aBdAddr, addressStr); // Distribute a signal to gattServer InfallibleTArray properties; AppendNamedValue(properties, "TransId", aTransId); AppendNamedValue(properties, "AttrHandle", aAttributeHandle); - AppendNamedValue(properties, "Address", bdAddrStr); + AppendNamedValue(properties, "Address", addressStr); AppendNamedValue(properties, "NeedResponse", true); AppendNamedValue(properties, "Value", new nsTArray()); bs->DistributeSignal(NS_LITERAL_STRING("ReadRequested"), - appUuidStr, + server->mAppUuid, properties); } @@ -3922,18 +3838,15 @@ BluetoothGattManager::RequestWriteNotification( return; } - nsAutoString bdAddrStr; - AddressToString(aBdAddr, bdAddrStr); - - nsAutoString appUuidStr; - UuidToString(server->mAppUuid, appUuidStr); + nsAutoString addressStr; + AddressToString(aBdAddr, addressStr); // Distribute a signal to gattServer InfallibleTArray properties; AppendNamedValue(properties, "TransId", aTransId); AppendNamedValue(properties, "AttrHandle", aAttributeHandle); - AppendNamedValue(properties, "Address", bdAddrStr); + AppendNamedValue(properties, "Address", addressStr); AppendNamedValue(properties, "NeedResponse", aNeedResponse); nsTArray value; @@ -3941,7 +3854,7 @@ BluetoothGattManager::RequestWriteNotification( AppendNamedValue(properties, "Value", value); bs->DistributeSignal(NS_LITERAL_STRING("WrtieRequested"), - appUuidStr, + server->mAppUuid, properties); } diff --git a/dom/bluetooth/common/BluetoothUtils.cpp b/dom/bluetooth/common/BluetoothUtils.cpp index 66fea686a29e..b8467c5f6fb1 100644 --- a/dom/bluetooth/common/BluetoothUtils.cpp +++ b/dom/bluetooth/common/BluetoothUtils.cpp @@ -230,26 +230,6 @@ StringToUuid(const nsAString& aString, BluetoothUuid& aUuid) memcpy(&aUuid.mUuid[14], &uuid5, sizeof(uint16_t)); } -nsresult -GenerateUuid(BluetoothUuid &aUuid) -{ - nsresult rv; - nsCOMPtr uuidGenerator = - do_GetService("@mozilla.org/uuid-generator;1", &rv); - NS_ENSURE_SUCCESS(rv, rv); - - nsID uuid; - rv = uuidGenerator->GenerateUUIDInPlace(&uuid); - NS_ENSURE_SUCCESS(rv, rv); - - aUuid = BluetoothUuid(uuid.m0 >> 24, uuid.m0 >> 16, uuid.m0 >> 8, uuid.m0, - uuid.m1 >> 8, uuid.m1, - uuid.m2 >> 8, uuid.m2, - uuid.m3[0], uuid.m3[1], uuid.m3[2], uuid.m3[3], - uuid.m3[4], uuid.m3[5], uuid.m3[6], uuid.m3[7]); - return NS_OK; -} - nsresult GenerateUuid(nsAString &aUuidString) { diff --git a/dom/bluetooth/common/BluetoothUtils.h b/dom/bluetooth/common/BluetoothUtils.h index b15741d894c6..79c76ec145c7 100644 --- a/dom/bluetooth/common/BluetoothUtils.h +++ b/dom/bluetooth/common/BluetoothUtils.h @@ -92,14 +92,6 @@ UuidToString(const BluetoothUuid& aUuid, nsAString& aString); void StringToUuid(const nsAString& aString, BluetoothUuid& aUuid); -/** - * Generate a random uuid. - * - * @param aUuid [out] The generated uuid. - */ -nsresult -GenerateUuid(BluetoothUuid &aUuid); - /** * Generate a random uuid. *