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 DHLOGI("DcameraSinkHidumper Dump args.size():%d.", args.size());
53 result.clear();
54 int32_t argsSize = static_cast<int32_t>(args.size());
55
56 if (args.empty()) {
57 ShowHelp(result);
58 return true;
59 } else if (args.size() > 1) {
60 ShowIllegalInfomation(result);
61 return true;
62 }
63
64 for (int32_t i = 0; i < argsSize; i++) {
65 DHLOGI("DcameraSinkHidumper Dump args[%d]: %s.", i, args.at(i).c_str());
66 }
67
68 if (ProcessDump(args[0], result) != DCAMERA_OK) {
69 return false;
70 }
71 return true;
72 }
73
ProcessDump(const std::string & args,std::string & result)74 int32_t DcameraSinkHidumper::ProcessDump(const std::string& args, std::string& result)
75 {
76 DHLOGI("ProcessDump Dump.");
77 HidumpFlag hf = HidumpFlag::UNKNOWN;
78 auto operatorIter = ARGS_MAP.find(args);
79 if (operatorIter != ARGS_MAP.end()) {
80 hf = operatorIter->second;
81 }
82
83 if (hf == HidumpFlag::GET_HELP) {
84 ShowHelp(result);
85 return DCAMERA_OK;
86 }
87 result.clear();
88 SetSinkDumpInfo(camDumpInfo_);
89 int32_t ret = DCAMERA_BAD_VALUE;
90 switch (hf) {
91 case HidumpFlag::GET_CAMERA_INFO: {
92 ret = GetLocalCameraNumber(result);
93 break;
94 }
95 case HidumpFlag::GET_OPENED_INFO: {
96 ret = GetOpenedCameraInfo(result);
97 break;
98 }
99 case HidumpFlag::GET_VERSION_INFO: {
100 ret = GetVersionInfo(result);
101 break;
102 }
103 case HidumpFlag::START_DUMP: {
104 ret = DcameraHidumper::GetInstance().StartDump();
105 result.append("Send dump order ok\n");
106 break;
107 }
108 case HidumpFlag::STOP_DUMP: {
109 ret = DcameraHidumper::GetInstance().StopDump();
110 result.append("Send stop dump order ok\n");
111 break;
112 }
113 default: {
114 ret = ShowIllegalInfomation(result);
115 break;
116 }
117 }
118
119 return ret;
120 }
121
GetLocalCameraNumber(std::string & result)122 int32_t DcameraSinkHidumper::GetLocalCameraNumber(std::string& result)
123 {
124 DHLOGI("GetLocalCameraNumber Dump.");
125 result.append("CameraNumber: ")
126 .append(std::to_string(camDumpInfo_.camNumber));
127 return DCAMERA_OK;
128 }
129
GetOpenedCameraInfo(std::string & result)130 int32_t DcameraSinkHidumper::GetOpenedCameraInfo(std::string& result)
131 {
132 DHLOGI("GetOpenedCameraInfo Dump.");
133 result.append("OpenedCamera:\n");
134 std::vector<std::string> camIds = camDumpInfo_.camIds;
135 for (size_t i = 0; i < camIds.size(); i++) {
136 result.append(camIds[i]);
137 result.append("\n");
138 }
139 return DCAMERA_OK;
140 }
141
GetVersionInfo(std::string & result)142 int32_t DcameraSinkHidumper::GetVersionInfo(std::string& result)
143 {
144 DHLOGI("GetVersionInfo Dump.");
145 result.append("CameraVersion: ")
146 .append(camDumpInfo_.version);
147 return DCAMERA_OK;
148 }
149
ShowHelp(std::string & result)150 void DcameraSinkHidumper::ShowHelp(std::string& result)
151 {
152 DHLOGI("ShowHelp Dump.");
153 result.append("Usage:dump <command> [options]\n")
154 .append("Description:\n")
155 .append("-h ")
156 .append(": show help\n")
157 .append("--version ")
158 .append(": dump camera version in the system\n")
159 .append("--camNum ")
160 .append(": dump local camera numbers in the system\n")
161 .append("--opened ")
162 .append(": dump the opened camera in the system\n")
163 .append("--startdump ")
164 .append(": dump camera data in /data/data/dcamera\n")
165 .append("--stopdump ")
166 .append(": stop dump camera data\n");
167 }
168
ShowIllegalInfomation(std::string & result)169 int32_t DcameraSinkHidumper::ShowIllegalInfomation(std::string& result)
170 {
171 DHLOGI("ShowIllegalInfomation Dump.");
172 result.append("unknown command, -h for help.");
173 return DCAMERA_OK;
174 }
175 } // namespace DistributedHardware
176 } // namespace OHOS