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 <string.h>
17 #include <stdlib.h>
18 #include <osal_mem.h>
19 #include <pthread.h>
20 #include "wifi_common_cmd.h"
21 #include "hdf_log.h"
22 #include "securec.h"
23
24 #ifdef __cplusplus
25 #if __cplusplus
26 extern "C" {
27 #endif
28 #endif
29
30 #ifndef EOK
31 #define EOK 0
32 #endif
33
34 #define MAX_CALL_BACK_COUNT 10
35 static struct CallbackEvent *g_callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
36 static struct Hid2dEvent *g_hid2dEventMap[MAX_CALL_BACK_COUNT] = {NULL};
37 static pthread_mutex_t g_callbackMutex;
38 static pthread_mutex_t g_hid2dEventMutex;
39
InitEventcallbackMutex(void)40 int32_t InitEventcallbackMutex(void)
41 {
42 if (pthread_mutex_init(&g_callbackMutex, NULL) != RET_CODE_SUCCESS) {
43 HDF_LOGE("%s: init g_callbackMutex failed.", __FUNCTION__);
44 return RET_CODE_FAILURE;
45 }
46 if (pthread_mutex_init(&g_hid2dEventMutex, NULL) != RET_CODE_SUCCESS) {
47 HDF_LOGE("%s: init g_hid2dEventMutex failed.", __FUNCTION__);
48 return RET_CODE_FAILURE;
49 }
50 return RET_CODE_SUCCESS;
51 }
52
DeinitEventcallbackMutex(void)53 void DeinitEventcallbackMutex(void)
54 {
55 pthread_mutex_destroy(&g_callbackMutex);
56 pthread_mutex_destroy(&g_hid2dEventMutex);
57 }
58
WifiEventReport(const char * ifName,uint32_t event,void * data)59 void WifiEventReport(const char *ifName, uint32_t event, void *data)
60 {
61 HDF_LOGI("hal enter %{public}s", __FUNCTION__);
62 uint32_t i;
63 OnReceiveFunc callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
64
65 pthread_mutex_lock(&g_callbackMutex);
66 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
67 if (g_callbackEventMap[i] != NULL && (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) &&
68 (((1 << event) & g_callbackEventMap[i]->eventType) != 0)) {
69 HDF_LOGI("send event=%{public}u, ifName=%{public}s, i=%{public}d", event, ifName, i);
70 callbackEventMap[i] = g_callbackEventMap[i]->onRecFunc;
71 }
72 }
73 pthread_mutex_unlock(&g_callbackMutex);
74 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
75 if (callbackEventMap[i] != NULL) {
76 HDF_LOGI("callbackEventMap i:%{public}d vent=%{public}u, ifName=%{public}s", i, event, ifName);
77 callbackEventMap[i](event, data, ifName);
78 }
79 }
80 HDF_LOGI("hal exit %{public}s", __FUNCTION__);
81 }
82
WifiRegisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)83 int32_t WifiRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
84 {
85 uint32_t i;
86 struct CallbackEvent *callbackEvent = NULL;
87 HDF_LOGI("hal enter %{public}s", __FUNCTION__);
88 if (onRecFunc == NULL || ifName == NULL) {
89 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
90 return RET_CODE_INVALID_PARAM;
91 }
92 pthread_mutex_lock(&g_callbackMutex);
93 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
94 if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
95 (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
96 HDF_LOGI("i:%{public}d ifName:%{public}s the onRecFunc has been registered!", i, ifName);
97 pthread_mutex_unlock(&g_callbackMutex);
98 return RET_CODE_SUCCESS;
99 }
100 }
101 pthread_mutex_unlock(&g_callbackMutex);
102 callbackEvent = (struct CallbackEvent *)malloc(sizeof(struct CallbackEvent));
103 if (callbackEvent == NULL) {
104 HDF_LOGE("%{public}s fail: malloc fail!", __FUNCTION__);
105 return RET_CODE_FAILURE;
106 }
107 callbackEvent->eventType = eventType;
108 if (strcpy_s(callbackEvent->ifName, IFNAMSIZ, ifName) != RET_CODE_SUCCESS) {
109 free(callbackEvent);
110 HDF_LOGE("%{public}s: ifName strcpy_s fail", __FUNCTION__);
111 return RET_CODE_FAILURE;
112 }
113 callbackEvent->onRecFunc = onRecFunc;
114 pthread_mutex_lock(&g_callbackMutex);
115 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
116 if (g_callbackEventMap[i] == NULL) {
117 g_callbackEventMap[i] = callbackEvent;
118 HDF_LOGI("g_callbackEventMap successful, i:%{public}u, ifName:%{public}s, eventType:%{public}u", i, ifName,
119 eventType);
120 pthread_mutex_unlock(&g_callbackMutex);
121 return RET_CODE_SUCCESS;
122 }
123 }
124 pthread_mutex_unlock(&g_callbackMutex);
125 free(callbackEvent);
126 HDF_LOGI("hal exit %{public}s fail register onRecFunc num more than %{public}d", __FUNCTION__, MAX_CALL_BACK_COUNT);
127 return RET_CODE_FAILURE;
128 }
129
WifiUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)130 void WifiUnregisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
131 {
132 uint32_t i;
133 HDF_LOGI("hal enter %{public}s", __FUNCTION__);
134 if (onRecFunc == NULL || ifName == NULL) {
135 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
136 return;
137 }
138 pthread_mutex_lock(&g_callbackMutex);
139 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
140 if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
141 (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
142 g_callbackEventMap[i]->onRecFunc = NULL;
143 free(g_callbackEventMap[i]);
144 g_callbackEventMap[i] = NULL;
145 HDF_LOGI("%{public}s: g_callbackEventMap null, i:%{public}u, ifName:%{public}s, eventType:%{public}u",
146 __FUNCTION__, i, ifName, eventType);
147 pthread_mutex_unlock(&g_callbackMutex);
148 return;
149 }
150 }
151 pthread_mutex_unlock(&g_callbackMutex);
152 HDF_LOGI("hal exit %{public}s", __FUNCTION__);
153 }
154
Hid2dEventReport(const char * ifName,const uint8_t * msg,uint32_t msgLen)155 void Hid2dEventReport(const char *ifName, const uint8_t *msg, uint32_t msgLen)
156 {
157 uint32_t i;
158 Hid2dCallback hid2dEventMap[MAX_CALL_BACK_COUNT] = {NULL};
159
160 pthread_mutex_lock(&g_hid2dEventMutex);
161 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
162 if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0)) {
163 HDF_LOGI("%{public}s: Hid2dEventReport ifName = %s", __FUNCTION__, ifName);
164 hid2dEventMap[i] = g_hid2dEventMap[i]->func;
165 }
166 }
167 pthread_mutex_unlock(&g_hid2dEventMutex);
168 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
169 if (hid2dEventMap[i] != NULL) {
170 hid2dEventMap[i](msg, msgLen);
171 }
172 }
173 }
174
WifiRegisterHid2dCallback(Hid2dCallback func,const char * ifName)175 int32_t WifiRegisterHid2dCallback(Hid2dCallback func, const char *ifName)
176 {
177 struct Hid2dEvent *event = NULL;
178 uint32_t i;
179 HDF_LOGI("hal enter %{public}s", __FUNCTION__);
180 if (func == NULL || ifName == NULL) {
181 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
182 return RET_CODE_INVALID_PARAM;
183 }
184 pthread_mutex_lock(&g_hid2dEventMutex);
185 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
186 if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0) &&
187 g_hid2dEventMap[i]->func == func) {
188 HDF_LOGI("%{public}s: i:%{public}d ifName:%{public}s, the callback function has been registered!",
189 __FUNCTION__, i, ifName);
190 pthread_mutex_unlock(&g_hid2dEventMutex);
191 return RET_CODE_SUCCESS;
192 }
193 }
194 pthread_mutex_unlock(&g_hid2dEventMutex);
195 event = (struct Hid2dEvent *)OsalMemCalloc(sizeof(struct Hid2dEvent));
196 if (event == NULL) {
197 HDF_LOGE("%s fail: OsalMemCalloc fail!", __FUNCTION__);
198 return RET_CODE_FAILURE;
199 }
200 do {
201 if (strcpy_s(event->ifName, IFNAMSIZ + 1, ifName) != RET_CODE_SUCCESS) {
202 HDF_LOGE("%s: ifName strcpy_s fail", __FUNCTION__);
203 break;
204 }
205 event->func = func;
206 pthread_mutex_lock(&g_hid2dEventMutex);
207 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
208 if (g_hid2dEventMap[i] == NULL) {
209 g_hid2dEventMap[i] = event;
210 HDF_LOGI("%{public}s, g_hid2dEventMap i:%{public}d event!", __FUNCTION__, i);
211 pthread_mutex_unlock(&g_hid2dEventMutex);
212 return RET_CODE_SUCCESS;
213 }
214 }
215 pthread_mutex_unlock(&g_hid2dEventMutex);
216 } while (0);
217
218 OsalMemFree(event);
219 HDF_LOGI("hal exit %{public}s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
220 return RET_CODE_FAILURE;
221 }
222
WifiUnregisterHid2dCallback(Hid2dCallback func,const char * ifName)223 void WifiUnregisterHid2dCallback(Hid2dCallback func, const char *ifName)
224 {
225 uint32_t i;
226 HDF_LOGI("hal enter %{public}s", __FUNCTION__);
227 if (func == NULL || ifName == NULL) {
228 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
229 return;
230 }
231 pthread_mutex_lock(&g_hid2dEventMutex);
232 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
233 if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0) &&
234 g_hid2dEventMap[i]->func == func) {
235 g_hid2dEventMap[i]->func = NULL;
236 OsalMemFree(g_hid2dEventMap[i]);
237 g_hid2dEventMap[i] = NULL;
238 pthread_mutex_unlock(&g_hid2dEventMutex);
239 HDF_LOGI("%{public}s, g_hid2dEventMap i:%{public}d null!", __FUNCTION__, i);
240 return;
241 }
242 }
243 pthread_mutex_unlock(&g_hid2dEventMutex);
244 HDF_LOGI("hal exit %{public}s", __FUNCTION__);
245 }
246
FreeScanResult(WifiScanResult * res)247 void FreeScanResult(WifiScanResult *res)
248 {
249 if (res == NULL) {
250 return;
251 }
252 if (res->bssid != NULL) {
253 OsalMemFree(res->bssid);
254 res->bssid = NULL;
255 }
256 if (res->ie != NULL) {
257 OsalMemFree(res->ie);
258 res->ie = NULL;
259 }
260 if (res->beaconIe != NULL) {
261 OsalMemFree(res->beaconIe);
262 res->beaconIe = NULL;
263 }
264 }
265
FreeScanResults(WifiScanResults * res)266 void FreeScanResults(WifiScanResults *res)
267 {
268 HDF_LOGI("hal enter %{public}s", __FUNCTION__);
269 uint32_t i;
270 if (res == NULL) {
271 return;
272 }
273 for (i = 0; i < res->num; i++) {
274 FreeScanResult(&res->scanResult[i]);
275 }
276 OsalMemFree(res->scanResult);
277 res->scanResult = NULL;
278 HDF_LOGI("hal exit %{public}s", __FUNCTION__);
279 }
280
InitScanResults(WifiScanResults * scanResults)281 int32_t InitScanResults(WifiScanResults *scanResults)
282 {
283 HDF_LOGI("hal enter %{public}s", __FUNCTION__);
284 if (scanResults == NULL) {
285 HDF_LOGE("%s: scanResults is NULL", __FUNCTION__);
286 return RET_CODE_FAILURE;
287 }
288 scanResults->scanResultCapacity = INIT_SCAN_RES_NUM;
289 scanResults->num = 0;
290 scanResults->scanResult = (WifiScanResult *)OsalMemCalloc(sizeof(WifiScanResult) * scanResults->scanResultCapacity);
291 if (scanResults->scanResult == NULL) {
292 HDF_LOGE("%s: scanResults->scanResult is NULL", __FUNCTION__);
293 return RET_CODE_NOMEM;
294 }
295 HDF_LOGI("hal exit %{public}s", __FUNCTION__);
296 return RET_CODE_SUCCESS;
297 }
298 #ifdef __cplusplus
299 #if __cplusplus
300 }
301 #endif
302 #endif
303