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 "dcamera_sink_hidumper.h"
17
18 #include "dcamera_hidumper.h"
19 #include "distributed_camera_errno.h"
20 #include "distributed_camera_sink_service.h"
21 #include "distributed_hardware_log.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 IMPLEMENT_SINGLE_INSTANCE(DcameraSinkHidumper);
26
27 namespace {
28 const std::string ARGS_HELP = "-h";
29 const std::string ARGS_VERSION_INFO = "--version";
30 const std::string ARGS_CAMERA_INFO = "--camNum";
31 const std::string ARGS_START_DUMP = "--startdump";
32 const std::string ARGS_STOP_DUMP = "--stopdump";
33 const std::string ARGS_OPENED_INFO = "--opened";
34
35 const std::map<std::string, HidumpFlag> ARGS_MAP = {
36 { ARGS_HELP, HidumpFlag::GET_HELP },
37 { ARGS_CAMERA_INFO, HidumpFlag::GET_CAMERA_INFO },
38 { ARGS_OPENED_INFO, HidumpFlag::GET_OPENED_INFO },
39 { ARGS_VERSION_INFO, HidumpFlag::GET_VERSION_INFO },
40 { ARGS_START_DUMP, HidumpFlag::START_DUMP },
41 { ARGS_STOP_DUMP, HidumpFlag::STOP_DUMP },
42 };
43 }
44
SetSinkDumpInfo(CameraDumpInfo & camDumpInfo_)45 void DcameraSinkHidumper::SetSinkDumpInfo(CameraDumpInfo& camDumpInfo_)
46 {
47 DistributedCameraSinkService::GetCamDumpInfo(camDumpInfo_);
48 }
49
Dump(const std::vector<std::string> & args,std::string & result)50 bool DcameraSinkHidumper::Dump(const std::vector<std::string>& args, std::string& result)
51 {
52 result.clear();
53 int32_t argsSize = static_cast<int32_t>(args.size());
54 if (argsSize > DUMP_MAX_SIZE) {
55 DHLOGE("DcameraSinkHidumper Dump args.size() is invalid");
56 return false;
57 }
58 DHLOGI("DcameraSinkHidumper Dump args.size():%{public}d.", argsSize);
59 if (args.empty()) {
60 ShowHelp(result);
61 return true;
62 } else if (args.size() > 1) {
63 ShowIllegalInfomation(result);
64 return true;
65 }
66
67 for (int32_t i = 0; i < argsSize; i++) {
68 DHLOGI("DcameraSinkHidumper Dump args[%{public}d]: %{public}s.", i, args.at(i).c_str());
69 }
70
71 if (ProcessDump(args[0], result) != DCAMERA_OK) {
72 return false;
73 }
74 return true;
75 }
76
ProcessDump(const std::string & args,std::string & result)77 int32_t DcameraSinkHidumper::ProcessDump(const std::string& args, std::string& result)
78 {
79 DHLOGI("ProcessDump Dump.");
80 HidumpFlag hf = HidumpFlag::UNKNOWN;
81 auto operatorIter = ARGS_MAP.find(args);
82 if (operatorIter != ARGS_MAP.end()) {
83 hf = operatorIter->second;
84 }
85
86 if (hf == HidumpFlag::GET_HELP) {
87 ShowHelp(result);
88 return DCAMERA_OK;
89 }
90 result.clear();
91 SetSinkDumpInfo(camDumpInfo_);
92 int32_t ret = DCAMERA_BAD_VALUE;
93 switch (hf) {
94 case HidumpFlag::GET_CAMERA_INFO: {
95 ret = GetLocalCameraNumber(result);
96 break;
97 }
98 case HidumpFlag::GET_OPENED_INFO: {
99 ret = GetOpenedCameraInfo(result);
100 break;
101 }
102 case HidumpFlag::GET_VERSION_INFO: {
103 ret = GetVersionInfo(result);
104 break;
105 }
106 case HidumpFlag::START_DUMP: {
107 ret = DcameraHidumper::GetInstance().StartDump();
108 result.append("Send dump order ok\n");
109 break;
110 }
111 case HidumpFlag::STOP_DUMP: {
112 ret = DcameraHidumper::GetInstance().StopDump();
113 result.append("Send stop dump order ok\n");
114 break;
115 }
116 default: {
117 ret = ShowIllegalInfomation(result);
118 break;
119 }
120 }
121
122 return ret;
123 }
124
GetLocalCameraNumber(std::string & result)125 int32_t DcameraSinkHidumper::GetLocalCameraNumber(std::string& result)
126 {
127 DHLOGI("GetLocalCameraNumber Dump.");
128 result.append("CameraNumber: ")
129 .append(std::to_string(camDumpInfo_.camNumber));
130 return DCAMERA_OK;
131 }
132
GetOpenedCameraInfo(std::string & result)133 int32_t DcameraSinkHidumper::GetOpenedCameraInfo(std::string& result)
134 {
135 DHLOGI("GetOpenedCameraInfo Dump.");
136 result.append("OpenedCamera:\n");
137 std::vector<std::string> camIds = camDumpInfo_.camIds;
138 for (size_t i = 0; i < camIds.size(); i++) {
139 result.append(camIds[i]);
140 result.append("\n");
141 }
142 return DCAMERA_OK;
143 }
144
GetVersionInfo(std::string & result)145 int32_t DcameraSinkHidumper::GetVersionInfo(std::string& result)
146 {
147 DHLOGI("GetVersionInfo Dump.");
148 result.append("CameraVersion: ")
149 .append(camDumpInfo_.version);
150 return DCAMERA_OK;
151 }
152
ShowHelp(std::string & result)153 void DcameraSinkHidumper::ShowHelp(std::string& result)
154 {
155 DHLOGI("ShowHelp Dump.");
156 result.append("Usage:dump <command> [options]\n")
157 .append("Description:\n")
158 .append("-h ")
159 .append(": show help\n")
160 .append("--version ")
161 .append(": dump camera version in the system\n")
162 .append("--camNum ")
163 .append(": dump local camera numbers in the system\n")
164 .append("--opened ")
165 .append(": dump the opened camera in the system\n")
166 .append("--startdump ")
167 .append(": dump camera data in /data/data/dcamera\n")
168 .append("--stopdump ")
169 .append(": stop dump camera data\n");
170 }
171
ShowIllegalInfomation(std::string & result)172 int32_t DcameraSinkHidumper::ShowIllegalInfomation(std::string& result)
173 {
174 DHLOGI("ShowIllegalInfomation Dump.");
175 result.append("unknown command, -h for help.");
176 return DCAMERA_OK;
177 }
178 } // namespace DistributedHardware
179 } // namespace OHOS