2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "receiver_type.h"
21 #include "receiver_internal.h"
22 #include "receiver_udp.h"
24 typedef struct __receiver_h {
25 receiver_type_e receiver_type;
29 struct __receiver_module_h {
30 receiver_type_e receiver_type;
32 receiver_init_func init;
33 receiver_fini_func fini;
34 receiver_start_func start;
35 receiver_stop_func stop;
36 receiver_get_state_func get_state;
37 receiver_state_changed_cb state_change_cb;
38 void *state_change_data;
41 static GHashTable *receiver_module_hash = NULL;
43 static void ___module_hash_destroy(gpointer data)
45 receiver_module_h *module_h = data;
47 module_h->fini(module_h);
53 int receiver_init(receiver_type_e type)
56 receiver_module_h *handle = NULL;
58 if (!receiver_module_hash) {
59 receiver_module_hash = g_hash_table_new_full(g_direct_hash,
60 g_direct_equal, NULL, ___module_hash_destroy);
62 if (!receiver_module_hash) {
63 _E("failed to create hash table");
67 handle = g_hash_table_lookup(receiver_module_hash,
68 GUINT_TO_POINTER(type));
71 _D("receiver [%d] type is already initialized", type);
76 handle = calloc(1, sizeof(struct __receiver_module_h));
78 _D("failed to alloc handle memory");
82 handle->receiver_type = type;
85 case RECEIVER_TYPE_UDP:
87 ret = receiver_udp_module_register(handle);
91 case RECEIVER_TYPE_BLUETOOTH:
92 /* TODO : for bluetooth module */
93 // ret = receiver_bluetooth_module_register(handle);
100 ret = handle->init(handle);
102 _E("failed to initialized type[%d]", type);
106 _W("receiver [%d] type is not implemented init func", type);
110 g_hash_table_insert(receiver_module_hash, GUINT_TO_POINTER(type), handle);
119 void receiver_fini(receiver_type_e type)
121 receiver_module_h *handle = NULL;
124 if (!receiver_module_hash)
127 handle = g_hash_table_lookup(receiver_module_hash,
128 GUINT_TO_POINTER(type));
131 _D("receiver [%d] type is not initialized", type);
136 handle->fini(handle);
138 g_hash_table_remove(receiver_module_hash, GUINT_TO_POINTER(type));
140 hash_size = g_hash_table_size(receiver_module_hash);
141 if (hash_size == 0) {
142 g_hash_table_unref(receiver_module_hash);
143 receiver_module_hash = NULL;
149 int receiver_start(receiver_type_e type)
151 receiver_module_h *handle = NULL;
153 if (!receiver_module_hash) {
154 _E("receiver is not initialized");
158 handle = g_hash_table_lookup(receiver_module_hash,
159 GUINT_TO_POINTER(type));
162 _E("receiver [%d] type is not initialized", type);
166 if (!handle->start) {
167 _D("receiver [%d] type is not implemented start func", type);
171 return handle->start(handle);
174 int receiver_stop(receiver_type_e type)
176 receiver_module_h *handle = NULL;
178 if (!receiver_module_hash) {
179 _E("receiver is not initialized");
183 handle = g_hash_table_lookup(receiver_module_hash,
184 GUINT_TO_POINTER(type));
187 _E("receiver [%d] type is not initialized", type);
192 _D("receiver [%d] type is not implemented stop func", type);
196 return handle->stop(handle);
199 receiver_state_e receiver_get_state(receiver_type_e type)
201 receiver_module_h *handle = NULL;
203 if (!receiver_module_hash) {
204 _E("receiver is not initialized");
205 return RECEIVER_STATE_NONE;
208 handle = g_hash_table_lookup(receiver_module_hash,
209 GUINT_TO_POINTER(type));
212 _E("receiver [%d] type is not initialized", type);
213 return RECEIVER_STATE_NONE;
216 if (!handle->get_state) {
217 _D("receiver [%d] type is not implemented get_state func", type);
218 return RECEIVER_STATE_NONE;
221 return handle->get_state(handle);
224 int receiver_set_state_changed_cb(receiver_type_e type,
225 receiver_state_changed_cb callback, void *user_data)
227 receiver_module_h *handle = NULL;
229 if (!receiver_module_hash) {
230 _E("receiver is not initialized");
234 handle = g_hash_table_lookup(receiver_module_hash,
235 GUINT_TO_POINTER(type));
238 _E("receiver [%d] type is not initialized", type);
242 handle->state_change_cb = callback;
245 handle->state_change_data = user_data;
247 handle->state_change_data = NULL;
252 int receiver_set_module_data(receiver_module_h *handle, void *module_data)
254 retv_if(!handle, -1);
256 handle->module_data = module_data;
261 void *receiver_get_module_data(receiver_module_h *handle)
263 retv_if(!handle, NULL);
265 return handle->module_data;
268 int receiver_set_module_init_function(
269 receiver_module_h *handle, receiver_init_func func)
271 retv_if(!handle, -1);
278 int receiver_set_module_fini_function(
279 receiver_module_h *handle, receiver_fini_func func)
281 retv_if(!handle, -1);
288 int receiver_set_module_start_function(
289 receiver_module_h *handle, receiver_start_func func)
291 retv_if(!handle, -1);
293 handle->start = func;
298 int receiver_set_module_stop_function(
299 receiver_module_h *handle, receiver_stop_func func)
301 retv_if(!handle, -1);
308 int receiver_set_module_get_state_function(
309 receiver_module_h *handle, receiver_get_state_func func)
311 retv_if(!handle, -1);
313 handle->get_state = func;
318 void receiver_module_state_changed(
319 receiver_module_h *handle, receiver_state_e state)
323 if (handle->state_change_cb)
324 handle->state_change_cb(handle->receiver_type,
325 state, handle->state_change_data);