tizen 2.3.1 release
[kernel/api/system-resource.git] / src / common / module.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2014 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 /**
21  * @file module.c
22  * @desc Module helper functions
23  **/
24
25 #include "macro.h"
26 #include "module.h"
27 #include "resourced.h"
28 #include "trace.h"
29
30 #include <glib.h>
31
32 static GSList *modules_list;
33
34 void add_module(const struct module_ops *module)
35 {
36         ret_msg_if(!module, "Invalid module handler\n");
37         if (module->priority == MODULE_PRIORITY_HIGH)
38                 modules_list = g_slist_prepend(modules_list, (gpointer)module);
39         else
40                 modules_list = g_slist_append(modules_list, (gpointer)module);
41 }
42
43 void remove_module(const struct module_ops *module)
44 {
45         modules_list = g_slist_remove(modules_list, (gpointer)module);
46 }
47
48 const struct module_ops *find_module(const char *name)
49 {
50         GSList *iter;
51         const struct module_ops *module;
52
53         gslist_for_each_item(iter, modules_list) {
54                 module = (struct module_ops *)iter->data;
55                 if (!strcmp(module->name, name))
56                         return module;
57         }
58         return NULL;
59 }
60
61 void modules_check_runtime_support(void *data)
62 {
63         GSList *iter;
64         const struct module_ops *module;
65         int ret_code = RESOURCED_ERROR_NONE;
66
67         gslist_for_each_item(iter, modules_list) {
68                 module = (const struct module_ops *)iter->data;
69                 _D("check runtime support [%s] module\n", module->name);
70
71                 if (!module->check_runtime_support)
72                         continue;
73
74                 ret_code = module->check_runtime_support((void *)module);
75                 if (ret_code != RESOURCED_ERROR_NONE) {
76                         _E("%s module check failed", module->name);
77                         remove_module(module);
78                         continue;
79                 }
80         }
81 }
82
83 void modules_init(void *data)
84 {
85         GSList *iter;
86         const struct module_ops *module;
87         int ret_code = RESOURCED_ERROR_NONE;
88
89         gslist_for_each_item(iter, modules_list) {
90                 module = (struct module_ops *)iter->data;
91                 _D("Initialize [%s] module\n", module->name);
92                 if (module->init)
93                         ret_code = module->init(data);
94                 if (ret_code < 0)
95                         _E("Fail to initialize [%s] module\n", module->name);
96         }
97 }
98
99 void modules_exit(void *data)
100 {
101         GSList *iter;
102         /* Deinitialize in reverse order */
103         GSList *reverse_list = g_slist_reverse(modules_list);
104         const struct module_ops *module;
105         int ret_code = RESOURCED_ERROR_NONE;
106
107         gslist_for_each_item(iter, reverse_list) {
108                 module = (struct module_ops *)iter->data;
109                 _D("Deinitialize [%s] module\n", module->name);
110                 if (module->exit)
111                         ret_code = module->exit(data);
112                 if (ret_code < 0)
113                         _E("Fail to deinitialize [%s] module\n", module->name);
114         }
115 }