Caleb Connolly | ae389bf | 2021-11-17 22:48:27 +0000 | [diff] [blame] | 1 | #include <memory> |
Caleb Connolly | ae389bf | 2021-11-17 22:48:27 +0000 | [diff] [blame] | 2 | |
| 3 | #include <health/utils.h> |
| 4 | #include <health2impl/Health.h> |
| 5 | #include <hidl/Status.h> |
| 6 | #include <log/log.h> |
Caleb Connolly | c28a5e3 | 2021-11-26 14:13:35 +0000 | [diff] [blame] | 7 | #include <android-base/strings.h> |
| 8 | #include <string> |
Vincent Knecht | fa06fba | 2022-08-29 19:28:58 +0200 | [diff] [blame^] | 9 | #include <fstream> |
Caleb Connolly | c072981 | 2021-11-23 18:39:11 +0000 | [diff] [blame] | 10 | |
Caleb Connolly | ae389bf | 2021-11-17 22:48:27 +0000 | [diff] [blame] | 11 | using ::android::sp; |
| 12 | using ::android::hardware::Return; |
| 13 | using ::android::hardware::Void; |
| 14 | using ::android::hardware::health::InitHealthdConfig; |
| 15 | using ::android::hardware::health::V2_1::IHealth; |
| 16 | using ::android::hidl::base::V1_0::IBase; |
| 17 | |
| 18 | using namespace std::literals; |
| 19 | |
| 20 | namespace android { |
| 21 | namespace hardware { |
| 22 | namespace health { |
| 23 | namespace V2_1 { |
| 24 | namespace implementation { |
| 25 | |
| 26 | // android::hardware::health::V2_1::implementation::Health implements most |
| 27 | // defaults. Uncomment functions that you need to override. |
| 28 | class HealthImpl : public Health { |
| 29 | public: |
| 30 | HealthImpl(std::unique_ptr<healthd_config>&& config) |
| 31 | : Health(std::move(config)) {} |
Caleb Connolly | ae389bf | 2021-11-17 22:48:27 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | } // namespace implementation |
| 35 | } // namespace V2_1 |
| 36 | } // namespace health |
| 37 | } // namespace hardware |
| 38 | } // namespace android |
| 39 | |
Caleb Connolly | ae389bf | 2021-11-17 22:48:27 +0000 | [diff] [blame] | 40 | extern "C" IHealth* HIDL_FETCH_IHealth(const char* instance) { |
| 41 | using ::android::hardware::health::V2_1::implementation::HealthImpl; |
Vincent Knecht | fa06fba | 2022-08-29 19:28:58 +0200 | [diff] [blame^] | 42 | |
| 43 | static const std::string POWERSUPPLY_DIR = "/sys/class/power_supply"; |
| 44 | |
Caleb Connolly | c072981 | 2021-11-23 18:39:11 +0000 | [diff] [blame] | 45 | if (instance != "default"sv) { |
Caleb Connolly | c28a5e3 | 2021-11-26 14:13:35 +0000 | [diff] [blame] | 46 | ALOGE("Instance is not supported"); |
Caleb Connolly | ae389bf | 2021-11-17 22:48:27 +0000 | [diff] [blame] | 47 | return nullptr; |
| 48 | } |
| 49 | auto config = std::make_unique<healthd_config>(); |
| 50 | InitHealthdConfig(config.get()); |
Vincent Knecht | fa06fba | 2022-08-29 19:28:58 +0200 | [diff] [blame^] | 51 | |
| 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 Connolly | ae389bf | 2021-11-17 22:48:27 +0000 | [diff] [blame] | 70 | |
| 71 | return new HealthImpl(std::move(config)); |
| 72 | } |