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