4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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 "device-notifier.h"
26 struct device_notifier {
27 enum device_notifier_type status;
28 int (*func)(void *data);
31 static dd_list *device_notifier_list;
33 #define FIND_NOTIFIER(a, b, d, e, f) \
34 DD_LIST_FOREACH(a, b, d) \
35 if (e == d->e && f == (d->f))
37 int register_notifier(enum device_notifier_type status, int (*func)(void *data))
40 struct device_notifier *data, *notifier;
42 _I("%d, %x", status, func);
45 _E("invalid func address!");
49 FIND_NOTIFIER(device_notifier_list, n, notifier, status, func) {
50 _E("function is already registered! [%d, %x]",
55 notifier = malloc(sizeof(struct device_notifier));
57 _E("Fail to malloc for notifier!");
61 notifier->status = status;
62 notifier->func = func;
64 DD_LIST_APPEND(device_notifier_list, notifier);
69 int unregister_notifier(enum device_notifier_type status, int (*func)(void *data))
72 struct device_notifier *notifier;
75 _E("invalid func address!");
79 FIND_NOTIFIER(device_notifier_list, n, notifier, status, func) {
80 _I("[%d, %x]", status, func);
81 DD_LIST_REMOVE(device_notifier_list, notifier);
88 void device_notify(enum device_notifier_type status, void *data)
91 struct device_notifier *notifier;
94 DD_LIST_FOREACH_SAFE(device_notifier_list, n, next, notifier) {
95 if (status == notifier->status) {
104 static void device_notifier_exit(void *data)
107 struct device_notifier *notifier;
109 DD_LIST_FOREACH(device_notifier_list, n, notifier)
110 DD_LIST_REMOVE(device_notifier_list, notifier);
114 static const struct device_ops notifier_device_ops = {
115 .priority = DEVICE_PRIORITY_NORMAL,
117 .exit = device_notifier_exit,
120 DEVICE_OPS_REGISTER(¬ifier_device_ops)