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;
38     };
39 
40     class IEnergyConsumer {
41       public:
42         virtual ~IEnergyConsumer() = default;
43         virtual std::pair<EnergyConsumerType, std::string> getInfo() = 0;
44         virtual std::optional<EnergyConsumerResult> getEnergyConsumed() = 0;
45         virtual std::string getConsumerName() = 0;
46     };
47 
48     class IEnergyMeterDataProvider {
49       public:
50         virtual ~IEnergyMeterDataProvider() = default;
51         virtual ndk::ScopedAStatus readEnergyMeter(
52                 const std::vector<int32_t> &in_channelIds,
53                 std::vector<EnergyMeasurement> *_aidl_return) = 0;
54         virtual ndk::ScopedAStatus getEnergyMeterInfo(std::vector<Channel> *_aidl_return) = 0;
55     };
56 
57     PowerStats() = default;
58     void addStateResidencyDataProvider(std::unique_ptr<IStateResidencyDataProvider> p);
59     void addEnergyConsumer(std::unique_ptr<IEnergyConsumer> p);
60     void setEnergyMeterDataProvider(std::unique_ptr<IEnergyMeterDataProvider> p);
61 
62     // Methods from aidl::android::hardware::power::stats::IPowerStats
63     ndk::ScopedAStatus getPowerEntityInfo(std::vector<PowerEntity> *_aidl_return) override;
64     ndk::ScopedAStatus getStateResidency(const std::vector<int32_t> &in_powerEntityIds,
65                                          std::vector<StateResidencyResult> *_aidl_return) override;
66     ndk::ScopedAStatus getEnergyConsumerInfo(std::vector<EnergyConsumer> *_aidl_return) override;
67     ndk::ScopedAStatus getEnergyConsumed(const std::vector<int32_t> &in_energyConsumerIds,
68                                          std::vector<EnergyConsumerResult> *_aidl_return) override;
69     ndk::ScopedAStatus getEnergyMeterInfo(std::vector<Channel> *_aidl_return) override;
70     ndk::ScopedAStatus readEnergyMeter(const std::vector<int32_t> &in_channelIds,
71                                        std::vector<EnergyMeasurement> *_aidl_return) override;
72     binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
73 
74   private:
75     void getEntityStateNames(
76             std::unordered_map<int32_t, std::string> *entityNames,
77             std::unordered_map<int32_t, std::unordered_map<int32_t, std::string>> *stateNames);
78     void getChannelNames(std::unordered_map<int32_t, std::string> *channelNames);
79     void dumpStateResidency(std::ostringstream &oss, bool delta);
80     void dumpStateResidencyDelta(std::ostringstream &oss,
81                                  const std::vector<StateResidencyResult> &results);
82     void dumpStateResidencyOneShot(std::ostringstream &oss,
83                                    const std::vector<StateResidencyResult> &results);
84     void dumpEnergyConsumer(std::ostringstream &oss, bool delta);
85     void dumpEnergyMeter(std::ostringstream &oss, bool delta);
86 
87     std::vector<std::unique_ptr<IStateResidencyDataProvider>> mStateResidencyDataProviders;
88     std::vector<PowerEntity> mPowerEntityInfos;
89     /* Index that maps each power entity id to an entry in mStateResidencyDataProviders */
90     std::vector<size_t> mStateResidencyDataProviderIndex;
91 
92     std::vector<std::unique_ptr<IEnergyConsumer>> mEnergyConsumers;
93     std::vector<EnergyConsumer> mEnergyConsumerInfos;
94 
95     std::unique_ptr<IEnergyMeterDataProvider> mEnergyMeterDataProvider;
96 };
97 
98 }  // namespace stats
99 }  // namespace power
100 }  // namespace hardware
101 }  // namespace android
102 }  // namespace aidl
103