• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
23 namespace HDI {
24 namespace Bluetooth {
25 namespace Hci {
26 namespace V1_0 {
27 using VendorInterface = OHOS::HDI::Bluetooth::Hci::V1_0::VendorInterface;
28 using HciPacketType = OHOS::HDI::Bluetooth::Hci::HciPacketType;
29 
HciInterfaceImplGetInstance(void)30 extern "C" IHciInterface *HciInterfaceImplGetInstance(void)
31 {
32     return new (std::nothrow) HciInterfaceImpl();
33 }
34 
HciInterfaceImpl()35 HciInterfaceImpl::HciInterfaceImpl()
36 {
37     remoteDeathRecipient_ =
38         new RemoteDeathRecipient(std::bind(&HciInterfaceImpl::OnRemoteDied, this, std::placeholders::_1));
39 }
40 
~HciInterfaceImpl()41 HciInterfaceImpl::~HciInterfaceImpl()
42 {
43     if (callbacks_ != nullptr) {
44         RemoveHciDeathRecipient(callbacks_);
45         callbacks_ = nullptr;
46     }
47 }
48 
Init(const sptr<IHciCallback> & callbackObj)49 int32_t HciInterfaceImpl::Init(const sptr<IHciCallback>& callbackObj)
50 {
51     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
52     if (callbackObj == nullptr) {
53         HDF_LOGE("HciInterfaceImpl %{public}s callbackObj null", __func__);
54         return HDF_FAILURE;
55     }
56 
57     VendorInterface::ReceiveCallback callback = {
58         .onAclReceive =
59             [callbackObj](
60                 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::ACL_DATA, packet); },
61         .onScoReceive =
62             [callbackObj](
63                 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::SCO_DATA, packet); },
64         .onEventReceive =
65             [callbackObj](
66                 const std::vector<uint8_t> &packet) { callbackObj->OnReceivedHciPacket(BtType::HCI_EVENT, packet); },
67     };
68 
69     bool result = VendorInterface::GetInstance()->Initialize(
70         [callbackObj](bool status) { callbackObj->OnInited(status ? BtStatus::SUCCESS : BtStatus::INITIAL_ERROR); },
71         callback);
72     if (result) {
73         callbacks_ = callbackObj;
74         AddHciDeathRecipient(callbacks_);
75     }
76     return result ? HDF_SUCCESS : HDF_FAILURE;
77 }
78 
SendHciPacket(BtType type,const std::vector<uint8_t> & data)79 int32_t HciInterfaceImpl::SendHciPacket(BtType type, const std::vector<uint8_t>& data)
80 {
81     HDF_LOGI("HciInterfaceImpl %{public}s, %{public}d", __func__, type);
82     if (data.empty()) {
83         return HDF_FAILURE;
84     }
85 
86     size_t result = VendorInterface::GetInstance()->SendPacket(static_cast<HciPacketType>(type), data);
87     return result ? HDF_SUCCESS : HDF_FAILURE;
88 }
89 
Close()90 int32_t HciInterfaceImpl::Close()
91 {
92     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
93     if (callbacks_ != nullptr) {
94         RemoveHciDeathRecipient(callbacks_);
95         callbacks_ = nullptr;
96     }
97     VendorInterface::GetInstance()->CleanUp();
98     VendorInterface::DestroyInstance();
99     return HDF_SUCCESS;
100 }
101 
OnRemoteDied(const wptr<IRemoteObject> & object)102 void HciInterfaceImpl::OnRemoteDied(const wptr<IRemoteObject> &object)
103 {
104     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
105     callbacks_ = nullptr;
106     VendorInterface::GetInstance()->CleanUp();
107     VendorInterface::DestroyInstance();
108 }
109 
AddHciDeathRecipient(const sptr<IHciCallback> & callbackObj)110 int32_t HciInterfaceImpl::AddHciDeathRecipient(const sptr<IHciCallback>& callbackObj)
111 {
112     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
113     const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IHciCallback>(callbackObj);
114     bool result = remote->AddDeathRecipient(remoteDeathRecipient_);
115     if (!result) {
116         HDF_LOGE("HciInterfaceImpl AddDeathRecipient fail");
117         return HDF_FAILURE;
118     }
119     return HDF_SUCCESS;
120 }
121 
RemoveHciDeathRecipient(const sptr<IHciCallback> & callbackObj)122 int32_t HciInterfaceImpl::RemoveHciDeathRecipient(const sptr<IHciCallback>& callbackObj)
123 {
124     HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
125     const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IHciCallback>(callbackObj);
126     bool result = remote->RemoveDeathRecipient(remoteDeathRecipient_);
127     if (!result) {
128         HDF_LOGE("HciInterfaceImpl RemoveDeathRecipient fail");
129         return HDF_FAILURE;
130     }
131     return HDF_SUCCESS;
132 }
133 } // V1_0
134 } // Hci
135 } // Bluetooth
136 } // HDI
137 } // OHOS
138