Initialize Tizen 2.3
[framework/system/deviced.git] / src / core / device-notifier.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
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 "log.h"
21 #include "devices.h"
22 #include "device-notifier.h"
23 #include "list.h"
24 #include "common.h"
25
26 struct device_notifier {
27         enum device_notifier_type status;
28         int (*func)(void *data);
29 };
30
31 static dd_list *device_notifier_list;
32
33 #define FIND_NOTIFIER(a, b, d, e, f) \
34         DD_LIST_FOREACH(a, b, d) \
35                 if (e == d->e && f == (d->f))
36
37 int register_notifier(enum device_notifier_type status, int (*func)(void *data))
38 {
39         dd_list *n;
40         struct device_notifier *data, *notifier;
41
42         _I("%d, %x", status, func);
43
44         if (!func) {
45                 _E("invalid func address!");
46                 return -EINVAL;
47         }
48
49         FIND_NOTIFIER(device_notifier_list, n, notifier, status, func) {
50                 _E("function is already registered! [%d, %x]",
51                     status, func);
52                 return -EINVAL;
53         }
54
55         notifier = malloc(sizeof(struct device_notifier));
56         if (!notifier) {
57                 _E("Fail to malloc for notifier!");
58                 return -ENOMEM;
59         }
60
61         notifier->status = status;
62         notifier->func = func;
63
64         DD_LIST_APPEND(device_notifier_list, notifier);
65
66         return 0;
67 }
68
69 int unregister_notifier(enum device_notifier_type status, int (*func)(void *data))
70 {
71         dd_list *n;
72         struct device_notifier *notifier;
73
74         if (!func) {
75                 _E("invalid func address!");
76                 return -EINVAL;
77         }
78
79         FIND_NOTIFIER(device_notifier_list, n, notifier, status, func) {
80                 _I("[%d, %x]", status, func);
81                 DD_LIST_REMOVE(device_notifier_list, notifier);
82                 free(notifier);
83         }
84
85         return 0;
86 }
87
88 void device_notify(enum device_notifier_type status, void *data)
89 {
90         dd_list *n, *next;
91         struct device_notifier *notifier;
92         int cnt = 0;
93
94         DD_LIST_FOREACH_SAFE(device_notifier_list, n, next, notifier) {
95                 if (status == notifier->status) {
96                         if (notifier->func) {
97                                 notifier->func(data);
98                                 cnt++;
99                         }
100                 }
101         }
102 }
103
104 static void device_notifier_exit(void *data)
105 {
106         dd_list *n;
107         struct device_notifier *notifier;
108
109         DD_LIST_FOREACH(device_notifier_list, n, notifier)
110                 DD_LIST_REMOVE(device_notifier_list, notifier);
111                 free(notifier);
112 }
113
114 static const struct device_ops notifier_device_ops = {
115         .priority = DEVICE_PRIORITY_NORMAL,
116         .name     = "notifier",
117         .exit     = device_notifier_exit,
118 };
119
120 DEVICE_OPS_REGISTER(&notifier_device_ops)