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 "nocopyable.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 { 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 ReceiveDataCallback onIsoReceive; 43 }; 44 45 static VendorInterface *GetInstance(); 46 bool Initialize(InitializeCompleteCallback initializeCompleteCallback, const ReceiveCallback &receiveCallback); 47 void CleanUp(); 48 size_t SendPacket(Hci::HciPacketType type, const std::vector<uint8_t> &packet); 49 50 private: 51 VendorInterface(); 52 ~VendorInterface(); 53 54 static void OnInitCallback(BtOpResultT result); 55 static void* OnMallocCallback(int size); 56 static void OnFreeCallback(void* buf); 57 static size_t OnCmdXmitCallback(uint16_t opcode, void* buf); 58 59 void OnEventReceived(const std::vector<uint8_t> &data); 60 bool WatchHciChannel(const ReceiveCallback &receiveCallback); 61 void WatcherTimeout(); 62 63 private: 64 InitializeCompleteCallback initializeCompleteCallback_; 65 ReceiveDataCallback eventDataCallback_; 66 void* vendorHandle_ = nullptr; 67 BtVendorInterfaceT *vendorInterface_ = nullptr; 68 static BtVendorCallbacksT vendorCallbacks_; 69 std::shared_ptr<HciWatcher> watcher_; 70 std::shared_ptr<Hci::HciProtocol> hci_ = nullptr; 71 uint16_t vendorSentOpcode_ = 0; 72 uint32_t lpmTimer_ = 0; 73 std::mutex wakeupMutex_; 74 bool wakeupLock_ = false; 75 bool activity_ = false; 76 std::mutex initAndCleanupProcessMutex_; 77 78 DISALLOW_COPY_AND_MOVE(VendorInterface); 79 }; 80 } // namespace V1_0 81 } // namespace Hci 82 } // namespace Bluetooth 83 } // namespace HDI 84 } // namespace OHOS 85 #endif /* OHOS_HDI_BLUETOOTH_HCI_V1_0_VENDOR_INTERFACE_H */