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 
reportSpeakerHealthStat(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorSpeakerStatsReported & speakerHealthStat)70 void reportSpeakerHealthStat(const std::shared_ptr<IStats> &stats_client,
71                              const PixelAtoms::VendorSpeakerStatsReported &speakerHealthStat) {
72     // Load values array
73     std::vector<VendorAtomValue> values(5);
74     VendorAtomValue tmp;
75     tmp.set<VendorAtomValue::intValue>(speakerHealthStat.speaker_location());
76     values[0] = tmp;
77     tmp.set<VendorAtomValue::intValue>(speakerHealthStat.impedance());
78     values[1] = tmp;
79     tmp.set<VendorAtomValue::intValue>(speakerHealthStat.max_temperature());
80     values[2] = tmp;
81     tmp.set<VendorAtomValue::intValue>(speakerHealthStat.excursion());
82     values[3] = tmp;
83     tmp.set<VendorAtomValue::intValue>(speakerHealthStat.heartbeat());
84     values[4] = tmp;
85 
86     // Send vendor atom to IStats HAL
87     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
88                         .atomId = PixelAtoms::Atom::kVendorSpeakerStatsReported,
89                         .values = std::move(values)};
90     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
91     if (!ret.isOk())
92         ALOGE("Unable to report VendorSpeakerStatsReported to Stats service");
93 }
94 
reportSlowIo(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorSlowIo & slowIo)95 void reportSlowIo(const std::shared_ptr<IStats> &stats_client,
96                   const PixelAtoms::VendorSlowIo &slowIo) {
97     // Load values array
98     std::vector<VendorAtomValue> values(2);
99     VendorAtomValue tmp;
100     tmp.set<VendorAtomValue::intValue>(slowIo.operation());
101     values[0] = tmp;
102     tmp.set<VendorAtomValue::intValue>(slowIo.count());
103     values[1] = tmp;
104 
105     // Send vendor atom to IStats HAL
106     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
107                         .atomId = PixelAtoms::Atom::kVendorSlowIo,
108                         .values = std::move(values)};
109     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
110     if (!ret.isOk())
111         ALOGE("Unable to report VendorSlowIo to Stats service");
112 }
113 
reportChargeCycles(const std::shared_ptr<IStats> & stats_client,const std::vector<int32_t> & chargeCycles)114 void reportChargeCycles(const std::shared_ptr<IStats> &stats_client,
115                         const std::vector<int32_t> &chargeCycles) {
116     // Load values array
117     const int32_t kChargeCyclesBucketsCount =
118             PixelAtoms::VendorChargeCycles::kCycleBucket10FieldNumber - kVendorAtomOffset + 1;
119     std::vector<VendorAtomValue> values(kChargeCyclesBucketsCount);
120     VendorAtomValue tmp;
121     for (int32_t bucketIdx = 0; bucketIdx < kChargeCyclesBucketsCount; ++bucketIdx) {
122         tmp.set<VendorAtomValue::intValue>(chargeCycles[bucketIdx]);
123         values[bucketIdx] = tmp;
124     }
125 
126     // Send vendor atom to IStats HAL
127     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
128                         .atomId = PixelAtoms::Atom::kVendorChargeCycles,
129                         .values = std::move(values)};
130     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
131     if (!ret.isOk())
132         ALOGE("Unable to report VendorChargeCycles to Stats service");
133 }
134 
reportHardwareFailed(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorHardwareFailed & failure)135 void reportHardwareFailed(const std::shared_ptr<IStats> &stats_client,
136                           const PixelAtoms::VendorHardwareFailed &failure) {
137     // Load values array
138     std::vector<VendorAtomValue> values(3);
139     VendorAtomValue tmp;
140     tmp.set<VendorAtomValue::intValue>(failure.hardware_type());
141     values[0] = tmp;
142     tmp.set<VendorAtomValue::intValue>(failure.hardware_location());
143     values[1] = tmp;
144     tmp.set<VendorAtomValue::intValue>(failure.failure_code());
145     values[2] = tmp;
146 
147     // Send vendor atom to IStats HAL
148     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
149                         .atomId = PixelAtoms::Atom::kVendorHardwareFailed,
150                         .values = std::move(values)};
151     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
152     if (!ret.isOk())
153         ALOGE("Unable to report VendorHardwareFailed to Stats service");
154 }
155 
reportSpeechDspStat(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorSpeechDspStat & dsp_stats)156 void reportSpeechDspStat(const std::shared_ptr<IStats> &stats_client,
157                          const PixelAtoms::VendorSpeechDspStat &dsp_stats) {
158     // Load values array
159     std::vector<VendorAtomValue> values(4);
160     VendorAtomValue tmp;
161     tmp.set<VendorAtomValue::intValue>(dsp_stats.total_uptime_millis());
162     values[0] = tmp;
163     tmp.set<VendorAtomValue::intValue>(dsp_stats.total_downtime_millis());
164     values[1] = tmp;
165     tmp.set<VendorAtomValue::intValue>(dsp_stats.total_crash_count());
166     values[2] = tmp;
167     tmp.set<VendorAtomValue::intValue>(dsp_stats.total_recover_count());
168     values[3] = tmp;
169 
170     // Send vendor atom to IStats HAL
171     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
172                         .atomId = PixelAtoms::Atom::kVendorSpeechDspStat,
173                         .values = std::move(values)};
174     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
175     if (!ret.isOk())
176         ALOGE("Unable to report VendorSpeechDspStat to Stats service");
177 }
178 
reportPhysicalDropDetected(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorPhysicalDropDetected & dropDetected)179 void reportPhysicalDropDetected(const std::shared_ptr<IStats> &stats_client,
180                                 const PixelAtoms::VendorPhysicalDropDetected &dropDetected) {
181     // Load values array
182     std::vector<VendorAtomValue> values(3);
183     VendorAtomValue tmp;
184     tmp.set<VendorAtomValue::intValue>(dropDetected.confidence_pctg());
185     values[0] = tmp;
186     tmp.set<VendorAtomValue::intValue>(dropDetected.accel_peak_thousandths_g());
187     values[1] = tmp;
188     tmp.set<VendorAtomValue::intValue>(dropDetected.freefall_time_millis());
189     values[2] = tmp;
190 
191     // Send vendor atom to IStats HAL
192     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
193                         .atomId = PixelAtoms::Atom::kVendorPhysicalDropDetected,
194                         .values = std::move(values)};
195     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
196     if (!ret.isOk())
197         ALOGE("Unable to report VendorPhysicalDropDetected to Stats service");
198 }
199 
reportUsbPortOverheat(const std::shared_ptr<IStats> & stats_client,const PixelAtoms::VendorUsbPortOverheat & overheat_info)200 void reportUsbPortOverheat(const std::shared_ptr<IStats> &stats_client,
201                            const PixelAtoms::VendorUsbPortOverheat &overheat_info) {
202     // Load values array
203     std::vector<VendorAtomValue> values(5);
204     VendorAtomValue tmp;
205     tmp.set<VendorAtomValue::intValue>(overheat_info.plug_temperature_deci_c());
206     values[0] = tmp;
207     tmp.set<VendorAtomValue::intValue>(overheat_info.max_temperature_deci_c());
208     values[1] = tmp;
209     tmp.set<VendorAtomValue::intValue>(overheat_info.time_to_overheat_secs());
210     values[2] = tmp;
211     tmp.set<VendorAtomValue::intValue>(overheat_info.time_to_hysteresis_secs());
212     values[3] = tmp;
213     tmp.set<VendorAtomValue::intValue>(overheat_info.time_to_inactive_secs());
214     values[4] = tmp;
215 
216     // Send vendor atom to IStats HAL
217     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
218                         .atomId = PixelAtoms::Atom::kVendorUsbPortOverheat,
219                         .values = std::move(values)};
220     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
221     if (!ret.isOk())
222         ALOGE("Unable to report VendorUsbPortOverheat to Stats service");
223 }
224 
225 }  // namespace pixel
226 }  // namespace google
227 }  // namespace hardware
228 }  // namespace android
229