• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <aidl/android/hardware/power/stats/BnPowerStats.h>
20 
21 #include <optional>
22 #include <unordered_map>
23 
24 namespace aidl {
25 namespace android {
26 namespace hardware {
27 namespace power {
28 namespace stats {
29 
30 class PowerStats : public BnPowerStats {
31   public:
32     class IStateResidencyDataProvider {
33       public:
34         virtual ~IStateResidencyDataProvider() = default;
35         virtual bool getStateResidencies(
36                 std::unordered_map<std::string, std::vector<StateResidency>> *residencies) = 0;
37         virtual std::unordered_map<std::string, std::vector<State>> getInfo() = 0;
registerStatesUpdateCallback(__unused std::function<void (const std::string &,const std::vector<State> &)>)38         virtual void registerStatesUpdateCallback(
39                 __unused std::function<void(const std::string &, const std::vector<State> &)>) {}
40     };
41 
42     class IEnergyConsumer {
43       public:
44         virtual ~IEnergyConsumer() = default;
45         virtual std::pair<EnergyConsumerType, std::string> getInfo() = 0;
46         virtual std::optional<EnergyConsumerResult> getEnergyConsumed() = 0;
47         virtual std::string getConsumerName() = 0;
48     };
49 
50     class IEnergyMeterDataProvider {
51       public:
52         virtual ~IEnergyMeterDataProvider() = default;
53         virtual ndk::ScopedAStatus readEnergyMeter(
54                 const std::vector<int32_t> &in_channelIds,
55                 std::vector<EnergyMeasurement> *_aidl_return) = 0;
56         virtual ndk::ScopedAStatus getEnergyMeterInfo(std::vector<Channel> *_aidl_return) = 0;
57     };
58 
59     PowerStats() = default;
60     void addStateResidencyDataProvider(std::unique_ptr<IStateResidencyDataProvider> p);
61     void addEnergyConsumer(std::unique_ptr<IEnergyConsumer> p);
62     void setEnergyMeterDataProvider(std::unique_ptr<IEnergyMeterDataProvider> p);
63 
64     // Methods from aidl::android::hardware::power::stats::IPowerStats
65     ndk::ScopedAStatus getPowerEntityInfo(std::vector<PowerEntity> *_aidl_return) override;
66     ndk::ScopedAStatus getStateResidency(const std::vector<int32_t> &in_powerEntityIds,
67                                          std::vector<StateResidencyResult> *_aidl_return) override;
68     ndk::ScopedAStatus getEnergyConsumerInfo(std::vector<EnergyConsumer> *_aidl_return) override;
69     ndk::ScopedAStatus getEnergyConsumed(const std::vector<int32_t> &in_energyConsumerIds,
70                                          std::vector<EnergyConsumerResult> *_aidl_return) override;
71     ndk::ScopedAStatus getEnergyMeterInfo(std::vector<Channel> *_aidl_return) override;
72     ndk::ScopedAStatus readEnergyMeter(const std::vector<int32_t> &in_channelIds,
73                                        std::vector<EnergyMeasurement> *_aidl_return) override;
74     binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
75 
76   private:
77     void statesUpdate(const std::string &entityName, const std::vector<State> &states);
78 
79     void getEntityStateNames(
80             std::unordered_map<int32_t, std::string> *entityNames,
81             std::unordered_map<int32_t, std::unordered_map<int32_t, std::string>> *stateNames);
82     void getChannelNames(std::unordered_map<int32_t, std::string> *channelNames);
83     void dumpStateResidency(std::ostringstream &oss, bool delta);
84     void dumpStateResidencyDelta(std::ostringstream &oss,
85                                  const std::vector<StateResidencyResult> &results);
86     void dumpStateResidencyOneShot(std::ostringstream &oss,
87                                    const std::vector<StateResidencyResult> &results);
88     void dumpEnergyConsumer(std::ostringstream &oss, bool delta);
89     void dumpEnergyMeter(std::ostringstream &oss, bool delta);
90 
91     std::vector<std::unique_ptr<IStateResidencyDataProvider>> mStateResidencyDataProviders;
92     std::vector<PowerEntity> mPowerEntityInfos;
93     /* Index that maps each power entity id to an entry in mStateResidencyDataProviders */
94     std::vector<size_t> mStateResidencyDataProviderIndex;
95 
96     std::vector<std::unique_ptr<IEnergyConsumer>> mEnergyConsumers;
97     std::vector<EnergyConsumer> mEnergyConsumerInfos;
98 
99     std::unique_ptr<IEnergyMeterDataProvider> mEnergyMeterDataProvider;
100 };
101 
102 }  // namespace stats
103 }  // namespace power
104 }  // namespace hardware
105 }  // namespace android
106 }  // namespace aidl
107