1 /** 2 * Copyright (c) 2021, 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 CPP_LIBSYSFSMONITOR_SRC_SYSFSMONITOR_H_ 18 #define CPP_LIBSYSFSMONITOR_SRC_SYSFSMONITOR_H_ 19 20 #include <android-base/result.h> 21 #include <android-base/unique_fd.h> 22 #include <utils/RefBase.h> 23 24 #include <functional> 25 #include <string> 26 #include <unordered_set> 27 28 namespace android { 29 namespace automotive { 30 31 using CallbackFunc = ::std::function<void(const std::vector<int32_t>&)>; 32 33 /** 34 * SysfsMonitor monitors sysfs file changes and invokes the registered callback when there is a 35 * change at the sysfs files to monitor. 36 */ 37 class SysfsMonitor final : public RefBase { 38 public: 39 // Initializes SysfsMonitor instance. 40 android::base::Result<void> init(CallbackFunc callback); 41 // Releases resources used for monitoring. 42 android::base::Result<void> release(); 43 // Registers a sysfs file to monitor. 44 android::base::Result<void> registerFd(int32_t fd); 45 // Unregisters a sysfs file to monitor. 46 android::base::Result<void> unregisterFd(int32_t fd); 47 // Starts observing sysfs file changes. 48 android::base::Result<void> observe(); 49 50 private: 51 android::base::unique_fd mEpollFd; 52 std::unordered_set<int32_t> mMonitoringFds; 53 CallbackFunc mCallback; 54 }; 55 56 } // namespace automotive 57 } // namespace android 58 59 #endif // CPP_LIBSYSFSMONITOR_SRC_SYSFSMONITOR_H_ 60