• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
registerCallbackByStates(const std::string & in_entityName,const std::shared_ptr<IPixelStateResidencyCallback> & in_cb,const std::vector<State> & in_states)57         ::ndk::ScopedAStatus registerCallbackByStates(
58                 const std::string &in_entityName,
59                 const std::shared_ptr<IPixelStateResidencyCallback> &in_cb,
60                 const std::vector<State> &in_states) override {
61             return mEnclosed->registerCallbackByStates(in_entityName, in_cb, in_states);
62         }
63 
unregisterCallback(const std::shared_ptr<IPixelStateResidencyCallback> & in_cb)64         ::ndk::ScopedAStatus unregisterCallback(
65                 const std::shared_ptr<IPixelStateResidencyCallback> &in_cb) override {
66             return mEnclosed->unregisterCallback(in_cb);
67         }
68 
69       private:
70         PixelStateResidencyDataProvider *mEnclosed;
71     };
72 
73     struct Entry {
EntryEntry74         Entry(std::string name, std::vector<State> states)
75             : mName(name), mStates(states), mCallback(nullptr) {}
76         std::string mName;
77         std::vector<State> mStates;
78         std::shared_ptr<IPixelStateResidencyCallback> mCallback;
79     };
80 
81     void registerStatesUpdateCallback(
82             std::function<void(const std::string &, const std::vector<State> &)>
83                     statesUpdateCallback) override;
84 
85     ::ndk::ScopedAStatus registerCallback(
86             const std::string &in_entityName,
87             const std::shared_ptr<IPixelStateResidencyCallback> &in_cb);
88 
89     ::ndk::ScopedAStatus registerCallbackByStates(
90             const std::string &in_entityName,
91             const std::shared_ptr<IPixelStateResidencyCallback> &in_cb,
92             const std::vector<State> &in_states);
93 
94     ::ndk::ScopedAStatus unregisterCallback(
95             const std::shared_ptr<IPixelStateResidencyCallback> &in_cb);
96 
97     ::ndk::ScopedAStatus getStateResidenciesTimed(const Entry &entry,
98                                                   std::vector<StateResidency> *residency);
99 
100     const std::string kInstance = "power.stats-vendor";
101     std::mutex mLock;
102     std::shared_ptr<ProviderService> mProviderService;
103     std::vector<Entry> mEntries;
104     std::function<void(const std::string &, const std::vector<State> &)> mStatesUpdateCallback;
105 };
106 
107 }  // namespace stats
108 }  // namespace power
109 }  // namespace hardware
110 }  // namespace android
111 }  // namespace aidl
112