1 /*
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 
17 #define LOG_TAG "healthd"
18 #include <android-base/logging.h>
19 
20 #include <android/hardware/health/1.0/IHealth.h>
21 #include <android/hardware/health/1.0/types.h>
22 #include <hal_conversion.h>
23 #include <health2/service.h>
24 #include <healthd/healthd.h>
25 #include <hidl/HidlTransportSupport.h>
26 
27 using android::OK;
28 using android::NAME_NOT_FOUND;
29 using android::hardware::health::V1_0::HealthConfig;
30 using android::hardware::health::V1_0::HealthInfo;
31 using android::hardware::health::V1_0::Result;
32 using android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
33 using android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
34 using android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
35 using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
36 
37 using IHealthLegacy = android::hardware::health::V1_0::IHealth;
38 
39 static android::sp<IHealthLegacy> gHealth_1_0;
40 
healthd_board_get_energy_counter(int64_t * energy)41 static 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;
53 }
54 
healthd_board_init(struct healthd_config * config)55 void 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.
healthd_board_battery_update(struct android::BatteryProperties * props)74 int 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;
89 }
90 
main()91 int main() {
92     return health_service_main("backup");
93 }
94