• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "wifi_common_cmd.h"
19 #include "hilog/log.h"
20 #include "securec.h"
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 #ifndef EOK
29 #define EOK 0
30 #endif
31 
32 #define MAX_CALL_BACK_COUNT 10
33 static struct CallbackEvent *g_callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
34 
WifiEventReport(const char * ifName,uint32_t event,void * data)35 void WifiEventReport(const char *ifName, uint32_t event, void *data)
36 {
37     uint32_t i;
38 
39     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
40         if (g_callbackEventMap[i] != NULL && (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) &&
41             (((1 << event) & g_callbackEventMap[i]->eventType) != 0)) {
42             HILOG_INFO(LOG_DOMAIN, "%s: WifiEventReport send event = %d, ifName = %s",
43                 __FUNCTION__, event, ifName);
44             g_callbackEventMap[i]->onRecFunc(event, data, ifName);
45         }
46     }
47 }
48 
WifiRegisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)49 int32_t WifiRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
50 {
51     uint32_t i;
52     struct CallbackEvent *callbackEvent = NULL;
53 
54     if (onRecFunc == NULL || ifName == NULL) {
55         HILOG_ERROR(LOG_DOMAIN, "%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
56         return RET_CODE_INVALID_PARAM;
57     }
58     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
59         if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
60             (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
61             HILOG_INFO(LOG_DOMAIN, "%s the onRecFunc has been registered!", __FUNCTION__);
62             return RET_CODE_SUCCESS;
63         }
64     }
65     callbackEvent = (struct CallbackEvent *)malloc(sizeof(struct CallbackEvent));
66     if (callbackEvent == NULL) {
67         HILOG_ERROR(LOG_DOMAIN, "%s fail: malloc fail!", __FUNCTION__);
68         return RET_CODE_FAILURE;
69     }
70     callbackEvent->eventType = eventType;
71     if (strcpy_s(callbackEvent->ifName, IFNAMSIZ, ifName) != RET_CODE_SUCCESS) {
72         free(callbackEvent);
73         return RET_CODE_FAILURE;
74     }
75     callbackEvent->onRecFunc = onRecFunc;
76     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
77         if (g_callbackEventMap[i] == NULL) {
78             g_callbackEventMap[i] = callbackEvent;
79             return RET_CODE_SUCCESS;
80         }
81     }
82     free(callbackEvent);
83     HILOG_ERROR(LOG_DOMAIN, "%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
84     return RET_CODE_FAILURE;
85 }
86 
WifiUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)87 void WifiUnregisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
88 {
89     uint32_t i;
90 
91     if (onRecFunc == NULL || ifName == NULL) {
92         HILOG_ERROR(LOG_DOMAIN, "%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
93         return;
94     }
95     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
96         if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
97             (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
98             g_callbackEventMap[i]->onRecFunc = NULL;
99             free(g_callbackEventMap[i]);
100             g_callbackEventMap[i] = NULL;
101             return;
102         }
103     }
104 }
105 
106 #ifdef __cplusplus
107 #if __cplusplus
108 }
109 #endif
110 #endif
111