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 53 private: 54 class HotplugHandler final : public IDeviceMgr { 55 public: 56 explicit HotplugHandler(DeviceManager &devMgr); 57 ~HotplugHandler() = default; 58 59 void AddDevice(const std::string &devNode) override; 60 void RemoveDevice(const std::string &devNode) override; 61 62 private: 63 DeviceManager &devMgr_; 64 }; 65 66 private: 67 int32_t OnInit(IContext *context); 68 int32_t OnEnable(); 69 int32_t OnDisable(); 70 int32_t OnEpollDispatch(uint32_t events); 71 int32_t ParseDeviceId(const std::string &devNode); 72 void OnDeviceRemoved(std::shared_ptr<IDevice> dev); 73 void OnDeviceAdded(std::shared_ptr<IDevice> dev); 74 int32_t OnAddDeviceObserver(std::weak_ptr<IDeviceObserver> observer); 75 int32_t OnRemoveDeviceObserver(std::weak_ptr<IDeviceObserver> observer); 76 int32_t OnRetriggerHotplug(std::weak_ptr<IDeviceObserver> observer); 77 int32_t RunGetDevice(std::packaged_task<std::shared_ptr<IDevice>(int32_t)> &task, int32_t id) const; 78 std::shared_ptr<IDevice> OnGetDevice(int32_t id) const; 79 std::shared_ptr<IDevice> AddDevice(const std::string &devNode); 80 std::shared_ptr<IDevice> RemoveDevice(const std::string &devNode); 81 std::shared_ptr<IDevice> FindDevice(const std::string &devPath); 82 83 private: 84 IContext *context_ { nullptr }; 85 Enumerator enumerator_; 86 Monitor monitor_; 87 HotplugHandler hotplug_; 88 std::shared_ptr<EpollManager> epollMgr_ { nullptr }; 89 std::set<std::weak_ptr<IDeviceObserver>> observers_; 90 std::unordered_map<int32_t, std::shared_ptr<IDevice>> devices_; 91 }; 92 GetFd()93inline int32_t DeviceManager::GetFd() const 94 { 95 return (epollMgr_ != nullptr ? epollMgr_->GetFd() : -1); 96 } 97 } // namespace DeviceStatus 98 } // namespace Msdp 99 } // namespace OHOS 100 #endif // DEVICE_MANAGER_H