-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.c
89 lines (82 loc) · 1.84 KB
/
callback.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include "callback.h"
callback_s callback[EVENT_MAX] = { { { NULL }, 0 } };
void callback_register( event_t event, callback_t cb_fn )
{
int count = 0;
if(event >= EVENT_0 && event < EVENT_MAX)
{
for(count=0; count<REGISTER_NUM_MAX; count++)
{
if(callback[event].cb[count] == NULL)
{
if(cb_fn)
{
callback[event].cb[callback[event].count++] = cb_fn;
printf("[SUCCESS] event [%d] has been registered [%d] times \r\n", event, callback[event].count);
}
else
{
printf("[WARNING] event [%d] callback function can not be NULL \r\n", event);
}
break;
}
}
if(count == REGISTER_NUM_MAX)
{
printf("[FORBIDDEN] the callback functions register count of this event [%d] has been up to max \r\n", event);
}
}
else
{
printf("[ERROR] illegal event [%d] \r\n", event);
}
}
void callback_execute( event_t event )
{
int count = 0;
if(event >= EVENT_0 && event < EVENT_MAX)
{
for(count=0; count<REGISTER_NUM_MAX; count++)
{
if(callback[event].cb[count])
{
callback[event].cb[count](NULL);
}
}
}
else
{
printf("[ERROR] illegal event [%d] \r\n", event);
}
}
void callback_unregister( event_t event, callback_t cb_fn )
{
int count = 0;
if(event >= EVENT_0 && event < EVENT_MAX)
{
for(count=0; count<REGISTER_NUM_MAX; count++)
{
if(callback[event].cb[count] == cb_fn && cb_fn)
{
callback[event].cb[count] = NULL;
callback[event].count--;
printf("[SUCCESS] event [%d] has been registered [%d] times \r\n", event, callback[event].count);
break;
}
else if(cb_fn == NULL)
{
printf("[WARNING] the callback function can not be NULL \r\n");
break;
}
}
if(count == REGISTER_NUM_MAX)
{
printf("[WARNING] the callback function is not registered \r\n");
}
}
else
{
printf("[ERROR] illegal event [%d] \r\n", event);
}
}