• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "i_epoll_event_source.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();
71     int32_t ParseDeviceId(const std::string &devNode);
72     void OnDeviceAdded(std::shared_ptr<IDevice> dev);
73     void OnDeviceRemoved(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 EpollCreate();
78     int32_t EpollAdd(IEpollEventSource *source);
79     void EpollDel(IEpollEventSource *source);
80     void EpollClose();
81     int32_t RunGetDevice(std::packaged_task<std::shared_ptr<IDevice>(int32_t)> &task, int32_t id) const;
82     std::shared_ptr<IDevice> OnGetDevice(int32_t id) const;
83     std::shared_ptr<IDevice> AddDevice(const std::string &devNode);
84     std::shared_ptr<IDevice> RemoveDevice(const std::string &devNode);
85     std::shared_ptr<IDevice> FindDevice(const std::string &devPath);
86 
87 private:
88     IContext *context_ { nullptr };
89     int32_t epollFd_ { -1 };
90     Enumerator enumerator_;
91     Monitor monitor_;
92     HotplugHandler hotplug_;
93     std::set<std::weak_ptr<IDeviceObserver>> observers_;
94     std::unordered_map<int32_t, std::shared_ptr<IDevice>> devices_;
95 };
96 
GetFd()97 inline int32_t DeviceManager::GetFd() const
98 {
99     return epollFd_;
100 }
101 } // namespace DeviceStatus
102 } // namespace Msdp
103 } // namespace OHOS
104 #endif // DEVICE_MANAGER_H