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 #ifndef POWERSTATSCOLLECTOR_H
17 #define POWERSTATSCOLLECTOR_H
18 
19 #include <memory>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <pwrstatsutil.pb.h>
24 
25 using com::google::android::pwrstatsutil::PowerStatistic;
26 using PowerStatCase = com::google::android::pwrstatsutil::PowerStatistic::PowerStatCase;
27 
28 /**
29  * Classes that inherit from this can be used to provide stats in the form of key/value
30  * pairs to PwrStatsUtil.
31  **/
32 class IPowerStatProvider {
33   public:
34     virtual ~IPowerStatProvider() = default;
35     int get(PowerStatistic* stat) const;
36     int get(const PowerStatistic& start, PowerStatistic* interval) const;
37     void dump(const PowerStatistic& stat, std::ostream* output) const;
38     virtual PowerStatCase typeOf() const = 0;
39 
40   private:
41     virtual int getImpl(PowerStatistic* stat) const = 0;
42     virtual int getImpl(const PowerStatistic& start, PowerStatistic* interval) const = 0;
43     virtual void dumpImpl(const PowerStatistic& stat, std::ostream* output) const = 0;
44 };
45 
46 /**
47  * This class is used to return stats in the form of key/value pairs for all registered classes
48  * that implement IPowerStatProvider.
49  **/
50 class PowerStatsCollector {
51   public:
52     PowerStatsCollector() = default;
53     int get(std::vector<PowerStatistic>* stats) const;
54     int get(const std::vector<PowerStatistic>& start, std::vector<PowerStatistic>* interval) const;
55     void dump(const std::vector<PowerStatistic>& stats, std::ostream* output) const;
56     void addDataProvider(std::unique_ptr<IPowerStatProvider> statProvider);
57 
58   private:
59     std::unordered_map<PowerStatCase, std::unique_ptr<IPowerStatProvider>> mStatProviders;
60 };
61 
62 int run(int argc, char** argv, const PowerStatsCollector& collector);
63 
64 #endif  // POWERSTATSCOLLECTOR_H
65