Yifan Hong | b7cd45f | 2017-11-13 18:47:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "charger_test" |
| 18 | #include <android/log.h> |
| 19 | |
| 20 | #include <chrono> |
| 21 | #include <condition_variable> |
| 22 | #include <fstream> |
| 23 | #include <iostream> |
| 24 | #include <mutex> |
| 25 | #include <streambuf> |
| 26 | #include <string> |
| 27 | #include <thread> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include <healthd/healthd.h> |
| 31 | |
| 32 | #define LOG_THIS(fmt, ...) \ |
| 33 | ALOGE(fmt, ##__VA_ARGS__); \ |
| 34 | printf(fmt "\n", ##__VA_ARGS__); |
| 35 | |
| 36 | template <typename T> |
| 37 | class Atomic { |
| 38 | public: |
| 39 | Atomic(T&& init) : mValue(std::move(init)) {} |
| 40 | void set(T&& newVal) { |
| 41 | { |
| 42 | std::lock_guard<std::mutex> lock(mMutex); |
| 43 | mValue = std::move(newVal); |
| 44 | } |
| 45 | mChanged.notify_all(); |
| 46 | } |
| 47 | bool waitFor(long ms, const T& expectVal) { |
| 48 | std::unique_lock<std::mutex> lock(mMutex); |
| 49 | return mChanged.wait_for(lock, std::chrono::milliseconds(ms), |
| 50 | [this, &expectVal] { return mValue == expectVal; }); |
| 51 | } |
| 52 | private: |
| 53 | std::mutex mMutex; |
| 54 | std::condition_variable mChanged; |
| 55 | T mValue; |
| 56 | }; |
| 57 | |
| 58 | Atomic<bool>& getUpdateNotifier() { |
| 59 | static Atomic<bool> val(false); |
| 60 | return val; |
| 61 | } |
| 62 | |
| 63 | int energyCounter(int64_t* counter) { |
| 64 | *counter = 0xEC12345; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | const char* createFile(const char* path, const char* content) { |
| 69 | std::ofstream stream(path); |
| 70 | if (!stream.is_open()) { |
| 71 | LOG_THIS("Cannot create file %s", path); |
| 72 | return NULL; |
| 73 | } |
| 74 | stream << content << std::endl; |
| 75 | stream.close(); |
| 76 | return path; |
| 77 | } |
| 78 | |
| 79 | std::string openToString(const char* path) { |
| 80 | std::ifstream stream(path); |
| 81 | if (!stream.is_open()) { |
| 82 | LOG_THIS("Cannot open file %s", path); |
| 83 | return ""; |
| 84 | } |
| 85 | return std::string(std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>()); |
| 86 | } |
| 87 | |
| 88 | int expectContains(const std::string& content, const std::vector<std::string>& fields) { |
| 89 | int status = 0; |
| 90 | for (const auto& field : fields) { |
| 91 | auto pos = content.find(field); |
| 92 | if (pos == std::string::npos) { |
| 93 | LOG_THIS("Cannot find substr '%s'", field.c_str()); |
| 94 | status = 1; |
| 95 | } |
| 96 | } |
| 97 | return status; |
| 98 | } |
| 99 | |
| 100 | void healthd_board_init(struct healthd_config* config) { |
| 101 | config->periodic_chores_interval_fast = 60; |
| 102 | config->periodic_chores_interval_slow = 600; |
| 103 | |
| 104 | config->batteryStatusPath = createFile("/data/local/tmp/batteryStatus", "Not charging"); |
| 105 | config->batteryHealthPath = createFile("/data/local/tmp/batteryHealth", "Unspecified failure"); |
| 106 | config->batteryPresentPath = createFile("/data/local/tmp/batteryPresent", "1"); |
| 107 | config->batteryCapacityPath = createFile("/data/local/tmp/batteryCapacity", "47"); |
| 108 | config->batteryVoltagePath = createFile("/data/local/tmp/batteryVoltage", "45000"); |
| 109 | config->batteryTemperaturePath = createFile("/data/local/tmp/batteryTemperature", "987"); |
| 110 | config->batteryTechnologyPath = createFile("/data/local/tmp/batteryTechnology", "NiCd"); |
| 111 | config->batteryCurrentNowPath = createFile("/data/local/tmp/batteryCurrentNow", "99000"); |
| 112 | config->batteryCurrentAvgPath = createFile("/data/local/tmp/batteryCurrentAvg", "98000"); |
| 113 | config->batteryChargeCounterPath = createFile("/data/local/tmp/batteryChargeCounter", "600"); |
| 114 | config->batteryFullChargePath = createFile("/data/local/tmp/batteryFullCharge", "3515547"); |
| 115 | config->batteryCycleCountPath = createFile("/data/local/tmp/batteryCycleCount", "77"); |
| 116 | |
| 117 | config->energyCounter = energyCounter; |
| 118 | config->boot_min_cap = 50; |
| 119 | config->screen_on = NULL; |
| 120 | } |
| 121 | |
| 122 | int healthd_board_battery_update(struct android::BatteryProperties*) { |
| 123 | getUpdateNotifier().set(true /* updated */); |
| 124 | |
| 125 | // return 0 to log periodic polled battery status to kernel log |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | extern int healthd_charger_main(int argc, char** argv); |
| 130 | |
| 131 | int main(int argc, char** argv) { |
| 132 | const char* dumpFile = "/data/local/tmp/dump.txt"; |
| 133 | |
| 134 | std::thread bgThread([=] { |
| 135 | healthd_charger_main(argc, argv); |
| 136 | }); |
| 137 | |
| 138 | // wait for healthd_init to finish |
| 139 | if (!getUpdateNotifier().waitFor(1000 /* wait ms */, true /* updated */)) { |
| 140 | LOG_THIS("Time out."); |
| 141 | exit(1); |
| 142 | } |
| 143 | |
| 144 | int fd = creat(dumpFile, S_IRUSR | S_IWUSR); |
| 145 | if (fd < 0) { |
| 146 | exit(errno); |
| 147 | } |
| 148 | healthd_dump_battery_state(fd); |
| 149 | close(fd); |
| 150 | |
| 151 | std::string content = openToString(dumpFile); |
| 152 | int status = expectContains(content, { |
| 153 | "status: 4", |
| 154 | "health: 6", |
| 155 | "present: 1", |
| 156 | "level: 47", |
| 157 | "voltage: 45", |
| 158 | "temp: 987", |
| 159 | "current now: 99000", |
| 160 | "current avg: 98000", |
| 161 | "charge counter: 600", |
| 162 | "current now: 99", |
| 163 | "cycle count: 77", |
| 164 | "Full charge: 3515547" |
| 165 | }); |
| 166 | |
| 167 | if (status == 0) { |
| 168 | LOG_THIS("Test success."); |
| 169 | } else { |
| 170 | LOG_THIS("Actual dump:\n%s", content.c_str()); |
| 171 | } |
| 172 | |
| 173 | exit(status); // force bgThread to exit |
| 174 | } |