• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdarg>
17 #include <cstring>
18 #include <iostream>
19 #include <securec.h>
20 #include <sstream>
21 #include <vector>
22 
23 #include "wifi_device.h"
24 #include "wifi_scan.h"
25 
26 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
27 
28 using namespace std;
29 
30 namespace OHOS {
31 namespace Wifi {
32 std::unique_ptr<WifiDevice> ptrWifiDevice = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
33 std::unique_ptr<WifiScan> ptrWifiScan = WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
34 
35 const int MAX_ARGS = 16;
36 const int BAND_2GHZ = 1;
37 const int BAND_5GHZ = 2;
38 const int CMD_IDX = 1;
39 const int ARG_IDX = 2;
40 const int MIN_WPA_LENGTH = 8;
41 
42 struct sta_cli_cmd {
43     const char *cmd;
44     void (*handler)(int argc, const char* argv[]);
45     const char *usage;
46 };
47 
Log(const char * fmt,...)48 static void Log(const char *fmt, ...)
49 {
50     va_list list;
51     va_start(list, fmt);
52     (void)vfprintf(stdout, fmt, list);
53     va_end(list);
54     fflush(stdout);
55 }
56 
57 #define Logd(fmt, ...) Log(fmt"\n", ##__VA_ARGS__)
58 
PrintLinkedInfo(const WifiLinkedInfo & linkedInfo)59 static void PrintLinkedInfo(const WifiLinkedInfo &linkedInfo)
60 {
61     std::stringstream ss;
62     ss << "Linked info details:" << endl;
63     ss << "  ssid:" << linkedInfo.ssid << endl;
64     ss << "  bssid:" << linkedInfo.bssid << endl;
65     ss << "  rssi:" << linkedInfo.rssi << endl;
66     ss << "  band:" << linkedInfo.band << endl;
67     ss << "  frequency:" << linkedInfo.frequency << endl;
68     ss << "  linkSpeed:" << linkedInfo.linkSpeed << endl;
69     ss << "  macAddress:" << linkedInfo.macAddress << endl;
70     ss << "  ipAddress:" << linkedInfo.ipAddress << endl;
71     ss << "  connState:" << static_cast<int>(linkedInfo.connState) << endl;
72     ss << "  ifHiddenSSID:" << linkedInfo.ifHiddenSSID << endl;
73     Logd("%s", ss.str().c_str());
74 }
75 
PrintIpInfo(IpInfo & ipInfo)76 static void PrintIpInfo(IpInfo &ipInfo)
77 {
78     std::stringstream ss;
79     ss << "IP information:" << endl;
80     ss << " ipAddress:" << ipInfo.ipAddress << endl;
81     ss << " gateway:" << ipInfo.gateway << endl;
82     ss << " netmask:" << ipInfo.netmask << endl;
83     ss << " primaryDns:" << ipInfo.primaryDns << endl;
84     ss << " secondDns:" << ipInfo.secondDns << endl;
85     Logd("%s", ss.str().c_str());
86 }
87 
PrintfDeviceConfigs(vector<WifiDeviceConfig> & configs)88 static void PrintfDeviceConfigs(vector<WifiDeviceConfig> &configs)
89 {
90     int idx = 0;
91     std::stringstream ss;
92     for (WifiDeviceConfig &config : configs) {
93         ss << "No. " << idx << "  network id:" << config.networkId << endl;
94         ss << " ssid:" << config.ssid << endl;
95         ss << " bssid:" << config.bssid << endl;
96         ss << " keyMgmt:" << config.keyMgmt << endl;
97         ss << " status:" << config.status << endl;
98         idx++;
99     }
100     Logd("%s", ss.str().c_str());
101 }
102 
PrintfScanResults(vector<WifiScanInfo> & scanInfos)103 static void PrintfScanResults(vector<WifiScanInfo> &scanInfos)
104 {
105     Logd("%s total size:%u", __func__, scanInfos.size());
106     int idx = 0;
107     std::stringstream ss;
108     for (WifiScanInfo &scanInfo : scanInfos) {
109         ss << "No. " << idx << "  ssid:" << scanInfo.ssid << endl;
110         ss << " bssid:" << scanInfo.bssid << endl;
111         ss << " frequency:" << scanInfo.frequency << endl;
112         ss << " rssi:" << scanInfo.rssi << endl;
113         ss << " securityType:" << static_cast<int>(scanInfo.securityType) << endl;
114         idx++;
115     }
116     Logd("%s", ss.str().c_str());
117 }
118 
119 class WifiDeviceEventCallback : public IWifiDeviceCallBack {
120 public:
WifiDeviceEventCallback()121     WifiDeviceEventCallback()
122     {
123     }
124 
~WifiDeviceEventCallback()125     virtual ~WifiDeviceEventCallback()
126     {
127     }
128 
OnWifiStateChanged(int state)129     void OnWifiStateChanged(int state) override
130     {
131         Logd("receive %s event, state:%d", __func__, state);
132     }
133 
OnWifiConnectionChanged(int state,const WifiLinkedInfo & info)134     void OnWifiConnectionChanged(int state, const WifiLinkedInfo &info) override
135     {
136         Logd("receive %s event, state:%d", __func__, state);
137         PrintLinkedInfo(info);
138     }
139 
OnWifiRssiChanged(int rssi)140     void OnWifiRssiChanged(int rssi) override
141     {
142         Logd("receive %s event, rssi:%d", __func__, rssi);
143     }
144 
OnWifiWpsStateChanged(int state,const std::string & pinCode)145     void OnWifiWpsStateChanged(int state, const std::string &pinCode) override
146     {
147         Logd("receive %s event, state:%d, pinCode:%s", __func__, state, pinCode.c_str());
148     }
149 
OnStreamChanged(int direction)150     void OnStreamChanged(int direction) override
151     {
152         Logd("receive %s event, direction:%d", __func__, direction);
153     }
154 
OnDeviceConfigChanged(ConfigChange value)155     void OnDeviceConfigChanged(ConfigChange value) override
156     {
157         Logd("receive %s event:%d", __func__, static_cast<int>(value));
158     }
159 
160 #ifndef OHOS_ARCH_LITE
AsObject()161     OHOS::sptr<OHOS::IRemoteObject> AsObject() override
162     {
163         return nullptr;
164     }
165 #endif
166 };
167 
168 class WifiScanEventCallback : public IWifiScanCallback {
169 public:
WifiScanEventCallback()170     WifiScanEventCallback()
171     {
172     }
173 
~WifiScanEventCallback()174     virtual ~WifiScanEventCallback()
175     {
176     }
177 
OnWifiScanStateChanged(int state)178     void OnWifiScanStateChanged(int state) override
179     {
180         Logd("receive %s event, state:%d", __func__, state);
181     }
182 
183 #ifndef OHOS_ARCH_LITE
AsObject()184     OHOS::sptr<OHOS::IRemoteObject> AsObject() override
185     {
186         return nullptr;
187     }
188 #endif
189 };
190 
191 #ifdef OHOS_ARCH_LITE
192 static std::shared_ptr<WifiDeviceEventCallback> deviceCallback = std::make_shared<WifiDeviceEventCallback>();
193 static std::shared_ptr<WifiScanEventCallback> scanCallback = std::make_shared<WifiScanEventCallback>();
194 #else
195 static sptr<WifiDeviceEventCallback> deviceCallback =
196     sptr<WifiDeviceEventCallback>(new (std::nothrow)WifiDeviceEventCallback());
197 static sptr<WifiScanEventCallback> scanCallback =
198     sptr<WifiScanEventCallback>(new (std::nothrow)WifiScanEventCallback());
199 #endif
200 
RegisterDeviceEvents(void)201 static void RegisterDeviceEvents(void)
202 {
203     if (ptrWifiDevice != nullptr) {
204         ErrCode ret = ptrWifiDevice->RegisterCallBack(deviceCallback);
205         if (ret == WIFI_OPT_SUCCESS) {
206             Logd("%s success", __func__);
207         } else {
208             Logd("%s failed", __func__);
209         }
210     }
211 }
212 
RegisterScanEvents(void)213 static void RegisterScanEvents(void)
214 {
215     if (ptrWifiScan != nullptr) {
216         ErrCode ret = ptrWifiScan->RegisterCallBack(scanCallback);
217         if (ret == WIFI_OPT_SUCCESS) {
218             Logd("%s success", __func__);
219         } else {
220             Logd("%s failed", __func__);
221         }
222     }
223 }
224 
225 static void HelpCommand(const char *command);
GetNetworkId(int argc,const char * argv[],int & nid)226 static bool GetNetworkId(int argc, const char *argv[], int &nid)
227 {
228     nid = -1;
229     for (int i = ARG_IDX; i < argc; i++) {
230         if (strncmp(argv[i], "nid=", strlen("nid=")) == 0) {
231             (void)sscanf_s(argv[i], "nid=%d", &nid);
232         }
233     }
234     if (nid < 0) {
235         HelpCommand(argv[CMD_IDX]);
236         return false;
237     }
238     return true;
239 }
240 
GetDeviceConfig(int argc,const char * argv[],WifiDeviceConfig & config)241 static bool GetDeviceConfig(int argc, const char *argv[], WifiDeviceConfig &config)
242 {
243     int phase2 = 0;
244     string keyMgmt = "";
245     config.ssid = "";
246     config.preSharedKey = "";
247     config.wifiEapConfig.eap = "";
248     config.wifiEapConfig.identity = "";
249 
250     for (int i = ARG_IDX; i < argc; i++) {
251         if (strncmp(argv[i], "ssid=", strlen("ssid=")) == 0) {
252             config.ssid = argv[i] + strlen("ssid=");
253         } else if (strncmp(argv[i], "pwd=", strlen("pwd=")) == 0) {
254             config.preSharedKey = argv[i] + strlen("pwd=");
255         } else if (strncmp(argv[i], "key_mgmt=", strlen("key_mgmt=")) == 0) {
256             keyMgmt = argv[i] + strlen("key_mgmt=");
257         } else if (strncmp(argv[i], "id=", strlen("id=")) == 0) {
258             config.wifiEapConfig.identity = argv[i] + strlen("id=");
259         } else if (strncmp(argv[i], "phase2=", strlen("phase2=")) == 0) {
260             (void)sscanf_s(argv[i], "phase2=%d", &phase2);
261         } else if (strncmp(argv[i], "eapmethod=", strlen("eapmethod=")) == 0) {
262             config.wifiEapConfig.eap = argv[i] + strlen("eapmethod=");
263         }
264     }
265     if (config.ssid == "" || keyMgmt == "") {
266         HelpCommand(argv[CMD_IDX]);
267         return false;
268     }
269     if (keyMgmt != "open" && keyMgmt != "wpa" && keyMgmt != "wpa2" && keyMgmt != "eap") {
270         Logd("key_mgmt should be one of {open, wpa, wpa2, eap}");
271         return false;
272     }
273     if ((keyMgmt != "open" && keyMgmt != "eap") && config.preSharedKey.length() < MIN_WPA_LENGTH) {
274         Logd("password length should be >= %d", MIN_WPA_LENGTH);
275         return false;
276     }
277     if (keyMgmt == "open") {
278         config.keyMgmt = KEY_MGMT_NONE;
279     } else if (keyMgmt == "eap") {
280         config.keyMgmt = KEY_MGMT_EAP;
281         if (config.wifiEapConfig.eap == EAP_METHOD_PEAP) {
282             config.wifiEapConfig.phase2Method = Phase2Method(phase2);
283             config.wifiEapConfig.password = config.preSharedKey;
284             config.preSharedKey = "";
285         } else {
286             Logd("EapMethod %s unsupported", config.wifiEapConfig.eap.c_str());
287         }
288     } else {
289         config.keyMgmt = KEY_MGMT_WPA_PSK;
290     }
291     return true;
292 }
293 
HandleEnable(int argc,const char * argv[])294 static void HandleEnable(int argc, const char* argv[])
295 {
296     Logd("enter command handler:%s", argv[CMD_IDX]);
297     if (ptrWifiDevice != nullptr) {
298         ErrCode ret = ptrWifiDevice->EnableWifi();
299         if (ret == WIFI_OPT_SUCCESS) {
300             Logd("%s success", __func__);
301         } else {
302             Logd("%s failed", __func__);
303         }
304     }
305 }
306 
HandleDisable(int argc,const char * argv[])307 static void HandleDisable(int argc, const char* argv[])
308 {
309     Logd("enter command handler:%s", argv[CMD_IDX]);
310     if (ptrWifiDevice != nullptr) {
311         ErrCode ret = ptrWifiDevice->DisableWifi();
312         if (ret == WIFI_OPT_SUCCESS) {
313             Logd("%s success", __func__);
314         } else {
315             Logd("%s failed", __func__);
316         }
317     }
318 }
319 
HandleScan(int argc,const char * argv[])320 static void HandleScan(int argc, const char* argv[])
321 {
322     Logd("enter command handler:%s", argv[CMD_IDX]);
323     if (ptrWifiScan != nullptr) {
324         ErrCode ret = ptrWifiScan->Scan();
325         if (ret == WIFI_OPT_SUCCESS) {
326             Logd("%s success", __func__);
327         } else {
328             Logd("%s failed", __func__);
329         }
330     }
331 }
332 
HandleDisconnect(int argc,const char * argv[])333 static void HandleDisconnect(int argc, const char* argv[])
334 {
335     Logd("enter command handler:%s", argv[CMD_IDX]);
336     if (ptrWifiDevice != nullptr) {
337         ErrCode ret = ptrWifiDevice->Disconnect();
338         if (ret == WIFI_OPT_SUCCESS) {
339             Logd("%s success", __func__);
340         } else {
341             Logd("%s failed", __func__);
342         }
343     }
344 }
345 
HandleGetStatus(int argc,const char * argv[])346 static void HandleGetStatus(int argc, const char* argv[])
347 {
348     Logd("enter command handler:%s", argv[CMD_IDX]);
349     if (ptrWifiDevice == nullptr) {
350         return;
351     }
352 
353     bool active;
354     ErrCode ret = ptrWifiDevice->IsWifiActive(active);
355     if (ret != WIFI_OPT_SUCCESS) {
356         Logd("IsWifiActive failed");
357         return;
358     }
359     if (!active) {
360         Logd("wifi is disabled");
361         return;
362     }
363     Logd("wifi is enabled");
364 #ifndef OHOS_ARCH_LITE
365     bool connected = false;
366     ret = ptrWifiDevice->IsConnected(connected);
367     if (ret != WIFI_OPT_SUCCESS || !connected) {
368         Logd("wifi is disconnected");
369         return;
370     }
371     Logd("wifi is connected");
372 #endif
373     WifiLinkedInfo linkedInfo;
374     ret = ptrWifiDevice->GetLinkedInfo(linkedInfo);
375     if (ret != WIFI_OPT_SUCCESS) {
376         Logd("GetLinkedInfo failed");
377         return;
378     }
379     PrintLinkedInfo(linkedInfo);
380     if (linkedInfo.connState != ConnState::CONNECTED) {
381         return;
382     }
383 
384     IpInfo ipInfo;
385     ret = ptrWifiDevice->GetIpInfo(ipInfo);
386     if (ret != WIFI_OPT_SUCCESS) {
387         Logd("GetIpInfo failed");
388         return;
389     }
390     PrintIpInfo(ipInfo);
391 }
392 
HandleGetConfigList(int argc,const char * argv[])393 static void HandleGetConfigList(int argc, const char* argv[])
394 {
395     Logd("enter command handler:%s", argv[CMD_IDX]);
396     if (ptrWifiDevice != nullptr) {
397         vector<WifiDeviceConfig> configs;
398         bool isCandidate = false;
399         ErrCode ret = ptrWifiDevice->GetDeviceConfigs(configs, isCandidate);
400         if (ret == WIFI_OPT_SUCCESS) {
401             Logd("%s success", __func__);
402             PrintfDeviceConfigs(configs);
403         } else {
404             Logd("%s failed", __func__);
405         }
406     }
407 }
408 
HandleGetScanResults(int argc,const char * argv[])409 static void HandleGetScanResults(int argc, const char* argv[])
410 {
411     Logd("enter command handler:%s", argv[CMD_IDX]);
412     if (ptrWifiScan != nullptr) {
413         vector<WifiScanInfo> result;
414         ErrCode ret = ptrWifiScan->GetScanInfoList(result);
415         if (ret == WIFI_OPT_SUCCESS) {
416             Logd("%s success", __func__);
417             PrintfScanResults(result);
418         } else {
419             Logd("%s failed", __func__);
420         }
421     }
422 }
423 
HandleUpdateConfig(int argc,const char * argv[])424 static void HandleUpdateConfig(int argc, const char* argv[])
425 {
426     Logd("enter command handler:%s", argv[CMD_IDX]);
427     int nid;
428     WifiDeviceConfig config;
429     if (!GetDeviceConfig(argc, argv, config) || !GetNetworkId(argc, argv, nid)) {
430         return;
431     }
432     if (ptrWifiDevice != nullptr) {
433         int updatedId;
434         ErrCode ret = ptrWifiDevice->UpdateDeviceConfig(config, updatedId);
435         if (ret == WIFI_OPT_SUCCESS) {
436             Logd("%s success, nid=%d", __func__, updatedId);
437         } else {
438             Logd("%s failed", __func__);
439         }
440     }
441 }
442 
HandleAddConfig(int argc,const char * argv[])443 static void HandleAddConfig(int argc, const char* argv[])
444 {
445     Logd("enter command handler:%s", argv[CMD_IDX]);
446     WifiDeviceConfig config;
447     if (!GetDeviceConfig(argc, argv, config)) {
448         return;
449     }
450     if (ptrWifiDevice != nullptr) {
451         int nid;
452         bool isCandidate = false;
453         ErrCode ret = ptrWifiDevice->AddDeviceConfig(config, nid, isCandidate);
454         if (ret == WIFI_OPT_SUCCESS) {
455             Logd("%s success, nid=%d", __func__, nid);
456         } else {
457             Logd("%s failed", __func__);
458         }
459     }
460 }
461 
HandleRemoveConfigs(int argc,const char * argv[])462 static void HandleRemoveConfigs(int argc, const char* argv[])
463 {
464     Logd("enter command handler:%s", argv[CMD_IDX]);
465     if (ptrWifiDevice != nullptr) {
466         ErrCode ret = ptrWifiDevice->RemoveAllDevice();
467         if (ret == WIFI_OPT_SUCCESS) {
468             Logd("%s success", __func__);
469         } else {
470             Logd("%s failed", __func__);
471         }
472     }
473 }
474 
HandleRemoveConfig(int argc,const char * argv[])475 static void HandleRemoveConfig(int argc, const char* argv[])
476 {
477     Logd("enter command handler:%s", argv[CMD_IDX]);
478     int nid;
479     if (!GetNetworkId(argc, argv, nid)) {
480         return;
481     }
482     if (ptrWifiDevice != nullptr) {
483         ErrCode ret = ptrWifiDevice->RemoveDevice(nid);
484         if (ret == WIFI_OPT_SUCCESS) {
485             Logd("%s success, nid=%d", __func__, nid);
486         } else {
487             Logd("%s failed", __func__);
488         }
489     }
490 }
491 
HandleConnectNetwork(int argc,const char * argv[])492 static void HandleConnectNetwork(int argc, const char* argv[])
493 {
494     Logd("enter command handler:%s", argv[CMD_IDX]);
495     int nid;
496     if (!GetNetworkId(argc, argv, nid)) {
497         return;
498     }
499     if (ptrWifiDevice != nullptr) {
500         bool isCandidate = false;
501         ErrCode ret = ptrWifiDevice->ConnectToNetwork(nid, isCandidate);
502         if (ret == WIFI_OPT_SUCCESS) {
503             Logd("%s success, nid=%d", __func__, nid);
504         } else {
505             Logd("%s failed", __func__);
506         }
507     }
508 }
509 
HandleConnectDevice(int argc,const char * argv[])510 static void HandleConnectDevice(int argc, const char* argv[])
511 {
512     Logd("enter command handler:%s", argv[CMD_IDX]);
513     WifiDeviceConfig config;
514     if (!GetDeviceConfig(argc, argv, config)) {
515         return;
516     }
517     if (ptrWifiDevice != nullptr) {
518         ErrCode ret = ptrWifiDevice->ConnectToDevice(config);
519         if (ret == WIFI_OPT_SUCCESS) {
520             Logd("%s success", __func__);
521         } else {
522             Logd("%s failed", __func__);
523         }
524     }
525 }
526 
HandleGetWifiState(int argc,const char * argv[])527 static void HandleGetWifiState(int argc, const char* argv[])
528 {
529     Logd("enter command handler:%s", argv[CMD_IDX]);
530     if (ptrWifiDevice != nullptr) {
531         int state;
532         ErrCode ret = ptrWifiDevice->GetWifiState(state);
533         if (ret == WIFI_OPT_SUCCESS) {
534             Logd("%s success, wifi state:%d", __func__, state);
535         } else {
536             Logd("%s failed", __func__);
537         }
538     }
539 }
540 
HandleGetCountry(int argc,const char * argv[])541 static void HandleGetCountry(int argc, const char* argv[])
542 {
543     Logd("enter command handler:%s", argv[CMD_IDX]);
544     if (ptrWifiDevice != nullptr) {
545         string countryCode;
546         ErrCode ret = ptrWifiDevice->GetCountryCode(countryCode);
547         if (ret == WIFI_OPT_SUCCESS) {
548             Logd("%s success, countryCode: %s", __func__, countryCode.c_str());
549         } else {
550             Logd("%s failed", __func__);
551         }
552     }
553 }
554 
HandleSetCountry(int argc,const char * argv[])555 static void HandleSetCountry(int argc, const char* argv[])
556 {
557     Logd("enter command handler:%s", argv[CMD_IDX]);
558     string code = "";
559     for (int i = ARG_IDX; i < argc; i++) {
560         if (strncmp(argv[i], "code=", strlen("code=")) == 0) {
561             code = argv[i] + strlen("code=");
562         }
563     }
564     if (code == "") {
565         HelpCommand(argv[CMD_IDX]);
566         return;
567     }
568     if (ptrWifiDevice != nullptr) {
569         ErrCode ret = ptrWifiDevice->SetCountryCode(code);
570         if (ret == WIFI_OPT_SUCCESS) {
571             Logd("%s success, code=%s", __func__, code.c_str());
572         } else {
573             Logd("%s failed", __func__);
574         }
575     }
576 }
577 
HandleGetSignalLevel(int argc,const char * argv[])578 static void HandleGetSignalLevel(int argc, const char* argv[])
579 {
580     Logd("enter command handler:%s", argv[CMD_IDX]);
581     int rssi = 0;
582     string band = "";
583     for (int i = ARG_IDX; i < argc; i++) {
584         if (strncmp(argv[i], "rssi=", strlen("rssi=")) == 0) {
585             (void)sscanf_s(argv[i], "rssi=%d", &rssi);
586         } else if (strncmp(argv[i], "band=", strlen("band=")) == 0) {
587             band = argv[i] + strlen("band=");
588         }
589     }
590     if (rssi >= 0 || (band != "2g" && band != "5g")) {
591         HelpCommand(argv[CMD_IDX]);
592         return;
593     }
594     if (ptrWifiDevice != nullptr) {
595         int level;
596         int bandType = (band == "2g" ? BAND_2GHZ : BAND_5GHZ);
597         ErrCode ret = ptrWifiDevice->GetSignalLevel(rssi, bandType, level);
598         if (ret == WIFI_OPT_SUCCESS) {
599             Logd("%s success, level=%d", __func__, level);
600         } else {
601             Logd("%s failed", __func__);
602         }
603     }
604 }
605 
HandleGetSupportedFeatures(int argc,const char * argv[])606 static void HandleGetSupportedFeatures(int argc, const char* argv[])
607 {
608     Logd("enter command handler:%s", argv[CMD_IDX]);
609     if (ptrWifiDevice != nullptr) {
610         long features = 0L;
611         ErrCode ret = ptrWifiDevice->GetSupportedFeatures(features);
612         if (ret == WIFI_OPT_SUCCESS) {
613             Logd("%s success, features=0x%lx", __func__, features);
614         } else {
615             Logd("%s failed", __func__);
616         }
617     }
618 }
619 
HandleEnableConfig(int argc,const char * argv[])620 static void HandleEnableConfig(int argc, const char* argv[])
621 {
622     Logd("enter command handler:%s", argv[CMD_IDX]);
623     int nid = -1;
624     string disableOthers = "";
625     for (int i = ARG_IDX; i < argc; i++) {
626         if (strncmp(argv[i], "nid=", strlen("nid=")) == 0) {
627             (void)sscanf_s(argv[i], "nid=%d", &nid);
628         } else if (strncmp(argv[i], "disableothers=", strlen("disableothers=")) == 0) {
629             disableOthers = argv[i] + strlen("disableothers=");
630         }
631     }
632     if (nid < 0 || (disableOthers != "true" && disableOthers != "false")) {
633         HelpCommand(argv[CMD_IDX]);
634         return;
635     }
636     if (ptrWifiDevice != nullptr) {
637         ErrCode ret = ptrWifiDevice->EnableDeviceConfig(nid, disableOthers == "true");
638         if (ret == WIFI_OPT_SUCCESS) {
639             Logd("%s success, nid=%d", __func__, nid);
640         } else {
641             Logd("%s failed", __func__);
642         }
643     }
644 }
645 
HandleDisableConfig(int argc,const char * argv[])646 static void HandleDisableConfig(int argc, const char* argv[])
647 {
648     Logd("enter command handler:%s", argv[CMD_IDX]);
649     int nid;
650     if (!GetNetworkId(argc, argv, nid)) {
651         return;
652     }
653     if (ptrWifiDevice != nullptr) {
654         ErrCode ret = ptrWifiDevice->DisableDeviceConfig(nid);
655         if (ret == WIFI_OPT_SUCCESS) {
656             Logd("%s success, nid=%d", __func__, nid);
657         } else {
658             Logd("%s failed", __func__);
659         }
660     }
661 }
662 
HandleReconnect(int argc,const char * argv[])663 static void HandleReconnect(int argc, const char* argv[])
664 {
665     Logd("enter command handler:%s", argv[CMD_IDX]);
666     if (ptrWifiDevice != nullptr) {
667         ErrCode ret = ptrWifiDevice->ReConnect();
668         if (ret == WIFI_OPT_SUCCESS) {
669             Logd("%s success", __func__);
670         } else {
671             Logd("%s failed", __func__);
672         }
673     }
674 }
675 
HandleReassociate(int argc,const char * argv[])676 static void HandleReassociate(int argc, const char* argv[])
677 {
678     Logd("enter command handler:%s", argv[CMD_IDX]);
679     if (ptrWifiDevice != nullptr) {
680         ErrCode ret = ptrWifiDevice->ReAssociate();
681         if (ret == WIFI_OPT_SUCCESS) {
682             Logd("%s success", __func__);
683         } else {
684             Logd("%s failed", __func__);
685         }
686     }
687 }
688 
HandleGetDeviceMac(int argc,const char * argv[])689 static void HandleGetDeviceMac(int argc, const char* argv[])
690 {
691     Logd("enter command handler:%s", argv[CMD_IDX]);
692     if (ptrWifiDevice != nullptr) {
693         string mac;
694         ErrCode ret = ptrWifiDevice->GetDeviceMacAddress(mac);
695         if (ret == WIFI_OPT_SUCCESS) {
696             Logd("%s success, mac is %s:", __func__, mac.c_str());
697         } else {
698             Logd("%s failed", __func__);
699         }
700     }
701 }
702 
703 static void ParseUserInput(std::string command);
HandleInteractive(int argc,const char * argv[])704 static void HandleInteractive(int argc, const char* argv[])
705 {
706     string inputLine;
707     Logd("enter interactive mode! input quit to exit this mode");
708     Logd("please input command:");
709     RegisterDeviceEvents();
710     RegisterScanEvents();
711 
712     do {
713         getline(cin, inputLine);
714         if (inputLine == "quit") {
715             break;
716         } else {
717             ParseUserInput(inputLine);
718             Logd("please input command:");
719         }
720     } while (true);
721 }
722 
723 static const struct sta_cli_cmd g_sta_cli_cmds[] = {
724     {"enable", HandleEnable, "enable"},
725     {"disable", HandleDisable, "disable"},
726     {"scan", HandleScan, "scan"},
727     {"disconnect", HandleDisconnect, "disconnect nid=%d"},
728     {"get_status", HandleGetStatus, "get_status"},
729     {"get_config_list", HandleGetConfigList, "get_config_list"},
730     {"get_scan_results", HandleGetScanResults, "get_scan_results"},
731     {"update_config", HandleUpdateConfig, "update_config nid=%d ssid=%s pwd=%s key_mgmt=open/wpa/wpa2"},
732     {"add_config", HandleAddConfig, "add_config ssid=%s pwd=%s key_mgmt=open/wpa/wpa2"},
733     {"remove_configs", HandleRemoveConfigs, "remove_configs"},
734     {"remove_config", HandleRemoveConfig, "remove_config nid=%d"},
735     {"connect_network", HandleConnectNetwork, "connect_network nid=%d"},
736     {"connect_device", HandleConnectDevice, "connect_device ssid=%s pwd=%s key_mgmt=open/wpa/wpa2"},
737     {"get_wifi_state", HandleGetWifiState, "get_wifi_state"},
738     {"set_country", HandleSetCountry, "set_country code=%s"},
739     {"get_country", HandleGetCountry, "get_country"},
740     {"get_signal_level", HandleGetSignalLevel, "get_signal_level rssi=%d band=2g/5g"},
741     {"get_supported_features", HandleGetSupportedFeatures, "get_supported_features"},
742     {"enable_config", HandleEnableConfig, "enable_config nid=%d disableothers=true/false"},
743     {"disable_config", HandleDisableConfig, "disable_config nid=%d"},
744     {"reconnect", HandleReconnect, "reconnect"},
745     {"reassociate", HandleReassociate, "reassociate"},
746     {"get_device_mac", HandleGetDeviceMac, "get_device_mac"},
747     {"interactive", HandleInteractive, "interactive"}
748 };
749 
HelpCommand(const char * command)750 static void HelpCommand(const char *command)
751 {
752     int count = ARRAY_SIZE(g_sta_cli_cmds);
753     for (int i = 0; i < count; i++) {
754         if (strcmp(command, g_sta_cli_cmds[i].cmd) == 0) {
755             Logd("%s", g_sta_cli_cmds[i].usage);
756             return;
757         }
758     }
759     Logd("can not find command %s", command);
760 }
761 
Help(void)762 static void Help(void)
763 {
764     Logd("%s", "support command as follows:");
765     int count = ARRAY_SIZE(g_sta_cli_cmds);
766     for (int i = 0; i < count; i++) {
767         Logd("%s", g_sta_cli_cmds[i].usage);
768     }
769 }
770 
HandleUserCommand(int argc,const char * argv[])771 static void HandleUserCommand(int argc, const char *argv[])
772 {
773     if (argc < ARG_IDX) {
774         Help();
775         return;
776     }
777 
778     int count = ARRAY_SIZE(g_sta_cli_cmds);
779     for (int i = 0; i < count; i++) {
780         if (strcmp(g_sta_cli_cmds[i].cmd, argv[CMD_IDX]) == 0) {
781             if (g_sta_cli_cmds[i].handler != nullptr) {
782                 g_sta_cli_cmds[i].handler(argc, argv);
783             } else {
784                 Logd("no handler for command:%s", g_sta_cli_cmds[i].cmd);
785             }
786             return;
787         }
788     }
789     Help();
790 }
791 
ParseUserInput(std::string command)792 static void ParseUserInput(std::string command)
793 {
794     int argc = 0;
795     const char* argv[MAX_ARGS] = { nullptr };
796     vector<string> cmdArgs;
797 
798     std::istringstream istr(command);
799     for (std::string s; istr >> s;) {
800         cmdArgs.push_back(s);
801     }
802 
803     argc = cmdArgs.size() + CMD_IDX;
804     if (argc > MAX_ARGS) {
805         argc = MAX_ARGS;
806     }
807     for (int i = CMD_IDX; i < argc; i++) {
808         argv[i] = cmdArgs[i - CMD_IDX].c_str();
809     }
810     HandleUserCommand(argc, argv);
811     cmdArgs.clear();
812 }
813 }
814 }
815 
main(int argc,char * argv[])816 int main(int argc, char *argv[])
817 {
818     OHOS::Wifi::HandleUserCommand(argc, const_cast<const char **>(argv));
819     return 0;
820 }
821