• 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_rwlock_t g_wpaCallbackMutex = PTHREAD_RWLOCK_INITIALIZER;
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     int ifNameLen = 0;
44 
45     if (ifName != NULL) {
46         ifNameLen = (int)strnlen(ifName, IFNAMSIZ + 1);
47     }
48     if (onRecFunc == NULL || ifName == NULL || ifNameLen == (IFNAMSIZ + 1)) {
49         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
50         return -1;
51     }
52     pthread_rwlock_wrlock(&g_wpaCallbackMutex);
53     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
54         if (g_wpaCallbackEventMap[i] != NULL  &&(strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0)
55             && g_wpaCallbackEventMap[i]->onRecFunc == onRecFunc) {
56             HDF_LOGI("%s the onRecFunc has been registered!", __FUNCTION__);
57             pthread_rwlock_unlock(&g_wpaCallbackMutex);
58             return 0;
59         }
60     }
61     callbackEvent = (struct WpaCallbackEvent *)malloc(sizeof(struct WpaCallbackEvent));
62     if (callbackEvent == NULL) {
63         HDF_LOGE("%s fail: malloc fail!", __FUNCTION__);
64         pthread_rwlock_unlock(&g_wpaCallbackMutex);
65         return -1;
66     }
67     callbackEvent->eventType = eventType;
68     if (memcpy_s(callbackEvent->ifName, IFNAMSIZ, ifName, ifNameLen) != 0) {
69         free(callbackEvent);
70         HDF_LOGE("%s: ifName memcpy_s fail", __FUNCTION__);
71         pthread_rwlock_unlock(&g_wpaCallbackMutex);
72         return -1;
73     }
74     callbackEvent->ifName[ifNameLen] = '\0';
75     callbackEvent->onRecFunc = onRecFunc;
76     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
77         if (g_wpaCallbackEventMap[i] == NULL) {
78             g_wpaCallbackEventMap[i] = callbackEvent;
79             HDF_LOGD("%s: WifiRegisterEventCallback successful", __FUNCTION__);
80             HDF_LOGD("%s: callbackEvent->eventType =%d ", __FUNCTION__, callbackEvent->eventType);
81             pthread_rwlock_unlock(&g_wpaCallbackMutex);
82             return 0;
83         }
84     }
85     pthread_rwlock_unlock(&g_wpaCallbackMutex);
86     free(callbackEvent);
87     HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
88     return -1;
89 }
90 
WpaUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)91 int32_t WpaUnregisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
92 {
93     uint32_t i;
94 
95     if (onRecFunc == NULL || ifName == NULL) {
96         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
97         return HDF_FAILURE;
98     }
99     pthread_rwlock_wrlock(&g_wpaCallbackMutex);
100     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
101         if (g_wpaCallbackEventMap[i] != NULL && g_wpaCallbackEventMap[i]->eventType == eventType &&
102             (strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0) &&
103             g_wpaCallbackEventMap[i]->onRecFunc == onRecFunc) {
104             g_wpaCallbackEventMap[i]->onRecFunc = NULL;
105             free(g_wpaCallbackEventMap[i]);
106             g_wpaCallbackEventMap[i] = NULL;
107             pthread_rwlock_unlock(&g_wpaCallbackMutex);
108             return HDF_SUCCESS;
109         }
110     }
111     pthread_rwlock_unlock(&g_wpaCallbackMutex);
112     return HDF_FAILURE;
113 }
114 
ReleaseEventCallback(void)115 void ReleaseEventCallback(void)
116 {
117     pthread_rwlock_wrlock(&g_wpaCallbackMutex);
118     for (uint32_t i = 0; i < MAX_CALL_BACK_COUNT; i++) {
119         if (g_wpaCallbackEventMap[i] != NULL) {
120             g_wpaCallbackEventMap[i]->onRecFunc = NULL;
121             free(g_wpaCallbackEventMap[i]);
122             g_wpaCallbackEventMap[i] = NULL;
123             break;
124         }
125     }
126     pthread_rwlock_unlock(&g_wpaCallbackMutex);
127 }
128 
RemoveIfaceCallback(const char * ifName)129 static void RemoveIfaceCallback(const char *ifName)
130 {
131     if (ifName == NULL) {
132         return;
133     }
134     for (int i = 0; i < MAX_CALL_BACK_COUNT; i++) {
135         if (g_wpaCallbackEventMap[i] != NULL && (strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0)) {
136             g_wpaCallbackEventMap[i]->onRecFunc = NULL;
137             free(g_wpaCallbackEventMap[i]);
138             g_wpaCallbackEventMap[i] = NULL;
139         }
140     }
141 }
142 
WpaEventReport(const char * ifName,uint32_t event,void * data)143 void WpaEventReport(const char *ifName, uint32_t event, void *data)
144 {
145     uint32_t i;
146     if (event == WPA_EVENT_IFACE_REMOVED) {
147         RemoveIfaceCallback(ifName);
148         return;
149     }
150     OnReceiveFunc callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
151 
152     pthread_rwlock_rdlock(&g_wpaCallbackMutex);
153     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
154         if (g_wpaCallbackEventMap[i] != NULL && ((strstr(ifName, g_wpaCallbackEventMap[i]->ifName))
155             || (strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0)) &&
156             (((1 << event) & g_wpaCallbackEventMap[i]->eventType) != 0)) {
157             HDF_LOGI("%s: send event = %u, ifName = %s", __FUNCTION__, event, ifName);
158             callbackEventMap[i] = g_wpaCallbackEventMap[i]->onRecFunc;
159         }
160     }
161     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
162         if (callbackEventMap[i] != NULL) {
163             HDF_LOGI("%s: call event = %u, ifName = %s", __FUNCTION__, event, ifName);
164             callbackEventMap[i](event, data, ifName);
165         }
166     }
167     pthread_rwlock_unlock(&g_wpaCallbackMutex);
168 }
169