1 /*
2 * Copyright (c) 2025 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 "ethernet_eap_client.h"
23
24 #include "v1_0/iethernet.h"
25 #include "v1_0/iethernet_callback.h"
26
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36 #define LOG_TAG "EthernetEapClient"
37 #ifdef LOG_DOMAIN
38 #undef LOG_DOMAIN
39 #endif
40 #define LOG_DOMAIN 0xD0015b0
41
42 #define MAX_CALL_BACK_COUNT 10
43 static struct EthEapCallback *g_ethEapCallbackMap[MAX_CALL_BACK_COUNT] = {NULL};
44 static pthread_rwlock_t g_eapCallbackMutex = PTHREAD_RWLOCK_INITIALIZER;
45
EthEapClientRegisterCallback(struct IEthernetCallback * callback,const char * ifName)46 int32_t EthEapClientRegisterCallback(struct IEthernetCallback* callback, const char *ifName)
47 {
48 uint32_t i;
49 int ifNameLen = 0;
50 if (ifName != NULL) {
51 ifNameLen = strnlen(ifName, IFNAMSIZE + 1);
52 }
53 if (callback == NULL || ifName == NULL || ifNameLen == (IFNAMSIZE + 1)) {
54 HDF_LOGE("%s: input param invalid", __FUNCTION__);
55 return HDF_FAILURE;
56 }
57 pthread_rwlock_wrlock(&g_eapCallbackMutex);
58 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
59 if (g_ethEapCallbackMap[i] != NULL &&(strcmp(g_ethEapCallbackMap[i]->ifName, ifName) == 0)
60 && g_ethEapCallbackMap[i]->callback == callback) {
61 HDF_LOGI("%s callback has been registered!", __FUNCTION__);
62 pthread_rwlock_unlock(&g_eapCallbackMutex);
63 return HDF_SUCCESS;
64 }
65 }
66 struct EthEapCallback *ethEapCallback = (struct EthEapCallback *)malloc(sizeof(struct EthEapCallback));
67 if (ethEapCallback == NULL) {
68 HDF_LOGE("%s malloc fail", __FUNCTION__);
69 pthread_rwlock_unlock(&g_eapCallbackMutex);
70 return HDF_FAILURE;
71 }
72 if (memcpy_s(ethEapCallback->ifName, IFNAMSIZE, ifName, ifNameLen) != 0) {
73 free(ethEapCallback);
74 HDF_LOGE("%s ifName memcpy_s fail", __FUNCTION__);
75 pthread_rwlock_unlock(&g_eapCallbackMutex);
76 return HDF_FAILURE;
77 }
78 ethEapCallback->ifName[ifNameLen] = '\0';
79 ethEapCallback->callback = callback;
80 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
81 if (g_ethEapCallbackMap[i] == NULL) {
82 g_ethEapCallbackMap[i] = ethEapCallback;
83 HDF_LOGI("%s success", __FUNCTION__);
84 pthread_rwlock_unlock(&g_eapCallbackMutex);
85 return HDF_SUCCESS;
86 }
87 }
88 pthread_rwlock_unlock(&g_eapCallbackMutex);
89 free(callback);
90 HDF_LOGE("%s fail", __FUNCTION__);
91 return HDF_FAILURE;
92 }
93
EthEapClientUnregisterCallback(struct IEthernetCallback * callback,const char * ifName)94 int32_t EthEapClientUnregisterCallback(struct IEthernetCallback* callback, const char *ifName)
95 {
96 uint32_t i;
97 if (callback == NULL || ifName == NULL) {
98 HDF_LOGE("%s: input param invalid", __FUNCTION__);
99 return HDF_FAILURE;
100 }
101 pthread_rwlock_wrlock(&g_eapCallbackMutex);
102 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
103 if (g_ethEapCallbackMap[i] != NULL && (strcmp(g_ethEapCallbackMap[i]->ifName, ifName) == 0) &&
104 g_ethEapCallbackMap[i]->callback == callback) {
105 g_ethEapCallbackMap[i]->callback = NULL;
106 free(g_ethEapCallbackMap[i]);
107 g_ethEapCallbackMap[i] = NULL;
108 pthread_rwlock_unlock(&g_eapCallbackMutex);
109 return HDF_SUCCESS;
110 }
111 }
112 pthread_rwlock_unlock(&g_eapCallbackMutex);
113 return HDF_FAILURE;
114 }
115
EthEapClientReleaseCallback(void)116 void EthEapClientReleaseCallback(void)
117 {
118 pthread_rwlock_wrlock(&g_eapCallbackMutex);
119 for (uint32_t i = 0; i < MAX_CALL_BACK_COUNT; i++) {
120 if (g_ethEapCallbackMap[i] != NULL) {
121 g_ethEapCallbackMap[i]->callback = NULL;
122 free(g_ethEapCallbackMap[i]);
123 g_ethEapCallbackMap[i] = NULL;
124 break;
125 }
126 }
127 pthread_rwlock_unlock(&g_eapCallbackMutex);
128 }
129
EthEapClientEventReport(const char * ifName,const char * data)130 void EthEapClientEventReport(const char *ifName, const char *data)
131 {
132 uint32_t i;
133 struct IEthernetCallback* callbackEventMap[MAX_CALL_BACK_COUNT] = { NULL };
134 pthread_rwlock_rdlock(&g_eapCallbackMutex);
135 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
136 if (g_ethEapCallbackMap[i] != NULL && ((strstr(ifName, g_ethEapCallbackMap[i]->ifName))
137 || (strcmp(g_ethEapCallbackMap[i]->ifName, ifName) == 0))) {
138 HDF_LOGI("%s: EapEventReport ifName = %s", __FUNCTION__, ifName);
139 callbackEventMap[i] = g_ethEapCallbackMap[i]->callback;
140 }
141 }
142 for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
143 if (callbackEventMap[i] != NULL) {
144 callbackEventMap[i]->OnEapEventNotify(callbackEventMap[i], ifName, data);
145 }
146 }
147 pthread_rwlock_unlock(&g_eapCallbackMutex);
148 }
149
150 #ifdef __cplusplus
151 }
152 #endif
153