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
21 #include "hdf_log.h"
22 #include "securec.h"
23 #include "hostapd_client.h"
24
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30
31 #ifndef EOK
32 #define EOK 0
33 #endif
34
35 #define MAX_CALL_BACK_COUNT 10
36 static struct HostapdCallbackEvent *g_hostapdCallbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
37 static pthread_mutex_t g_hostapdCallbackMutex = PTHREAD_MUTEX_INITIALIZER;
38
HostapdRegisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)39 int32_t HostapdRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
40 {
41 uint32_t i;
42 struct HostapdCallbackEvent *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_hostapdCallbackMutex);
49 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
50 if (g_hostapdCallbackEventMap[i] != NULL && (strcmp(g_hostapdCallbackEventMap[i]->ifName, ifName) == 0)
51 && g_hostapdCallbackEventMap[i]->onRecFunc == onRecFunc) {
52 HDF_LOGI("%s the onRecFunc has been registered!", __FUNCTION__);
53 pthread_mutex_unlock(&g_hostapdCallbackMutex);
54 return 0;
55 }
56 }
57 pthread_mutex_unlock(&g_hostapdCallbackMutex);
58 callbackEvent = (struct HostapdCallbackEvent *)malloc(sizeof(struct HostapdCallbackEvent));
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_hostapdCallbackMutex);
71 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
72 if (g_hostapdCallbackEventMap[i] == NULL) {
73 g_hostapdCallbackEventMap[i] = callbackEvent;
74 HDF_LOGD("%s: WifiRegisterEventCallback successful", __FUNCTION__);
75 HDF_LOGD("%s: callbackEvent->eventType =%d ", __FUNCTION__, callbackEvent->eventType);
76 pthread_mutex_unlock(&g_hostapdCallbackMutex);
77 return 0;
78 }
79 }
80 pthread_mutex_unlock(&g_hostapdCallbackMutex);
81 free(callbackEvent);
82 HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
83 return -1;
84 }
85
HostapdUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)86 int32_t HostapdUnregisterEventCallback(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_hostapdCallbackMutex);
95 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
96 if (g_hostapdCallbackEventMap[i] != NULL && g_hostapdCallbackEventMap[i]->eventType == eventType &&
97 (strcmp(g_hostapdCallbackEventMap[i]->ifName, ifName) == 0) &&
98 g_hostapdCallbackEventMap[i]->onRecFunc == onRecFunc) {
99 g_hostapdCallbackEventMap[i]->onRecFunc = NULL;
100 free(g_hostapdCallbackEventMap[i]);
101 g_hostapdCallbackEventMap[i] = NULL;
102 pthread_mutex_unlock(&g_hostapdCallbackMutex);
103 return HDF_SUCCESS;
104 }
105 }
106 pthread_mutex_unlock(&g_hostapdCallbackMutex);
107 return HDF_FAILURE;
108 }
109
HostapdEventReport(const char * ifName,uint32_t event,void * data)110 void HostapdEventReport(const char *ifName, uint32_t event, void *data)
111 {
112 uint32_t i;
113
114 OnReceiveFunc callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
115 pthread_mutex_lock(&g_hostapdCallbackMutex);
116 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
117 if (g_hostapdCallbackEventMap[i] != NULL && (strcmp(g_hostapdCallbackEventMap[i]->ifName, ifName) == 0) &&
118 (((1 << event) & g_hostapdCallbackEventMap[i]->eventType) != 0)) {
119 HDF_LOGI("%s: send event = %u, ifName = %s", __FUNCTION__, event, ifName);
120 callbackEventMap[i] = g_hostapdCallbackEventMap[i]->onRecFunc;
121 }
122 }
123 pthread_mutex_unlock(&g_hostapdCallbackMutex);
124 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
125 if (callbackEventMap[i] != NULL) {
126 HDF_LOGI("%s: call event = %u, ifName = %s", __FUNCTION__, event, ifName);
127 callbackEventMap[i](event, data, ifName);
128 }
129 }
130 }
131