1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "miscdevice_dump.h"
17
18 #include <getopt.h>
19
20 #include <cinttypes>
21 #include <cstring>
22 #include <ctime>
23 #include <map>
24
25 #include "securec.h"
26 #include "sensors_errors.h"
27
28 namespace OHOS {
29 namespace Sensors {
30 using namespace OHOS::HiviewDFX;
31 namespace {
32 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "MiscdeviceDump" };
33 constexpr uint32_t MAX_DUMP_RECORD_SIZE = 30;
34 constexpr uint32_t BASE_YEAR = 1900;
35 constexpr uint32_t BASE_MON = 1;
36 constexpr int32_t MAX_DUMP_PARAMETERS = 32;
37 constexpr int32_t CONVERSION_RATE = 1000;
38 } // namespace
39
40 static std::map<int32_t, std::string> usageMap_ = {
41 {USAGE_UNKNOWN, "unknown"},
42 {USAGE_ALARM, "alarm"},
43 {USAGE_RING, "ring"},
44 {USAGE_NOTIFICATION, "notification"},
45 {USAGE_COMMUNICATION, "communication"},
46 {USAGE_TOUCH, "touch"},
47 {USAGE_MEDIA, "media"},
48 {USAGE_PHYSICAL_FEEDBACK, "physicalFeedback"},
49 {USAGE_SIMULATE_REALITY, "simulateReality"},
50 };
51
MiscdeviceDump()52 MiscdeviceDump::MiscdeviceDump() {}
53
~MiscdeviceDump()54 MiscdeviceDump::~MiscdeviceDump() {}
55
ParseCommand(int32_t fd,const std::vector<std::string> & args)56 void MiscdeviceDump::ParseCommand(int32_t fd, const std::vector<std::string>& args)
57 {
58 int32_t count = 0;
59 for (const auto &str : args) {
60 if (str.find("--") == 0) {
61 ++count;
62 continue;
63 }
64 if (str.find("-") == 0) {
65 count += static_cast<int32_t>(str.size()) - 1;
66 continue;
67 }
68 }
69 if (count > MAX_DUMP_PARAMETERS) {
70 MISC_HILOGE("Cmd param number not more than 32");
71 dprintf(fd, "cmd param number not more than 32\n");
72 return;
73 }
74 int32_t optionIndex = 0;
75 struct option dumpOptions[] = {
76 {"record", no_argument, 0, 'r'},
77 {"help", no_argument, 0, 'h'},
78 {NULL, 0, 0, 0}
79 };
80 char **argv = new (std::nothrow) char *[args.size()];
81 CHKPV(argv);
82 if (memset_s(argv, args.size() * sizeof(char *), 0, args.size() * sizeof(char *)) != EOK) {
83 MISC_HILOGE("Call memset_s failed");
84 delete[] argv;
85 return;
86 }
87 for (size_t i = 0; i < args.size(); ++i) {
88 argv[i] = new (std::nothrow) char[args[i].size() + 1];
89 if (argv[i] == nullptr) {
90 MISC_HILOGE("Alloc failure");
91 goto RELEASE_RES;
92 }
93 if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) {
94 MISC_HILOGE("strcpy_s error");
95 goto RELEASE_RES;
96 }
97 }
98 optind = 1;
99 int32_t c;
100 while ((c = getopt_long(args.size(), argv, "rh", dumpOptions, &optionIndex)) != -1) {
101 switch (c) {
102 case 'r': {
103 DumpMiscdeviceRecord(fd);
104 break;
105 }
106 case 'h': {
107 DumpHelp(fd);
108 break;
109 }
110 default: {
111 dprintf(fd, "Unrecognized option: %s\nMore info with: \"hidumper -s 3602 -a -h\"\n", argv[optind-1]);
112 break;
113 }
114 }
115 }
116 RELEASE_RES:
117 for (size_t i = 0; i < args.size(); ++i) {
118 if (argv[i] != nullptr) {
119 delete[] argv[i];
120 }
121 }
122 delete[] argv;
123 }
124
DumpHelp(int32_t fd)125 void MiscdeviceDump::DumpHelp(int32_t fd)
126 {
127 dprintf(fd, "Usage:\n");
128 dprintf(fd, " -h, --help: dump help\n");
129 dprintf(fd, " -r, --record: dump the list of vibrate recorded\n");
130 }
131
DumpMiscdeviceRecord(int32_t fd)132 void MiscdeviceDump::DumpMiscdeviceRecord(int32_t fd)
133 {
134 std::lock_guard<std::mutex> queueLock(recordQueueMutex_);
135 if (dumpQueue_.empty()) {
136 MISC_HILOGW("dumpQueue_ is empty");
137 return;
138 }
139 size_t length = dumpQueue_.size() > MAX_DUMP_RECORD_SIZE ? MAX_DUMP_RECORD_SIZE : dumpQueue_.size();
140 for (size_t i = 0; i < length; ++i) {
141 auto record = dumpQueue_.front();
142 dumpQueue_.push(record);
143 dumpQueue_.pop();
144 VibrateInfo info = record.info;
145 if (info.mode == "time") {
146 dprintf(fd, "startTime:%s | uid:%d | pid:%d | packageName:%s | duration:%d | usage:%s\n",
147 record.startTime.c_str(), info.uid, info.pid, info.packageName.c_str(),
148 info.duration, GetUsageName(info.usage).c_str());
149 } else if (info.mode == "preset") {
150 dprintf(fd, "startTime:%s | uid:%d | pid:%d | packageName:%s | effect:%s | count:%d | usage:%s\n",
151 record.startTime.c_str(), info.uid, info.pid, info.packageName.c_str(),
152 info.effect.c_str(), info.count, GetUsageName(info.usage).c_str());
153 } else {
154 dprintf(fd, "startTime:%s | uid:%d | pid:%d | packageName:%s | usage:%s\n",
155 record.startTime.c_str(), info.uid, info.pid, info.packageName.c_str(),
156 GetUsageName(info.usage).c_str());
157 }
158 }
159 }
160
DumpCurrentTime(std::string & startTime)161 void MiscdeviceDump::DumpCurrentTime(std::string &startTime)
162 {
163 timespec curTime;
164 clock_gettime(CLOCK_REALTIME, &curTime);
165 struct tm *timeinfo = localtime(&(curTime.tv_sec));
166 CHKPV(timeinfo);
167 startTime.append(std::to_string(timeinfo->tm_year + BASE_YEAR)).append("-")
168 .append(std::to_string(timeinfo->tm_mon + BASE_MON)).append("-").append(std::to_string(timeinfo->tm_mday))
169 .append(" ").append(std::to_string(timeinfo->tm_hour)).append(":").append(std::to_string(timeinfo->tm_min))
170 .append(":").append(std::to_string(timeinfo->tm_sec)).append(".")
171 .append(std::to_string(curTime.tv_nsec / (CONVERSION_RATE * CONVERSION_RATE)));
172 }
173
UpdateRecordQueue(const VibrateRecord & record)174 void MiscdeviceDump::UpdateRecordQueue(const VibrateRecord &record)
175 {
176 std::lock_guard<std::mutex> queueLock(recordQueueMutex_);
177 dumpQueue_.push(record);
178 if (dumpQueue_.size() > MAX_DUMP_RECORD_SIZE) {
179 dumpQueue_.pop();
180 }
181 }
182
SaveVibrateRecord(const VibrateInfo & vibrateInfo)183 void MiscdeviceDump::SaveVibrateRecord(const VibrateInfo &vibrateInfo)
184 {
185 VibrateRecord record;
186 record.info = vibrateInfo;
187 DumpCurrentTime(record.startTime);
188 UpdateRecordQueue(record);
189 }
190
GetUsageName(int32_t usage)191 std::string MiscdeviceDump::GetUsageName(int32_t usage)
192 {
193 auto it = usageMap_.find(usage);
194 if (it == usageMap_.end()) {
195 MISC_HILOGE("Usage:%{public}d is invalid", usage);
196 return {};
197 }
198 return it->second;
199 }
200 } // namespace Sensors
201 } // namespace OHOS
202