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-base/logging.h>
18 #include <android/binder_manager.h>
19 #include <pixelhealth/StatsHelper.h>
20 
21 #define LOG_TAG "pixelhealth-vendor"
22 
23 #include <utils/Log.h>
24 
25 namespace hardware {
26 namespace google {
27 namespace pixel {
28 namespace health {
29 
30 using aidl::android::frameworks::stats::VendorAtom;
31 using aidl::android::frameworks::stats::VendorAtomValue;
32 namespace PixelAtoms = android::hardware::google::pixel::PixelAtoms;
33 
getStatsService()34 std::shared_ptr<IStats> getStatsService() {
35     const std::string instance = std::string() + IStats::descriptor + "/default";
36     static bool isStatsDeclared = false;
37     if (!isStatsDeclared) {
38         // It is good to cache the result - it would not be changed
39         isStatsDeclared = AServiceManager_isDeclared(instance.c_str());
40         if (!isStatsDeclared) {
41             LOG(ERROR) << "Stats service is not registered.";
42             return nullptr;
43         }
44     }
45     /* TODO stayfan@: b/187221893 Review implementing separate thread to log atoms
46      * to prevent data loss at device boot stage, while IStats might not be ready
47      */
48     return IStats::fromBinder(ndk::SpAIBinder(AServiceManager_getService(instance.c_str())));
49 }
50 
reportBatteryHealthSnapshot(const std::shared_ptr<IStats> & stats_client,const VendorBatteryHealthSnapshot & batteryHealthSnapshot)51 void reportBatteryHealthSnapshot(const std::shared_ptr<IStats> &stats_client,
52                                  const VendorBatteryHealthSnapshot &batteryHealthSnapshot) {
53     // Load values array
54     std::vector<VendorAtomValue> values(7);
55     VendorAtomValue tmp;
56     tmp.set<VendorAtomValue::intValue>(batteryHealthSnapshot.type());
57     values[0] = tmp;
58     tmp.set<VendorAtomValue::intValue>(batteryHealthSnapshot.temperature_deci_celsius());
59     values[1] = tmp;
60     tmp.set<VendorAtomValue::intValue>(batteryHealthSnapshot.voltage_micro_volt());
61     values[2] = tmp;
62     tmp.set<VendorAtomValue::intValue>(batteryHealthSnapshot.current_micro_amps());
63     values[3] = tmp;
64     tmp.set<VendorAtomValue::intValue>(batteryHealthSnapshot.open_circuit_micro_volt());
65     values[4] = tmp;
66     tmp.set<VendorAtomValue::intValue>(batteryHealthSnapshot.resistance_micro_ohm());
67     values[5] = tmp;
68     tmp.set<VendorAtomValue::intValue>(batteryHealthSnapshot.level_percent());
69     values[6] = tmp;
70 
71     // Send vendor atom to IStats HAL
72     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
73                         .atomId = PixelAtoms::Atom::kVendorBatteryHealthSnapshot,
74                         .values = std::move(values)};
75     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
76     if (!ret.isOk())
77         LOG(ERROR) << "Unable to report VendorBatteryHealthSnapshot to IStats service";
78 }
79 
reportBatteryCausedShutdown(const std::shared_ptr<IStats> & stats_client,const VendorBatteryCausedShutdown & batteryCausedShutdown)80 void reportBatteryCausedShutdown(const std::shared_ptr<IStats> &stats_client,
81                                  const VendorBatteryCausedShutdown &batteryCausedShutdown) {
82     // Load values array
83     std::vector<VendorAtomValue> values(1);
84     VendorAtomValue tmp;
85     tmp.set<VendorAtomValue::intValue>(batteryCausedShutdown.last_recorded_micro_volt());
86     values[0] = tmp;
87 
88     // Send vendor atom to IStats HAL
89     VendorAtom event = {.reverseDomainName = PixelAtoms::ReverseDomainNames().pixel(),
90                         .atomId = PixelAtoms::Atom::kVendorBatteryCausedShutdown,
91                         .values = std::move(values)};
92     const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
93     if (!ret.isOk())
94         LOG(ERROR) << "Unable to report VendorBatteryHealthSnapshot to IStats service";
95 }
96 
97 }  // namespace health
98 }  // namespace pixel
99 }  // namespace google
100 }  // namespace hardware
101