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 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_mutex_lock(&g_hostapdCallbackMutex);
53 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
54 if (g_hostapdCallbackEventMap[i] != NULL && (strcmp(g_hostapdCallbackEventMap[i]->ifName, ifName) == 0)
55 && g_hostapdCallbackEventMap[i]->onRecFunc == onRecFunc) {
56 HDF_LOGI("%s the onRecFunc has been registered!", __FUNCTION__);
57 pthread_mutex_unlock(&g_hostapdCallbackMutex);
58 return 0;
59 }
60 }
61 pthread_mutex_unlock(&g_hostapdCallbackMutex);
62 callbackEvent = (struct HostapdCallbackEvent *)malloc(sizeof(struct HostapdCallbackEvent));
63 if (callbackEvent == NULL) {
64 HDF_LOGE("%s fail: malloc fail!", __FUNCTION__);
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 return -1;
72 }
73 callbackEvent->ifName[ifNameLen] = '\0';
74 callbackEvent->onRecFunc = onRecFunc;
75 pthread_mutex_lock(&g_hostapdCallbackMutex);
76 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
77 if (g_hostapdCallbackEventMap[i] == NULL) {
78 g_hostapdCallbackEventMap[i] = callbackEvent;
79 HDF_LOGD("%s: WifiRegisterEventCallback successful", __FUNCTION__);
80 HDF_LOGD("%s: callbackEvent->eventType =%d ", __FUNCTION__, callbackEvent->eventType);
81 pthread_mutex_unlock(&g_hostapdCallbackMutex);
82 return 0;
83 }
84 }
85 pthread_mutex_unlock(&g_hostapdCallbackMutex);
86 free(callbackEvent);
87 HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
88 return -1;
89 }
90
HostapdUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)91 int32_t HostapdUnregisterEventCallback(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_mutex_lock(&g_hostapdCallbackMutex);
100 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
101 if (g_hostapdCallbackEventMap[i] != NULL && g_hostapdCallbackEventMap[i]->eventType == eventType &&
102 (strcmp(g_hostapdCallbackEventMap[i]->ifName, ifName) == 0) &&
103 g_hostapdCallbackEventMap[i]->onRecFunc == onRecFunc) {
104 g_hostapdCallbackEventMap[i]->onRecFunc = NULL;
105 free(g_hostapdCallbackEventMap[i]);
106 g_hostapdCallbackEventMap[i] = NULL;
107 pthread_mutex_unlock(&g_hostapdCallbackMutex);
108 return HDF_SUCCESS;
109 }
110 }
111 pthread_mutex_unlock(&g_hostapdCallbackMutex);
112 return HDF_FAILURE;
113 }
114
HostapdEventReport(const char * ifName,uint32_t event,void * data)115 void HostapdEventReport(const char *ifName, uint32_t event, void *data)
116 {
117 uint32_t i;
118
119 OnReceiveFunc callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
120 pthread_mutex_lock(&g_hostapdCallbackMutex);
121 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
122 if (g_hostapdCallbackEventMap[i] != NULL && (strcmp(g_hostapdCallbackEventMap[i]->ifName, ifName) == 0) &&
123 (((1 << event) & g_hostapdCallbackEventMap[i]->eventType) != 0)) {
124 HDF_LOGI("%s: send event = %u, ifName = %s", __FUNCTION__, event, ifName);
125 callbackEventMap[i] = g_hostapdCallbackEventMap[i]->onRecFunc;
126 }
127 }
128 pthread_mutex_unlock(&g_hostapdCallbackMutex);
129 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
130 if (callbackEventMap[i] != NULL) {
131 HDF_LOGI("%s: call event = %u, ifName = %s", __FUNCTION__, event, ifName);
132 callbackEventMap[i](event, data, ifName);
133 }
134 }
135 }
136