blob: 039570a7c077dc25762b06d9c0c543d043df13a5 [file] [log] [blame]
Yifan Hong4ad11a92018-01-22 11:26:01 -08001/*
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#define LOG_TAG "health@2.0/"
18#include <android-base/logging.h>
19
20#include <android/hardware/health/1.0/types.h>
21#include <hal_conversion.h>
22#include <health2/Health.h>
23#include <health2/service.h>
24#include <healthd/healthd.h>
25#include <hidl/HidlTransportSupport.h>
Steven Moreland2ea3fa52018-07-31 18:16:41 -070026#include <hwbinder/IPCThreadState.h>
Yifan Hong4ad11a92018-01-22 11:26:01 -080027
28using android::hardware::IPCThreadState;
29using android::hardware::configureRpcThreadpool;
30using android::hardware::handleTransportPoll;
31using android::hardware::setupTransportPolling;
32using android::hardware::health::V2_0::HealthInfo;
33using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
34using android::hardware::health::V2_0::IHealth;
35using android::hardware::health::V2_0::implementation::Health;
36
37extern int healthd_main(void);
38
39static int gBinderFd = -1;
40static std::string gInstanceName;
41
42static void binder_event(uint32_t /*epevents*/) {
43 if (gBinderFd >= 0) handleTransportPoll(gBinderFd);
44}
45
46void healthd_mode_service_2_0_init(struct healthd_config* config) {
47 LOG(INFO) << LOG_TAG << gInstanceName << " Hal is starting up...";
48
49 gBinderFd = setupTransportPolling();
50
51 if (gBinderFd >= 0) {
52 if (healthd_register_event(gBinderFd, binder_event))
53 LOG(ERROR) << LOG_TAG << gInstanceName << ": Register for binder events failed";
54 }
55
56 android::sp<IHealth> service = Health::initInstance(config);
57 CHECK_EQ(service->registerAsService(gInstanceName), android::OK)
58 << LOG_TAG << gInstanceName << ": Failed to register HAL";
59
60 LOG(INFO) << LOG_TAG << gInstanceName << ": 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) {
73 HealthInfo info;
74 convertToHealthInfo(prop, info.legacy);
75 Health::getImplementation()->notifyListeners(&info);
76}
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 health_service_main(const char* instance) {
86 gInstanceName = instance;
87 if (gInstanceName.empty()) {
88 gInstanceName = "default";
89 }
90 healthd_mode_ops = &healthd_mode_service_2_0_ops;
91 LOG(INFO) << LOG_TAG << gInstanceName << ": Hal starting main loop...";
92 return healthd_main();
93}