blob: 160db77a783bd0f71a1ef542e198a1cd4ff7a58f [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;
Yifan Hong1106e832017-11-06 20:50:45 +000031using android::hardware::health::V1_0::HealthInfo;
Yifan Hong2763df82017-09-19 17:57:50 -070032using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
33using android::hardware::health::V2_0::IHealth;
34using android::hardware::health::V2_0::implementation::Health;
35
36// see healthd_common.cpp
37android::sp<IHealth> gHealth;
38
39static int gBinderFd;
40
41extern int healthd_main(void);
42
43static void binder_event(uint32_t /*epevents*/) {
44 IPCThreadState::self()->handlePolledCommands();
45}
46
Yifan Hong2763df82017-09-19 17:57:50 -070047void healthd_mode_service_2_0_init(struct healthd_config* config) {
48 LOG(INFO) << LOG_TAG << " Hal is starting up...";
49
Yifan Hong2763df82017-09-19 17:57:50 -070050 configureRpcThreadpool(1, false /* callerWillJoin */);
51 IPCThreadState::self()->disableBackgroundScheduling(true);
52 IPCThreadState::self()->setupPolling(&gBinderFd);
53
54 if (gBinderFd >= 0) {
55 if (healthd_register_event(gBinderFd, binder_event))
56 LOG(ERROR) << LOG_TAG << ": Register for binder events failed";
57 }
58
Yifan Hongcded9002017-11-06 16:47:54 -080059 // Implementation-defined init logic goes here.
60 // 1. config->periodic_chores_interval_* variables
61 // 2. config->battery*Path variables
62 // 3. config->energyCounter. In this implementation, energyCounter is not defined.
63 // TODO(b/68724651): healthd_board_* functions should be removed in health@2.0
64 healthd_board_init(config);
65
Yifan Hong2763df82017-09-19 17:57:50 -070066 gHealth = new ::android::hardware::health::V2_0::implementation::Health(config);
67 CHECK_EQ(gHealth->registerAsService(HEALTH_INSTANCE_NAME), android::OK)
68 << LOG_TAG << ": Failed to register HAL";
69
70 LOG(INFO) << LOG_TAG << ": Hal init done";
71}
72
73int healthd_mode_service_2_0_preparetowait(void) {
74 IPCThreadState::self()->flushCommands();
75 return -1;
76}
77
78void healthd_mode_service_2_0_heartbeat(void) {
79 // noop
80}
81
82void healthd_mode_service_2_0_battery_update(struct android::BatteryProperties* prop) {
Yifan Hong2763df82017-09-19 17:57:50 -070083 // Implementation-defined update logic goes here. An implementation
84 // can make modifications to prop before broadcasting it to all callbacks.
85
Yifan Hong1106e832017-11-06 20:50:45 +000086 HealthInfo info;
87 convertToHealthInfo(prop, info);
88 static_cast<Health*>(gHealth.get())->notifyListeners(info);
Yifan Hong2763df82017-09-19 17:57:50 -070089}
90
91static struct healthd_mode_ops healthd_mode_service_2_0_ops = {
92 .init = healthd_mode_service_2_0_init,
93 .preparetowait = healthd_mode_service_2_0_preparetowait,
94 .heartbeat = healthd_mode_service_2_0_heartbeat,
95 .battery_update = healthd_mode_service_2_0_battery_update,
96};
97
98int main() {
99 healthd_mode_ops = &healthd_mode_service_2_0_ops;
100 LOG(INFO) << LOG_TAG << ": Hal starting main loop...";
101 return healthd_main();
102}