1 /*
2  * eventreg.c - CC31xx/CC32xx Host Driver Implementation
3  *
4  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
5  *
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *    Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  *    Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the
17  *    distribution.
18  *
19  *    Neither the name of Texas Instruments Incorporated nor the names of
20  *    its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35 */
36 
37 /*****************************************************************************/
38 /* Include files                                                             */
39 /*****************************************************************************/
40 #include <ti/drivers/net/wifi/simplelink.h>
41 #include <ti/drivers/net/wifi/eventreg.h>
42 
43 
44 typedef void (*_pSlDeviceFatalErrorEvtHdlr_t)(SlDeviceFatal_t *pSlFatalErrorEvent);
45 typedef void (*_pSlDeviceGeneralEvtHdlr_t)(SlDeviceEvent_t *pSlDeviceEvent);
46 typedef void (*_pSlWlanEvtHdlr)(SlWlanEvent_t* pSlWlanEvent);
47 typedef void (*_pSlNetAppEvtHdlr)(SlNetAppEvent_t* pSlNetAppEvent);
48 typedef void (*_pSlSockEvtHdlr)(SlSockEvent_t* pSlSockEvent);
49 typedef void (*_pSlNetAppHttpServerHdlr)(SlNetAppHttpServerEvent_t *pSlHttpServerEvent, SlNetAppHttpServerResponse_t *pSlHttpServerResponse);
50 typedef void (*_pSlNetAppRequestHdlr)(SlNetAppRequest_t *pNetAppRequest, SlNetAppResponse_t *pNetAppResponse);
51 typedef void (*_pSlNetAppRequestMemFree)(_u8 *buffer);
52 typedef void (*_pSlSocketTriggerEventHandler)(SlSockTriggerEvent_t* pSlSockTriggerEvent);
53 
54 
55 typedef _i32 (*_pSlPropogationDeviceFatalErrorEvtHdlr_t)(SlDeviceFatal_t *pSlFatalErrorEvent);
56 typedef _i32 (*_pSlPropogationDeviceGeneralEvtHdlr_t)(SlDeviceEvent_t *pSlDeviceEvent);
57 typedef _i32 (*_pSlPropogationWlanEvtHdlr)(SlWlanEvent_t* pSlWlanEvent);
58 typedef _i32 (*_pSlPropogationNetAppEvtHdlr)(SlNetAppEvent_t* pSlNetAppEvent);
59 typedef _i32 (*_pSlPropogationSockEvtHdlr)(SlSockEvent_t* pSlSockEvent);
60 typedef _i32 (*_pSlPropogationNetAppHttpServerHdlr)(SlNetAppHttpServerEvent_t *pSlHttpServerEvent, SlNetAppHttpServerResponse_t *pSlHttpServerResponse);
61 typedef _i32 (*_pSlPropogationNetAppRequestHdlr)(SlNetAppRequest_t *pNetAppRequest, SlNetAppResponse_t *pNetAppResponse);
62 typedef _i32 (*_pSlPropogationNetAppRequestMemFree)(_u8 *buffer);
63 typedef _i32 (*_pSlPropogationSocketTriggerEventHandler)(SlSockTriggerEvent_t* pSlSockTriggerEvent);
64 
65 #ifdef SL_RUNTIME_EVENT_REGISTERATION
66 
67 void* g_UserEvents[SL_NUM_OF_EVENT_TYPES] = {0};
68 SlEventsListNode_t* g_LibsEvents[SL_NUM_OF_EVENT_TYPES] = {0};
69 
70 #endif
71 
72 
_SlIsEventRegistered(SlEventHandler_e EventHandlerType)73 _i32 _SlIsEventRegistered(SlEventHandler_e EventHandlerType)
74 {
75 #ifdef SL_RUNTIME_EVENT_REGISTERATION
76     if( (NULL != g_LibsEvents[EventHandlerType]) || (NULL != g_UserEvents[EventHandlerType]) )
77     {
78         return 1;
79     }
80 #endif
81     if(SL_EVENT_HDL_MEM_FREE == EventHandlerType)
82     {
83 #ifdef slcb_NetAppRequestMemFree
84         return 1;
85 #endif
86     }
87     if(SL_EVENT_HDL_SOCKET_TRIGGER == EventHandlerType)
88     {
89 #ifdef slcb_SocketTriggerEventHandler
90         return 1;
91 #endif
92     }
93 
94     return 0;
95 }
96 
97 #ifdef SL_RUNTIME_EVENT_REGISTERATION
98 
sl_RegisterEventHandler(SlEventHandler_e EventHandlerType,void * EventHandler)99 _i32 sl_RegisterEventHandler(SlEventHandler_e EventHandlerType , void* EventHandler)
100 {
101     g_UserEvents[EventHandlerType] = EventHandler;
102     return 0;
103 }
104 
sl_RegisterLibsEventHandler(SlEventHandler_e EventHandlerType,SlEventsListNode_t * EventHandlerNode)105 _i32 sl_RegisterLibsEventHandler(SlEventHandler_e EventHandlerType , SlEventsListNode_t* EventHandlerNode)
106 {
107     EventHandlerNode->next = NULL;
108 
109     if(g_LibsEvents[EventHandlerType] == NULL)
110     {
111         g_LibsEvents[EventHandlerType] = EventHandlerNode;
112     }
113     else
114     {
115         SlEventsListNode_t* currentNode = g_LibsEvents[EventHandlerType];
116         while(currentNode->next != NULL)
117         {
118             currentNode = currentNode->next;
119         }
120 
121         currentNode->next = EventHandlerNode;
122     }
123     return 0;
124 }
125 
sl_UnregisterLibsEventHandler(SlEventHandler_e EventHandlerType,SlEventsListNode_t * EventHandlerNode)126 _i32 sl_UnregisterLibsEventHandler(SlEventHandler_e EventHandlerType , SlEventsListNode_t* EventHandlerNode)
127 {
128     SlEventsListNode_t* currentNode = g_LibsEvents[EventHandlerType];
129     SlEventsListNode_t* lastNode = g_LibsEvents[EventHandlerType];
130     int count = 0;
131     while(currentNode != NULL)
132     {
133         if(EventHandlerNode == currentNode)
134         {
135             if(count == 0)
136             {
137                 g_LibsEvents[EventHandlerType] = g_LibsEvents[EventHandlerType]->next;
138             }
139             else
140             {
141                 lastNode->next = currentNode->next;
142             }
143             return 0;
144         }
145 
146         if(count != 0)
147         {
148             lastNode = lastNode->next;
149         }
150         count++;
151         currentNode = currentNode->next;
152     }
153 
154     return SL_RET_CODE_EVENT_LINK_NOT_FOUND;
155 }
156 
157 
158 /* Event handlers section */
_SlDeviceFatalErrorEvtHdlr(SlDeviceFatal_t * pSlFatalErrorEvent)159 void _SlDeviceFatalErrorEvtHdlr(SlDeviceFatal_t *pSlFatalErrorEvent)
160 {
161     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_FATAL_ERROR];
162     while(currentNode != NULL)
163     {
164         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationDeviceFatalErrorEvtHdlr_t)(currentNode->event))(pSlFatalErrorEvent))
165         {
166             return;
167         }
168         currentNode = currentNode->next;
169     }
170 
171     if (NULL != g_UserEvents[SL_EVENT_HDL_FATAL_ERROR])
172     {
173         ((_pSlDeviceFatalErrorEvtHdlr_t)g_UserEvents[SL_EVENT_HDL_FATAL_ERROR])(pSlFatalErrorEvent);
174     }
175 
176 #ifdef slcb_DeviceFatalErrorEvtHdlr
177     else
178     {
179         slcb_DeviceFatalErrorEvtHdlr(pSlFatalErrorEvent);
180     }
181 #endif
182 }
183 
184 
_SlDeviceGeneralEvtHdlr(SlDeviceEvent_t * pSlDeviceEvent)185 void _SlDeviceGeneralEvtHdlr(SlDeviceEvent_t *pSlDeviceEvent)
186 {
187     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_DEVICE_GENERAL];
188     while(currentNode != NULL)
189     {
190         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationDeviceGeneralEvtHdlr_t)(currentNode->event))(pSlDeviceEvent))
191         {
192             return;
193         }
194         currentNode = currentNode->next;
195     }
196 
197     if (NULL != g_UserEvents[SL_EVENT_HDL_DEVICE_GENERAL])
198     {
199         ((_pSlDeviceGeneralEvtHdlr_t)g_UserEvents[SL_EVENT_HDL_DEVICE_GENERAL])(pSlDeviceEvent);
200     }
201 #ifdef slcb_DeviceGeneralEvtHdlr
202     else
203     {
204         slcb_DeviceGeneralEvtHdlr(pSlDeviceEvent);
205     }
206 #endif
207 }
208 
209 
_SlWlanEvtHdlr(SlWlanEvent_t * pSlWlanEvent)210 void _SlWlanEvtHdlr(SlWlanEvent_t* pSlWlanEvent)
211 {
212     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_WLAN];
213     while(currentNode != NULL)
214     {
215         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationWlanEvtHdlr)(currentNode->event))(pSlWlanEvent))
216         {
217             return;
218         }
219         currentNode = currentNode->next;
220     }
221 
222     if (NULL != g_UserEvents[SL_EVENT_HDL_WLAN])
223     {
224         ((_pSlWlanEvtHdlr)g_UserEvents[SL_EVENT_HDL_WLAN])(pSlWlanEvent);
225     }
226 #ifdef slcb_WlanEvtHdlr
227     else
228     {
229         slcb_WlanEvtHdlr(pSlWlanEvent);
230     }
231 #endif
232 }
233 
234 
_SlNetAppEvtHdlr(SlNetAppEvent_t * pSlNetAppEvent)235 void _SlNetAppEvtHdlr(SlNetAppEvent_t* pSlNetAppEvent)
236 {
237     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_NETAPP];
238     while(currentNode != NULL)
239     {
240         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationNetAppEvtHdlr)(currentNode->event))(pSlNetAppEvent))
241         {
242             return;
243         }
244         currentNode = currentNode->next;
245     }
246     if (NULL != g_UserEvents[SL_EVENT_HDL_NETAPP])
247     {
248         ((_pSlNetAppEvtHdlr)g_UserEvents[SL_EVENT_HDL_NETAPP])(pSlNetAppEvent);
249     }
250 #ifdef slcb_NetAppEvtHdlr
251     else
252     {
253         slcb_NetAppEvtHdlr(pSlNetAppEvent);
254     }
255 #endif
256 }
257 
258 
_SlSockEvtHdlr(SlSockEvent_t * pSlSockEvent)259 void _SlSockEvtHdlr(SlSockEvent_t* pSlSockEvent)
260 {
261     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_SOCKET];
262     while(currentNode != NULL)
263     {
264         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationSockEvtHdlr)(currentNode->event))(pSlSockEvent))
265         {
266             return;
267         }
268         currentNode = currentNode->next;
269     }
270     if (NULL != g_UserEvents[SL_EVENT_HDL_SOCKET])
271     {
272         ((_pSlSockEvtHdlr)g_UserEvents[SL_EVENT_HDL_SOCKET])(pSlSockEvent);
273     }
274 
275 #ifdef slcb_SockEvtHdlr
276     else
277     {
278         slcb_SockEvtHdlr(pSlSockEvent);
279     }
280 #endif
281 }
282 
283 
_SlNetAppHttpServerHdlr(SlNetAppHttpServerEvent_t * pSlHttpServerEvent,SlNetAppHttpServerResponse_t * pSlHttpServerResponse)284 void _SlNetAppHttpServerHdlr(SlNetAppHttpServerEvent_t *pSlHttpServerEvent, SlNetAppHttpServerResponse_t *pSlHttpServerResponse)
285 {
286     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_HTTP_SERVER];
287     while(currentNode != NULL)
288     {
289         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationNetAppHttpServerHdlr)(currentNode->event))(pSlHttpServerEvent,pSlHttpServerResponse))
290         {
291             return;
292         }
293         currentNode = currentNode->next;
294     }
295     if (NULL != g_UserEvents[SL_EVENT_HDL_HTTP_SERVER])
296     {
297         ((_pSlNetAppHttpServerHdlr)g_UserEvents[SL_EVENT_HDL_HTTP_SERVER])(pSlHttpServerEvent,pSlHttpServerResponse);
298     }
299 #ifdef slcb_NetAppHttpServerHdlr
300     else
301     {
302         slcb_NetAppHttpServerHdlr(pSlHttpServerEvent,pSlHttpServerResponse);
303     }
304 #endif
305 }
306 
307 
308 
_SlNetAppRequestHdlr(SlNetAppRequest_t * pNetAppRequest,SlNetAppResponse_t * pNetAppResponse)309 void _SlNetAppRequestHdlr(SlNetAppRequest_t *pNetAppRequest, SlNetAppResponse_t *pNetAppResponse)
310 {
311     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_NETAPP_REQUEST];
312     while(currentNode != NULL)
313     {
314         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationNetAppRequestHdlr)(currentNode->event))(pNetAppRequest,pNetAppResponse))
315         {
316             return;
317         }
318         currentNode = currentNode->next;
319     }
320     if (NULL != g_UserEvents[SL_EVENT_HDL_NETAPP_REQUEST])
321     {
322         ((_pSlNetAppRequestHdlr)g_UserEvents[SL_EVENT_HDL_NETAPP_REQUEST])(pNetAppRequest,pNetAppResponse);
323     }
324 #ifdef slcb_NetAppRequestHdlr
325     else
326     {
327         slcb_NetAppRequestHdlr(pNetAppRequest,pNetAppResponse);
328     }
329 #endif
330 }
331 
332 
333 
_SlNetAppRequestMemFree(_u8 * buffer)334 void _SlNetAppRequestMemFree (_u8 *buffer)
335 {
336     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_MEM_FREE];
337     while(currentNode != NULL)
338     {
339         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationNetAppRequestMemFree)(currentNode->event))(buffer))
340         {
341             return;
342         }
343         currentNode = currentNode->next;
344     }
345     if (NULL != g_UserEvents[SL_EVENT_HDL_MEM_FREE])
346     {
347         ((_pSlNetAppRequestMemFree)g_UserEvents[SL_EVENT_HDL_MEM_FREE])(buffer);
348     }
349 #ifdef slcb_NetAppRequestMemFree
350     else
351     {
352         slcb_NetAppRequestMemFree(buffer);
353     }
354 #endif
355 }
356 
357 
_SlSocketTriggerEventHandler(SlSockTriggerEvent_t * pSlSockTriggerEvent)358 void _SlSocketTriggerEventHandler(SlSockTriggerEvent_t* pSlSockTriggerEvent)
359 {
360     SlEventsListNode_t* currentNode = g_LibsEvents[SL_EVENT_HDL_SOCKET_TRIGGER];
361     while(currentNode != NULL)
362     {
363         if(EVENT_PROPAGATION_BLOCK == ((_pSlPropogationSocketTriggerEventHandler)(currentNode->event))(pSlSockTriggerEvent))
364         {
365             return;
366         }
367         currentNode = currentNode->next;
368     }
369     if (NULL != g_UserEvents[SL_EVENT_HDL_SOCKET_TRIGGER])
370     {
371         ((_pSlSocketTriggerEventHandler)g_UserEvents[SL_EVENT_HDL_SOCKET_TRIGGER])(pSlSockTriggerEvent);
372     }
373 #ifdef slcb_SocketTriggerEventHandler
374     else
375     {
376         slcb_SocketTriggerEventHandler(pSlSockTriggerEvent);
377     }
378 #endif
379 }
380 
381 #endif /* SL_RUNTIME_EVENT_REGISTERATION */
382