1 /** 2 * Copyright (c) 2020, 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 CPP_POWERPOLICY_SERVER_SRC_CARPOWERPOLICYSERVER_H_ 18 #define CPP_POWERPOLICY_SERVER_SRC_CARPOWERPOLICYSERVER_H_ 19 20 #include "PolicyManager.h" 21 #include "PowerComponentHandler.h" 22 #include "SilentModeHandler.h" 23 24 #include <android-base/result.h> 25 #include <android/frameworks/automotive/powerpolicy/BnCarPowerPolicyServer.h> 26 #include <android/frameworks/automotive/powerpolicy/internal/BnCarPowerPolicySystemNotification.h> 27 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h> 28 #include <binder/IBinder.h> 29 #include <binder/Status.h> 30 #include <utils/Looper.h> 31 #include <utils/Mutex.h> 32 #include <utils/String16.h> 33 #include <utils/StrongPointer.h> 34 #include <utils/Vector.h> 35 36 #include <optional> 37 #include <unordered_set> 38 39 namespace android { 40 namespace frameworks { 41 namespace automotive { 42 namespace powerpolicy { 43 44 struct CallbackInfo { CallbackInfoCallbackInfo45 CallbackInfo(const android::sp<ICarPowerPolicyChangeCallback>& callback, 46 const CarPowerPolicyFilter& filter, int32_t pid) : 47 callback(callback), 48 filter(filter), 49 pid(pid) {} 50 51 android::sp<ICarPowerPolicyChangeCallback> callback; 52 CarPowerPolicyFilter filter; 53 pid_t pid; 54 }; 55 56 // Forward declaration for testing use only. 57 namespace internal { 58 59 class CarPowerPolicyServerPeer; 60 61 } // namespace internal 62 63 // Forward declaration for defining binder death handler and property change listener. 64 class CarPowerPolicyServer; 65 66 class BinderDeathRecipient : public android::IBinder::DeathRecipient { 67 public: 68 explicit BinderDeathRecipient(const android::sp<CarPowerPolicyServer>& service); 69 70 void binderDied(const android::wp<android::IBinder>& who) override; 71 72 private: 73 android::sp<CarPowerPolicyServer> mService; 74 }; 75 76 class HidlDeathRecipient : public android::hardware::hidl_death_recipient { 77 public: 78 explicit HidlDeathRecipient(const android::sp<CarPowerPolicyServer>& service); 79 80 void serviceDied(uint64_t cookie, 81 const android::wp<android::hidl::base::V1_0::IBase>& who) override; 82 83 private: 84 sp<CarPowerPolicyServer> mService; 85 }; 86 87 class PropertyChangeListener : 88 public android::hardware::automotive::vehicle::V2_0::IVehicleCallback { 89 public: 90 explicit PropertyChangeListener(const android::sp<CarPowerPolicyServer>& service); 91 92 android::hardware::Return<void> onPropertyEvent( 93 const android::hardware::hidl_vec< 94 hardware::automotive::vehicle::V2_0::VehiclePropValue>& propValues) override; 95 android::hardware::Return<void> onPropertySet( 96 const android::hardware::automotive::vehicle::V2_0::VehiclePropValue& propValue); 97 android::hardware::Return<void> onPropertySetError( 98 android::hardware::automotive::vehicle::V2_0::StatusCode status, int32_t propId, 99 int32_t areaId); 100 101 private: 102 android::sp<CarPowerPolicyServer> mService; 103 }; 104 105 class MessageHandlerImpl : public android::MessageHandler { 106 public: 107 explicit MessageHandlerImpl(const android::sp<CarPowerPolicyServer>& service); 108 109 void handleMessage(const android::Message& message) override; 110 111 private: 112 android::sp<CarPowerPolicyServer> mService; 113 }; 114 115 class CarServiceNotificationHandler : 116 public android::frameworks::automotive::powerpolicy::internal:: 117 BnCarPowerPolicySystemNotification { 118 public: 119 explicit CarServiceNotificationHandler(const android::sp<CarPowerPolicyServer>& server); 120 121 android::status_t dump(int fd, const android::Vector<android::String16>& args) override; 122 android::binder::Status notifyCarServiceReady( 123 android::frameworks::automotive::powerpolicy::internal::PolicyState* policyState) 124 override; 125 android::binder::Status notifyPowerPolicyChange(const std::string& policyId) override; 126 android::binder::Status notifyPowerPolicyDefinition( 127 const std::string& policyId, const std::vector<std::string>& enabledComponents, 128 const std::vector<std::string>& disabledComponents) override; 129 130 private: 131 android::sp<CarPowerPolicyServer> mService; 132 }; 133 134 /** 135 * ISilentModeChangeHandler defines a method which is called when a Silent Mode hw state is changed. 136 */ 137 class ISilentModeChangeHandler { 138 public: 139 virtual ~ISilentModeChangeHandler() = 0; 140 141 // Called when Silent Mode is changed. 142 virtual void notifySilentModeChange(const bool isSilent) = 0; 143 }; 144 145 /** 146 * CarPowerPolicyServer implements ISilentModeChangeHandler and ICarPowerPolicyServer.aidl. 147 * It handles power policy requests and Silent Mode before Android framework takes control of the 148 * device. 149 */ 150 class CarPowerPolicyServer final : public ISilentModeChangeHandler, public BnCarPowerPolicyServer { 151 public: 152 static base::Result<sp<CarPowerPolicyServer>> startService(const sp<android::Looper>& looper); 153 static void terminateService(); 154 155 // Implements ICarPowerPolicyServer.aidl. 156 status_t dump(int fd, const Vector<String16>& args) override; 157 binder::Status getCurrentPowerPolicy(CarPowerPolicy* aidlReturn) override; 158 binder::Status getPowerComponentState(PowerComponent componentId, bool* aidlReturn) override; 159 binder::Status registerPowerPolicyChangeCallback( 160 const android::sp<ICarPowerPolicyChangeCallback>& callback, 161 const CarPowerPolicyFilter& filter) override; 162 binder::Status unregisterPowerPolicyChangeCallback( 163 const android::sp<ICarPowerPolicyChangeCallback>& callback) override; 164 165 void connectToVhalHelper(); 166 void handleBinderDeath(const android::wp<android::IBinder>& who); 167 void handleHidlDeath(const android::wp<android::hidl::base::V1_0::IBase>& who); 168 169 // Implements ICarPowerPolicySystemNotification.aidl. 170 android::binder::Status notifyCarServiceReady( 171 android::frameworks::automotive::powerpolicy::internal::PolicyState* policyState); 172 android::binder::Status notifyPowerPolicyChange(const std::string& policyId); 173 android::binder::Status notifyPowerPolicyDefinition( 174 const std::string& policyId, const std::vector<std::string>& enabledComponents, 175 const std::vector<std::string>& disabledComponents); 176 177 /** 178 * Applies the given power policy. 179 * 180 * @param carServiceInOperation expected Car Service running state. 181 * @param overridePreemptive whether to override a preemptive power policy. 182 */ 183 android::base::Result<void> applyPowerPolicy(const std::string& policyId, 184 const bool carServiceInOperation, 185 const bool overridePreemptive); 186 /** 187 * Sets the power policy group which contains rules to map a power state to a default power 188 * policy to apply. 189 */ 190 android::base::Result<void> setPowerPolicyGroup(const std::string& groupId); 191 192 // Implements ISilentModeChangeHandler. 193 void notifySilentModeChange(const bool isSilent); 194 195 private: 196 CarPowerPolicyServer(); 197 198 android::base::Result<void> init(const android::sp<android::Looper>& looper); 199 void terminate(); 200 bool isRegisteredLocked(const android::sp<ICarPowerPolicyChangeCallback>& callback); 201 void connectToVhal(); 202 void applyInitialPowerPolicy(); 203 void subscribeToVhal(); 204 void subscribeToProperty( 205 int32_t prop, 206 std::function< 207 void(const android::hardware::automotive::vehicle::V2_0::VehiclePropValue&)> 208 processor); 209 android::base::Result<void> notifyVhalNewPowerPolicy(const std::string& policyId); 210 bool isPropertySupported(const int32_t prop); 211 bool isPowerPolicyAppliedLocked() const; 212 213 private: 214 static android::sp<CarPowerPolicyServer> sCarPowerPolicyServer; 215 216 sp<android::Looper> mHandlerLooper; 217 sp<MessageHandlerImpl> mMessageHandler; 218 PowerComponentHandler mComponentHandler; 219 PolicyManager mPolicyManager; 220 SilentModeHandler mSilentModeHandler; 221 android::Mutex mMutex; 222 CarPowerPolicyMeta mCurrentPowerPolicyMeta GUARDED_BY(mMutex); 223 std::string mCurrentPolicyGroupId GUARDED_BY(mMutex); 224 std::string mPendingPowerPolicyId GUARDED_BY(mMutex); 225 bool mIsPowerPolicyLocked GUARDED_BY(mMutex); 226 std::vector<CallbackInfo> mPolicyChangeCallbacks GUARDED_BY(mMutex); 227 android::sp<android::hardware::automotive::vehicle::V2_0::IVehicle> mVhalService 228 GUARDED_BY(mMutex); 229 std::optional<int64_t> mLastApplyPowerPolicyUptimeMs GUARDED_BY(mMutex); 230 std::optional<int64_t> mLastSetDefaultPowerPolicyGroupUptimeMs GUARDED_BY(mMutex); 231 bool mIsCarServiceInOperation GUARDED_BY(mMutex); 232 // No thread-safety guard is needed because only accessed through main thread handler. 233 bool mIsFirstConnectionToVhal; 234 std::unordered_map<int32_t, bool> mSupportedProperties; 235 android::sp<BinderDeathRecipient> mBinderDeathRecipient; 236 android::sp<HidlDeathRecipient> mHidlDeathRecipient; 237 android::sp<PropertyChangeListener> mPropertyChangeListener; 238 android::sp<CarServiceNotificationHandler> mCarServiceNotificationHandler; 239 int32_t mRemainingConnectionRetryCount; 240 241 // For unit tests. 242 friend class android::frameworks::automotive::powerpolicy::internal::CarPowerPolicyServerPeer; 243 }; 244 245 } // namespace powerpolicy 246 } // namespace automotive 247 } // namespace frameworks 248 } // namespace android 249 250 #endif // CPP_POWERPOLICY_SERVER_SRC_CARPOWERPOLICYSERVER_H_ 251