1 /*
2  * Copyright (C) 2020 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 #define DEBUG false  // STOPSHIP if true
18 #define LOG_TAG "StatsHal"
19 
20 #include "StatsHal.h"
21 
22 #include <log/log.h>
23 #include <statslog.h>
24 
25 namespace android {
26 namespace frameworks {
27 namespace stats {
28 namespace V1_0 {
29 namespace implementation {
30 
StatsHal()31 StatsHal::StatsHal() {
32 }
33 
reportSpeakerImpedance(const SpeakerImpedance & speakerImpedance)34 hardware::Return<void> StatsHal::reportSpeakerImpedance(const SpeakerImpedance& speakerImpedance) {
35     android::util::stats_write(android::util::SPEAKER_IMPEDANCE_REPORTED,
36                                speakerImpedance.speakerLocation, speakerImpedance.milliOhms);
37 
38     return hardware::Void();
39 }
40 
reportHardwareFailed(const HardwareFailed & hardwareFailed)41 hardware::Return<void> StatsHal::reportHardwareFailed(const HardwareFailed& hardwareFailed) {
42     android::util::stats_write(android::util::HARDWARE_FAILED, int32_t(hardwareFailed.hardwareType),
43                                hardwareFailed.hardwareLocation, int32_t(hardwareFailed.errorCode));
44 
45     return hardware::Void();
46 }
47 
reportPhysicalDropDetected(const PhysicalDropDetected & physicalDropDetected)48 hardware::Return<void> StatsHal::reportPhysicalDropDetected(
49         const PhysicalDropDetected& physicalDropDetected) {
50     android::util::stats_write(
51             android::util::PHYSICAL_DROP_DETECTED, int32_t(physicalDropDetected.confidencePctg),
52             physicalDropDetected.accelPeak, physicalDropDetected.freefallDuration);
53 
54     return hardware::Void();
55 }
56 
reportChargeCycles(const ChargeCycles & chargeCycles)57 hardware::Return<void> StatsHal::reportChargeCycles(const ChargeCycles& chargeCycles) {
58     std::vector<int32_t> buckets = chargeCycles.cycleBucket;
59     int initialSize = buckets.size();
60     for (int i = 0; i < 10 - initialSize; i++) {
61         buckets.push_back(0);  // Push 0 for buckets that do not exist.
62     }
63     android::util::stats_write(android::util::CHARGE_CYCLES_REPORTED, buckets[0], buckets[1],
64                                buckets[2], buckets[3], buckets[4], buckets[5], buckets[6],
65                                buckets[7], buckets[8], buckets[9]);
66 
67     return hardware::Void();
68 }
69 
reportBatteryHealthSnapshot(const BatteryHealthSnapshotArgs & batteryHealthSnapshotArgs)70 hardware::Return<void> StatsHal::reportBatteryHealthSnapshot(
71         const BatteryHealthSnapshotArgs& batteryHealthSnapshotArgs) {
72     android::util::stats_write(
73             android::util::BATTERY_HEALTH_SNAPSHOT, int32_t(batteryHealthSnapshotArgs.type),
74             batteryHealthSnapshotArgs.temperatureDeciC, batteryHealthSnapshotArgs.voltageMicroV,
75             batteryHealthSnapshotArgs.currentMicroA,
76             batteryHealthSnapshotArgs.openCircuitVoltageMicroV,
77             batteryHealthSnapshotArgs.resistanceMicroOhm, batteryHealthSnapshotArgs.levelPercent);
78 
79     return hardware::Void();
80 }
81 
reportSlowIo(const SlowIo & slowIo)82 hardware::Return<void> StatsHal::reportSlowIo(const SlowIo& slowIo) {
83     android::util::stats_write(android::util::SLOW_IO, int32_t(slowIo.operation), slowIo.count);
84 
85     return hardware::Void();
86 }
87 
reportBatteryCausedShutdown(const BatteryCausedShutdown & batteryCausedShutdown)88 hardware::Return<void> StatsHal::reportBatteryCausedShutdown(
89         const BatteryCausedShutdown& batteryCausedShutdown) {
90     android::util::stats_write(android::util::BATTERY_CAUSED_SHUTDOWN,
91                                batteryCausedShutdown.voltageMicroV);
92 
93     return hardware::Void();
94 }
95 
reportUsbPortOverheatEvent(const UsbPortOverheatEvent & usbPortOverheatEvent)96 hardware::Return<void> StatsHal::reportUsbPortOverheatEvent(
97         const UsbPortOverheatEvent& usbPortOverheatEvent) {
98     android::util::stats_write(
99             android::util::USB_PORT_OVERHEAT_EVENT_REPORTED,
100             usbPortOverheatEvent.plugTemperatureDeciC, usbPortOverheatEvent.maxTemperatureDeciC,
101             usbPortOverheatEvent.timeToOverheat, usbPortOverheatEvent.timeToHysteresis,
102             usbPortOverheatEvent.timeToInactive);
103 
104     return hardware::Void();
105 }
106 
reportSpeechDspStat(const SpeechDspStat & speechDspStat)107 hardware::Return<void> StatsHal::reportSpeechDspStat(const SpeechDspStat& speechDspStat) {
108     android::util::stats_write(android::util::SPEECH_DSP_STAT_REPORTED,
109                                speechDspStat.totalUptimeMillis, speechDspStat.totalDowntimeMillis,
110                                speechDspStat.totalCrashCount, speechDspStat.totalRecoverCount);
111 
112     return hardware::Void();
113 }
114 
reportVendorAtom(const VendorAtom & vendorAtom)115 hardware::Return<void> StatsHal::reportVendorAtom(const VendorAtom& vendorAtom) {
116     if (vendorAtom.atomId < 100000 || vendorAtom.atomId >= 200000) {
117         ALOGE("Atom ID %ld is not a valid vendor atom ID", (long)vendorAtom.atomId);
118         return hardware::Void();
119     }
120     if (vendorAtom.reverseDomainName.size() > 50) {
121         ALOGE("Vendor atom reverse domain name %s is too long.",
122               vendorAtom.reverseDomainName.c_str());
123         return hardware::Void();
124     }
125     AStatsEvent* event = AStatsEvent_obtain();
126     AStatsEvent_setAtomId(event, vendorAtom.atomId);
127     AStatsEvent_writeString(event, vendorAtom.reverseDomainName.c_str());
128     for (int i = 0; i < (int)vendorAtom.values.size(); i++) {
129         switch (vendorAtom.values[i].getDiscriminator()) {
130             case VendorAtom::Value::hidl_discriminator::intValue:
131                 AStatsEvent_writeInt32(event, vendorAtom.values[i].intValue());
132                 break;
133             case VendorAtom::Value::hidl_discriminator::longValue:
134                 AStatsEvent_writeInt64(event, vendorAtom.values[i].longValue());
135                 break;
136             case VendorAtom::Value::hidl_discriminator::floatValue:
137                 AStatsEvent_writeFloat(event, vendorAtom.values[i].floatValue());
138                 break;
139             case VendorAtom::Value::hidl_discriminator::stringValue:
140                 AStatsEvent_writeString(event, vendorAtom.values[i].stringValue().c_str());
141                 break;
142         }
143     }
144     AStatsEvent_build(event);
145     AStatsEvent_write(event);
146     AStatsEvent_release(event);
147 
148     return hardware::Void();
149 }
150 
151 }  // namespace implementation
152 }  // namespace V1_0
153 }  // namespace stats
154 }  // namespace frameworks
155 }  // namespace android
156