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     VendorInterface::ReceiveCallback callback = {
63         .onAclReceive =
64             [callbackObj](
65                 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::ACL_DATA, packet); },
66         .onScoReceive =
67             [callbackObj](
68                 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::SCO_DATA, packet); },
69         .onEventReceive =
70             [callbackObj](
71                 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::HCI_EVENT, packet); },
72     };
73 
74     bool result = VendorInterface::GetInstance()->Initialize(
75         [callbackObj](bool status) { callbackObj->OnInited(status ? BtStatus::SUCCESS : BtStatus::INITIAL_ERROR); },
76         callback);
77     if (result) {
78         callbacks_ = callbackObj;
79         AddHciDeathRecipient(callbacks_);
80     }
81     return result ? HDF_SUCCESS : HDF_FAILURE;
82 }
83 
SendHciPacket(BtType type,const std::vector<uint8_t> & data)84 int32_t HciInterfaceImpl::SendHciPacket(BtType type, const std::vector<uint8_t>& data)
85 {
86     HDF_LOGD("HciInterfaceImpl %{public}s, %{public}d", __func__, type);
87     if (data.empty()) {
88         return HDF_FAILURE;
89     }
90 
91     size_t result = VendorInterface::GetInstance()->SendPacket(static_cast<HciPacketType>(type), data);
92     return result ? HDF_SUCCESS : HDF_FAILURE;
93 }
94 
Close()95 int32_t HciInterfaceImpl::Close()
96 {
97     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
98     if (callbacks_ != nullptr) {
99         RemoveHciDeathRecipient(callbacks_);
100         callbacks_ = nullptr;
101     }
102     VendorInterface::GetInstance()->CleanUp();
103     VendorInterface::DestroyInstance();
104     return HDF_SUCCESS;
105 }
106 
OnRemoteDied(const wptr<IRemoteObject> & object)107 void HciInterfaceImpl::OnRemoteDied(const wptr<IRemoteObject> &object)
108 {
109     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
110     callbacks_ = nullptr;
111     VendorInterface::GetInstance()->CleanUp();
112     VendorInterface::DestroyInstance();
113 }
114 
AddHciDeathRecipient(const sptr<IHciCallback> & callbackObj)115 int32_t HciInterfaceImpl::AddHciDeathRecipient(const sptr<IHciCallback>& callbackObj)
116 {
117     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
118     const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IHciCallback>(callbackObj);
119     bool result = remote->AddDeathRecipient(remoteDeathRecipient_);
120     if (!result) {
121         HDF_LOGE("HciInterfaceImpl AddDeathRecipient fail");
122         return HDF_FAILURE;
123     }
124     return HDF_SUCCESS;
125 }
126 
RemoveHciDeathRecipient(const sptr<IHciCallback> & callbackObj)127 int32_t HciInterfaceImpl::RemoveHciDeathRecipient(const sptr<IHciCallback>& callbackObj)
128 {
129     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
130     const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IHciCallback>(callbackObj);
131     bool result = remote->RemoveDeathRecipient(remoteDeathRecipient_);
132     if (!result) {
133         HDF_LOGE("HciInterfaceImpl RemoveDeathRecipient fail");
134         return HDF_FAILURE;
135     }
136     return HDF_SUCCESS;
137 }
138 } // V1_0
139 } // Hci
140 } // Bluetooth
141 } // HDI
142 } // OHOS
143