blob: a2ca44b05860eb35bab184f514e65644ed657067 [file] [log] [blame]
Yifan Hong2763df82017-09-19 17:57:50 -07001/*
2 * Copyright 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// This is a reference implementation for health@2.0 HAL. A vendor
18// can write its own HealthService.cpp with customized init and update functions.
19
20#define LOG_TAG "health@2.0/" HEALTH_INSTANCE_NAME
21#include <android-base/logging.h>
22
23#include <android/hardware/health/1.0/types.h>
24#include <hal_conversion.h>
25#include <health2/Health.h>
26#include <healthd/healthd.h>
27#include <hidl/HidlTransportSupport.h>
28
29using android::hardware::IPCThreadState;
30using android::hardware::configureRpcThreadpool;
Martijn Coenena5c1eec2017-11-27 15:54:01 -080031using android::hardware::handleTransportPoll;
32using android::hardware::setupTransportPolling;
Hridya Valsaraju79d38eb2018-01-17 23:02:56 -080033using android::hardware::health::V2_0::HealthInfo;
Yifan Hong2763df82017-09-19 17:57:50 -070034using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
35using android::hardware::health::V2_0::IHealth;
36using android::hardware::health::V2_0::implementation::Health;
37
Yifan Hong2763df82017-09-19 17:57:50 -070038extern int healthd_main(void);
39
Martijn Coenena5c1eec2017-11-27 15:54:01 -080040static int gBinderFd = -1;
41
Yifan Hong2763df82017-09-19 17:57:50 -070042static void binder_event(uint32_t /*epevents*/) {
Martijn Coenena5c1eec2017-11-27 15:54:01 -080043 if (gBinderFd >= 0) handleTransportPoll(gBinderFd);
Yifan Hong2763df82017-09-19 17:57:50 -070044}
45
Yifan Hong2763df82017-09-19 17:57:50 -070046void healthd_mode_service_2_0_init(struct healthd_config* config) {
47 LOG(INFO) << LOG_TAG << " Hal is starting up...";
48
Martijn Coenena5c1eec2017-11-27 15:54:01 -080049 gBinderFd = setupTransportPolling();
Yifan Hong2763df82017-09-19 17:57:50 -070050
Martijn Coenena5c1eec2017-11-27 15:54:01 -080051 if (gBinderFd >= 0) {
52 if (healthd_register_event(gBinderFd, binder_event))
Yifan Hong2763df82017-09-19 17:57:50 -070053 LOG(ERROR) << LOG_TAG << ": Register for binder events failed";
54 }
55
Yifan Hong9cffa8a2017-11-07 14:59:14 -080056 android::sp<IHealth> service = Health::initInstance(config);
57 CHECK_EQ(service->registerAsService(HEALTH_INSTANCE_NAME), android::OK)
Yifan Hong2763df82017-09-19 17:57:50 -070058 << LOG_TAG << ": Failed to register HAL";
59
60 LOG(INFO) << LOG_TAG << ": Hal init done";
61}
62
63int healthd_mode_service_2_0_preparetowait(void) {
64 IPCThreadState::self()->flushCommands();
65 return -1;
66}
67
68void healthd_mode_service_2_0_heartbeat(void) {
69 // noop
70}
71
72void healthd_mode_service_2_0_battery_update(struct android::BatteryProperties* prop) {
Yifan Hong1106e832017-11-06 20:50:45 +000073 HealthInfo info;
Hridya Valsaraju79d38eb2018-01-17 23:02:56 -080074 convertToHealthInfo(prop, info.legacy);
75 Health::getImplementation()->notifyListeners(&info);
Yifan Hong2763df82017-09-19 17:57:50 -070076}
77
78static struct healthd_mode_ops healthd_mode_service_2_0_ops = {
79 .init = healthd_mode_service_2_0_init,
80 .preparetowait = healthd_mode_service_2_0_preparetowait,
81 .heartbeat = healthd_mode_service_2_0_heartbeat,
82 .battery_update = healthd_mode_service_2_0_battery_update,
83};
84
85int main() {
86 healthd_mode_ops = &healthd_mode_service_2_0_ops;
87 LOG(INFO) << LOG_TAG << ": Hal starting main loop...";
88 return healthd_main();
89}