• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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: SetScanMacAddr 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     int32_t ret;
262     ret = WifiStartPnoScan(ifName, pnoSettings);
263     if (ret != HDF_SUCCESS) {
264         HDF_LOGE("%s: WifiStartPnoScan failed", __FUNCTION__);
265     }
266     return ret;
267 }
268 
HalCmdStopPnoScan(const char * ifName)269 int32_t HalCmdStopPnoScan(const char *ifName)
270 {
271     int32_t ret;
272     ret = WifiStopPnoScan(ifName);
273     if (ret != HDF_SUCCESS) {
274         HDF_LOGE("%s: WifiStopPnoScan failed", __FUNCTION__);
275     }
276     return ret;
277 }
278 
HalCmdGetSignalPollInfo(const char * ifName,struct SignalResult * signalResult)279 int32_t HalCmdGetSignalPollInfo(const char *ifName, struct SignalResult *signalResult)
280 {
281     int32_t ret;
282     ret = WifiGetSignalPollInfo(ifName, signalResult);
283     if (ret != HDF_SUCCESS) {
284         HDF_LOGE("%s: WifiGetSignalInfo failed", __FUNCTION__);
285     }
286     return ret;
287 }
288 
HalCmdSetResetDriver(const uint8_t chipId,const char * ifName)289 int32_t HalCmdSetResetDriver(const uint8_t chipId, const char *ifName)
290 {
291     int32_t ret;
292     ret = SetResetDriver(chipId, ifName);
293     if (ret != HDF_SUCCESS) {
294         HDF_LOGE("%s: SetResetDriver failed", __FUNCTION__);
295     }
296     return ret;
297 }
298 
ClearIWiFiList(void)299 void ClearIWiFiList(void)
300 {
301     struct IWiFiList *networkList = NULL;
302     struct IWiFiList *tmp = NULL;
303 
304     DLIST_FOR_EACH_ENTRY_SAFE(networkList, tmp, &g_networkHead, struct IWiFiList, entry) {
305         DListRemove(&networkList->entry);
306         free(networkList);
307         networkList = NULL;
308     }
309     InitIWiFiList();
310 }
311 
InitIWiFiList(void)312 void InitIWiFiList(void)
313 {
314     DListHeadInit(&g_networkHead);
315 }
316 
317 #ifdef __cplusplus
318 #if __cplusplus
319 }
320 #endif
321 #endif