xref: /aosp_15_r20/frameworks/native/services/stats/StatsHal.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #define DEBUG false  // STOPSHIP if true
18*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "StatsHal"
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include "StatsHal.h"
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker #include <log/log.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <statslog.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker namespace android {
26*38e8c45fSAndroid Build Coastguard Worker namespace frameworks {
27*38e8c45fSAndroid Build Coastguard Worker namespace stats {
28*38e8c45fSAndroid Build Coastguard Worker namespace V1_0 {
29*38e8c45fSAndroid Build Coastguard Worker namespace implementation {
30*38e8c45fSAndroid Build Coastguard Worker 
StatsHal()31*38e8c45fSAndroid Build Coastguard Worker StatsHal::StatsHal() {
32*38e8c45fSAndroid Build Coastguard Worker }
33*38e8c45fSAndroid Build Coastguard Worker 
reportSpeakerImpedance(const SpeakerImpedance & speakerImpedance)34*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportSpeakerImpedance(const SpeakerImpedance& speakerImpedance) {
35*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(android::util::SPEAKER_IMPEDANCE_REPORTED,
36*38e8c45fSAndroid Build Coastguard Worker                                speakerImpedance.speakerLocation, speakerImpedance.milliOhms);
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
39*38e8c45fSAndroid Build Coastguard Worker }
40*38e8c45fSAndroid Build Coastguard Worker 
reportHardwareFailed(const HardwareFailed & hardwareFailed)41*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportHardwareFailed(const HardwareFailed& hardwareFailed) {
42*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(android::util::HARDWARE_FAILED, int32_t(hardwareFailed.hardwareType),
43*38e8c45fSAndroid Build Coastguard Worker                                hardwareFailed.hardwareLocation, int32_t(hardwareFailed.errorCode));
44*38e8c45fSAndroid Build Coastguard Worker 
45*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
46*38e8c45fSAndroid Build Coastguard Worker }
47*38e8c45fSAndroid Build Coastguard Worker 
reportPhysicalDropDetected(const PhysicalDropDetected & physicalDropDetected)48*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportPhysicalDropDetected(
49*38e8c45fSAndroid Build Coastguard Worker         const PhysicalDropDetected& physicalDropDetected) {
50*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(
51*38e8c45fSAndroid Build Coastguard Worker             android::util::PHYSICAL_DROP_DETECTED, int32_t(physicalDropDetected.confidencePctg),
52*38e8c45fSAndroid Build Coastguard Worker             physicalDropDetected.accelPeak, physicalDropDetected.freefallDuration);
53*38e8c45fSAndroid Build Coastguard Worker 
54*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker 
reportChargeCycles(const ChargeCycles & chargeCycles)57*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportChargeCycles(const ChargeCycles& chargeCycles) {
58*38e8c45fSAndroid Build Coastguard Worker     std::vector<int32_t> buckets = chargeCycles.cycleBucket;
59*38e8c45fSAndroid Build Coastguard Worker     int initialSize = buckets.size();
60*38e8c45fSAndroid Build Coastguard Worker     for (int i = 0; i < 10 - initialSize; i++) {
61*38e8c45fSAndroid Build Coastguard Worker         buckets.push_back(0);  // Push 0 for buckets that do not exist.
62*38e8c45fSAndroid Build Coastguard Worker     }
63*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(android::util::CHARGE_CYCLES_REPORTED, buckets[0], buckets[1],
64*38e8c45fSAndroid Build Coastguard Worker                                buckets[2], buckets[3], buckets[4], buckets[5], buckets[6],
65*38e8c45fSAndroid Build Coastguard Worker                                buckets[7], buckets[8], buckets[9]);
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
68*38e8c45fSAndroid Build Coastguard Worker }
69*38e8c45fSAndroid Build Coastguard Worker 
reportBatteryHealthSnapshot(const BatteryHealthSnapshotArgs & batteryHealthSnapshotArgs)70*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportBatteryHealthSnapshot(
71*38e8c45fSAndroid Build Coastguard Worker         const BatteryHealthSnapshotArgs& batteryHealthSnapshotArgs) {
72*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(
73*38e8c45fSAndroid Build Coastguard Worker             android::util::BATTERY_HEALTH_SNAPSHOT, int32_t(batteryHealthSnapshotArgs.type),
74*38e8c45fSAndroid Build Coastguard Worker             batteryHealthSnapshotArgs.temperatureDeciC, batteryHealthSnapshotArgs.voltageMicroV,
75*38e8c45fSAndroid Build Coastguard Worker             batteryHealthSnapshotArgs.currentMicroA,
76*38e8c45fSAndroid Build Coastguard Worker             batteryHealthSnapshotArgs.openCircuitVoltageMicroV,
77*38e8c45fSAndroid Build Coastguard Worker             batteryHealthSnapshotArgs.resistanceMicroOhm, batteryHealthSnapshotArgs.levelPercent);
78*38e8c45fSAndroid Build Coastguard Worker 
79*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
80*38e8c45fSAndroid Build Coastguard Worker }
81*38e8c45fSAndroid Build Coastguard Worker 
reportSlowIo(const SlowIo & slowIo)82*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportSlowIo(const SlowIo& slowIo) {
83*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(android::util::SLOW_IO, int32_t(slowIo.operation), slowIo.count);
84*38e8c45fSAndroid Build Coastguard Worker 
85*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
86*38e8c45fSAndroid Build Coastguard Worker }
87*38e8c45fSAndroid Build Coastguard Worker 
reportBatteryCausedShutdown(const BatteryCausedShutdown & batteryCausedShutdown)88*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportBatteryCausedShutdown(
89*38e8c45fSAndroid Build Coastguard Worker         const BatteryCausedShutdown& batteryCausedShutdown) {
90*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(android::util::BATTERY_CAUSED_SHUTDOWN,
91*38e8c45fSAndroid Build Coastguard Worker                                batteryCausedShutdown.voltageMicroV);
92*38e8c45fSAndroid Build Coastguard Worker 
93*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
94*38e8c45fSAndroid Build Coastguard Worker }
95*38e8c45fSAndroid Build Coastguard Worker 
reportUsbPortOverheatEvent(const UsbPortOverheatEvent & usbPortOverheatEvent)96*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportUsbPortOverheatEvent(
97*38e8c45fSAndroid Build Coastguard Worker         const UsbPortOverheatEvent& usbPortOverheatEvent) {
98*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(
99*38e8c45fSAndroid Build Coastguard Worker             android::util::USB_PORT_OVERHEAT_EVENT_REPORTED,
100*38e8c45fSAndroid Build Coastguard Worker             usbPortOverheatEvent.plugTemperatureDeciC, usbPortOverheatEvent.maxTemperatureDeciC,
101*38e8c45fSAndroid Build Coastguard Worker             usbPortOverheatEvent.timeToOverheat, usbPortOverheatEvent.timeToHysteresis,
102*38e8c45fSAndroid Build Coastguard Worker             usbPortOverheatEvent.timeToInactive);
103*38e8c45fSAndroid Build Coastguard Worker 
104*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
105*38e8c45fSAndroid Build Coastguard Worker }
106*38e8c45fSAndroid Build Coastguard Worker 
reportSpeechDspStat(const SpeechDspStat & speechDspStat)107*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportSpeechDspStat(const SpeechDspStat& speechDspStat) {
108*38e8c45fSAndroid Build Coastguard Worker     android::util::stats_write(android::util::SPEECH_DSP_STAT_REPORTED,
109*38e8c45fSAndroid Build Coastguard Worker                                speechDspStat.totalUptimeMillis, speechDspStat.totalDowntimeMillis,
110*38e8c45fSAndroid Build Coastguard Worker                                speechDspStat.totalCrashCount, speechDspStat.totalRecoverCount);
111*38e8c45fSAndroid Build Coastguard Worker 
112*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
113*38e8c45fSAndroid Build Coastguard Worker }
114*38e8c45fSAndroid Build Coastguard Worker 
reportVendorAtom(const VendorAtom & vendorAtom)115*38e8c45fSAndroid Build Coastguard Worker hardware::Return<void> StatsHal::reportVendorAtom(const VendorAtom& vendorAtom) {
116*38e8c45fSAndroid Build Coastguard Worker     if (vendorAtom.atomId < 100000 || vendorAtom.atomId >= 200000) {
117*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Atom ID %ld is not a valid vendor atom ID", (long)vendorAtom.atomId);
118*38e8c45fSAndroid Build Coastguard Worker         return hardware::Void();
119*38e8c45fSAndroid Build Coastguard Worker     }
120*38e8c45fSAndroid Build Coastguard Worker     if (vendorAtom.reverseDomainName.size() > 50) {
121*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Vendor atom reverse domain name %s is too long.",
122*38e8c45fSAndroid Build Coastguard Worker               vendorAtom.reverseDomainName.c_str());
123*38e8c45fSAndroid Build Coastguard Worker         return hardware::Void();
124*38e8c45fSAndroid Build Coastguard Worker     }
125*38e8c45fSAndroid Build Coastguard Worker     AStatsEvent* event = AStatsEvent_obtain();
126*38e8c45fSAndroid Build Coastguard Worker     AStatsEvent_setAtomId(event, vendorAtom.atomId);
127*38e8c45fSAndroid Build Coastguard Worker     AStatsEvent_writeString(event, vendorAtom.reverseDomainName.c_str());
128*38e8c45fSAndroid Build Coastguard Worker     for (int i = 0; i < (int)vendorAtom.values.size(); i++) {
129*38e8c45fSAndroid Build Coastguard Worker         switch (vendorAtom.values[i].getDiscriminator()) {
130*38e8c45fSAndroid Build Coastguard Worker             case VendorAtom::Value::hidl_discriminator::intValue:
131*38e8c45fSAndroid Build Coastguard Worker                 AStatsEvent_writeInt32(event, vendorAtom.values[i].intValue());
132*38e8c45fSAndroid Build Coastguard Worker                 break;
133*38e8c45fSAndroid Build Coastguard Worker             case VendorAtom::Value::hidl_discriminator::longValue:
134*38e8c45fSAndroid Build Coastguard Worker                 AStatsEvent_writeInt64(event, vendorAtom.values[i].longValue());
135*38e8c45fSAndroid Build Coastguard Worker                 break;
136*38e8c45fSAndroid Build Coastguard Worker             case VendorAtom::Value::hidl_discriminator::floatValue:
137*38e8c45fSAndroid Build Coastguard Worker                 AStatsEvent_writeFloat(event, vendorAtom.values[i].floatValue());
138*38e8c45fSAndroid Build Coastguard Worker                 break;
139*38e8c45fSAndroid Build Coastguard Worker             case VendorAtom::Value::hidl_discriminator::stringValue:
140*38e8c45fSAndroid Build Coastguard Worker                 AStatsEvent_writeString(event, vendorAtom.values[i].stringValue().c_str());
141*38e8c45fSAndroid Build Coastguard Worker                 break;
142*38e8c45fSAndroid Build Coastguard Worker         }
143*38e8c45fSAndroid Build Coastguard Worker     }
144*38e8c45fSAndroid Build Coastguard Worker     AStatsEvent_build(event);
145*38e8c45fSAndroid Build Coastguard Worker     AStatsEvent_write(event);
146*38e8c45fSAndroid Build Coastguard Worker     AStatsEvent_release(event);
147*38e8c45fSAndroid Build Coastguard Worker 
148*38e8c45fSAndroid Build Coastguard Worker     return hardware::Void();
149*38e8c45fSAndroid Build Coastguard Worker }
150*38e8c45fSAndroid Build Coastguard Worker 
151*38e8c45fSAndroid Build Coastguard Worker }  // namespace implementation
152*38e8c45fSAndroid Build Coastguard Worker }  // namespace V1_0
153*38e8c45fSAndroid Build Coastguard Worker }  // namespace stats
154*38e8c45fSAndroid Build Coastguard Worker }  // namespace frameworks
155*38e8c45fSAndroid Build Coastguard Worker }  // namespace android
156