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; |
Yifan Hong | 1106e83 | 2017-11-06 20:50:45 +0000 | [diff] [blame] | 31 | using android::hardware::health::V1_0::HealthInfo; |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 36 | extern int healthd_main(void); |
| 37 | |
| 38 | static void binder_event(uint32_t /*epevents*/) { |
| 39 | IPCThreadState::self()->handlePolledCommands(); |
| 40 | } |
| 41 | |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 42 | void healthd_mode_service_2_0_init(struct healthd_config* config) { |
Yifan Hong | 5382983 | 2017-11-07 15:02:10 -0800 | [diff] [blame] | 43 | int binderFd; |
| 44 | |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 45 | LOG(INFO) << LOG_TAG << " Hal is starting up..."; |
| 46 | |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 47 | configureRpcThreadpool(1, false /* callerWillJoin */); |
| 48 | IPCThreadState::self()->disableBackgroundScheduling(true); |
Yifan Hong | 5382983 | 2017-11-07 15:02:10 -0800 | [diff] [blame] | 49 | IPCThreadState::self()->setupPolling(&binderFd); |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 50 | |
Yifan Hong | 5382983 | 2017-11-07 15:02:10 -0800 | [diff] [blame] | 51 | if (binderFd >= 0) { |
| 52 | if (healthd_register_event(binderFd, binder_event)) |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 53 | LOG(ERROR) << LOG_TAG << ": Register for binder events failed"; |
| 54 | } |
| 55 | |
Yifan Hong | cded900 | 2017-11-06 16:47:54 -0800 | [diff] [blame] | 56 | // Implementation-defined init logic goes here. |
| 57 | // 1. config->periodic_chores_interval_* variables |
| 58 | // 2. config->battery*Path variables |
| 59 | // 3. config->energyCounter. In this implementation, energyCounter is not defined. |
| 60 | // TODO(b/68724651): healthd_board_* functions should be removed in health@2.0 |
| 61 | healthd_board_init(config); |
| 62 | |
Yifan Hong | 9cffa8a | 2017-11-07 14:59:14 -0800 | [diff] [blame^] | 63 | android::sp<IHealth> service = Health::initInstance(config); |
| 64 | CHECK_EQ(service->registerAsService(HEALTH_INSTANCE_NAME), android::OK) |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 65 | << LOG_TAG << ": Failed to register HAL"; |
| 66 | |
| 67 | LOG(INFO) << LOG_TAG << ": Hal init done"; |
| 68 | } |
| 69 | |
| 70 | int healthd_mode_service_2_0_preparetowait(void) { |
| 71 | IPCThreadState::self()->flushCommands(); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | void healthd_mode_service_2_0_heartbeat(void) { |
| 76 | // noop |
| 77 | } |
| 78 | |
| 79 | void healthd_mode_service_2_0_battery_update(struct android::BatteryProperties* prop) { |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 80 | // Implementation-defined update logic goes here. An implementation |
| 81 | // can make modifications to prop before broadcasting it to all callbacks. |
| 82 | |
Yifan Hong | 1106e83 | 2017-11-06 20:50:45 +0000 | [diff] [blame] | 83 | HealthInfo info; |
| 84 | convertToHealthInfo(prop, info); |
Yifan Hong | 9cffa8a | 2017-11-07 14:59:14 -0800 | [diff] [blame^] | 85 | Health::getImplementation()->notifyListeners(info); |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static struct healthd_mode_ops healthd_mode_service_2_0_ops = { |
| 89 | .init = healthd_mode_service_2_0_init, |
| 90 | .preparetowait = healthd_mode_service_2_0_preparetowait, |
| 91 | .heartbeat = healthd_mode_service_2_0_heartbeat, |
| 92 | .battery_update = healthd_mode_service_2_0_battery_update, |
| 93 | }; |
| 94 | |
| 95 | int main() { |
| 96 | healthd_mode_ops = &healthd_mode_service_2_0_ops; |
| 97 | LOG(INFO) << LOG_TAG << ": Hal starting main loop..."; |
| 98 | return healthd_main(); |
| 99 | } |