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_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", __FUNCTION__);
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(int32_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(int32_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(int32_t), result.freqs, result.nums * sizeof(int32_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
HalCmdGetAsscociatedStas(const char * ifName,struct StaInfo * staInfo,uint32_t count,uint32_t * num)186 int32_t HalCmdGetAsscociatedStas(const char *ifName, struct StaInfo *staInfo, uint32_t count, uint32_t *num)
187 {
188 int32_t ret;
189 struct AssocStaInfoResult result;
190
191 ret = GetAssociatedStas(ifName, &result);
192 if (ret != HDF_SUCCESS) {
193 HDF_LOGE("%s: GetAssociatedStas failed", __FUNCTION__);
194 return ret;
195 }
196 if (memcpy_s(staInfo, count * sizeof(*staInfo), result.infos,
197 result.num * sizeof(struct AssocStaInfo)) != EOK) {
198 HDF_LOGE("%s: memcpy staInfo failed", __FUNCTION__);
199 return HDF_FAILURE;
200 }
201 *num = result.num;
202 return ret;
203 }
204
HalCmdSetCountryCode(const char * ifName,const char * code,uint32_t len)205 int32_t HalCmdSetCountryCode(const char *ifName, const char *code, uint32_t len)
206 {
207 int32_t ret;
208 ret = WifiSetCountryCode(ifName, code, len);
209 if (ret != HDF_SUCCESS) {
210 HDF_LOGE("%s: WifiSetCountryCode failed", __FUNCTION__);
211 }
212 return ret;
213 }
214
HalCmdSetScanningMacAddress(const char * ifName,unsigned char * scanMac,uint8_t len)215 int32_t HalCmdSetScanningMacAddress(const char *ifName, unsigned char *scanMac, uint8_t len)
216 {
217 int32_t ret;
218 ret = SetScanMacAddr(ifName, scanMac, len);
219 if (ret != HDF_SUCCESS) {
220 HDF_LOGE("%s: SetScanMacAddr failed", __FUNCTION__);
221 }
222 return ret;
223 }
224
HalCmdStartScanInner(const char * ifName,WifiScan * scan)225 int32_t HalCmdStartScanInner(const char *ifName, WifiScan *scan)
226 {
227 int32_t ret;
228 ret = WifiCmdScan(ifName, (WifiScan *)scan);
229 if (ret != HDF_SUCCESS) {
230 HDF_LOGE("%s: SetScanMacAddr failed", __FUNCTION__);
231 }
232 return ret;
233 }
234
HalCmdGetChipId(const char * ifName,uint8_t * chipId)235 int32_t HalCmdGetChipId(const char *ifName, uint8_t *chipId)
236 {
237 int32_t ret;
238 ret = AcquireChipId(ifName, chipId);
239 if (ret != HDF_SUCCESS) {
240 HDF_LOGE("%s: AcquireChipId failed", __FUNCTION__);
241 }
242 return ret;
243 }
244
HalCmdGetIfNamesByChipId(const uint8_t chipId,char ** ifNames,uint32_t * num)245 int32_t HalCmdGetIfNamesByChipId(const uint8_t chipId, char **ifNames, uint32_t *num)
246 {
247 int32_t ret;
248 ret = GetIfNamesByChipId(chipId, ifNames, num);
249 if (ret != HDF_SUCCESS) {
250 HDF_LOGE("%s: GetIfNamesByChipId failed", __FUNCTION__);
251 }
252 return ret;
253 }
254
HalCmdSetResetDriver(const uint8_t chipId,const char * ifName)255 int32_t HalCmdSetResetDriver(const uint8_t chipId, const char *ifName)
256 {
257 int32_t ret;
258 ret = SetResetDriver(chipId, ifName);
259 if (ret != HDF_SUCCESS) {
260 HDF_LOGE("%s: SetResetDriver failed", __FUNCTION__);
261 }
262 return ret;
263 }
264
ClearIWiFiList(void)265 void ClearIWiFiList(void)
266 {
267 struct IWiFiList *networkList = NULL;
268 struct IWiFiList *tmp = NULL;
269
270 DLIST_FOR_EACH_ENTRY_SAFE(networkList, tmp, &g_networkHead, struct IWiFiList, entry) {
271 DListRemove(&networkList->entry);
272 free(networkList);
273 networkList = NULL;
274 }
275 InitIWiFiList();
276 }
277
InitIWiFiList(void)278 void InitIWiFiList(void)
279 {
280 DListHeadInit(&g_networkHead);
281 }
282
283 #ifdef __cplusplus
284 #if __cplusplus
285 }
286 #endif
287 #endif