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