blob: f0d4ee7dfc15089e2d26cfe606c55c586206e2e2 [file] [log] [blame]
Benjamin Schwartz9c944a12021-03-01 13:29:11 -08001/*
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 <PowerStats.h>
20
21#include <android-base/chrono_utils.h>
22
23#include <chrono>
24#include <random>
25
26namespace aidl {
27namespace android {
28namespace hardware {
29namespace power {
30namespace stats {
31
32class FakeEnergyMeter : public PowerStats::IEnergyMeter {
33 public:
34 FakeEnergyMeter(std::vector<std::pair<std::string, std::string>> channelNames) {
35 int32_t channelId = 0;
36 for (const auto& [name, subsystem] : channelNames) {
37 Channel c;
38 c.id = channelId++;
39 c.name = name;
40 c.subsystem = subsystem;
41
42 EnergyMeasurement m;
43 m.id = c.id;
44 m.timestampMs = 0;
45 m.durationMs = 0;
46 m.energyUWs = 0;
47
48 mChannels.push_back(c);
49 mEnergyMeasurements.push_back(m);
50 }
51 }
52 ~FakeEnergyMeter() = default;
53 ndk::ScopedAStatus readEnergyMeter(const std::vector<int32_t>& in_channelIds,
54 std::vector<EnergyMeasurement>* _aidl_return) override {
55 for (auto& measurement : mEnergyMeasurements) {
56 mFakeEnergyMeasurement.update(&measurement);
57 }
58
59 if (in_channelIds.empty()) {
60 *_aidl_return = mEnergyMeasurements;
61 } else {
62 for (int32_t id : in_channelIds) {
63 if (id >= 0 && id < mEnergyMeasurements.size()) {
64 _aidl_return->push_back(mEnergyMeasurements[id]);
65 }
66 }
67 }
68
69 return ndk::ScopedAStatus::ok();
70 }
71
72 ndk::ScopedAStatus getEnergyMeterInfo(std::vector<Channel>* _aidl_return) override {
73 *_aidl_return = mChannels;
74 return ndk::ScopedAStatus::ok();
75 }
76
77 private:
78 class FakeEnergyMeasurement {
79 public:
80 FakeEnergyMeasurement() : mDistribution(1, 100) {}
81 void update(EnergyMeasurement* measurement) {
82 // generates number in the range 1..100
83 auto randNum = std::bind(mDistribution, mGenerator);
84
85 // Get current time since boot in milliseconds
86 uint64_t now = std::chrono::time_point_cast<std::chrono::milliseconds>(
87 ::android::base::boot_clock::now())
88 .time_since_epoch()
89 .count();
90 measurement->timestampMs = now;
91 measurement->durationMs = now;
92 measurement->energyUWs += randNum() * 100;
93 }
94
95 private:
96 std::default_random_engine mGenerator;
97 std::uniform_int_distribution<int> mDistribution;
98 };
99
100 std::vector<Channel> mChannels;
101 FakeEnergyMeasurement mFakeEnergyMeasurement;
102 std::vector<EnergyMeasurement> mEnergyMeasurements;
103};
104
105} // namespace stats
106} // namespace power
107} // namespace hardware
108} // namespace android
109} // namespace aidl