/* * Copyright (c) 2021, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CPP_POWERPOLICY_SERVER_SRC_SILENTMODEHANDLER_H_ #define CPP_POWERPOLICY_SERVER_SRC_SILENTMODEHANDLER_H_ #include #include #include #include #include #include #include // NOLINT(build/c++11) namespace android { namespace frameworks { namespace automotive { namespace powerpolicy { inline constexpr const char* kBootReasonForcedNonSilent = "reboot,forcednonsilent"; inline constexpr const char* kBootReasonForcedSilent = "reboot,forcedsilent"; inline constexpr const char* kValueNonSilentMode = "0"; inline constexpr const char* kValueSilentMode = "1"; class ISilentModeChangeHandler; // Forward declaration for testing use only. namespace internal { class SilentModeHandlerPeer; } // namespace internal /** * SilentModeHandler monitors {@code /sys/power/pm_silentmode_hw_state} in sysfs to detect Silent * Mode change by a vehicle processor. Also, it updates {@code /sys/power/pm_silentmode_kernel} in * sysfs to tell kernel the current Silent Mode. */ class SilentModeHandler final { public: explicit SilentModeHandler(ISilentModeChangeHandler* server); // Initialize SilentModeHandler instance. void init(); // Release the resource to prepare termination. void release(); // Returns the current Silent Mode. bool isSilentMode(); // Stops monitoring the change on /sys/power/pm_silentmode_hw_state. void stopMonitoringSilentModeHwState(bool shouldWaitThread); // Dumps the internal state. android::base::Result dump(int fd, const Vector& args); private: android::base::Result updateKernelSilentMode(bool silent); void startMonitoringSilentModeHwState(); void handleSilentModeHwStateChange(); void handleSilentModeChange(bool silent); android::base::Result enableBootAnimation(bool enabled); android::Mutex mMutex; bool mSilentModeByHwState GUARDED_BY(mMutex); bool mForcedMode = false; std::string mBootReason; std::string mSilentModeHwStateFilename; std::string mKernelSilentModeFilename; ISilentModeChangeHandler* mSilentModeChangeHandler; std::thread mSilentModeMonitoringThread; android::base::unique_fd mFdInotify; int mWdSilentModeHwState = -1; std::atomic_bool mIsMonitoring = false; // For unit tests. friend class internal::SilentModeHandlerPeer; }; } // namespace powerpolicy } // namespace automotive } // namespace frameworks } // namespace android #endif // CPP_POWERPOLICY_SERVER_SRC_SILENTMODEHANDLER_H_