• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CAR_POWER_MANAGER
18 #define CAR_POWER_MANAGER
19 
20 #include <binder/Status.h>
21 #include <utils/RefBase.h>
22 
23 #include "android/car/ICar.h"
24 #include "android/car/hardware/power/BnCarPowerStateListener.h"
25 #include "android/car/hardware/power/ICarPower.h"
26 
27 using android::binder::Status;
28 
29 namespace android {
30 namespace car {
31 namespace hardware {
32 namespace power {
33 
34 
35 class CarPowerManager : public RefBase {
36 public:
37     // Enumeration of possible boot reasons
38     //  NOTE:  The entries in this enum must match the ones in located CarPowerManager java class:
39     //      packages/services/Car/car-lib/src/android/car/hardware/power/CarPowerManager.java
40     enum class BootReason {
41         kUserPowerOn = 1,
42         kDoorUnlock = 2,
43         kTimer = 3,
44         kDoorOpen = 4,
45         kRemoteStart = 5,
46 
47         kFirst = kUserPowerOn,
48         kLast = kRemoteStart,
49     };
50 
51     // Enumeration of state change events
52     //  NOTE:  The entries in this enum must match the ones in CarPowerStateListener located in
53     //      packages/services/Car/car-lib/src/android/car/hardware/power/CarPowerManager.java
54     enum class State {
55         kShutdownCancelled = 0,
56         kShutdownEnter = 1,
57         kSuspendEnter = 2,
58         kSuspendExit = 3,
59 
60         kFirst = kShutdownCancelled,
61         kLast = kSuspendExit,
62     };
63 
64     using Listener = std::function<void(State)>;
65 
66     CarPowerManager() = default;
67     virtual ~CarPowerManager() = default;
68 
69     // Removes the listener and turns off callbacks
70     //  Returns 0 on success
71     int clearListener();
72 
73     // Returns the boot reason, defined in kBootReason*
74     //  Returns 0 on success
75     int getBootReason(BootReason *bootReason);
76 
77     // Request device to shutdown in lieu of suspend at the next opportunity
78     //  Returns 0 on success
79     int requestShutdownOnNextSuspend();
80 
81     // Set the callback function.  This will execute in the binder thread.
82     //  Returns 0 on success
83     int setListener(Listener listener);
84 
85 private:
86     class CarPowerStateListener final : public BnCarPowerStateListener {
87     public:
CarPowerStateListener(CarPowerManager * parent)88         explicit CarPowerStateListener(CarPowerManager* parent) : mParent(parent) {};
89 
onStateChanged(int state,int token)90         Status onStateChanged(int state, int token) override {
91             sp<CarPowerManager> parent = mParent.promote();
92             if ((parent != nullptr) && (parent->mListener != nullptr)) {
93                 if ((state >= static_cast<int>(State::kFirst)) &&
94                     (state <= static_cast<int>(State::kLast))) {
95                     parent->mListener(static_cast<State>(state));
96                 } else {
97                     ALOGE("onStateChanged unknown state received = %d", state);
98                 }
99             }
100 
101             if (token != 0) {
102                 // Call finished() method to let CPMS know that we're ready to suspend/shutdown.
103                 parent->mICarPower->finished(parent->mListenerToService, token);
104             }
105             return binder::Status::ok();
106         };
107 
108     private:
109         wp<CarPowerManager> mParent;
110     };
111 
112     bool connectToCarService();
113 
114     sp<ICarPower> mICarPower;
115     bool mIsConnected;
116     Listener mListener;
117     sp<CarPowerStateListener> mListenerToService;
118 };
119 
120 } // namespace power
121 } // namespace hardware
122 } // namespace car
123 } // namespace android
124 
125 #endif // CAR_POWER_MANAGER
126 
127