• 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) {
41             printf("xsxs: g_callbackEventMap[i]->ifName=%s\n", g_callbackEventMap[i]->ifName);
42         }
43         if (g_callbackEventMap[i] != NULL && (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) &&
44             (((1 << event) & g_callbackEventMap[i]->eventType) != 0)) {
45             HILOG_INFO(LOG_DOMAIN, "%s: WifiEventReport send event = %d, ifName = %s",
46                 __FUNCTION__, event, ifName);
47             g_callbackEventMap[i]->onRecFunc(event, data, ifName);
48         }
49     }
50 }
51 
WifiRegisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)52 int32_t WifiRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
53 {
54     uint32_t i;
55     struct CallbackEvent *callbackEvent = NULL;
56 
57     if (onRecFunc == NULL || ifName == NULL) {
58         HILOG_ERROR(LOG_DOMAIN, "%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
59         return RET_CODE_INVALID_PARAM;
60     }
61     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
62         if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
63             (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
64             HILOG_INFO(LOG_DOMAIN, "%s the onRecFunc has been registered!", __FUNCTION__);
65             return RET_CODE_SUCCESS;
66         }
67     }
68     callbackEvent = (struct CallbackEvent *)malloc(sizeof(struct CallbackEvent));
69     if (callbackEvent == NULL) {
70         HILOG_ERROR(LOG_DOMAIN, "%s fail: malloc fail!", __FUNCTION__);
71         return RET_CODE_FAILURE;
72     }
73     callbackEvent->eventType = eventType;
74     if (strcpy_s(callbackEvent->ifName, IFNAMSIZ, ifName) != RET_CODE_SUCCESS) {
75         free(callbackEvent);
76         return RET_CODE_FAILURE;
77     }
78     callbackEvent->onRecFunc = onRecFunc;
79     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
80         if (g_callbackEventMap[i] == NULL) {
81             g_callbackEventMap[i] = callbackEvent;
82             return RET_CODE_SUCCESS;
83         }
84     }
85     free(callbackEvent);
86     HILOG_ERROR(LOG_DOMAIN, "%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
87     return RET_CODE_FAILURE;
88 }
89 
WifiUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)90 void WifiUnregisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
91 {
92     uint32_t i;
93 
94     if (onRecFunc == NULL || ifName == NULL) {
95         HILOG_ERROR(LOG_DOMAIN, "%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
96         return;
97     }
98     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
99         if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
100             (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
101             g_callbackEventMap[i]->onRecFunc = NULL;
102             free(g_callbackEventMap[i]);
103             g_callbackEventMap[i] = NULL;
104             return;
105         }
106     }
107 }
108 
109 #ifdef __cplusplus
110 #if __cplusplus
111 }
112 #endif
113 #endif
114