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