3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sync_agent.h>
22 #include "common/dm-status/oma_dm_status_db_handler.h"
23 #include "common/dm-status/oma_dm_status_db.h"
25 #ifndef OMADM_AGENT_LOG
27 #define LOG_TAG "OMA_DM_DB"
30 static db_handler_mgr *pDBHandlerMgr = 0;
32 /* static function define */
33 static DM_ERROR _add_agent_handler(unsigned int key, DB_HANDLER * pHandler);
34 static DM_ERROR _remove_agent_handler(unsigned int key);
35 static DB_HANDLER *_get_agent_handler(unsigned int key);
36 static DM_ERROR __alloc_agent_dc_handlerTable();
37 static DM_ERROR __alloc_agent_dc_handlerTable_mutex();
38 static DM_ERROR _free_agent_dc_handlerTable();
39 static DM_ERROR _free_agent_dc_handlerTable_mutex();
40 static DM_ERROR _create_agentDBHandlerMgrInfo();
41 static void ___destroy_agentDBHashTableValue(void *pHandler);
42 static void ___destory_agentDBHashTableKey(void *key);
44 /* test function define*/
45 /*static void __print_agentDBHashTableLog();*/
47 /* general function implementation */
49 * ==================================
50 * external API (1. about db handler mgr)
51 * ==================================
53 DM_ERROR Alloc_DB_Handler_Mgr()
59 /* alloc the agentDBHandlerMgr */
60 pDBHandlerMgr = (db_handler_mgr *) calloc(1, sizeof(db_handler_mgr));
61 if (pDBHandlerMgr == NULL) {
62 _DEBUG_INFO("alloc fail");
64 return COMMON_ERR_ALLOC;
66 _DEBUG_INFO("[%s] pMoDBHandlerMgr alloc success !\n", __func__);
68 /* alloc the agentDBHandlerMgr information */
69 ret = _create_agentDBHandlerMgrInfo();
71 _DEBUG_INFO("[%s] __create_agentDBHandlerMgrInfo alloc fail !\n", __func__);
73 _DEBUG_INFO("[%s] __create_agentDBHandlerMgrInfo alloc success !\n", __func__);
79 DM_ERROR Free_DB_Handler_Mgr()
84 if (pDBHandlerMgr != NULL) {
86 ret = _free_agent_dc_handlerTable_mutex();
89 return DM_ERR_UNKNOWN;
92 ret = _free_agent_dc_handlerTable();
95 return DM_ERR_UNKNOWN;
103 void Free_Memory_Engine_Status(engine_status ** status, int count)
106 engine_status *temp_status = *status;
109 for (i = 0; i < count; i++) {
110 MEMORY_FREE(temp_status[i].server_id);
111 MEMORY_FREE(temp_status[i].server_url);
112 MEMORY_FREE(temp_status[i].correlator);
113 MEMORY_FREE(temp_status[i].ui_mode);
114 MEMORY_FREE(temp_status[i].mo_path);
117 MEMORY_FREE(temp_status);
123 * ==================================
124 * external API (2. about agent db )
125 * ==================================
132 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
133 if (db_handler == NULL) {
135 _DEBUG_INFO("db_handler is 0");
136 _DEBUG_INFO("should register db_handler to db_handler_hash_table");
137 ret = dm_open_agent(&db_handler);
143 if (db_handler != NULL) {
144 /* add agentDB handler to agentDBHandleMgr */
145 ret = _add_agent_handler(GET_THREAD_ID, db_handler);
152 _DEBUG_INFO("db_handler is not null");
153 _DEBUG_INFO("should not register db_handler to db_handler_hash_table");
156 _DEBUG_INFO("agent_db_agent_open_success");
165 DM_ERROR ret = DM_OK;
167 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
168 if (db_handler != NULL) {
170 /* remove agentDB handler to agentDBHandleMgr */
171 ret = _remove_agent_handler(GET_THREAD_ID);
173 _DEBUG_INFO("agent_db_agent_close_fail");
177 _DEBUG_INFO("agent_db_agent_close_success");
182 DM_ERROR DB_Create_Table()
186 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
187 if (db_handler == NULL) {
188 _DEBUG_INFO("[%s] no handler for key\n", __func__);
190 return DM_ERR_UNKNOWN;
193 return dm_create_table(db_handler);
196 DM_ERROR DB_Delete_Table()
200 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
201 if (db_handler == NULL) {
202 _DEBUG_INFO("[%s] no handler for key\n", __func__);
204 return DM_ERR_UNKNOWN;
207 return dm_delete_table(db_handler);
210 DM_ERROR DB_Reset_Table()
214 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
215 if (db_handler == NULL) {
216 _DEBUG_INFO("[%s] no handler for key\n", __func__);
218 return DM_ERR_UNKNOWN;
221 return dm_reset_table(db_handler);
224 DM_ERROR DB_Begin_Transaction()
228 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
229 if (db_handler == NULL) {
230 _DEBUG_INFO("[%s] no handler for key\n", __func__);
232 return DM_ERR_UNKNOWN;
235 return dm_begin_transaction(db_handler);
238 DM_ERROR DB_End_Transaction(DB_TRANSACTION transaction)
242 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
243 if (db_handler == NULL) {
244 _DEBUG_INFO("[%s] no handler for key\n", __func__);
246 return DM_ERR_UNKNOWN;
249 return dm_end_transaction(db_handler, transaction);
252 DM_ERROR Init_Engine_Status()
256 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
257 if (db_handler == NULL) {
258 _DEBUG_INFO("[%s] no handler for key\n", __func__);
260 return DM_ERR_UNKNOWN;
263 return dm_init_engine_status(db_handler);
266 DM_ERROR Add_Engine_Status(engine_status * status)
270 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
271 if (db_handler == NULL) {
272 _DEBUG_INFO("[%s] no handler for key\n", __func__);
274 return DM_ERR_UNKNOWN;
277 return dm_add_engine_status(db_handler, status);
280 DM_ERROR Delete_Engine_Status(int engine_id)
284 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
285 if (db_handler == NULL) {
286 _DEBUG_INFO("[%s] no handler for key\n", __func__);
288 return DM_ERR_UNKNOWN;
292 return dm_delete_engine_status(db_handler, engine_id);
295 DM_ERROR Update_Engine_Status(engine_status * status)
299 retvm_if(status == NULL, DM_ERR_UNKNOWN, "engine status is NULL !!");
301 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
302 if (db_handler == NULL) {
303 _DEBUG_INFO("[%s] no handler for key\n", __func__);
305 return DM_ERR_UNKNOWN;
308 int ret = dm_isExist_engine_id(db_handler, status->engine_id);
311 return dm_add_engine_status(db_handler, status);
314 return dm_update_engine_status(db_handler, status);
318 DM_ERROR Update_Engine_Status_Column(int engine_id, ENGINE_STATUS_VALUE engine_status_value, void *value)
322 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
323 if (db_handler == NULL) {
324 _DEBUG_INFO("[%s] no handler for key\n", __func__);
326 return DM_ERR_UNKNOWN;
329 return dm_update_engine_status_column(db_handler, engine_id, engine_status_value, value);
332 DM_ERROR Get_Engine_Status(int engine_id, engine_status ** status)
336 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
337 if (db_handler == NULL) {
338 _DEBUG_INFO("[%s] no handler for key\n", __func__);
340 return DM_ERR_UNKNOWN;
343 return dm_get_engine_status(db_handler, engine_id, status);
346 DM_ERROR Get_AII_Engine_Status(engine_status ** status_list, int *count)
350 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
351 if (db_handler == NULL) {
352 _DEBUG_INFO("[%s] no handler for key\n", __func__);
354 return DM_ERR_UNKNOWN;
358 return dm_get_aII_engine_status(db_handler, status_list, count);
361 int IsExist_Engine_id(int engine_id)
365 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
366 if (db_handler == NULL) {
367 _DEBUG_INFO("[%s] no handler for key\n", __func__);
369 return DM_ERR_UNKNOWN;
374 return dm_isExist_engine_id(db_handler, engine_id);
378 * ==================================
379 * DB module internal API
380 * ==================================
382 DM_ERROR _add_agent_handler(unsigned int key, DB_HANDLER * pHandler)
386 if (pDBHandlerMgr == NULL || pDBHandlerMgr->db_handlerTable_mutex == NULL) {
387 return DM_ERR_UNKNOWN;
390 DM_ERROR ret = DM_OK;
392 unsigned int *pKey_copy = (unsigned int *)calloc(1, sizeof(unsigned int));
393 if (pKey_copy == NULL) {
395 return COMMON_ERR_ALLOC;
400 pthread_mutex_lock(pDBHandlerMgr->db_handlerTable_mutex);
401 if (pDBHandlerMgr->db_handlerTable == NULL) {
402 ret = __alloc_agent_dc_handlerTable();
404 goto addagentHandler;
407 /* insert to handler */
408 _DEBUG_TRACE("[%s] Key = %d, Handler = %p !\n", __func__, key, pHandler);
409 g_hash_table_insert(pDBHandlerMgr->db_handlerTable, pKey_copy, pHandler);
413 __print_agentDBHashTableLog();
415 pthread_mutex_unlock(pDBHandlerMgr->db_handlerTable_mutex);
416 if(pKey_copy != NULL) {
423 pthread_mutex_unlock(pDBHandlerMgr->db_handlerTable_mutex);
424 if(pKey_copy != NULL) {
431 DM_ERROR _remove_agent_handler(unsigned int key)
435 if (pDBHandlerMgr == NULL || pDBHandlerMgr->db_handlerTable_mutex == NULL || pDBHandlerMgr->db_handlerTable == NULL) {
437 return DM_ERR_UNKNOWN;
440 /* remove the handler to hash table */
441 pthread_mutex_lock(pDBHandlerMgr->db_handlerTable_mutex);
442 int isSuccess = g_hash_table_remove(pDBHandlerMgr->db_handlerTable, &key);
443 pthread_mutex_unlock(pDBHandlerMgr->db_handlerTable_mutex);
447 __print_agentDBHashTableLog();
450 _DEBUG_TRACE("[%s] end !\n", __func__);
451 if (isSuccess == 1) {
458 return DM_ERR_UNKNOWN;
461 DB_HANDLER *_get_agent_handler(unsigned int key)
465 if (pDBHandlerMgr == NULL || pDBHandlerMgr->db_handlerTable_mutex == NULL || pDBHandlerMgr->db_handlerTable == NULL) {
466 _DEBUG_TRACE("[%s] DM_ERR_UNKNOWN !\n", __func__);
472 /* get the handler to hash table */
473 pthread_mutex_lock(pDBHandlerMgr->db_handlerTable_mutex);
474 DB_HANDLER *pHandler = g_hash_table_lookup(pDBHandlerMgr->db_handlerTable, &key);
475 pthread_mutex_unlock(pDBHandlerMgr->db_handlerTable_mutex);
478 if (pHandler != NULL) {
479 _DEBUG_TRACE("[%s] ######### > Value Search Success(key = %d, handler = %p)\n", __func__, key, pHandler);
481 _DEBUG_TRACE("[%s] ######### > Value Search Fail(key = %d, handler = %p)\n", __func__, key, pHandler);
489 /* static function implementation */
490 static DM_ERROR __alloc_agent_dc_handlerTable()
494 if (pDBHandlerMgr != NULL) {
495 pDBHandlerMgr->db_handlerTable = g_hash_table_new_full(g_int_hash, g_int_equal, ___destory_agentDBHashTableKey, ___destroy_agentDBHashTableValue);
496 if (pDBHandlerMgr->db_handlerTable == NULL) {
497 _DEBUG_TRACE("[%s] Not Create agent dc handler hash table \n", __func__);
499 return DM_ERR_UNKNOWN;
501 _DEBUG_TRACE("[%s] create agent dc handler hash table \n", __func__);
510 static DM_ERROR __alloc_agent_dc_handlerTable_mutex()
514 if (pDBHandlerMgr != NULL) {
516 pDBHandlerMgr->db_handlerTable_mutex = (pthread_mutex_t *) calloc(1, sizeof(pthread_mutex_t));
517 if (pDBHandlerMgr->db_handlerTable_mutex == NULL) {
518 _DEBUG_TRACE("[%s] Not Create agent dc handler hash table mutex \n", __func__);
520 return COMMON_ERR_ALLOC;
523 if (pthread_mutex_init(pDBHandlerMgr->db_handlerTable_mutex, 0) != 0) {
524 _DEBUG_TRACE("[%s] Not init agent dc handler hash table mutex\n", __func__);
526 return DM_ERR_UNKNOWN;
528 _DEBUG_TRACE("[%s] Init agent dc handler hash table mutex\n", __func__);
537 static DM_ERROR _free_agent_dc_handlerTable()
541 if (pDBHandlerMgr != NULL && pDBHandlerMgr->db_handlerTable != NULL) {
542 g_hash_table_destroy(pDBHandlerMgr->db_handlerTable);
543 _DEBUG_TRACE("[%s] db_handlerTable free success !\n", __func__);
544 pDBHandlerMgr->db_handlerTable = 0;;
551 static DM_ERROR _free_agent_dc_handlerTable_mutex()
555 if (pDBHandlerMgr != NULL && pDBHandlerMgr->db_handlerTable_mutex != NULL) {
557 int ret = pthread_mutex_destroy(pDBHandlerMgr->db_handlerTable_mutex);
559 _DEBUG_TRACE("[%s] db_handlerTable_mutex free error !\n", __func__);
560 return DM_ERR_UNKNOWN;
562 free(pDBHandlerMgr->db_handlerTable_mutex);
563 pDBHandlerMgr->db_handlerTable_mutex = 0;
564 _DEBUG_TRACE("[%s] db_handlerTable_mutex free success !\n", __func__);
572 static DM_ERROR _create_agentDBHandlerMgrInfo()
576 DM_ERROR ret = DM_OK;
578 if (pDBHandlerMgr != NULL) {
580 /* alloc agent db hander hash table */
581 ret = __alloc_agent_dc_handlerTable();
583 goto allocAgentDBHandlerMgrInfoErr;
585 /* alloc agent db hander hash table mutex */
586 ret = __alloc_agent_dc_handlerTable_mutex();
588 goto allocAgentDBHandlerMgrInfoErr;
594 allocAgentDBHandlerMgrInfoErr:
595 Free_DB_Handler_Mgr();
598 return DM_ERR_UNKNOWN;
601 static void ___destroy_agentDBHashTableValue(void *pHandler)
605 _DEBUG_VERBOSE("[%s] start \n", __func__);
606 if ((DB_HANDLER *) pHandler != 0) {
607 dm_close_agent((DB_HANDLER *) pHandler);
613 static void ___destory_agentDBHashTableKey(void *key)
617 _DEBUG_VERBOSE("[%s] start \n", __func__);
618 if ((unsigned int *)key != 0) {
619 free((unsigned int *)key);
626 static void __print_agentDBHashTableLog()
628 _DEBUG_VERBOSE("[%s] ---------HashKeyList --------\n", __func__);
629 int size = g_hash_table_size(pDBHandlerMgr->db_handlerTable);
630 _DEBUG_VERBOSE("[%s] pDBHandlerMgr->db_handlerTable Size is %d\n", __func__, size);
631 _DEBUG_VERBOSE("[%s] ----------------------------------------\n", __func__);
633 GList *keyList = g_hash_table_get_keys(pDBHandlerMgr->db_handlerTable);
634 for (iter = keyList; iter != 0; iter = g_list_next(iter))
635 _DEBUG_VERBOSE("[%s] Handler Key %d\n", __func__, *((unsigned int *)(iter->data)));
636 _DEBUG_VERBOSE("[%s] ----------------------------------------\n", __func__);