1 /*
2  * Copyright (C) 2021 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 #pragma once
18 
19 #include <PowerStatsAidl.h>
20 #include <aidl/android/vendor/powerstats/BnPixelStateResidencyCallback.h>
21 #include <aidl/android/vendor/powerstats/BnPixelStateResidencyProvider.h>
22 
23 #include <android/binder_manager.h>
24 
25 using ::aidl::android::vendor::powerstats::BnPixelStateResidencyProvider;
26 using ::aidl::android::vendor::powerstats::IPixelStateResidencyCallback;
27 
28 namespace aidl {
29 namespace android {
30 namespace hardware {
31 namespace power {
32 namespace stats {
33 
34 class PixelStateResidencyDataProvider : public PowerStats::IStateResidencyDataProvider {
35   public:
36     PixelStateResidencyDataProvider();
37     ~PixelStateResidencyDataProvider() = default;
38     void addEntity(std::string name, std::vector<State> states);
39     void start();
40 
41     // Methods from PowerStats::IStateResidencyDataProvider
42     bool getStateResidencies(
43             std::unordered_map<std::string, std::vector<StateResidency>> *residencies) override;
44     std::unordered_map<std::string, std::vector<State>> getInfo() override;
45 
46   private:
47     class ProviderService : public BnPixelStateResidencyProvider {
48       public:
ProviderService(PixelStateResidencyDataProvider * enclosed)49         ProviderService(PixelStateResidencyDataProvider *enclosed) : mEnclosed(enclosed) {}
50         // Methods from BnPixelStateResidencyProvider
registerCallback(const std::string & in_entityName,const std::shared_ptr<IPixelStateResidencyCallback> & in_cb)51         ::ndk::ScopedAStatus registerCallback(
52                 const std::string &in_entityName,
53                 const std::shared_ptr<IPixelStateResidencyCallback> &in_cb) override {
54             return mEnclosed->registerCallback(in_entityName, in_cb);
55         }
56 
unregisterCallback(const std::shared_ptr<IPixelStateResidencyCallback> & in_cb)57         ::ndk::ScopedAStatus unregisterCallback(
58                 const std::shared_ptr<IPixelStateResidencyCallback> &in_cb) override {
59             return mEnclosed->unregisterCallback(in_cb);
60         }
61 
62       private:
63         PixelStateResidencyDataProvider *mEnclosed;
64     };
65 
66     struct Entry {
EntryEntry67         Entry(std::string name, std::vector<State> states)
68             : mName(name), mStates(states), mCallback(nullptr) {}
69         std::string mName;
70         std::vector<State> mStates;
71         std::shared_ptr<IPixelStateResidencyCallback> mCallback;
72     };
73 
74     ::ndk::ScopedAStatus registerCallback(
75             const std::string &in_entityName,
76             const std::shared_ptr<IPixelStateResidencyCallback> &in_cb);
77 
78     ::ndk::ScopedAStatus unregisterCallback(
79             const std::shared_ptr<IPixelStateResidencyCallback> &in_cb);
80 
81     ::ndk::ScopedAStatus getStateResidenciesTimed(const Entry &entry,
82                                                   std::vector<StateResidency> *residency);
83 
84     const std::string kInstance = "power.stats-vendor";
85     std::mutex mLock;
86     std::shared_ptr<ProviderService> mProviderService;
87     std::vector<Entry> mEntries;
88 };
89 
90 }  // namespace stats
91 }  // namespace power
92 }  // namespace hardware
93 }  // namespace android
94 }  // namespace aidl
95