1 /*
2  * Copyright (C) 2018 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 #pragma once
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <thread>
24 
25 #include <perfmgr/HintManager.h>
26 
27 namespace aidl {
28 namespace google {
29 namespace hardware {
30 namespace power {
31 namespace impl {
32 namespace pixel {
33 
34 using ::android::perfmgr::HintManager;
35 
36 enum InteractionState {
37     INTERACTION_STATE_UNINITIALIZED,
38     INTERACTION_STATE_IDLE,
39     INTERACTION_STATE_INTERACTION,
40     INTERACTION_STATE_WAITING,
41 };
42 
43 class InteractionHandler {
44   public:
45     InteractionHandler(std::shared_ptr<HintManager> const &hint_manager);
46     ~InteractionHandler();
47     bool Init();
48     void Exit();
49     void Acquire(int32_t duration);
50 
51   private:
52     void Release();
53     void WaitForIdle(int32_t wait_ms, int32_t timeout_ms);
54     void AbortWaitLocked();
55     void Routine();
56 
57     void PerfLock();
58     void PerfRel();
59 
60     enum InteractionState mState;
61     int mIdleFd;
62     int mEventFd;
63     int32_t mDurationMs;
64     struct timespec mLastTimespec;
65     std::unique_ptr<std::thread> mThread;
66     std::mutex mLock;
67     std::condition_variable mCond;
68     std::shared_ptr<HintManager> mHintManager;
69 };
70 
71 }  // namespace pixel
72 }  // namespace impl
73 }  // namespace power
74 }  // namespace hardware
75 }  // namespace google
76 }  // namespace aidl
77