1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <string.h>
17094332d3Sopenharmony_ci#include <stdlib.h>
18094332d3Sopenharmony_ci#include <osal_mem.h>
19094332d3Sopenharmony_ci#include <pthread.h>
20094332d3Sopenharmony_ci#include "hdf_log.h"
21094332d3Sopenharmony_ci#include "securec.h"
22094332d3Sopenharmony_ci#include "wpa_client.h"
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ci#ifdef __cplusplus
25094332d3Sopenharmony_ci#if __cplusplus
26094332d3Sopenharmony_ciextern "C" {
27094332d3Sopenharmony_ci#endif
28094332d3Sopenharmony_ci#endif
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ci#ifndef EOK
31094332d3Sopenharmony_ci#define EOK 0
32094332d3Sopenharmony_ci#endif
33094332d3Sopenharmony_ci
34094332d3Sopenharmony_ci#define MAX_CALL_BACK_COUNT 10
35094332d3Sopenharmony_cistatic struct WpaCallbackEvent *g_wpaCallbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
36094332d3Sopenharmony_cistatic pthread_rwlock_t g_wpaCallbackMutex = PTHREAD_RWLOCK_INITIALIZER;
37094332d3Sopenharmony_ci
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_ciint32_t WpaRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
40094332d3Sopenharmony_ci{
41094332d3Sopenharmony_ci    uint32_t i;
42094332d3Sopenharmony_ci    struct WpaCallbackEvent *callbackEvent = NULL;
43094332d3Sopenharmony_ci
44094332d3Sopenharmony_ci    if (onRecFunc == NULL || ifName == NULL) {
45094332d3Sopenharmony_ci        HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
46094332d3Sopenharmony_ci        return -1;
47094332d3Sopenharmony_ci    }
48094332d3Sopenharmony_ci    pthread_rwlock_wrlock(&g_wpaCallbackMutex);
49094332d3Sopenharmony_ci    for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
50094332d3Sopenharmony_ci        if (g_wpaCallbackEventMap[i] != NULL  &&(strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0)
51094332d3Sopenharmony_ci            && g_wpaCallbackEventMap[i]->onRecFunc == onRecFunc) {
52094332d3Sopenharmony_ci            HDF_LOGI("%s the onRecFunc has been registered!", __FUNCTION__);
53094332d3Sopenharmony_ci            pthread_rwlock_unlock(&g_wpaCallbackMutex);
54094332d3Sopenharmony_ci            return 0;
55094332d3Sopenharmony_ci        }
56094332d3Sopenharmony_ci    }
57094332d3Sopenharmony_ci    callbackEvent = (struct WpaCallbackEvent *)malloc(sizeof(struct WpaCallbackEvent));
58094332d3Sopenharmony_ci    if (callbackEvent == NULL) {
59094332d3Sopenharmony_ci        HDF_LOGE("%s fail: malloc fail!", __FUNCTION__);
60094332d3Sopenharmony_ci        pthread_rwlock_unlock(&g_wpaCallbackMutex);
61094332d3Sopenharmony_ci        return -1;
62094332d3Sopenharmony_ci    }
63094332d3Sopenharmony_ci    callbackEvent->eventType = eventType;
64094332d3Sopenharmony_ci    if (strcpy_s(callbackEvent->ifName, IFNAMSIZ, ifName) != 0) {
65094332d3Sopenharmony_ci        free(callbackEvent);
66094332d3Sopenharmony_ci        HDF_LOGE("%s: ifName strcpy_s fail", __FUNCTION__);
67094332d3Sopenharmony_ci        pthread_rwlock_unlock(&g_wpaCallbackMutex);
68094332d3Sopenharmony_ci        return -1;
69094332d3Sopenharmony_ci    }
70094332d3Sopenharmony_ci    callbackEvent->onRecFunc = onRecFunc;
71094332d3Sopenharmony_ci    for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
72094332d3Sopenharmony_ci        if (g_wpaCallbackEventMap[i] == NULL) {
73094332d3Sopenharmony_ci            g_wpaCallbackEventMap[i] = callbackEvent;
74094332d3Sopenharmony_ci            HDF_LOGD("%s: WifiRegisterEventCallback successful", __FUNCTION__);
75094332d3Sopenharmony_ci            HDF_LOGD("%s: callbackEvent->eventType =%d ", __FUNCTION__, callbackEvent->eventType);
76094332d3Sopenharmony_ci            pthread_rwlock_unlock(&g_wpaCallbackMutex);
77094332d3Sopenharmony_ci            return 0;
78094332d3Sopenharmony_ci        }
79094332d3Sopenharmony_ci    }
80094332d3Sopenharmony_ci    pthread_rwlock_unlock(&g_wpaCallbackMutex);
81094332d3Sopenharmony_ci    free(callbackEvent);
82094332d3Sopenharmony_ci    HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
83094332d3Sopenharmony_ci    return -1;
84094332d3Sopenharmony_ci}
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ciint32_t WpaUnregisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
87094332d3Sopenharmony_ci{
88094332d3Sopenharmony_ci    uint32_t i;
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_ci    if (onRecFunc == NULL || ifName == NULL) {
91094332d3Sopenharmony_ci        HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
92094332d3Sopenharmony_ci        return HDF_FAILURE;
93094332d3Sopenharmony_ci    }
94094332d3Sopenharmony_ci    pthread_rwlock_wrlock(&g_wpaCallbackMutex);
95094332d3Sopenharmony_ci    for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
96094332d3Sopenharmony_ci        if (g_wpaCallbackEventMap[i] != NULL && g_wpaCallbackEventMap[i]->eventType == eventType &&
97094332d3Sopenharmony_ci            (strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0) &&
98094332d3Sopenharmony_ci            g_wpaCallbackEventMap[i]->onRecFunc == onRecFunc) {
99094332d3Sopenharmony_ci            g_wpaCallbackEventMap[i]->onRecFunc = NULL;
100094332d3Sopenharmony_ci            free(g_wpaCallbackEventMap[i]);
101094332d3Sopenharmony_ci            g_wpaCallbackEventMap[i] = NULL;
102094332d3Sopenharmony_ci            pthread_rwlock_unlock(&g_wpaCallbackMutex);
103094332d3Sopenharmony_ci            return HDF_SUCCESS;
104094332d3Sopenharmony_ci        }
105094332d3Sopenharmony_ci    }
106094332d3Sopenharmony_ci    pthread_rwlock_unlock(&g_wpaCallbackMutex);
107094332d3Sopenharmony_ci    return HDF_FAILURE;
108094332d3Sopenharmony_ci}
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_civoid ReleaseEventCallback(void)
111094332d3Sopenharmony_ci{
112094332d3Sopenharmony_ci    pthread_rwlock_wrlock(&g_wpaCallbackMutex);
113094332d3Sopenharmony_ci    for (uint32_t i = 0; i < MAX_CALL_BACK_COUNT; i++) {
114094332d3Sopenharmony_ci        if (g_wpaCallbackEventMap[i] != NULL) {
115094332d3Sopenharmony_ci            g_wpaCallbackEventMap[i]->onRecFunc = NULL;
116094332d3Sopenharmony_ci            free(g_wpaCallbackEventMap[i]);
117094332d3Sopenharmony_ci            g_wpaCallbackEventMap[i] = NULL;
118094332d3Sopenharmony_ci            break;
119094332d3Sopenharmony_ci        }
120094332d3Sopenharmony_ci    }
121094332d3Sopenharmony_ci    pthread_rwlock_unlock(&g_wpaCallbackMutex);
122094332d3Sopenharmony_ci}
123094332d3Sopenharmony_ci
124094332d3Sopenharmony_civoid WpaEventReport(const char *ifName, uint32_t event, void *data)
125094332d3Sopenharmony_ci{
126094332d3Sopenharmony_ci    uint32_t i;
127094332d3Sopenharmony_ci    OnReceiveFunc callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
128094332d3Sopenharmony_ci
129094332d3Sopenharmony_ci    pthread_rwlock_rdlock(&g_wpaCallbackMutex);
130094332d3Sopenharmony_ci    for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
131094332d3Sopenharmony_ci        if (g_wpaCallbackEventMap[i] != NULL && ((strstr(ifName, g_wpaCallbackEventMap[i]->ifName))
132094332d3Sopenharmony_ci            || (strcmp(g_wpaCallbackEventMap[i]->ifName, ifName) == 0)) &&
133094332d3Sopenharmony_ci            (((1 << event) & g_wpaCallbackEventMap[i]->eventType) != 0)) {
134094332d3Sopenharmony_ci            HDF_LOGI("%s: send event = %u, ifName = %s", __FUNCTION__, event, ifName);
135094332d3Sopenharmony_ci            callbackEventMap[i] = g_wpaCallbackEventMap[i]->onRecFunc;
136094332d3Sopenharmony_ci        }
137094332d3Sopenharmony_ci    }
138094332d3Sopenharmony_ci    for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
139094332d3Sopenharmony_ci        if (callbackEventMap[i] != NULL) {
140094332d3Sopenharmony_ci            HDF_LOGI("%s: call event = %u, ifName = %s", __FUNCTION__, event, ifName);
141094332d3Sopenharmony_ci            callbackEventMap[i](event, data, ifName);
142094332d3Sopenharmony_ci        }
143094332d3Sopenharmony_ci    }
144094332d3Sopenharmony_ci    pthread_rwlock_unlock(&g_wpaCallbackMutex);
145094332d3Sopenharmony_ci}
146