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_CLIENT_INCLUDE_POWERPOLICYCLIENTBASE_H_ 18 #define CPP_POWERPOLICY_CLIENT_INCLUDE_POWERPOLICYCLIENTBASE_H_ 19 20 #include <aidl/android/frameworks/automotive/powerpolicy/BnCarPowerPolicyChangeCallback.h> 21 #include <aidl/android/frameworks/automotive/powerpolicy/BnCarPowerPolicyServer.h> 22 #include <aidl/android/frameworks/automotive/powerpolicy/CarPowerPolicyFilter.h> 23 #include <android-base/result.h> 24 25 #include <shared_mutex> 26 #include <thread> // NOLINT(build/c++11) 27 #include <vector> 28 29 namespace android { 30 namespace frameworks { 31 namespace automotive { 32 namespace powerpolicy { 33 34 // Utility function to test if a PowerComponent list has the given component. 35 bool hasComponent( 36 const std::vector<::aidl::android::frameworks::automotive::powerpolicy::PowerComponent>& 37 components, 38 ::aidl::android::frameworks::automotive::powerpolicy::PowerComponent component); 39 40 /** 41 * PowerPolicyClientBase handles the connection to car power policy daemon and wraps 42 * ICarPowerPolicyChangeCallback in order to help HALs handle the policy change easier. 43 * 44 * In the inheriting class, the change notifiction can be handled as follows: 45 * 1. Implement getComponentsOfInterest() so that it returns the vector of components of interest. 46 * 2. Override ICarPowerPolicyChangeCallbackk::onPolicyChanged callback. 47 * 3. Check if the component of interest is in enabled or disabled components. 48 * 4. Handle each case. 49 * 50 * ScopedAStatus PowerPolicyClient::onPolicyChanged(const CarPowerPolicy& powerPolicy) { 51 * if (hasComponent(powerPolicy.enabledComponents, PowerComponent::AUDIO)) { 52 * // Do something when AUDIO is enabled. 53 * } else if (hasComponent(powerPolicy.disabledComponents, PowerComponent::AUDIO)) { 54 * // Do something when AUDIO is disabled. 55 * } 56 * return ScopedAStatus::ok(); 57 * } 58 */ 59 class PowerPolicyClientBase : 60 public ::aidl::android::frameworks::automotive::powerpolicy::BnCarPowerPolicyChangeCallback { 61 public: 62 static void onBinderDied(void* cookie); 63 64 // When initialization fails, this callback is invoked from a thread other than the main thread. onInitFailed()65 virtual void onInitFailed() {} 66 67 // Implement this method to specify components of interest. 68 virtual std::vector<::aidl::android::frameworks::automotive::powerpolicy::PowerComponent> 69 getComponentsOfInterest() = 0; 70 71 // init makes connection to power policy daemon and registers to policy change in the 72 // background. Call this method one time when you want to listen to power policy changes. 73 void init(); 74 75 void handleBinderDeath(); 76 77 protected: 78 PowerPolicyClientBase(); 79 virtual ~PowerPolicyClientBase(); 80 81 private: 82 android::base::Result<void> connectToDaemon(); 83 84 std::thread mConnectionThread; 85 mutable std::shared_mutex mRWMutex; 86 std::shared_ptr<::aidl::android::frameworks::automotive::powerpolicy::ICarPowerPolicyServer> 87 mPolicyServer = nullptr; // GUARDED_BY(mRWMutext) 88 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient; 89 }; 90 91 } // namespace powerpolicy 92 } // namespace automotive 93 } // namespace frameworks 94 } // namespace android 95 96 #endif // CPP_POWERPOLICY_CLIENT_INCLUDE_POWERPOLICYCLIENTBASE_H_ 97