Add unit(in variable) & fix bugs
[platform/core/system/resourced.git] / src / common / notifier.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include "macro.h"
21 #include "module.h"
22 #include "module-data.h"
23 #include "notifier.h"
24
25 #include <resourced.h>
26 #include <trace.h>
27 #include <stdlib.h>
28 #include <glib.h>
29
30 struct resourced_notifier {
31         int (*func)(void *data);
32 };
33
34 static GSList *resourced_notifier_list[RESOURCED_NOTIFIER_MAX];
35
36 #define FIND_NOTIFIER(a, b, d, f) \
37         gslist_for_each(a, b, d) \
38                 if (f == (d->f))
39
40 int register_notifier(enum notifier_type status, int (*func)(void *data))
41 {
42         GSList *n;
43         struct resourced_notifier *notifier;
44
45
46         if (!func) {
47                 _E("invalid func address!");
48                 return -EINVAL;
49         }
50
51         if(status >= RESOURCED_NOTIFIER_MAX ||
52            status < RESOURCED_NOTIFIER_APP_LAUNCH) {
53                 _E("invalid status!");
54                 return -EINVAL;
55         }
56
57         FIND_NOTIFIER(resourced_notifier_list[status], n, notifier, func) {
58                 _E("function is already registered! [%d, %p]",
59                     status, func);
60                 return -EINVAL;
61         }
62
63         notifier = malloc(sizeof(struct resourced_notifier));
64         if (!notifier) {
65                 _E("Fail to malloc for notifier!");
66                 return -ENOMEM;
67         }
68
69         notifier->func = func;
70
71         resourced_notifier_list[status] = g_slist_append(resourced_notifier_list[status], notifier);
72
73         _D("[status: %d] %p is registered", status, func);
74         return 0;
75 }
76
77 int unregister_notifier(enum notifier_type status, int (*func)(void *data))
78 {
79         GSList *n;
80         struct resourced_notifier *notifier;
81
82         if (!func) {
83                 _E("invalid func address!");
84                 return -EINVAL;
85         }
86
87         if(status >= RESOURCED_NOTIFIER_MAX ||
88            status < RESOURCED_NOTIFIER_APP_LAUNCH) {
89                 _E("invalid status!");
90                 return -EINVAL;
91         }
92
93         FIND_NOTIFIER(resourced_notifier_list[status], n, notifier, func) {
94                 _I("[%d, %p]", status, func);
95                 resourced_notifier_list[status] = g_slist_remove(resourced_notifier_list[status], notifier);
96                 free(notifier);
97                 return 0;
98         }
99
100         _E("[%d, %p] is unregistered!", status, func);
101         return -EINVAL;
102 }
103
104 void resourced_notify(enum notifier_type status, void *data)
105 {
106         GSList *iter;
107         struct resourced_notifier *notifier;
108
109         gslist_for_each_item(iter, resourced_notifier_list[status]) {
110                 notifier = (struct resourced_notifier *)iter->data;
111                         if (notifier->func)
112                                 notifier->func(data);
113         }
114 }
115
116 static int notifier_exit(void *data)
117 {
118         GSList *iter;
119         /* Deinitialize in reverse order */
120         struct resourced_notifier *notifier;
121
122         for(int status = RESOURCED_NOTIFIER_APP_LAUNCH;
123                 status < RESOURCED_NOTIFIER_MAX; status++) {
124                 GSList *reverse_list = g_slist_reverse(resourced_notifier_list[status]);
125                 gslist_for_each_item(iter, reverse_list) {
126                         notifier = (struct resourced_notifier *)iter->data;
127                         resourced_notifier_list[status] =
128                                 g_slist_remove(resourced_notifier_list[status], notifier);
129                         free(notifier);
130                 }
131         }
132
133         return RESOURCED_ERROR_NONE;
134 }
135
136 static struct module_ops notifier_ops = {
137         .priority = MODULE_PRIORITY_NORMAL,
138         .name     = "notifier",
139         .exit     = notifier_exit,
140 };
141
142 MODULE_REGISTER(&notifier_ops)
143