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 <PowerStats.h> 20 21 #include <android-base/chrono_utils.h> 22 23 #include <chrono> 24 #include <random> 25 26 namespace aidl { 27 namespace android { 28 namespace hardware { 29 namespace power { 30 namespace stats { 31 32 class FakeEnergyConsumer : public PowerStats::IEnergyConsumer { 33 public: FakeEnergyConsumer(EnergyConsumerType type,std::string name)34 FakeEnergyConsumer(EnergyConsumerType type, std::string name) : mType(type), mName(name) { 35 mResult.timestampMs = 0; 36 mResult.energyUWs = 0; 37 mResult.attribution = {}; 38 } 39 40 ~FakeEnergyConsumer() = default; 41 getName()42 std::string getName() override { return mName; } 43 getType()44 EnergyConsumerType getType() override { return mType; } 45 getEnergyConsumed()46 std::optional<EnergyConsumerResult> getEnergyConsumed() override { 47 mFakeEnergyConsumerResult.update(&mResult); 48 return mResult; 49 } 50 51 private: 52 class FakeEnergyConsumerResult { 53 public: FakeEnergyConsumerResult()54 FakeEnergyConsumerResult() : mDistribution(1, 100) {} update(EnergyConsumerResult * result)55 void update(EnergyConsumerResult* result) { 56 // generates number in the range 1..100 57 auto randNum = std::bind(mDistribution, mGenerator); 58 59 // Get current time since boot in milliseconds 60 uint64_t now = std::chrono::time_point_cast<std::chrono::milliseconds>( 61 ::android::base::boot_clock::now()) 62 .time_since_epoch() 63 .count(); 64 result->timestampMs = now; 65 result->energyUWs += randNum() * 100; 66 } 67 68 private: 69 std::default_random_engine mGenerator; 70 std::uniform_int_distribution<int> mDistribution; 71 }; 72 73 EnergyConsumerType mType; 74 std::string mName; 75 FakeEnergyConsumerResult mFakeEnergyConsumerResult; 76 EnergyConsumerResult mResult; 77 }; 78 79 } // namespace stats 80 } // namespace power 81 } // namespace hardware 82 } // namespace android 83 } // namespace aidl