1 /*
2  * Copyright (C) 2021 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 "wifi_dumper.h"
17 #include <functional>
18 #include "wifi_logger.h"
19 
20 DEFINE_WIFILOG_LABEL("WifiDumper");
21 namespace OHOS {
22 namespace Wifi {
23 const std::string ARGS_HELP = "-h";
24 
PrintArgs(const std::vector<std::string> & vecArgs)25 void WifiDumper::PrintArgs(const std::vector<std::string>& vecArgs)
26 {
27     std::string strArgs;
28     for (auto& each: vecArgs) {
29         strArgs += each + "|";
30     }
31     WIFI_LOGD("Dumper[%{public}zu] args: %{public}s", vecArgs.size(), strArgs.c_str());
32 }
33 
ShowDeviceDumpUsage() const34 std::string WifiDumper::ShowDeviceDumpUsage() const
35 {
36     std::string help;
37     return help.append("WiFi device dump options:\n")
38         .append("  [-h]\n")
39         .append("  description of the cmd option:\n")
40         .append("    -h: show help.\n");
41 }
42 
DeviceDump(std::function<void (std::string &)> saBasicDumpFunc,const std::vector<std::string> & vecArgs,std::string & result)43 bool WifiDumper::DeviceDump(std::function<void(std::string&)> saBasicDumpFunc,
44     const std::vector<std::string>& vecArgs, std::string& result)
45 {
46     PrintArgs(vecArgs);
47     result.clear();
48     if (!vecArgs.empty() && vecArgs[0] == ARGS_HELP) {
49         result = ShowDeviceDumpUsage();
50         return true;
51     }
52 
53     saBasicDumpFunc(result);
54     return true;
55 }
56 
ShowScanDumpUsage() const57 std::string WifiDumper::ShowScanDumpUsage() const
58 {
59     std::string help;
60     return help.append("WiFi scan dump options:\n")
61         .append("  [-h]\n")
62         .append("  description of the cmd option:\n")
63         .append("    -h: show help.\n");
64 }
65 
ScanDump(std::function<void (std::string &)> saBasicDumpFunc,const std::vector<std::string> & vecArgs,std::string & result)66 bool WifiDumper::ScanDump(std::function<void(std::string&)> saBasicDumpFunc,
67     const std::vector<std::string>& vecArgs, std::string& result)
68 {
69     PrintArgs(vecArgs);
70     result.clear();
71     if (!vecArgs.empty() && vecArgs[0] == ARGS_HELP) {
72         result = ShowScanDumpUsage();
73         return true;
74     }
75 
76     saBasicDumpFunc(result);
77     return true;
78 }
79 
ShowP2pDumpUsage() const80 std::string WifiDumper::ShowP2pDumpUsage() const
81 {
82     std::string help;
83     return help.append("WiFi P2P dump options:\n")
84         .append("  [-h]\n")
85         .append("  description of the cmd option:\n")
86         .append("    -h: show help.\n");
87 }
88 
P2pDump(std::function<void (std::string &)> saBasicDumpFunc,const std::vector<std::string> & vecArgs,std::string & result)89 bool WifiDumper::P2pDump(std::function<void(std::string&)> saBasicDumpFunc,
90     const std::vector<std::string>& vecArgs, std::string& result)
91 {
92     PrintArgs(vecArgs);
93     result.clear();
94     if (!vecArgs.empty() && vecArgs[0] == ARGS_HELP) {
95         result = ShowP2pDumpUsage();
96         return true;
97     }
98 
99     saBasicDumpFunc(result);
100     return true;
101 }
102 
ShowHotspotDumpUsage() const103 std::string WifiDumper::ShowHotspotDumpUsage() const
104 {
105     std::string help;
106     return help.append("WiFi hotspot dump options:\n")
107         .append("  [-h]\n")
108         .append("  description of the cmd option:\n")
109         .append("    -h: show help.\n");
110 }
111 
HotspotDump(std::function<void (std::string &)> saBasicDumpFunc,const std::vector<std::string> & vecArgs,std::string & result)112 bool WifiDumper::HotspotDump(std::function<void(std::string&)> saBasicDumpFunc,
113     const std::vector<std::string>& vecArgs, std::string& result)
114 {
115     PrintArgs(vecArgs);
116     result.clear();
117     if (!vecArgs.empty() && vecArgs[0] == ARGS_HELP) {
118         result = ShowHotspotDumpUsage();
119         return true;
120     }
121 
122     saBasicDumpFunc(result);
123     return true;
124 }
125 }  // namespace Wifi
126 }  // namespace OHOS
127