blob: 7906a1f58b851e543ab6eb2d2b102378671b1a2f [file] [log] [blame]
Adam Shihd0f53602023-02-18 15:26:21 +08001/*
2 * Copyright 2022 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 */
Adam Shihd0f53602023-02-18 15:26:21 +080016#include <android-base/file.h>
Cheng Chang2c8ec7e2024-10-16 04:39:03 +000017#include <android-base/properties.h>
18#include <dirent.h>
19#include <dump/pixel_dump.h>
Edwin Tunge8884c92024-11-19 17:51:47 +080020#include <string.h>
Cheng Chang2c8ec7e2024-10-16 04:39:03 +000021#include <sys/stat.h>
22#include <unistd.h>
Adam Shihd0f53602023-02-18 15:26:21 +080023
24#define GPS_LOG_NUMBER_PROPERTY "persist.vendor.gps.aol.log_num"
25#define GPS_LOG_DIRECTORY "/data/vendor/gps/logs"
Cheng Chang2c8ec7e2024-10-16 04:39:03 +000026#define GPS_RESOURCE_DIRECTORY "/data/vendor/gps/resource"
Adam Shihd0f53602023-02-18 15:26:21 +080027#define GPS_TMP_LOG_DIRECTORY "/data/vendor/gps/logs/.tmp"
28#define GPS_LOG_PREFIX "gl-"
29#define GPS_MCU_LOG_PREFIX "esw-"
Cheng Chang0e67f942023-03-28 05:00:40 +000030#define GPS_MALLOC_LOG_DIRECTORY "/data/vendor/gps"
31#define GPS_MALLOC_LOG_PREFIX "malloc_"
Cheng Chang498c4cb2023-06-26 03:49:09 +000032#define GPS_VENDOR_CHIP_INFO "/data/vendor/gps/chip.info"
Edwin Tung42f1c4d2023-07-12 17:02:08 +080033#define GPS_RAWLOG_PREFIX "rawbin"
34#define GPS_MEMDUMP_LOG_PREFIX "memdump_"
Adam Shihd0f53602023-02-18 15:26:21 +080035
Cheng Chang2c8ec7e2024-10-16 04:39:03 +000036static void copyDirectory(const std::string &source,
37 const std::string &outputDir) {
38 DIR *dir = opendir(source.c_str());
39 if (dir == nullptr) {
40 return;
41 }
42
43 if (mkdir(outputDir.c_str(), 0777) == -1) {
44 closedir(dir);
45 return;
46 }
47
48 struct dirent *entry;
49 while ((entry = readdir(dir)) != nullptr) {
50 std::string entryName = entry->d_name;
51 if (entryName == "." || entryName == "..") {
52 continue;
53 }
54
55 std::string sourcePath = source + "/" + entryName;
56 std::string destPath = outputDir + "/" + entryName;
57
58 struct stat st;
59 if (stat(sourcePath.c_str(), &st) == 0) {
60 if (S_ISDIR(st.st_mode))
61 copyDirectory(sourcePath, destPath);
62 else
63 copyFile(sourcePath.c_str(), destPath.c_str());
64 }
65 }
66 closedir(dir);
67 return;
68}
69
Edwin Tunge8884c92024-11-19 17:51:47 +080070int compareFileExtensions(const struct dirent **a, const struct dirent **b) {
71 int num_a, num_b;
72 sscanf((*a)->d_name, "rawbinlog.out.%d", &num_a);
73 sscanf((*b)->d_name, "rawbinlog.out.%d", &num_b);
74
75 return num_a - num_b;
76}
77
78void dumpLogsAscending(const char* SrcDir, const char* DestDir, int limit, const char* prefix) {
79
80 struct dirent **dirent_list = NULL;
81 int num_entries = scandir(SrcDir, &dirent_list, 0, (int (*)(const struct dirent **, const struct dirent **)) alphasort);
82 if (!dirent_list) {
83 printf("Unable to scan dir: %s.\n", SrcDir);
84 return;
85 } else if (num_entries <= 0) {
86 printf("No file is found.\n");
87 return;
88 }
89
90 if (access(DestDir, R_OK)) {
91 printf("Unable to find folder: %s\n", DestDir);
92 return;
93 }
94
95 qsort(dirent_list, num_entries, sizeof(struct dirent *), (int (*)(const void *, const void *)) compareFileExtensions);
96
97 int copiedFiles = 0;
98
99 for (int i = 0 ; i < num_entries; i++) {
100
101 if (0 != strncmp(dirent_list[i]->d_name, prefix, strlen(prefix))) {
102 continue;
103 }
104
105 if ((copiedFiles >= limit) && (limit != -1)) {
106 printf("Skipped %s\n", dirent_list[i]->d_name);
107 continue;
108 }
109
110 copiedFiles++;
111 copyFile(concatenatePath(SrcDir, dirent_list[i]->d_name).c_str(), concatenatePath(DestDir, dirent_list[i]->d_name).c_str());
112 }
113
114 while (num_entries--) {
115 free(dirent_list[num_entries]);
116 }
117
118 free(dirent_list);
119 return;
120}
121
Adam Shihd0f53602023-02-18 15:26:21 +0800122int main() {
123 if(!::android::base::GetBoolProperty("vendor.gps.aol.enabled", false)) {
124 printf("vendor.gps.aol.enabled is false. gps logging is not running.\n");
125 return 0;
126 }
127 int maxFileNum = ::android::base::GetIntProperty(GPS_LOG_NUMBER_PROPERTY, 20);
128 std::string outputDir = concatenatePath(BUGREPORT_PACKING_DIR, "gps");
129 if (mkdir(outputDir.c_str(), 0777) == -1) {
130 printf("Unable to create folder: %s\n", outputDir.c_str());
131 return 0;
132 }
133
134 dumpLogs(GPS_TMP_LOG_DIRECTORY, outputDir.c_str(), 1, GPS_LOG_PREFIX);
135 dumpLogs(GPS_LOG_DIRECTORY, outputDir.c_str(), 3, GPS_MCU_LOG_PREFIX);
136 dumpLogs(GPS_LOG_DIRECTORY, outputDir.c_str(), maxFileNum, GPS_LOG_PREFIX);
Cheng Chang0e67f942023-03-28 05:00:40 +0000137 dumpLogs(GPS_MALLOC_LOG_DIRECTORY, outputDir.c_str(), 1, GPS_MALLOC_LOG_PREFIX);
Cheng Chang498c4cb2023-06-26 03:49:09 +0000138 if (access(GPS_VENDOR_CHIP_INFO, F_OK) == 0) {
139 copyFile(GPS_VENDOR_CHIP_INFO, concatenatePath(outputDir.c_str(), "chip.info").c_str());
140 }
Edwin Tunge8884c92024-11-19 17:51:47 +0800141 dumpLogsAscending(GPS_LOG_DIRECTORY, outputDir.c_str(), 5, GPS_RAWLOG_PREFIX);
Edwin Tung42f1c4d2023-07-12 17:02:08 +0800142 dumpLogs(GPS_LOG_DIRECTORY, outputDir.c_str(), 18, GPS_MEMDUMP_LOG_PREFIX);
Cheng Chang2c8ec7e2024-10-16 04:39:03 +0000143 copyDirectory(GPS_RESOURCE_DIRECTORY, concatenatePath(outputDir.c_str(), "resource"));
Adam Shihd0f53602023-02-18 15:26:21 +0800144 return 0;
145}