• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <map>
21 
22 #include "securec.h"
23 #include "sensors_errors.h"
24 
25 #undef LOG_TAG
26 #define LOG_TAG "MiscdeviceDump"
27 
28 namespace OHOS {
29 namespace Sensors {
30 namespace {
31 constexpr uint32_t MAX_DUMP_RECORD_SIZE = 30;
32 constexpr uint32_t BASE_YEAR = 1900;
33 constexpr uint32_t BASE_MON = 1;
34 constexpr int32_t MAX_DUMP_PARAMETERS = 32;
35 constexpr int32_t CONVERSION_RATE = 1000;
36 }  // namespace
37 
38 static std::map<int32_t, std::string> usageMap_ = {
39     {USAGE_UNKNOWN, "unknown"},
40     {USAGE_ALARM, "alarm"},
41     {USAGE_RING, "ring"},
42     {USAGE_NOTIFICATION, "notification"},
43     {USAGE_COMMUNICATION, "communication"},
44     {USAGE_TOUCH, "touch"},
45     {USAGE_MEDIA, "media"},
46     {USAGE_PHYSICAL_FEEDBACK, "physicalFeedback"},
47     {USAGE_SIMULATE_REALITY, "simulateReality"},
48 };
49 
MiscdeviceDump()50 MiscdeviceDump::MiscdeviceDump() {}
51 
~MiscdeviceDump()52 MiscdeviceDump::~MiscdeviceDump() {}
53 
ParseCommand(int32_t fd,const std::vector<std::string> & args)54 void MiscdeviceDump::ParseCommand(int32_t fd, const std::vector<std::string> &args)
55 {
56     int32_t count = 0;
57     for (const auto &str : args) {
58         if (str.find("--") == 0) {
59             ++count;
60             continue;
61         }
62         if (str.find("-") == 0) {
63             count += static_cast<int32_t>(str.size()) - 1;
64             continue;
65         }
66     }
67     if (count > MAX_DUMP_PARAMETERS) {
68         MISC_HILOGE("Cmd param number not more than 32");
69         dprintf(fd, "Cmd param number not more than 32\n");
70         return;
71     }
72     int32_t optionIndex = 0;
73     char **argv = new (std::nothrow) char *[args.size()];
74     CHKPV(argv);
75     if (memset_s(argv, args.size() * sizeof(char *), 0, args.size() * sizeof(char *)) != EOK) {
76         MISC_HILOGE("Call memset_s failed");
77         delete[] argv;
78         return;
79     }
80     for (size_t i = 0; i < args.size(); ++i) {
81         argv[i] = new (std::nothrow) char[args[i].size() + 1];
82         if (argv[i] == nullptr) {
83             MISC_HILOGE("Alloc failure");
84             goto RELEASE_RES;
85         }
86         if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) {
87             MISC_HILOGE("strcpy_s error");
88             goto RELEASE_RES;
89         }
90     }
91     RunVibratorDump(fd, optionIndex, args, argv);
92     RELEASE_RES:
93     for (size_t i = 0; i < args.size(); ++i) {
94         if (argv[i] != nullptr) {
95             delete[] argv[i];
96         }
97     }
98     delete[] argv;
99 }
100 
RunVibratorDump(int32_t fd,int32_t optionIndex,const std::vector<std::string> & args,char ** argv)101 void MiscdeviceDump::RunVibratorDump(int32_t fd, int32_t optionIndex, const std::vector<std::string> &args, char **argv)
102 {
103     struct option dumpOptions[] = {
104         {"record", no_argument, 0, 'r'},
105         {"help", no_argument, 0, 'h'},
106         {NULL, 0, 0, 0}
107     };
108     optind = 1;
109     int32_t c;
110     while ((c = getopt_long(args.size(), argv, "rh", dumpOptions, &optionIndex)) != -1) {
111         switch (c) {
112             case 'r': {
113                 DumpMiscdeviceRecord(fd);
114                 break;
115             }
116             case 'h': {
117                 DumpHelp(fd);
118                 break;
119             }
120             default: {
121                 dprintf(fd, "Unrecognized option, More info with: \"hidumper -s 3602 -a -h\"\n");
122                 break;
123             }
124         }
125     }
126 }
127 
DumpHelp(int32_t fd)128 void MiscdeviceDump::DumpHelp(int32_t fd)
129 {
130     dprintf(fd, "Usage:\n");
131     dprintf(fd, "      -h, --help: dump help\n");
132     dprintf(fd, "      -r, --record: dump the list of vibrate recorded\n");
133 }
134 
DumpMiscdeviceRecord(int32_t fd)135 void MiscdeviceDump::DumpMiscdeviceRecord(int32_t fd)
136 {
137     std::lock_guard<std::mutex> queueLock(recordQueueMutex_);
138     if (dumpQueue_.empty()) {
139         MISC_HILOGW("dumpQueue_ is empty");
140         return;
141     }
142     size_t length = dumpQueue_.size() > MAX_DUMP_RECORD_SIZE ? MAX_DUMP_RECORD_SIZE : dumpQueue_.size();
143     for (size_t i = 0; i < length; ++i) {
144         auto record = dumpQueue_.front();
145         dumpQueue_.push(record);
146         dumpQueue_.pop();
147         VibrateInfo info = record.info;
148         if (info.mode == "time") {
149             dprintf(fd, "startTime:%s | uid:%d | pid:%d | packageName:%s | duration:%d | usage:%s\n",
150                 record.startTime.c_str(), info.uid, info.pid, info.packageName.c_str(),
151                 info.duration, GetUsageName(info.usage).c_str());
152         } else if (info.mode == "preset") {
153             dprintf(fd, "startTime:%s | uid:%d | pid:%d | packageName:%s | effect:%s | count:%d | usage:%s\n",
154                 record.startTime.c_str(), info.uid, info.pid, info.packageName.c_str(),
155                 info.effect.c_str(), info.count, GetUsageName(info.usage).c_str());
156         } else {
157             dprintf(fd, "startTime:%s | uid:%d | pid:%d | packageName:%s | usage:%s\n",
158                 record.startTime.c_str(), info.uid, info.pid, info.packageName.c_str(),
159                 GetUsageName(info.usage).c_str());
160         }
161     }
162 }
163 
DumpCurrentTime(std::string & startTime)164 void MiscdeviceDump::DumpCurrentTime(std::string &startTime)
165 {
166     timespec curTime;
167     clock_gettime(CLOCK_REALTIME, &curTime);
168     struct tm *timeinfo = localtime(&(curTime.tv_sec));
169     CHKPV(timeinfo);
170     startTime.append(std::to_string(timeinfo->tm_year + BASE_YEAR)).append("-")
171         .append(std::to_string(timeinfo->tm_mon + BASE_MON)).append("-").append(std::to_string(timeinfo->tm_mday))
172         .append(" ").append(std::to_string(timeinfo->tm_hour)).append(":").append(std::to_string(timeinfo->tm_min))
173         .append(":").append(std::to_string(timeinfo->tm_sec)).append(".")
174         .append(std::to_string(curTime.tv_nsec / (CONVERSION_RATE * CONVERSION_RATE)));
175 }
176 
UpdateRecordQueue(const VibrateRecord & record)177 void MiscdeviceDump::UpdateRecordQueue(const VibrateRecord &record)
178 {
179     std::lock_guard<std::mutex> queueLock(recordQueueMutex_);
180     dumpQueue_.push(record);
181     if (dumpQueue_.size() > MAX_DUMP_RECORD_SIZE) {
182         dumpQueue_.pop();
183     }
184 }
185 
SaveVibrateRecord(const VibrateInfo & vibrateInfo)186 void MiscdeviceDump::SaveVibrateRecord(const VibrateInfo &vibrateInfo)
187 {
188     VibrateRecord record;
189     record.info = vibrateInfo;
190     DumpCurrentTime(record.startTime);
191     UpdateRecordQueue(record);
192 }
193 
GetUsageName(int32_t usage)194 std::string MiscdeviceDump::GetUsageName(int32_t usage)
195 {
196     auto it = usageMap_.find(usage);
197     if (it == usageMap_.end()) {
198         MISC_HILOGE("Usage:%{public}d is invalid", usage);
199         return {};
200     }
201     return it->second;
202 }
203 }  // namespace Sensors
204 }  // namespace OHOS
205