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 "health-hal"
18 
19 #include <Health.h>
20 #include <include/hal_conversion.h>
21 
22 namespace android {
23 namespace hardware {
24 namespace health {
25 namespace V1_0 {
26 namespace implementation {
27 
28 using ::android::hardware::health::V1_0::hal_conversion::convertToHealthConfig;
29 using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
30 using ::android::hardware::health::V1_0::hal_conversion::convertToHealthInfo;
31 using ::android::hardware::health::V1_0::hal_conversion::convertFromHealthInfo;
32 
33 // Methods from ::android::hardware::health::V1_0::IHealth follow.
init(const HealthConfig & config,init_cb _hidl_cb)34 Return<void> Health::init(const HealthConfig& config, init_cb _hidl_cb)  {
35     struct healthd_config healthd_config = {};
36     HealthConfig configOut;
37 
38     // To keep working with existing healthd static HALs,
39     // convert the new HealthConfig to the old healthd_config
40     // and back.
41 
42     convertFromHealthConfig(config, &healthd_config);
43     healthd_board_init(&healthd_config);
44     mGetEnergyCounter = healthd_config.energyCounter;
45     convertToHealthConfig(&healthd_config, configOut);
46 
47     _hidl_cb(configOut);
48 
49     return Void();
50 }
51 
update(const HealthInfo & info,update_cb _hidl_cb)52 Return<void> Health::update(const HealthInfo& info, update_cb _hidl_cb)  {
53     struct android::BatteryProperties p = {};
54     HealthInfo infoOut;
55 
56     // To keep working with existing healthd static HALs,
57     // convert the new HealthInfo to android::Batteryproperties
58     // and back.
59 
60     convertFromHealthInfo(info, &p);
61     int skipLogging = healthd_board_battery_update(&p);
62     convertToHealthInfo(&p, infoOut);
63 
64     _hidl_cb(!!skipLogging, infoOut);
65 
66     return Void();
67 }
68 
energyCounter(energyCounter_cb _hidl_cb)69 Return<void> Health::energyCounter(energyCounter_cb _hidl_cb) {
70     int64_t energy = 0;
71     Result result = Result::NOT_SUPPORTED;
72 
73     if (mGetEnergyCounter) {
74         int status = mGetEnergyCounter(&energy);
75         if (status == 0) {
76             result = Result::SUCCESS;
77         }
78     }
79 
80     _hidl_cb(result, energy);
81 
82    return Void();
83 }
84 
HIDL_FETCH_IHealth(const char *)85 IHealth* HIDL_FETCH_IHealth(const char* /* name */) {
86     return new Health();
87 }
88 
89 } // namespace implementation
90 }  // namespace V1_0
91 }  // namespace health
92 }  // namespace hardware
93 }  // namespace android
94