1 /*
2  * Copyright (C) 2019 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 HARDWARE_GOOGLE_PIXEL_POWERSTATS_AIDLSTATERESIDENCYDATAPROVIDER_H
18 #define HARDWARE_GOOGLE_PIXEL_POWERSTATS_AIDLSTATERESIDENCYDATAPROVIDER_H
19 
20 #include <android/vendor/powerstats/BnPixelPowerStatsCallback.h>
21 #include <android/vendor/powerstats/BnPixelPowerStatsProvider.h>
22 #include <pixelpowerstats/PowerStats.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace google {
27 namespace pixel {
28 namespace powerstats {
29 
30 using android::sp;
31 using android::vendor::powerstats::BnPixelPowerStatsProvider;
32 using android::vendor::powerstats::IPixelPowerStatsCallback;
33 using binderStatus = android::binder::Status;
34 using ::android::hidl::base::V1_0::IBase;
35 
36 class AidlStateResidencyDataProvider : public IStateResidencyDataProvider,
37                                        public BnPixelPowerStatsProvider,
38                                        public android::IBinder::DeathRecipient {
39   public:
40     AidlStateResidencyDataProvider() = default;
41     ~AidlStateResidencyDataProvider() = default;
42     void addEntity(uint32_t id, std::string entityName, std::vector<std::string> stateNames);
43 
44     // From IStateResidencyDataProvider
45     bool getResults(std::unordered_map<uint32_t, PowerEntityStateResidencyResult> &results) override;
46     std::vector<PowerEntityStateSpace> getStateSpaces() override;
47 
48     // From BnPixelPowerStatsProvider
49     binderStatus registerCallback(const std::string &entityName,
50                                   const sp<IPixelPowerStatsCallback> &callback) override;
51     binderStatus unregisterCallback(const sp<IPixelPowerStatsCallback> &callback) override;
52 
53     // From IBinder::DeathRecipient
54     void binderDied(const wp<android::IBinder> &who) override;
55 
56   private:
57     struct StateSpace {
58         uint32_t powerEntityId;
59         std::unordered_map<std::string, uint32_t> stateInfos;
60     };
61     bool buildResult(std::string entityName,
62                      const std::vector<android::vendor::powerstats::StateResidencyData> &stats,
63                      PowerEntityStateResidencyResult &result);
64     binderStatus unregisterCallbackInternal(const sp<IBinder> &callback);
65     std::unordered_map<std::string, StateSpace> mEntityInfos;
66     std::unordered_map<std::string, sp<IPixelPowerStatsCallback>> mCallbacks;
67     std::mutex mLock;
68 };
69 
70 }  // namespace powerstats
71 }  // namespace pixel
72 }  // namespace google
73 }  // namespace hardware
74 }  // namespace android
75 
76 #endif  // HARDWARE_GOOGLE_PIXEL_POWERSTATS_AIDLSTATERESIDENCYDATAPROVIDER_H