Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame^] | 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 | // 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 | |
| 29 | using android::hardware::IPCThreadState; |
| 30 | using android::hardware::configureRpcThreadpool; |
| 31 | using android::hardware::health::V1_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 | // see healthd_common.cpp |
| 37 | android::sp<IHealth> gHealth; |
| 38 | |
| 39 | static int gBinderFd; |
| 40 | |
| 41 | extern int healthd_main(void); |
| 42 | |
| 43 | static 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 |
| 48 | int 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 | |
| 54 | void 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 | |
| 78 | int healthd_mode_service_2_0_preparetowait(void) { |
| 79 | IPCThreadState::self()->flushCommands(); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | void healthd_mode_service_2_0_heartbeat(void) { |
| 84 | // noop |
| 85 | } |
| 86 | |
| 87 | void 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 | |
| 92 | HealthInfo info; |
| 93 | convertToHealthInfo(prop, info); |
| 94 | static_cast<Health*>(gHealth.get())->notifyListeners(info); |
| 95 | } |
| 96 | |
| 97 | static 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 | |
| 104 | int 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 | } |