1 /* 2 * Copyright (c) 2022-2023 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 DEVICE_MANAGER_H 17 #define DEVICE_MANAGER_H 18 19 #include <future> 20 #include <memory> 21 #include <set> 22 #include <string> 23 #include <unordered_map> 24 25 #include "nocopyable.h" 26 27 #include "enumerator.h" 28 #include "i_context.h" 29 #include "i_device_mgr.h" 30 #include "epoll_manager.h" 31 #include "monitor.h" 32 33 namespace OHOS { 34 namespace Msdp { 35 namespace DeviceStatus { 36 class DeviceManager final : public IDeviceManager, 37 public IEpollEventSource { 38 public: 39 DeviceManager(); 40 DISALLOW_COPY_AND_MOVE(DeviceManager); 41 ~DeviceManager() = default; 42 43 int32_t Init(IContext *context); 44 int32_t Enable(); 45 int32_t Disable(); 46 int32_t GetFd() const override; 47 void Dispatch(const struct epoll_event &ev) override; 48 std::shared_ptr<IDevice> GetDevice(int32_t id) const override; 49 int32_t AddDeviceObserver(std::weak_ptr<IDeviceObserver> observer) override; 50 void RemoveDeviceObserver(std::weak_ptr<IDeviceObserver> observer) override; 51 void RetriggerHotplug(std::weak_ptr<IDeviceObserver> observer) override; 52 bool AnyOf(std::function<bool(std::shared_ptr<IDevice>)> pred) override; 53 bool HasLocalPointerDevice() override; 54 bool HasLocalKeyboardDevice() override; 55 bool HasKeyboard() override; 56 std::vector<std::shared_ptr<IDevice>> GetKeyboard() override; 57 58 private: 59 class HotplugHandler final : public IDeviceMgr { 60 public: 61 explicit HotplugHandler(DeviceManager &devMgr); 62 ~HotplugHandler() = default; 63 64 void AddDevice(const std::string &devNode) override; 65 void RemoveDevice(const std::string &devNode) override; 66 67 private: 68 DeviceManager &devMgr_; 69 }; 70 71 private: 72 int32_t OnInit(IContext *context); 73 int32_t OnEnable(); 74 int32_t OnDisable(); 75 int32_t OnEpollDispatch(uint32_t events); 76 int32_t ParseDeviceId(const std::string &devNode); 77 void OnDeviceRemoved(std::shared_ptr<IDevice> dev); 78 void OnDeviceAdded(std::shared_ptr<IDevice> dev); 79 int32_t OnAddDeviceObserver(std::weak_ptr<IDeviceObserver> observer); 80 int32_t OnRemoveDeviceObserver(std::weak_ptr<IDeviceObserver> observer); 81 int32_t OnRetriggerHotplug(std::weak_ptr<IDeviceObserver> observer); 82 int32_t RunGetDevice(std::packaged_task<std::shared_ptr<IDevice>(int32_t)> &task, int32_t id) const; 83 std::shared_ptr<IDevice> OnGetDevice(int32_t id) const; 84 std::shared_ptr<IDevice> AddDevice(const std::string &devNode); 85 std::shared_ptr<IDevice> RemoveDevice(const std::string &devNode); 86 std::shared_ptr<IDevice> FindDevice(const std::string &devPath); 87 88 private: 89 IContext *context_ { nullptr }; 90 Enumerator enumerator_; 91 Monitor monitor_; 92 HotplugHandler hotplug_; 93 std::shared_ptr<EpollManager> epollMgr_ { nullptr }; 94 std::set<std::weak_ptr<IDeviceObserver>> observers_; 95 std::unordered_map<int32_t, std::shared_ptr<IDevice>> devices_; 96 }; 97 GetFd()98inline int32_t DeviceManager::GetFd() const 99 { 100 return (epollMgr_ != nullptr ? epollMgr_->GetFd() : -1); 101 } 102 } // namespace DeviceStatus 103 } // namespace Msdp 104 } // namespace OHOS 105 #endif // DEVICE_MANAGER_H