• 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 state change events
38     //  NOTE:  The entries in this enum must match the ones in CarPowerStateListener located in
39     //      packages/services/Car/car-lib/src/android/car/hardware/power/CarPowerManager.java
40     enum class State {
41         kWaitForVhal = 1,
42         kSuspendEnter = 2,
43         kSuspendExit = 3,
44         kShutdownEnter = 5,
45         kOn = 6,
46         kShutdownPrepare = 7,
47         kShutdownCancelled = 8,
48 
49 
50         kFirst = kWaitForVhal,
51         kLast = kShutdownCancelled,
52     };
53 
54     using Listener = std::function<void(State)>;
55 
56     CarPowerManager() = default;
~CarPowerManager()57     virtual ~CarPowerManager() {
58         // Clear the listener if one is set
59         clearListener();
60     }
61 
62     // Removes the listener and turns off callbacks
63     //  Returns 0 on success
64     int clearListener();
65 
66     // Request device to shutdown in lieu of suspend at the next opportunity
67     //  Returns 0 on success
68     int requestShutdownOnNextSuspend();
69 
70     // Set the callback function.  This will execute in the binder thread.
71     //  Returns 0 on success
72     int setListener(Listener listener);
73 
74 private:
75     class CarPowerStateListener final : public BnCarPowerStateListener {
76     public:
CarPowerStateListener(CarPowerManager * parent)77         explicit CarPowerStateListener(CarPowerManager* parent) : mParent(parent) {};
78 
onStateChanged(int state)79         Status onStateChanged(int state) override {
80             sp<CarPowerManager> parent = mParent;
81             if ((parent == nullptr) || (parent->mListener == nullptr)) {
82                 ALOGE("CarPowerManagerNative: onStateChanged null pointer detected!");
83             } else if ((state < static_cast<int>(State::kFirst)) ||
84                        (state > static_cast<int>(State::kLast)) )  {
85                 ALOGE("CarPowerManagerNative: onStateChanged unknown state: %d", state);
86             } else {
87                 // Notify the listener of the state transition
88                 parent->mListener(static_cast<State>(state));
89             }
90             return binder::Status::ok();
91         };
92 
93     private:
94         sp<CarPowerManager> mParent;
95     };
96 
97     bool connectToCarService();
98 
99     sp<ICarPower> mICarPower;
100     bool mIsConnected;
101     Listener mListener;
102     sp<CarPowerStateListener> mListenerToService;
103 };
104 
105 } // namespace power
106 } // namespace hardware
107 } // namespace car
108 } // namespace android
109 
110 #endif // CAR_POWER_MANAGER
111