| @@ -23,9 +23,13 @@ |
| #include "stdexcept" | #include "stdexcept" |
| #include "bluetooth_errorcode.h" | #include "bluetooth_errorcode.h" |
| #include "bluetooth_hid_host.h" | #include "bluetooth_hid_host.h" |
| +#include "bluetooth_hid_device.h" |
| +#include "bluetooth_address_info.h" |
| #include "bluetooth_log.h" | #include "bluetooth_log.h" |
| #include "taihe_bluetooth_hid_host_observer.h" | #include "taihe_bluetooth_hid_host_observer.h" |
| +#include "taihe_bluetooth_hid_device_observer.h" |
| #include "taihe_bluetooth_utils.h" | #include "taihe_bluetooth_utils.h" |
| +#include "hid_device_utils.h" |
| | |
| namespace OHOS { | namespace OHOS { |
| namespace Bluetooth { | namespace Bluetooth { |
| @@ -57,36 +61,44 @@ public: |
| void Connect(taihe::string_view deviceId) | void Connect(taihe::string_view deviceId) |
| { | { |
| HILOGI("enter"); | HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_SERVICE_DISCONNECTED, |
| + BT_ERR_INVALID_STATE, BT_ERR_PROFILE_DISABLED, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| std::string remoteAddr = std::string(deviceId); | std::string remoteAddr = std::string(deviceId); |
| bool checkRet = CheckDeviceIdParam(remoteAddr); | bool checkRet = CheckDeviceIdParam(remoteAddr); |
| - TAIHE_BT_ASSERT_RETURN_VOID(checkRet, BT_ERR_INVALID_PARAM); | + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(checkRet, BT_ERR_INVALID_PARAM); |
| | |
| HidHost *profile = HidHost::GetProfile(); | HidHost *profile = HidHost::GetProfile(); |
| BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR); | BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR); |
| int32_t errorCode = profile->Connect(device); | int32_t errorCode = profile->Connect(device); |
| HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); | HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); |
| - TAIHE_BT_ASSERT_RETURN_VOID(errorCode == BT_NO_ERROR, errorCode); | + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| } | } |
| | |
| void Disconnect(taihe::string_view deviceId) | void Disconnect(taihe::string_view deviceId) |
| { | { |
| HILOGI("Disconnect called"); | HILOGI("Disconnect called"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_SERVICE_DISCONNECTED, |
| + BT_ERR_INVALID_STATE, BT_ERR_PROFILE_DISABLED, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| std::string remoteAddr = std::string(deviceId); | std::string remoteAddr = std::string(deviceId); |
| bool checkRet = CheckDeviceIdParam(remoteAddr); | bool checkRet = CheckDeviceIdParam(remoteAddr); |
| - TAIHE_BT_ASSERT_RETURN_VOID(checkRet, BT_ERR_INVALID_PARAM); | + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(checkRet, BT_ERR_INVALID_PARAM); |
| | |
| HidHost *profile = HidHost::GetProfile(); | HidHost *profile = HidHost::GetProfile(); |
| BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR); | BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR); |
| int32_t errorCode = profile->Disconnect(device); | int32_t errorCode = profile->Disconnect(device); |
| HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); | HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); |
| - TAIHE_BT_ASSERT_RETURN_VOID(errorCode == BT_NO_ERROR, errorCode); | + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| } | } |
| | |
| ohos::bluetooth::constant::ProfileConnectionState GetConnectionState(taihe::string_view deviceId) | ohos::bluetooth::constant::ProfileConnectionState GetConnectionState(taihe::string_view deviceId) |
| { | { |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| std::string remoteAddr = std::string(deviceId); | std::string remoteAddr = std::string(deviceId); |
| bool checkRet = CheckDeviceIdParam(remoteAddr); | bool checkRet = CheckDeviceIdParam(remoteAddr); |
| - TAIHE_BT_ASSERT_RETURN(checkRet, BT_ERR_INVALID_PARAM, | + TAIHE_BT_ASSERT_RETURN_VERIFY(checkRet, BT_ERR_INVALID_PARAM, |
| ohos::bluetooth::constant::ProfileConnectionState::from_value(0)); | ohos::bluetooth::constant::ProfileConnectionState::from_value(0)); |
| | |
| HidHost *profile = HidHost::GetProfile(); | HidHost *profile = HidHost::GetProfile(); |
| @@ -94,7 +106,7 @@ public: |
| int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED); | int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED); |
| int32_t errorCode = profile->GetDeviceState(device, state); | int32_t errorCode = profile->GetDeviceState(device, state); |
| int32_t profileState = TaiheUtils::GetProfileConnectionState(state); | int32_t profileState = TaiheUtils::GetProfileConnectionState(state); |
| - TAIHE_BT_ASSERT_RETURN(errorCode == BT_NO_ERROR, errorCode, | + TAIHE_BT_ASSERT_RETURN_VERIFY(errorCode == BT_NO_ERROR, errorCode, |
| ohos::bluetooth::constant::ProfileConnectionState::from_value(profileState)); | ohos::bluetooth::constant::ProfileConnectionState::from_value(profileState)); |
| | |
| return ohos::bluetooth::constant::ProfileConnectionState::from_value(profileState); | return ohos::bluetooth::constant::ProfileConnectionState::from_value(profileState); |
| @@ -103,12 +115,14 @@ public: |
| taihe::array<taihe::string> GetConnectedDevices() | taihe::array<taihe::string> GetConnectedDevices() |
| { | { |
| HILOGI("enter"); | HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| HidHost *profile = HidHost::GetProfile(); | HidHost *profile = HidHost::GetProfile(); |
| std::vector<int> states = { static_cast<int>(BTConnectState::CONNECTED) }; | std::vector<int> states = { static_cast<int>(BTConnectState::CONNECTED) }; |
| std::vector<BluetoothRemoteDevice> devices; | std::vector<BluetoothRemoteDevice> devices; |
| int errorCode = profile->GetDevicesByStates(states, devices); | int errorCode = profile->GetDevicesByStates(states, devices); |
| HILOGI("errorCode:%{public}s, devices size:%{public}zu", GetTaiheErrMsg(errorCode).c_str(), devices.size()); | HILOGI("errorCode:%{public}s, devices size:%{public}zu", GetTaiheErrMsg(errorCode).c_str(), devices.size()); |
| - TAIHE_BT_ASSERT_RETURN(errorCode == BT_NO_ERROR, errorCode, taihe::array<taihe::string>{}); | + TAIHE_BT_ASSERT_RETURN_VERIFY(errorCode == BT_NO_ERROR, errorCode, taihe::array<taihe::string>{}); |
| | |
| std::vector<std::string> deviceVector; | std::vector<std::string> deviceVector; |
| for (auto &device: devices) { | for (auto &device: devices) { |
| @@ -122,9 +136,11 @@ public: |
| ohos::bluetooth::baseProfile::ConnectionStrategy GetConnectionStrategySync(taihe::string_view deviceId) | ohos::bluetooth::baseProfile::ConnectionStrategy GetConnectionStrategySync(taihe::string_view deviceId) |
| { | { |
| HILOGD("start"); | HILOGD("start"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| std::string remoteAddr = std::string(deviceId); | std::string remoteAddr = std::string(deviceId); |
| bool checkRet = CheckDeviceIdParam(remoteAddr); | bool checkRet = CheckDeviceIdParam(remoteAddr); |
| - TAIHE_BT_ASSERT_RETURN(checkRet, BT_ERR_INVALID_PARAM, | + TAIHE_BT_ASSERT_RETURN_VERIFY(checkRet, BT_ERR_INVALID_PARAM, |
| ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(0)); | ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(0)); |
| | |
| BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR); | BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR); |
| @@ -132,7 +148,7 @@ public: |
| int strategy = 0; | int strategy = 0; |
| int32_t err = profile->GetConnectStrategy(remoteDevice, strategy); | int32_t err = profile->GetConnectStrategy(remoteDevice, strategy); |
| HILOGI("err: %{public}d, strategy: %{public}d", err, strategy); | HILOGI("err: %{public}d, strategy: %{public}d", err, strategy); |
| - TAIHE_BT_ASSERT_RETURN(err == BT_NO_ERROR, err, | + TAIHE_BT_ASSERT_RETURN_VERIFY(err == BT_NO_ERROR, err, |
| ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(strategy)); | ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(strategy)); |
| | |
| return ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(strategy); | return ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(strategy); |
| @@ -142,16 +158,18 @@ public: |
| ohos::bluetooth::baseProfile::ConnectionStrategy strategy) | ohos::bluetooth::baseProfile::ConnectionStrategy strategy) |
| { | { |
| HILOGD("start"); | HILOGD("start"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| std::string remoteAddr = std::string(deviceId); | std::string remoteAddr = std::string(deviceId); |
| int32_t connStrategy = strategy.get_value(); | int32_t connStrategy = strategy.get_value(); |
| bool checkRet = CheckSetConnectStrategyParam(remoteAddr, connStrategy); | bool checkRet = CheckSetConnectStrategyParam(remoteAddr, connStrategy); |
| - TAIHE_BT_ASSERT_RETURN_VOID(checkRet, BT_ERR_INVALID_PARAM); | + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(checkRet, BT_ERR_INVALID_PARAM); |
| | |
| BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR); | BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR); |
| HidHost *profile = HidHost::GetProfile(); | HidHost *profile = HidHost::GetProfile(); |
| int32_t err = profile->SetConnectStrategy(remoteDevice, connStrategy); | int32_t err = profile->SetConnectStrategy(remoteDevice, connStrategy); |
| HILOGI("err: %{public}d", err); | HILOGI("err: %{public}d", err); |
| - TAIHE_BT_ASSERT_RETURN_VOID(err == BT_NO_ERROR, err); | + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(err == BT_NO_ERROR, err); |
| } | } |
| private: | private: |
| std::shared_ptr<TaiheBluetoothHidHostObserver> observer_ = nullptr; | std::shared_ptr<TaiheBluetoothHidHostObserver> observer_ = nullptr; |
| @@ -163,10 +181,492 @@ private: |
| | |
| return taihe::make_holder<HidHostProfileImpl, ::ohos::bluetooth::hid::HidHostProfile>(); | return taihe::make_holder<HidHostProfileImpl, ::ohos::bluetooth::hid::HidHostProfile>(); |
| } | } |
| + |
| +std::shared_ptr<TaiheBluetoothHidDeviceObserver> g_hidDeviceObserver = |
| + std::make_shared<TaiheBluetoothHidDeviceObserver>(); |
| + |
| +class HidDeviceProfileImpl { |
| +public: |
| + HidDeviceProfileImpl() |
| + { |
| + observer_ = g_hidDeviceObserver; |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + profile->RegisterObserver(g_hidDeviceObserver); |
| + } |
| + |
| + void OnConnectionStateChange(::taihe::callback_view<void( |
| + ::ohos::bluetooth::baseProfile::StateChangeParam const& data)> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->connectionStateChangeLock_); |
| + auto eventCb = ::taihe::optional<::taihe::callback<void( |
| + ::ohos::bluetooth::baseProfile::StateChangeParam const& data)>>{std::in_place_t{}, callback}; |
| + auto& callbackVec = observer_->connectionStateChangeVec_; |
| + if (std::find(callbackVec.begin(), callbackVec.end(), eventCb) != callbackVec.end()) { |
| + return; |
| + } |
| + callbackVec.emplace_back(eventCb); |
| + } |
| + } |
| + |
| + void OffConnectionStateChange(::taihe::optional_view<::taihe::callback<void( |
| + ::ohos::bluetooth::baseProfile::StateChangeParam const& data)>> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->connectionStateChangeLock_); |
| + auto& callbackVec = observer_->connectionStateChangeVec_; |
| + if (callback.has_value()) { |
| + callbackVec.erase( |
| + std::remove_if(callbackVec.begin(), callbackVec.end(), |
| + [callback](const ::taihe::optional< |
| + ::taihe::callback<void(::ohos::bluetooth::baseProfile::StateChangeParam const& data)>>& it) { |
| + return it == callback; |
| + }), |
| + callbackVec.end()); |
| + } else { |
| + callbackVec.clear(); |
| + } |
| + } |
| + } |
| + |
| + void OnSetProtocol(::taihe::callback_view<void( |
| + ::ohos::bluetooth::hid::ProtocolData const& data)> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->setProtocolLock_); |
| + auto eventCb = ::taihe::optional<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::ProtocolData const& data)>>{std::in_place_t{}, callback}; |
| + auto& callbackVec = observer_->setProtocolVec_; |
| + if (std::find(callbackVec.begin(), callbackVec.end(), eventCb) != callbackVec.end()) { |
| + return; |
| + } |
| + callbackVec.emplace_back(eventCb); |
| + } |
| + } |
| + |
| + void OffSetProtocol(::taihe::optional_view<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::ProtocolData const& data)>> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->setProtocolLock_); |
| + auto& callbackVec = observer_->setProtocolVec_; |
| + if (callback.has_value()) { |
| + callbackVec.erase( |
| + std::remove_if(callbackVec.begin(), callbackVec.end(), |
| + [callback](const ::taihe::optional< |
| + ::taihe::callback<void(::ohos::bluetooth::hid::ProtocolData const& data)>>& it) { |
| + return it == callback; |
| + }), |
| + callbackVec.end()); |
| + } else { |
| + callbackVec.clear(); |
| + } |
| + } |
| + } |
| + |
| + void OnInterruptDataReceived(::taihe::callback_view<void( |
| + ::ohos::bluetooth::hid::InterruptData const& data)> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->interruptDataReceivedLock_); |
| + auto eventCb = ::taihe::optional<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::InterruptData const& data)>>{std::in_place_t{}, callback}; |
| + auto& callbackVec = observer_->interruptDataReceivedVec_; |
| + if (std::find(callbackVec.begin(), callbackVec.end(), eventCb) != callbackVec.end()) { |
| + return; |
| + } |
| + callbackVec.emplace_back(eventCb); |
| + } |
| + } |
| + |
| + void OffInterruptDataReceived(::taihe::optional_view<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::InterruptData const& data)>> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->interruptDataReceivedLock_); |
| + auto& callbackVec = observer_->interruptDataReceivedVec_; |
| + if (callback.has_value()) { |
| + callbackVec.erase( |
| + std::remove_if(callbackVec.begin(), callbackVec.end(), |
| + [callback](const ::taihe::optional< |
| + ::taihe::callback<void(::ohos::bluetooth::hid::InterruptData const& data)>>& it) { |
| + return it == callback; |
| + }), |
| + callbackVec.end()); |
| + } else { |
| + callbackVec.clear(); |
| + } |
| + } |
| + } |
| + |
| + void OnSetReport(::taihe::callback_view<void( |
| + ::ohos::bluetooth::hid::SetReportData const& data)> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->setReportLock_); |
| + auto eventCb = ::taihe::optional<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::SetReportData const& data)>>{std::in_place_t{}, callback}; |
| + auto& callbackVec = observer_->setReportVec_; |
| + if (std::find(callbackVec.begin(), callbackVec.end(), eventCb) != callbackVec.end()) { |
| + return; |
| + } |
| + callbackVec.emplace_back(eventCb); |
| + } |
| + } |
| + |
| + void OffSetReport(::taihe::optional_view<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::SetReportData const& data)>> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->setReportLock_); |
| + auto& callbackVec = observer_->setReportVec_; |
| + if (callback.has_value()) { |
| + callbackVec.erase( |
| + std::remove_if(callbackVec.begin(), callbackVec.end(), |
| + [callback](const ::taihe::optional< |
| + ::taihe::callback<void(::ohos::bluetooth::hid::SetReportData const& data)>>& it) { |
| + return it == callback; |
| + }), |
| + callbackVec.end()); |
| + } else { |
| + callbackVec.clear(); |
| + } |
| + } |
| + } |
| + |
| + void OnGetReport(::taihe::callback_view<void( |
| + ::ohos::bluetooth::hid::GetReportData const& data)> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->getReportLock_); |
| + auto eventCb = ::taihe::optional<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::GetReportData const& data)>>{std::in_place_t{}, callback}; |
| + auto& callbackVec = observer_->getReportVec_; |
| + if (std::find(callbackVec.begin(), callbackVec.end(), eventCb) != callbackVec.end()) { |
| + return; |
| + } |
| + callbackVec.emplace_back(eventCb); |
| + } |
| + } |
| + |
| + void OffGetReport(::taihe::optional_view<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::GetReportData const& data)>> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->getReportLock_); |
| + auto& callbackVec = observer_->getReportVec_; |
| + if (callback.has_value()) { |
| + callbackVec.erase( |
| + std::remove_if(callbackVec.begin(), callbackVec.end(), |
| + [callback](const ::taihe::optional< |
| + ::taihe::callback<void(::ohos::bluetooth::hid::GetReportData const& data)>>& it) { |
| + return it == callback; |
| + }), |
| + callbackVec.end()); |
| + } else { |
| + callbackVec.clear(); |
| + } |
| + } |
| + } |
| + |
| + void OnVirtualCableUnplug(::taihe::callback_view<void(::ohos::bluetooth::hid::UndefinedType const& data)> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->virtualCableUnplugLock_); |
| + auto eventCb = ::taihe::optional<::taihe::callback<void( |
| + ::ohos::bluetooth::hid::UndefinedType const& data)>>{std::in_place_t{}, callback}; |
| + auto& callbackVec = observer_->virtualCableUnplugVec_; |
| + if (std::find(callbackVec.begin(), callbackVec.end(), eventCb) != callbackVec.end()) { |
| + return; |
| + } |
| + callbackVec.emplace_back(eventCb); |
| + } |
| + } |
| + |
| + void OffVirtualCableUnplug(::taihe::optional_view<::taihe::callback<void(::ohos::bluetooth::hid::UndefinedType const& data)>> callback) |
| + { |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->virtualCableUnplugLock_); |
| + auto& callbackVec = observer_->virtualCableUnplugVec_; |
| + if (callback.has_value()) { |
| + callbackVec.erase( |
| + std::remove_if(callbackVec.begin(), callbackVec.end(), |
| + [callback](const ::taihe::optional< |
| + ::taihe::callback<void(::ohos::bluetooth::hid::UndefinedType const& data)>>& it) { |
| + return it == callback; |
| + }), |
| + callbackVec.end()); |
| + } else { |
| + callbackVec.clear(); |
| + } |
| + } |
| + } |
| + |
| + void Connect(::ohos::bluetooth::common::BluetoothAddress const& deviceId) |
| + { |
| + HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INVALID_STATE, |
| + BT_ERR_PROFILE_DISABLED, BT_ERR_INTERNAL_ERROR, BT_ERR_HID_APP_NOT_REGISTER }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + AddressInfo addressInfo; |
| + addressInfo.SetAddress(std::string(deviceId.address)); |
| + addressInfo.SetAddressType(static_cast<uint8_t>(deviceId.addressType)); |
| + addressInfo.SetRawAddressType(static_cast<uint8_t>(deviceId.rawAddressType.value_or(::ohos::bluetooth::common::BluetoothRawAddressType::key_t::PUBLIC))); |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + int32_t errorCode = profile->Connect(addressInfo); |
| + HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| + } |
| + |
| + void Disconnect() |
| + { |
| + HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INVALID_STATE, |
| + BT_ERR_INTERNAL_ERROR, BT_ERR_HID_APP_NOT_REGISTER }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + int32_t errorCode = profile->Disconnect(); |
| + HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| + } |
| + |
| + void ReportError(::ohos::bluetooth::hid::ErrorReason error) |
| + { |
| + HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INVALID_STATE, |
| + BT_ERR_INTERNAL_ERROR, BT_ERR_HID_APP_NOT_REGISTER, BT_ERR_HID_DEVICE_NOT_CONNECTED }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + int32_t errorCode = profile->ReportError(static_cast<ErrorReason>(error.get_value())); |
| + HILOGI("errorCode:%{public}d", errorCode); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| + } |
| + |
| + void UnregisterHidDevice() |
| + { |
| + HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INVALID_STATE, |
| + BT_ERR_INTERNAL_ERROR, BT_ERR_HID_APP_NOT_REGISTER }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->appStatusChangeLock_); |
| + observer_->appStatusChangeVec_.clear(); |
| + } |
| + int32_t errorCode = profile->UnregisterHidDevice(); |
| + HILOGI("errorCode:%{public}d", errorCode); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| + } |
| + |
| + void SendReport(int32_t id, ::taihe::array_view<uint8_t> reportData) |
| + { |
| + HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INVALID_STATE, |
| + BT_ERR_INTERNAL_ERROR, BT_ERR_HID_APP_NOT_REGISTER, BT_ERR_HID_DEVICE_NOT_CONNECTED }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + std::vector<uint8_t> data; |
| + if (reportData.size() > 0) { |
| + data.assign(reportData.data(), reportData.data() + reportData.size()); |
| + } |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + int32_t errorCode = profile->SendReport(id, data); |
| + HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| + } |
| + |
| + void ReplyReport(::ohos::bluetooth::hid::ReportType type, int32_t id, ::taihe::array_view<uint8_t> reportData) |
| + { |
| + HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INVALID_STATE, |
| + BT_ERR_INTERNAL_ERROR, BT_ERR_HID_APP_NOT_REGISTER, BT_ERR_HID_DEVICE_NOT_CONNECTED }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + std::vector<uint8_t> data; |
| + if (reportData.size() > 0) { |
| + data.assign(reportData.data(), reportData.data() + reportData.size()); |
| + } |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + int32_t errorCode = profile->ReplyReport(static_cast<ReportType>(type.get_value()), id, data); |
| + HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| + } |
| + |
| + void RegisterHidDevice(::ohos::bluetooth::hid::HidDeviceSdp const& sdp, |
| + ::ohos::bluetooth::hid::HidDeviceQos const& inQos, |
| + ::ohos::bluetooth::hid::HidDeviceQos const& outQos, |
| + ::taihe::callback_view<void(bool data)> callback) |
| + { |
| + HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INVALID_STATE, |
| + BT_ERR_INTERNAL_ERROR, BT_ERR_HID_APPLICATION_NOT_IN_FOREGROUND, |
| + BT_ERR_HID_APP_HAS_BEEN_REGISTERED, BT_ERR_HID_APP_NOT_REGISTER, |
| + BT_ERR_HID_DEVICE_NOT_CONNECTED }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + |
| + |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(IsValidSDPSettingParam(std::string(sdp.name), MAX_NAME_SIZE), |
| + BT_ERR_INVALID_PARAM); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(IsValidSDPSettingParam(std::string(sdp.description), MAX_DESCRIPTION_SIZE), |
| + BT_ERR_INVALID_PARAM); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(IsValidSDPSettingParam(std::string(sdp.provider), MAX_PROVIDER_SIZE), |
| + BT_ERR_INVALID_PARAM); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(IsValidSubclass(sdp.subclass.get_value()), |
| + BT_ERR_INVALID_PARAM); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(sdp.descriptors.size() > 0 && sdp.descriptors.size() <= MAX_DESCRIPTOR_SIZE, |
| + BT_ERR_INVALID_PARAM); |
| + |
| + |
| + if (inQos.serviceType.has_value()) { |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(IsValidServiceType(inQos.serviceType.value().get_value()), |
| + BT_ERR_INVALID_PARAM); |
| + } |
| + if (outQos.serviceType.has_value()) { |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(IsValidServiceType(outQos.serviceType.value().get_value()), |
| + BT_ERR_INVALID_PARAM); |
| + } |
| + |
| + BluetoothHidDeviceSdp sdpSetting( |
| + bluetooth::SdpSetting(std::string(sdp.name), std::string(sdp.description), |
| + std::string(sdp.provider), sdp.subclass.get_value(), |
| + std::vector<uint8_t>(sdp.descriptors.data(), sdp.descriptors.data() + sdp.descriptors.size()))); |
| + auto convertQos = [](::ohos::bluetooth::hid::HidDeviceQos const& qos) { |
| + int32_t serviceType = static_cast<int32_t>(ServiceType::SERVICE_BEST_EFFORT); |
| + if (qos.serviceType.has_value()) { |
| + serviceType = static_cast<int32_t>(qos.serviceType.value().get_key()); |
| + } |
| + int32_t tokenRate = qos.tokenRate.value_or(0); |
| + int32_t tokenBucketSize = qos.tokenBucketSize.value_or(0); |
| + int32_t peakBandwidth = qos.peakBandwidth.value_or(0); |
| + int32_t latency = qos.latency.value_or(-1); |
| + int32_t delayVariation = qos.delayVariation.value_or(-1); |
| + return BluetoothHidDeviceQos( |
| + bluetooth::QosSetting(serviceType, tokenRate, tokenBucketSize, |
| + peakBandwidth, latency, delayVariation)); |
| + }; |
| + BluetoothHidDeviceQos inQosSetting = convertQos(inQos); |
| + BluetoothHidDeviceQos outQosSetting = convertQos(outQos); |
| + |
| + HILOGI("sdpSetting: name_=%{public}s, description_=%{public}s, provider_=%{public}s, " |
| + "subclass_=%{public}d, sdpSettings_.size=%{public}zu", |
| + sdpSetting.name_.c_str(), sdpSetting.description_.c_str(), |
| + sdpSetting.provider_.c_str(), sdpSetting.subclass_, |
| + sdpSetting.sdpSettings_.size()); |
| + HILOGI("inQosSetting: serviceType_=%{public}d, tokenRate_=%{public}d, " |
| + "tokenBucketSize_=%{public}d, peakBandwidth_=%{public}d, latency_=%{public}d, delayVariation_=%{public}d", |
| + inQosSetting.serviceType_, inQosSetting.tokenRate_, |
| + inQosSetting.tokenBucketSize_, inQosSetting.peakBandwidth_, |
| + inQosSetting.latency_, inQosSetting.delayVariation_); |
| + HILOGI("outQosSetting: serviceType_=%{public}d, tokenRate_=%{public}d, " |
| + "tokenBucketSize_=%{public}d, peakBandwidth_=%{public}d, latency_=%{public}d, delayVariation_=%{public}d", |
| + outQosSetting.serviceType_, outQosSetting.tokenRate_, |
| + outQosSetting.tokenBucketSize_, outQosSetting.peakBandwidth_, |
| + outQosSetting.latency_, outQosSetting.delayVariation_); |
| + |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + int32_t errorCode = profile->RegisterHidDevice(sdpSetting, inQosSetting, outQosSetting); |
| + HILOGI("errorCode:%{public}s", GetTaiheErrMsg(errorCode).c_str()); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(errorCode == BT_NO_ERROR, errorCode); |
| + if (observer_) { |
| + std::unique_lock<std::shared_mutex> guard(observer_->appStatusChangeLock_); |
| + auto eventCb = ::taihe::optional<::taihe::callback<void(bool)>>{std::in_place_t{}, callback}; |
| + auto& callbackVec = observer_->appStatusChangeVec_; |
| + if (std::find(callbackVec.begin(), callbackVec.end(), eventCb) != callbackVec.end()) { |
| + return; |
| + } |
| + callbackVec.emplace_back(eventCb); |
| + } |
| + } |
| + |
| + ::ohos::bluetooth::constant::ProfileConnectionState GetConnectionState(::taihe::string_view deviceId) |
| + { |
| + HILOGD("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + std::string remoteAddr = std::string(deviceId); |
| + bool checkRet = CheckDeviceIdParam(remoteAddr); |
| + TAIHE_BT_ASSERT_RETURN_VERIFY(checkRet, BT_ERR_INVALID_PARAM, |
| + ::ohos::bluetooth::constant::ProfileConnectionState::from_value(0)); |
| + |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR); |
| + int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED); |
| + int32_t errorCode = profile->GetConnectionState(device, state); |
| + int32_t profileState = TaiheUtils::GetProfileConnectionState(state); |
| + TAIHE_BT_ASSERT_RETURN_VERIFY(errorCode == BT_NO_ERROR, errorCode, |
| + ::ohos::bluetooth::constant::ProfileConnectionState::from_value(profileState)); |
| + return ::ohos::bluetooth::constant::ProfileConnectionState::from_value(profileState); |
| + } |
| + |
| + ::taihe::array<::taihe::string> GetConnectedDevices() |
| + { |
| + HILOGI("enter"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + std::vector<BluetoothRemoteDevice> devices; |
| + int errorCode = profile->GetConnectedDevices(devices); |
| + HILOGI("errorCode:%{public}s, devices size:%{public}zu", |
| + GetTaiheErrMsg(errorCode).c_str(), devices.size()); |
| + TAIHE_BT_ASSERT_RETURN_VERIFY(errorCode == BT_NO_ERROR, errorCode, taihe::array<taihe::string>{}); |
| + |
| + std::vector<std::string> deviceVector; |
| + for (auto &device : devices) { |
| + deviceVector.push_back(device.GetDeviceAddr()); |
| + } |
| + return deviceVector.empty() ? taihe::array<taihe::string>{} |
| + : taihe::array<::taihe::string>(taihe::copy_data_t{}, deviceVector.data(), deviceVector.size()); |
| + } |
| + |
| + ::ohos::bluetooth::baseProfile::ConnectionStrategy GetConnectionStrategySync(::taihe::string_view deviceId) |
| + { |
| + HILOGD("start"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + std::string remoteAddr = std::string(deviceId); |
| + bool checkRet = CheckDeviceIdParam(remoteAddr); |
| + TAIHE_BT_ASSERT_RETURN_VERIFY(checkRet, BT_ERR_INVALID_PARAM, |
| + ::ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(0)); |
| + |
| + BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR); |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + int strategy = 0; |
| + int32_t err = profile->GetConnectStrategy(remoteDevice, strategy); |
| + HILOGI("err: %{public}d, strategy: %{public}d", err, strategy); |
| + TAIHE_BT_ASSERT_RETURN_VERIFY(err == BT_NO_ERROR, err, |
| + ::ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(strategy)); |
| + return ::ohos::bluetooth::baseProfile::ConnectionStrategy::from_value(strategy); |
| + } |
| + |
| + void SetConnectionStrategySync(::taihe::string_view deviceId, |
| + ::ohos::bluetooth::baseProfile::ConnectionStrategy strategy) |
| + { |
| + HILOGD("start"); |
| + std::vector<int32_t> validErrCodes = { BT_ERR_INVALID_PARAM, BT_ERR_INTERNAL_ERROR }; |
| + TAIHE_BT_CONTEXT_WITHOUT_HA(validErrCodes); |
| + std::string remoteAddr = std::string(deviceId); |
| + int32_t connStrategy = strategy.get_value(); |
| + bool checkRet = CheckSetConnectStrategyParam(remoteAddr, connStrategy); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(checkRet, BT_ERR_INVALID_PARAM); |
| + |
| + BluetoothRemoteDevice remoteDevice(remoteAddr, BT_TRANSPORT_BREDR); |
| + HidDevice *profile = HidDevice::GetProfile(); |
| + int32_t err = profile->SetConnectStrategy(remoteDevice, connStrategy); |
| + HILOGI("err: %{public}d", err); |
| + TAIHE_BT_ASSERT_RETURN_VOID_VERIFY(err == BT_NO_ERROR, err); |
| + } |
| + |
| +private: |
| + std::shared_ptr<TaiheBluetoothHidDeviceObserver> observer_ = nullptr; |
| +}; |
| + |
| +::ohos::bluetooth::hid::HidDeviceProfile CreateHidDeviceProfile() |
| +{ |
| + return taihe::make_holder<HidDeviceProfileImpl, ::ohos::bluetooth::hid::HidDeviceProfile>(); |
| +} |
| } | } |
| } | } |
| | |
| |
| |
| TH_EXPORT_CPP_API_CreateHidHostProfile(OHOS::Bluetooth::CreateHidHostProfile); | TH_EXPORT_CPP_API_CreateHidHostProfile(OHOS::Bluetooth::CreateHidHostProfile); |
| +TH_EXPORT_CPP_API_CreateHidDeviceProfile(OHOS::Bluetooth::CreateHidDeviceProfile); |
| |
| |