1 /*
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>
26
27 using android::hardware::IPCThreadState;
28 using android::hardware::configureRpcThreadpool;
29 using android::hardware::handleTransportPoll;
30 using android::hardware::setupTransportPolling;
31 using android::hardware::health::V2_0::HealthInfo;
32 using android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
33 using android::hardware::health::V2_0::IHealth;
34 using android::hardware::health::V2_0::implementation::Health;
35
36 extern int healthd_main(void);
37
38 static int gBinderFd = -1;
39 static std::string gInstanceName;
40
binder_event(uint32_t)41 static void binder_event(uint32_t /*epevents*/) {
42 if (gBinderFd >= 0) handleTransportPoll(gBinderFd);
43 }
44
healthd_mode_service_2_0_init(struct healthd_config * config)45 void healthd_mode_service_2_0_init(struct healthd_config* config) {
46 LOG(INFO) << LOG_TAG << gInstanceName << " Hal is starting up...";
47
48 gBinderFd = setupTransportPolling();
49
50 if (gBinderFd >= 0) {
51 if (healthd_register_event(gBinderFd, binder_event))
52 LOG(ERROR) << LOG_TAG << gInstanceName << ": Register for binder events failed";
53 }
54
55 android::sp<IHealth> service = Health::initInstance(config);
56 CHECK_EQ(service->registerAsService(gInstanceName), android::OK)
57 << LOG_TAG << gInstanceName << ": Failed to register HAL";
58
59 LOG(INFO) << LOG_TAG << gInstanceName << ": Hal init done";
60 }
61
healthd_mode_service_2_0_preparetowait(void)62 int healthd_mode_service_2_0_preparetowait(void) {
63 IPCThreadState::self()->flushCommands();
64 return -1;
65 }
66
healthd_mode_service_2_0_heartbeat(void)67 void healthd_mode_service_2_0_heartbeat(void) {
68 // noop
69 }
70
healthd_mode_service_2_0_battery_update(struct android::BatteryProperties * prop)71 void healthd_mode_service_2_0_battery_update(struct android::BatteryProperties* prop) {
72 HealthInfo info;
73 convertToHealthInfo(prop, info.legacy);
74 Health::getImplementation()->notifyListeners(&info);
75 }
76
77 static struct healthd_mode_ops healthd_mode_service_2_0_ops = {
78 .init = healthd_mode_service_2_0_init,
79 .preparetowait = healthd_mode_service_2_0_preparetowait,
80 .heartbeat = healthd_mode_service_2_0_heartbeat,
81 .battery_update = healthd_mode_service_2_0_battery_update,
82 };
83
health_service_main(const char * instance)84 int health_service_main(const char* instance) {
85 gInstanceName = instance;
86 if (gInstanceName.empty()) {
87 gInstanceName = "default";
88 }
89 healthd_mode_ops = &healthd_mode_service_2_0_ops;
90 LOG(INFO) << LOG_TAG << gInstanceName << ": Hal starting main loop...";
91 return healthd_main();
92 }
93