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_POWERPOLICY_SERVER_SRC_SILENTMODEHANDLER_H_ 18 #define CPP_POWERPOLICY_SERVER_SRC_SILENTMODEHANDLER_H_ 19 20 #include <android-base/result.h> 21 #include <android-base/unique_fd.h> 22 #include <utils/Mutex.h> 23 #include <utils/String16.h> 24 #include <utils/StrongPointer.h> 25 #include <utils/Vector.h> 26 27 #include <SysfsMonitor.h> 28 29 #include <atomic> 30 #include <thread> // NOLINT(build/c++11) 31 32 namespace android { 33 namespace frameworks { 34 namespace automotive { 35 namespace powerpolicy { 36 37 inline constexpr const char* kBootReasonForcedNonSilent = "reboot,forcednonsilent"; 38 inline constexpr const char* kBootReasonForcedSilent = "reboot,forcedsilent"; 39 inline constexpr const char* kValueNonSilentMode = "0"; 40 inline constexpr const char* kValueSilentMode = "1"; 41 42 class ISilentModeChangeHandler; 43 44 // Forward declaration for testing use only. 45 namespace internal { 46 47 class SilentModeHandlerPeer; 48 49 } // namespace internal 50 51 /** 52 * SilentModeHandler monitors {@code /sys/power/pm_silentmode_hw_state} in sysfs to detect Silent 53 * Mode change by a vehicle processor. Also, it updates 54 * {@code /sys/power/pm_silentmode_kernel_state} in sysfs to tell kernel the current Silent Mode. 55 */ 56 class SilentModeHandler final { 57 public: 58 explicit SilentModeHandler(ISilentModeChangeHandler* server); 59 60 // Initialize SilentModeHandler instance. 61 void init(); 62 // Release the resource to prepare termination. 63 void release(); 64 // Returns the current Silent Mode. 65 bool isSilentMode(); 66 // Stops monitoring the change on /sys/power/pm_silentmode_hw_state. 67 void stopMonitoringSilentModeHwState(bool shouldWaitThread); 68 // Dumps the internal state. 69 android::base::Result<void> dump(int fd, const Vector<String16>& args); 70 71 private: 72 android::base::Result<void> updateKernelSilentMode(bool silent); 73 void startMonitoringSilentModeHwState(); 74 void handleSilentModeHwStateChange(); 75 void handleSilentModeChange(bool silent); 76 android::base::Result<void> enableBootAnimation(bool enabled); 77 78 android::Mutex mMutex; 79 bool mSilentModeByHwState GUARDED_BY(mMutex); 80 bool mForcedMode = false; 81 std::string mBootReason; 82 std::string mSilentModeHwStateFilename; 83 std::string mKernelSilentModeFilename; 84 ISilentModeChangeHandler* mSilentModeChangeHandler; 85 std::thread mSilentModeMonitoringThread; 86 std::atomic_bool mIsMonitoring = false; 87 android::sp<android::automotive::SysfsMonitor> mSysfsMonitor; 88 android::base::unique_fd mFdSilentModeHwState; 89 90 // For unit tests. 91 friend class internal::SilentModeHandlerPeer; 92 }; 93 94 } // namespace powerpolicy 95 } // namespace automotive 96 } // namespace frameworks 97 } // namespace android 98 99 #endif // CPP_POWERPOLICY_SERVER_SRC_SILENTMODEHANDLER_H_ 100