| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 1 | /* | 
| Sandeep Patil | 526f8cf | 2016-11-01 16:41:56 -0700 | [diff] [blame] | 2 |  * Copyright (C) 2016 The Android Open Source Project | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 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 "healthd" | 
 | 18 | #define KLOG_LEVEL 6 | 
 | 19 |  | 
| Yabin Cui | e98e177 | 2016-02-17 12:21:34 -0800 | [diff] [blame] | 20 | #include <healthd/healthd.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 21 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 22 | #include <stdio.h> | 
 | 23 | #include <stdlib.h> | 
 | 24 | #include <unistd.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 25 | #include <cutils/klog.h> | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 26 |  | 
| Sandeep Patil | a368195 | 2016-11-08 16:27:54 -0800 | [diff] [blame] | 27 | #include <android/hardware/health/1.0/IHealth.h> | 
 | 28 | #include <android/hardware/health/1.0/types.h> | 
 | 29 | #include <hal_conversion.h> | 
 | 30 |  | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 31 | using namespace android; | 
 | 32 |  | 
| Sandeep Patil | a368195 | 2016-11-08 16:27:54 -0800 | [diff] [blame] | 33 | using IHealth = ::android::hardware::health::V1_0::IHealth; | 
 | 34 | using Result = ::android::hardware::health::V1_0::Result; | 
 | 35 | using HealthConfig = ::android::hardware::health::V1_0::HealthConfig; | 
 | 36 | using HealthInfo = ::android::hardware::health::V1_0::HealthInfo; | 
 | 37 |  | 
 | 38 | using ::android::hardware::health::V1_0::hal_conversion::convertToHealthConfig; | 
 | 39 | using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig; | 
 | 40 | using ::android::hardware::health::V1_0::hal_conversion::convertToHealthInfo; | 
 | 41 | using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo; | 
 | 42 |  | 
 | 43 | // device specific hal interface; | 
 | 44 | static sp<IHealth> gHealth; | 
 | 45 |  | 
| Sandeep Patil | 526f8cf | 2016-11-01 16:41:56 -0700 | [diff] [blame] | 46 | // main healthd loop | 
 | 47 | extern int healthd_main(void); | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 48 |  | 
 | 49 | // Android mode | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 50 | extern void healthd_mode_android_init(struct healthd_config *config); | 
 | 51 | extern int healthd_mode_android_preparetowait(void); | 
| Sandeep Patil | 526f8cf | 2016-11-01 16:41:56 -0700 | [diff] [blame] | 52 | extern void healthd_mode_android_heartbeat(void); | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 53 | extern void healthd_mode_android_battery_update( | 
 | 54 |     struct android::BatteryProperties *props); | 
 | 55 |  | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 56 | static struct healthd_mode_ops android_ops = { | 
 | 57 |     .init = healthd_mode_android_init, | 
 | 58 |     .preparetowait = healthd_mode_android_preparetowait, | 
| Sandeep Patil | 526f8cf | 2016-11-01 16:41:56 -0700 | [diff] [blame] | 59 |     .heartbeat = healthd_mode_android_heartbeat, | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 60 |     .battery_update = healthd_mode_android_battery_update, | 
 | 61 | }; | 
 | 62 |  | 
| Sandeep Patil | a368195 | 2016-11-08 16:27:54 -0800 | [diff] [blame] | 63 | // default energy counter property redirect to talk to device | 
 | 64 | // HAL | 
 | 65 | static int healthd_board_get_energy_counter(int64_t *energy) { | 
 | 66 |  | 
 | 67 |     if (gHealth == nullptr) { | 
 | 68 |         return NAME_NOT_FOUND; | 
 | 69 |     } | 
 | 70 |  | 
 | 71 |     Result result = Result::NOT_SUPPORTED; | 
 | 72 |     gHealth->energyCounter([=, &result] (Result ret, int64_t energyOut) { | 
 | 73 |                 result = ret; | 
 | 74 |                 *energy = energyOut; | 
 | 75 |             }); | 
 | 76 |  | 
 | 77 |     return result == Result::SUCCESS ? OK : NAME_NOT_FOUND; | 
 | 78 | } | 
 | 79 |  | 
 | 80 | void healthd_board_init(struct healthd_config *config) { | 
 | 81 |  | 
 | 82 |     // Initialize the board HAL - Equivalent of healthd_board_init(config) | 
 | 83 |     // in charger/recovery mode. | 
 | 84 |  | 
| Chris Phoenix | 68f4085 | 2017-01-23 14:49:09 -0800 | [diff] [blame] | 85 |     gHealth = IHealth::getService(); | 
| Sandeep Patil | a368195 | 2016-11-08 16:27:54 -0800 | [diff] [blame] | 86 |     if (gHealth == nullptr) { | 
 | 87 |         KLOG_WARNING(LOG_TAG, "unable to get HAL interface, using defaults\n"); | 
 | 88 |         return; | 
 | 89 |     } | 
 | 90 |  | 
 | 91 |     HealthConfig halConfig; | 
 | 92 |     convertToHealthConfig(config, halConfig); | 
 | 93 |     gHealth->init(halConfig, [=] (const auto &halConfigOut) { | 
 | 94 |             convertFromHealthConfig(halConfigOut, config); | 
 | 95 |             // always redirect energy counter queries | 
 | 96 |             config->energyCounter = healthd_board_get_energy_counter; | 
 | 97 |             }); | 
 | 98 | } | 
 | 99 |  | 
 | 100 | int healthd_board_battery_update(struct android::BatteryProperties *props) { | 
 | 101 |     int logthis = 0; | 
 | 102 |  | 
 | 103 |     if (gHealth == nullptr) { | 
 | 104 |         return logthis; | 
 | 105 |     } | 
 | 106 |  | 
 | 107 |     HealthInfo info; | 
 | 108 |     convertToHealthInfo(props, info); | 
 | 109 |     gHealth->update(info, | 
 | 110 |             [=, &logthis] (int32_t ret, const auto &infoOut) { | 
 | 111 |                 logthis = ret; | 
 | 112 |                 convertFromHealthInfo(infoOut, props); | 
 | 113 |             }); | 
 | 114 |  | 
 | 115 |     return logthis; | 
 | 116 | } | 
 | 117 |  | 
| Sandeep Patil | 526f8cf | 2016-11-01 16:41:56 -0700 | [diff] [blame] | 118 | int main(int /*argc*/, char ** /*argv*/) { | 
| Todd Poynor | fea5b4d | 2013-09-09 12:09:08 -0700 | [diff] [blame] | 119 |  | 
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 120 |     healthd_mode_ops = &android_ops; | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 121 |  | 
| Sandeep Patil | 526f8cf | 2016-11-01 16:41:56 -0700 | [diff] [blame] | 122 |     return healthd_main(); | 
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 123 | } |