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