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 22#include <chrono> 23#include <functional> 24#include <optional> 25 26#include <android-base/unique_fd.h> 27 28#include "uevent.h" 29 30#define UEVENT_MSG_LEN 2048 31 32namespace android { 33namespace init { 34 35enum class ListenerAction { 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 40using ListenerCallback = std::function<ListenerAction(const Uevent&)>; 41 42class UeventListener { 43 public: 44 UeventListener(); 45 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; 51 52 private: 53 bool ReadUevent(Uevent* uevent) const; 54 ListenerAction RegenerateUeventsForDir(DIR* d, const ListenerCallback& callback) const; 55 56 android::base::unique_fd device_fd_; 57}; 58 59} // namespace init 60} // namespace android 61 62#endif 63