1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker * Copyright (C) 2024 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker *
4*ec779b8eSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker *
8*ec779b8eSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker *
10*ec779b8eSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker */
16*ec779b8eSAndroid Build Coastguard Worker #include <android-base/logging.h>
17*ec779b8eSAndroid Build Coastguard Worker #include <psh_utils/HealthStats.h>
18*ec779b8eSAndroid Build Coastguard Worker
19*ec779b8eSAndroid Build Coastguard Worker namespace android::media::psh_utils {
20*ec779b8eSAndroid Build Coastguard Worker
21*ec779b8eSAndroid Build Coastguard Worker template <typename T>
choose_voltage(const T & a,const T & b)22*ec779b8eSAndroid Build Coastguard Worker const T& choose_voltage(const T& a, const T& b) {
23*ec779b8eSAndroid Build Coastguard Worker return std::max(a, b); // we use max here, could use avg.
24*ec779b8eSAndroid Build Coastguard Worker }
25*ec779b8eSAndroid Build Coastguard Worker
toString() const26*ec779b8eSAndroid Build Coastguard Worker std::string HealthStats::toString() const {
27*ec779b8eSAndroid Build Coastguard Worker std::string result;
28*ec779b8eSAndroid Build Coastguard Worker const float batteryVoltage = batteryVoltageMillivolts * 1e-3f; // Volts
29*ec779b8eSAndroid Build Coastguard Worker const float charge = batteryChargeCounterUah * (3600 * 1e-6); // Joules = Amp-Second
30*ec779b8eSAndroid Build Coastguard Worker result.append("{Net Battery V: ")
31*ec779b8eSAndroid Build Coastguard Worker .append(std::to_string(batteryVoltage))
32*ec779b8eSAndroid Build Coastguard Worker .append(" J: ")
33*ec779b8eSAndroid Build Coastguard Worker .append(std::to_string(charge))
34*ec779b8eSAndroid Build Coastguard Worker .append("}");
35*ec779b8eSAndroid Build Coastguard Worker return result;
36*ec779b8eSAndroid Build Coastguard Worker }
37*ec779b8eSAndroid Build Coastguard Worker
normalizedEnergy(double timeSec) const38*ec779b8eSAndroid Build Coastguard Worker std::string HealthStats::normalizedEnergy(double timeSec) const {
39*ec779b8eSAndroid Build Coastguard Worker std::string result;
40*ec779b8eSAndroid Build Coastguard Worker const float batteryVoltage = batteryVoltageMillivolts * 1e-3f; // Volts
41*ec779b8eSAndroid Build Coastguard Worker const float charge = -batteryChargeCounterUah * (3600 * 1e-6f); // Joules = Amp-Second
42*ec779b8eSAndroid Build Coastguard Worker const float watts = charge * batteryVoltage / timeSec;
43*ec779b8eSAndroid Build Coastguard Worker result.append("{Net Battery V: ")
44*ec779b8eSAndroid Build Coastguard Worker .append(std::to_string(batteryVoltage))
45*ec779b8eSAndroid Build Coastguard Worker .append(" J: ")
46*ec779b8eSAndroid Build Coastguard Worker .append(std::to_string(charge))
47*ec779b8eSAndroid Build Coastguard Worker .append(" W: ")
48*ec779b8eSAndroid Build Coastguard Worker .append(std::to_string(watts))
49*ec779b8eSAndroid Build Coastguard Worker .append("}");
50*ec779b8eSAndroid Build Coastguard Worker return result;
51*ec779b8eSAndroid Build Coastguard Worker }
52*ec779b8eSAndroid Build Coastguard Worker
operator +=(const HealthStats & other)53*ec779b8eSAndroid Build Coastguard Worker HealthStats HealthStats::operator+=(const HealthStats& other) {
54*ec779b8eSAndroid Build Coastguard Worker batteryVoltageMillivolts = choose_voltage(
55*ec779b8eSAndroid Build Coastguard Worker batteryVoltageMillivolts, other.batteryVoltageMillivolts);
56*ec779b8eSAndroid Build Coastguard Worker batteryFullChargeUah = std::max(batteryFullChargeUah, other.batteryFullChargeUah);
57*ec779b8eSAndroid Build Coastguard Worker batteryChargeCounterUah += other.batteryChargeCounterUah;
58*ec779b8eSAndroid Build Coastguard Worker return *this;
59*ec779b8eSAndroid Build Coastguard Worker }
60*ec779b8eSAndroid Build Coastguard Worker
operator -=(const HealthStats & other)61*ec779b8eSAndroid Build Coastguard Worker HealthStats HealthStats::operator-=(const HealthStats& other) {
62*ec779b8eSAndroid Build Coastguard Worker batteryVoltageMillivolts = choose_voltage(
63*ec779b8eSAndroid Build Coastguard Worker batteryVoltageMillivolts, other.batteryVoltageMillivolts);
64*ec779b8eSAndroid Build Coastguard Worker batteryFullChargeUah = std::max(batteryFullChargeUah, other.batteryFullChargeUah);
65*ec779b8eSAndroid Build Coastguard Worker batteryChargeCounterUah -= other.batteryChargeCounterUah;
66*ec779b8eSAndroid Build Coastguard Worker return *this;
67*ec779b8eSAndroid Build Coastguard Worker }
68*ec779b8eSAndroid Build Coastguard Worker
operator +(const HealthStats & other) const69*ec779b8eSAndroid Build Coastguard Worker HealthStats HealthStats::operator+(const HealthStats& other) const {
70*ec779b8eSAndroid Build Coastguard Worker HealthStats result = *this;
71*ec779b8eSAndroid Build Coastguard Worker result += other;
72*ec779b8eSAndroid Build Coastguard Worker return result;
73*ec779b8eSAndroid Build Coastguard Worker }
74*ec779b8eSAndroid Build Coastguard Worker
operator -(const HealthStats & other) const75*ec779b8eSAndroid Build Coastguard Worker HealthStats HealthStats::operator-(const HealthStats& other) const {
76*ec779b8eSAndroid Build Coastguard Worker HealthStats result = *this;
77*ec779b8eSAndroid Build Coastguard Worker result -= other;
78*ec779b8eSAndroid Build Coastguard Worker return result;
79*ec779b8eSAndroid Build Coastguard Worker }
80*ec779b8eSAndroid Build Coastguard Worker
81*ec779b8eSAndroid Build Coastguard Worker } // namespace android::media::psh_utils
82