blob: 5fd2597d68d9b1ab3e856c8d287a39588343ba17 [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>
Yifan Hongaffa24f2018-01-19 15:53:25 -080023#include <health2/service.h>
Yifan Hong31cc64a2017-11-06 16:48:36 -080024#include <healthd/healthd.h>
25#include <hidl/HidlTransportSupport.h>
26
27using android::OK;
28using android::NAME_NOT_FOUND;
29using android::hardware::health::V1_0::HealthConfig;
30using android::hardware::health::V1_0::HealthInfo;
31using android::hardware::health::V1_0::Result;
32using android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
33using android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
34using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
35using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
36
37using IHealthLegacy = android::hardware::health::V1_0::IHealth;
38
39static android::sp<IHealthLegacy> gHealth_1_0;
40
41static int healthd_board_get_energy_counter(int64_t* energy) {
42 if (gHealth_1_0 == nullptr) {
43 return NAME_NOT_FOUND;
44 }
45
46 Result result = Result::NOT_SUPPORTED;
47 gHealth_1_0->energyCounter([energy, &result](Result ret, int64_t energyOut) {
48 result = ret;
49 *energy = energyOut;
50 });
51
52 return result == Result::SUCCESS ? OK : NAME_NOT_FOUND;
Yifan Hongcded9002017-11-06 16:47:54 -080053}
54
Yifan Hong31cc64a2017-11-06 16:48:36 -080055void healthd_board_init(struct healthd_config* config) {
56 gHealth_1_0 = IHealthLegacy::getService();
57
58 if (gHealth_1_0 == nullptr) {
59 return;
60 }
61
62 HealthConfig halConfig{};
63 convertToHealthConfig(config, halConfig);
64 gHealth_1_0->init(halConfig, [config](const auto& halConfigOut) {
65 convertFromHealthConfig(halConfigOut, config);
66 // always redirect energy counter queries
67 config->energyCounter = healthd_board_get_energy_counter;
68 });
69 LOG(INFO) << LOG_TAG << ": redirecting calls to 1.0 health HAL";
70}
71
72// TODO(b/68724651): Move this function into healthd_mode_service_2_0_battery_update
73// with logthis returned.
74int healthd_board_battery_update(struct android::BatteryProperties* props) {
75 int logthis = 0;
76
77 if (gHealth_1_0 == nullptr) {
78 return logthis;
79 }
80
81 HealthInfo info;
82 convertToHealthInfo(props, info);
83 gHealth_1_0->update(info, [props, &logthis](int32_t ret, const auto& infoOut) {
84 logthis = ret;
85 convertFromHealthInfo(infoOut, props);
86 });
87
88 return logthis;
Yifan Hongcded9002017-11-06 16:47:54 -080089}
Yifan Hongaffa24f2018-01-19 15:53:25 -080090
91int main() {
92 return health_service_main("backup");
93}