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 MONITOR_H
17#define MONITOR_H
18
19#include <memory>
20#include <set>
21
22#include <sys/inotify.h>
23
24#include "nocopyable.h"
25
26#include "i_context.h"
27#include "i_device_mgr.h"
28#include "i_epoll_event_source.h"
29
30namespace OHOS {
31namespace Msdp {
32namespace DeviceStatus {
33class Monitor final : public IEpollEventSource {
34public:
35    Monitor() = default;
36    DISALLOW_COPY_AND_MOVE(Monitor);
37    ~Monitor();
38
39    int32_t GetFd() const override;
40    void Dispatch(const struct epoll_event &ev) override;
41    void SetDeviceMgr(IDeviceMgr *devMgr);
42    int32_t Enable();
43    void Disable();
44
45private:
46    int32_t OpenConnection();
47    int32_t EnableReceiving();
48    void ReceiveDevice();
49    void HandleInotifyEvent(struct inotify_event *event) const;
50    void AddDevice(const std::string &devNode) const;
51    void RemoveDevice(const std::string &devNode) const;
52
53private:
54    int32_t inotifyFd_ { -1 };
55    int32_t devWd_ { -1 };
56    IDeviceMgr *devMgr_ { nullptr };
57};
58
59inline int32_t Monitor::GetFd() const
60{
61    return inotifyFd_;
62}
63} // namespace DeviceStatus
64} // namespace Msdp
65} // namespace OHOS
66#endif // MONITOR_H