• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "hdf_log.h"
21 #include "securec.h"
22 #include "wpa_client.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 WpaCallbackEvent *g_wpaCallbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
36 static pthread_mutex_t g_wpaCallbackMutex;
37 
38 
WpaRegisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)39 int32_t WpaRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
40 {
41     uint32_t i;
42     struct WpaCallbackEvent *callbackEvent = NULL;
43 
44     if (onRecFunc == NULL || ifName == NULL) {
45         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
46         return -1;
47     }
48     pthread_mutex_lock(&g_wpaCallbackMutex);
49     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
50         if (g_wpaCallbackEventMap[i] != NULL  &&(strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0)
51             && g_wpaCallbackEventMap[i]->onRecFunc == onRecFunc) {
52             HDF_LOGI("%s the onRecFunc has been registered!", __FUNCTION__);
53             pthread_mutex_unlock(&g_wpaCallbackMutex);
54             return 0;
55         }
56     }
57     pthread_mutex_unlock(&g_wpaCallbackMutex);
58     callbackEvent = (struct WpaCallbackEvent *)malloc(sizeof(struct WpaCallbackEvent));
59     if (callbackEvent == NULL) {
60         HDF_LOGE("%s fail: malloc fail!", __FUNCTION__);
61         return -1;
62     }
63     callbackEvent->eventType = eventType;
64     if (strcpy_s(callbackEvent->ifName, IFNAMSIZ, ifName) != 0) {
65         free(callbackEvent);
66         HDF_LOGE("%s: ifName strcpy_s fail", __FUNCTION__);
67         return -1;
68     }
69     callbackEvent->onRecFunc = onRecFunc;
70     pthread_mutex_lock(&g_wpaCallbackMutex);
71     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
72         if (g_wpaCallbackEventMap[i] == NULL) {
73             g_wpaCallbackEventMap[i] = callbackEvent;
74             HDF_LOGD("%s: WifiRegisterEventCallback successful", __FUNCTION__);
75             HDF_LOGD("%s: callbackEvent->eventType =%d ", __FUNCTION__, callbackEvent->eventType);
76             pthread_mutex_unlock(&g_wpaCallbackMutex);
77             return 0;
78         }
79     }
80     pthread_mutex_unlock(&g_wpaCallbackMutex);
81     free(callbackEvent);
82     HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
83     return -1;
84 }
85 
WpaUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)86 int32_t WpaUnregisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
87 {
88     uint32_t i;
89 
90     if (onRecFunc == NULL || ifName == NULL) {
91         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
92         return HDF_FAILURE;
93     }
94     pthread_mutex_lock(&g_wpaCallbackMutex);
95     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
96         if (g_wpaCallbackEventMap[i] != NULL && g_wpaCallbackEventMap[i]->eventType == eventType &&
97             (strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0) &&
98             g_wpaCallbackEventMap[i]->onRecFunc == onRecFunc) {
99             g_wpaCallbackEventMap[i]->onRecFunc = NULL;
100             free(g_wpaCallbackEventMap[i]);
101             g_wpaCallbackEventMap[i] = NULL;
102             pthread_mutex_unlock(&g_wpaCallbackMutex);
103             return HDF_SUCCESS;
104         }
105     }
106     pthread_mutex_unlock(&g_wpaCallbackMutex);
107     return HDF_FAILURE;
108 }
109 
WpaEventReport(const char * ifName,uint32_t event,void * data)110 void WpaEventReport(const char *ifName, uint32_t event, void *data)
111 {
112     uint32_t i;
113     OnReceiveFunc callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
114     pthread_mutex_lock(&g_wpaCallbackMutex);
115 
116     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
117         if (g_wpaCallbackEventMap[i] != NULL && ((strstr(ifName, g_wpaCallbackEventMap[i]->ifName))
118             || (strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0)) &&
119             (((1 << event) & g_wpaCallbackEventMap[i]->eventType) != 0)) {
120             HDF_LOGI("%s: send event = %u, ifName = %s", __FUNCTION__, event, ifName);
121             callbackEventMap[i] = g_wpaCallbackEventMap[i]->onRecFunc;
122         }
123     }
124     pthread_mutex_unlock(&g_wpaCallbackMutex);
125     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
126         if (callbackEventMap[i] != NULL) {
127             HDF_LOGI("%s: call event = %u, ifName = %s", __FUNCTION__, event, ifName);
128             callbackEventMap[i](event, data, ifName);
129         }
130     }
131 }
132