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
144 result.freqs = OsalMemCalloc(size * sizeof(uint32_t));
145 if (result.freqs == NULL) {
146 HDF_LOGE("%s: OsalMemCalloc failed", __FUNCTION__);
147 return HDF_FAILURE;
148 }
149
150 result.txPower = OsalMemCalloc(size * sizeof(uint32_t));
151 if (result.txPower == NULL) {
152 HDF_LOGE("%s: OsalMemCalloc failed", __FUNCTION__);
153 OsalMemFree(result.freqs);
154 return HDF_FAILURE;
155 }
156
157 do {
158 ret = GetValidFreqByBand(ifName, band, &result, size);
159 if (ret != HDF_SUCCESS) {
160 HDF_LOGE("%s: GetValidFreqByBand failed", __FUNCTION__);
161 break;
162 }
163 if (memcpy_s(freqs, size * sizeof(uint32_t), result.freqs, result.nums * sizeof(uint32_t)) != EOK) {
164 HDF_LOGE("%s: memcpy failed, line: %d", __FUNCTION__, __LINE__);
165 ret = HDF_FAILURE;
166 break;
167 }
168 *num = result.nums;
169 } while (0);
170
171 OsalMemFree(result.txPower);
172 OsalMemFree(result.freqs);
173 return ret;
174 }
175
HalCmdSetTxPower(const char * ifName,int32_t power)176 int32_t HalCmdSetTxPower(const char *ifName, int32_t power)
177 {
178 int32_t ret;
179 ret = SetTxPower(ifName, power);
180 if (ret != HDF_SUCCESS) {
181 HDF_LOGE("%s: SetTxPower failed", __FUNCTION__);
182 }
183 return ret;
184 }
185
HalCmdGetAssociatedStas(const char * ifName,struct StaInfo * staInfo,uint32_t count,uint32_t * num)186 int32_t HalCmdGetAssociatedStas(const char *ifName, struct StaInfo *staInfo, uint32_t count, uint32_t *num)
187 {
188 if (num == NULL) {
189 HDF_LOGE("%s: HalCmdGetAssociatedStas num NULL!", __FUNCTION__);
190 return HDF_FAILURE;
191 }
192 int32_t ret;
193 struct AssocStaInfoResult result;
194
195 ret = GetAssociatedStas(ifName, &result);
196 if (ret != HDF_SUCCESS) {
197 HDF_LOGE("%s: GetAssociatedStas failed", __FUNCTION__);
198 return ret;
199 }
200 if (memcpy_s(staInfo, count * sizeof(*staInfo), result.infos,
201 result.num * sizeof(struct AssocStaInfo)) != EOK) {
202 HDF_LOGE("%s: memcpy staInfo failed", __FUNCTION__);
203 return HDF_FAILURE;
204 }
205 *num = result.num;
206 return ret;
207 }
208
HalCmdSetCountryCode(const char * ifName,const char * code,uint32_t len)209 int32_t HalCmdSetCountryCode(const char *ifName, const char *code, uint32_t len)
210 {
211 int32_t ret;
212 ret = WifiSetCountryCode(ifName, code, len);
213 if (ret != HDF_SUCCESS) {
214 HDF_LOGE("%s: WifiSetCountryCode failed", __FUNCTION__);
215 }
216 return ret;
217 }
218
HalCmdSetScanningMacAddress(const char * ifName,unsigned char * scanMac,uint8_t len)219 int32_t HalCmdSetScanningMacAddress(const char *ifName, unsigned char *scanMac, uint8_t len)
220 {
221 int32_t ret;
222 ret = SetScanMacAddr(ifName, scanMac, len);
223 if (ret != HDF_SUCCESS) {
224 HDF_LOGE("%s: SetScanMacAddr failed", __FUNCTION__);
225 }
226 return ret;
227 }
228
HalCmdStartScanInner(const char * ifName,WifiScan * scan)229 int32_t HalCmdStartScanInner(const char *ifName, WifiScan *scan)
230 {
231 int32_t ret;
232 ret = WifiCmdScan(ifName, (WifiScan *)scan);
233 if (ret != HDF_SUCCESS) {
234 HDF_LOGE("%s: WifiStartScan failed", __FUNCTION__);
235 }
236 return ret;
237 }
238
HalCmdGetChipId(const char * ifName,uint8_t * chipId)239 int32_t HalCmdGetChipId(const char *ifName, uint8_t *chipId)
240 {
241 int32_t ret;
242 ret = AcquireChipId(ifName, chipId);
243 if (ret != HDF_SUCCESS) {
244 HDF_LOGE("%s: AcquireChipId failed", __FUNCTION__);
245 }
246 return ret;
247 }
248
HalCmdGetIfNamesByChipId(const uint8_t chipId,char ** ifNames,uint32_t * num)249 int32_t HalCmdGetIfNamesByChipId(const uint8_t chipId, char **ifNames, uint32_t *num)
250 {
251 int32_t ret;
252 ret = GetIfNamesByChipId(chipId, ifNames, num);
253 if (ret != HDF_SUCCESS) {
254 HDF_LOGE("%s: GetIfNamesByChipId failed", __FUNCTION__);
255 }
256 return ret;
257 }
258
HalCmdStartPnoScan(const char * ifName,const WifiPnoSettings * pnoSettings)259 int32_t HalCmdStartPnoScan(const char *ifName, const WifiPnoSettings *pnoSettings)
260 {
261 HDF_LOGI("hal enter %{public}s ifName:%{public}s", __FUNCTION__, ifName);
262 int32_t ret;
263 ret = WifiStartPnoScan(ifName, pnoSettings);
264 if (ret != HDF_SUCCESS) {
265 HDF_LOGE("%s: WifiStartPnoScan failed", __FUNCTION__);
266 }
267 HDF_LOGI("hal exit %{public}s ret:%{public}d", __FUNCTION__, ret);
268 return ret;
269 }
270
HalCmdStopPnoScan(const char * ifName)271 int32_t HalCmdStopPnoScan(const char *ifName)
272 {
273 HDF_LOGI("hal enter %{public}s ifName:%{public}s", __FUNCTION__, ifName);
274 int32_t ret;
275 ret = WifiStopPnoScan(ifName);
276 if (ret != HDF_SUCCESS) {
277 HDF_LOGE("%s: WifiStopPnoScan failed", __FUNCTION__);
278 }
279 HDF_LOGI("hal exit %{public}s ret:%{public}d", __FUNCTION__, ret);
280 return ret;
281 }
282
HalCmdGetSignalPollInfo(const char * ifName,struct SignalResult * signalResult)283 int32_t HalCmdGetSignalPollInfo(const char *ifName, struct SignalResult *signalResult)
284 {
285 int32_t ret;
286 ret = WifiGetSignalPollInfo(ifName, signalResult);
287 if (ret != HDF_SUCCESS) {
288 HDF_LOGE("%s: WifiGetSignalInfo failed", __FUNCTION__);
289 }
290 return ret;
291 }
292
HalCmdSetResetDriver(const uint8_t chipId,const char * ifName)293 int32_t HalCmdSetResetDriver(const uint8_t chipId, const char *ifName)
294 {
295 int32_t ret;
296 ret = SetResetDriver(chipId, ifName);
297 if (ret != HDF_SUCCESS) {
298 HDF_LOGE("%s: SetResetDriver failed", __FUNCTION__);
299 }
300 return ret;
301 }
302
HalCmdGetFeatureByIfName(const char * ifName,struct IWiFiBaseFeature ** ifeature)303 int32_t HalCmdGetFeatureByIfName(const char *ifName, struct IWiFiBaseFeature **ifeature)
304 {
305 struct DListHead *networkHead = GetNetworkHead();
306 struct IWiFiList *networkNode = NULL;
307
308 if (ifName == NULL || ifeature == NULL) {
309 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
310 return HDF_ERR_INVALID_PARAM;
311 }
312 DLIST_FOR_EACH_ENTRY(networkNode, networkHead, struct IWiFiList, entry) {
313 if (networkNode == NULL) {
314 HDF_LOGE("%s: networkNode is NULL, line: %d", __FUNCTION__, __LINE__);
315 return HDF_FAILURE;
316 }
317 if (strcmp(networkNode->ifName, ifName) == HDF_SUCCESS) {
318 *ifeature = networkNode->ifeature;
319 return HDF_SUCCESS;
320 }
321 }
322 HDF_LOGE("%s: cannot find feature by ifName, line: %d", __FUNCTION__, __LINE__);
323 return HDF_FAILURE;
324 }
325
HalCmdGetApBandwidth(const char * ifName,uint8_t * bandwidth)326 int32_t HalCmdGetApBandwidth(const char *ifName, uint8_t *bandwidth)
327 {
328 int32_t ret = ClientGetApBandwidth(ifName, bandwidth);
329 if (ret != HDF_SUCCESS) {
330 HDF_LOGE("%s: get ap bandwidth failed, code=%d", __FUNCTION__, ret);
331 }
332 return ret;
333 }
334
HalCmdResetToFactoryMacAddress(const char * ifName)335 int32_t HalCmdResetToFactoryMacAddress(const char *ifName)
336 {
337 int32_t ret;
338 struct IWiFiBaseFeature *ifeature = NULL;
339 ret = HalCmdGetFeatureByIfName(ifName, &ifeature);
340 if (ret != HDF_SUCCESS || ifeature == NULL) {
341 HDF_LOGE("%s: hal cmd get devmac addr failed, code=%d", __FUNCTION__, ret);
342 return ret;
343 }
344
345 unsigned char mac[ETH_ADDR_LEN] = {0};
346 ret = HalCmdGetDevMacAddr(ifName, ifeature->type, mac, ETH_ADDR_LEN);
347 if (ret != HDF_SUCCESS) {
348 HDF_LOGE("%s: hal cmd get devmac addr failed, code=%d", __FUNCTION__, ret);
349 return ret;
350 }
351
352 ret = HalCmdSetMacAddr(ifName, mac, ETH_ADDR_LEN);
353 if (ret != HDF_SUCCESS) {
354 HDF_LOGE("%s: hal cmd set mac addr failed, code=%d", __FUNCTION__, ret);
355 }
356 return ret;
357 }
358
ClearIWiFiList(void)359 void ClearIWiFiList(void)
360 {
361 struct IWiFiList *networkList = NULL;
362 struct IWiFiList *tmp = NULL;
363
364 DLIST_FOR_EACH_ENTRY_SAFE(networkList, tmp, &g_networkHead, struct IWiFiList, entry) {
365 DListRemove(&networkList->entry);
366 free(networkList);
367 networkList = NULL;
368 }
369 InitIWiFiList();
370 }
371
InitIWiFiList(void)372 void InitIWiFiList(void)
373 {
374 DListHeadInit(&g_networkHead);
375 }
376
377 #ifdef __cplusplus
378 #if __cplusplus
379 }
380 #endif
381 #endif