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