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
44 if (onRecFunc == NULL || ifName == NULL) {
45 HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
46 return -1;
47 }
48 pthread_rwlock_wrlock(&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_rwlock_unlock(&g_wpaCallbackMutex);
54 return 0;
55 }
56 }
57 callbackEvent = (struct WpaCallbackEvent *)malloc(sizeof(struct WpaCallbackEvent));
58 if (callbackEvent == NULL) {
59 HDF_LOGE("%s fail: malloc fail!", __FUNCTION__);
60 pthread_rwlock_unlock(&g_wpaCallbackMutex);
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 pthread_rwlock_unlock(&g_wpaCallbackMutex);
68 return -1;
69 }
70 callbackEvent->onRecFunc = onRecFunc;
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_rwlock_unlock(&g_wpaCallbackMutex);
77 return 0;
78 }
79 }
80 pthread_rwlock_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_rwlock_wrlock(&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_rwlock_unlock(&g_wpaCallbackMutex);
103 return HDF_SUCCESS;
104 }
105 }
106 pthread_rwlock_unlock(&g_wpaCallbackMutex);
107 return HDF_FAILURE;
108 }
109
ReleaseEventCallback(void)110 void ReleaseEventCallback(void)
111 {
112 pthread_rwlock_wrlock(&g_wpaCallbackMutex);
113 for (uint32_t i = 0; i < MAX_CALL_BACK_COUNT; i++) {
114 if (g_wpaCallbackEventMap[i] != NULL) {
115 g_wpaCallbackEventMap[i]->onRecFunc = NULL;
116 free(g_wpaCallbackEventMap[i]);
117 g_wpaCallbackEventMap[i] = NULL;
118 break;
119 }
120 }
121 pthread_rwlock_unlock(&g_wpaCallbackMutex);
122 }
123
WpaEventReport(const char * ifName,uint32_t event,void * data)124 void WpaEventReport(const char *ifName, uint32_t event, void *data)
125 {
126 uint32_t i;
127 OnReceiveFunc callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
128
129 pthread_rwlock_rdlock(&g_wpaCallbackMutex);
130 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
131 if (g_wpaCallbackEventMap[i] != NULL && ((strstr(ifName, g_wpaCallbackEventMap[i]->ifName))
132 || (strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0)) &&
133 (((1 << event) & g_wpaCallbackEventMap[i]->eventType) != 0)) {
134 HDF_LOGI("%s: send event = %u, ifName = %s", __FUNCTION__, event, ifName);
135 callbackEventMap[i] = g_wpaCallbackEventMap[i]->onRecFunc;
136 }
137 }
138 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
139 if (callbackEventMap[i] != NULL) {
140 HDF_LOGI("%s: call event = %u, ifName = %s", __FUNCTION__, event, ifName);
141 callbackEventMap[i](event, data, ifName);
142 }
143 }
144 pthread_rwlock_unlock(&g_wpaCallbackMutex);
145 }
146