1 /*
2  * Copyright (C) 2021-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 #ifndef OHOS_HDI_BLUETOOTH_HCI_V1_0_VENDOR_INTERFACE_H
17 #define OHOS_HDI_BLUETOOTH_HCI_V1_0_VENDOR_INTERFACE_H
18 
19 #include <functional>
20 #include <vector>
21 
22 #include "singleton.h"
23 
24 #include "hci_internal.h"
25 #include "hci_protocol.h"
26 #include "hci_watcher.h"
27 #include "ohos_bt_vendor_lib.h"
28 
29 namespace OHOS {
30 namespace HDI {
31 namespace Bluetooth {
32 namespace Hci {
33 namespace V1_0 {
34 class VendorInterface : public DelayedSingleton<VendorInterface> {
35 public:
36     using InitializeCompleteCallback = std::function<void(bool isSuccess)>;
37     using ReceiveDataCallback = Hci::HciProtocol::HciDataCallback;
38     struct ReceiveCallback {
39         ReceiveDataCallback onAclReceive;
40         ReceiveDataCallback onScoReceive;
41         ReceiveDataCallback onEventReceive;
42     };
43 
44     bool Initialize(InitializeCompleteCallback initializeCompleteCallback, const ReceiveCallback &receiveCallback);
45     void CleanUp();
46     size_t SendPacket(Hci::HciPacketType type, const std::vector<uint8_t> &packet);
47 
48 private:
49     static void OnInitCallback(BtOpResultT result);
50     static void* OnMallocCallback(int size);
51     static void OnFreeCallback(void* buf);
52     static size_t OnCmdXmitCallback(uint16_t opcode, void* buf);
53 
54     void OnEventReceived(const std::vector<uint8_t> &data);
55     bool WatchHciChannel(const ReceiveCallback &receiveCallback);
56     void WatcherTimeout();
57 
58 private:
59     InitializeCompleteCallback initializeCompleteCallback_;
60     ReceiveDataCallback eventDataCallback_;
61     void* vendorHandle_ = nullptr;
62     BtVendorInterfaceT *vendorInterface_ = nullptr;
63     static BtVendorCallbacksT vendorCallbacks_;
64     HciWatcher watcher_;
65     std::shared_ptr<Hci::HciProtocol> hci_ = nullptr;
66     uint16_t vendorSentOpcode_ = 0;
67     uint32_t lpmTimer_ = 0;
68     std::mutex wakeupMutex_;
69     bool wakeupLock_ = false;
70     bool activity_ = false;
71     std::mutex initAndCleanupProcessMutex_;
72 
73     DECLARE_DELAYED_SINGLETON(VendorInterface);
74     DISALLOW_COPY_AND_MOVE(VendorInterface);
75 };
76 }  // namespace V1_0
77 }  // namespace Hci
78 }  // namespace Bluetooth
79 }  // namespace HDI
80 }  // namespace OHOS
81 #endif /* OHOS_HDI_BLUETOOTH_HCI_V1_0_VENDOR_INTERFACE_H */