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 <log/log.h>
21 #include <statslog.h>
22 
23 #include "StatsHal.h"
24 
25 namespace android {
26 namespace frameworks {
27 namespace stats {
28 namespace V1_0 {
29 namespace implementation {
30 
StatsHal()31 StatsHal::StatsHal() {}
32 
reportSpeakerImpedance(const SpeakerImpedance & speakerImpedance)33 hardware::Return<void> StatsHal::reportSpeakerImpedance(
34         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(android::util::PHYSICAL_DROP_DETECTED,
51             int32_t(physicalDropDetected.confidencePctg), physicalDropDetected.accelPeak,
52             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(-1); // Push -1 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], buckets[7], buckets[8],
65             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(android::util::BATTERY_HEALTH_SNAPSHOT,
73             int32_t(batteryHealthSnapshotArgs.type), batteryHealthSnapshotArgs.temperatureDeciC,
74             batteryHealthSnapshotArgs.voltageMicroV, batteryHealthSnapshotArgs.currentMicroA,
75             batteryHealthSnapshotArgs.openCircuitVoltageMicroV,
76             batteryHealthSnapshotArgs.resistanceMicroOhm, batteryHealthSnapshotArgs.levelPercent);
77 
78     return hardware::Void();
79 }
80 
reportSlowIo(const SlowIo & slowIo)81 hardware::Return<void> StatsHal::reportSlowIo(const SlowIo& slowIo) {
82     android::util::stats_write(android::util::SLOW_IO, int32_t(slowIo.operation), slowIo.count);
83 
84     return hardware::Void();
85 }
86 
reportBatteryCausedShutdown(const BatteryCausedShutdown & batteryCausedShutdown)87 hardware::Return<void> StatsHal::reportBatteryCausedShutdown(
88         const BatteryCausedShutdown& batteryCausedShutdown) {
89     android::util::stats_write(android::util::BATTERY_CAUSED_SHUTDOWN,
90             batteryCausedShutdown.voltageMicroV);
91 
92     return hardware::Void();
93 }
94 
reportUsbPortOverheatEvent(const UsbPortOverheatEvent & usbPortOverheatEvent)95 hardware::Return<void> StatsHal::reportUsbPortOverheatEvent(
96         const UsbPortOverheatEvent& usbPortOverheatEvent) {
97     android::util::stats_write(android::util::USB_PORT_OVERHEAT_EVENT_REPORTED,
98             usbPortOverheatEvent.plugTemperatureDeciC, usbPortOverheatEvent.maxTemperatureDeciC,
99             usbPortOverheatEvent.timeToOverheat, usbPortOverheatEvent.timeToHysteresis,
100             usbPortOverheatEvent.timeToInactive);
101 
102     return hardware::Void();
103 }
104 
reportSpeechDspStat(const SpeechDspStat & speechDspStat)105 hardware::Return<void> StatsHal::reportSpeechDspStat(
106         const SpeechDspStat& speechDspStat) {
107     android::util::stats_write(android::util::SPEECH_DSP_STAT_REPORTED,
108             speechDspStat.totalUptimeMillis, speechDspStat.totalDowntimeMillis,
109             speechDspStat.totalCrashCount, speechDspStat.totalRecoverCount);
110 
111     return hardware::Void();
112 }
113 
reportVendorAtom(const VendorAtom & vendorAtom)114 hardware::Return<void> StatsHal::reportVendorAtom(const VendorAtom& vendorAtom) {
115     std::string reverseDomainName = (std::string) vendorAtom.reverseDomainName;
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 (reverseDomainName.length() > 50) {
121         ALOGE("Vendor atom reverse domain name %s is too long.", reverseDomainName.c_str());
122         return hardware::Void();
123     }
124     AStatsEvent* event = AStatsEvent_obtain();
125     AStatsEvent_setAtomId(event, vendorAtom.atomId);
126     AStatsEvent_writeString(event, vendorAtom.reverseDomainName.c_str());
127     for (int i = 0; i < (int)vendorAtom.values.size(); i++) {
128         switch (vendorAtom.values[i].getDiscriminator()) {
129             case VendorAtom::Value::hidl_discriminator::intValue:
130                 AStatsEvent_writeInt32(event, vendorAtom.values[i].intValue());
131                 break;
132             case VendorAtom::Value::hidl_discriminator::longValue:
133                 AStatsEvent_writeInt64(event, vendorAtom.values[i].longValue());
134                 break;
135             case VendorAtom::Value::hidl_discriminator::floatValue:
136                 AStatsEvent_writeFloat(event, vendorAtom.values[i].floatValue());
137                 break;
138             case VendorAtom::Value::hidl_discriminator::stringValue:
139                 AStatsEvent_writeString(event, vendorAtom.values[i].stringValue().c_str());
140                 break;
141         }
142     }
143     AStatsEvent_build(event);
144     AStatsEvent_write(event);
145     AStatsEvent_release(event);
146 
147     return hardware::Void();
148 }
149 
150 }  // namespace implementation
151 }  // namespace V1_0
152 }  // namespace stats
153 }  // namespace frameworks
154 }  // namespace android
155