4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "module-data.h"
25 #include <resourced.h>
30 struct resourced_notifier {
31 int (*func)(void *data);
34 static GSList *resourced_notifier_list[RESOURCED_NOTIFIER_MAX];
36 #define FIND_NOTIFIER(a, b, d, f) \
37 gslist_for_each(a, b, d) \
40 int register_notifier(enum notifier_type status, int (*func)(void *data))
43 struct resourced_notifier *notifier;
47 _E("invalid func address!");
51 if(status >= RESOURCED_NOTIFIER_MAX ||
52 status < RESOURCED_NOTIFIER_APP_LAUNCH) {
53 _E("invalid status!");
57 FIND_NOTIFIER(resourced_notifier_list[status], n, notifier, func) {
58 _E("function is already registered! [%d, %p]",
63 notifier = malloc(sizeof(struct resourced_notifier));
65 _E("Fail to malloc for notifier!");
69 notifier->func = func;
71 resourced_notifier_list[status] = g_slist_append(resourced_notifier_list[status], notifier);
73 _D("[status: %d] %p is registered", status, func);
77 int unregister_notifier(enum notifier_type status, int (*func)(void *data))
80 struct resourced_notifier *notifier;
83 _E("invalid func address!");
87 if(status >= RESOURCED_NOTIFIER_MAX ||
88 status < RESOURCED_NOTIFIER_APP_LAUNCH) {
89 _E("invalid status!");
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);
100 _E("[%d, %p] is unregistered!", status, func);
104 void resourced_notify(enum notifier_type status, void *data)
107 struct resourced_notifier *notifier;
109 gslist_for_each_item(iter, resourced_notifier_list[status]) {
110 notifier = (struct resourced_notifier *)iter->data;
112 notifier->func(data);
116 static int notifier_exit(void *data)
119 /* Deinitialize in reverse order */
120 struct resourced_notifier *notifier;
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);
133 return RESOURCED_ERROR_NONE;
136 static struct module_ops notifier_ops = {
137 .priority = MODULE_PRIORITY_NORMAL,
139 .exit = notifier_exit,
142 MODULE_REGISTER(¬ifier_ops)