blob: a7e2161964f1a8cf36ba2b08b5cb77b84aae9428 [file] [log] [blame]
Yifan Hongb7cd45f2017-11-13 18:47:03 -08001/*
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
Yifan Hong25eb37a2017-11-13 13:31:35 -080030#include <health2/Health.h>
Yifan Hongb7cd45f2017-11-13 18:47:03 -080031
32#define LOG_THIS(fmt, ...) \
33 ALOGE(fmt, ##__VA_ARGS__); \
34 printf(fmt "\n", ##__VA_ARGS__);
35
36template <typename T>
37class 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
58Atomic<bool>& getUpdateNotifier() {
59 static Atomic<bool> val(false);
60 return val;
61}
62
63int energyCounter(int64_t* counter) {
64 *counter = 0xEC12345;
65 return 0;
66}
67
68const 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
79std::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
88int 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
Yifan Hong25eb37a2017-11-13 13:31:35 -0800100::android::hardware::hidl_handle createHidlHandle(const char* filepath) {
101 int fd = creat(filepath, S_IRUSR | S_IWUSR);
102 if (fd < 0) return {};
103 native_handle_t* nativeHandle = native_handle_create(1, 0);
104 nativeHandle->data[0] = fd;
105 ::android::hardware::hidl_handle handle;
106 handle.setTo(nativeHandle, true /* shouldOwn */);
107 return handle;
108}
109
Yifan Hongb7cd45f2017-11-13 18:47:03 -0800110void healthd_board_init(struct healthd_config* config) {
111 config->periodic_chores_interval_fast = 60;
112 config->periodic_chores_interval_slow = 600;
113
114 config->batteryStatusPath = createFile("/data/local/tmp/batteryStatus", "Not charging");
115 config->batteryHealthPath = createFile("/data/local/tmp/batteryHealth", "Unspecified failure");
116 config->batteryPresentPath = createFile("/data/local/tmp/batteryPresent", "1");
117 config->batteryCapacityPath = createFile("/data/local/tmp/batteryCapacity", "47");
118 config->batteryVoltagePath = createFile("/data/local/tmp/batteryVoltage", "45000");
119 config->batteryTemperaturePath = createFile("/data/local/tmp/batteryTemperature", "987");
120 config->batteryTechnologyPath = createFile("/data/local/tmp/batteryTechnology", "NiCd");
121 config->batteryCurrentNowPath = createFile("/data/local/tmp/batteryCurrentNow", "99000");
122 config->batteryCurrentAvgPath = createFile("/data/local/tmp/batteryCurrentAvg", "98000");
123 config->batteryChargeCounterPath = createFile("/data/local/tmp/batteryChargeCounter", "600");
124 config->batteryFullChargePath = createFile("/data/local/tmp/batteryFullCharge", "3515547");
125 config->batteryCycleCountPath = createFile("/data/local/tmp/batteryCycleCount", "77");
126
127 config->energyCounter = energyCounter;
128 config->boot_min_cap = 50;
129 config->screen_on = NULL;
130}
131
132int healthd_board_battery_update(struct android::BatteryProperties*) {
133 getUpdateNotifier().set(true /* updated */);
134
135 // return 0 to log periodic polled battery status to kernel log
136 return 0;
137}
138
139extern int healthd_charger_main(int argc, char** argv);
140
141int main(int argc, char** argv) {
Yifan Hong25eb37a2017-11-13 13:31:35 -0800142 using android::hardware::health::V2_0::implementation::Health;
143
Yifan Hongb7cd45f2017-11-13 18:47:03 -0800144 const char* dumpFile = "/data/local/tmp/dump.txt";
145
146 std::thread bgThread([=] {
147 healthd_charger_main(argc, argv);
148 });
149
150 // wait for healthd_init to finish
151 if (!getUpdateNotifier().waitFor(1000 /* wait ms */, true /* updated */)) {
152 LOG_THIS("Time out.");
153 exit(1);
154 }
155
Yifan Hong25eb37a2017-11-13 13:31:35 -0800156 Health::getImplementation()->debug(createHidlHandle(dumpFile), {} /* options */);
Yifan Hongb7cd45f2017-11-13 18:47:03 -0800157
158 std::string content = openToString(dumpFile);
159 int status = expectContains(content, {
160 "status: 4",
161 "health: 6",
162 "present: 1",
163 "level: 47",
164 "voltage: 45",
165 "temp: 987",
166 "current now: 99000",
167 "current avg: 98000",
168 "charge counter: 600",
169 "current now: 99",
170 "cycle count: 77",
171 "Full charge: 3515547"
172 });
173
174 if (status == 0) {
175 LOG_THIS("Test success.");
176 } else {
177 LOG_THIS("Actual dump:\n%s", content.c_str());
178 }
179
180 exit(status); // force bgThread to exit
181}