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_hal.h"
17 #include <stdbool.h>
18 #include "securec.h"
19 #include "unistd.h"
20 #include "hdf_log.h"
21 #include "wifi_hal_cmd.h"
22 #include "wifi_hal_common.h"
23 #include "wifi_hal_util.h"
24 #include "wifi_driver_client.h"
25
26 #ifdef __cplusplus
27 #if __cplusplus
28 extern "C" {
29 #endif
30 #endif
31
32 #define WIFI_HOST 3026
33 #define WIFI_C_HOST 3036
34 #define MAX_AUTH_NUM 1000
35 static bool g_wifiIsStarted = false;
36
StartInner(struct IWiFi * iwifi)37 static int32_t StartInner(struct IWiFi *iwifi)
38 {
39 int32_t ret;
40
41 if (iwifi == NULL) {
42 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
43 return HDF_ERR_INVALID_PARAM;
44 }
45 if (g_wifiIsStarted) {
46 HDF_LOGE("%s: wifi has started already, line: %d", __FUNCTION__, __LINE__);
47 return HDF_FAILURE;
48 }
49 ret = WifiDriverClientInit();
50 if (ret != HDF_SUCCESS) {
51 HDF_LOGE("%s: WifiDriverClientInit failed, line: %d, error no: %d", __FUNCTION__, __LINE__, ret);
52 return ret;
53 }
54
55 ret = HalCmdGetAvailableNetwork();
56 if (ret != HDF_SUCCESS) {
57 HDF_LOGE("%s: HalCmdGetAvailableNetwork failed, line: %d, error no: %d", __FUNCTION__, __LINE__, ret);
58 WifiDriverClientDeinit();
59 return ret;
60 }
61 g_wifiIsStarted = true;
62 return ret;
63 }
64
StopInner(struct IWiFi * iwifi)65 static int32_t StopInner(struct IWiFi *iwifi)
66 {
67 if (iwifi == NULL) {
68 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
69 return HDF_ERR_INVALID_PARAM;
70 }
71 if (!g_wifiIsStarted) {
72 HDF_LOGE("%s: wifi has stopped already, line: %d", __FUNCTION__, __LINE__);
73 return HDF_FAILURE;
74 }
75 WifiDriverClientDeinit();
76 ClearIWiFiList();
77 g_wifiIsStarted = false;
78 return HDF_SUCCESS;
79 }
80
GetSupportFeatureInner(uint8_t * supType,uint32_t size)81 static int32_t GetSupportFeatureInner(uint8_t *supType, uint32_t size)
82 {
83 if (supType == NULL || size <= PROTOCOL_80211_IFTYPE_NUM) {
84 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
85 return HDF_ERR_INVALID_PARAM;
86 }
87 return HalCmdGetSupportType(supType);
88 }
89
GetSupportComboInner(uint64_t * combo,uint32_t size)90 static int32_t GetSupportComboInner(uint64_t *combo, uint32_t size)
91 {
92 if (combo == NULL) {
93 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
94 return HDF_ERR_INVALID_PARAM;
95 }
96 return HalCmdGetSupportCombo(combo, size);
97 }
98
InitFeatureByType(int32_t type,struct IWiFiBaseFeature ** ifeature)99 static int32_t InitFeatureByType(int32_t type, struct IWiFiBaseFeature **ifeature)
100 {
101 int32_t ret;
102
103 switch (type) {
104 case PROTOCOL_80211_IFTYPE_AP:
105 *ifeature = (struct IWiFiBaseFeature *)malloc(sizeof(struct IWiFiAp));
106 if (*ifeature == NULL) {
107 HDF_LOGE("%s: malloc failed, line: %d", __FUNCTION__, __LINE__);
108 return HDF_FAILURE;
109 }
110 (void)memset_s(*ifeature, sizeof(struct IWiFiAp), 0, sizeof(struct IWiFiAp));
111 ret = InitApFeature((struct IWiFiAp **)ifeature);
112 break;
113 case PROTOCOL_80211_IFTYPE_STATION:
114 *ifeature = (struct IWiFiBaseFeature *)malloc(sizeof(struct IWiFiSta));
115 if (*ifeature == NULL) {
116 HDF_LOGE("%s: malloc failed, line: %d", __FUNCTION__, __LINE__);
117 return HDF_FAILURE;
118 }
119 (void)memset_s(*ifeature, sizeof(struct IWiFiSta), 0, sizeof(struct IWiFiSta));
120 ret = InitStaFeature((struct IWiFiSta **)ifeature);
121 break;
122 default:
123 HDF_LOGE("%s: type not support, line: %d", __FUNCTION__, __LINE__);
124 return HDF_FAILURE;
125 }
126 if (ret != HDF_SUCCESS) {
127 free(*ifeature);
128 *ifeature = NULL;
129 }
130 return ret;
131 }
132
FindValidNetwork(int32_t type,struct IWiFiBaseFeature ** feature)133 static int32_t FindValidNetwork(int32_t type, struct IWiFiBaseFeature **feature)
134 {
135 struct DListHead *networkHead = GetNetworkHead();
136 struct IWiFiList *networkNode = NULL;
137
138 DLIST_FOR_EACH_ENTRY(networkNode, networkHead, struct IWiFiList, entry) {
139 if (networkNode->ifeature == NULL && networkNode->supportMode[type] == 1) {
140 if (memcpy_s((*feature)->ifName, IFNAME_MAX_LEN, networkNode->ifName, strlen(networkNode->ifName)) != EOK) {
141 HDF_LOGE("%s: memcpy_s failed, line: %d", __FUNCTION__, __LINE__);
142 return HDF_FAILURE;
143 }
144 (*feature)->type = type;
145 networkNode->ifeature = *feature;
146 return HDF_SUCCESS;
147 }
148 }
149 HDF_LOGE("%s: cannot find available network, line: %d", __FUNCTION__, __LINE__);
150 return HDF_FAILURE;
151 }
152
CreateFeatureInner(int32_t type,struct IWiFiBaseFeature ** ifeature)153 static int32_t CreateFeatureInner(int32_t type, struct IWiFiBaseFeature **ifeature)
154 {
155 int32_t ret;
156
157 if (ifeature == NULL) {
158 HDF_LOGE("%s: ifeature is null, line: %d", __FUNCTION__, __LINE__);
159 return HDF_FAILURE;
160 }
161 ret = InitFeatureByType(type, ifeature);
162 if (ret != HDF_SUCCESS) {
163 HDF_LOGE("%s: init feature failed, line: %d, error no: %d", __FUNCTION__, __LINE__, ret);
164 return ret;
165 }
166
167 ret = FindValidNetwork(type, ifeature);
168 if (ret != HDF_SUCCESS) {
169 HDF_LOGE("%s: create feature failed, line: %d, error no: %d", __FUNCTION__, __LINE__, ret);
170 if (*ifeature != NULL) {
171 free(*ifeature);
172 *ifeature = NULL;
173 }
174 return ret;
175 }
176 return HDF_SUCCESS;
177 }
178
GetFeatureByIfNameInner(const char * ifName,struct IWiFiBaseFeature ** ifeature)179 static int32_t GetFeatureByIfNameInner(const char *ifName, struct IWiFiBaseFeature **ifeature)
180 {
181 struct DListHead *networkHead = GetNetworkHead();
182 struct IWiFiList *networkNode = NULL;
183
184 if (ifName == NULL) {
185 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
186 return HDF_ERR_INVALID_PARAM;
187 }
188 DLIST_FOR_EACH_ENTRY(networkNode, networkHead, struct IWiFiList, entry) {
189 if (strcmp(networkNode->ifName, ifName) == HDF_SUCCESS) {
190 *ifeature = networkNode->ifeature;
191 return HDF_SUCCESS;
192 }
193 }
194 HDF_LOGE("%s: cannot find feature by ifName, line: %d", __FUNCTION__, __LINE__);
195 return HDF_FAILURE;
196 }
197
DestroyFeatureInner(struct IWiFiBaseFeature * ifeature)198 static int32_t DestroyFeatureInner(struct IWiFiBaseFeature *ifeature)
199 {
200 struct DListHead *networkHead = GetNetworkHead();
201 struct IWiFiList *networkNode = NULL;
202
203 if (ifeature == NULL) {
204 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
205 return HDF_ERR_INVALID_PARAM;
206 }
207
208 DLIST_FOR_EACH_ENTRY(networkNode, networkHead, struct IWiFiList, entry) {
209 if (strcmp(networkNode->ifName, ifeature->ifName) == HDF_SUCCESS) {
210 free(ifeature);
211 networkNode->ifeature = NULL;
212 return HDF_SUCCESS;
213 }
214 }
215 HDF_LOGE("%s: cannot find feature to destroy, line: %d", __FUNCTION__, __LINE__);
216 return HDF_FAILURE;
217 }
218
RegisterEventCallbackInner(OnReceiveFunc onRecFunc,const char * ifName)219 static int32_t RegisterEventCallbackInner(OnReceiveFunc onRecFunc, const char *ifName)
220 {
221 if (onRecFunc == NULL || ifName == NULL) {
222 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
223 return HDF_ERR_INVALID_PARAM;
224 }
225 if (WifiRegisterEventCallback(onRecFunc, WIFI_KERNEL_TO_HAL_CLIENT, ifName) != HDF_SUCCESS) {
226 HDF_LOGE("%s: callback function has been registered, line: %d", __FUNCTION__, __LINE__);
227 return HDF_FAILURE;
228 }
229 return HDF_SUCCESS;
230 }
231
UnregisterEventCallbackInner(OnReceiveFunc onRecFunc,const char * ifName)232 static int32_t UnregisterEventCallbackInner(OnReceiveFunc onRecFunc, const char *ifName)
233 {
234 if (onRecFunc == NULL || ifName == NULL) {
235 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
236 return HDF_ERR_INVALID_PARAM;
237 }
238 WifiUnregisterEventCallback(onRecFunc, WIFI_KERNEL_TO_HAL_CLIENT, ifName);
239 return HDF_SUCCESS;
240 }
241
ResetDriverInner(uint8_t chipId,const char * ifName)242 static int32_t ResetDriverInner(uint8_t chipId, const char *ifName)
243 {
244 return HalCmdSetResetDriver(chipId, ifName);
245 }
246
GetNetDevInfoInner(struct NetDeviceInfoResult * netDeviceInfoResult)247 static int32_t GetNetDevInfoInner(struct NetDeviceInfoResult *netDeviceInfoResult)
248 {
249 if (netDeviceInfoResult == NULL) {
250 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
251 return HDF_ERR_INVALID_PARAM;
252 }
253 return GetNetDeviceInfo(netDeviceInfoResult);
254 }
255
Start(struct IWiFi * iwifi)256 static int32_t Start(struct IWiFi *iwifi)
257 {
258 HalMutexLock();
259 int32_t ret = StartInner(iwifi);
260 HalMutexUnlock();
261 return ret;
262 }
263
Stop(struct IWiFi * iwifi)264 static int32_t Stop(struct IWiFi *iwifi)
265 {
266 HalMutexLock();
267 int32_t ret = StopInner(iwifi);
268 HalMutexUnlock();
269 return ret;
270 }
271
GetSupportFeature(uint8_t * supType,uint32_t size)272 static int32_t GetSupportFeature(uint8_t *supType, uint32_t size)
273 {
274 HalMutexLock();
275 int32_t ret = GetSupportFeatureInner(supType, size);
276 HalMutexUnlock();
277 return ret;
278 }
279
GetSupportCombo(uint64_t * combo,uint32_t size)280 static int32_t GetSupportCombo(uint64_t *combo, uint32_t size)
281 {
282 HalMutexLock();
283 int32_t ret = GetSupportComboInner(combo, size);
284 HalMutexUnlock();
285 return ret;
286 }
287
CreateFeature(int32_t type,struct IWiFiBaseFeature ** ifeature)288 static int32_t CreateFeature(int32_t type, struct IWiFiBaseFeature **ifeature)
289 {
290 HalMutexLock();
291 int32_t ret = CreateFeatureInner(type, ifeature);
292 HalMutexUnlock();
293 return ret;
294 }
295
GetFeatureByIfName(const char * ifName,struct IWiFiBaseFeature ** ifeature)296 static int32_t GetFeatureByIfName(const char *ifName, struct IWiFiBaseFeature **ifeature)
297 {
298 HalMutexLock();
299 int32_t ret = GetFeatureByIfNameInner(ifName, ifeature);
300 HalMutexUnlock();
301 return ret;
302 }
303
DestroyFeature(struct IWiFiBaseFeature * ifeature)304 static int32_t DestroyFeature(struct IWiFiBaseFeature *ifeature)
305 {
306 HalMutexLock();
307 int32_t ret = DestroyFeatureInner(ifeature);
308 HalMutexUnlock();
309 return ret;
310 }
311
HalRegisterEventCallback(OnReceiveFunc onRecFunc,const char * ifName)312 static int32_t HalRegisterEventCallback(OnReceiveFunc onRecFunc, const char *ifName)
313 {
314 HalMutexLock();
315 int32_t ret = RegisterEventCallbackInner(onRecFunc, ifName);
316 HalMutexUnlock();
317 return ret;
318 }
319
HalUnregisterEventCallback(OnReceiveFunc onRecFunc,const char * ifName)320 static int32_t HalUnregisterEventCallback(OnReceiveFunc onRecFunc, const char *ifName)
321 {
322 HalMutexLock();
323 int32_t ret = UnregisterEventCallbackInner(onRecFunc, ifName);
324 HalMutexUnlock();
325 return ret;
326 }
327
ResetDriver(const uint8_t chipId,const char * ifName)328 static int32_t ResetDriver(const uint8_t chipId, const char *ifName)
329 {
330 if ((getuid() != WIFI_HOST) && (getuid() != WIFI_C_HOST) && (getuid() > MAX_AUTH_NUM)) {
331 HDF_LOGE("%s: don't have authorized access, line: %d", __FUNCTION__, __LINE__);
332 return ERR_UNAUTH_ACCESS;
333 }
334
335 HalMutexLock();
336 int32_t ret = ResetDriverInner(chipId, ifName);
337 HalMutexUnlock();
338 return ret;
339 }
340
GetNetDevInfo(struct NetDeviceInfoResult * netDeviceInfoResult)341 static int32_t GetNetDevInfo(struct NetDeviceInfoResult *netDeviceInfoResult)
342 {
343 HalMutexLock();
344 int32_t ret = GetNetDevInfoInner(netDeviceInfoResult);
345 HalMutexUnlock();
346 return ret;
347 }
348
WifiConstruct(struct IWiFi ** wifiInstance)349 int32_t WifiConstruct(struct IWiFi **wifiInstance)
350 {
351 static bool isInited = false;
352 static struct IWiFi singleWifiInstance;
353
354 if (!isInited) {
355 if (HalMutexInit() != HDF_SUCCESS) {
356 HDF_LOGE("%s: HalMutexInit failed, line: %d\n", __FUNCTION__, __LINE__);
357 return HDF_FAILURE;
358 }
359
360 singleWifiInstance.start = Start;
361 singleWifiInstance.stop = Stop;
362 singleWifiInstance.getSupportFeature = GetSupportFeature;
363 singleWifiInstance.getSupportCombo = GetSupportCombo;
364 singleWifiInstance.createFeature = CreateFeature;
365 singleWifiInstance.getFeatureByIfName = GetFeatureByIfName;
366 singleWifiInstance.destroyFeature = DestroyFeature;
367 singleWifiInstance.registerEventCallback = HalRegisterEventCallback;
368 singleWifiInstance.unregisterEventCallback = HalUnregisterEventCallback;
369 singleWifiInstance.resetDriver = ResetDriver;
370 singleWifiInstance.getNetDevInfo = GetNetDevInfo;
371 InitIWiFiList();
372 isInited = true;
373 }
374 (*wifiInstance) = &singleWifiInstance;
375 return HDF_SUCCESS;
376 }
377
WifiDestruct(struct IWiFi ** wifiInstance)378 int32_t WifiDestruct(struct IWiFi **wifiInstance)
379 {
380 if (wifiInstance == NULL) {
381 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
382 return HDF_ERR_INVALID_PARAM;
383 }
384 *wifiInstance = NULL;
385 if (HalMutexDestroy() != HDF_SUCCESS) {
386 HDF_LOGE("%s: HalMutexDestroy failed, line: %d", __FUNCTION__, __LINE__);
387 return HDF_FAILURE;
388 }
389 return HDF_SUCCESS;
390 }
391
392 #ifdef __cplusplus
393 #if __cplusplus
394 }
395 #endif
396 #endif
397