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_source_hidumper.h"
17
18 #include "distributed_camera_errno.h"
19 #include "distributed_camera_source_service.h"
20 #include "distributed_hardware_log.h"
21
22 namespace OHOS {
23 namespace DistributedHardware {
24 IMPLEMENT_SINGLE_INSTANCE(DcameraSourceHidumper);
25
26 namespace {
27 const std::string ARGS_HELP = "-h";
28 const std::string ARGS_VERSION_INFO = "--version";
29 const std::string ARGS_REGISTERED_INFO = "--registered";
30 const std::string ARGS_CURRENTSTATE_INFO = "--curState";
31 const std::string STATE_INT = "Init";
32 const std::string STATE_REGISTERED = "Registered";
33 const std::string STATE_OPENED = "Opened";
34 const std::string STATE_CONFIG_STREAM = "ConfigStream";
35 const std::string STATE_CAPTURE = "Capture";
36
37 const std::map<std::string, HidumpFlag> ARGS_MAP = {
38 { ARGS_HELP, HidumpFlag::GET_HELP },
39 { ARGS_REGISTERED_INFO, HidumpFlag::GET_REGISTERED_INFO },
40 { ARGS_CURRENTSTATE_INFO, HidumpFlag::GET_CURRENTSTATE_INFO },
41 { ARGS_VERSION_INFO, HidumpFlag::GET_VERSION_INFO },
42 };
43
44 const std::map<int32_t, std::string> STATE_MAP = {
45 { DCAMERA_STATE_INIT_DUMP, STATE_INT },
46 { DCAMERA_STATE_REGIST_DUMP, STATE_REGISTERED },
47 { DCAMERA_STATE_OPENED_DUMP, STATE_OPENED },
48 { DCAMERA_STATE_CONFIG_STREAM_DUMP, STATE_CONFIG_STREAM },
49 { DCAMERA_STATE_CAPTURE_DUMP, STATE_CAPTURE },
50 };
51 }
52
SetSourceDumpInfo(CameraDumpInfo & camDumpInfo_)53 void DcameraSourceHidumper::SetSourceDumpInfo(CameraDumpInfo& camDumpInfo_)
54 {
55 DistributedCameraSourceService::GetDumpInfo(camDumpInfo_);
56 }
57
Dump(const std::vector<std::string> & args,std::string & result)58 bool DcameraSourceHidumper::Dump(const std::vector<std::string>& args, std::string& result)
59 {
60 DHLOGI("DcameraSourceHidumper Dump args.size():%d.", args.size());
61 result.clear();
62 int32_t argsSize = static_cast<int32_t>(args.size());
63
64 if (args.empty()) {
65 ShowHelp(result);
66 return true;
67 } else if (args.size() > 1) {
68 ShowIllegalInfomation(result);
69 return true;
70 }
71
72 for (int32_t i = 0; i < argsSize; i++) {
73 DHLOGI("DcameraSourceHidumper Dump args[%d]: %s.", i, args.at(i).c_str());
74 }
75
76 if (ProcessDump(args[0], result) != DCAMERA_OK) {
77 return false;
78 }
79 return true;
80 }
81
ProcessDump(const std::string & args,std::string & result)82 int32_t DcameraSourceHidumper::ProcessDump(const std::string& args, std::string& result)
83 {
84 DHLOGI("ProcessDump Dump.");
85 HidumpFlag hf = HidumpFlag::UNKNOWN;
86 auto operatorIter = ARGS_MAP.find(args);
87 if (operatorIter != ARGS_MAP.end()) {
88 hf = operatorIter->second;
89 }
90
91 if (hf == HidumpFlag::GET_HELP) {
92 ShowHelp(result);
93 return DCAMERA_OK;
94 }
95 result.clear();
96 SetSourceDumpInfo(camDumpInfo_);
97 int32_t ret = DCAMERA_BAD_VALUE;
98 switch (hf) {
99 case HidumpFlag::GET_REGISTERED_INFO: {
100 ret = GetRegisteredInfo(result);
101 break;
102 }
103 case HidumpFlag::GET_CURRENTSTATE_INFO: {
104 ret = GetCurrentStateInfo(result);
105 break;
106 }
107 case HidumpFlag::GET_VERSION_INFO: {
108 ret = GetVersionInfo(result);
109 break;
110 }
111 default: {
112 ret = ShowIllegalInfomation(result);
113 break;
114 }
115 }
116
117 return ret;
118 }
119
GetRegisteredInfo(std::string & result)120 int32_t DcameraSourceHidumper::GetRegisteredInfo(std::string& result)
121 {
122 DHLOGI("GetRegisteredInfo Dump.");
123 result.append("CameraNumber: ")
124 .append(std::to_string(camDumpInfo_.regNumber));
125 return DCAMERA_OK;
126 }
127
GetCurrentStateInfo(std::string & result)128 int32_t DcameraSourceHidumper::GetCurrentStateInfo(std::string& result)
129 {
130 DHLOGI("GetCurrentStateInfo Dump.");
131 std::map<std::string, int32_t> devState = camDumpInfo_.curState;
132 result.append("CameraId\tState\n");
133 for (auto it = devState.begin(); it != devState.end(); it++) {
134 std::string deviceId("");
135 int32_t camState = 0;
136 deviceId = it->first;
137 camState = it->second;
138 DHLOGI("GetCurrentStateInfo camState is %d.", camState);
139 auto state = STATE_MAP.find(camState);
140 std::string curState("");
141 if (state != STATE_MAP.end()) {
142 curState = state->second;
143 }
144 result.append(deviceId)
145 .append("\t")
146 .append(curState)
147 .append("\n");
148 }
149 return DCAMERA_OK;
150 }
151
GetVersionInfo(std::string & result)152 int32_t DcameraSourceHidumper::GetVersionInfo(std::string& result)
153 {
154 DHLOGI("GetVersionInfo Dump.");
155 result.append("CameraVersion: ")
156 .append(camDumpInfo_.version);
157 return DCAMERA_OK;
158 }
159
ShowHelp(std::string & result)160 void DcameraSourceHidumper::ShowHelp(std::string& result)
161 {
162 DHLOGI("ShowHelp Dump.");
163 result.append("Usage:dump <command> [options]\n")
164 .append("Description:\n")
165 .append("-h ")
166 .append(": show help\n")
167 .append("--version ")
168 .append(": dump camera version in the system\n")
169 .append("--registered ")
170 .append(": dump number of registered cameras in the system\n")
171 .append("--curState ")
172 .append(": dump current state of the camera in the system\n");
173 }
174
ShowIllegalInfomation(std::string & result)175 int32_t DcameraSourceHidumper::ShowIllegalInfomation(std::string& result)
176 {
177 DHLOGI("ShowIllegalInfomation Dump.");
178 result.append("unknown command, -h for help.");
179 return DCAMERA_OK;
180 }
181 } // namespace DistributedHardware
182 } // namespace OHOS