1 /*
2  * Copyright 2019 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 ANDROID_SYSTEM_SYSTEM_SUSPEND_CONTROL_SERVICE_H
18 #define ANDROID_SYSTEM_SYSTEM_SUSPEND_CONTROL_SERVICE_H
19 
20 #include <android/system/suspend/BnSuspendControlService.h>
21 #include <android/system/suspend/internal/BnSuspendControlServiceInternal.h>
22 #include <android/system/suspend/internal/SuspendInfo.h>
23 #include <android/system/suspend/internal/WakeLockInfo.h>
24 #include <android/system/suspend/internal/WakeupInfo.h>
25 
26 using ::android::system::suspend::BnSuspendControlService;
27 using ::android::system::suspend::ISuspendCallback;
28 using ::android::system::suspend::IWakelockCallback;
29 using ::android::system::suspend::internal::BnSuspendControlServiceInternal;
30 using ::android::system::suspend::internal::SuspendInfo;
31 using ::android::system::suspend::internal::WakeLockInfo;
32 using ::android::system::suspend::internal::WakeupInfo;
33 
34 namespace android {
35 namespace system {
36 namespace suspend {
37 namespace V1_0 {
38 
39 class SystemSuspend;
40 
41 class SuspendControlService : public BnSuspendControlService,
42                               public virtual IBinder::DeathRecipient {
43    public:
44     SuspendControlService() = default;
45     ~SuspendControlService() override = default;
46 
47     binder::Status registerCallback(const sp<ISuspendCallback>& callback,
48                                     bool* _aidl_return) override;
49     binder::Status registerWakelockCallback(const sp<IWakelockCallback>& callback,
50                                             const std::string& name, bool* _aidl_return) override;
51 
52     void binderDied(const wp<IBinder>& who) override;
53 
54     void notifyWakelock(const std::string& name, bool isAcquired);
55     void notifyWakeup(bool success, std::vector<std::string>& wakeupReasons);
56 
57    private:
58     std::map<std::string, std::vector<sp<IWakelockCallback>>> mWakelockCallbacks;
59     std::mutex mCallbackLock;
60     std::mutex mWakelockCallbackLock;
61     std::vector<sp<ISuspendCallback>> mCallbacks;
findCb(const wp<IBinder> & cb)62     const std::vector<sp<ISuspendCallback>>::iterator findCb(const wp<IBinder>& cb) {
63         return std::find_if(
64             mCallbacks.begin(), mCallbacks.end(),
65             [&cb](const sp<ISuspendCallback>& i) { return cb == IInterface::asBinder(i); });
66     }
67 };
68 
69 class SuspendControlServiceInternal : public BnSuspendControlServiceInternal,
70                                       public virtual IBinder::DeathRecipient {
71    public:
72     SuspendControlServiceInternal() = default;
73     ~SuspendControlServiceInternal() override = default;
74 
75     binder::Status enableAutosuspend(bool* _aidl_return) override;
76     binder::Status forceSuspend(bool* _aidl_return) override;
77     binder::Status getSuspendStats(SuspendInfo* _aidl_return) override;
78     binder::Status getWakeLockStats(std::vector<WakeLockInfo>* _aidl_return) override;
79     binder::Status getWakeupStats(std::vector<WakeupInfo>* _aidl_return) override;
80 
binderDied(const wp<IBinder> & who)81     void binderDied([[maybe_unused]] const wp<IBinder>& who) override {}
82 
83     void setSuspendService(const wp<SystemSuspend>& suspend);
84     status_t dump(int fd, const Vector<String16>& args) override;
85 
86    private:
87     wp<SystemSuspend> mSuspend;
88 };
89 
90 }  // namespace V1_0
91 }  // namespace suspend
92 }  // namespace system
93 }  // namespace android
94 
95 #endif  // ANDROID_SYSTEM_SYSTEM_SUSPEND_CONTROL_SERVICE_H
96