• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "wifi_common_cmd.h"
20 #include "hdf_log.h"
21 #include "securec.h"
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 #ifndef EOK
30 #define EOK 0
31 #endif
32 
33 #define MAX_CALL_BACK_COUNT 10
34 static struct CallbackEvent *g_callbackEventMap[MAX_CALL_BACK_COUNT] = {NULL};
35 static struct Hid2dEvent *g_hid2dEventMap[MAX_CALL_BACK_COUNT] = {NULL};
36 
WifiEventReport(const char * ifName,uint32_t event,void * data)37 void WifiEventReport(const char *ifName, uint32_t event, void *data)
38 {
39     uint32_t i;
40 
41     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
42         if (g_callbackEventMap[i] != NULL && (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) &&
43             (((1 << event) & g_callbackEventMap[i]->eventType) != 0)) {
44             HDF_LOGI("%s: WifiEventReport send event = %u, ifName = %s",
45                 __FUNCTION__, event, ifName);
46             g_callbackEventMap[i]->onRecFunc(event, data, ifName);
47         }
48     }
49 }
50 
WifiRegisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)51 int32_t WifiRegisterEventCallback(OnReceiveFunc onRecFunc, uint32_t eventType, const char *ifName)
52 {
53     uint32_t i;
54     struct CallbackEvent *callbackEvent = NULL;
55 
56     if (onRecFunc == NULL || ifName == NULL) {
57         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
58         return RET_CODE_INVALID_PARAM;
59     }
60     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
61         if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
62             (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
63             HDF_LOGI("%s the onRecFunc has been registered!", __FUNCTION__);
64             return RET_CODE_SUCCESS;
65         }
66     }
67     callbackEvent = (struct CallbackEvent *)malloc(sizeof(struct CallbackEvent));
68     if (callbackEvent == NULL) {
69         HDF_LOGE("%s fail: malloc fail!", __FUNCTION__);
70         return RET_CODE_FAILURE;
71     }
72     callbackEvent->eventType = eventType;
73     if (strcpy_s(callbackEvent->ifName, IFNAMSIZ, ifName) != RET_CODE_SUCCESS) {
74         free(callbackEvent);
75         HDF_LOGE("%s: ifName strcpy_s fail", __FUNCTION__);
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             HDF_LOGD("%s: WifiRegisterEventCallback successful", __FUNCTION__);
83             return RET_CODE_SUCCESS;
84         }
85     }
86     free(callbackEvent);
87     HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
88     return RET_CODE_FAILURE;
89 }
90 
WifiUnregisterEventCallback(OnReceiveFunc onRecFunc,uint32_t eventType,const char * ifName)91 void WifiUnregisterEventCallback(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;
98     }
99     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
100         if (g_callbackEventMap[i] != NULL && g_callbackEventMap[i]->eventType == eventType &&
101             (strcmp(g_callbackEventMap[i]->ifName, ifName) == 0) && g_callbackEventMap[i]->onRecFunc == onRecFunc) {
102             g_callbackEventMap[i]->onRecFunc = NULL;
103             free(g_callbackEventMap[i]);
104             g_callbackEventMap[i] = NULL;
105             return;
106         }
107     }
108 }
109 
Hid2dEventReport(const char * ifName,const uint8_t * msg,uint32_t msgLen)110 void Hid2dEventReport(const char *ifName, const uint8_t *msg, uint32_t msgLen)
111 {
112     uint32_t i;
113 
114     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
115         if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0)) {
116             HDF_LOGI("%s: Hid2dEventReport ifName = %s", __FUNCTION__, ifName);
117             g_hid2dEventMap[i]->func(msg, msgLen);
118         }
119     }
120 }
121 
WifiRegisterHid2dCallback(Hid2dCallback func,const char * ifName)122 int32_t WifiRegisterHid2dCallback(Hid2dCallback func, const char *ifName)
123 {
124     struct Hid2dEvent *event = NULL;
125     uint32_t i;
126 
127     if (func == NULL || ifName == NULL) {
128         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
129         return RET_CODE_INVALID_PARAM;
130     }
131     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
132         if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0) &&
133             g_hid2dEventMap[i]->func == func) {
134             HDF_LOGI("%s the callback function has been registered!", __FUNCTION__);
135             return RET_CODE_SUCCESS;
136         }
137     }
138     event = (struct Hid2dEvent *)OsalMemCalloc(sizeof(struct Hid2dEvent));
139     if (event == NULL) {
140         HDF_LOGE("%s fail: OsalMemCalloc fail!", __FUNCTION__);
141         return RET_CODE_FAILURE;
142     }
143     do {
144         if (strcpy_s(event->ifName, IFNAMSIZ + 1, ifName) != RET_CODE_SUCCESS) {
145             HDF_LOGE("%s: ifName strcpy_s fail", __FUNCTION__);
146             break;
147         }
148         event->func = func;
149         for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
150             if (g_hid2dEventMap[i] == NULL) {
151                 g_hid2dEventMap[i] = event;
152                 HDF_LOGD("%s: WifiRegisterHid2dCallback successful", __FUNCTION__);
153                 return RET_CODE_SUCCESS;
154             }
155         }
156     } while (0);
157 
158     OsalMemFree(event);
159     HDF_LOGE("%s fail: register onRecFunc num more than %d!", __FUNCTION__, MAX_CALL_BACK_COUNT);
160     return RET_CODE_FAILURE;
161 }
162 
WifiUnregisterHid2dCallback(Hid2dCallback func,const char * ifName)163 void WifiUnregisterHid2dCallback(Hid2dCallback func, const char *ifName)
164 {
165     uint32_t i;
166 
167     if (func == NULL || ifName == NULL) {
168         HDF_LOGE("%s: input parameter invalid, line: %d", __FUNCTION__, __LINE__);
169         return;
170     }
171     for (i = 0; i < MAX_CALL_BACK_COUNT; i++) {
172         if (g_hid2dEventMap[i] != NULL && (strcmp(g_hid2dEventMap[i]->ifName, ifName) == 0) &&
173             g_hid2dEventMap[i]->func == func) {
174             g_hid2dEventMap[i]->func = NULL;
175             OsalMemFree(g_hid2dEventMap[i]);
176             g_hid2dEventMap[i] = NULL;
177             return;
178         }
179     }
180 }
181 
182 #ifdef __cplusplus
183 #if __cplusplus
184 }
185 #endif
186 #endif
187