blob: c3de157a0128cb156b361501fc7e271e1b678053 [file] [log] [blame]
Caleb Connollyae389bf2021-11-17 22:48:27 +00001#include <memory>
Caleb Connollyae389bf2021-11-17 22:48:27 +00002
3#include <health/utils.h>
4#include <health2impl/Health.h>
5#include <hidl/Status.h>
6#include <log/log.h>
Caleb Connollyc28a5e32021-11-26 14:13:35 +00007#include <android-base/strings.h>
8#include <string>
Vincent Knechtfa06fba2022-08-29 19:28:58 +02009#include <fstream>
Caleb Connollyc0729812021-11-23 18:39:11 +000010
Caleb Connollyae389bf2021-11-17 22:48:27 +000011using ::android::sp;
12using ::android::hardware::Return;
13using ::android::hardware::Void;
14using ::android::hardware::health::InitHealthdConfig;
15using ::android::hardware::health::V2_1::IHealth;
16using ::android::hidl::base::V1_0::IBase;
17
18using namespace std::literals;
19
20namespace android {
21namespace hardware {
22namespace health {
23namespace V2_1 {
24namespace implementation {
25
26// android::hardware::health::V2_1::implementation::Health implements most
27// defaults. Uncomment functions that you need to override.
28class HealthImpl : public Health {
29 public:
30 HealthImpl(std::unique_ptr<healthd_config>&& config)
31 : Health(std::move(config)) {}
Caleb Connollyae389bf2021-11-17 22:48:27 +000032};
33
34} // namespace implementation
35} // namespace V2_1
36} // namespace health
37} // namespace hardware
38} // namespace android
39
Caleb Connollyae389bf2021-11-17 22:48:27 +000040extern "C" IHealth* HIDL_FETCH_IHealth(const char* instance) {
41 using ::android::hardware::health::V2_1::implementation::HealthImpl;
Vincent Knechtfa06fba2022-08-29 19:28:58 +020042
43 static const std::string POWERSUPPLY_DIR = "/sys/class/power_supply";
44
Caleb Connollyc0729812021-11-23 18:39:11 +000045 if (instance != "default"sv) {
Caleb Connollyc28a5e32021-11-26 14:13:35 +000046 ALOGE("Instance is not supported");
Caleb Connollyae389bf2021-11-17 22:48:27 +000047 return nullptr;
48 }
49 auto config = std::make_unique<healthd_config>();
50 InitHealthdConfig(config.get());
Vincent Knechtfa06fba2022-08-29 19:28:58 +020051
52 if (auto supplies = opendir(POWERSUPPLY_DIR.c_str())) {
53 while (dirent *ent = readdir(supplies)) {
54 if ((ent->d_type == DT_DIR && ent->d_name[0] != '.') || ent->d_type == DT_LNK) {
55 std::string supplyPath = POWERSUPPLY_DIR + "/" + ent->d_name;
56 ALOGI("Found %s", supplyPath.c_str());
57 if (auto stream = std::ifstream(supplyPath + "/status")) {
58 if (stream.good()) {
59 config->batteryStatusPath = supplyPath.append("/status").c_str();
60 break;
61 }
62 }
63 }
64 }
65 closedir(supplies);
66 ALOGI("Using %s", config->batteryStatusPath.c_str());
67 } else {
68 ALOGE("Failed to open %s", POWERSUPPLY_DIR.c_str());
69 }
Caleb Connollyae389bf2021-11-17 22:48:27 +000070
71 return new HealthImpl(std::move(config));
72}