4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include "core_object.h"
33 struct callback_type {
36 CoreObjectCallback callback;
40 struct tcore_object_type {
44 TcorePlugin *parent_plugin;
49 CoreObjectFreeHook free_hook;
50 CoreObjectCloneHook clone_hook;
51 CoreObjectDispatcher dispatcher;
57 static CoreObject *_object_new(TcorePlugin *plugin, const char *name, unsigned int type)
61 co = calloc(sizeof(struct tcore_object_type), 1);
65 co->parent_plugin = plugin;
68 co->name = strdup(name);
75 static gboolean _on_at_event(TcoreAT *at, const GSList *lines, void *user_data)
79 struct callback_type *cb = user_data;
81 ret = cb->callback(cb->co, lines, cb->user_data);
89 static void _remove_at_callback(TcoreAT *at, struct callback_type *cb)
91 tcore_at_remove_notification_full(at, cb->event, _on_at_event, cb);
94 CoreObject *tcore_object_new(TcorePlugin *plugin,
95 const char *name, TcoreHal *hal)
99 co = _object_new(plugin, name, CORE_OBJECT_TYPE_DEFAULT);
103 tcore_object_set_hal(co, hal);
106 tcore_plugin_add_core_object(plugin, co);
111 void tcore_object_free(CoreObject *co)
114 struct callback_type *cb = NULL;
119 dbg("co_name=%s", co->name);
125 for (l = co->callbacks; l; l = l->next) {
139 g_slist_free(co->callbacks);
140 co->callbacks = NULL;
149 CoreObject *tcore_object_clone(CoreObject *src, TcorePlugin *new_parent, const char *new_name)
155 struct callback_type *cb = NULL;
163 p = src->parent_plugin;
170 dest = _object_new(p, name, src->type);
174 dest->object = src->object;
175 dest->user_data = src->user_data;
176 dest->free_hook = src->free_hook;
177 dest->clone_hook = src->clone_hook;
178 dest->dispatcher = src->dispatcher;
179 dest->hal = src->hal;
181 for (l = src->callbacks; l; l = l->next) {
189 tcore_object_add_callback(dest, cb->event, cb->callback, cb->user_data);
192 tcore_plugin_add_core_object(p, dest);
195 src->clone_hook(src, dest);
200 const char *tcore_object_ref_name(CoreObject *co)
208 TReturn tcore_object_set_free_hook(CoreObject *co,
209 CoreObjectFreeHook free_hook)
212 return TCORE_RETURN_EINVAL;
214 co->free_hook = free_hook;
216 return TCORE_RETURN_SUCCESS;
220 TReturn tcore_object_set_clone_hook(CoreObject *co,
221 CoreObjectCloneHook clone_hook)
224 return TCORE_RETURN_EINVAL;
226 co->clone_hook = clone_hook;
228 return TCORE_RETURN_SUCCESS;
231 TReturn tcore_object_set_name(CoreObject *co, const char *name)
234 return TCORE_RETURN_EINVAL;
242 co->name = strdup(name);
244 return TCORE_RETURN_SUCCESS;
247 TcorePlugin *tcore_object_ref_plugin(CoreObject *co)
252 return co->parent_plugin;
255 TReturn tcore_object_link_object(CoreObject *co, void *object)
258 return TCORE_RETURN_EINVAL;
262 return TCORE_RETURN_SUCCESS;
265 void *tcore_object_ref_object(CoreObject *co)
273 TReturn tcore_object_set_type(CoreObject *co, unsigned int type)
276 return TCORE_RETURN_EINVAL;
280 return TCORE_RETURN_SUCCESS;
283 unsigned int tcore_object_get_type(CoreObject *co)
291 TReturn tcore_object_set_hal(CoreObject *co, TcoreHal *hal)
294 struct callback_type *cb = NULL;
298 return TCORE_RETURN_EINVAL;
301 // remove unsolicited callbacks
302 if (tcore_hal_get_mode(co->hal) == TCORE_HAL_MODE_AT) {
303 at = tcore_hal_get_at(co->hal);
304 for (l = co->callbacks; l; l = l->next) {
312 tcore_at_remove_notification_full(at, cb->event, _on_at_event, cb);
319 return TCORE_RETURN_SUCCESS;
321 // register unsolicited callbacks
322 if (tcore_hal_get_mode(hal) == TCORE_HAL_MODE_AT) {
323 at = tcore_hal_get_at(hal);
324 for (l = co->callbacks; l; l = l->next) {
332 if (cb->event[0] == 27)
333 tcore_at_add_notification(at, cb->event + 1, TRUE, _on_at_event, cb);
335 tcore_at_add_notification(at, cb->event, FALSE, _on_at_event, cb);
339 return TCORE_RETURN_SUCCESS;
342 TcoreHal *tcore_object_get_hal(CoreObject *co)
350 TReturn tcore_object_link_user_data(CoreObject *co,
354 return TCORE_RETURN_EINVAL;
356 co->user_data = user_data;
358 return TCORE_RETURN_SUCCESS;
361 void *tcore_object_ref_user_data(CoreObject *co)
366 return co->user_data;
369 TReturn tcore_object_dispatch_request(CoreObject *co,
373 return TCORE_RETURN_EINVAL;
376 return TCORE_RETURN_ENOSYS;
378 return co->dispatcher(co, ur);
381 TReturn tcore_object_set_dispatcher(CoreObject *co,
382 CoreObjectDispatcher func)
385 return TCORE_RETURN_EINVAL;
387 co->dispatcher = func;
389 return TCORE_RETURN_SUCCESS;
392 TReturn tcore_object_add_callback(CoreObject *co,
394 CoreObjectCallback callback, void *user_data)
396 struct callback_type *cb = NULL;
399 if (!co || !event || !callback)
400 return TCORE_RETURN_EINVAL;
402 if (strlen(event) < 1)
403 return TCORE_RETURN_EINVAL;
405 cb = calloc(sizeof(struct callback_type), 1);
407 return TCORE_RETURN_ENOMEM;
410 cb->event = strdup(event);
411 cb->callback = callback;
412 cb->user_data = user_data;
414 co->callbacks = g_slist_append(co->callbacks, cb);
417 if (tcore_hal_get_mode(co->hal) == TCORE_HAL_MODE_AT) {
418 at = tcore_hal_get_at(co->hal);
420 tcore_at_add_notification(at, cb->event + 1, TRUE, _on_at_event, cb);
422 tcore_at_add_notification(at, cb->event, FALSE, _on_at_event, cb);
427 return TCORE_RETURN_SUCCESS;
430 TReturn tcore_object_del_callback(CoreObject *co,
431 const char *event, CoreObjectCallback callback)
433 struct callback_type *cb = NULL;
437 if (!co || !event || !callback || !co->callbacks)
438 return TCORE_RETURN_EINVAL;
440 if (strlen(event) < 1)
441 return TCORE_RETURN_EINVAL;
444 if (tcore_hal_get_mode(co->hal) == TCORE_HAL_MODE_AT)
445 at = tcore_hal_get_at(co->hal);
448 for (l = co->callbacks; l; l = l->next) {
456 if (cb->callback != callback)
459 if (g_strcmp0(cb->event, event) != 0)
463 _remove_at_callback(at, cb);
466 co->callbacks = g_slist_remove(co->callbacks, cb);
470 return TCORE_RETURN_SUCCESS;
473 TReturn tcore_object_emit_callback(CoreObject *co,
474 const char *event, const void *event_info)
476 struct callback_type *cb = NULL;
481 return TCORE_RETURN_EINVAL;
491 if (g_strcmp0(cb->event, event) != 0) {
497 ret = cb->callback(co, event_info, cb->user_data);
500 co->callbacks = g_slist_remove(co->callbacks, cb);
508 return TCORE_RETURN_SUCCESS;