• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <arpa/inet.h>
17 
18 #include "wifi_utils.h"
19 
20 using std::unique_ptr;
21 using namespace OHOS::Wifi;
22 
23 const static int WIFI_SA_ID = 1125;
24 const static int TWO_SECOND = 2;
25 const static int FIVE_SECOND = 5;
26 static const char* def_ssid = "OpenHarmony_Private_Net_01";
27 static unique_ptr<WifiDevice> wifiDevicePtr = WifiDevice::GetInstance(WIFI_SA_ID);
28 
EnableWifi()29 int WiFiUtils::EnableWifi()
30 {
31     bool isActive = false;
32     int ret = wifiDevicePtr->IsWifiActive(isActive);
33     if (ret != SOFTBUS_OK) {
34         LOG("[wifi]call IsWifiActive fail, ret:%d", ret);
35     }
36     if (!isActive) {
37         LOG("[wifi]not active, call enable...");
38         ret = wifiDevicePtr->EnableWifi();
39         if (ret != SOFTBUS_OK) {
40             LOG("[wifi]call EnableWifi fail, ret:%d", ret);
41             return SOFTBUS_ERR;
42         }
43 
44         LOG("[wifi]call EnableWifi success, wait state active");
45         int timeout = 10;
46         do {
47             ret = wifiDevicePtr->IsWifiActive(isActive);
48             if (ret != SOFTBUS_OK) {
49                 LOG("[wifi]call IsWifiActive fail, ret:%d", ret);
50             }
51             if (isActive) {
52                 LOG("[wifi]is active, timeout:%d", timeout);
53                 break;
54             }
55             sleep(FIVE_SECOND);
56             timeout--;
57         } while (timeout > 0);
58 
59         if (timeout <= 0) {
60             LOG("[wifi]wait wifi state fail[timeout]");
61             return SOFTBUS_ERR;
62         } else {
63             LOG("[wifi]wait wifi state success");
64             return SOFTBUS_OK;
65         }
66     } else {
67         LOG("[wifi]is active, do nothing");
68         return SOFTBUS_OK;
69     }
70 }
71 
DisableWifi()72 int WiFiUtils::DisableWifi()
73 {
74     bool isActive = false;
75     int ret = wifiDevicePtr->IsWifiActive(isActive);
76     if (ret != SOFTBUS_OK) {
77         LOG("[wifi]call IsWifiActive fail, ret:%d", ret);
78     }
79     if (isActive) {
80         LOG("[wifi]is active, call disable...");
81         ret = wifiDevicePtr->DisableWifi();
82         if (ret != SOFTBUS_OK) {
83             LOG("[wifi]call DisableWifi fail, ret:%d", ret);
84             return SOFTBUS_ERR;
85         }
86 
87         LOG("[wifi]call DisableWifi success, wait state active");
88         int timeout = 10;
89         do {
90             ret = wifiDevicePtr->IsWifiActive(isActive);
91             if (ret != SOFTBUS_OK) {
92                 LOG("[wifi]call IsWifiActive fail, ret:%d", ret);
93             }
94             if (!isActive) {
95                 LOG("[wifi]not active, timeout:%d", timeout);
96                 break;
97             }
98             sleep(1);
99             timeout--;
100         } while (timeout > 0);
101 
102         if (timeout <= 0) {
103             LOG("[wifi]wait wifi state fail[timeout]");
104             return SOFTBUS_ERR;
105         } else {
106             LOG("[wifi]wait wifi state success");
107             return SOFTBUS_OK;
108         }
109     } else {
110         LOG("[wifi]not active, do nothing");
111         return SOFTBUS_OK;
112     }
113 }
114 
DisableThenEnable(int delaySeconds)115 int WiFiUtils::DisableThenEnable(int delaySeconds)
116 {
117     int ret = DisableWifi();
118     if (ret != SOFTBUS_OK) {
119         LOG("[wifi]DisableWifi fail");
120         return ret;
121     }
122     sleep(delaySeconds);
123     ret = EnableWifi();
124     if (ret != SOFTBUS_OK) {
125         LOG("[wifi]EnableWifi fail");
126     }
127     return ret;
128 }
129 
CheckIsConnectToDefault(void)130 int WiFiUtils::CheckIsConnectToDefault(void)
131 {
132     WifiLinkedInfo linkInfo;
133     int ret = wifiDevicePtr->GetLinkedInfo(linkInfo);
134     if (ret != SOFTBUS_OK) {
135         LOG("[wifi]call GetLinkedInfo fail, ret:%d", ret);
136     } else {
137         char rstBuff[16];
138         struct in_addr inputAddr = { 0 };
139         inputAddr.s_addr = htonl(linkInfo.ipAddress);
140         inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
141         LOG("[wifi]link info,netid:%d, ssid:%s, state:%d, ip:%u,%s", linkInfo.networkId, linkInfo.ssid.c_str(),
142             linkInfo.connState, linkInfo.ipAddress, rstBuff);
143         if (strncmp(linkInfo.ssid.c_str(), def_ssid, strlen(def_ssid)) == 0) {
144             LOG("[wifi]check success");
145             return SOFTBUS_OK;
146         } else {
147             LOG("[wifi]check fail");
148         }
149     }
150     return SOFTBUS_ERR;
151 }
152 
ConnectTo(const std::string & ssid,const std::string & passwd)153 int WiFiUtils::ConnectTo(const std::string& ssid, const std::string& passwd)
154 {
155     WifiDeviceConfig deviceConfig;
156     deviceConfig.ssid = ssid;
157     deviceConfig.preSharedKey = passwd;
158     deviceConfig.keyMgmt = "WPA-PSK";
159     int netId;
160     int ret = wifiDevicePtr->AddDeviceConfig(deviceConfig, netId);
161     if (ret != SOFTBUS_OK) {
162         LOG("[wifi]call AddDeviceConfig fail, ret:%d", ret);
163     } else {
164         LOG("[wifi]call AddDeviceConfig success, netId:%d", netId);
165     }
166 
167     ret = wifiDevicePtr->ConnectToNetwork(netId);
168     if (ret != SOFTBUS_OK) {
169         LOG("[wifi]call ConnectTo fail, ret:%d", ret);
170         return SOFTBUS_ERR;
171     } else {
172         LOG("[wifi]call ConnectTo success, netId:%d", netId);
173     }
174     int timeout = 10;
175     char rstBuff[16];
176     WifiLinkedInfo linkInfo;
177     struct in_addr inputAddr = { 0 };
178     while (timeout > 0) {
179         sleep(TWO_SECOND);
180         ret = wifiDevicePtr->GetLinkedInfo(linkInfo);
181         if (ret != SOFTBUS_OK) {
182             LOG("[wifi]call GetLinkedInfo fail, ret:%d", ret);
183         } else {
184             LOG("[wifi]call GetLinkedInfo success, now state:%d", linkInfo.connState);
185             if (linkInfo.connState == ConnState::CONNECTED && linkInfo.ipAddress != 0) {
186                 inputAddr.s_addr = htonl(linkInfo.ipAddress);
187                 inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
188                 LOG("[wifi]state OK,netid:%d, ssid:%s, state:%d, ip:%u,%s", linkInfo.networkId, linkInfo.ssid.c_str(),
189                     linkInfo.connState, linkInfo.ipAddress, rstBuff);
190                 break;
191             }
192         }
193         timeout -= TWO_SECOND;
194     }
195     if (timeout == 0) {
196         inputAddr.s_addr = htonl(linkInfo.ipAddress);
197         inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
198         LOG("[wifi]state(timeout=0),netid:%d, ssid:%s, state:%d, ip:%u,%s", linkInfo.networkId, linkInfo.ssid.c_str(),
199             linkInfo.connState, linkInfo.ipAddress, rstBuff);
200     }
201     return SOFTBUS_OK;
202 }
203 
ConnectToNew(const std::string & ssid,const std::string & passwd)204 int WiFiUtils::ConnectToNew(const std::string& ssid, const std::string& passwd)
205 {
206     int ret = wifiDevicePtr->Disconnect();
207     LOG("[wifi]call Disconnect ret:%d", ret);
208     ret = ConnectTo(ssid, passwd);
209     return ret;
210 }
211 
ConnectToOpenAP(const std::string & ssid)212 int WiFiUtils::ConnectToOpenAP(const std::string& ssid)
213 {
214     WifiDeviceConfig deviceConfig;
215     deviceConfig.ssid = ssid;
216     deviceConfig.keyMgmt = "NONE";
217     int netId;
218     int ret = wifiDevicePtr->AddDeviceConfig(deviceConfig, netId);
219     if (ret != SOFTBUS_OK) {
220         LOG("[wifi]call AddDeviceConfig fail, ret:%d", ret);
221     } else {
222         LOG("[wifi]call AddDeviceConfig success, netId:%d", netId);
223     }
224 
225     ret = wifiDevicePtr->ConnectToNetwork(netId);
226     if (ret != SOFTBUS_OK) {
227         LOG("[wifi]call ConnectTo fail, ret:%d", ret);
228         return SOFTBUS_ERR;
229     } else {
230         LOG("[wifi]call ConnectTo success, netId:%d", netId);
231     }
232     sleep(FIVE_SECOND);
233     int state;
234     ret = wifiDevicePtr->GetWifiState(state);
235     LOG("[wifi]call GetWifiState ret:%d,state:%d", ret, state);
236 
237     WifiLinkedInfo linkInfo;
238     ret = wifiDevicePtr->GetLinkedInfo(linkInfo);
239     if (ret != SOFTBUS_OK) {
240         LOG("[wifi]call GetLinkedInfo fail, ret:%d", ret);
241     } else {
242         LOG("[wifi]call GetLinkedInfo success");
243         char rstBuff[16];
244         struct in_addr inputAddr = { 0 };
245         inputAddr.s_addr = htonl(linkInfo.ipAddress);
246         inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
247 
248         LOG("[wifi]link info,netid:%d, ssid:%s, state:%d, ip:%u,%s", linkInfo.networkId, linkInfo.ssid.c_str(),
249             linkInfo.connState, linkInfo.ipAddress, rstBuff);
250     }
251     return SOFTBUS_OK;
252 }
253 
EnableThenConnect(const std::string & ssid,const std::string & passwd)254 int WiFiUtils::EnableThenConnect(const std::string& ssid, const std::string& passwd)
255 {
256     int ret = EnableWifi();
257     if (ret != SOFTBUS_OK) {
258         LOG("[wifi]EnableWifi fail");
259         return ret;
260     }
261     ret = ConnectTo(ssid, passwd);
262     if (ret != SOFTBUS_OK) {
263         LOG("[wifi]ConnectTo fail");
264     }
265     return ret;
266 }
267 
DisableThenEnableAndConnect(int delaySeconds,const std::string & ssid,const std::string & passwd)268 int WiFiUtils::DisableThenEnableAndConnect(int delaySeconds, const std::string& ssid, const std::string& passwd)
269 {
270     int ret = DisableWifi();
271     if (ret != SOFTBUS_OK) {
272         LOG("[wifi]DisableWifi fail");
273         return ret;
274     }
275     sleep(delaySeconds);
276     ret = EnableWifi();
277     if (ret != SOFTBUS_OK) {
278         LOG("[wifi]EnableWifi fail");
279         return ret;
280     }
281     ret = ConnectTo(ssid, passwd);
282     if (ret != SOFTBUS_OK) {
283         LOG("[wifi]ConnectTo fail");
284     }
285     return ret;
286 }
287