1 /*
2  * Copyright (c) 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 MMI_HOT_PLUG_DETECTOR_H
17 #define MMI_HOT_PLUG_DETECTOR_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "nocopyable.h"
23 #include "unique_fd.h"
24 
25 namespace OHOS {
26 namespace MMI {
27 class HotplugDetector final {
28 public:
29     using callback = std::function<void(std::string)>;
30     HotplugDetector();
31     ~HotplugDetector();
32     DISALLOW_COPY_AND_MOVE(HotplugDetector);
33 
34     bool Init(const callback& addFunc, const callback& removeFunc);
35     void Stop();
36     void OnEvent() const;
37 
GetFd()38     int32_t GetFd() const
39     {
40         return inotifyFd_;
41     }
42 
43 private:
44     bool Scan() const;
45 
46     callback addFunc_;
47     callback removeFunc_;
48 
49     UniqueFd inotifyFd_;
50 };
51 } // namespace MMI
52 } // namespace OHOS
53 #endif // MMI_HOT_PLUG_DETECTOR_H