blob: 2ad0416784858e22a4a3e3a4a2d6383529e6e2ab [file] [log] [blame]
Subbaraman Narayanamurthy995c63a2020-08-26 21:51:48 -07001/*
2 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#define LOG_TAG "android.hardware.health@2.1-service.qti"
31
32#include <android-base/logging.h>
33#include <health/utils.h>
34#include <health2impl/Health.h>
35#include <log/log.h>
36
37using ::android::hardware::health::InitHealthdConfig;
38using ::android::hardware::health::V2_1::IHealth;
39using namespace std::literals;
40
41namespace {
42void qti_healthd_board_init(struct healthd_config *)
43{
44 int fd;
45 unsigned char retries = 50;
46
47retry:
48 if (!retries) {
49 ALOGE("Cannot open battery/capacity, fd=%d\n", fd);
50 return;
51 }
52
53 fd = open("/sys/class/power_supply/battery/capacity", 0440);
54 if (fd >= 0) {
55 ALOGI("opened battery/capacity after %d retries\n", 50 - retries);
56 close(fd);
57 return;
58 }
59
60 retries--;
61 usleep(100000);
62 goto retry;
63}
64} //anonymous namespace
65
66namespace android {
67namespace hardware {
68namespace health {
69namespace V2_1 {
70namespace implementation {
71
72class HealthImpl : public Health {
73 public:
74 HealthImpl(std::unique_ptr<healthd_config>&& config)
75 : Health(std::move(config)) {}
76};
77
78} // namespace implementation
79} // namespace V2_1
80} // namespace health
81} // namespace hardware
82} // namespace android
83
84
85extern "C" IHealth* HIDL_FETCH_IHealth(const char* instance) {
86 using ::android::hardware::health::V2_1::implementation::HealthImpl;
87 if (instance != "default"sv) {
88 return nullptr;
89 }
90 auto config = std::make_unique<healthd_config>();
91 InitHealthdConfig(config.get());
92 qti_healthd_board_init(config.get());
93
94 return new HealthImpl(std::move(config));
95}