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 enum notifier_type status;
32 int (*func)(void *data);
35 static GSList *resourced_notifier_list;
37 #define FIND_NOTIFIER(a, b, d, e, f) \
38 gslist_for_each(a, b, d) \
39 if (e == d->e && f == (d->f))
41 int register_notifier(enum notifier_type status, int (*func)(void *data))
44 struct resourced_notifier *notifier;
46 _I("%d, %x", status, func);
49 _E("invalid func address!");
53 FIND_NOTIFIER(resourced_notifier_list, n, notifier, status, func) {
54 _E("function is already registered! [%d, %x]",
59 notifier = malloc(sizeof(struct resourced_notifier));
61 _E("Fail to malloc for notifier!");
65 notifier->status = status;
66 notifier->func = func;
68 resourced_notifier_list = g_slist_append(resourced_notifier_list, notifier);
73 int unregister_notifier(enum notifier_type status, int (*func)(void *data))
76 struct resourced_notifier *notifier;
79 _E("invalid func address!");
83 FIND_NOTIFIER(resourced_notifier_list, n, notifier, status, func) {
84 _I("[%d, %x]", status, func);
85 resourced_notifier_list = g_slist_remove(resourced_notifier_list, notifier);
92 void resourced_notify(enum notifier_type status, void *data)
95 struct resourced_notifier *notifier;
97 gslist_for_each_item(iter, resourced_notifier_list) {
98 notifier = (struct resourced_notifier *)iter->data;
99 if (status == notifier->status) {
101 notifier->func(data);
106 static int notifier_exit(void *data)
109 /* Deinitialize in reverse order */
110 GSList *reverse_list = g_slist_reverse(resourced_notifier_list);
111 struct resourced_notifier *notifier;
113 gslist_for_each_item(iter, reverse_list) {
114 notifier = (struct resourced_notifier *)iter->data;
115 resourced_notifier_list = g_slist_remove(resourced_notifier_list, iter);
118 return RESOURCED_ERROR_NONE;
121 static struct module_ops notifier_ops = {
122 .priority = MODULE_PRIORITY_NORMAL,
124 .exit = notifier_exit,
127 MODULE_REGISTER(¬ifier_ops)