merge with master
[platform/core/system/sync-agent.git] / src / framework / device-manager / mo_database_handler.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "utility/sync_util.h"
19 #include "data-adapter/common.h"
20 #include "data-adapter/agent_handler_manager.h"
21 #include "data-adapter/agent_handler_manager_internal.h"
22
23 #include "device-manager/mo_database.h"
24
25 #include "device-manager/mo_database_handler.h"
26
27 #ifndef SYNC_AGENT_LOG
28 #undef LOG_TAG
29 #define LOG_TAG "AF_MO"
30 #endif
31
32 static dm_mo_handler_mgr_s *pMoDBHandlerMgr = NULL;
33
34 /* static function define */
35 static sync_agent_dm_mo_error_e _add_agent_handler(unsigned int key, SYNC_AGENT_DA_MO_HANDLER * pHandler);
36 static sync_agent_dm_mo_error_e _remove_agent_handler(unsigned int key);
37 static SYNC_AGENT_DA_MO_HANDLER *_get_agent_handler(unsigned int key);
38 static sync_agent_dm_mo_error_e __alloc_agent_dc_handlerTable(void);
39 static sync_agent_dm_mo_error_e _alloc_agent_dc_handlerTable_mutex(void);
40 static sync_agent_dm_mo_error_e _free_agent_dc_handlerTable(void);
41 static sync_agent_dm_mo_error_e _free_agent_dc_handlerTable_mutex(void);
42 static sync_agent_dm_mo_error_e _create_agentDBHandlerMgrInfo(void);
43 static void __destroy_agentDBHashTableValue(void *pHandler);
44 static void __destory_agentDBHashTableKey(void *key);
45
46 /* general function implementation */
47 /*
48  * ==================================
49  * external API (1. about agent db handler mgr)
50  * ==================================
51  */
52 sync_agent_dm_mo_error_e dm_mo_alloc_modb_handler_mgr(void)
53 {
54         _EXTERN_FUNC_ENTER;
55
56         _DEBUG_INFO("[%s] start !\n", __func__);
57         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
58
59         /* alloc the agentDBHandlerMgr */
60         pMoDBHandlerMgr = (dm_mo_handler_mgr_s *) calloc(1, sizeof(dm_mo_handler_mgr_s));
61         if (pMoDBHandlerMgr == NULL)
62                 return SYNC_AGENT_DM_MO_FAIL;
63         _DEBUG_INFO("[%s] pMoDBHandlerMgr alloc success !\n", __func__);
64
65         /* alloc the agentDBHandlerMgr information */
66         ret = _create_agentDBHandlerMgrInfo();
67         if (ret != SYNC_AGENT_DM_MO_SUCCESS) {
68                 _DEBUG_INFO("[%s] _create_agentDBHandlerMgrInfo alloc fail !\n", __func__);
69         } else {
70                 _DEBUG_INFO("[%s] _create_agentDBHandlerMgrInfo alloc success !\n", __func__);
71         }
72
73         _DEBUG_INFO("[%s] end !\n", __func__);
74
75         _EXTERN_FUNC_EXIT;
76
77         return ret;
78 }
79
80 sync_agent_dm_mo_error_e dm_mo_free_modb_handler_mgr(void)
81 {
82         _EXTERN_FUNC_ENTER;
83
84         _DEBUG_INFO("[%s] start !\n", __func__);
85         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
86
87         if (pMoDBHandlerMgr != NULL) {
88
89                 ret = _free_agent_dc_handlerTable_mutex();
90                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
91                         return SYNC_AGENT_DM_MO_FAIL;
92
93                 ret = _free_agent_dc_handlerTable();
94                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
95                         return SYNC_AGENT_DM_MO_FAIL;
96
97         }
98
99         _DEBUG_INFO("[%s] end !\n", __func__);
100
101         _EXTERN_FUNC_EXIT;
102
103         return ret;
104 }
105
106 /*
107  * ==================================
108  * external API (2. about agent db )
109  * ==================================
110  */
111 sync_agent_dm_mo_error_e dm_mo_open(char *database_path)
112 {
113         _EXTERN_FUNC_ENTER;
114
115         _DEBUG_INFO("[%s] Start !\n", __func__);
116         int ret = 0;
117
118         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
119         if (mo_handler == NULL) {
120
121                 _DEBUG_INFO("mo_handler is 0");
122                 _DEBUG_INFO("should register mo_handler to mo_handler_hash_table");
123                 ret = mo_open_agent(&mo_handler, database_path);
124                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
125                         return ret;
126
127                 if (mo_handler != NULL) {
128
129                         /* add agentDB handler to agentDBHandleMgr */
130                         ret = _add_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID, mo_handler);
131                         if (ret != SYNC_AGENT_DM_MO_SUCCESS)
132                                 return ret;
133
134                 }
135         } else {
136                 _DEBUG_INFO("mo_handler is not null");
137                 _DEBUG_INFO("should not register mo_handler to mo_handler_hash_table");
138         }
139
140         _DEBUG_INFO("agent_db_agent_open_success");
141         _DEBUG_INFO("[%s] End !\n", __func__);
142
143         _EXTERN_FUNC_EXIT;
144
145         return SYNC_AGENT_DM_MO_SUCCESS;
146 }
147
148 sync_agent_dm_mo_error_e dm_mo_close(void)
149 {
150         _EXTERN_FUNC_ENTER;
151
152         _DEBUG_INFO("[%s] Start !\n", __func__);
153         int ret = 0;
154
155         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
156         if (mo_handler != NULL) {
157
158                 /* remove agentDB handler to agentDBHandleMgr */
159                 ret = _remove_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
160                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
161                         return ret;
162
163                 _DEBUG_INFO("agent_db_agent_close_success");
164         }
165
166         _DEBUG_INFO("[%s] End !\n", __func__);
167
168         _EXTERN_FUNC_EXIT;
169
170         return SYNC_AGENT_DM_MO_SUCCESS;
171 }
172
173 sync_agent_dm_mo_error_e dm_mo_create_mo_table_wraper(void)
174 {
175         _EXTERN_FUNC_ENTER;
176
177         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
178         if (mo_handler == NULL) {
179                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
180                 return SYNC_AGENT_DM_MO_FAIL;
181         }
182
183         _EXTERN_FUNC_EXIT;
184
185         return dm_mo_create_mo_table(mo_handler);
186 }
187
188 sync_agent_dm_mo_error_e dm_mo_begin_transaction_wraper(void)
189 {
190         _EXTERN_FUNC_ENTER;
191
192         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
193         if (mo_handler == NULL) {
194                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
195                 return SYNC_AGENT_DM_MO_FAIL;
196         }
197
198         _EXTERN_FUNC_EXIT;
199
200         return dm_mo_begin_transaction(mo_handler);
201 }
202
203 sync_agent_dm_mo_error_e dm_mo_end_transaction_wraper(sync_agent_da_transaction_e transaction)
204 {
205         _EXTERN_FUNC_ENTER;
206
207         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
208         if (mo_handler == NULL) {
209                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
210                 return SYNC_AGENT_DM_MO_FAIL;
211         }
212
213         _EXTERN_FUNC_EXIT;
214
215         return dm_mo_end_transaction(mo_handler, transaction);
216 }
217
218 sync_agent_dm_mo_error_e dm_mo_add_node_wraper(sync_agent_dm_mo_type_e mo_type, sync_agent_dm_mo_node_s * mo_node)
219 {
220         _EXTERN_FUNC_ENTER;
221
222         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
223         if (mo_handler == NULL) {
224                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
225                 return SYNC_AGENT_DM_MO_FAIL;
226         }
227
228         _EXTERN_FUNC_EXIT;
229
230         return dm_mo_add_node(mo_handler, mo_type, mo_node);
231 }
232
233 sync_agent_dm_mo_error_e dm_mo_delete_node_wraper(int mo_node_id)
234 {
235         _EXTERN_FUNC_ENTER;
236
237         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
238         if (mo_handler == NULL) {
239                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
240                 return SYNC_AGENT_DM_MO_FAIL;
241         }
242
243         _EXTERN_FUNC_EXIT;
244
245         return dm_mo_delete_node(mo_handler, mo_node_id);
246 }
247
248 sync_agent_dm_mo_error_e dm_mo_update_node_wraper(sync_agent_dm_mo_type_e mo_type, const char *path, const sync_agent_dm_mo_node_s * mo_node)
249 {
250         _EXTERN_FUNC_ENTER;
251
252         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
253         if (mo_handler == NULL) {
254                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
255                 return SYNC_AGENT_DM_MO_FAIL;
256         }
257
258         _EXTERN_FUNC_EXIT;
259
260         return dm_mo_update_node(mo_handler, mo_type, path, mo_node);
261 }
262
263 sync_agent_dm_mo_error_e dm_mo_get_node_wraper(const char *path, sync_agent_dm_mo_node_s ** mo_node)
264 {
265         _EXTERN_FUNC_ENTER;
266
267         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
268         if (mo_handler == NULL) {
269                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
270                 return SYNC_AGENT_DM_MO_FAIL;
271         }
272
273         _EXTERN_FUNC_EXIT;
274
275         return dm_mo_get_node(mo_handler, path, mo_node);
276 }
277
278 sync_agent_dm_mo_error_e dm_mo_get_nodes_wraper(const char *path, sync_agent_dm_mo_node_s ** mo_node)
279 {
280         _EXTERN_FUNC_ENTER;
281
282         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
283         if (mo_handler == NULL) {
284                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
285                 return SYNC_AGENT_DM_MO_FAIL;
286         }
287
288         _EXTERN_FUNC_EXIT;
289
290         return dm_mo_get_nodes(mo_handler, path, mo_node);
291 }
292
293 sync_agent_dm_mo_error_e dm_mo_is_exist_node_wraper(const char *path)
294 {
295         _EXTERN_FUNC_ENTER;
296
297         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
298         if (mo_handler == NULL) {
299                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
300                 return SYNC_AGENT_DM_MO_FAIL;
301         }
302
303         _EXTERN_FUNC_EXIT;
304
305         return dm_mo_is_exist_node(mo_handler, path);
306 }
307
308 sync_agent_dm_mo_error_e dm_mo_get_child_node_wraper(sync_agent_dm_mo_type_e mo_type, const char *path, sync_agent_dm_mo_node_s ** mo_node, int *count)
309 {
310         _EXTERN_FUNC_ENTER;
311
312         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
313         if (mo_handler == NULL) {
314                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
315                 return SYNC_AGENT_DM_MO_FAIL;
316         }
317
318         _EXTERN_FUNC_EXIT;
319
320         return dm_mo_get_child_node(mo_type, mo_handler, path, mo_node, count);
321 }
322
323 sync_agent_dm_mo_error_e dm_mo_delete_all_node_wraper(void)
324 {
325         _EXTERN_FUNC_ENTER;
326
327         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
328         if (mo_handler == NULL) {
329                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
330                 return SYNC_AGENT_DM_MO_FAIL;
331         }
332
333         _EXTERN_FUNC_EXIT;
334
335         return dm_mo_delete_all_node(mo_handler);
336 }
337
338 sync_agent_dm_mo_type_e dm_mo_get_mo_type_wraper(const char *mo_full_path)
339 {
340         _EXTERN_FUNC_ENTER;
341
342         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
343         if (mo_handler == NULL) {
344                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
345                 return SYNC_AGENT_DM_MO_FAIL;
346         }
347
348         _EXTERN_FUNC_EXIT;
349
350         return dm_mo_get_mo_type(mo_handler, mo_full_path);
351 }
352
353 sync_agent_dm_mo_error_e dm_mo_add_framework_property_wraper(int mo_node_id, sync_agent_dm_mo_framework_property_s * framework_property)
354 {
355         _EXTERN_FUNC_ENTER;
356
357         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
358         if (mo_handler == NULL) {
359                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
360                 return SYNC_AGENT_DM_MO_FAIL;
361         }
362
363         _EXTERN_FUNC_EXIT;
364
365         return dm_mo_add_framework_property(mo_handler, mo_node_id, framework_property);
366 }
367
368 sync_agent_dm_mo_error_e dm_mo_get_node_from_id_wraper(int node_id, sync_agent_dm_mo_node_s ** mo_node)
369 {
370         _EXTERN_FUNC_ENTER;
371
372         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
373         if (mo_handler == NULL) {
374                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
375                 return SYNC_AGENT_DM_MO_FAIL;
376         }
377
378         _EXTERN_FUNC_EXIT;
379
380         return dm_mo_get_node_from_id(mo_handler, node_id, mo_node);
381 }
382
383 sync_agent_dm_mo_error_e dm_mo_get_acc_info_wraper(const char *server_id, const char *server_id_string, const char *acc_type_string, sync_agent_dm_acc_info_s ** acc_info)
384 {
385         _EXTERN_FUNC_ENTER;
386
387         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
388         if (mo_handler == NULL) {
389                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
390                 return SYNC_AGENT_DM_MO_FAIL;
391         }
392
393         _EXTERN_FUNC_EXIT;
394
395         return dm_mo_get_acc_info(mo_handler, server_id, server_id_string, acc_type_string, acc_info);
396 }
397
398 sync_agent_dm_mo_error_e dm_mo_set_acc_info_wraper(const char *server_id, const char *server_id_string, const char *acc_type_string, sync_agent_dm_acc_info_s * acc_info)
399 {
400         _EXTERN_FUNC_ENTER;
401
402         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
403         if (mo_handler == NULL) {
404                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
405                 return SYNC_AGENT_DM_MO_FAIL;
406         }
407
408         _EXTERN_FUNC_EXIT;
409
410         return dm_mo_set_acc_info(mo_handler, server_id, server_id_string, acc_type_string, acc_info);
411 }
412
413 sync_agent_dm_mo_error_e dm_mo_get_server_id_list_wraper(const char *server_id_string, sync_agent_dm_server_info_s ** head_ptr)
414 {
415         _EXTERN_FUNC_ENTER;
416
417         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
418         if (mo_handler == NULL) {
419                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
420                 return SYNC_AGENT_DM_MO_FAIL;
421         }
422
423         _EXTERN_FUNC_EXIT;
424
425         return dm_mo_get_server_id_list(mo_handler, server_id_string, head_ptr);
426 }
427
428 sync_agent_dm_mo_error_e dm_mo_get_root_path_wraper(const char *mo_full_path, char **root_path)
429 {
430         _EXTERN_FUNC_ENTER;
431
432         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
433         if (mo_handler == NULL) {
434                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
435                 return SYNC_AGENT_DM_MO_FAIL;
436         }
437
438         _EXTERN_FUNC_EXIT;
439
440         return dm_mo_get_root_path(mo_handler, mo_full_path, root_path);
441 }
442
443 sync_agent_dm_mo_error_e dm_mo_delete_framework_property_wraper(int mo_node_id)
444 {
445         _EXTERN_FUNC_ENTER;
446
447         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
448         if (mo_handler == NULL) {
449                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
450                 return SYNC_AGENT_DM_MO_FAIL;
451         }
452
453         _EXTERN_FUNC_EXIT;
454
455         return dm_mo_delete_framework_property(mo_handler, mo_node_id);
456 }
457
458 sync_agent_dm_mo_error_e dm_mo_update_framework_property_wraper(int mo_node_id, sync_agent_dm_mo_framework_property_s * framework_property)
459 {
460         _EXTERN_FUNC_ENTER;
461
462         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
463         if (mo_handler == NULL) {
464                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
465                 return SYNC_AGENT_DM_MO_FAIL;
466         }
467
468         _EXTERN_FUNC_EXIT;
469
470         return dm_mo_update_framework_property(mo_handler, mo_node_id, framework_property);
471 }
472
473 sync_agent_dm_mo_error_e dm_mo_get_framework_property_wraper(int mo_node_id, sync_agent_dm_mo_framework_property_s ** framework_property)
474 {
475         _EXTERN_FUNC_ENTER;
476
477         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
478         if (mo_handler == NULL) {
479                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
480                 return SYNC_AGENT_DM_MO_FAIL;
481         }
482
483         _EXTERN_FUNC_EXIT;
484
485         return dm_mo_get_framework_property(mo_handler, mo_node_id, framework_property);
486 }
487
488 sync_agent_dm_mo_error_e dm_mo_delete_all_framework_property_wraper(void)
489 {
490         _EXTERN_FUNC_ENTER;
491
492         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
493         if (mo_handler == NULL) {
494                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
495                 return SYNC_AGENT_DM_MO_FAIL;
496         }
497
498         _EXTERN_FUNC_EXIT;
499
500         return dm_mo_delete_all_framework_property(mo_handler);
501 }
502
503 sync_agent_dm_mo_error_e dm_mo_add_runtime_property_wraper(int mo_node_id, sync_agent_dm_mo_runtime_property_s * runtime_property)
504 {
505         _EXTERN_FUNC_ENTER;
506
507         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
508         if (mo_handler == NULL) {
509                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
510                 return SYNC_AGENT_DM_MO_FAIL;
511         }
512
513         _EXTERN_FUNC_EXIT;
514
515         return dm_mo_add_runtime_property(mo_handler, mo_node_id, runtime_property);
516 }
517
518 sync_agent_dm_mo_error_e dm_mo_delete_runtime_property_wraper(int mo_node_id)
519 {
520         _EXTERN_FUNC_ENTER;
521
522         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
523         if (mo_handler == NULL) {
524                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
525                 return SYNC_AGENT_DM_MO_FAIL;
526         }
527
528         _EXTERN_FUNC_EXIT;
529
530         return dm_mo_delete_runtime_property(mo_handler, mo_node_id);
531 }
532
533 sync_agent_dm_mo_error_e dm_mo_update_runtime_property_wraper(int mo_node_id, sync_agent_dm_mo_runtime_property_s * runtime_property)
534 {
535         _EXTERN_FUNC_ENTER;
536
537         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
538         if (mo_handler == NULL) {
539                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
540                 return SYNC_AGENT_DM_MO_FAIL;
541         }
542
543         _EXTERN_FUNC_EXIT;
544
545         return dm_mo_update_runtime_property(mo_handler, mo_node_id, runtime_property);
546 }
547
548 sync_agent_dm_mo_error_e dm_mo_get_runtime_property_wraper(int mo_node_id, sync_agent_dm_mo_runtime_property_s ** runtime_property)
549 {
550         _EXTERN_FUNC_ENTER;
551
552         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
553         if (mo_handler == NULL) {
554                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
555                 return SYNC_AGENT_DM_MO_FAIL;
556         }
557
558         _EXTERN_FUNC_EXIT;
559
560         return dm_mo_get_runtime_property(mo_handler, mo_node_id, runtime_property);
561 }
562
563 sync_agent_dm_mo_error_e dm_mo_delete_all_runtime_property_wraper(void)
564 {
565         _EXTERN_FUNC_ENTER;
566
567         SYNC_AGENT_DA_MO_HANDLER *mo_handler = _get_agent_handler(SYNC_AGENT_DA_GET_THREAD_ID);
568         if (mo_handler == NULL) {
569                 _DEBUG_ERROR("[%s] no handler for key\n", __func__);
570                 return SYNC_AGENT_DM_MO_FAIL;
571         }
572
573         _EXTERN_FUNC_EXIT;
574
575         return dm_mo_delete_all_runtime_property(mo_handler);
576 }
577
578 /*
579  * ==================================
580  *  Mo module internal API
581  * ==================================
582  */
583 sync_agent_dm_mo_error_e _add_agent_handler(unsigned int key, SYNC_AGENT_DA_MO_HANDLER * pHandler)
584 {
585         _INNER_FUNC_ENTER;
586
587         if (pMoDBHandlerMgr == NULL || pMoDBHandlerMgr->mo_handlerTable_mutex == NULL) {
588                 return SYNC_AGENT_DM_MO_FAIL;
589         }
590
591         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
592
593         unsigned int *pKey_copy = (unsigned int *)malloc(sizeof(unsigned int));
594         if (pKey_copy == NULL)
595                 return SYNC_AGENT_DM_MO_FAIL;
596
597         *pKey_copy = key;
598
599         pthread_mutex_lock(pMoDBHandlerMgr->mo_handlerTable_mutex);
600         if (pMoDBHandlerMgr->mo_handlerTable == NULL) {
601                 ret = __alloc_agent_dc_handlerTable();
602                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
603                         goto addagentHandler;
604         }
605
606         /* insert to handler */
607         _DEBUG_TRACE("[%s] Key  =  %d, Handler  =  %p  !\n", __func__, key, pHandler);
608         g_hash_table_insert(pMoDBHandlerMgr->mo_handlerTable, pKey_copy, pHandler);
609
610 #ifdef DC_PRINT
611         /* For test log */
612         __print_agentDBHashTableLog();
613 #endif
614
615  addagentHandler:
616         pthread_mutex_unlock(pMoDBHandlerMgr->mo_handlerTable_mutex);
617
618         _INNER_FUNC_EXIT;
619
620         return ret;
621 }
622
623 sync_agent_dm_mo_error_e _remove_agent_handler(unsigned int key)
624 {
625         _INNER_FUNC_ENTER;
626
627         if (pMoDBHandlerMgr == NULL || pMoDBHandlerMgr->mo_handlerTable_mutex == NULL || pMoDBHandlerMgr->mo_handlerTable == NULL) {
628                 return SYNC_AGENT_DM_MO_FAIL;
629         }
630
631         /* remove the handler to hash table */
632         pthread_mutex_lock(pMoDBHandlerMgr->mo_handlerTable_mutex);
633         int isSuccess = g_hash_table_remove(pMoDBHandlerMgr->mo_handlerTable, &key);
634         pthread_mutex_unlock(pMoDBHandlerMgr->mo_handlerTable_mutex);
635
636 #ifdef DC_PRINT
637         /* for test log */
638         __print_agentDBHashTableLog();
639 #endif
640
641         _INNER_FUNC_EXIT;
642
643         if (isSuccess == 1)
644                 return SYNC_AGENT_DM_MO_SUCCESS;
645
646         return SYNC_AGENT_DM_MO_FAIL;
647 }
648
649 SYNC_AGENT_DA_MO_HANDLER *_get_agent_handler(unsigned int key)
650 {
651         _INNER_FUNC_ENTER;
652
653         if (pMoDBHandlerMgr == NULL || pMoDBHandlerMgr->mo_handlerTable_mutex == NULL || pMoDBHandlerMgr->mo_handlerTable == NULL) {
654                 _DEBUG_TRACE("[%s] SYNC_AGENT_DM_MO_FAIL !\n", __func__);
655                 return NULL;
656         }
657
658         /* get the handler to hash table */
659         pthread_mutex_lock(pMoDBHandlerMgr->mo_handlerTable_mutex);
660         SYNC_AGENT_DA_MO_HANDLER *pHandler = g_hash_table_lookup(pMoDBHandlerMgr->mo_handlerTable, &key);
661         pthread_mutex_unlock(pMoDBHandlerMgr->mo_handlerTable_mutex);
662
663 #ifdef DC_PRINT
664         if (pHandler != 0) {
665                 _DEBUG_TRACE("[%s] ######### > Value Search Success(key = %d, handler = %p)\n", __func__, key, pHandler);
666         } else {
667                 _DEBUG_TRACE("[%s] ######### > Value Search Fail(key = %d, handler = %p)\n", __func__, key, pHandler);
668         }
669 #endif
670
671         _INNER_FUNC_EXIT;
672
673         return pHandler;
674 }
675
676 /* static function implementation */
677 static sync_agent_dm_mo_error_e __alloc_agent_dc_handlerTable(void)
678 {
679         _INNER_FUNC_ENTER;
680
681         if (pMoDBHandlerMgr != NULL) {
682                 pMoDBHandlerMgr->mo_handlerTable = g_hash_table_new_full(g_int_hash, g_int_equal, __destory_agentDBHashTableKey, __destroy_agentDBHashTableValue);
683                 if (pMoDBHandlerMgr->mo_handlerTable == NULL) {
684                         _DEBUG_ERROR("[%s] Not Create agent dc handler hash table \n", __func__);
685                         return SYNC_AGENT_DM_MO_FAIL;
686                 } else {
687                         _DEBUG_VERBOSE("[%MO_FAILent dc handler hash table \n", __func__);
688                 }
689         }
690
691         _INNER_FUNC_EXIT;
692
693         return SYNC_AGENT_DM_MO_SUCCESS;
694 }
695
696 static sync_agent_dm_mo_error_e _alloc_agent_dc_handlerTable_mutex(void)
697 {
698         _INNER_FUNC_ENTER;
699
700         if (pMoDBHandlerMgr != NULL) {
701
702                 pMoDBHandlerMgr->mo_handlerTable_mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
703                 if (pMoDBHandlerMgr->mo_handlerTable_mutex == NULL) {
704                         _DEBUG_TRACE("[%s] Not Create agent dc handler hash table mutex \n", __func__);
705                         return SYNC_AGENT_DM_MO_FAIL;
706                 } else {
707
708                         if (0 != pthread_mutex_init(pMoDBHandlerMgr->mo_handlerTable_mutex, 0)) {
709                                 _DEBUG_TRACE("[%s] Not init agent dc handler hash table mutex\n", __func__);
710                                 return SYNC_AGENT_DM_MO_FAIL;
711                         }
712                         _DEBUG_TRACE("[%s] Init agent dc handler hash table mutex\n", __func__);
713                 }
714         }
715
716         _INNER_FUNC_EXIT;
717
718         return SYNC_AGENT_DM_MO_SUCCESS;
719 }
720
721 static sync_agent_dm_mo_error_e _free_agent_dc_handlerTable(void)
722 {
723         _INNER_FUNC_ENTER;
724
725         if (pMoDBHandlerMgr != NULL && pMoDBHandlerMgr->mo_handlerTable != NULL) {
726                 g_hash_table_destroy(pMoDBHandlerMgr->mo_handlerTable);
727                 _DEBUG_TRACE("[%s] mo_handlerTable free success !\n", __func__);
728                 pMoDBHandlerMgr->mo_handlerTable = NULL;
729         }
730
731         _INNER_FUNC_EXIT;
732
733         return SYNC_AGENT_DM_MO_SUCCESS;
734 }
735
736 static sync_agent_dm_mo_error_e _free_agent_dc_handlerTable_mutex(void)
737 {
738         _INNER_FUNC_ENTER;
739
740         if (pMoDBHandlerMgr != NULL && pMoDBHandlerMgr->mo_handlerTable_mutex != NULL) {
741
742                 int ret = pthread_mutex_destroy(pMoDBHandlerMgr->mo_handlerTable_mutex);
743                 if (ret != 0) {
744                         _DEBUG_TRACE("[%s] mo_handlerTable_mutex free error !\n", __func__);
745                         return SYNC_AGENT_DM_MO_FAIL;
746                 } else {
747                         free(pMoDBHandlerMgr->mo_handlerTable_mutex);
748                         pMoDBHandlerMgr->mo_handlerTable_mutex = NULL;
749                         _DEBUG_TRACE("[%s] mo_handlerTable_mutex free success !\n", __func__);
750                 }
751         }
752
753         _INNER_FUNC_EXIT;
754
755         return SYNC_AGENT_DM_MO_SUCCESS;
756 }
757
758 static sync_agent_dm_mo_error_e _create_agentDBHandlerMgrInfo(void)
759 {
760         _INNER_FUNC_ENTER;
761
762         sync_agent_dm_mo_error_e ret = SYNC_AGENT_DM_MO_SUCCESS;
763
764         if (pMoDBHandlerMgr != NULL) {
765
766                 /* alloc agent db hander hash table */
767                 ret = __alloc_agent_dc_handlerTable();
768                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
769                         goto allocAgentDBHandlerMgrInfoErr;
770
771                 /* alloc agent db hander hash table mutex */
772                 ret = _alloc_agent_dc_handlerTable_mutex();
773                 if (ret != SYNC_AGENT_DM_MO_SUCCESS)
774                         goto allocAgentDBHandlerMgrInfoErr;
775         }
776
777         _INNER_FUNC_EXIT;
778
779         return SYNC_AGENT_DM_MO_SUCCESS;
780
781  allocAgentDBHandlerMgrInfoErr:
782         da_free_agentdb_handler_mgr();
783         return SYNC_AGENT_DM_MO_FAIL;
784 }
785
786 static void __destroy_agentDBHashTableValue(void *pHandler)
787 {
788         _INNER_FUNC_ENTER;
789
790         _DEBUG_VERBOSE("[%s] start \n", __func__);
791         if (pHandler != NULL) {
792                 mo_close_agent((SYNC_AGENT_DA_MO_HANDLER *) pHandler);
793         }
794         _DEBUG_VERBOSE("[%s] end \n", __func__);
795
796         _INNER_FUNC_EXIT;
797 }
798
799 static void __destory_agentDBHashTableKey(void *key)
800 {
801         _INNER_FUNC_ENTER;
802
803         _DEBUG_VERBOSE("[%s] start \n", __func__);
804         if (key != NULL)
805                 free(key);
806         _DEBUG_VERBOSE("[%s] end \n", __func__);
807
808         _INNER_FUNC_EXIT;
809 }