refines receiver and receiver udp module
[apps/native/gear-racing-car.git] / src / receiver.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <glib.h>
19 #include "log.h"
20 #include "receiver_type.h"
21 #include "receiver_internal.h"
22 #include "receiver_udp.h"
23
24 typedef struct __receiver_h {
25         receiver_type_e receiver_type;
26         /* TODO */
27 } receiver_h;
28
29 struct __receiver_module_h {
30         receiver_type_e receiver_type;
31         void *module_data;
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;
39 };
40
41 static GHashTable *receiver_module_hash = NULL;
42
43 static void ___module_hash_destroy(gpointer data)
44 {
45         receiver_module_h *module_h = data;
46
47         module_h->fini(module_h);
48         free(module_h);
49
50         return;
51 }
52
53 int receiver_init(receiver_type_e type)
54 {
55         int ret = 0;
56         receiver_module_h *handle = NULL;
57
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);
61
62                 if (!receiver_module_hash) {
63                         _E("failed to create hash table");
64                         return -1;
65                 }
66         } else {
67                 handle = g_hash_table_lookup(receiver_module_hash,
68                         GUINT_TO_POINTER(type));
69
70                 if (handle) {
71                         _D("receiver [%d] type is already initialized", type);
72                         return 0;
73                 }
74         }
75
76         handle = calloc(1, sizeof(struct __receiver_module_h));
77         if (!handle) {
78                 _D("failed to alloc handle memory");
79                 return -1;
80         }
81
82         handle->receiver_type = type;
83
84         switch (type) {
85         case RECEIVER_TYPE_UDP:
86                 /* TODO */
87                 ret = receiver_udp_module_register(handle);
88                 if (ret)
89                         goto ERROR;
90                 break;
91         case RECEIVER_TYPE_BLUETOOTH:
92                 /* TODO : for bluetooth module */
93                 // ret = receiver_bluetooth_module_register(handle);
94                 // if (ret)
95                 //      goto ERROR;
96                 break;
97         }
98
99         if (handle->init) {
100                 ret = handle->init(handle);
101                 if (ret) {
102                         _E("failed to initialized type[%d]", type);
103                         goto ERROR;
104                 }
105         } else {
106                 _W("receiver [%d] type is not implemented init func", type);
107                 goto ERROR;
108         }
109
110         g_hash_table_insert(receiver_module_hash, GUINT_TO_POINTER(type), handle);
111
112         return 0;
113
114 ERROR:
115         free(handle);
116         return -1;
117 }
118
119 void receiver_fini(receiver_type_e type)
120 {
121         receiver_module_h *handle = NULL;
122         guint hash_size = 0;
123
124         if (!receiver_module_hash)
125                 return;
126
127         handle = g_hash_table_lookup(receiver_module_hash,
128                 GUINT_TO_POINTER(type));
129
130         if (!handle) {
131                 _D("receiver [%d] type is not initialized", type);
132                 return;
133         }
134
135         if (!handle->fini)
136                 handle->fini(handle);
137
138         g_hash_table_remove(receiver_module_hash, GUINT_TO_POINTER(type));
139
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;
144         }
145
146         return;
147 }
148
149 int receiver_start(receiver_type_e type)
150 {
151         receiver_module_h *handle = NULL;
152
153         if (!receiver_module_hash) {
154                 _E("receiver is not initialized");
155                 return -1;
156         }
157
158         handle = g_hash_table_lookup(receiver_module_hash,
159                 GUINT_TO_POINTER(type));
160
161         if (!handle) {
162                 _E("receiver [%d] type is not initialized", type);
163                 return -1;
164         }
165
166         if (!handle->start) {
167                 _D("receiver [%d] type is not implemented start func", type);
168                 return -1;
169         }
170
171         return handle->start(handle);
172 }
173
174 int receiver_stop(receiver_type_e type)
175 {
176         receiver_module_h *handle = NULL;
177
178         if (!receiver_module_hash) {
179                 _E("receiver is not initialized");
180                 return -1;
181         }
182
183         handle = g_hash_table_lookup(receiver_module_hash,
184                 GUINT_TO_POINTER(type));
185
186         if (!handle) {
187                 _E("receiver [%d] type is not initialized", type);
188                 return -1;
189         }
190
191         if (!handle->stop) {
192                 _D("receiver [%d] type is not implemented stop func", type);
193                 return -1;
194         }
195
196         return handle->stop(handle);
197 }
198
199 receiver_state_e receiver_get_state(receiver_type_e type)
200 {
201         receiver_module_h *handle = NULL;
202
203         if (!receiver_module_hash) {
204                 _E("receiver is not initialized");
205                 return RECEIVER_STATE_NONE;
206         }
207
208         handle = g_hash_table_lookup(receiver_module_hash,
209                 GUINT_TO_POINTER(type));
210
211         if (!handle) {
212                 _E("receiver [%d] type is not initialized", type);
213                 return RECEIVER_STATE_NONE;
214         }
215
216         if (!handle->get_state) {
217                 _D("receiver [%d] type is not implemented get_state func", type);
218                 return RECEIVER_STATE_NONE;
219         }
220
221         return handle->get_state(handle);
222 }
223
224 int receiver_set_state_changed_cb(receiver_type_e type,
225         receiver_state_changed_cb callback, void *user_data)
226 {
227         receiver_module_h *handle = NULL;
228
229         if (!receiver_module_hash) {
230                 _E("receiver is not initialized");
231                 return -1;
232         }
233
234         handle = g_hash_table_lookup(receiver_module_hash,
235                 GUINT_TO_POINTER(type));
236
237         if (!handle) {
238                 _E("receiver [%d] type is not initialized", type);
239                 return -1;
240         }
241
242         handle->state_change_cb = callback;
243
244         if (callback)
245                 handle->state_change_data = user_data;
246         else
247                 handle->state_change_data = NULL;
248
249         return 0;
250 }
251
252 int receiver_set_module_data(receiver_module_h *handle, void *module_data)
253 {
254         retv_if(!handle, -1);
255
256         handle->module_data = module_data;
257
258         return 0;
259 }
260
261 void *receiver_get_module_data(receiver_module_h *handle)
262 {
263         retv_if(!handle, NULL);
264
265         return handle->module_data;
266 }
267
268 int receiver_set_module_init_function(
269         receiver_module_h *handle, receiver_init_func func)
270 {
271         retv_if(!handle, -1);
272
273         handle->init = func;
274
275         return 0;
276 }
277
278 int receiver_set_module_fini_function(
279         receiver_module_h *handle, receiver_fini_func func)
280 {
281         retv_if(!handle, -1);
282
283         handle->fini = func;
284
285         return 0;
286 }
287
288 int receiver_set_module_start_function(
289         receiver_module_h *handle, receiver_start_func func)
290 {
291         retv_if(!handle, -1);
292
293         handle->start = func;
294
295         return 0;
296 }
297
298 int receiver_set_module_stop_function(
299         receiver_module_h *handle, receiver_stop_func func)
300 {
301         retv_if(!handle, -1);
302
303         handle->stop = func;
304
305         return 0;
306 }
307
308 int receiver_set_module_get_state_function(
309         receiver_module_h *handle, receiver_get_state_func func)
310 {
311         retv_if(!handle, -1);
312
313         handle->get_state = func;
314
315         return 0;
316 }
317
318 void receiver_module_state_changed(
319         receiver_module_h *handle, receiver_state_e state)
320 {
321         ret_if(!handle);
322
323         if (handle->state_change_cb)
324                 handle->state_change_cb(handle->receiver_type,
325                         state, handle->state_change_data);
326
327         return;
328 }