blob: ed1971a9771f609fb74c591aa35c165a0436cf1a [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
Sandeep Patil526f8cf2016-11-01 16:41:56 -07002 * Copyright (C) 2016 The Android Open Source Project
Todd Poynor752faf22013-06-12 13:25:59 -07003 *
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 Cuie98e1772016-02-17 12:21:34 -080020#include <healthd/healthd.h>
Todd Poynor752faf22013-06-12 13:25:59 -070021
Todd Poynor752faf22013-06-12 13:25:59 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
Todd Poynor752faf22013-06-12 13:25:59 -070025#include <cutils/klog.h>
Todd Poynor752faf22013-06-12 13:25:59 -070026
Sandeep Patila3681952016-11-08 16:27:54 -080027#include <android/hardware/health/1.0/IHealth.h>
28#include <android/hardware/health/1.0/types.h>
29#include <hal_conversion.h>
30
Todd Poynor752faf22013-06-12 13:25:59 -070031using namespace android;
32
Sandeep Patila3681952016-11-08 16:27:54 -080033using IHealth = ::android::hardware::health::V1_0::IHealth;
34using Result = ::android::hardware::health::V1_0::Result;
35using HealthConfig = ::android::hardware::health::V1_0::HealthConfig;
36using HealthInfo = ::android::hardware::health::V1_0::HealthInfo;
37
38using ::android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
39using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
40using ::android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
41using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
42
43// device specific hal interface;
44static sp<IHealth> gHealth;
45
Sandeep Patil526f8cf2016-11-01 16:41:56 -070046// main healthd loop
47extern int healthd_main(void);
Todd Poynorc7464c92013-09-10 12:40:00 -070048
49// Android mode
Todd Poynorc7464c92013-09-10 12:40:00 -070050extern void healthd_mode_android_init(struct healthd_config *config);
51extern int healthd_mode_android_preparetowait(void);
Sandeep Patil526f8cf2016-11-01 16:41:56 -070052extern void healthd_mode_android_heartbeat(void);
Todd Poynorc7464c92013-09-10 12:40:00 -070053extern void healthd_mode_android_battery_update(
54 struct android::BatteryProperties *props);
55
Todd Poynorc7464c92013-09-10 12:40:00 -070056static struct healthd_mode_ops android_ops = {
57 .init = healthd_mode_android_init,
58 .preparetowait = healthd_mode_android_preparetowait,
Sandeep Patil526f8cf2016-11-01 16:41:56 -070059 .heartbeat = healthd_mode_android_heartbeat,
Todd Poynorc7464c92013-09-10 12:40:00 -070060 .battery_update = healthd_mode_android_battery_update,
61};
62
Sandeep Patila3681952016-11-08 16:27:54 -080063// default energy counter property redirect to talk to device
64// HAL
65static 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
80void 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 Phoenix68f40852017-01-23 14:49:09 -080085 gHealth = IHealth::getService();
Sandeep Patila3681952016-11-08 16:27:54 -080086 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
100int 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 Patil526f8cf2016-11-01 16:41:56 -0700118int main(int /*argc*/, char ** /*argv*/) {
Todd Poynorfea5b4d2013-09-09 12:09:08 -0700119
Todd Poynorc7464c92013-09-10 12:40:00 -0700120 healthd_mode_ops = &android_ops;
Todd Poynor752faf22013-06-12 13:25:59 -0700121
Sandeep Patil526f8cf2016-11-01 16:41:56 -0700122 return healthd_main();
Todd Poynor752faf22013-06-12 13:25:59 -0700123}