1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hci_interface_impl.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 #include <iproxy_broker.h>
20 #include "vendor_interface.h"
21
22 #ifdef LOG_DOMAIN
23 #undef LOG_DOMAIN
24 #endif
25 #define LOG_DOMAIN 0xD000105
26
27 namespace OHOS {
28 namespace HDI {
29 namespace Bluetooth {
30 namespace Hci {
31 namespace V1_0 {
32 using VendorInterface = OHOS::HDI::Bluetooth::Hci::V1_0::VendorInterface;
33 using HciPacketType = OHOS::HDI::Bluetooth::Hci::HciPacketType;
34
HciInterfaceImplGetInstance(void)35 extern "C" IHciInterface *HciInterfaceImplGetInstance(void)
36 {
37 return new (std::nothrow) HciInterfaceImpl();
38 }
39
HciInterfaceImpl()40 HciInterfaceImpl::HciInterfaceImpl()
41 {
42 remoteDeathRecipient_ =
43 new RemoteDeathRecipient(std::bind(&HciInterfaceImpl::OnRemoteDied, this, std::placeholders::_1));
44 }
45
~HciInterfaceImpl()46 HciInterfaceImpl::~HciInterfaceImpl()
47 {
48 if (callbacks_ != nullptr) {
49 RemoveHciDeathRecipient(callbacks_);
50 callbacks_ = nullptr;
51 }
52 }
53
Init(const sptr<IHciCallback> & callbackObj)54 int32_t HciInterfaceImpl::Init(const sptr<IHciCallback>& callbackObj)
55 {
56 HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
57 if (callbackObj == nullptr) {
58 HDF_LOGE("HciInterfaceImpl %{public}s callbackObj null", __func__);
59 return HDF_FAILURE;
60 }
61
62 AddHciDeathRecipient(callbackObj);
63 callbacks_ = callbackObj;
64
65 VendorInterface::ReceiveCallback callback = {
66 .onAclReceive =
67 [callbackObj](
68 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::ACL_DATA, packet); },
69 .onScoReceive =
70 [callbackObj](
71 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::SCO_DATA, packet); },
72 .onEventReceive =
73 [callbackObj](
74 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::HCI_EVENT, packet); },
75 .onIsoReceive =
76 [callbackObj](
77 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::ISO_DATA, packet); },
78 };
79
80 bool result = VendorInterface::GetInstance()->Initialize(
81 [callbackObj](bool status) { callbackObj->OnInited(status ? BtStatus::SUCCESS : BtStatus::INITIAL_ERROR); },
82 callback);
83 if (!result) {
84 RemoveHciDeathRecipient(callbackObj);
85 callbacks_ = nullptr;
86 }
87 return result ? HDF_SUCCESS : HDF_FAILURE;
88 }
89
SendHciPacket(BtType type,const std::vector<uint8_t> & data)90 int32_t HciInterfaceImpl::SendHciPacket(BtType type, const std::vector<uint8_t>& data)
91 {
92 HDF_LOGD("HciInterfaceImpl %{public}s, %{public}d", __func__, type);
93 if (data.empty()) {
94 return HDF_FAILURE;
95 }
96
97 size_t result = VendorInterface::GetInstance()->SendPacket(static_cast<HciPacketType>(type), data);
98 return result ? HDF_SUCCESS : HDF_FAILURE;
99 }
100
Close()101 int32_t HciInterfaceImpl::Close()
102 {
103 HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
104 if (callbacks_ != nullptr) {
105 RemoveHciDeathRecipient(callbacks_);
106 callbacks_ = nullptr;
107 }
108 VendorInterface::GetInstance()->CleanUp();
109 return HDF_SUCCESS;
110 }
111
OnRemoteDied(const wptr<IRemoteObject> & object)112 void HciInterfaceImpl::OnRemoteDied(const wptr<IRemoteObject> &object)
113 {
114 HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
115 callbacks_ = nullptr;
116 VendorInterface::GetInstance()->CleanUp();
117 }
118
AddHciDeathRecipient(const sptr<IHciCallback> & callbackObj)119 int32_t HciInterfaceImpl::AddHciDeathRecipient(const sptr<IHciCallback>& callbackObj)
120 {
121 HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
122 const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IHciCallback>(callbackObj);
123 bool result = remote->AddDeathRecipient(remoteDeathRecipient_);
124 if (!result) {
125 HDF_LOGE("HciInterfaceImpl AddDeathRecipient fail");
126 return HDF_FAILURE;
127 }
128 return HDF_SUCCESS;
129 }
130
RemoveHciDeathRecipient(const sptr<IHciCallback> & callbackObj)131 int32_t HciInterfaceImpl::RemoveHciDeathRecipient(const sptr<IHciCallback>& callbackObj)
132 {
133 HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
134 const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IHciCallback>(callbackObj);
135 bool result = remote->RemoveDeathRecipient(remoteDeathRecipient_);
136 if (!result) {
137 HDF_LOGE("HciInterfaceImpl RemoveDeathRecipient fail");
138 return HDF_FAILURE;
139 }
140 return HDF_SUCCESS;
141 }
142 } // V1_0
143 } // Hci
144 } // Bluetooth
145 } // HDI
146 } // OHOS
147