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::shared_ptr;
21 using namespace OHOS::Wifi;
22
23 const static int WIFI_SA_ID = 1120;
24 const static int TWO_SECOND = 2;
25 const static int FIVE_SECOND = 5;
26 static shared_ptr<WifiDevice> wifiDevicePtr = WifiDevice::GetInstance(WIFI_SA_ID);
27
EnableWifi()28 int WiFiUtils::EnableWifi()
29 {
30 bool isActive = false;
31 int ret = wifiDevicePtr->IsWifiActive(isActive);
32 if (ret != SOFTBUS_OK) {
33 LOG("[wifi]call IsWifiActive fail, ret:%d", ret);
34 }
35 if (!isActive) {
36 LOG("[wifi]not active, call enable...");
37 ret = wifiDevicePtr->EnableWifi();
38 if (ret != SOFTBUS_OK) {
39 LOG("[wifi]call EnableWifi fail, ret:%d", ret);
40 return SOFTBUS_ERR;
41 }
42
43 LOG("[wifi]call EnableWifi success, wait state active");
44 int timeout = 10;
45 do {
46 ret = wifiDevicePtr->IsWifiActive(isActive);
47 if (ret != SOFTBUS_OK) {
48 LOG("[wifi]call IsWifiActive fail, ret:%d", ret);
49 }
50 if (isActive) {
51 LOG("[wifi]is active, timeout:%d", timeout);
52 break;
53 }
54 sleep(FIVE_SECOND);
55 timeout--;
56 } while (timeout > 0);
57
58 if (timeout <= 0) {
59 LOG("[wifi]wait wifi state fail[timeout]");
60 return SOFTBUS_ERR;
61 } else {
62 LOG("[wifi]wait wifi state success");
63 return SOFTBUS_OK;
64 }
65 } else {
66 LOG("[wifi]is active, do nothing");
67 return SOFTBUS_OK;
68 }
69 }
70
DisableWifi()71 int WiFiUtils::DisableWifi()
72 {
73 bool isActive = false;
74 int ret = wifiDevicePtr->IsWifiActive(isActive);
75 if (ret != SOFTBUS_OK) {
76 LOG("[wifi]call IsWifiActive fail, ret:%d", ret);
77 }
78 if (isActive) {
79 LOG("[wifi]is active, call disable...");
80 ret = wifiDevicePtr->DisableWifi();
81 if (ret != SOFTBUS_OK) {
82 LOG("[wifi]call DisableWifi fail, ret:%d", ret);
83 return SOFTBUS_ERR;
84 }
85
86 LOG("[wifi]call DisableWifi success, wait state active");
87 int timeout = 10;
88 do {
89 ret = wifiDevicePtr->IsWifiActive(isActive);
90 if (ret != SOFTBUS_OK) {
91 LOG("[wifi]call IsWifiActive fail, ret:%d", ret);
92 }
93 if (!isActive) {
94 LOG("[wifi]not active, timeout:%d", timeout);
95 break;
96 }
97 sleep(1);
98 timeout--;
99 } while (timeout > 0);
100
101 if (timeout <= 0) {
102 LOG("[wifi]wait wifi state fail[timeout]");
103 return SOFTBUS_ERR;
104 } else {
105 LOG("[wifi]wait wifi state success");
106 return SOFTBUS_OK;
107 }
108 } else {
109 LOG("[wifi]not active, do nothing");
110 return SOFTBUS_OK;
111 }
112 }
113
ConnectTo(const std::string & ssid,const std::string & passwd)114 int WiFiUtils::ConnectTo(const std::string& ssid, const std::string& passwd)
115 {
116 WifiDeviceConfig deviceConfig;
117 deviceConfig.ssid = ssid;
118 deviceConfig.preSharedKey = passwd;
119 deviceConfig.keyMgmt = "WPA-PSK";
120 int netId;
121 bool isCandidate = false;
122 int ret = wifiDevicePtr->AddDeviceConfig(deviceConfig, netId, isCandidate);
123 if (ret != SOFTBUS_OK) {
124 LOG("[wifi]call AddDeviceConfig fail, ret:%d", ret);
125 } else {
126 LOG("[wifi]call AddDeviceConfig success, netId:%d", netId);
127 }
128
129 ret = wifiDevicePtr->ConnectToNetwork(netId, isCandidate);
130 if (ret != SOFTBUS_OK) {
131 LOG("[wifi]call ConnectTo fail, ret:%d", ret);
132 return SOFTBUS_ERR;
133 } else {
134 LOG("[wifi]call ConnectTo success, netId:%d", netId);
135 }
136 int timeout = 10;
137 char rstBuff[16];
138 WifiLinkedInfo linkInfo;
139 struct in_addr inputAddr = { 0 };
140 while (timeout > 0) {
141 sleep(TWO_SECOND);
142 ret = wifiDevicePtr->GetLinkedInfo(linkInfo);
143 if (ret != SOFTBUS_OK) {
144 LOG("[wifi]call GetLinkedInfo fail, ret:%d", ret);
145 } else {
146 LOG("[wifi]call GetLinkedInfo success, now state:%d", linkInfo.connState);
147 if (linkInfo.connState == ConnState::CONNECTED && linkInfo.ipAddress != 0) {
148 inputAddr.s_addr = htonl(linkInfo.ipAddress);
149 inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
150 LOG("[wifi]state OK,netid:%d, ssid:%s, state:%d, ip:%u,%s", linkInfo.networkId, linkInfo.ssid.c_str(),
151 linkInfo.connState, linkInfo.ipAddress, rstBuff);
152 break;
153 }
154 }
155 timeout -= TWO_SECOND;
156 }
157 if (timeout == 0) {
158 inputAddr.s_addr = htonl(linkInfo.ipAddress);
159 inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff));
160 LOG("[wifi]state(timeout=0),netid:%d, ssid:%s, state:%d, ip:%u,%s", linkInfo.networkId, linkInfo.ssid.c_str(),
161 linkInfo.connState, linkInfo.ipAddress, rstBuff);
162 }
163 return SOFTBUS_OK;
164 }
165
ConnectToNew(const std::string & ssid,const std::string & passwd)166 int WiFiUtils::ConnectToNew(const std::string& ssid, const std::string& passwd)
167 {
168 int ret = wifiDevicePtr->Disconnect();
169 LOG("[wifi]call Disconnect ret:%d", ret);
170 ret = ConnectTo(ssid, passwd);
171 return ret;
172 }
173
EnableThenConnect(const std::string & ssid,const std::string & passwd)174 int WiFiUtils::EnableThenConnect(const std::string& ssid, const std::string& passwd)
175 {
176 int ret = EnableWifi();
177 if (ret != SOFTBUS_OK) {
178 LOG("[wifi]EnableWifi fail");
179 return ret;
180 }
181 ret = ConnectTo(ssid, passwd);
182 if (ret != SOFTBUS_OK) {
183 LOG("[wifi]ConnectTo fail");
184 }
185 return ret;
186 }
187
DisableThenEnableAndConnect(int delaySeconds,const std::string & ssid,const std::string & passwd)188 int WiFiUtils::DisableThenEnableAndConnect(int delaySeconds, const std::string& ssid, const std::string& passwd)
189 {
190 int ret = DisableWifi();
191 if (ret != SOFTBUS_OK) {
192 LOG("[wifi]DisableWifi fail");
193 return ret;
194 }
195 sleep(delaySeconds);
196 ret = EnableWifi();
197 if (ret != SOFTBUS_OK) {
198 LOG("[wifi]EnableWifi fail");
199 return ret;
200 }
201 ret = ConnectTo(ssid, passwd);
202 if (ret != SOFTBUS_OK) {
203 LOG("[wifi]ConnectTo fail");
204 }
205 return ret;
206 }
207