1 /*
2 * Copyright (C) 2021 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 #include <android/binder_manager.h>
18 #include <pixelstats/StatsHelper.h>
19
20 #define LOG_TAG "pixelstats-vendor"
21
22 #include <utils/Log.h>
23
24 namespace android {
25 namespace hardware {
26 namespace google {
27 namespace pixel {
28
29 using aidl::android::frameworks::stats::VendorAtom;
30 using aidl::android::frameworks::stats::VendorAtomValue;
31
32 // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
33 // store everything in the values array at the index of the field number
34 // -2.
35 const int kVendorAtomOffset = 2;
36
getStatsService()37 std::shared_ptr<IStats> getStatsService() {
38 const std::string instance = std::string() + IStats::descriptor + "/default";
39 static bool isStatsDeclared = false;
40 if (!isStatsDeclared) {
41 // It is good to cache the result - it would not be changed
42 isStatsDeclared = AServiceManager_isDeclared(instance.c_str());
43 if (!isStatsDeclared) {
44 ALOGE("Stats service is not registered.");
45 return nullptr;
46 }
47 }
48 return IStats::fromBinder(ndk::SpAIBinder(AServiceManager_waitForService(instance.c_str())));
49 }
50
reportSpeakerImpedance(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorSpeakerImpedance & speakerImpedance)51 void reportSpeakerImpedance(const std::shared_ptr<IStats> &stats_client,
52 const PixelAtoms::VendorSpeakerImpedance &speakerImpedance) {
53 // Load values array
54 std::vector<VendorAtomValue> values(2);
55 VendorAtomValue tmp;
56 tmp.set<VendorAtomValue::intValue>(speakerImpedance.speaker_location());
57 values[0] = tmp;
58 tmp.set<VendorAtomValue::intValue>(speakerImpedance.impedance());
59 values[1] = tmp;
60
61 // Send vendor atom to IStats HAL
62 VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
63 .atomId = PixelAtoms::Atom::kVendorSpeakerImpedance,
64 .values = std::move(values)};
65 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
66 if (!ret.isOk())
67 ALOGE("Unable to report VendorSpeakerImpedance to Stats service");
68 }
69
reportSlowIo(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorSlowIo & slowIo)70 void reportSlowIo(const std::shared_ptr<IStats> &stats_client,
71 const PixelAtoms::VendorSlowIo &slowIo) {
72 // Load values array
73 std::vector<VendorAtomValue> values(2);
74 VendorAtomValue tmp;
75 tmp.set<VendorAtomValue::intValue>(slowIo.operation());
76 values[0] = tmp;
77 tmp.set<VendorAtomValue::intValue>(slowIo.count());
78 values[1] = tmp;
79
80 // Send vendor atom to IStats HAL
81 VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
82 .atomId = PixelAtoms::Atom::kVendorSlowIo,
83 .values = std::move(values)};
84 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
85 if (!ret.isOk())
86 ALOGE("Unable to report VendorSlowIo to Stats service");
87 }
88
reportChargeCycles(const std::shared_ptr<IStats> & stats_client,const std::vector<int32_t> & chargeCycles)89 void reportChargeCycles(const std::shared_ptr<IStats> &stats_client,
90 const std::vector<int32_t> &chargeCycles) {
91 // Load values array
92 const int32_t kChargeCyclesBucketsCount =
93 PixelAtoms::VendorChargeCycles::kCycleBucket10FieldNumber - kVendorAtomOffset + 1;
94 std::vector<VendorAtomValue> values(kChargeCyclesBucketsCount);
95 VendorAtomValue tmp;
96 for (int32_t bucketIdx = 0; bucketIdx < kChargeCyclesBucketsCount; ++bucketIdx) {
97 tmp.set<VendorAtomValue::intValue>(chargeCycles[bucketIdx]);
98 values[bucketIdx] = tmp;
99 }
100
101 // Send vendor atom to IStats HAL
102 VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
103 .atomId = PixelAtoms::Atom::kVendorChargeCycles,
104 .values = std::move(values)};
105 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
106 if (!ret.isOk())
107 ALOGE("Unable to report VendorChargeCycles to Stats service");
108 }
109
reportHardwareFailed(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorHardwareFailed & failure)110 void reportHardwareFailed(const std::shared_ptr<IStats> &stats_client,
111 const PixelAtoms::VendorHardwareFailed &failure) {
112 // Load values array
113 std::vector<VendorAtomValue> values(3);
114 VendorAtomValue tmp;
115 tmp.set<VendorAtomValue::intValue>(failure.hardware_type());
116 values[0] = tmp;
117 tmp.set<VendorAtomValue::intValue>(failure.hardware_location());
118 values[1] = tmp;
119 tmp.set<VendorAtomValue::intValue>(failure.failure_code());
120 values[2] = tmp;
121
122 // Send vendor atom to IStats HAL
123 VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
124 .atomId = PixelAtoms::Atom::kVendorHardwareFailed,
125 .values = std::move(values)};
126 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
127 if (!ret.isOk())
128 ALOGE("Unable to report VendorHardwareFailed to Stats service");
129 }
130
reportSpeechDspStat(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorSpeechDspStat & dsp_stats)131 void reportSpeechDspStat(const std::shared_ptr<IStats> &stats_client,
132 const PixelAtoms::VendorSpeechDspStat &dsp_stats) {
133 // Load values array
134 std::vector<VendorAtomValue> values(4);
135 VendorAtomValue tmp;
136 tmp.set<VendorAtomValue::intValue>(dsp_stats.total_uptime_millis());
137 values[0] = tmp;
138 tmp.set<VendorAtomValue::intValue>(dsp_stats.total_downtime_millis());
139 values[1] = tmp;
140 tmp.set<VendorAtomValue::intValue>(dsp_stats.total_crash_count());
141 values[2] = tmp;
142 tmp.set<VendorAtomValue::intValue>(dsp_stats.total_recover_count());
143 values[3] = tmp;
144
145 // Send vendor atom to IStats HAL
146 VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
147 .atomId = PixelAtoms::Atom::kVendorSpeechDspStat,
148 .values = std::move(values)};
149 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
150 if (!ret.isOk())
151 ALOGE("Unable to report VendorSpeechDspStat to Stats service");
152 }
153
reportPhysicalDropDetected(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorPhysicalDropDetected & dropDetected)154 void reportPhysicalDropDetected(const std::shared_ptr<IStats> &stats_client,
155 const PixelAtoms::VendorPhysicalDropDetected &dropDetected) {
156 // Load values array
157 std::vector<VendorAtomValue> values(3);
158 VendorAtomValue tmp;
159 tmp.set<VendorAtomValue::intValue>(dropDetected.confidence_pctg());
160 values[0] = tmp;
161 tmp.set<VendorAtomValue::intValue>(dropDetected.accel_peak_thousandths_g());
162 values[1] = tmp;
163 tmp.set<VendorAtomValue::intValue>(dropDetected.freefall_time_millis());
164 values[2] = tmp;
165
166 // Send vendor atom to IStats HAL
167 VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
168 .atomId = PixelAtoms::Atom::kVendorPhysicalDropDetected,
169 .values = std::move(values)};
170 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
171 if (!ret.isOk())
172 ALOGE("Unable to report VendorPhysicalDropDetected to Stats service");
173 }
174
reportUsbPortOverheat(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorUsbPortOverheat & overheat_info)175 void reportUsbPortOverheat(const std::shared_ptr<IStats> &stats_client,
176 const PixelAtoms::VendorUsbPortOverheat &overheat_info) {
177 // Load values array
178 std::vector<VendorAtomValue> values(5);
179 VendorAtomValue tmp;
180 tmp.set<VendorAtomValue::intValue>(overheat_info.plug_temperature_deci_c());
181 values[0] = tmp;
182 tmp.set<VendorAtomValue::intValue>(overheat_info.max_temperature_deci_c());
183 values[1] = tmp;
184 tmp.set<VendorAtomValue::intValue>(overheat_info.time_to_overheat_secs());
185 values[2] = tmp;
186 tmp.set<VendorAtomValue::intValue>(overheat_info.time_to_hysteresis_secs());
187 values[3] = tmp;
188 tmp.set<VendorAtomValue::intValue>(overheat_info.time_to_inactive_secs());
189 values[4] = tmp;
190
191 // Send vendor atom to IStats HAL
192 VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
193 .atomId = PixelAtoms::Atom::kVendorUsbPortOverheat,
194 .values = std::move(values)};
195 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
196 if (!ret.isOk())
197 ALOGE("Unable to report VendorUsbPortOverheat to Stats service");
198 }
199
200 } // namespace pixel
201 } // namespace google
202 } // namespace hardware
203 } // namespace android
204