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 !!");
300 _DEBUG_INFO("status [%d]", status);
302 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
303 if (db_handler == NULL) {
304 _DEBUG_INFO("[%s] no handler for key\n", __func__);
306 return DM_ERR_UNKNOWN;
309 int ret = dm_isExist_engine_id(db_handler, status->engine_id);
312 return dm_add_engine_status(db_handler, status);
314 _DEBUG_INFO("status->engine_id [%d]", status->engine_id);
315 _DEBUG_INFO("status->download_click [%d]", status->download_click);
317 return dm_update_engine_status(db_handler, status);
321 DM_ERROR Update_Engine_Status_Column(int engine_id, ENGINE_STATUS_VALUE engine_status_value, void *value)
325 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
326 if (db_handler == NULL) {
327 _DEBUG_INFO("[%s] no handler for key\n", __func__);
329 return DM_ERR_UNKNOWN;
332 return dm_update_engine_status_column(db_handler, engine_id, engine_status_value, value);
335 DM_ERROR Get_Engine_Status(int engine_id, engine_status ** status)
339 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
340 if (db_handler == NULL) {
341 _DEBUG_INFO("[%s] no handler for key\n", __func__);
343 return DM_ERR_UNKNOWN;
346 return dm_get_engine_status(db_handler, engine_id, status);
349 DM_ERROR Get_AII_Engine_Status(engine_status ** status_list, int *count)
353 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
354 if (db_handler == NULL) {
355 _DEBUG_INFO("[%s] no handler for key\n", __func__);
357 return DM_ERR_UNKNOWN;
361 return dm_get_aII_engine_status(db_handler, status_list, count);
364 int IsExist_Engine_id(int engine_id)
368 DB_HANDLER *db_handler = _get_agent_handler(GET_THREAD_ID);
369 if (db_handler == NULL) {
370 _DEBUG_INFO("[%s] no handler for key\n", __func__);
372 return DM_ERR_UNKNOWN;
377 return dm_isExist_engine_id(db_handler, engine_id);
381 * ==================================
382 * DB module internal API
383 * ==================================
385 DM_ERROR _add_agent_handler(unsigned int key, DB_HANDLER * pHandler)
389 if (pDBHandlerMgr == NULL || pDBHandlerMgr->db_handlerTable_mutex == NULL) {
390 return DM_ERR_UNKNOWN;
393 DM_ERROR ret = DM_OK;
395 unsigned int *pKey_copy = (unsigned int *)calloc(1, sizeof(unsigned int));
396 if (pKey_copy == NULL) {
398 return COMMON_ERR_ALLOC;
403 pthread_mutex_lock(pDBHandlerMgr->db_handlerTable_mutex);
404 if (pDBHandlerMgr->db_handlerTable == NULL) {
405 ret = __alloc_agent_dc_handlerTable();
407 goto addagentHandler;
410 /* insert to handler */
411 _DEBUG_TRACE("[%s] Key = %d, Handler = %p !\n", __func__, key, pHandler);
412 g_hash_table_insert(pDBHandlerMgr->db_handlerTable, pKey_copy, pHandler);
416 __print_agentDBHashTableLog();
418 pthread_mutex_unlock(pDBHandlerMgr->db_handlerTable_mutex);
419 if(pKey_copy != NULL) {
426 pthread_mutex_unlock(pDBHandlerMgr->db_handlerTable_mutex);
427 if(pKey_copy != NULL) {
434 DM_ERROR _remove_agent_handler(unsigned int key)
438 if (pDBHandlerMgr == NULL || pDBHandlerMgr->db_handlerTable_mutex == NULL || pDBHandlerMgr->db_handlerTable == NULL) {
440 return DM_ERR_UNKNOWN;
443 /* remove the handler to hash table */
444 pthread_mutex_lock(pDBHandlerMgr->db_handlerTable_mutex);
445 int isSuccess = g_hash_table_remove(pDBHandlerMgr->db_handlerTable, &key);
446 pthread_mutex_unlock(pDBHandlerMgr->db_handlerTable_mutex);
450 __print_agentDBHashTableLog();
453 _DEBUG_TRACE("[%s] end !\n", __func__);
454 if (isSuccess == 1) {
461 return DM_ERR_UNKNOWN;
464 DB_HANDLER *_get_agent_handler(unsigned int key)
468 if (pDBHandlerMgr == NULL || pDBHandlerMgr->db_handlerTable_mutex == NULL || pDBHandlerMgr->db_handlerTable == NULL) {
469 _DEBUG_TRACE("[%s] DM_ERR_UNKNOWN !\n", __func__);
475 /* get the handler to hash table */
476 pthread_mutex_lock(pDBHandlerMgr->db_handlerTable_mutex);
477 DB_HANDLER *pHandler = g_hash_table_lookup(pDBHandlerMgr->db_handlerTable, &key);
478 pthread_mutex_unlock(pDBHandlerMgr->db_handlerTable_mutex);
481 if (pHandler != NULL) {
482 _DEBUG_TRACE("[%s] ######### > Value Search Success(key = %d, handler = %p)\n", __func__, key, pHandler);
484 _DEBUG_TRACE("[%s] ######### > Value Search Fail(key = %d, handler = %p)\n", __func__, key, pHandler);
492 /* static function implementation */
493 static DM_ERROR __alloc_agent_dc_handlerTable()
497 if (pDBHandlerMgr != NULL) {
498 pDBHandlerMgr->db_handlerTable = g_hash_table_new_full(g_int_hash, g_int_equal, ___destory_agentDBHashTableKey, ___destroy_agentDBHashTableValue);
499 if (pDBHandlerMgr->db_handlerTable == NULL) {
500 _DEBUG_TRACE("[%s] Not Create agent dc handler hash table \n", __func__);
502 return DM_ERR_UNKNOWN;
504 _DEBUG_TRACE("[%s] create agent dc handler hash table \n", __func__);
513 static DM_ERROR __alloc_agent_dc_handlerTable_mutex()
517 if (pDBHandlerMgr != NULL) {
519 pDBHandlerMgr->db_handlerTable_mutex = (pthread_mutex_t *) calloc(1, sizeof(pthread_mutex_t));
520 if (pDBHandlerMgr->db_handlerTable_mutex == NULL) {
521 _DEBUG_TRACE("[%s] Not Create agent dc handler hash table mutex \n", __func__);
523 return COMMON_ERR_ALLOC;
526 if (pthread_mutex_init(pDBHandlerMgr->db_handlerTable_mutex, 0) != 0) {
527 _DEBUG_TRACE("[%s] Not init agent dc handler hash table mutex\n", __func__);
529 return DM_ERR_UNKNOWN;
531 _DEBUG_TRACE("[%s] Init agent dc handler hash table mutex\n", __func__);
540 static DM_ERROR _free_agent_dc_handlerTable()
544 if (pDBHandlerMgr != NULL && pDBHandlerMgr->db_handlerTable != NULL) {
545 g_hash_table_destroy(pDBHandlerMgr->db_handlerTable);
546 _DEBUG_TRACE("[%s] db_handlerTable free success !\n", __func__);
547 pDBHandlerMgr->db_handlerTable = 0;;
554 static DM_ERROR _free_agent_dc_handlerTable_mutex()
558 if (pDBHandlerMgr != NULL && pDBHandlerMgr->db_handlerTable_mutex != NULL) {
560 int ret = pthread_mutex_destroy(pDBHandlerMgr->db_handlerTable_mutex);
562 _DEBUG_TRACE("[%s] db_handlerTable_mutex free error !\n", __func__);
563 return DM_ERR_UNKNOWN;
565 free(pDBHandlerMgr->db_handlerTable_mutex);
566 pDBHandlerMgr->db_handlerTable_mutex = 0;
567 _DEBUG_TRACE("[%s] db_handlerTable_mutex free success !\n", __func__);
575 static DM_ERROR _create_agentDBHandlerMgrInfo()
579 DM_ERROR ret = DM_OK;
581 if (pDBHandlerMgr != NULL) {
583 /* alloc agent db hander hash table */
584 ret = __alloc_agent_dc_handlerTable();
586 goto allocAgentDBHandlerMgrInfoErr;
588 /* alloc agent db hander hash table mutex */
589 ret = __alloc_agent_dc_handlerTable_mutex();
591 goto allocAgentDBHandlerMgrInfoErr;
597 allocAgentDBHandlerMgrInfoErr:
598 Free_DB_Handler_Mgr();
601 return DM_ERR_UNKNOWN;
604 static void ___destroy_agentDBHashTableValue(void *pHandler)
608 _DEBUG_VERBOSE("[%s] start \n", __func__);
609 if ((DB_HANDLER *) pHandler != 0) {
610 dm_close_agent((DB_HANDLER *) pHandler);
616 static void ___destory_agentDBHashTableKey(void *key)
620 _DEBUG_VERBOSE("[%s] start \n", __func__);
621 if ((unsigned int *)key != 0) {
622 free((unsigned int *)key);
629 static void __print_agentDBHashTableLog()
631 _DEBUG_VERBOSE("[%s] ---------HashKeyList --------\n", __func__);
632 int size = g_hash_table_size(pDBHandlerMgr->db_handlerTable);
633 _DEBUG_VERBOSE("[%s] pDBHandlerMgr->db_handlerTable Size is %d\n", __func__, size);
634 _DEBUG_VERBOSE("[%s] ----------------------------------------\n", __func__);
636 GList *keyList = g_hash_table_get_keys(pDBHandlerMgr->db_handlerTable);
637 for (iter = keyList; iter != 0; iter = g_list_next(iter))
638 _DEBUG_VERBOSE("[%s] Handler Key %d\n", __func__, *((unsigned int *)(iter->data)));
639 _DEBUG_VERBOSE("[%s] ----------------------------------------\n", __func__);