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 
22 using ::android::system::suspend::BnSuspendControlService;
23 using ::android::system::suspend::ISuspendCallback;
24 
25 namespace android {
26 namespace system {
27 namespace suspend {
28 namespace V1_0 {
29 
30 class SystemSuspend;
31 
32 class SuspendControlService : public BnSuspendControlService,
33                               public virtual IBinder::DeathRecipient {
34    public:
35     SuspendControlService() = default;
36     ~SuspendControlService() override = default;
37 
38     binder::Status enableAutosuspend(bool* _aidl_return) override;
39     binder::Status registerCallback(const sp<ISuspendCallback>& callback,
40                                     bool* _aidl_return) override;
41     binder::Status forceSuspend(bool* _aidl_return) override;
42     binder::Status getWakeLockStats(std::vector<WakeLockInfo>* _aidl_return) override;
43 
44     void binderDied(const wp<IBinder>& who) override;
45 
46     void setSuspendService(const wp<SystemSuspend>& suspend);
47     void notifyWakeup(bool success);
48     status_t dump(int fd, const Vector<String16>& args) override;
49 
50    private:
51     wp<SystemSuspend> mSuspend;
52 
53     std::mutex mCallbackLock;
54     std::vector<sp<ISuspendCallback> > mCallbacks;
findCb(const wp<IBinder> & cb)55     std::vector<sp<ISuspendCallback> >::iterator findCb(const wp<IBinder>& cb) {
56         return std::find_if(
57             mCallbacks.begin(), mCallbacks.end(),
58             [&cb](const sp<ISuspendCallback> i) { return cb == IInterface::asBinder(i); });
59     }
60 };
61 
62 }  // namespace V1_0
63 }  // namespace suspend
64 }  // namespace system
65 }  // namespace android
66 
67 #endif  // ANDROID_SYSTEM_SYSTEM_SUSPEND_CONTROL_SERVICE_H
68