tizen 2.3.1 release
[framework/telephony/libtcore.git] / src / plugin.c
1 /*
2  * libtcore
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <dlfcn.h>
28
29 #include <glib.h>
30
31 #include "tcore.h"
32 #include "core_object.h"
33 #include "plugin.h"
34 #include "server.h"
35
36 struct tcore_plugin_type {
37         char *filename;
38         const struct tcore_plugin_define_desc *desc;
39         void *handle;
40
41         void *user_data;
42         Communicator *comm;
43         GSList *list_co;
44         GHashTable *property;
45
46         Server *parent_server;
47 };
48
49 TcorePlugin *tcore_plugin_new(Server *server,
50                 const struct tcore_plugin_define_desc *desc,
51                 const char *filename, void *handle)
52 {
53         TcorePlugin *p;
54
55         p = calloc(1, sizeof(struct tcore_plugin_type));
56         if (!p)
57                 return NULL;
58
59         if (filename)
60                 p->filename = strdup(filename);
61
62         p->desc = desc;
63         p->property = g_hash_table_new(g_str_hash, g_str_equal);
64         p->handle = handle;
65         p->parent_server = server;
66
67         return p;
68 }
69
70 void tcore_plugin_free(TcorePlugin *plugin)
71 {
72         GSList *list;
73         CoreObject *o;
74
75         if (!plugin)
76                 return;
77
78         dbg("");
79
80         if (plugin->list_co) {
81                 for (list = plugin->list_co; list; list = list->next) {
82                         o = list->data;
83                         if (!o)
84                                 continue;
85
86                         tcore_object_free(o);
87                         list->data = NULL;
88                 }
89
90                 g_slist_free(plugin->list_co);
91                 plugin->list_co = NULL;
92         }
93
94         if (plugin->filename) {
95                 free(plugin->filename);
96                 plugin->filename = NULL;
97         }
98
99         if (plugin->property) {
100                 g_hash_table_destroy(plugin->property);
101                 plugin->property = NULL;
102         }
103
104         plugin->desc = NULL;
105
106         if (plugin->handle) {
107                 dlclose(plugin->handle);
108                 plugin->handle = NULL;
109         }
110
111         free(plugin);
112 }
113
114 const struct tcore_plugin_define_desc *tcore_plugin_get_description(TcorePlugin *plugin)
115 {
116         if (!plugin)
117                 return NULL;
118
119         return plugin->desc;
120 }
121
122 char *tcore_plugin_get_filename(TcorePlugin *plugin)
123 {
124         if (!plugin)
125                 return NULL;
126
127         if (!plugin->filename)
128                 return NULL;
129
130         return strdup(plugin->filename);
131 }
132
133 const char* tcore_plugin_ref_plugin_name(TcorePlugin *plugin)
134 {
135         if (!plugin)
136                 return NULL;
137
138         if (!plugin->desc->name)
139                 return NULL;
140
141         return plugin->desc->name;
142 }
143
144 Server *tcore_plugin_ref_server(TcorePlugin *plugin)
145 {
146         if (!plugin)
147                 return NULL;
148
149         return plugin->parent_server;
150 }
151
152 TReturn tcore_plugin_link_user_data(TcorePlugin *plugin, void *user_data)
153 {
154         if (!plugin)
155                 return TCORE_RETURN_EINVAL;
156
157         plugin->user_data = user_data;
158
159         return TCORE_RETURN_SUCCESS;
160 }
161
162 void *tcore_plugin_ref_user_data(TcorePlugin *plugin)
163 {
164         if (!plugin)
165                 return NULL;
166
167         return plugin->user_data;
168 }
169
170 TReturn tcore_plugin_add_core_object(TcorePlugin *plugin, CoreObject *co)
171 {
172         if (!plugin || !co)
173                 return TCORE_RETURN_EINVAL;
174
175         dbg("add core_object! (name=%s)", tcore_object_ref_name(co));
176
177         plugin->list_co = g_slist_insert(plugin->list_co, co, 0);
178
179         return TCORE_RETURN_SUCCESS;
180 }
181
182 TReturn tcore_plugin_remove_core_object(TcorePlugin *plugin, CoreObject *co)
183 {
184         if (!plugin || !co)
185                 return TCORE_RETURN_EINVAL;
186
187         dbg("remove core_object! (name=%s)", tcore_object_ref_name(co));
188
189         plugin->list_co = g_slist_remove(plugin->list_co, co);
190
191         return TCORE_RETURN_SUCCESS;
192 }
193
194 CoreObject *tcore_plugin_ref_core_object(TcorePlugin *plugin, unsigned int type)
195 {
196         GSList *list;
197         CoreObject *co;
198
199         if (!plugin)
200                 return NULL;
201
202         for (list = plugin->list_co; list; list = list->next) {
203                 co = list->data;
204                 if (!co)
205                         continue;
206
207                 if (tcore_object_get_type(co) == type) {
208                         return co;
209                 }
210         }
211
212         return NULL;
213 }
214
215 GSList *tcore_plugin_get_core_objects(TcorePlugin *plugin)
216 {
217         GSList *list, *rlist = NULL;
218         CoreObject *co;
219
220         if (!plugin)
221                 return NULL;
222
223         for (list = plugin->list_co; list; list = list->next) {
224                 co = list->data;
225                 if (!co)
226                         continue;
227
228                 rlist = g_slist_append(rlist, co);
229         }
230
231         return rlist;
232 }
233
234 GSList *tcore_plugin_get_core_objects_bytype(TcorePlugin *plugin, unsigned int type)
235 {
236         GSList *list, *rlist = NULL;
237         CoreObject *co;
238
239         if (!plugin)
240                 return NULL;
241
242         for (list = plugin->list_co; list; list = list->next) {
243                 co = list->data;
244                 if (!co)
245                         continue;
246
247                 if ((CORE_OBJECT_TYPE_DEFAULT |(tcore_object_get_type(co) & 0x0FF00000)) == type)
248                         rlist = g_slist_append(rlist, co);
249         }
250
251         return rlist;
252 }
253
254 TReturn tcore_plugin_core_object_event_emit(TcorePlugin *plugin, const char *event, const void *event_info)
255 {
256         GSList *list;
257         CoreObject *co;
258
259         if (!plugin)
260                 return TCORE_RETURN_EINVAL;
261
262         dbg("event(%s) emit", event);
263
264         for (list = plugin->list_co; list; list = list->next) {
265                 co = list->data;
266                 if (!co)
267                         continue;
268
269                 tcore_object_emit_callback(co, event, event_info);
270         }
271
272         return TCORE_RETURN_SUCCESS;
273 }
274
275 TReturn tcore_plugin_link_property(TcorePlugin *plugin, const char *key, void *data)
276 {
277         void *prev;
278
279         if (!plugin)
280                 return TCORE_RETURN_EINVAL;
281
282         if (!plugin->property)
283                 return TCORE_RETURN_EINVAL;
284
285         prev = g_hash_table_lookup(plugin->property, key);
286         if (prev != NULL) {
287                 free(prev);
288                 g_hash_table_replace(plugin->property, (gpointer)key, data);
289         }
290         else {
291                 g_hash_table_insert(plugin->property, strdup(key), data);
292         }
293
294         return TCORE_RETURN_SUCCESS;
295 }
296
297 void *tcore_plugin_ref_property(TcorePlugin *plugin, const char *key)
298 {
299         if (!plugin)
300                 return NULL;
301
302         if (!plugin->property)
303                 return NULL;
304
305         return g_hash_table_lookup(plugin->property, key);
306 }