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/kernel/silent_boot/pm_silentmode_hw_state} in sysfs to 53 * detect Silent Mode change by a vehicle processor. Also, it updates 54 * {@code /sys/kernel/silent_boot/pm_silentmode_kernel_state} in sysfs to tell kernel the current 55 * Silent Mode. 56 */ 57 class SilentModeHandler final { 58 public: 59 explicit SilentModeHandler(ISilentModeChangeHandler* server); 60 61 // Initialize SilentModeHandler instance. 62 void init(); 63 // Release the resource to prepare termination. 64 void release(); 65 // Returns the current Silent Mode. 66 bool isSilentMode(); 67 // Stops monitoring the change on pm_silentmode_hw_state. 68 void stopMonitoringSilentModeHwState(bool shouldWaitThread); 69 // Dumps the internal state. 70 android::base::Result<void> dump(int fd, const Vector<String16>& args); 71 72 private: 73 android::base::Result<void> updateKernelSilentMode(bool silent); 74 void startMonitoringSilentModeHwState(); 75 void handleSilentModeHwStateChange(); 76 void handleSilentModeChange(bool silent); 77 android::base::Result<void> enableBootAnimation(bool enabled); 78 79 android::Mutex mMutex; 80 bool mSilentModeByHwState GUARDED_BY(mMutex); 81 bool mForcedMode = false; 82 std::string mBootReason; 83 std::string mSilentModeHwStateFilename; 84 std::string mKernelSilentModeFilename; 85 ISilentModeChangeHandler* mSilentModeChangeHandler; 86 std::thread mSilentModeMonitoringThread; 87 std::atomic_bool mIsMonitoring = false; 88 android::sp<android::automotive::SysfsMonitor> mSysfsMonitor; 89 android::base::unique_fd mFdSilentModeHwState; 90 91 // For unit tests. 92 friend class internal::SilentModeHandlerPeer; 93 }; 94 95 } // namespace powerpolicy 96 } // namespace automotive 97 } // namespace frameworks 98 } // namespace android 99 100 #endif // CPP_POWERPOLICY_SERVER_SRC_SILENTMODEHANDLER_H_ 101