blob: 27728606421a10e66079b7cd5d672646ad480d81 [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
Tom Cherry939b41c2020-07-20 13:18:01 -070030#define UEVENT_MSG_LEN 8192
Tom Cherryed506f72017-05-25 15:58:59 -070031
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
P.Adarsh Reddya86bf052020-07-21 03:09:28 +053040enum class ReadUeventResult {
41 kSuccess = 0, // Uevent was successfully read.
42 kFailed, // Uevent reading has failed.
43 kInvalid, // An Invalid Uevent was read (like say, the msg received is >= UEVENT_MSG_LEN).
44};
45
Sandeep Patil4cbedee2017-06-21 13:02:57 -070046using ListenerCallback = std::function<ListenerAction(const Uevent&)>;
Tom Cherryed506f72017-05-25 15:58:59 -070047
Tom Cherryed506f72017-05-25 15:58:59 -070048class UeventListener {
49 public:
Tom Cherrye2910102018-12-06 13:29:30 -080050 UeventListener(size_t uevent_socket_rcvbuf_size);
Tom Cherryed506f72017-05-25 15:58:59 -070051
Sandeep Patil4cbedee2017-06-21 13:02:57 -070052 void RegenerateUevents(const ListenerCallback& callback) const;
53 ListenerAction RegenerateUeventsForPath(const std::string& path,
54 const ListenerCallback& callback) const;
55 void Poll(const ListenerCallback& callback,
56 const std::optional<std::chrono::milliseconds> relative_timeout = {}) const;
Tom Cherryed506f72017-05-25 15:58:59 -070057
58 private:
P.Adarsh Reddya86bf052020-07-21 03:09:28 +053059 ReadUeventResult ReadUevent(Uevent* uevent) const;
Sandeep Patil4cbedee2017-06-21 13:02:57 -070060 ListenerAction RegenerateUeventsForDir(DIR* d, const ListenerCallback& callback) const;
Tom Cherryed506f72017-05-25 15:58:59 -070061
62 android::base::unique_fd device_fd_;
63};
64
Tom Cherry81f5d3e2017-06-22 12:53:17 -070065} // namespace init
66} // namespace android
67
Tom Cherryed506f72017-05-25 15:58:59 -070068#endif