diff --git a/frameworks/inner/c_adapter/ohos_bt_socket.cpp b/frameworks/inner/c_adapter/ohos_bt_socket.cpp index 8c42d92b295d777343aae71499e794b24fa5658f..0335a281fc29f069bd22837663f4efce5a5d8b89 100644 --- a/frameworks/inner/c_adapter/ohos_bt_socket.cpp +++ b/frameworks/inner/c_adapter/ohos_bt_socket.cpp @@ -92,7 +92,16 @@ static bool GetSocketUuidPara(const BluetoothCreateSocketPara *socketPara, UUID } serverUuid = UUID::FromString(tmpUuid); } else if (socketPara->socketType == OHOS_SOCKET_L2CAP_LE) { - serverUuid = UUID::RandomUUID(); + if (socketPara->uuid.uuid == nullptr || strlen(socketPara->uuid.uuid) != socketPara->uuid.uuidLen) { + serverUuid = UUID::RandomUUID(); + } else { + string tmpUuid(socketPara->uuid.uuid); + if (!IsValidUuid(tmpUuid)) { + HILOGE("match the UUID faild."); + return false; + } + serverUuid = UUID::FromString(tmpUuid); + } } else { HILOGE("param socketType invalid. socketType: %{public}d", socketPara->socketType); return false; @@ -260,6 +269,9 @@ int SocketConnect(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdA HILOGE("SocketConnect fail, result: %{public}d", result); client->Close(); HILOGE("SocketConnect closed."); + if (result == BT_ERR_SPP_CONNECT_FAILED) { + return BT_SOCEKET_CONNECT_FAILED; + } return BT_SOCKET_INVALID_ID; } int clientId = g_clientMap.AddObject(client); @@ -280,7 +292,7 @@ int SocketConnect(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdA int SocketConnectEx(const BluetoothCreateSocketPara *socketPara, const BdAddr *bdAddr, int psm, BtSocketConnectionCallback *callback) { - HILOGI("SocketConnect start."); + HILOGI("SocketConnectEx start."); if (socketPara == nullptr || bdAddr == nullptr || callback == nullptr) { HILOGE("socketPara is nullptr, or bdAddr is nullptr, or callback is nullptr"); return BT_SOCKET_INVALID_ID; @@ -322,6 +334,32 @@ int SocketConnectEx(const BluetoothCreateSocketPara *socketPara, const BdAddr *b return clientId; } +/** + * @brief Obtain the device random address based on the device real address and tokenId. + * + * @param realAddr The remote device real address. + * @param randomAddr The remote device random address. + * @param tokenId The relative ID used to identify the current client socket. + * @return Returns the operation result status {@link BtStatus}. + */ +int GetRamdomAddress(const BdAddr *realAddr, BdAddr *randomAddr, int tokenId) +{ + if (realAddr == nullptr) { + HILOGE("realAddr is null"); + return OHOS_BT_STATUS_FAIL; + } + + string strRealAddr; + ConvertAddr(realAddr->addr, strRealAddr); + string strRandomAddr; + BluetoothHost *host = &BluetoothHost::GetDefaultHost(); + int result = host->GetRandomAddress(strRealAddr, strRandomAddr, tokenId); + if (result == OHOS_BT_STATUS_SUCCESS) { + GetAddrFromString(strRandomAddr, randomAddr->addr); + } + return result; +} + /** * @brief Disables a connection and releases related resources. * diff --git a/frameworks/inner/ipc/include/bluetooth_host_proxy.h b/frameworks/inner/ipc/include/bluetooth_host_proxy.h index 39727f8b770c2d593d4cc4010564fba7e9a68919..d6e8275906c36e7e318715118d29de9b52f52363 100644 --- a/frameworks/inner/ipc/include/bluetooth_host_proxy.h +++ b/frameworks/inner/ipc/include/bluetooth_host_proxy.h @@ -96,7 +96,7 @@ public: void RegisterBlePeripheralCallback(const sptr &observer) override; void DeregisterBlePeripheralCallback(const sptr &observer) override; int32_t SetFastScan(bool isEnable) override; - int32_t GetRandomAddress(const std::string &realAddr, std::string &randomAddr) override; + int32_t GetRandomAddress(const std::string &realAddr, std::string &randomAddr, int64_t tokenId = -1) override; int32_t SyncRandomAddress(const std::string &realAddr, const std::string &randomAddr) override; int32_t ConnectAllowedProfiles(const std::string &remoteAddr) override; int32_t DisconnectAllowedProfiles(const std::string &remoteAddr) override; diff --git a/frameworks/inner/ipc/interface/i_bluetooth_host.h b/frameworks/inner/ipc/interface/i_bluetooth_host.h index f455d56708cc4ff02d5229f409c3d23807e28ca2..44923f25b8fd4971c20ad31776503dbfaa2b3b91 100644 --- a/frameworks/inner/ipc/interface/i_bluetooth_host.h +++ b/frameworks/inner/ipc/interface/i_bluetooth_host.h @@ -123,7 +123,7 @@ public: virtual void RegisterBlePeripheralCallback(const sptr &observer) = 0; virtual void DeregisterBlePeripheralCallback(const sptr &observer) = 0; virtual int32_t SetFastScan(bool isEnable) = 0; - virtual int32_t GetRandomAddress(const std::string &realAddr, std::string &randomAddr) = 0; + virtual int32_t GetRandomAddress(const std::string &realAddr, std::string &randomAddr, int64_t tokenId = -1) = 0; virtual int32_t SyncRandomAddress(const std::string &realAddr, const std::string &randomAddr) = 0; virtual int32_t ConnectAllowedProfiles(const std::string &remoteAddr) = 0; virtual int32_t DisconnectAllowedProfiles(const std::string &remoteAddr) = 0; diff --git a/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp b/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp index 64a127128b939e54dbe969a67894e9ddcdd2ce4e..986315fb498c7a583e5bbdbdc87a54cf6f12fdbc 100644 --- a/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp +++ b/frameworks/inner/ipc/src/bluetooth_host_proxy.cpp @@ -1535,7 +1535,7 @@ int32_t BluetoothHostProxy::SetFastScan(bool isEnable) return reply.ReadInt32(); } -int32_t BluetoothHostProxy::GetRandomAddress(const std::string &realAddr, std::string &randomAddr) +int32_t BluetoothHostProxy::GetRandomAddress(const std::string &realAddr, std::string &randomAddr, int64_t tokenId) { MessageParcel data; if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) { @@ -1546,6 +1546,10 @@ int32_t BluetoothHostProxy::GetRandomAddress(const std::string &realAddr, std::s HILOGE("BluetoothHostProxy::GetRandomAddress Write realAddr error"); return BT_ERR_IPC_TRANS_FAILED; } + if (!data.WriteInt64(tokenId)) { + HILOGE("BluetoothHostProxy::GetRandomAddress Write tokenId error"); + return BT_ERR_IPC_TRANS_FAILED; + } MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; int32_t error = InnerTransact(BluetoothHostInterfaceCode::GET_RANDOM_ADDRESS, option, data, reply); diff --git a/frameworks/inner/src/bluetooth_host.cpp b/frameworks/inner/src/bluetooth_host.cpp index de33b35219ce1c937f47ed9c771d6e0eefcda583..06aafa7c6669965bb9fd3bcae81c31aeb48cf87b 100644 --- a/frameworks/inner/src/bluetooth_host.cpp +++ b/frameworks/inner/src/bluetooth_host.cpp @@ -1163,10 +1163,12 @@ int BluetoothHost::SetFastScan(bool isEnable) return proxy->SetFastScan(isEnable); } -int BluetoothHost::GetRandomAddress(const std::string &realAddr, std::string &randomAddr) const +int BluetoothHost::GetRandomAddress(const std::string &realAddr, std::string &randomAddr, int64_t tokenId) const { - randomAddr = realAddr; - return BT_NO_ERROR; + CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off."); + sptr proxy = GetRemoteProxy(BLUETOOTH_HOST); + CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "pimpl or bluetooth host is nullptr"); + return proxy->GetRandomAddress(realAddr, randomAddr, tokenId); } int BluetoothHost::ConnectAllowedProfiles(const std::string &remoteAddr) const diff --git a/frameworks/inner/src/bluetooth_socket.cpp b/frameworks/inner/src/bluetooth_socket.cpp index 57963c340184a3f8d1c5fc56b2ec483d10c9eb34..7c3cc9f987d2e1ac59fba3975eb0c47d44fdfe83 100644 --- a/frameworks/inner/src/bluetooth_socket.cpp +++ b/frameworks/inner/src/bluetooth_socket.cpp @@ -465,7 +465,7 @@ int ClientSocket::Connect(int psm) HILOGI("fd_: %{public}d", pimpl->fd_); CHECK_AND_RETURN_LOG_RET(pimpl->fd_ != -1, BtStatus::BT_FAILURE, "connect failed!"); - CHECK_AND_RETURN_LOG_RET(pimpl->RecvSocketPsmOrScn(), BT_ERR_INVALID_STATE, "recv psm or scn failed"); + CHECK_AND_RETURN_LOG_RET(pimpl->RecvSocketPsmOrScn(), BT_ERR_SPP_CONNECT_FAILED, "recv psm or scn failed"); bool recvret = pimpl->RecvSocketSignal(); HILOGI("recvret: %{public}d", recvret); diff --git a/interfaces/inner_api/include/bluetooth_errorcode.h b/interfaces/inner_api/include/bluetooth_errorcode.h index 286d8a669d9f3c527afa574ec43addd4f5f01008..c07a98c6486b9e8ce38856642ec39149961a7ddf 100644 --- a/interfaces/inner_api/include/bluetooth_errorcode.h +++ b/interfaces/inner_api/include/bluetooth_errorcode.h @@ -42,6 +42,7 @@ enum BtErrCode { BT_ERR_UNAVAILABLE_PROXY = BT_ERR_BASE_SYSCAP + 8, BT_ERR_DIALOG_FOR_USER_CONFIRM = BT_ERR_BASE_SYSCAP + 9, BT_ERR_MAX_RESOURCES = BT_ERR_BASE_SYSCAP + 10, + BT_ERR_SPP_CONNECT_FAILED = BT_ERR_BASE_SYSCAP + 11, BT_ERR_INTERNAL_ERROR = BT_ERR_BASE_SYSCAP + 99, BT_ERR_IPC_TRANS_FAILED = BT_ERR_BASE_SYSCAP + 100, diff --git a/interfaces/inner_api/include/bluetooth_host.h b/interfaces/inner_api/include/bluetooth_host.h index 04feabfe3353d4d4ce66ce0eaaf307d7e8b95beb..48f6a3b8b2162ba1c29ed0cffb9a5ff5861bdf85 100644 --- a/interfaces/inner_api/include/bluetooth_host.h +++ b/interfaces/inner_api/include/bluetooth_host.h @@ -751,10 +751,11 @@ public: * the interface needs to be used for address translation. * @param realAddr real address. * @param[out] randomAddr random address. + * @param tokenId tokenId. * @return Returns {@link BT_NO_ERROR} if get random address success; * returns an error code defined in {@link BtErrCode} otherwise. */ - int GetRandomAddress(const std::string &realAddr, std::string &randomAddr) const; + int GetRandomAddress(const std::string &realAddr, std::string &randomAddr, int64_t tokenId = -1) const; /** * @brief Connects all allowed bluetooth profiles between the local and remote device. diff --git a/interfaces/inner_api/include/c_header/ohos_bt_socket.h b/interfaces/inner_api/include/c_header/ohos_bt_socket.h index f91e2e3307374af5ce6f8495085bb309a825ced1..1053bb735898522f4e33db71e099b37d4528435d 100644 --- a/interfaces/inner_api/include/c_header/ohos_bt_socket.h +++ b/interfaces/inner_api/include/c_header/ohos_bt_socket.h @@ -32,6 +32,7 @@ extern "C" { #define BT_SOCKET_INVALID_SCN (-1) #define BT_SOCKET_INVALID_TYPE (-2) #define BT_SOCKET_LIMITED_RESOURCES (-3) +#define BT_SOCEKET_CONNECT_FAILED (-4) typedef enum { OHOS_SOCKET_SPP_RFCOMM = 0x0,