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 #include "pixelatoms_defs.h"
22
23 #define LOG_TAG "pixelhealth-vendor"
24
25 #include <utils/Log.h>
26
27 namespace hardware {
28 namespace google {
29 namespace pixel {
30 namespace health {
31
32 using aidl::android::frameworks::stats::VendorAtom;
33 using aidl::android::frameworks::stats::VendorAtomValue;
34
35 namespace PixelAtoms = hardware::google::pixel::PixelAtoms;
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 LOG(ERROR) << "Stats service is not registered.";
45 return nullptr;
46 }
47 }
48 /* TODO stayfan@: b/187221893 Review implementing separate thread to log atoms
49 * to prevent data loss at device boot stage, while IStats might not be ready
50 */
51 return IStats::fromBinder(ndk::SpAIBinder(AServiceManager_getService(instance.c_str())));
52 }
53
reportBatteryHealthSnapshot(const std::shared_ptr<IStats> & stats_client,int32_t type,int32_t temperature_deci_celsius,int32_t voltage_micro_volt,int32_t current_micro_amps,int32_t open_circuit_micro_volt,int32_t resistance_micro_ohm,int32_t level_percent)54 void reportBatteryHealthSnapshot(const std::shared_ptr<IStats> &stats_client, int32_t type,
55 int32_t temperature_deci_celsius, int32_t voltage_micro_volt,
56 int32_t current_micro_amps, int32_t open_circuit_micro_volt,
57 int32_t resistance_micro_ohm, int32_t level_percent) {
58 // Load values array
59 std::vector<VendorAtomValue> values(7);
60 VendorAtomValue tmp;
61 tmp.set<VendorAtomValue::intValue>(type);
62 values[0] = tmp;
63 tmp.set<VendorAtomValue::intValue>(temperature_deci_celsius);
64 values[1] = tmp;
65 tmp.set<VendorAtomValue::intValue>(voltage_micro_volt);
66 values[2] = tmp;
67 tmp.set<VendorAtomValue::intValue>(current_micro_amps);
68 values[3] = tmp;
69 tmp.set<VendorAtomValue::intValue>(open_circuit_micro_volt);
70 values[4] = tmp;
71 tmp.set<VendorAtomValue::intValue>(resistance_micro_ohm);
72 values[5] = tmp;
73 tmp.set<VendorAtomValue::intValue>(level_percent);
74 values[6] = tmp;
75
76 // Send vendor atom to IStats HAL
77 VendorAtom event = {.atomId = PixelAtoms::VENDOR_BATTERY_HEALTH_SNAPSHOT,
78 .values = std::move(values)};
79 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
80 if (!ret.isOk())
81 LOG(ERROR) << "Unable to report VendorBatteryHealthSnapshot to IStats service";
82 }
83
reportBatteryCausedShutdown(const std::shared_ptr<IStats> & stats_client,int32_t last_recorded_micro_volt)84 void reportBatteryCausedShutdown(const std::shared_ptr<IStats> &stats_client,
85 int32_t last_recorded_micro_volt) {
86 // Load values array
87 std::vector<VendorAtomValue> values(1);
88 VendorAtomValue tmp;
89 tmp.set<VendorAtomValue::intValue>(last_recorded_micro_volt);
90 values[0] = tmp;
91
92 // Send vendor atom to IStats HAL
93 VendorAtom event = {.atomId = PixelAtoms::VENDOR_BATTERY_CAUSED_SHUTDOWN,
94 .values = std::move(values)};
95 const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
96 if (!ret.isOk())
97 LOG(ERROR) << "Unable to report VendorBatteryHealthSnapshot to IStats service";
98 }
99
100 } // namespace health
101 } // namespace pixel
102 } // namespace google
103 } // namespace hardware
104