blob: 5b453fe6592516adf8ac19fe8f3b631e03c17d0e [file] [log] [blame]
Tom Cherryed506f72017-05-25 15:58:59 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _INIT_UEVENT_LISTENER_H
18#define _INIT_UEVENT_LISTENER_H
19
20#include <dirent.h>
21
Sandeep Patil4cbedee2017-06-21 13:02:57 -070022#include <chrono>
Tom Cherryed506f72017-05-25 15:58:59 -070023#include <functional>
Sandeep Patil4cbedee2017-06-21 13:02:57 -070024#include <optional>
Tom Cherryed506f72017-05-25 15:58:59 -070025
26#include <android-base/unique_fd.h>
27
28#include "uevent.h"
29
30#define UEVENT_MSG_LEN 2048
31
Tom Cherry81f5d3e2017-06-22 12:53:17 -070032namespace android {
33namespace init {
34
Sandeep Patil4cbedee2017-06-21 13:02:57 -070035enum class ListenerAction {
Tom Cherryed506f72017-05-25 15:58:59 -070036 kStop = 0, // Stop regenerating uevents as we've handled the one(s) we're interested in.
37 kContinue, // Continue regenerating uevents as we haven't seen the one(s) we're interested in.
38};
39
Sandeep Patil4cbedee2017-06-21 13:02:57 -070040using ListenerCallback = std::function<ListenerAction(const Uevent&)>;
Tom Cherryed506f72017-05-25 15:58:59 -070041
Tom Cherryed506f72017-05-25 15:58:59 -070042class UeventListener {
43 public:
44 UeventListener();
45
Sandeep Patil4cbedee2017-06-21 13:02:57 -070046 void RegenerateUevents(const ListenerCallback& callback) const;
47 ListenerAction RegenerateUeventsForPath(const std::string& path,
48 const ListenerCallback& callback) const;
49 void Poll(const ListenerCallback& callback,
50 const std::optional<std::chrono::milliseconds> relative_timeout = {}) const;
Tom Cherryed506f72017-05-25 15:58:59 -070051
52 private:
53 bool ReadUevent(Uevent* uevent) const;
Sandeep Patil4cbedee2017-06-21 13:02:57 -070054 ListenerAction RegenerateUeventsForDir(DIR* d, const ListenerCallback& callback) const;
Tom Cherryed506f72017-05-25 15:58:59 -070055
56 android::base::unique_fd device_fd_;
57};
58
Tom Cherry81f5d3e2017-06-22 12:53:17 -070059} // namespace init
60} // namespace android
61
Tom Cherryed506f72017-05-25 15:58:59 -070062#endif