blob: 72a446ad46d0978c58b317d56cc23e8955663b8d [file] [log] [blame]
Yifan Hongcded9002017-11-06 16:47:54 -08001/*
2 * Copyright (C) 2017 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
Yifan Hong31cc64a2017-11-06 16:48:36 -080017#define LOG_TAG "healthd"
18#include <android-base/logging.h>
Yifan Hongcded9002017-11-06 16:47:54 -080019
Yifan Hong31cc64a2017-11-06 16:48:36 -080020#include <android/hardware/health/1.0/IHealth.h>
21#include <android/hardware/health/1.0/types.h>
22#include <hal_conversion.h>
23#include <healthd/healthd.h>
24#include <hidl/HidlTransportSupport.h>
25
26using android::OK;
27using android::NAME_NOT_FOUND;
28using android::hardware::health::V1_0::HealthConfig;
29using android::hardware::health::V1_0::HealthInfo;
30using android::hardware::health::V1_0::Result;
31using android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
32using android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
33using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
34using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
35
36using IHealthLegacy = android::hardware::health::V1_0::IHealth;
37
38static android::sp<IHealthLegacy> gHealth_1_0;
39
40static int healthd_board_get_energy_counter(int64_t* energy) {
41 if (gHealth_1_0 == nullptr) {
42 return NAME_NOT_FOUND;
43 }
44
45 Result result = Result::NOT_SUPPORTED;
46 gHealth_1_0->energyCounter([energy, &result](Result ret, int64_t energyOut) {
47 result = ret;
48 *energy = energyOut;
49 });
50
51 return result == Result::SUCCESS ? OK : NAME_NOT_FOUND;
Yifan Hongcded9002017-11-06 16:47:54 -080052}
53
Yifan Hong31cc64a2017-11-06 16:48:36 -080054void healthd_board_init(struct healthd_config* config) {
55 gHealth_1_0 = IHealthLegacy::getService();
56
57 if (gHealth_1_0 == nullptr) {
58 return;
59 }
60
61 HealthConfig halConfig{};
62 convertToHealthConfig(config, halConfig);
63 gHealth_1_0->init(halConfig, [config](const auto& halConfigOut) {
64 convertFromHealthConfig(halConfigOut, config);
65 // always redirect energy counter queries
66 config->energyCounter = healthd_board_get_energy_counter;
67 });
68 LOG(INFO) << LOG_TAG << ": redirecting calls to 1.0 health HAL";
69}
70
71// TODO(b/68724651): Move this function into healthd_mode_service_2_0_battery_update
72// with logthis returned.
73int healthd_board_battery_update(struct android::BatteryProperties* props) {
74 int logthis = 0;
75
76 if (gHealth_1_0 == nullptr) {
77 return logthis;
78 }
79
80 HealthInfo info;
81 convertToHealthInfo(props, info);
82 gHealth_1_0->update(info, [props, &logthis](int32_t ret, const auto& infoOut) {
83 logthis = ret;
84 convertFromHealthInfo(infoOut, props);
85 });
86
87 return logthis;
Yifan Hongcded9002017-11-06 16:47:54 -080088}