Home | History | Annotate | Download | only in healthd
      1 /*
      2  * Copyright (C) 2016 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 "healthd"
     18 #define KLOG_LEVEL 6
     19 
     20 #include <healthd/healthd.h>
     21 
     22 #include <stdio.h>
     23 #include <stdlib.h>
     24 #include <unistd.h>
     25 #include <cutils/klog.h>
     26 
     27 #include <android/hardware/health/1.0/IHealth.h>
     28 #include <android/hardware/health/1.0/types.h>
     29 #include <hal_conversion.h>
     30 
     31 using namespace android;
     32 
     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 
     46 // main healthd loop
     47 extern int healthd_main(void);
     48 
     49 // Android mode
     50 extern void healthd_mode_android_init(struct healthd_config *config);
     51 extern int healthd_mode_android_preparetowait(void);
     52 extern void healthd_mode_android_heartbeat(void);
     53 extern void healthd_mode_android_battery_update(
     54     struct android::BatteryProperties *props);
     55 
     56 static struct healthd_mode_ops android_ops = {
     57     .init = healthd_mode_android_init,
     58     .preparetowait = healthd_mode_android_preparetowait,
     59     .heartbeat = healthd_mode_android_heartbeat,
     60     .battery_update = healthd_mode_android_battery_update,
     61 };
     62 
     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 
     85     gHealth = IHealth::getService();
     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 
    118 int main(int /*argc*/, char ** /*argv*/) {
    119 
    120     healthd_mode_ops = &android_ops;
    121 
    122     return healthd_main();
    123 }
    124