1 /*
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  */
16 #include <android-base/file.h>
17 #include <android-base/properties.h>
18 #include <dirent.h>
19 #include <dump/pixel_dump.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 
24 #define GPS_LOG_NUMBER_PROPERTY "persist.vendor.gps.aol.log_num"
25 #define GPS_LOG_DIRECTORY "/data/vendor/gps/logs"
26 #define GPS_RESOURCE_DIRECTORY "/data/vendor/gps/resource"
27 #define GPS_TMP_LOG_DIRECTORY "/data/vendor/gps/logs/.tmp"
28 #define GPS_LOG_PREFIX "gl-"
29 #define GPS_MCU_LOG_PREFIX "esw-"
30 #define GPS_MALLOC_LOG_DIRECTORY "/data/vendor/gps"
31 #define GPS_MALLOC_LOG_PREFIX "malloc_"
32 #define GPS_VENDOR_CHIP_INFO "/data/vendor/gps/chip.info"
33 #define GPS_RAWLOG_PREFIX "rawbin"
34 #define GPS_MEMDUMP_LOG_PREFIX "memdump_"
35 
copyDirectory(const std::string & source,const std::string & outputDir)36 static 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 
compareFileExtensions(const struct dirent ** a,const struct dirent ** b)70 int 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 
dumpLogsAscending(const char * SrcDir,const char * DestDir,int limit,const char * prefix)78 void 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 
main()122 int 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);
137     dumpLogs(GPS_MALLOC_LOG_DIRECTORY, outputDir.c_str(), 1, GPS_MALLOC_LOG_PREFIX);
138     if (access(GPS_VENDOR_CHIP_INFO, F_OK) == 0) {
139         copyFile(GPS_VENDOR_CHIP_INFO, concatenatePath(outputDir.c_str(), "chip.info").c_str());
140     }
141     dumpLogsAscending(GPS_LOG_DIRECTORY, outputDir.c_str(), 5, GPS_RAWLOG_PREFIX);
142     dumpLogs(GPS_LOG_DIRECTORY, outputDir.c_str(), 18, GPS_MEMDUMP_LOG_PREFIX);
143     copyDirectory(GPS_RESOURCE_DIRECTORY, concatenatePath(outputDir.c_str(), "resource"));
144     return 0;
145 }
146