1 /*
2 * Copyright (C) 2024 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 #include <android-base/logging.h>
17 #include <psh_utils/HealthStats.h>
18
19 namespace android::media::psh_utils {
20
21 template <typename T>
choose_voltage(const T & a,const T & b)22 const T& choose_voltage(const T& a, const T& b) {
23 return std::max(a, b); // we use max here, could use avg.
24 }
25
toString() const26 std::string HealthStats::toString() const {
27 std::string result;
28 const float batteryVoltage = batteryVoltageMillivolts * 1e-3f; // Volts
29 const float charge = batteryChargeCounterUah * (3600 * 1e-6); // Joules = Amp-Second
30 result.append("{Net Battery V: ")
31 .append(std::to_string(batteryVoltage))
32 .append(" J: ")
33 .append(std::to_string(charge))
34 .append("}");
35 return result;
36 }
37
normalizedEnergy(double timeSec) const38 std::string HealthStats::normalizedEnergy(double timeSec) const {
39 std::string result;
40 const float batteryVoltage = batteryVoltageMillivolts * 1e-3f; // Volts
41 const float charge = -batteryChargeCounterUah * (3600 * 1e-6f); // Joules = Amp-Second
42 const float watts = charge * batteryVoltage / timeSec;
43 result.append("{Net Battery V: ")
44 .append(std::to_string(batteryVoltage))
45 .append(" J: ")
46 .append(std::to_string(charge))
47 .append(" W: ")
48 .append(std::to_string(watts))
49 .append("}");
50 return result;
51 }
52
operator +=(const HealthStats & other)53 HealthStats HealthStats::operator+=(const HealthStats& other) {
54 batteryVoltageMillivolts = choose_voltage(
55 batteryVoltageMillivolts, other.batteryVoltageMillivolts);
56 batteryFullChargeUah = std::max(batteryFullChargeUah, other.batteryFullChargeUah);
57 batteryChargeCounterUah += other.batteryChargeCounterUah;
58 return *this;
59 }
60
operator -=(const HealthStats & other)61 HealthStats HealthStats::operator-=(const HealthStats& other) {
62 batteryVoltageMillivolts = choose_voltage(
63 batteryVoltageMillivolts, other.batteryVoltageMillivolts);
64 batteryFullChargeUah = std::max(batteryFullChargeUah, other.batteryFullChargeUah);
65 batteryChargeCounterUah -= other.batteryChargeCounterUah;
66 return *this;
67 }
68
operator +(const HealthStats & other) const69 HealthStats HealthStats::operator+(const HealthStats& other) const {
70 HealthStats result = *this;
71 result += other;
72 return result;
73 }
74
operator -(const HealthStats & other) const75 HealthStats HealthStats::operator-(const HealthStats& other) const {
76 HealthStats result = *this;
77 result -= other;
78 return result;
79 }
80
81 } // namespace android::media::psh_utils
82