• 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 <string.h>
17 #include <stdlib.h>
18 #include <osal_mem.h>
19 #include "wifi_common_cmd.h"
20 #include "hdf_log.h"
21 #include "securec.h"
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 #ifndef EOK
30 #define EOK 0
31 #endif
32 
33 #define MAX_CALL_BACK_COUNT 10
34 static struct CallbackEvent *g_callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
35 static struct Hid2dEvent *g_hid2dEventMap[MAX_CALL_BACK_COUNT] = {NULL};
36 
WifiEventReport(const char * ifName,uint32_t event,void * data)37 void WifiEventReport(const char *ifName, uint32_t event, void *data)
38 {
39     uint32_t i;
40 
41     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
42         if (g_callbackEventMap[i] != NULL && (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) &&
43             (((1 << event) & g_callbackEventMap[i]->eventType) != 0)) {
44             HDF_LOGI("%s: send event = %u, ifName = %s", __FUNCTION__, event, ifName);
45             g_callbackEventMap[i]->onRecFunc(event, data, ifName);
46         }
47     }
48 }
49 
WifiRegisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)50 int32_t WifiRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
51 {
52     uint32_t i;
53     struct CallbackEvent *callbackEvent = NULL;
54 
55     if (onRecFunc == NULL || ifName == NULL) {
56         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
57         return RET_CODE_INVALID_PARAM;
58     }
59     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
60         if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
61             (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
62             HDF_LOGI("%s the onRecFunc has been registered!", __FUNCTION__);
63             return RET_CODE_SUCCESS;
64         }
65     }
66     callbackEvent = (struct CallbackEvent *)malloc(sizeof(struct CallbackEvent));
67     if (callbackEvent == NULL) {
68         HDF_LOGE("%s fail: malloc fail!", __FUNCTION__);
69         return RET_CODE_FAILURE;
70     }
71     callbackEvent->eventType = eventType;
72     if (strcpy_s(callbackEvent->ifName, IFNAMSIZ, ifName) != RET_CODE_SUCCESS) {
73         free(callbackEvent);
74         HDF_LOGE("%s: ifName strcpy_s fail", __FUNCTION__);
75         return RET_CODE_FAILURE;
76     }
77     callbackEvent->onRecFunc = onRecFunc;
78     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
79         if (g_callbackEventMap[i] == NULL) {
80             g_callbackEventMap[i] = callbackEvent;
81             HDF_LOGD("%s: WifiRegisterEventCallback successful", __FUNCTION__);
82             return RET_CODE_SUCCESS;
83         }
84     }
85     free(callbackEvent);
86     HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
87     return RET_CODE_FAILURE;
88 }
89 
WifiUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)90 void WifiUnregisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
91 {
92     uint32_t i;
93 
94     if (onRecFunc == NULL || ifName == NULL) {
95         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
96         return;
97     }
98     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
99         if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
100             (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
101             g_callbackEventMap[i]->onRecFunc = NULL;
102             free(g_callbackEventMap[i]);
103             g_callbackEventMap[i] = NULL;
104             return;
105         }
106     }
107 }
108 
Hid2dEventReport(const char * ifName,const uint8_t * msg,uint32_t msgLen)109 void Hid2dEventReport(const char *ifName, const uint8_t *msg, uint32_t msgLen)
110 {
111     uint32_t i;
112 
113     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
114         if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0)) {
115             HDF_LOGI("%s: Hid2dEventReport ifName = %s", __FUNCTION__, ifName);
116             g_hid2dEventMap[i]->func(msg, msgLen);
117         }
118     }
119 }
120 
WifiRegisterHid2dCallback(Hid2dCallback func,const char * ifName)121 int32_t WifiRegisterHid2dCallback(Hid2dCallback func, const char *ifName)
122 {
123     struct Hid2dEvent *event = NULL;
124     uint32_t i;
125 
126     if (func == NULL || ifName == NULL) {
127         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
128         return RET_CODE_INVALID_PARAM;
129     }
130     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
131         if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0) &&
132             g_hid2dEventMap[i]->func == func) {
133             HDF_LOGI("%s the callback function has been registered!", __FUNCTION__);
134             return RET_CODE_SUCCESS;
135         }
136     }
137     event = (struct Hid2dEvent *)OsalMemCalloc(sizeof(struct Hid2dEvent));
138     if (event == NULL) {
139         HDF_LOGE("%s fail: OsalMemCalloc fail!", __FUNCTION__);
140         return RET_CODE_FAILURE;
141     }
142     do {
143         if (strcpy_s(event->ifName, IFNAMSIZ + 1, ifName) != RET_CODE_SUCCESS) {
144             HDF_LOGE("%s: ifName strcpy_s fail", __FUNCTION__);
145             break;
146         }
147         event->func = func;
148         for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
149             if (g_hid2dEventMap[i] == NULL) {
150                 g_hid2dEventMap[i] = event;
151                 HDF_LOGD("%s: WifiRegisterHid2dCallback successful", __FUNCTION__);
152                 return RET_CODE_SUCCESS;
153             }
154         }
155     } while (0);
156 
157     OsalMemFree(event);
158     HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
159     return RET_CODE_FAILURE;
160 }
161 
WifiUnregisterHid2dCallback(Hid2dCallback func,const char * ifName)162 void WifiUnregisterHid2dCallback(Hid2dCallback func, const char *ifName)
163 {
164     uint32_t i;
165 
166     if (func == NULL || ifName == NULL) {
167         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
168         return;
169     }
170     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
171         if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0) &&
172             g_hid2dEventMap[i]->func == func) {
173             g_hid2dEventMap[i]->func = NULL;
174             OsalMemFree(g_hid2dEventMap[i]);
175             g_hid2dEventMap[i] = NULL;
176             return;
177         }
178     }
179 }
180 
FreeScanResult(WifiScanResult * res)181 void FreeScanResult(WifiScanResult *res)
182 {
183     if (res == NULL) {
184         return;
185     }
186     if (res->bssid != NULL) {
187         OsalMemFree(res->bssid);
188     }
189     if (res->ie != NULL) {
190         OsalMemFree(res->ie);
191     }
192     if (res->beaconIe != NULL) {
193         OsalMemFree(res->beaconIe);
194     }
195 }
196 
FreeScanResults(WifiScanResults * res)197 void FreeScanResults(WifiScanResults *res)
198 {
199     uint32_t i;
200     if (res == NULL) {
201         return;
202     }
203     for (i = 0; i < res->num; i++) {
204         FreeScanResult(&res->scanResult[i]);
205     }
206 }
207 
208 #ifdef __cplusplus
209 #if __cplusplus
210 }
211 #endif
212 #endif
213