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 #ifndef HID_HOST_SERVICE_H 17 #define HID_HOST_SERVICE_H 18 19 #include <list> 20 #include <mutex> 21 #include <vector> 22 #include <memory.h> 23 #include <cmath> 24 #include <cstring> 25 #include <mutex> 26 #include "log.h" 27 #include "log_util.h" 28 #include "packet.h" 29 #include "securec.h" 30 #include "adapter_config.h" 31 #include "base_observer_list.h" 32 #include "class_creator.h" 33 #include "context.h" 34 #include "interface_profile_hid_host.h" 35 36 #include "profile_config.h" 37 #include "profile_service_manager.h" 38 #include "raw_address.h" 39 #include "hid_host_message.h" 40 #include "base_def.h" 41 #include "hid_host_statemachine.h" 42 43 namespace OHOS { 44 namespace bluetooth { 45 class HidHostService : public IProfileHidHost, public utility::Context { 46 public: 47 /** 48 * @brief Get the instance of the HfpHfService object. 49 * 50 * @return Returns the instance of the HfpHfService object. 51 */ 52 static HidHostService *GetService(); 53 /** 54 * @brief Construct a new Hid Host Service object 55 * 56 */ 57 explicit HidHostService(); 58 /** 59 * @brief Destroy the Hid Host Service object 60 * 61 */ 62 virtual ~HidHostService(); 63 utility::Context *GetContext() override; 64 void Enable(void) override; 65 void Disable(void) override; 66 int Connect(const RawAddress &device) override; 67 std::list<RawAddress> GetConnectDevices() override; 68 int GetConnectState(void) override; 69 int GetMaxConnectNum(void) override; 70 int Disconnect(const RawAddress &device) override; 71 std::vector<RawAddress> GetDevicesByStates(std::vector<int> states) override; 72 int GetDeviceState(const RawAddress &device) override; 73 void RegisterObserver(IHidHostObserver &hidHostObserver) override; 74 void DeregisterObserver(IHidHostObserver &hidHostObserver) override; 75 void NotifyStateChanged(const RawAddress &device, int state); 76 void ShutDownDone(bool isAllDisconnected); 77 void ProcessEvent(const HidHostMessage &event); 78 79 /** 80 * @brief Send the event of the Hid Host role. 81 * 82 * @param event The event of the Hid Host role. 83 */ 84 void PostEvent(const HidHostMessage &event); 85 void RemoveStateMachine(const std::string &device); 86 87 std::string HidHostFindDeviceByLcid(uint16_t lcid, bool *isControlLcid); 88 int HidHostVCUnplug(std::string device, uint8_t id, uint16_t size, uint8_t type) override; 89 int HidHostSendData(std::string device, uint8_t id, uint16_t size, uint8_t type) override; 90 int HidHostSetReport(std::string device, uint8_t type, uint16_t size, const uint8_t* report) override; 91 int HidHostGetReport(std::string device, uint8_t id, uint16_t size, uint8_t type) override; 92 93 private: 94 /** 95 * @brief Service startup. 96 * 97 */ 98 void StartUp(); 99 100 /** 101 * @brief Service shutdown. 102 * 103 */ 104 void ShutDown(); 105 /** 106 * @brief Get the max connection devices number. 107 * 108 * @return Returns the max connection devices number. 109 */ 110 int GetMaxConnectionsDeviceNum() const; 111 int GetConnectionsDeviceNum() const; 112 bool IsConnected(const std::string &address) const; 113 void ProcessConnectEvent(const HidHostMessage &event); 114 void ProcessDefaultEvent(const HidHostMessage &event) const; 115 void ProcessRemoveStateMachine(const std::string &address); 116 // service status 117 bool isStarted_ {false}; 118 // service status 119 bool isShuttingDown_ {false}; 120 // the mutex variable 121 std::recursive_mutex mutex_ {}; 122 123 // The maximum default number of connection devices 124 static const int HID_HOST_MAX_DEFAULT_CONNECTIONS_NUMR = 6; 125 // the maximum number of connection devices. 126 int maxConnectionsNum_ {HID_HOST_MAX_DEFAULT_CONNECTIONS_NUMR}; 127 BaseObserverList<IHidHostObserver> hidHostObservers_ {}; 128 // the map of the device and sate machine 129 std::map<const std::string, std::unique_ptr<HidHostStateMachine>> stateMachines_ {}; 130 // const state map 131 const std::map<const int, const int> stateMap_ = { 132 {HID_HOST_STATE_DISCONNECTED, static_cast<int>(BTConnectState::DISCONNECTED)}, 133 {HID_HOST_STATE_CONNECTING, static_cast<int>(BTConnectState::CONNECTING)}, 134 {HID_HOST_STATE_DISCONNECTING, static_cast<int>(BTConnectState::DISCONNECTING)}, 135 {HID_HOST_STATE_CONNECTED, static_cast<int>(BTConnectState::CONNECTED)} 136 }; 137 }; 138 } // namespace bluetooth 139 } // namespace OHOS 140 #endif // HID_HOST_SERVICE_H 141