blob: 79ed4ccc0f02592b6a5ae0a39088f0aeb16a22d3 [file] [log] [blame]
samou6c250222024-07-16 07:40:36 +00001
2/*
3 * Copyright 2023 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include <cstring>
18#include <dirent.h>
19#include <dump/pixel_dump.h>
20#include <fstream>
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/sysinfo.h>
24#include <time.h>
25#include <vector>
26#include <android-base/file.h>
27#include <android-base/strings.h>
28#include "DumpstateUtil.h"
29void printTitle(const char *msg) {
30 printf("\n------ %s ------\n", msg);
31}
32int getCommandOutput(const char *cmd, std::string *output) {
33 char buffer[1024];
34 FILE *pipe = popen(cmd, "r");
35 if (!pipe) {
36 return -1;
37 }
38 while (fgets(buffer, sizeof buffer, pipe) != NULL) {
39 *output += buffer;
40 }
41 pclose(pipe);
42 if (output->back() == '\n')
43 output->pop_back();
44 return 0;
45}
46bool isValidFile(const char *file) {
Spade Lee7bbc59e2024-10-29 16:07:29 +000047 FILE *fp = fopen(file, "r");
48 if (fp != NULL) {
49 fclose(fp);
50 return true;
samou6c250222024-07-16 07:40:36 +000051 }
Spade Lee7bbc59e2024-10-29 16:07:29 +000052 return false;
53}
54bool isValidDir(const char *directory) {
55 DIR *dir = opendir(directory);
56 if (dir == NULL)
57 return false;
58
59 closedir(dir);
samou6c250222024-07-16 07:40:36 +000060 return true;
61}
62bool isUserBuild() {
63 return ::android::os::dumpstate::PropertiesHelper::IsUserBuild();
64}
65int getFilesInDir(const char *directory, std::vector<std::string> *files) {
66 std::string content;
67 struct dirent *entry;
68 DIR *dir = opendir(directory);
69 if (dir == NULL)
70 return -1;
71 files->clear();
72 while ((entry = readdir(dir)) != NULL)
73 files->push_back(entry->d_name);
74 closedir(dir);
75 sort(files->begin(), files->end());
76 return 0;
77}
78void dumpPowerStatsTimes() {
79 const char *title = "Power Stats Times";
80 char rBuff[128];
81 struct timespec rTs;
82 struct sysinfo info;
83 int ret;
84 printTitle(title);
85 sysinfo(&info);
86 const time_t boottime = time(NULL) - info.uptime;
87 ret = clock_gettime(CLOCK_REALTIME, &rTs);
88 if (ret)
89 return;
90 struct tm *nowTime = std::localtime(&rTs.tv_sec);
91 std::strftime(rBuff, sizeof(rBuff), "%m/%d/%Y %H:%M:%S", nowTime);
92 printf("Boot: %s", ctime(&boottime));
93 printf("Now: %s\n", rBuff);
94}
95int readContentsOfDir(const char* title, const char* directory, const char* strMatch,
96 bool useStrMatch = false, bool printDirectory = false) {
97 std::vector<std::string> files;
98 std::string content;
99 std::string fileLocation;
100 int ret;
101 ret = getFilesInDir(directory, &files);
102 if (ret < 0)
103 return ret;
104 printTitle(title);
105 for (auto &file : files) {
106 if (useStrMatch && std::string::npos == std::string(file).find(strMatch)) {
107 continue;
108 }
109 fileLocation = std::string(directory) + std::string(file);
110 if (!android::base::ReadFileToString(fileLocation, &content)) {
111 continue;
112 }
113 if (printDirectory) {
114 printf("\n\n%s\n", fileLocation.c_str());
115 }
116 if (content.back() == '\n')
117 content.pop_back();
118 printf("%s\n", content.c_str());
119 }
120 return 0;
121}
122void dumpAcpmStats() {
123 const char* acpmDir = "/sys/devices/platform/acpm_stats/";
124 const char* statsSubStr = "_stats";
125 const char* acpmTitle = "ACPM stats";
126 readContentsOfDir(acpmTitle, acpmDir, statsSubStr, true, true);
127}
128void dumpPowerSupplyStats() {
129 const char* dumpList[][2] = {
130 {"CPU PM stats", "/sys/devices/system/cpu/cpupm/cpupm/time_in_state"},
samou6c250222024-07-16 07:40:36 +0000131 {"Power supply property battery", "/sys/class/power_supply/battery/uevent"},
132 {"Power supply property dc", "/sys/class/power_supply/dc/uevent"},
133 {"Power supply property gcpm", "/sys/class/power_supply/gcpm/uevent"},
134 {"Power supply property gcpm_pps", "/sys/class/power_supply/gcpm_pps/uevent"},
135 {"Power supply property main-charger", "/sys/class/power_supply/main-charger/uevent"},
Spade Lee7bbc59e2024-10-29 16:07:29 +0000136 {"Power supply property pca9486-mains", "/sys/class/power_supply/pca9486-mains/uevent"},
137 {"Power supply property tcpm", "/sys/class/power_supply/tcpm-source-psy-i2c-max77759tcpc/uevent"},
samou6c250222024-07-16 07:40:36 +0000138 {"Power supply property usb", "/sys/class/power_supply/usb/uevent"},
139 {"Power supply property wireless", "/sys/class/power_supply/wireless/uevent"},
140 };
141 for (const auto &row : dumpList) {
142 dumpFileContent(row[0], row[1]);
143 }
144}
145void dumpMaxFg() {
146 const char *maxfgLoc = "/sys/class/power_supply/maxfg";
147 const char *maxfg [][2] = {
148 {"Power supply property maxfg", "/sys/class/power_supply/maxfg/uevent"},
149 {"m5_state", "/sys/class/power_supply/maxfg/m5_model_state"},
Spade Lee7bbc59e2024-10-29 16:07:29 +0000150 {"maxfg registers", "/sys/class/power_supply/maxfg/registers_dump"},
samou6c250222024-07-16 07:40:36 +0000151 {"maxfg", "/dev/logbuffer_maxfg"},
152 {"maxfg", "/dev/logbuffer_maxfg_monitor"},
153 };
154 const char *maxfgFlip [][2] = {
155 {"Power supply property maxfg_base", "/sys/class/power_supply/maxfg_base/uevent"},
156 {"Power supply property maxfg_flip", "/sys/class/power_supply/maxfg_flip/uevent"},
Spade Lee7bbc59e2024-10-29 16:07:29 +0000157 {"maxfg_base registers", "/sys/class/power_supply/maxfg_base/registers_dump"},
158 {"maxfg_secondary registers", "/sys/class/power_supply/maxfg_secondary/registers_dump"},
samou6c250222024-07-16 07:40:36 +0000159 {"m5_state", "/sys/class/power_supply/maxfg_base/m5_model_state"},
160 {"maxfg_base", "/dev/logbuffer_maxfg_base"},
161 {"maxfg_flip", "/dev/logbuffer_maxfg_flip"},
162 {"maxfg_base", "/dev/logbuffer_maxfg_base_monitor"},
163 {"maxfg_flip", "/dev/logbuffer_maxfg_flip_monitor"},
164 };
165 const char *maxfgHistoryName = "Maxim FG History";
166 const char *maxfgHistoryDir = "/dev/maxfg_history";
167 std::string content;
Spade Lee7bbc59e2024-10-29 16:07:29 +0000168 if (isValidDir(maxfgLoc)) {
samou6c250222024-07-16 07:40:36 +0000169 for (const auto &row : maxfg) {
170 dumpFileContent(row[0], row[1]);
171 }
172 } else {
173 for (const auto &row : maxfgFlip) {
174 dumpFileContent(row[0], row[1]);
175 }
176 }
177 if (isValidFile(maxfgHistoryDir)) {
178 dumpFileContent(maxfgHistoryName, maxfgHistoryDir);
179 }
180}
181void dumpPowerSupplyDock() {
182 const char* powerSupplyPropertyDockTitle = "Power supply property dock";
183 const char* powerSupplyPropertyDockFile = "/sys/class/power_supply/dock/uevent";
184 dumpFileContent(powerSupplyPropertyDockTitle, powerSupplyPropertyDockFile);
185}
186void dumpLogBufferTcpm() {
187 const char* logbufferTcpmTitle = "Logbuffer TCPM";
188 const char* logbufferTcpmFile = "/dev/logbuffer_tcpm";
189 const char* debugTcpmFile = "/sys/kernel/debug/tcpm";
190 const char* tcpmLogTitle = "TCPM logs";
191 const char* tcpmFile = "/sys/kernel/debug/tcpm";
192 const char* tcpmFileAlt = "/sys/kernel/debug/usb/tcpm";
193 int retCode;
194 dumpFileContent(logbufferTcpmTitle, logbufferTcpmFile);
195 retCode = readContentsOfDir(tcpmLogTitle, isValidFile(debugTcpmFile) ? tcpmFile : tcpmFileAlt,
196 NULL);
197 if (retCode < 0)
198 printTitle(tcpmLogTitle);
199}
200void dumpTcpc() {
201 int ret;
202 const char* max77759TcpcHead = "TCPC";
203 const char* i2cSubDirMatch = "i2c-";
204 const char* directory = "/sys/devices/platform/10d60000.hsi2c/";
205 const char* max77759Tcpc [][2] {
206 {"registers:", "/i2c-max77759tcpc/registers"},
207 {"frs:", "/i2c-max77759tcpc/frs"},
208 {"auto_discharge:", "/i2c-max77759tcpc/auto_discharge"},
209 {"bcl2_enabled:", "/i2c-max77759tcpc/bcl2_enabled"},
210 {"cc_toggle_enable:", "/i2c-max77759tcpc/cc_toggle_enable"},
211 {"containment_detection:", "/i2c-max77759tcpc/containment_detection"},
212 {"containment_detection_status:", "/i2c-max77759tcpc/containment_detection_status"},
213 };
214 std::vector<std::string> files;
215 std::string content;
216 printTitle(max77759TcpcHead);
217 ret = getFilesInDir(directory, &files);
218 if (ret < 0) {
219 for (auto &tcpcVal : max77759Tcpc)
220 printf("%s\n", tcpcVal[0]);
221 return;
222 }
223 for (auto &file : files) {
224 for (auto &tcpcVal : max77759Tcpc) {
225 printf("%s ", tcpcVal[0]);
226 if (std::string::npos == std::string(file).find(i2cSubDirMatch)) {
227 continue;
228 }
229 std::string fileName = directory + file + "/" + std::string(tcpcVal[1]);
230 if (!android::base::ReadFileToString(fileName, &content)) {
231 continue;
232 }
233 printf("%s\n", content.c_str());
234 }
235 }
236}
237void dumpPdEngine() {
238 const char* pdEngine [][2] {
239 {"PD Engine", "/dev/logbuffer_usbpd"},
240 {"PPS-google_cpm", "/dev/logbuffer_cpm"},
241 {"PPS-dc", "/dev/logbuffer_pca9468"},
242 };
243 for (const auto &row : pdEngine) {
244 dumpFileContent(row[0], row[1]);
245 }
246}
samou6c250222024-07-16 07:40:36 +0000247void dumpBatteryHealth() {
248 const char* batteryHealth [][2] {
249 {"Battery Health", "/sys/class/power_supply/battery/health_index_stats"},
Spade Lee7bbc59e2024-10-29 16:07:29 +0000250 {"Battery Health SoC Residency", "/sys/class/power_supply/battery/swelling_data"},
samou6c250222024-07-16 07:40:36 +0000251 {"BMS", "/dev/logbuffer_ssoc"},
252 {"TTF", "/dev/logbuffer_ttf"},
253 {"TTF details", "/sys/class/power_supply/battery/ttf_details"},
254 {"TTF stats", "/sys/class/power_supply/battery/ttf_stats"},
255 {"aacr_state", "/sys/class/power_supply/battery/aacr_state"},
256 {"maxq", "/dev/logbuffer_maxq"},
257 {"TEMP/DOCK-DEFEND", "/dev/logbuffer_bd"},
258 };
259 for (const auto &row : batteryHealth) {
260 dumpFileContent(row[0], row[1]);
261 }
262}
263void dumpBatteryDefend() {
264 const char* defendConfig [][3] {
265 {"TRICKLE-DEFEND Config",
266 "/sys/devices/platform/google,battery/power_supply/battery/", "bd_"},
267 {"DWELL-DEFEND Config", "/sys/devices/platform/google,charger/", "charge_s"},
268 {"TEMP-DEFEND Config", "/sys/devices/platform/google,charger/", "bd_"},
269 };
270 std::vector<std::string> files;
271 struct dirent *entry;
272 std::string content;
273 std::string fileLocation;
274 for (auto &config : defendConfig) {
275 DIR *dir = opendir(config[1]);
276 if (dir == NULL)
277 continue;
278 printTitle(config[0]);
279 while ((entry = readdir(dir)) != NULL) {
280 if (std::string(entry->d_name).find(config[2]) != std::string::npos &&
281 strncmp(config[2], entry->d_name, strlen(config[2])) == 0) {
282 files.push_back(entry->d_name);
283 }
284 }
285 closedir(dir);
286 sort(files.begin(), files.end());
287 for (auto &file : files) {
288 fileLocation = std::string(config[1]) + std::string(file);
Spade Lee7bbc59e2024-10-29 16:07:29 +0000289 if (!android::base::ReadFileToString(fileLocation, &content) || content.empty()) {
samou6c250222024-07-16 07:40:36 +0000290 content = "\n";
291 }
292 printf("%s: %s", file.c_str(), content.c_str());
293 if (content.back() != '\n')
294 printf("\n");
295 }
296 files.clear();
297 }
298}
Spade Lee7bbc59e2024-10-29 16:07:29 +0000299void printValuesOfDirectory(const char *directory, std::string debugfs, const char *strMatch) {
300 std::vector<std::string> files;
301 auto info = directory;
302 std::string content;
303 struct dirent *entry;
304 DIR *dir = opendir(debugfs.c_str());
305 if (dir == NULL)
306 return;
307
308 printTitle((debugfs + std::string(strMatch) + "/" + std::string(info)).c_str());
309 while ((entry = readdir(dir)) != NULL)
310 if (std::string(entry->d_name).find(strMatch) != std::string::npos)
311 files.push_back(entry->d_name);
312 closedir(dir);
313
314 sort(files.begin(), files.end());
315
316 for (auto &file : files) {
317 std::string fileDirectory = debugfs + file;
318 std::string fileLocation = fileDirectory + "/" + std::string(info);
319 if (!android::base::ReadFileToString(fileLocation, &content)) {
320 content = "\n";
321 }
322
323 printf("%s:\n%s", fileDirectory.c_str(), content.c_str());
324
325 if (content.back() != '\n')
326 printf("\n");
327 }
328 files.clear();
329}
330void dumpChg() {
331 const std::string pmic_bus = "/sys/devices/platform/10cb0000.hsi2c/i2c-11/11-0066";
332 const char* chg_reg_dump_file = "/sys/class/power_supply/main-charger/device/registers_dump";
333 const std::string chg_name_cmd = "/sys/class/power_supply/main-charger/device/name";
334 const std::string pmic_name_cmd = pmic_bus + "/name";
335 const std::string pmic_reg_dump_file = pmic_bus + "/registers_dump";
336 const std::string reg_dump_str = " registers dump";
337 const char* chgConfig [][2] {
338 {"DC_registers dump", "/sys/class/power_supply/pca9468-mains/device/registers_dump"},
339 };
340 std::string chg_name;
341 std::string pmic_name;
342
343 printf("\n");
344
345 int ret = android::base::ReadFileToString(chg_name_cmd, &chg_name);
346 if (ret && !chg_name.empty()) {
347 chg_name.erase(chg_name.length() - 1); // remove new line
348 const std::string chg_reg_dump_title = chg_name + reg_dump_str;
349
350 /* CHG reg dump */
351 dumpFileContent(chg_reg_dump_title.c_str(), chg_reg_dump_file);
352 }
353
354 ret = android::base::ReadFileToString(pmic_name_cmd, &pmic_name);
355 if (ret && !pmic_name.empty()) {
356 pmic_name.erase(pmic_name.length() - 1); // remove new line
357 const std::string pmic_reg_dump_title = pmic_name + reg_dump_str;
358
359 /* PMIC reg dump */
360 dumpFileContent(pmic_reg_dump_title.c_str(), pmic_reg_dump_file.c_str());
361 }
362
363 for (auto &config : chgConfig) {
364 dumpFileContent(config[0], config[1]);
365 }
366}
367void dumpChgUserDebug() {
368 const std::string debugfs = "/d/";
369 const char *maxFgDir = "/d/maxfg";
370 const char *maxFgStrMatch = "maxfg";
371 const char *maxFg77779StrMatch = "max77779fg";
372 const char *chgTblName = "Charging table dump";
373 const char *chgTblDir = "/d/google_battery/chg_raw_profile";
374
375 const char *maxFgInfo [] {
376 "fg_model",
377 "algo_ver",
378 "model_ok",
379 };
380
381 const char *max77779FgInfo [] {
382 "fg_model",
383 "algo_ver",
384 "model_ok",
385 };
386
387 if (isUserBuild())
388 return;
389
390 dumpFileContent(chgTblName, chgTblDir);
391
392 if (isValidDir(maxFgDir)) {
393 for (auto & directory : maxFgInfo) {
394 printValuesOfDirectory(directory, debugfs, maxFgStrMatch);
395 }
396 } else {
397 for (auto & directory : max77779FgInfo) {
398 printValuesOfDirectory(directory, debugfs, maxFg77779StrMatch);
399 }
400 }
401}
samou6c250222024-07-16 07:40:36 +0000402void dumpBatteryEeprom() {
403 const char *title = "Battery EEPROM";
404 const char *files[] {
Spade Lee7bbc59e2024-10-29 16:07:29 +0000405 "/sys/devices/platform/10970000.hsi2c/i2c-8/8-0050/eeprom",
samou6c250222024-07-16 07:40:36 +0000406 };
407 std::string result;
408 std::string xxdCmd;
409 printTitle(title);
410 for (auto &file : files) {
411 if (!isValidFile(file))
412 continue;
413 xxdCmd = "xxd " + std::string(file);
414 int ret = getCommandOutput(xxdCmd.c_str(), &result);
415 if (ret < 0)
416 return;
417 printf("%s\n", result.c_str());
418 }
419}
420void dumpChargerStats() {
421 const char *chgStatsTitle = "Charger Stats";
422 const char *chgStatsLocation = "/sys/class/power_supply/battery/charge_details";
423 const char *chargerStats [][3] {
424 {"Google Charger", "/sys/kernel/debug/google_charger/", "pps_"},
425 {"Google Battery", "/sys/kernel/debug/google_battery/", "ssoc_"},
426 };
427 std::vector<std::string> files;
428 std::string content;
429 struct dirent *entry;
430 dumpFileContent(chgStatsTitle, chgStatsLocation);
Spade Lee7bbc59e2024-10-29 16:07:29 +0000431 if (isUserBuild())
samou6c250222024-07-16 07:40:36 +0000432 return;
433 for (auto &stat : chargerStats) {
434 DIR *dir = opendir(stat[1]);
435 if (dir == NULL)
436 return;
437 printTitle(stat[0]);
438 while ((entry = readdir(dir)) != NULL)
439 if (std::string(entry->d_name).find(stat[2]) != std::string::npos)
440 files.push_back(entry->d_name);
441 closedir(dir);
442 sort(files.begin(), files.end());
443 for (auto &file : files) {
444 std::string fileLocation = std::string(stat[1]) + file;
445 if (!android::base::ReadFileToString(fileLocation, &content)) {
446 content = "\n";
447 }
448 printf("%s: %s", file.c_str(), content.c_str());
449 if (content.back() != '\n')
450 printf("\n");
451 }
452 files.clear();
453 }
454}
455void dumpWlcLogs() {
456 const char *dumpWlcList [][2] {
457 {"WLC Logs", "/dev/logbuffer_wireless"},
458 {"WLC VER", "/sys/class/power_supply/wireless/device/version"},
459 {"WLC STATUS", "/sys/class/power_supply/wireless/device/status"},
460 {"WLC FW Version", "/sys/class/power_supply/wireless/device/fw_rev"},
461 {"RTX", "/dev/logbuffer_rtx"},
462 };
463 for (auto &row : dumpWlcList) {
464 if (!isValidFile(row[1]))
465 printTitle(row[0]);
466 dumpFileContent(row[0], row[1]);
467 }
468}
469void dumpGvoteables() {
470 const char *directory = "/sys/kernel/debug/gvotables/";
471 const char *statusName = "/status";
472 const char *title = "gvotables";
473 std::string content;
474 std::vector<std::string> files;
475 int ret;
Spade Lee7bbc59e2024-10-29 16:07:29 +0000476 if (isUserBuild())
samou6c250222024-07-16 07:40:36 +0000477 return;
478 ret = getFilesInDir(directory, &files);
479 if (ret < 0)
480 return;
481 printTitle(title);
482 for (auto &file : files) {
483 std::string fileLocation = std::string(directory) + file + std::string(statusName);
484 if (!android::base::ReadFileToString(fileLocation, &content)) {
485 continue;
486 }
487 printf("%s: %s", file.c_str(), content.c_str());
488 if (content.back() != '\n')
489 printf("\n");
490 }
491 files.clear();
492}
493void dumpMitigation() {
494 const char *mitigationList [][2] {
495 {"Lastmeal" , "/data/vendor/mitigation/lastmeal.txt"},
496 {"Thismeal" , "/data/vendor/mitigation/thismeal.txt"},
497 };
498 for (auto &row : mitigationList) {
499 if (!isValidFile(row[1]))
500 printTitle(row[0]);
501 dumpFileContent(row[0], row[1]);
502 }
503}
504void dumpMitigationStats() {
505 int ret;
506 const char *directory = "/sys/devices/virtual/pmic/mitigation/last_triggered_count/";
507 const char *capacityDirectory = "/sys/devices/virtual/pmic/mitigation/last_triggered_capacity/";
508 const char *timestampDirectory =
509 "/sys/devices/virtual/pmic/mitigation/last_triggered_timestamp/";
510 const char *voltageDirectory = "/sys/devices/virtual/pmic/mitigation/last_triggered_voltage/";
511 const char *capacitySuffix = "_cap";
512 const char *timeSuffix = "_time";
513 const char *voltageSuffix = "_volt";
514 const char *countSuffix = "_count";
515 const char *title = "Mitigation Stats";
516 std::vector<std::string> files;
517 std::string content;
518 std::string fileLocation;
519 std::string source;
520 std::string subModuleName;
521 int count;
522 int soc;
523 int time;
524 int voltage;
525 ret = getFilesInDir(directory, &files);
526 if (ret < 0)
527 return;
528 printTitle(title);
529 printf("Source\t\tCount\tSOC\tTime\tVoltage\n");
530 for (auto &file : files) {
531 fileLocation = std::string(directory) + std::string(file);
532 if (!android::base::ReadFileToString(fileLocation, &content)) {
533 continue;
534 }
535 ret = atoi(android::base::Trim(content).c_str());
536 if (ret == -1)
537 continue;
538 count = ret;
539 subModuleName = std::string(file);
540 subModuleName.erase(subModuleName.find(countSuffix), strlen(countSuffix));
541 fileLocation = std::string(capacityDirectory) + std::string(subModuleName) +
542 std::string(capacitySuffix);
543 if (!android::base::ReadFileToString(fileLocation, &content)) {
544 continue;
545 }
546 ret = atoi(android::base::Trim(content).c_str());
547 if (ret == -1)
548 continue;
549 soc = ret;
550 fileLocation = std::string(timestampDirectory) + std::string(subModuleName) +
551 std::string(timeSuffix);
552 if (!android::base::ReadFileToString(fileLocation, &content)) {
553 continue;
554 }
555 ret = atoi(android::base::Trim(content).c_str());
556 if (ret == -1)
557 continue;
558 time = ret;
559 fileLocation = std::string(voltageDirectory) + std::string(subModuleName) +
560 std::string(voltageSuffix);
561 if (!android::base::ReadFileToString(fileLocation, &content)) {
562 continue;
563 }
564 ret = atoi(android::base::Trim(content).c_str());
565 if (ret == -1)
566 continue;
567 voltage = ret;
568 printf("%s \t%i\t%i\t%i\t%i\n", subModuleName.c_str(), count, soc, time, voltage);
569 }
570}
571void dumpMitigationDirs() {
572 const int paramCount = 4;
573 const char *titles[] = {
574 "Clock Divider Ratio",
575 "Clock Stats",
576 "Triggered Level",
577 "Instruction",
578 };
579 const char *directories[] = {
580 "/sys/devices/virtual/pmic/mitigation/clock_ratio/",
581 "/sys/devices/virtual/pmic/mitigation/clock_stats/",
582 "/sys/devices/virtual/pmic/mitigation/triggered_lvl/",
583 "/sys/devices/virtual/pmic/mitigation/instruction/",
584 };
585 const char *paramSuffix[] = {"_ratio", "_stats", "_lvl", ""};
586 const char *titleRowVal[] = {
587 "Source\t\tRatio",
588 "Source\t\tStats",
589 "Source\t\tLevel",
590 "",
591 };
592 const int eraseCnt[] = {6, 6, 4, 0};
593 const bool useTitleRow[] = {true, true, true, false};
594 std::vector<std::string> files;
595 std::string content;
596 std::string fileLocation;
597 std::string source;
598 std::string subModuleName;
599 std::string readout;
600 for (int i = 0; i < paramCount; i++) {
601 printTitle(titles[i]);
602 if (useTitleRow[i]) {
603 printf("%s\n", titleRowVal[i]);
604 }
605 getFilesInDir(directories[i], &files);
606 for (auto &file : files) {
607 fileLocation = std::string(directories[i]) + std::string(file);
608 if (!android::base::ReadFileToString(fileLocation, &content)) {
609 continue;
610 }
611 readout = android::base::Trim(content);
612 subModuleName = std::string(file);
613 subModuleName.erase(subModuleName.find(paramSuffix[i]), eraseCnt[i]);
614 if (useTitleRow[i]) {
615 printf("%s \t%s\n", subModuleName.c_str(), readout.c_str());
616 } else {
617 printf("%s=%s\n", subModuleName.c_str(), readout.c_str());
618 }
619 }
620 }
621}
622void dumpIrqDurationCounts() {
623 const char *title = "IRQ Duration Counts";
624 const char *colNames = "Source\t\t\t\tlt_5ms_cnt\tbt_5ms_to_10ms_cnt\tgt_10ms_cnt\tCode"
625 "\tCurrent Threshold (uA)\tCurrent Reading (uA)\n";
626 const int nonOdpmChannelCnt = 9;
627 const int odpmChCnt = 12;
628 enum Duration {
629 LT_5MS,
630 BT_5MS_10MS,
631 GT_10MS,
632 DUR_MAX,
633 };
634 const char *irqDurDirectories[] = {
635 "/sys/devices/virtual/pmic/mitigation/irq_dur_cnt/less_than_5ms_count",
636 "/sys/devices/virtual/pmic/mitigation/irq_dur_cnt/between_5ms_to_10ms_count",
637 "/sys/devices/virtual/pmic/mitigation/irq_dur_cnt/greater_than_10ms_count",
638 };
639 enum PowerWarn {
640 MAIN,
641 SUB,
642 PWRWARN_MAX,
643 };
644 const char *pwrwarnDirectories[] = {
645 "/sys/devices/virtual/pmic/mitigation/main_pwrwarn/",
646 "/sys/devices/virtual/pmic/mitigation/sub_pwrwarn/",
647 };
648 const char *lpfCurrentDirs[] = {
649 "/sys/devices/platform/acpm_mfd_bus@15500000/i2c-1/1-001f/s2mpg14-meter/"
650 "s2mpg14-odpm/iio:device1/lpf_current",
651 "/sys/devices/platform/acpm_mfd_bus@15510000/i2c-0/0-002f/s2mpg15-meter/"
652 "s2mpg15-odpm/iio:device0/lpf_current",
653 };
654 bool titlesInitialized = false;
655 std::vector<std::string> channelNames;
656 std::vector<std::string> channelData[DUR_MAX];
657 std::vector<std::string> pwrwarnThreshold[PWRWARN_MAX];
658 std::vector<std::string> pwrwarnCode[PWRWARN_MAX];
659 std::vector<std::string> lpfCurrentVals[PWRWARN_MAX];
660 std::vector<std::string> files;
661 std::string content;
662 std::string token;
663 std::string tokenCh;
664 std::string fileLocation;
665 for (int i = 0; i < DUR_MAX; i++) {
666 if (!android::base::ReadFileToString(irqDurDirectories[i], &content)) {
667 return;
668 }
669 std::istringstream tokenStream(content);
670 while (std::getline(tokenStream, token, '\n')) {
671 if (!titlesInitialized) {
672 tokenCh = token;
673 tokenCh.erase(tokenCh.find(':'), tokenCh.length());
674 channelNames.push_back(tokenCh);
675 }
676 // there is a space after the ':' which needs to be removed
677 token.erase(0, token.find(':') + 1);
678 channelData[i].push_back(token);
679 }
680 if (!titlesInitialized)
681 titlesInitialized = true;
682 }
683 for (int i = 0; i < PWRWARN_MAX; i++) {
684 getFilesInDir(pwrwarnDirectories[i], &files);
685 for (auto &file : files) {
686 fileLocation = std::string(pwrwarnDirectories[i]) + std::string(file);
687 if (!android::base::ReadFileToString(fileLocation, &content)) {
688 continue;
689 }
690 std::string readout;
691 readout = android::base::Trim(content);
692 std::string readoutThreshold = readout;
693 readoutThreshold.erase(0, readoutThreshold.find('=') + 1);
694 std::string readoutCode = readout;
695 readoutCode.erase(readoutCode.find('='), readoutCode.length());
696 pwrwarnThreshold[i].push_back(readoutThreshold);
697 pwrwarnCode[i].push_back(readoutCode);
698 }
699 }
700 for (int i = 0; i < PWRWARN_MAX; i++) {
701 if (!android::base::ReadFileToString(lpfCurrentDirs[i], &content)) {
702 continue;
703 }
704 std::istringstream tokenStream(content);
705 bool first = true;
706 while (std::getline(tokenStream, token, '\n')) {
707 token.erase(0, token.find(' '));
708 if (first) {
709 first = false;
710 continue;
711 }
712 lpfCurrentVals[i].push_back(token);
713 }
714 }
715 printTitle(title);
716 printf("%s", colNames);
717 for (uint i = 0; i < channelNames.size(); i++) {
718 std::string code = "";
719 std::string threshold = "";
720 std::string current = "";
721 std::string ltDataMsg = "";
722 std::string btDataMsg = "";
723 std::string gtDataMsg = "";
724 int pmicSel = 0;
725 int offset = 0;
726 std::string channelNameSuffix = " \t";
727 if (i >= nonOdpmChannelCnt) {
728 offset = nonOdpmChannelCnt;
729 if (i >= (odpmChCnt + nonOdpmChannelCnt)) {
730 pmicSel = 1;
731 offset = odpmChCnt + nonOdpmChannelCnt;
732 }
733 channelNameSuffix = "";
734 code = pwrwarnCode[pmicSel][i - offset];
735 threshold = pwrwarnThreshold[pmicSel][i - offset];
736 current = lpfCurrentVals[pmicSel][i - offset];
737 }
738 if (i < channelData[0].size())
739 ltDataMsg = channelData[0][i];
740 if (i < channelData[1].size())
741 btDataMsg = channelData[1][i];
742 if (i < channelData[2].size())
743 gtDataMsg = channelData[2][i];
744 std::string adjustedChannelName = channelNames[i] + channelNameSuffix;
745 printf("%s \t%s\t\t%s\t\t\t%s\t\t%s \t%s \t\t%s\n",
746 adjustedChannelName.c_str(),
747 ltDataMsg.c_str(),
748 btDataMsg.c_str(),
749 gtDataMsg.c_str(),
750 code.c_str(),
751 threshold.c_str(),
752 current.c_str());
753 }
754}
755int main() {
756 dumpPowerStatsTimes();
757 dumpAcpmStats();
758 dumpPowerSupplyStats();
759 dumpMaxFg();
760 dumpPowerSupplyDock();
761 dumpLogBufferTcpm();
762 dumpTcpc();
763 dumpPdEngine();
samou6c250222024-07-16 07:40:36 +0000764 dumpBatteryHealth();
765 dumpBatteryDefend();
Spade Lee7bbc59e2024-10-29 16:07:29 +0000766 dumpChg();
767 dumpChgUserDebug();
samou6c250222024-07-16 07:40:36 +0000768 dumpBatteryEeprom();
769 dumpChargerStats();
770 dumpWlcLogs();
771 dumpGvoteables();
772 dumpMitigation();
773 dumpMitigationStats();
774 dumpMitigationDirs();
775 dumpIrqDurationCounts();
776}