• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 += 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         delete[] argv[i];
119     }
120     delete[] argv;
121 }
122 
DumpHelp(int32_t fd)123 void MiscdeviceDump::DumpHelp(int32_t fd)
124 {
125     dprintf(fd, "Usage:\n");
126     dprintf(fd, "      -h, --help: dump help\n");
127     dprintf(fd, "      -r, --record: dump the list of vibrate recorded\n");
128 }
129 
DumpMiscdeviceRecord(int32_t fd)130 void MiscdeviceDump::DumpMiscdeviceRecord(int32_t fd)
131 {
132     std::lock_guard<std::mutex> queueLock(recordQueueMutex_);
133     if (dumpQueue_.empty()) {
134         MISC_HILOGW("dumpQueue_ is empty");
135         return;
136     }
137     size_t length = dumpQueue_.size() > MAX_DUMP_RECORD_SIZE ? MAX_DUMP_RECORD_SIZE : dumpQueue_.size();
138     for (size_t i = 0; i < length; ++i) {
139         auto record = dumpQueue_.front();
140         dumpQueue_.push(record);
141         dumpQueue_.pop();
142         VibrateInfo info = record.info;
143         if (info.mode == "time") {
144             dprintf(fd, "startTime:%s | uid:%d | pid:%d | packageName:%s | duration:%d | usage:%s\n",
145                 record.startTime.c_str(), info.uid, info.pid, info.packageName.c_str(),
146                 info.duration, GetUsageName(info.usage).c_str());
147         } else {
148             dprintf(fd, "startTime:%s | uid:%d | pid:%d | packageName:%s | effect:%s | count:%d | usage:%s\n",
149                 record.startTime.c_str(), info.uid, info.pid, info.packageName.c_str(),
150                 info.effect.c_str(), info.count, GetUsageName(info.usage).c_str());
151         }
152     }
153 }
154 
DumpCurrentTime(std::string & startTime)155 void MiscdeviceDump::DumpCurrentTime(std::string &startTime)
156 {
157     timespec curTime;
158     clock_gettime(CLOCK_REALTIME, &curTime);
159     struct tm *timeinfo = localtime(&(curTime.tv_sec));
160     CHKPV(timeinfo);
161     startTime.append(std::to_string(timeinfo->tm_year + BASE_YEAR)).append("-")
162         .append(std::to_string(timeinfo->tm_mon + BASE_MON)).append("-").append(std::to_string(timeinfo->tm_mday))
163         .append(" ").append(std::to_string(timeinfo->tm_hour)).append(":").append(std::to_string(timeinfo->tm_min))
164         .append(":").append(std::to_string(timeinfo->tm_sec)).append(".")
165         .append(std::to_string(curTime.tv_nsec / (CONVERSION_RATE * CONVERSION_RATE)));
166 }
167 
UpdateRecordQueue(const VibrateRecord & record)168 void MiscdeviceDump::UpdateRecordQueue(const VibrateRecord &record)
169 {
170     std::lock_guard<std::mutex> queueLock(recordQueueMutex_);
171     dumpQueue_.push(record);
172     if (dumpQueue_.size() > MAX_DUMP_RECORD_SIZE) {
173         dumpQueue_.pop();
174     }
175 }
176 
SaveVibrateRecord(const VibrateInfo & vibrateInfo)177 void MiscdeviceDump::SaveVibrateRecord(const VibrateInfo &vibrateInfo)
178 {
179     VibrateRecord record;
180     record.info = vibrateInfo;
181     DumpCurrentTime(record.startTime);
182     UpdateRecordQueue(record);
183 }
184 
GetUsageName(int32_t usage)185 std::string MiscdeviceDump::GetUsageName(int32_t usage)
186 {
187     auto it = usageMap_.find(usage);
188     if (it == usageMap_.end()) {
189         MISC_HILOGE("usage: %{public}d is invalid", usage);
190         return {};
191     }
192     return it->second;
193 }
194 }  // namespace Sensors
195 }  // namespace OHOS
196