1 /*
2 * Copyright (c) 2021-2023 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_cmd.h"
17 #include <osal_mem.h>
18 #include "hdf_log.h"
19 #include "securec.h"
20 #include "wifi_hal_sta_feature.h"
21
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27
28 static struct DListHead g_networkHead = {0};
29
GetNetworkHead(void)30 struct DListHead *GetNetworkHead(void)
31 {
32 return &g_networkHead;
33 }
34
HalCmdGetAvailableNetwork(void)35 int32_t HalCmdGetAvailableNetwork(void)
36 {
37 int32_t ret;
38 struct NetworkInfoResult networkInfo = { 0 };
39 uint32_t i;
40
41 ret = GetUsableNetworkInfo(&networkInfo);
42 if (ret != HDF_SUCCESS) {
43 HDF_LOGE("%s: get network info failed", __FUNCTION__);
44 return ret;
45 }
46 if (!DListIsEmpty(&g_networkHead)) {
47 ClearIWiFiList();
48 }
49 for (i = 0; i < networkInfo.nums; i++) {
50 struct IWiFiList *networkList = (struct IWiFiList *)malloc(sizeof(struct IWiFiList));
51 if (networkList == NULL) {
52 HDF_LOGE("%s: malloc failed, line: %d", __FUNCTION__, __LINE__);
53 ClearIWiFiList();
54 return HDF_FAILURE;
55 }
56 (void)memset_s(networkList, sizeof(struct IWiFiList), 0, sizeof(struct IWiFiList));
57 DListInsertTail(&networkList->entry, &g_networkHead);
58 if (memcpy_s(networkList->ifName, IFNAME_MAX_LEN, networkInfo.infos[i].name,
59 strlen(networkInfo.infos[i].name)) != EOK) {
60 HDF_LOGE("%s: memcpy_s failed, line: %d", __FUNCTION__, __LINE__);
61 ClearIWiFiList();
62 return HDF_FAILURE;
63 }
64 if (memcpy_s(networkList->supportMode, PROTOCOL_80211_IFTYPE_NUM,
65 networkInfo.infos[i].supportMode, PROTOCOL_80211_IFTYPE_NUM) != EOK) {
66 HDF_LOGE("%s: memcpy_s failed, line: %d", __FUNCTION__, __LINE__);
67 ClearIWiFiList();
68 return HDF_FAILURE;
69 }
70 networkList->ifeature = NULL;
71 }
72 return ret;
73 }
74
GetSupportTypeByList(uint8_t * supType)75 static void GetSupportTypeByList(uint8_t *supType)
76 {
77 int32_t i;
78 struct IWiFiList *networkList = NULL;
79
80 DLIST_FOR_EACH_ENTRY(networkList, &g_networkHead, struct IWiFiList, entry) {
81 for (i = 0; i < PROTOCOL_80211_IFTYPE_NUM; i++) {
82 if (networkList->supportMode[i] == 1) {
83 supType[i] = 1;
84 }
85 }
86 }
87 }
88
HalCmdGetSupportType(uint8_t * supType)89 int32_t HalCmdGetSupportType(uint8_t *supType)
90 {
91 int32_t ret;
92 uint8_t isComboValid;
93
94 GetSupportTypeByList(supType);
95 ret = IsSupportCombo(&isComboValid);
96 if (ret != HDF_SUCCESS) {
97 HDF_LOGE("%s:IsSupportCombo failed, line: %d", __FUNCTION__, __LINE__);
98 return ret;
99 }
100 supType[PROTOCOL_80211_IFTYPE_NUM] = isComboValid;
101
102 return ret;
103 }
104
HalCmdGetSupportCombo(uint64_t * supCombo,uint32_t size)105 int32_t HalCmdGetSupportCombo(uint64_t *supCombo, uint32_t size)
106 {
107 int32_t ret;
108
109 ret = GetComboInfo(supCombo, size);
110 if (ret != HDF_SUCCESS) {
111 HDF_LOGE("%s: GetComboInfo failed, line: %d", __FUNCTION__, __LINE__);
112 }
113 return ret;
114 }
115
HalCmdGetDevMacAddr(const char * ifName,int32_t type,unsigned char * mac,uint8_t len)116 int32_t HalCmdGetDevMacAddr(const char *ifName, int32_t type, unsigned char *mac, uint8_t len)
117 {
118 int32_t ret;
119
120 ret = GetDevMacAddr(ifName, type, (uint8_t *)mac, len);
121 if (ret != HDF_SUCCESS) {
122 HDF_LOGE("%s:GetDevMacAddr failed, line: %d", __FUNCTION__, __LINE__);
123 }
124 return ret;
125 }
126
HalCmdSetMacAddr(const char * ifName,unsigned char * mac,uint8_t len)127 int32_t HalCmdSetMacAddr(const char *ifName, unsigned char *mac, uint8_t len)
128 {
129 int32_t ret;
130
131 ret = SetMacAddr(ifName, mac, len);
132 if (ret != HDF_SUCCESS) {
133 HDF_LOGE("%s: SetMacAddr failed, ret = %{public}d", __FUNCTION__, ret);
134 }
135 return ret;
136 }
137
HalCmdGetValidFreqWithBand(const char * ifName,int32_t band,int32_t * freqs,uint32_t size,uint32_t * num)138 int32_t HalCmdGetValidFreqWithBand(const char *ifName, int32_t band, int32_t *freqs,
139 uint32_t size, uint32_t *num)
140 {
141 int32_t ret;
142 struct FreqInfoResult result;
143 if (size > MAX_OSALMEMCALLOC_NUM) {
144 HDF_LOGE("%s: OsalMemCalloc failed", __FUNCTION__);
145 return HDF_FAILURE;
146 }
147
148 result.freqs = OsalMemCalloc(size * sizeof(uint32_t));
149 if (result.freqs == NULL) {
150 HDF_LOGE("%s: OsalMemCalloc failed", __FUNCTION__);
151 return HDF_FAILURE;
152 }
153
154 result.txPower = OsalMemCalloc(size * sizeof(uint32_t));
155 if (result.txPower == NULL) {
156 HDF_LOGE("%s: OsalMemCalloc failed", __FUNCTION__);
157 OsalMemFree(result.freqs);
158 return HDF_FAILURE;
159 }
160
161 do {
162 ret = GetValidFreqByBand(ifName, band, &result, size);
163 if (ret != HDF_SUCCESS) {
164 HDF_LOGE("%s: GetValidFreqByBand failed", __FUNCTION__);
165 break;
166 }
167 if (memcpy_s(freqs, size * sizeof(uint32_t), result.freqs, result.nums * sizeof(uint32_t)) != EOK) {
168 HDF_LOGE("%s: memcpy failed, line: %d", __FUNCTION__, __LINE__);
169 ret = HDF_FAILURE;
170 break;
171 }
172 *num = result.nums;
173 } while (0);
174
175 OsalMemFree(result.txPower);
176 OsalMemFree(result.freqs);
177 return ret;
178 }
179
HalCmdSetTxPower(const char * ifName,int32_t power)180 int32_t HalCmdSetTxPower(const char *ifName, int32_t power)
181 {
182 int32_t ret;
183 ret = SetTxPower(ifName, power);
184 if (ret != HDF_SUCCESS) {
185 HDF_LOGE("%s: SetTxPower failed", __FUNCTION__);
186 }
187 return ret;
188 }
189
HalCmdGetAssociatedStas(const char * ifName,struct StaInfo * staInfo,uint32_t count,uint32_t * num)190 int32_t HalCmdGetAssociatedStas(const char *ifName, struct StaInfo *staInfo, uint32_t count, uint32_t *num)
191 {
192 if (num == NULL) {
193 HDF_LOGE("%s: HalCmdGetAssociatedStas num NULL!", __FUNCTION__);
194 return HDF_FAILURE;
195 }
196 int32_t ret;
197 struct AssocStaInfoResult result;
198
199 ret = GetAssociatedStas(ifName, &result);
200 if (ret != HDF_SUCCESS) {
201 HDF_LOGE("%s: GetAssociatedStas failed", __FUNCTION__);
202 return ret;
203 }
204 if (memcpy_s(staInfo, count * sizeof(*staInfo), result.infos,
205 result.num * sizeof(struct AssocStaInfo)) != EOK) {
206 HDF_LOGE("%s: memcpy staInfo failed", __FUNCTION__);
207 return HDF_FAILURE;
208 }
209 *num = result.num;
210 return ret;
211 }
212
HalCmdSetCountryCode(const char * ifName,const char * code,uint32_t len)213 int32_t HalCmdSetCountryCode(const char *ifName, const char *code, uint32_t len)
214 {
215 int32_t ret;
216 ret = WifiSetCountryCode(ifName, code, len);
217 if (ret != HDF_SUCCESS) {
218 HDF_LOGE("%s: WifiSetCountryCode failed", __FUNCTION__);
219 }
220 return ret;
221 }
222
HalCmdSetScanningMacAddress(const char * ifName,unsigned char * scanMac,uint8_t len)223 int32_t HalCmdSetScanningMacAddress(const char *ifName, unsigned char *scanMac, uint8_t len)
224 {
225 int32_t ret;
226 ret = SetScanMacAddr(ifName, scanMac, len);
227 if (ret != HDF_SUCCESS) {
228 HDF_LOGE("%s: SetScanMacAddr failed", __FUNCTION__);
229 }
230 return ret;
231 }
232
HalCmdStartScanInner(const char * ifName,WifiScan * scan)233 int32_t HalCmdStartScanInner(const char *ifName, WifiScan *scan)
234 {
235 int32_t ret;
236 ret = WifiCmdScan(ifName, (WifiScan *)scan);
237 if (ret != HDF_SUCCESS) {
238 HDF_LOGE("%s: WifiStartScan failed", __FUNCTION__);
239 }
240 return ret;
241 }
242
HalCmdGetChipId(const char * ifName,uint8_t * chipId)243 int32_t HalCmdGetChipId(const char *ifName, uint8_t *chipId)
244 {
245 int32_t ret;
246 ret = AcquireChipId(ifName, chipId);
247 if (ret != HDF_SUCCESS) {
248 HDF_LOGE("%s: AcquireChipId failed", __FUNCTION__);
249 }
250 return ret;
251 }
252
HalCmdGetIfNamesByChipId(const uint8_t chipId,char ** ifNames,uint32_t * num)253 int32_t HalCmdGetIfNamesByChipId(const uint8_t chipId, char **ifNames, uint32_t *num)
254 {
255 int32_t ret;
256 ret = GetIfNamesByChipId(chipId, ifNames, num);
257 if (ret != HDF_SUCCESS) {
258 HDF_LOGE("%s: GetIfNamesByChipId failed", __FUNCTION__);
259 }
260 return ret;
261 }
262
HalCmdStartPnoScan(const char * ifName,const WifiPnoSettings * pnoSettings)263 int32_t HalCmdStartPnoScan(const char *ifName, const WifiPnoSettings *pnoSettings)
264 {
265 HDF_LOGI("hal enter %{public}s ifName:%{public}s", __FUNCTION__, ifName);
266 int32_t ret;
267 ret = WifiStartPnoScan(ifName, pnoSettings);
268 if (ret != HDF_SUCCESS) {
269 HDF_LOGE("%s: WifiStartPnoScan failed", __FUNCTION__);
270 }
271 HDF_LOGI("hal exit %{public}s ret:%{public}d", __FUNCTION__, ret);
272 return ret;
273 }
274
HalCmdStopPnoScan(const char * ifName)275 int32_t HalCmdStopPnoScan(const char *ifName)
276 {
277 HDF_LOGI("hal enter %{public}s ifName:%{public}s", __FUNCTION__, ifName);
278 int32_t ret;
279 ret = WifiStopPnoScan(ifName);
280 if (ret != HDF_SUCCESS) {
281 HDF_LOGE("%s: WifiStopPnoScan failed", __FUNCTION__);
282 }
283 HDF_LOGI("hal exit %{public}s ret:%{public}d", __FUNCTION__, ret);
284 return ret;
285 }
286
HalCmdGetSignalPollInfo(const char * ifName,struct SignalResult * signalResult)287 int32_t HalCmdGetSignalPollInfo(const char *ifName, struct SignalResult *signalResult)
288 {
289 int32_t ret;
290 ret = WifiGetSignalPollInfo(ifName, signalResult);
291 if (ret != HDF_SUCCESS) {
292 HDF_LOGE("%s: WifiGetSignalInfo failed", __FUNCTION__);
293 }
294 return ret;
295 }
296
HalCmdSetResetDriver(const uint8_t chipId,const char * ifName)297 int32_t HalCmdSetResetDriver(const uint8_t chipId, const char *ifName)
298 {
299 int32_t ret;
300 ret = SetResetDriver(chipId, ifName);
301 if (ret != HDF_SUCCESS) {
302 HDF_LOGE("%s: SetResetDriver failed", __FUNCTION__);
303 }
304 return ret;
305 }
306
HalCmdGetFeatureByIfName(const char * ifName,struct IWiFiBaseFeature ** ifeature)307 int32_t HalCmdGetFeatureByIfName(const char *ifName, struct IWiFiBaseFeature **ifeature)
308 {
309 struct DListHead *networkHead = GetNetworkHead();
310 struct IWiFiList *networkNode = NULL;
311
312 if (ifName == NULL || ifeature == NULL) {
313 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
314 return HDF_ERR_INVALID_PARAM;
315 }
316 DLIST_FOR_EACH_ENTRY(networkNode, networkHead, struct IWiFiList, entry) {
317 if (networkNode == NULL) {
318 HDF_LOGE("%s: networkNode is NULL, line: %d", __FUNCTION__, __LINE__);
319 return HDF_FAILURE;
320 }
321 if (strcmp(networkNode->ifName, ifName) == HDF_SUCCESS) {
322 *ifeature = networkNode->ifeature;
323 return HDF_SUCCESS;
324 }
325 }
326 HDF_LOGE("%s: cannot find feature by ifName, line: %d", __FUNCTION__, __LINE__);
327 return HDF_FAILURE;
328 }
329
HalCmdGetApBandwidth(const char * ifName,uint8_t * bandwidth)330 int32_t HalCmdGetApBandwidth(const char *ifName, uint8_t *bandwidth)
331 {
332 int32_t ret = ClientGetApBandwidth(ifName, bandwidth);
333 if (ret != HDF_SUCCESS) {
334 HDF_LOGE("%s: get ap bandwidth failed, code=%d", __FUNCTION__, ret);
335 }
336 return ret;
337 }
338
HalCmdResetToFactoryMacAddress(const char * ifName)339 int32_t HalCmdResetToFactoryMacAddress(const char *ifName)
340 {
341 int32_t ret;
342 struct IWiFiBaseFeature *ifeature = NULL;
343 ret = HalCmdGetFeatureByIfName(ifName, &ifeature);
344 if (ret != HDF_SUCCESS || ifeature == NULL) {
345 HDF_LOGE("%s: hal cmd get devmac addr failed, code=%d", __FUNCTION__, ret);
346 return ret;
347 }
348
349 unsigned char mac[ETH_ADDR_LEN] = {0};
350 ret = HalCmdGetDevMacAddr(ifName, ifeature->type, mac, ETH_ADDR_LEN);
351 if (ret != HDF_SUCCESS) {
352 HDF_LOGE("%s: hal cmd get devmac addr failed, code=%d", __FUNCTION__, ret);
353 return ret;
354 }
355
356 ret = HalCmdSetMacAddr(ifName, mac, ETH_ADDR_LEN);
357 if (ret != HDF_SUCCESS) {
358 HDF_LOGE("%s: hal cmd set mac addr failed, code=%d", __FUNCTION__, ret);
359 }
360 return ret;
361 }
362
ClearIWiFiList(void)363 void ClearIWiFiList(void)
364 {
365 struct IWiFiList *networkList = NULL;
366 struct IWiFiList *tmp = NULL;
367
368 DLIST_FOR_EACH_ENTRY_SAFE(networkList, tmp, &g_networkHead, struct IWiFiList, entry) {
369 DListRemove(&networkList->entry);
370 free(networkList);
371 networkList = NULL;
372 }
373 InitIWiFiList();
374 }
375
InitIWiFiList(void)376 void InitIWiFiList(void)
377 {
378 DListHeadInit(&g_networkHead);
379 }
380
381 #ifdef __cplusplus
382 #if __cplusplus
383 }
384 #endif
385 #endif