blob: 29a29ed4cc4407f4325a3ac8b2afeb1cba824738 [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 Hong2763df82017-09-19 17:57:50 -070031using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
Yifan Hongbe17a4f2017-10-11 10:40:56 -070032using android::hardware::health::V2_0::HealthInfo;
Yifan Hong2763df82017-09-19 17:57:50 -070033using 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
47// TODO(b/67463967): healthd_board_* functions should be removed in health@2.0
48int healthd_board_battery_update(struct android::BatteryProperties*)
49{
50 // return 0 to log periodic polled battery status to kernel log
51 return 0;
52}
53
54void healthd_mode_service_2_0_init(struct healthd_config* config) {
55 LOG(INFO) << LOG_TAG << " Hal is starting up...";
56
57 // Implementation-defined init logic goes here.
58 // 1. config->periodic_chores_interval_* variables
59 // 2. config->battery*Path variables
60 // 3. config->energyCounter. In this implementation, energyCounter is not defined.
61
62 configureRpcThreadpool(1, false /* callerWillJoin */);
63 IPCThreadState::self()->disableBackgroundScheduling(true);
64 IPCThreadState::self()->setupPolling(&gBinderFd);
65
66 if (gBinderFd >= 0) {
67 if (healthd_register_event(gBinderFd, binder_event))
68 LOG(ERROR) << LOG_TAG << ": Register for binder events failed";
69 }
70
71 gHealth = new ::android::hardware::health::V2_0::implementation::Health(config);
72 CHECK_EQ(gHealth->registerAsService(HEALTH_INSTANCE_NAME), android::OK)
73 << LOG_TAG << ": Failed to register HAL";
74
75 LOG(INFO) << LOG_TAG << ": Hal init done";
76}
77
78int healthd_mode_service_2_0_preparetowait(void) {
79 IPCThreadState::self()->flushCommands();
80 return -1;
81}
82
83void healthd_mode_service_2_0_heartbeat(void) {
84 // noop
85}
86
87void healthd_mode_service_2_0_battery_update(struct android::BatteryProperties* prop) {
88
89 // Implementation-defined update logic goes here. An implementation
90 // can make modifications to prop before broadcasting it to all callbacks.
91
Yifan Hongbe17a4f2017-10-11 10:40:56 -070092 HealthInfo info{};
93 convertToHealthInfo(prop, info.legacy);
94 static_cast<Health*>(gHealth.get())->updateAndNotify(&info);
Yifan Hong2763df82017-09-19 17:57:50 -070095}
96
97static struct healthd_mode_ops healthd_mode_service_2_0_ops = {
98 .init = healthd_mode_service_2_0_init,
99 .preparetowait = healthd_mode_service_2_0_preparetowait,
100 .heartbeat = healthd_mode_service_2_0_heartbeat,
101 .battery_update = healthd_mode_service_2_0_battery_update,
102};
103
104int main() {
105 healthd_mode_ops = &healthd_mode_service_2_0_ops;
106 LOG(INFO) << LOG_TAG << ": Hal starting main loop...";
107 return healthd_main();
108}