Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 1 | /* |
| 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 Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 22 | #include <chrono> |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 23 | #include <functional> |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 24 | #include <optional> |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 25 | |
| 26 | #include <android-base/unique_fd.h> |
| 27 | |
| 28 | #include "uevent.h" |
| 29 | |
| 30 | #define UEVENT_MSG_LEN 2048 |
| 31 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace init { |
| 34 | |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 35 | enum class ListenerAction { |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 36 | 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 Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 40 | using ListenerCallback = std::function<ListenerAction(const Uevent&)>; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 41 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 42 | class UeventListener { |
| 43 | public: |
| 44 | UeventListener(); |
| 45 | |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 46 | 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 Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | bool ReadUevent(Uevent* uevent) const; |
Sandeep Patil | 4cbedee | 2017-06-21 13:02:57 -0700 | [diff] [blame] | 54 | ListenerAction RegenerateUeventsForDir(DIR* d, const ListenerCallback& callback) const; |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 55 | |
| 56 | android::base::unique_fd device_fd_; |
| 57 | }; |
| 58 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 59 | } // namespace init |
| 60 | } // namespace android |
| 61 | |
Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 62 | #endif |