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
27namespace OHOS {
28namespace HDI {
29namespace Bluetooth {
30namespace Hci {
31namespace V1_0 {
32using VendorInterface = OHOS::HDI::Bluetooth::Hci::V1_0::VendorInterface;
33using HciPacketType = OHOS::HDI::Bluetooth::Hci::HciPacketType;
34
35extern "C" IHciInterface *HciInterfaceImplGetInstance(void)
36{
37    return new (std::nothrow) HciInterfaceImpl();
38}
39
40HciInterfaceImpl::HciInterfaceImpl()
41{
42    remoteDeathRecipient_ =
43        new RemoteDeathRecipient(std::bind(&HciInterfaceImpl::OnRemoteDied, this, std::placeholders::_1));
44}
45
46HciInterfaceImpl::~HciInterfaceImpl()
47{
48    if (callbacks_ != nullptr) {
49        RemoveHciDeathRecipient(callbacks_);
50        callbacks_ = nullptr;
51    }
52}
53
54int32_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    };
76
77    bool result = VendorInterface::GetInstance()->Initialize(
78        [callbackObj](bool status) { callbackObj->OnInited(status ? BtStatus::SUCCESS : BtStatus::INITIAL_ERROR); },
79        callback);
80    if (!result) {
81        RemoveHciDeathRecipient(callbackObj);
82        callbacks_ = nullptr;
83    }
84    return result ? HDF_SUCCESS : HDF_FAILURE;
85}
86
87int32_t HciInterfaceImpl::SendHciPacket(BtType type, const std::vector<uint8_t>& data)
88{
89    HDF_LOGD("HciInterfaceImpl %{public}s, %{public}d", __func__, type);
90    if (data.empty()) {
91        return HDF_FAILURE;
92    }
93
94    size_t result = VendorInterface::GetInstance()->SendPacket(static_cast<HciPacketType>(type), data);
95    return result ? HDF_SUCCESS : HDF_FAILURE;
96}
97
98int32_t HciInterfaceImpl::Close()
99{
100    HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
101    if (callbacks_ != nullptr) {
102        RemoveHciDeathRecipient(callbacks_);
103        callbacks_ = nullptr;
104    }
105    VendorInterface::GetInstance()->CleanUp();
106    VendorInterface::DestroyInstance();
107    return HDF_SUCCESS;
108}
109
110void HciInterfaceImpl::OnRemoteDied(const wptr<IRemoteObject> &object)
111{
112    HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
113    callbacks_ = nullptr;
114    VendorInterface::GetInstance()->CleanUp();
115    VendorInterface::DestroyInstance();
116}
117
118int32_t HciInterfaceImpl::AddHciDeathRecipient(const sptr<IHciCallback>& callbackObj)
119{
120    HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
121    const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IHciCallback>(callbackObj);
122    bool result = remote->AddDeathRecipient(remoteDeathRecipient_);
123    if (!result) {
124        HDF_LOGE("HciInterfaceImpl AddDeathRecipient fail");
125        return HDF_FAILURE;
126    }
127    return HDF_SUCCESS;
128}
129
130int32_t HciInterfaceImpl::RemoveHciDeathRecipient(const sptr<IHciCallback>& callbackObj)
131{
132    HDF_LOGI("HciInterfaceImpl %{public}s", __func__);
133    const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IHciCallback>(callbackObj);
134    bool result = remote->RemoveDeathRecipient(remoteDeathRecipient_);
135    if (!result) {
136        HDF_LOGE("HciInterfaceImpl RemoveDeathRecipient fail");
137        return HDF_FAILURE;
138    }
139    return HDF_SUCCESS;
140}
141} // V1_0
142} // Hci
143} // Bluetooth
144} // HDI
145} // OHOS
146