Create an handle when handle is not avaliable
[platform/core/uifw/voice-control.git] / common / vc_cmd_db.c
1 /*
2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 <app_manager.h>
18 #include <db-util.h>
19 #include <dlog.h>
20 #include <sqlite3.h>
21 #include <tzplatform_config.h>
22
23 #include "vc_cmd_db.h"
24 #include "vc_main.h"
25 #include "vc_command.h"
26 #include "voice_control_command_expand.h"
27
28
29 const char* vc_db_tag()
30 {
31         return TAG_VCDB;
32 }
33
34 /*!
35  * \note
36  * DB Table schema
37  *
38  * vc_info
39  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
40  * | id  | pid  | type | format | domain | command | parameter | appid | invocation_name | fixed |
41  * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
42  *
43  * CREATE TABLE vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, appid TEXT);
44  *
45  * vc_result
46  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
47  * | id  | result | event | msg | exclusive | pid | type | format | domain | command | parameter | appid | invocation_name | fixed |
48  * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
49  *
50  * CREATE TABLE vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,
51  *                                      pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT,\
52  *
53  */
54
55 #define FREE_MEM(X) {if (NULL != X) {free(X); X = NULL; } }
56
57
58 //#define DB_PATH tzplatform_mkpath(TZ_USER_DB, ".vc_info.db")
59 static sqlite3* g_db_handle = NULL;
60 static sqlite3* g_db_backup_handle = NULL;
61 static char* g_path = NULL;
62 static char* g_backup_path = NULL;
63 static int g_ref_cnt = 0;
64 static bool is_db_corrupted = false;
65 int g_fpid = -1;
66 int g_db_cnt = 0;
67 int g_backup_db_cnt = 0;
68
69 static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist);
70 static int __vc_db_begin_transaction(sqlite3* db_handle);
71 static int __vc_db_create_table(sqlite3* db_handle, const char* table);
72 static int __vc_db_rollback_transaction(sqlite3* db_handle);
73 static int __vc_db_commit_transaction(sqlite3* db_handle);
74
75
76 int __vc_db_reset_handle(void)
77 {
78         if (!g_db_handle || !g_db_backup_handle) {
79                 vc_db_finalize();
80                 vc_db_initialize();
81         }
82         return 0;
83 }
84
85 static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
86 {
87         sqlite3_stmt* pStmt = NULL;
88         int ret = -1;
89
90         /* prepare db */
91         ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
92         if (ret != SQLITE_OK) {
93                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
94                 return VC_DB_ERROR_OPERATION_FAILED;
95         }
96
97         if (sqlite3_step(pStmt) != SQLITE_DONE) {
98                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
99                 sqlite3_finalize(pStmt);
100                 return VC_DB_ERROR_OPERATION_FAILED;
101         }
102
103         sqlite3_finalize(pStmt);
104         return VC_DB_ERROR_NONE;
105 }
106
107 static int __vc_db_begin_transaction(sqlite3* db_handle)
108 {
109         int ret = __vc_db_transaction(db_handle, "BEGIN IMMEDIATE TRANSACTION");
110         return ret;
111 }
112
113 static int __vc_db_rollback_transaction(sqlite3* db_handle)
114 {
115         int ret = __vc_db_transaction(db_handle, "ROLLBACK TRANSACTION");
116         return ret;
117 }
118
119 static int __vc_db_commit_transaction(sqlite3* db_handle)
120 {
121         int ret = __vc_db_transaction(db_handle, "COMMIT TRANSACTION");
122         return ret;
123 }
124
125 static int __vc_db_exec_query(sqlite3* db_handle, const char* sql)
126 {
127         char* err_msg = NULL;
128
129         int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
130         if (ret != SQLITE_OK) {
131                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
132                 sqlite3_free(err_msg);
133                 return VC_DB_ERROR_OPERATION_FAILED;
134         }
135         return VC_DB_ERROR_NONE;
136 }
137
138 static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist)
139 {
140         char* sql = NULL;
141         *is_exist = false;
142         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
143                 sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_result';");
144         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
145                 sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_info';");
146         } else {
147                 return VC_DB_ERROR_INVALID_PARAMETER;
148         }
149
150         if (NULL == sql) {
151                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
152                 return VC_DB_ERROR_OUT_OF_MEMORY;
153         }
154
155         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
156
157         char* err_msg = NULL;
158         char** results = NULL;
159         int ret = sqlite3_get_table(db_handle, sql, &results, NULL, NULL, &err_msg);
160         if (ret != SQLITE_OK) {
161                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_get_table return fail, ret(%d), err(%s)", ret, err_msg);
162                 sqlite3_free(err_msg);
163                 return VC_DB_ERROR_OPERATION_FAILED;
164         }
165
166         if (0 != atoi(results[1])) {
167                 *is_exist = true;
168                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] table exists (%d)", atoi(results[1]));
169         } else {
170                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] table doesn't exist (%d)", atoi(results[1]));
171         }
172
173         sqlite3_free_table(results);
174         free(sql);
175         sql = NULL;
176         return VC_DB_ERROR_NONE;
177 }
178
179 static int __vc_db_delete_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, const char* appid)
180 {
181         sqlite3_stmt* stmt = NULL;
182         char* sql = NULL;
183
184         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid)
185                 sql = strdup("DELETE FROM vc_info WHERE type = ?;");
186         else if (NULL != appid)
187                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND appid = ?;");
188         else
189                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
190
191         if (NULL == sql) {
192                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
193                 return VC_DB_ERROR_OUT_OF_MEMORY;
194         }
195
196         int ret = 0;
197         /* check whether there is a table or not */
198         bool is_exist = false;
199         ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
200         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
201                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
202                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
203                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
204                         return VC_DB_ERROR_OPERATION_FAILED;
205                 }
206         } else {
207                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
208         }
209
210         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
211         if (ret != SQLITE_OK) {
212                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
213                 free(sql);
214                 sql = NULL;
215                 return VC_DB_ERROR_OPERATION_FAILED;
216         }
217         ret = sqlite3_bind_int(stmt, 1, (int)type);
218         if (ret != SQLITE_OK) {
219                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
220                 sqlite3_finalize(stmt);
221                 free(sql);
222                 sql = NULL;
223                 return VC_DB_ERROR_OPERATION_FAILED;
224         }
225
226         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid) {
227                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d;", type);
228         } else if (NULL != appid) {
229                 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
230                 if (ret != SQLITE_OK) {
231                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
232                         sqlite3_clear_bindings(stmt);
233                         sqlite3_finalize(stmt);
234                         free(sql);
235                         sql = NULL;
236                         return VC_DB_ERROR_OPERATION_FAILED;
237                 }
238                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
239         } else  {
240                 ret = sqlite3_bind_int(stmt, 2, pid);
241                 if (ret != SQLITE_OK) {
242                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
243                         sqlite3_clear_bindings(stmt);
244                         sqlite3_finalize(stmt);
245                         free(sql);
246                         sql = NULL;
247                         return VC_DB_ERROR_OPERATION_FAILED;
248                 }
249                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
250         }
251
252         ret = sqlite3_step(stmt);
253         if (ret != SQLITE_DONE) {
254                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned, sql(%s), ret(%d), err(%s)", sql, ret, sqlite3_errmsg(db_handle));
255                 sqlite3_clear_bindings(stmt);
256                 sqlite3_finalize(stmt);
257                 free(sql);
258                 sql = NULL;
259                 return VC_DB_ERROR_OPERATION_FAILED;
260         }
261
262         sqlite3_reset(stmt);
263         sqlite3_clear_bindings(stmt);
264         sqlite3_finalize(stmt);
265
266         free(sql);
267         sql = NULL;
268         return VC_DB_ERROR_NONE;
269 }
270
271 static int __vc_db_insert_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
272 {
273         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
274
275         sqlite3_stmt* stmt = NULL;
276         const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
277         int ret = 0;
278         /* check whether there is a table or not */
279         bool is_exist = false;
280         ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
281         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
282                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
283                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
284                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
285                         return VC_DB_ERROR_OPERATION_FAILED;
286                 }
287         } else {
288                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
289         }
290
291         /* prepare db */
292         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
293         if (ret != SQLITE_OK) {
294                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
295                 return VC_DB_ERROR_OPERATION_FAILED;
296         }
297         ret = sqlite3_bind_int(stmt, 2, pid);
298         if (ret != SQLITE_OK) {
299                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
300                 sqlite3_finalize(stmt);
301                 return VC_DB_ERROR_OPERATION_FAILED;
302         }
303         ret = sqlite3_bind_int(stmt, 3, cmd->type);
304         if (ret != SQLITE_OK) {
305                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
306                 sqlite3_clear_bindings(stmt);
307                 sqlite3_finalize(stmt);
308                 return VC_DB_ERROR_OPERATION_FAILED;
309         }
310         ret = sqlite3_bind_int(stmt, 4, cmd->format);
311         if (ret != SQLITE_OK) {
312                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
313                 sqlite3_clear_bindings(stmt);
314                 sqlite3_finalize(stmt);
315                 return VC_DB_ERROR_OPERATION_FAILED;
316         }
317         ret = sqlite3_bind_int(stmt, 5, cmd->domain);
318         if (ret != SQLITE_OK) {
319                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
320                 sqlite3_clear_bindings(stmt);
321                 sqlite3_finalize(stmt);
322                 return VC_DB_ERROR_OPERATION_FAILED;
323         }
324         ret = sqlite3_bind_text(stmt, 6, cmd->command, -1, SQLITE_TRANSIENT);
325         if (ret != SQLITE_OK) {
326                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
327                 sqlite3_clear_bindings(stmt);
328                 sqlite3_finalize(stmt);
329                 return VC_DB_ERROR_OPERATION_FAILED;
330         }
331
332         char* appid = NULL;
333         if (NULL == cmd->appid) {
334                 // Get appid by pid using app control
335                 ret = app_manager_get_app_id(pid, &appid);
336                 if (APP_MANAGER_ERROR_NONE != ret) {
337                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
338                 }
339                 if (NULL != appid) {
340                         cmd->appid = strdup(appid);
341                         FREE_MEM(appid);
342                 }
343         }
344         ret = sqlite3_bind_text(stmt, 8, cmd->appid, -1, SQLITE_TRANSIENT);
345         if (ret != SQLITE_OK) {
346                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
347                 sqlite3_clear_bindings(stmt);
348                 sqlite3_finalize(stmt);
349                 return VC_DB_ERROR_OPERATION_FAILED;
350         }
351         if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
352                 ret = sqlite3_bind_text(stmt, 9, cmd->invocation_name, -1, SQLITE_TRANSIENT);
353                 if (ret != SQLITE_OK) {
354                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
355                         sqlite3_clear_bindings(stmt);
356                         sqlite3_finalize(stmt);
357                         return VC_DB_ERROR_OPERATION_FAILED;
358                 }
359         }
360         ret = sqlite3_bind_text(stmt, 10, cmd->fixed, -1, SQLITE_TRANSIENT);
361         if (ret != SQLITE_OK) {
362                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
363                 sqlite3_clear_bindings(stmt);
364                 sqlite3_finalize(stmt);
365                 return VC_DB_ERROR_OPERATION_FAILED;
366         }
367         ret = sqlite3_step(stmt);
368         if (ret != SQLITE_DONE) {
369                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
370                 sqlite3_clear_bindings(stmt);
371                 sqlite3_finalize(stmt);
372                 return VC_DB_ERROR_OPERATION_FAILED;
373         }
374
375         SLOG(LOG_INFO, vc_db_tag(), "[SQL] INSERT INTO vc_info (id, pid(%d), type(%d), format(%d), domain(%d), command(%s)", pid, cmd->type, cmd->format, cmd->domain, cmd->command);
376         SLOG(LOG_INFO, vc_db_tag(), "[SQL] @@ appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
377
378         sqlite3_reset(stmt);
379         sqlite3_clear_bindings(stmt);
380         sqlite3_finalize(stmt);
381         return VC_DB_ERROR_NONE;
382 }
383
384 static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList** cmd_list)
385 {
386         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
387
388         int ret = 0;
389         sqlite3_stmt* stmt = NULL;
390         char* sql = NULL;
391         char* appid = NULL;
392
393         if (VC_COMMAND_TYPE_BACKGROUND == type) {
394                 /* For background command */
395                 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
396         } else {
397                 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
398         }
399
400         if (NULL == sql) {
401                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
402                 return VC_DB_ERROR_OUT_OF_MEMORY;
403         }
404
405         /* check whether there is a table or not */
406         bool is_exist = false;
407         ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
408         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
409                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
410                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
411                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
412                         return VC_DB_ERROR_OPERATION_FAILED;
413                 }
414         } else {
415                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
416         }
417
418         /* prepare db */
419         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
420         if (ret != SQLITE_OK) {
421                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
422                 free(sql);
423                 sql = NULL;
424                 return VC_DB_ERROR_OPERATION_FAILED;
425         }
426
427         ret = sqlite3_bind_int(stmt, 1, (int)type);
428         if (ret != SQLITE_OK) {
429                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
430                 sqlite3_finalize(stmt);
431                 free(sql);
432                 sql = NULL;
433                 return VC_DB_ERROR_OPERATION_FAILED;
434         }
435         if (VC_COMMAND_TYPE_BACKGROUND != type) {
436                 ret = sqlite3_bind_int(stmt, 2, pid);
437                 if (ret != SQLITE_OK) {
438                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
439                         sqlite3_clear_bindings(stmt);
440                         sqlite3_finalize(stmt);
441                         free(sql);
442                         sql = NULL;
443                         return VC_DB_ERROR_OPERATION_FAILED;
444                 }
445         }
446
447         if (VC_COMMAND_TYPE_BACKGROUND == type)
448                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
449         else
450                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
451
452         ret = sqlite3_step(stmt);
453         if (ret == SQLITE_DONE) {
454                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
455                 sqlite3_clear_bindings(stmt);
456                 sqlite3_finalize(stmt);
457                 free(sql);
458                 sql = NULL;
459                 return VC_DB_ERROR_NONE;
460         }
461
462         if (VC_COMMAND_TYPE_BACKGROUND == type && -1 != pid) {
463                 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
464                         SLOG(LOG_WARN, vc_db_tag(), "[WARN] fail to get app id, pid(%d)", pid);
465                 }
466         }
467
468         while (SQLITE_ROW == ret) {
469                 int temp = 0;
470                 int ret = -1;
471                 char* temp_text = NULL;
472                 vc_cmd_h temp_cmd;
473                 ret = vc_cmd_create(&temp_cmd);
474
475                 if (0 != ret) {
476                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
477                         if (NULL != appid) {
478                                 free(appid);
479                                 appid = NULL;
480                         }
481                         sqlite3_reset(stmt);
482                         sqlite3_clear_bindings(stmt);
483                         sqlite3_finalize(stmt);
484                         free(sql);
485                         sql = NULL;
486                         return VC_DB_ERROR_OUT_OF_MEMORY;
487                 }
488
489                 temp_text = (char*)sqlite3_column_text(stmt, 7);
490                 if (NULL != temp_text)
491                         vc_cmd_set_appid(temp_cmd, temp_text);
492
493                 ret = vc_cmd_get_appid(temp_cmd, &temp_text);
494                 if (NULL != appid && 0 == ret) {
495                         if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_text, strlen(appid))) {
496                                 SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
497
498                                 free(temp_text);
499                                 temp_text = NULL;
500
501                                 vc_cmd_destroy(temp_cmd);
502                                 temp_cmd = NULL;
503
504                                 ret = sqlite3_step(stmt);
505                                 if (SQLITE_DONE == ret)
506                                         break;
507
508                                 continue;
509                         }
510                 }
511
512                 if (NULL != temp_text) {
513                         free(temp_text);
514                         temp_text = NULL;
515                 }
516
517                 temp = sqlite3_column_int(stmt, 0);
518                 vc_cmd_set_id(temp_cmd, temp);
519
520                 temp = sqlite3_column_int(stmt, 1);
521                 vc_cmd_set_pid(temp_cmd, temp);
522
523                 temp = sqlite3_column_int(stmt, 2);
524                 vc_cmd_set_type(temp_cmd, temp);
525
526                 temp = sqlite3_column_int(stmt, 3);
527                 vc_cmd_set_format(temp_cmd, temp);
528
529                 temp  = sqlite3_column_int(stmt, 4);
530                 vc_cmd_set_domain(temp_cmd, temp);
531
532                 temp_text = (char*)sqlite3_column_text(stmt, 5);
533                 if (NULL != temp_text)
534                         vc_cmd_set_command(temp_cmd, temp_text);
535
536                 temp_text = (char*)sqlite3_column_text(stmt, 6);
537                 if (NULL != temp_text)
538                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
539
540                 temp_text = (char*)sqlite3_column_text(stmt, 8);
541                 if (NULL != temp_text)
542                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
543
544                 temp_text = (char*)sqlite3_column_text(stmt, 9);
545                 if (NULL != temp_text)
546                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
547
548                 ret = vc_cmd_get_type(temp_cmd, &temp);
549                 if (0 == ret && type == temp) {
550                         *cmd_list = g_slist_append(*cmd_list, temp_cmd);
551                 } else {
552                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp, type);
553
554                         vc_cmd_destroy(temp_cmd);
555                         temp_cmd = NULL;
556                 }
557                 ret = sqlite3_step(stmt);
558                 if (SQLITE_DONE == ret)
559                         break;
560         }
561
562         if (NULL != appid) {
563                 free(appid);
564                 appid = NULL;
565         }
566
567         sqlite3_reset(stmt);
568         sqlite3_clear_bindings(stmt);
569         sqlite3_finalize(stmt);
570
571         free(sql);
572         sql = NULL;
573         return VC_DB_ERROR_NONE;
574 }
575
576 static int __vc_db_get_pid(const char* appid, int* pid)
577 {
578         bool running = false;
579         int ret = app_manager_is_running(appid, &running);
580         if (APP_MANAGER_ERROR_NONE != ret) {
581                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to check running with appid(%s)", appid);
582                 return VC_DB_ERROR_OPERATION_FAILED;
583         }
584         if (true == running) {
585                 app_context_h app_context = NULL;
586                 ret = app_manager_get_app_context(appid, &app_context);
587                 if (APP_MANAGER_ERROR_NONE != ret) {
588                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
589                         return VC_DB_ERROR_OPERATION_FAILED;
590                 }
591
592                 ret = app_context_get_pid(app_context, pid);
593                 if (APP_MANAGER_ERROR_NONE != ret) {
594                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get pid, ret(%d), appid(%s)", ret, appid);
595                         return VC_DB_ERROR_OPERATION_FAILED;
596                 }
597
598                 ret = app_context_destroy(app_context);
599                 if (APP_MANAGER_ERROR_NONE != ret) {
600                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to destroy app context, ret(%d), appid(%s)", ret, appid);
601                         return VC_DB_ERROR_OPERATION_FAILED;
602                 }
603         } else {
604                 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
605         }
606         return VC_DB_ERROR_NONE;
607 }
608
609 static int __vc_db_insert_result(sqlite3* db_handle, const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
610 {
611         SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
612
613         sqlite3_stmt* stmt = NULL;
614         const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
615         int ret = 0;
616
617         /* check whether there is a table or not */
618         bool is_exist = false;
619         ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
620         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
621                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
622                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
623                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
624                         return VC_DB_ERROR_OPERATION_FAILED;
625                 }
626         } else {
627                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
628         }
629
630         /* prepare db */
631         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
632         if (ret != SQLITE_OK) {
633                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
634                 return VC_DB_ERROR_OPERATION_FAILED;
635         }
636         ret = sqlite3_bind_text(stmt, 2, result_text, -1, SQLITE_TRANSIENT);
637         if (ret != SQLITE_OK) {
638                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
639                 sqlite3_finalize(stmt);
640                 return VC_DB_ERROR_OPERATION_FAILED;
641         }
642         ret = sqlite3_bind_int(stmt, 3, event);
643         if (ret != SQLITE_OK) {
644                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
645                 sqlite3_clear_bindings(stmt);
646                 sqlite3_finalize(stmt);
647                 return VC_DB_ERROR_OPERATION_FAILED;
648         }
649         ret = sqlite3_bind_text(stmt, 4, msg, -1, SQLITE_TRANSIENT);
650         if (ret != SQLITE_OK) {
651                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
652                 sqlite3_clear_bindings(stmt);
653                 sqlite3_finalize(stmt);
654                 return VC_DB_ERROR_OPERATION_FAILED;
655         }
656         ret = sqlite3_bind_int(stmt, 5, exclusive);
657         if (ret != SQLITE_OK) {
658                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
659                 sqlite3_clear_bindings(stmt);
660                 sqlite3_finalize(stmt);
661                 return VC_DB_ERROR_OPERATION_FAILED;
662         }
663
664         if (NULL == cmd) {
665                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d))", result_text, event, msg, exclusive);
666
667                 ret = sqlite3_step(stmt);
668                 if (ret != SQLITE_DONE) {
669                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
670                         sqlite3_clear_bindings(stmt);
671                         sqlite3_finalize(stmt);
672                         return VC_DB_ERROR_OPERATION_FAILED;
673                 }
674                 sqlite3_reset(stmt);
675                 sqlite3_clear_bindings(stmt);
676                 sqlite3_finalize(stmt);
677                 return VC_DB_ERROR_NONE;
678         }
679
680         if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
681                 int pid = -1;
682                 ret = __vc_db_get_pid(cmd->appid, &pid);
683                 if (0 != ret) {
684                         SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
685                 } else {
686                         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
687                         if (-1 != pid)
688                                 cmd->pid = pid;
689                 }
690         }
691         ret = sqlite3_bind_int(stmt, 6, cmd->pid);
692         if (ret != SQLITE_OK) {
693                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
694                 sqlite3_clear_bindings(stmt);
695                 sqlite3_finalize(stmt);
696                 return VC_DB_ERROR_OPERATION_FAILED;
697         }
698         ret = sqlite3_bind_int(stmt, 7, cmd->type);
699         if (ret != SQLITE_OK) {
700                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
701                 sqlite3_clear_bindings(stmt);
702                 sqlite3_finalize(stmt);
703                 return VC_DB_ERROR_OPERATION_FAILED;
704         }
705         ret = sqlite3_bind_int(stmt, 8, cmd->format);
706         if (ret != SQLITE_OK) {
707                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
708                 sqlite3_clear_bindings(stmt);
709                 sqlite3_finalize(stmt);
710                 return VC_DB_ERROR_OPERATION_FAILED;
711         }
712         ret = sqlite3_bind_int(stmt, 9, cmd->domain);
713         if (ret != SQLITE_OK) {
714                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
715                 sqlite3_clear_bindings(stmt);
716                 sqlite3_finalize(stmt);
717                 return VC_DB_ERROR_OPERATION_FAILED;
718         }
719         ret = sqlite3_bind_text(stmt, 10, cmd->command, -1, SQLITE_TRANSIENT);
720         if (ret != SQLITE_OK) {
721                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
722                 sqlite3_clear_bindings(stmt);
723                 sqlite3_finalize(stmt);
724                 return VC_DB_ERROR_OPERATION_FAILED;
725         }
726         ret = sqlite3_bind_text(stmt, 11, cmd->parameter, -1, SQLITE_TRANSIENT);
727         if (ret != SQLITE_OK) {
728                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
729                 sqlite3_clear_bindings(stmt);
730                 sqlite3_finalize(stmt);
731                 return VC_DB_ERROR_OPERATION_FAILED;
732         }
733         ret = sqlite3_bind_text(stmt, 12, cmd->appid, -1, SQLITE_TRANSIENT);
734         if (ret != SQLITE_OK) {
735                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
736                 sqlite3_clear_bindings(stmt);
737                 sqlite3_finalize(stmt);
738                 return VC_DB_ERROR_OPERATION_FAILED;
739         }
740         ret = sqlite3_bind_text(stmt, 13, cmd->invocation_name, -1, SQLITE_TRANSIENT);
741         if (ret != SQLITE_OK) {
742                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
743                 sqlite3_clear_bindings(stmt);
744                 sqlite3_finalize(stmt);
745                 return VC_DB_ERROR_OPERATION_FAILED;
746         }
747         ret = sqlite3_bind_text(stmt, 14, cmd->fixed, -1, SQLITE_TRANSIENT);
748         if (ret != SQLITE_OK) {
749                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
750                 sqlite3_clear_bindings(stmt);
751                 sqlite3_finalize(stmt);
752                 return VC_DB_ERROR_OPERATION_FAILED;
753         }
754         ret = sqlite3_step(stmt);
755         if (ret != SQLITE_DONE) {
756                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
757                 sqlite3_clear_bindings(stmt);
758                 sqlite3_finalize(stmt);
759                 return VC_DB_ERROR_OPERATION_FAILED;
760         }
761
762         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d), pid(%d), type(%d), format(%d), domain(%d), command(%s), parameter(%s), appid(%s), invocation(%s), fixed(%s)",
763                                 result_text, event, msg, exclusive, cmd->pid, cmd->type, cmd->format, cmd->domain, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
764
765         sqlite3_reset(stmt);
766         sqlite3_clear_bindings(stmt);
767         sqlite3_finalize(stmt);
768
769         return VC_DB_ERROR_NONE;
770 }
771
772 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
773 {
774         if (NULL == org_cmd || NULL == new_cmd) {
775                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
776                 return VC_DB_ERROR_INVALID_PARAMETER;
777         }
778
779         if (NULL == invocation_name) {
780                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, invocation name is NULL, org_cmd(%s)", org_cmd);
781                 return VC_DB_ERROR_INVALID_PARAMETER;
782         }
783
784         if (strlen(org_cmd) <= strlen(invocation_name)) {
785                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
786                 return VC_DB_ERROR_INVALID_PARAMETER;
787         }
788
789         if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
790                 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
791         }
792         SLOG(LOG_DEBUG, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, *new_cmd, invocation_name);
793         return VC_DB_ERROR_NONE;
794 }
795
796 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
797 {
798         if (NULL == command || NULL == temp_unfixed) {
799                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
800                 return VC_DB_ERROR_INVALID_PARAMETER;
801         }
802
803         if (NULL == fixed) {
804                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, fixed cmd is NULL, org_cmd(%s)", command);
805                 return VC_DB_ERROR_INVALID_PARAMETER;
806         }
807
808         if (strlen(command) <= strlen(fixed)) {
809                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to extract unfixed command, cmd(%s) fixed(%s)", command, fixed);
810                 return VC_DB_ERROR_INVALID_PARAMETER;
811         }
812
813         char* temp = (char*)calloc(256, sizeof(char));
814         if (NULL == temp) {
815                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
816                 return VC_DB_ERROR_OUT_OF_MEMORY;
817         }
818         if (0 == strncasecmp(command, fixed, strlen(fixed))) {
819                 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
820                 SLOG(LOG_WARN, vc_db_tag(), "@@@");
821         } else  if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
822                 strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
823                 SLOG(LOG_WARN, vc_db_tag(), "@@@");
824         }
825
826         SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
827         if (NULL != temp) {
828                 *temp_unfixed = strdup(temp);
829                 free(temp);
830                 temp = NULL;
831         }
832         return VC_DB_ERROR_NONE;
833 }
834
835 static int __vc_db_get_result(sqlite3* db_handle, char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
836 {
837         int ret = 0;
838         sqlite3_stmt* stmt = NULL;
839         char* sql = NULL;
840         if (-1 == pid)
841                 sql = strdup("SELECT * FROM vc_result;");
842         else if (NULL != appid)
843                 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
844         else
845                 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
846
847         if (NULL == sql) {
848                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
849                 return VC_DB_ERROR_OUT_OF_MEMORY;
850         }
851
852         /* check whether there is a table or not */
853         bool is_exist = false;
854         ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
855         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
856                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
857                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
858                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
859                         return VC_DB_ERROR_OPERATION_FAILED;
860                 }
861         } else {
862                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
863         }
864
865         /* prepare db */
866         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
867         if (ret != SQLITE_OK) {
868                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
869                 free(sql);
870                 sql = NULL;
871                 return VC_DB_ERROR_OPERATION_FAILED;
872         }
873         if (NULL != appid) {
874                 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
875                 if (ret != SQLITE_OK) {
876                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
877                         sqlite3_finalize(stmt);
878                         free(sql);
879                         sql = NULL;
880                         return VC_DB_ERROR_OPERATION_FAILED;
881                 }
882         } else if (-1 != pid) {
883                 ret = sqlite3_bind_int(stmt, 1, pid);
884                 if (ret != SQLITE_OK) {
885                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
886                         sqlite3_finalize(stmt);
887                         free(sql);
888                         sql = NULL;
889                         return VC_DB_ERROR_OPERATION_FAILED;
890                 }
891         }
892         ret = sqlite3_step(stmt);
893         if (ret == SQLITE_DONE) {
894                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
895                 sqlite3_clear_bindings(stmt);
896                 sqlite3_finalize(stmt);
897                 free(sql);
898                 sql = NULL;
899                 return VC_DB_ERROR_NONE;
900         }
901
902         if (-1 == pid)
903                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
904         else if (NULL != appid)
905                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %s;", appid);
906
907         else
908                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
909
910         vc_cmd_h temp_cmd = NULL;
911         while (SQLITE_ROW == ret) {
912                 int temp = 0;
913                 char* temp_text = NULL;
914                 const char* invocation_name = NULL;
915
916                 temp_text = (char*)sqlite3_column_text(stmt, 1);
917                 if (NULL != temp_text)
918                         *result_text = strdup(temp_text);
919
920                 temp = sqlite3_column_int(stmt, 2);
921                 *event = temp;
922
923                 if (NULL != msg) {
924                         temp_text = (char*)sqlite3_column_text(stmt, 3);
925                         if (NULL != temp_text)
926                                 *msg = strdup(temp_text);
927                 }
928
929                 if (0 != vc_cmd_create(&temp_cmd)) {
930                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
931                         if (NULL != *result_text) {
932                                 free(*result_text);
933                                 *result_text = NULL;
934                         }
935
936                         if (NULL != msg && NULL != *msg) {
937                                 free(*msg);
938                                 *msg = NULL;
939                         }
940
941                         sqlite3_reset(stmt);
942                         sqlite3_clear_bindings(stmt);
943                         sqlite3_finalize(stmt);
944                         free(sql);
945                         sql = NULL;
946                         return -1;
947                 }
948
949                 temp = sqlite3_column_int(stmt, 0);
950                 vc_cmd_set_id(temp_cmd, temp);
951
952                 temp = sqlite3_column_int(stmt, 5);
953                 vc_cmd_set_pid(temp_cmd, temp);
954
955                 temp = sqlite3_column_int(stmt, 6);
956                 vc_cmd_set_type(temp_cmd, temp);
957
958                 temp = sqlite3_column_int(stmt, 7);
959                 vc_cmd_set_format(temp_cmd, temp);
960
961                 temp = sqlite3_column_int(stmt, 8);
962                 vc_cmd_set_domain(temp_cmd, temp);
963
964                 // invocation name
965                 invocation_name = (char*)sqlite3_column_text(stmt, 12);
966                 // command name
967                 temp_text = (char*)sqlite3_column_text(stmt, 9);
968                 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
969
970                 if (NULL != temp_text) {
971                         char* temp_command = NULL;
972                         char* temp_unfixed = NULL;
973                         char* temp_fixed = NULL;
974
975                         if (NULL != invocation_name)
976                                 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
977
978                         if (NULL == temp_command) {
979                                 temp_command = strdup(temp_text);
980                         } else {
981                                 // remove invocation name from result_text
982                                 free(*result_text);
983                                 *result_text = strdup(temp_command);
984                         }
985
986                         // fixed
987                         temp_fixed = (char*)sqlite3_column_text(stmt, 13);
988                         if (NULL != temp_fixed)
989                                 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
990
991                         // unfixed
992                         temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
993
994                         if (NULL != temp_unfixed) {
995                                 char merge_result[256] = {0, };
996                                 vc_cmd_set_command(temp_cmd, temp_command);
997                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
998                                 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
999                                 if (NULL != *result_text)
1000                                         free(*result_text);
1001                                 *result_text = strdup(merge_result);
1002                         } else if (NULL != temp_fixed) {
1003                                 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
1004                                 vc_cmd_set_command(temp_cmd, temp_fixed);
1005                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
1006                                 if (NULL != temp_unfixed) {
1007                                         free(temp_unfixed);
1008                                 temp_unfixed = NULL;
1009                                 }
1010                         } else {
1011                                 vc_cmd_set_command(temp_cmd, temp_command);
1012                         }
1013                         free(temp_command);
1014                         temp_command = NULL;
1015                 }
1016
1017                 temp_text = (char*)sqlite3_column_text(stmt, 11);
1018                 if (NULL != temp_text)
1019                         vc_cmd_set_appid(temp_cmd, temp_text);
1020
1021                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1022                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1023                         vc_cmd_destroy(temp_cmd);
1024                         vc_cmd_list_destroy(vc_cmd_list, true);
1025                         if (NULL != *result_text) {
1026                                 free(*result_text);
1027                                 *result_text = NULL;
1028                         }
1029
1030                         if (NULL != msg && NULL != *msg) {
1031                                 free(*msg);
1032                                 *msg = NULL;
1033                         }
1034
1035                         sqlite3_reset(stmt);
1036                         sqlite3_clear_bindings(stmt);
1037                         sqlite3_finalize(stmt);
1038                         free(sql);
1039                         sql = NULL;
1040                         return VC_DB_ERROR_OPERATION_FAILED;
1041                 }
1042
1043                 ret = sqlite3_step(stmt);
1044                 if (SQLITE_DONE == ret)
1045                         break;
1046         }
1047
1048         sqlite3_reset(stmt);
1049         sqlite3_clear_bindings(stmt);
1050         sqlite3_finalize(stmt);
1051
1052         free(sql);
1053         sql = NULL;
1054         return VC_DB_ERROR_NONE;
1055 }
1056
1057 void __vc_db_demandable_client_free(void* data)
1058 {
1059         vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data;
1060
1061         if (NULL != d_app) {
1062                 if (NULL != d_app->appid) {
1063                         free(d_app->appid);
1064                         d_app->appid = NULL;
1065                 }
1066
1067                 free(d_app);
1068         }
1069 }
1070
1071 static int __vc_db_get_appid(sqlite3* db_handle, const char* result, GSList** app_list)
1072 {
1073         GSList* temp_app_list = NULL;
1074
1075         int ret = 0;
1076         sqlite3_stmt* stmt = NULL;
1077         const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
1078
1079         /* check whether there is a table or not */
1080         bool is_exist = false;
1081         ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
1082         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1083                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
1084                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
1085                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1086                         return VC_DB_ERROR_OPERATION_FAILED;
1087                 }
1088         } else {
1089                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
1090         }
1091
1092         /* prepare db */
1093         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1094         if (ret != SQLITE_OK) {
1095                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1096                 return VC_DB_ERROR_OPERATION_FAILED;
1097         }
1098         ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
1099         if (ret != SQLITE_OK) {
1100                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1101                 sqlite3_finalize(stmt);
1102                 return VC_DB_ERROR_OPERATION_FAILED;
1103         }
1104         ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
1105         if (ret != SQLITE_OK) {
1106                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1107                 sqlite3_clear_bindings(stmt);
1108                 sqlite3_finalize(stmt);
1109                 return VC_DB_ERROR_OPERATION_FAILED;
1110         }
1111         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
1112         ret = sqlite3_step(stmt);
1113         if (ret == SQLITE_DONE) {
1114                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1115                 sqlite3_clear_bindings(stmt);
1116                 sqlite3_finalize(stmt);
1117                 return VC_DB_ERROR_NONE;
1118         }
1119
1120         while (SQLITE_ROW == ret) {
1121                 char* temp_text = NULL;
1122                 vc_deactivated_app_s* temp_app = NULL;
1123                 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
1124                 if (NULL == temp_app) {
1125                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allocation fail");
1126
1127                         if (NULL != temp_app_list) {
1128                                 g_slist_free_full(temp_app_list, __vc_db_demandable_client_free);
1129                                 temp_app_list = NULL;
1130                         }
1131
1132                         sqlite3_reset(stmt);
1133                         sqlite3_clear_bindings(stmt);
1134                         sqlite3_finalize(stmt);
1135                         return VC_DB_ERROR_OUT_OF_MEMORY;
1136                 }
1137
1138                 temp_text = (char*)sqlite3_column_text(stmt, 11);
1139                 if (NULL != temp_text)
1140                         temp_app->appid = strdup(temp_text);
1141
1142                 temp_app_list = g_slist_append(temp_app_list, temp_app);
1143
1144                 ret = sqlite3_step(stmt);
1145                 if (SQLITE_DONE == ret)
1146                         break;
1147         }
1148
1149         *app_list = temp_app_list;
1150
1151         sqlite3_reset(stmt);
1152         sqlite3_clear_bindings(stmt);
1153         sqlite3_finalize(stmt);
1154         return VC_DB_ERROR_NONE;
1155 }
1156
1157 int __vc_db_get_result_pid_list(sqlite3* db_handle, const char* result, GSList** pid_list)
1158 {
1159         GSList* temp_pid_list = NULL;
1160
1161         int ret = 0;
1162         sqlite3_stmt* stmt = NULL;
1163         const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
1164
1165
1166         /* check whether there is a table or not */
1167         bool is_exist = false;
1168         ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
1169         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1170                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
1171                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
1172                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1173                         return VC_DB_ERROR_OPERATION_FAILED;
1174                 }
1175         } else {
1176                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
1177         }
1178
1179         /* prepare db */
1180         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1181         if (ret != SQLITE_OK) {
1182                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1183                 return VC_DB_ERROR_OPERATION_FAILED;
1184         }
1185         ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
1186         if (ret != SQLITE_OK) {
1187                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1188                 sqlite3_finalize(stmt);
1189                 return VC_DB_ERROR_OPERATION_FAILED;
1190         }
1191         ret = sqlite3_step(stmt);
1192         if (ret == SQLITE_DONE) {
1193                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1194                 sqlite3_clear_bindings(stmt);
1195                 sqlite3_finalize(stmt);
1196                 return VC_DB_ERROR_NONE;
1197         }
1198
1199         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
1200
1201         while (SQLITE_ROW == ret) {
1202                 int temp = 0;
1203                 vc_cmd_s* temp_cmd = NULL;
1204                 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
1205                 if (NULL == temp_cmd) {
1206                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allocation fail");
1207
1208                         if (NULL != temp_pid_list) {
1209                                 g_slist_free_full(temp_pid_list, free);
1210                                 temp_pid_list = NULL;
1211                         }
1212
1213                         sqlite3_reset(stmt);
1214                         sqlite3_clear_bindings(stmt);
1215                         sqlite3_finalize(stmt);
1216                         return VC_DB_ERROR_OUT_OF_MEMORY;
1217                 }
1218
1219                 temp = sqlite3_column_int(stmt, 0);
1220                 temp_cmd->pid = temp;
1221
1222                 temp = sqlite3_column_int(stmt, 1);
1223                 temp_cmd->type = temp;
1224
1225                 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
1226
1227                 ret = sqlite3_step(stmt);
1228                 if (SQLITE_DONE == ret)
1229                         break;
1230         }
1231
1232         *pid_list = temp_pid_list;
1233
1234         sqlite3_reset(stmt);
1235         sqlite3_clear_bindings(stmt);
1236         sqlite3_finalize(stmt);
1237         return VC_DB_ERROR_NONE;
1238 }
1239
1240 static int __vc_db_append_commands(sqlite3* db_handle, int pid, int type, vc_cmd_list_h vc_cmd_list)
1241 {
1242         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
1243
1244         int ret = 0;
1245         sqlite3_stmt* stmt = NULL;
1246         const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
1247
1248         /* check whether there is a table or not */
1249         bool is_exist = false;
1250         ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
1251         if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1252                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
1253                 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
1254                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1255                         return VC_DB_ERROR_OPERATION_FAILED;
1256                 }
1257         } else {
1258                 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
1259         }
1260
1261         /* prepare db */
1262         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1263         if (ret != SQLITE_OK) {
1264                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1265                 return VC_DB_ERROR_OPERATION_FAILED;
1266         }
1267         ret = sqlite3_bind_int(stmt, 1, pid);
1268         if (ret != SQLITE_OK) {
1269                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1270                 sqlite3_finalize(stmt);
1271                 return VC_DB_ERROR_OPERATION_FAILED;
1272         }
1273         ret = sqlite3_bind_int(stmt, 2, type);
1274         if (ret != SQLITE_OK) {
1275                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1276                 sqlite3_clear_bindings(stmt);
1277                 sqlite3_finalize(stmt);
1278                 return VC_DB_ERROR_OPERATION_FAILED;
1279         }
1280         ret = sqlite3_step(stmt);
1281         if (ret == SQLITE_DONE) {
1282                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1283                 sqlite3_clear_bindings(stmt);
1284                 sqlite3_finalize(stmt);
1285                 return VC_DB_ERROR_NONE;
1286         }
1287
1288         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
1289
1290         vc_cmd_h temp_cmd = NULL;
1291
1292         while (SQLITE_ROW == ret) {
1293                 int temp = 0;
1294                 char* temp_text = NULL;
1295
1296                 if (0 != vc_cmd_create(&temp_cmd)) {
1297                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
1298                         sqlite3_reset(stmt);
1299                         sqlite3_clear_bindings(stmt);
1300                         sqlite3_finalize(stmt);
1301                         return -1;
1302                 }
1303                 temp = sqlite3_column_int(stmt, 0);
1304                 vc_cmd_set_id(temp_cmd, temp);
1305
1306                 temp = sqlite3_column_int(stmt, 1);
1307                 vc_cmd_set_pid(temp_cmd, temp);
1308
1309                 temp = sqlite3_column_int(stmt, 2);
1310                 vc_cmd_set_type(temp_cmd, temp);
1311
1312                 temp = sqlite3_column_int(stmt, 3);
1313                 vc_cmd_set_format(temp_cmd, temp);
1314
1315                 temp = sqlite3_column_int(stmt, 4);
1316                 vc_cmd_set_domain(temp_cmd, temp);
1317
1318                 temp_text = (char*)sqlite3_column_text(stmt, 5);
1319                 if (NULL != temp_text)
1320                         vc_cmd_set_command(temp_cmd, temp_text);
1321
1322                 temp_text = (char*)sqlite3_column_text(stmt, 6);
1323                 if (NULL != temp_text)
1324                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
1325
1326                 temp_text = (char*)sqlite3_column_text(stmt, 7);
1327                 if (NULL != temp_text)
1328                         vc_cmd_set_appid(temp_cmd, temp_text);
1329
1330                 temp_text = (char*)sqlite3_column_text(stmt, 8);
1331                 if (NULL != temp_text)
1332                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
1333
1334                 temp_text = (char*)sqlite3_column_text(stmt, 9);
1335                 if (NULL != temp_text)
1336                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
1337
1338                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1339                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1340                         vc_cmd_destroy(temp_cmd);
1341                         vc_cmd_list_destroy(vc_cmd_list, true);
1342                         sqlite3_reset(stmt);
1343                         sqlite3_clear_bindings(stmt);
1344                         sqlite3_finalize(stmt);
1345                         return VC_DB_ERROR_OPERATION_FAILED;
1346                 }
1347
1348                 ret = sqlite3_step(stmt);
1349                 if (SQLITE_DONE == ret)
1350                         break;
1351         }
1352
1353         sqlite3_reset(stmt);
1354         sqlite3_clear_bindings(stmt);
1355         sqlite3_finalize(stmt);
1356         return VC_DB_ERROR_NONE;
1357 }
1358
1359 static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
1360 {
1361         if (NULL == src_cmd) {
1362                 SLOG(LOG_WARN, vc_db_tag(), "[Client Data] Input command is NULL");
1363                 return NULL;
1364         }
1365
1366         vc_cmd_s* temp_cmd = NULL;
1367         temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
1368         if (NULL == temp_cmd) {
1369                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1370                 return NULL;
1371         }
1372
1373         temp_cmd->id = src_cmd->id;
1374         temp_cmd->pid = src_cmd->pid;
1375         temp_cmd->index = src_cmd->index;
1376         temp_cmd->type = src_cmd->type;
1377         temp_cmd->format = src_cmd->format;
1378         temp_cmd->domain = src_cmd->domain;
1379
1380         if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type)           temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
1381         else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
1382         else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type)      temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
1383         else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1384         else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type)   temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
1385         else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type)  temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
1386
1387         if (NULL != src_cmd->command) {
1388                 temp_cmd->command = strdup(src_cmd->command);
1389         }
1390
1391         if (NULL != src_cmd->parameter) {
1392                 temp_cmd->parameter = strdup(src_cmd->parameter);
1393         }
1394
1395         if (NULL != src_cmd->appid) {
1396                 temp_cmd->appid = strdup(src_cmd->appid);
1397         }
1398
1399         if (NULL != src_cmd->invocation_name) {
1400                 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
1401         }
1402
1403         if (NULL != src_cmd->fixed) {
1404                 temp_cmd->fixed = strdup(src_cmd->fixed);
1405         }
1406
1407         temp_cmd->key = src_cmd->key;
1408         temp_cmd->modifier = src_cmd->modifier;
1409
1410         return temp_cmd;
1411 }
1412
1413 static int __vc_db_create_table(sqlite3* db_handle, const char* table)
1414 {
1415         SLOG(LOG_INFO, vc_db_tag(), "[INFO] Create DB table (%s)", table);
1416         char* sql = NULL;
1417
1418         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1419                 sql = strdup("CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
1420                                 pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
1421         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1422                 sql = strdup("CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
1423                                 command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
1424         } else {
1425                 return VC_DB_ERROR_INVALID_PARAMETER;
1426         }
1427
1428         int ret = __vc_db_exec_query(db_handle, sql);
1429         if (ret != VC_DB_ERROR_NONE) {
1430                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", table, ret);
1431                 return VC_DB_ERROR_OPERATION_FAILED;
1432         }
1433         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1434
1435         return VC_DB_ERROR_NONE;
1436 }
1437
1438 int __vc_db_open_db(char** path, sqlite3** db_handle)
1439 {
1440         struct stat stat;
1441         int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1442         if (ret != SQLITE_OK) {
1443                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", *path, ret, sqlite3_errmsg(*db_handle));
1444                 if (*db_handle) {
1445                         db_util_close(*db_handle);
1446                         *db_handle = NULL;
1447                 }
1448
1449                 free(*path);
1450                 *path = NULL;
1451                 return VC_DB_ERROR_OPERATION_FAILED;
1452         }
1453
1454         if (lstat(*path, &stat) < 0) {
1455                 char buf_err[256];
1456                 SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
1457                 if (*db_handle)
1458                         db_util_close(*db_handle);
1459                 *db_handle = NULL;
1460
1461                 free(*path);
1462                 *path = NULL;
1463                 return VC_DB_ERROR_OPERATION_FAILED;
1464         }
1465
1466         if (!S_ISREG(stat.st_mode)) {
1467                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1468                 if (*db_handle)
1469                         db_util_close(*db_handle);
1470                 *db_handle = NULL;
1471
1472                 free(*path);
1473                 *path = NULL;
1474                 return VC_DB_ERROR_OPERATION_FAILED;
1475         }
1476
1477         if (!stat.st_size) {
1478                 __vc_db_begin_transaction(*db_handle);
1479
1480                 int ret = -1;
1481                 ret = __vc_db_create_table(*db_handle, VC_INFO_TABLE);
1482                 if (ret != VC_DB_ERROR_NONE) {
1483                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1484                         __vc_db_rollback_transaction(*db_handle);
1485                         return VC_DB_ERROR_OPERATION_FAILED;
1486                 }
1487                 ret = __vc_db_create_table(*db_handle, VC_RESULT_TABLE);
1488                 if (ret != VC_DB_ERROR_NONE) {
1489                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1490                         __vc_db_rollback_transaction(*db_handle);
1491                         return VC_DB_ERROR_OPERATION_FAILED;
1492                 }
1493
1494                 __vc_db_commit_transaction(*db_handle);
1495         }
1496
1497         if (*db_handle) {
1498                 char* err_msg = NULL;
1499                 static const const char* sql = "PRAGMA journal_mode = WAL";
1500                 int ret = sqlite3_exec(*db_handle, sql, NULL, NULL, &err_msg);
1501                 if (ret != SQLITE_OK) {
1502                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1503                 }
1504         }
1505         return VC_DB_ERROR_NONE;
1506 }
1507
1508 static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
1509 {
1510         SLOG(LOG_INFO, vc_db_tag(), "integrity check cb is called");
1511
1512         char *check_str = "ok";
1513         if (0 != strncmp(argv[0], check_str, strlen(check_str))) {
1514                 SLOG(LOG_ERROR, vc_db_tag(), "db integrity result : %s", argv[0]);
1515                 is_db_corrupted = true;
1516                 return -1;
1517         }
1518
1519         SLOG(LOG_INFO, vc_db_tag(), "db integrity result : %s", argv[0]);
1520         return 0;
1521 }
1522
1523 bool __vc_db_connect_db(char** path, sqlite3** db_handle)
1524 {
1525         bool is_connect = false;
1526         int ret = __vc_db_open_db(path, db_handle);
1527         if (0 != ret) {
1528                 int cnt = 0;
1529                 while (cnt <= 5) {
1530                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1531
1532                         usleep(100000);
1533                         if (0 == __vc_db_open_db(path, db_handle)) {
1534                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
1535                                 is_connect = true;
1536                                 break;
1537                         }
1538                         cnt++;
1539                 }
1540         } else {
1541                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
1542                 is_connect = true;
1543         }
1544         return is_connect;
1545 }
1546
1547 int vc_db_initialize(void)
1548 {
1549         SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
1550
1551         if (0 < g_ref_cnt) {
1552                 g_ref_cnt++;
1553                 return VC_DB_ERROR_NONE;
1554         }
1555
1556         /* For voice control DB */
1557         g_path = (char*)calloc(256, sizeof(char));
1558         if (NULL == g_path) {
1559                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1560                 return VC_DB_ERROR_OUT_OF_MEMORY;
1561         }
1562         /* This should be changed to general DB space - TZ_USER_DB */
1563         snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1564
1565         /* For Backup DB */
1566         g_backup_path = (char*)calloc(256, sizeof(char));
1567         if (NULL == g_backup_path) {
1568                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1569                 return VC_DB_ERROR_OUT_OF_MEMORY;
1570         }
1571         snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
1572
1573         bool is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1574         if (false == is_connect) {
1575                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing file");
1576                 if (0 != remove(g_path)) {
1577                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1578                         g_db_cnt = (g_db_cnt + 1) % 1000;
1579                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1580                 }
1581                 is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1582                 if (true == is_connect) {
1583                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
1584                         is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1585                         if (true == is_connect) {
1586                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
1587                                 if (0 != vc_db_restore_command()) {
1588                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1589                                 }
1590                         }
1591                 } else {
1592                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
1593                         return VC_DB_ERROR_OPERATION_FAILED;
1594                 }
1595         }
1596
1597         int ret = sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL);
1598         if (true == is_db_corrupted || SQLITE_CORRUPT == ret) {
1599                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1600                 if (g_db_handle) {
1601                         ret = db_util_close(g_db_handle);
1602                         if (ret != SQLITE_OK) {
1603                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to close db, ret %d: %s", ret, sqlite3_errmsg(g_db_handle));
1604                         }
1605                         g_db_handle = NULL;
1606                 }
1607                 if (0 != remove(g_path)) {
1608                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1609                         g_db_cnt = (g_db_cnt + 1) % 1000;
1610                         snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1611                 }
1612                 is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1613                 if (true == is_connect) {
1614                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
1615                         is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1616                         if (true == is_connect) {
1617                                 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
1618                                 if (0 != vc_db_restore_command()) {
1619                                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1620                                 }
1621                         }
1622                 } else {
1623                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
1624                         return VC_DB_ERROR_OPERATION_FAILED;
1625                 }
1626
1627                 g_ref_cnt++;
1628                 SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization after restore");
1629                 return 0;
1630         }
1631
1632         is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1633         if (false == is_connect) {
1634                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file");
1635                 if (0 != remove(g_backup_path)) {
1636                         g_backup_db_cnt = (g_backup_db_cnt + 1) % 1000;
1637                         SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
1638                         snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
1639                 }
1640                 is_connect = __vc_db_connect_db(&g_path, &g_db_backup_handle);
1641                 if (true == is_connect) {
1642                         SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
1643                         if (0 != vc_db_restore_command()) {
1644                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1645                         }
1646                 }
1647         }
1648
1649         g_ref_cnt++;
1650
1651         SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization");
1652         return VC_DB_ERROR_NONE;
1653 }
1654
1655 int vc_db_finalize(void)
1656 {
1657         if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
1658         if (0 != --g_ref_cnt)
1659                 return VC_DB_ERROR_NONE;
1660
1661         if (NULL != g_path) {
1662                 free(g_path);
1663                 g_path = NULL;
1664         }
1665
1666         if (NULL != g_backup_path) {
1667                 free(g_backup_path);
1668                 g_backup_path = NULL;
1669         }
1670
1671         if (!g_db_handle)
1672                 return 0;
1673         db_util_close(g_db_handle);
1674         g_db_handle = NULL;
1675
1676         if (!g_db_backup_handle)
1677                 return 0;
1678         db_util_close(g_db_backup_handle);
1679         g_db_backup_handle = NULL;
1680
1681         return VC_DB_ERROR_NONE;
1682 }
1683
1684 int vc_db_create_table()
1685 {
1686         __vc_db_reset_handle();
1687         __vc_db_begin_transaction(g_db_handle);
1688
1689         int ret = __vc_db_create_table(g_db_handle, VC_INFO_TABLE);
1690         if (ret != VC_DB_ERROR_NONE) {
1691                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1692                 __vc_db_rollback_transaction(g_db_handle);
1693                 return VC_DB_ERROR_OPERATION_FAILED;
1694         }
1695         ret = __vc_db_create_table(g_db_handle, VC_RESULT_TABLE);
1696         if (ret != VC_DB_ERROR_NONE) {
1697                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1698                 __vc_db_rollback_transaction(g_db_handle);
1699                 return VC_DB_ERROR_OPERATION_FAILED;
1700         }
1701
1702         __vc_db_commit_transaction(g_db_handle);
1703         return VC_DB_ERROR_NONE;
1704 }
1705
1706 int __vc_db_delete_table(sqlite3* db_handle, const char* table)
1707 {
1708         char* sql = NULL;
1709         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1710                 sql = strdup("DELETE FROM vc_result;");
1711         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1712                 sql = strdup("DELETE FROM vc_info;");
1713         } else {
1714                 return VC_DB_ERROR_INVALID_PARAMETER;
1715         }
1716
1717         if (NULL == sql) {
1718                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1719                 return VC_DB_ERROR_OUT_OF_MEMORY;
1720         }
1721
1722         int ret = __vc_db_exec_query(db_handle, sql);
1723         if (ret != VC_DB_ERROR_NONE) {
1724                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1725                 free(sql);
1726                 sql = NULL;
1727                 return ret;
1728         }
1729
1730         free(sql);
1731         sql = NULL;
1732
1733         if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1734                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1735         } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1736                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1737         }
1738
1739         if (NULL == sql) {
1740                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1741                 return VC_DB_ERROR_OUT_OF_MEMORY;
1742         }
1743
1744         ret = __vc_db_exec_query(db_handle, sql);
1745         if (ret != VC_DB_ERROR_NONE) {
1746                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1747                 free(sql);
1748                 sql = NULL;
1749                 return ret;
1750         }
1751
1752         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1753
1754         free(sql);
1755         sql = NULL;
1756         return VC_DB_ERROR_NONE;
1757 }
1758
1759 int vc_db_delete_table(const char* table)
1760 {
1761         __vc_db_reset_handle();
1762         __vc_db_begin_transaction(g_db_handle);
1763
1764         int ret = __vc_db_delete_table(g_db_handle, table);
1765         if (0 != ret) {
1766                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1767                 __vc_db_rollback_transaction(g_db_handle);
1768                 return ret;
1769         }
1770
1771         __vc_db_commit_transaction(g_db_handle);
1772
1773         return VC_DB_ERROR_NONE;
1774 }
1775
1776 int vc_db_begin_transaction(void)
1777 {
1778         __vc_db_reset_handle();
1779         int ret = __vc_db_begin_transaction(g_db_handle);
1780         return ret;
1781 }
1782
1783 int vc_db_rollback_transaction(void)
1784 {
1785         __vc_db_reset_handle();
1786         int ret = __vc_db_rollback_transaction(g_db_handle);
1787         return ret;
1788 }
1789
1790 int vc_db_commit_transaction(void)
1791 {
1792         __vc_db_reset_handle();
1793         int ret = __vc_db_commit_transaction(g_db_handle);
1794         return ret;
1795 }
1796
1797 static void __vc_db_remove_space(char** string)
1798 {
1799         if (NULL == string || NULL == *string)
1800                 return;
1801
1802         char* temp = *string;
1803
1804         //remove previous space
1805         if (' ' == temp[0])
1806                 memmove(temp, temp + 1, strlen(temp));
1807
1808         // remove next space
1809         if (' ' == temp[strlen(temp) - 1])
1810                 temp[strlen(temp) - 1] = '\0';
1811 }
1812
1813 static bool __vc_db_is_valid_vfixed_string(char* string)
1814 {
1815         char* temp = strchr(string, '}');
1816         if (NULL == temp)
1817                 return false;
1818
1819         temp = strchr(string, '{');
1820         if (NULL == temp)
1821                 return false;
1822
1823         temp = strchr(string, '|');
1824         if (NULL == temp)
1825                 return false;
1826         return true;
1827 }
1828
1829 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1830 {
1831         if (NULL == cmd || NULL == cmd->command) {
1832                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
1833                 return VC_DB_ERROR_INVALID_PARAMETER;
1834         }
1835
1836         GSList* temp_list = NULL;
1837         char* temp = NULL;
1838         char* src_cmd = strdup(cmd->command);
1839         char* dst_cmd = NULL;
1840         char merge_cmd[256] = {0, };
1841
1842         if (NULL == src_cmd) {
1843                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1844                 return VC_DB_ERROR_OUT_OF_MEMORY;
1845         }
1846
1847         if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
1848                 // check string validation
1849                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1850                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1851                         free(src_cmd);
1852                         src_cmd = NULL;
1853                         return VC_DB_ERROR_INVALID_PARAMETER;
1854                 }
1855
1856                 // remove close brace, '}'
1857                 char* temp_close = strchr(src_cmd, '}');
1858                 if (NULL != temp_close) {
1859                         temp_close[0] = '\0';
1860                 }
1861
1862                 // extract fixed command and remove space in front of  '{'
1863                 char *tok_ptr = NULL;
1864                 temp = strtok_r(src_cmd, "{", &tok_ptr);
1865                 if (NULL != temp) {
1866                         __vc_db_remove_space(&temp);
1867                         *fixed_cmd = strdup(temp);
1868
1869                         // merge command with fixed and vfixed
1870                         while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1871                                 __vc_db_remove_space(&temp);
1872
1873                                 snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
1874                                 dst_cmd = strdup(merge_cmd);
1875                                 temp_list = g_slist_append(temp_list, dst_cmd);
1876                                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1877                         }
1878                 } else {
1879                         *fixed_cmd = strdup(cmd->command);
1880                 }
1881         } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
1882                 // check string validation
1883                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1884                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1885                         free(src_cmd);
1886                         src_cmd = NULL;
1887                         return VC_DB_ERROR_INVALID_PARAMETER;
1888                 }
1889
1890                 // extract fixed command
1891                 char* temp_fixed = strchr(src_cmd, '}') + 1;
1892                 __vc_db_remove_space(&temp_fixed);
1893                 *fixed_cmd = strdup(temp_fixed);
1894
1895                 // remove close brace, '}'
1896                 char *tok_ptr = NULL;
1897                 temp = strtok_r(src_cmd, "}", &tok_ptr);
1898
1899                 // remove open brace, '{'
1900                 temp = strchr(src_cmd, '{') + 1;
1901
1902                 tok_ptr = NULL;
1903                 temp = strtok_r(temp, "|", &tok_ptr);
1904                 __vc_db_remove_space(&temp);
1905
1906                 // merge command with fixed and vfixed
1907                 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1908                 dst_cmd = strdup(merge_cmd);
1909                 temp_list = g_slist_append(temp_list, dst_cmd);
1910                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1911
1912                 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1913                         __vc_db_remove_space(&temp);
1914
1915                         // merge command with fixed and vfixed
1916                         snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1917                         dst_cmd = strdup(merge_cmd);
1918                         temp_list = g_slist_append(temp_list, dst_cmd);
1919                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1920                 }
1921         } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
1922                 dst_cmd = strdup(src_cmd);
1923                 temp_list = g_slist_append(temp_list, dst_cmd);
1924                 *fixed_cmd = strdup(src_cmd);
1925
1926         } else {
1927                 dst_cmd = strdup(src_cmd);
1928                 temp_list = g_slist_append(temp_list, dst_cmd);
1929         }
1930
1931         *cmd_list = temp_list;
1932
1933         free(src_cmd);
1934         src_cmd = NULL;
1935         return VC_DB_ERROR_NONE;
1936 }
1937
1938 static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
1939 {
1940         GSList* cmd_list = NULL;
1941         char* fixed_cmd = NULL;
1942         vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
1943
1944         int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
1945         if (0 != ret) {
1946                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
1947
1948                 if (NULL != tmp_cmd) {
1949                         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1950                 }
1951                 return ret;
1952         }
1953
1954         if (0 != g_slist_length(cmd_list)) {
1955                 GSList *iter = NULL;
1956                 char* temp_command = NULL;
1957                 iter = g_slist_nth(cmd_list, 0);
1958
1959                 while (NULL != iter) {
1960                         temp_command = iter->data;
1961
1962                         if (NULL != temp_command) {
1963                                 if (NULL != tmp_cmd->command) {
1964                                         free(tmp_cmd->command);
1965                                         tmp_cmd->command = NULL;
1966                                 }
1967                                 tmp_cmd->command = strdup(temp_command);
1968                                 if (NULL != fixed_cmd)
1969                                         tmp_cmd->fixed = strdup(fixed_cmd);
1970                                 else
1971                                         tmp_cmd->fixed = NULL;
1972
1973                                 ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
1974                                 if (ret != VC_DB_ERROR_NONE) {
1975                                         if (NULL != fixed_cmd) {
1976                                                 free(fixed_cmd);
1977                                                 fixed_cmd = NULL;
1978                                         }
1979                                         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1980                                         break;
1981                                 }
1982
1983                                 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name && false == skip_invocation) {
1984                                         char temp[256] = {0, };
1985                                         snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
1986                                         if (NULL != tmp_cmd->command)
1987                                                 free(tmp_cmd->command);
1988
1989                                         tmp_cmd->command = strdup(temp);
1990
1991                                         ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
1992                                         if (ret != VC_DB_ERROR_NONE) {
1993                                                 if (NULL != fixed_cmd) {
1994                                                         free(fixed_cmd);
1995                                                         fixed_cmd = NULL;
1996                                                 }
1997                                                 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1998                                                 break;
1999                                         }
2000                                 }
2001                                 cmd_list = g_slist_remove(cmd_list, temp_command);
2002                                 free(temp_command);
2003                                 temp_command = NULL;
2004                         }
2005                         iter = g_slist_nth(cmd_list, 0);
2006                 }
2007
2008                 if (VC_DB_ERROR_NONE != ret) {
2009                         while (NULL != iter) {
2010                                 temp_command = iter->data;
2011
2012                                 if (NULL != temp_command) {
2013                                         cmd_list = g_slist_remove(cmd_list, temp_command);
2014                                         free(temp_command);
2015                                         temp_command = NULL;
2016                                 }
2017
2018                                 iter = g_slist_nth(cmd_list, 0);
2019                         }
2020
2021                         g_slist_free(cmd_list);
2022                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
2023
2024                         return ret;
2025                 }
2026                 cmd_list = NULL;
2027         }
2028
2029         if (NULL != fixed_cmd) {
2030                 free(fixed_cmd);
2031                 fixed_cmd = NULL;
2032         }
2033         vc_cmd_destroy((vc_cmd_h)tmp_cmd);
2034         return VC_DB_ERROR_NONE;
2035 }
2036
2037 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
2038 {
2039         int ret = __vc_db_insert_command(g_db_handle, pid, type, cmd, skip_invocation);
2040         if (0 != ret) {
2041                 SLOG(LOG_DEBUG, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
2042         }
2043         return ret;
2044 }
2045
2046 static int __vc_db_insert_commands_list(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
2047 {
2048         GSList *iter = NULL;
2049         vc_cmd_s *temp_cmd;
2050
2051         int i;
2052         int count = g_slist_length(cmd_list);
2053         iter = g_slist_nth(cmd_list, 0);
2054
2055         SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
2056
2057         for (i = 0; i < count; i++) {
2058                 if (NULL == iter)
2059                         break;
2060
2061                 temp_cmd = iter->data;
2062
2063                 if (NULL == temp_cmd) {
2064                         SLOG(LOG_ERROR, vc_db_tag(), "command is NULL");
2065                         break;
2066                 }
2067
2068                 if (type == temp_cmd->type) {
2069                         if (NULL != invocation_name)
2070                                 temp_cmd->invocation_name = strdup(invocation_name);
2071
2072                         int ret = __vc_db_insert_command(db_handle, pid, type, temp_cmd, skip_invocation);
2073                         if (ret != VC_DB_ERROR_NONE) {
2074                                 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
2075                                 return ret;
2076                         }
2077                 } else {
2078                         SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
2079                 }
2080                 iter = g_slist_next(iter);
2081         }
2082
2083         return VC_DB_ERROR_NONE;
2084 }
2085
2086 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
2087 {
2088         __vc_db_reset_handle();
2089         __vc_db_begin_transaction(g_db_handle);
2090
2091         int ret = __vc_db_insert_commands_list(g_db_handle, pid, type, cmd_list, invocation_name, false);
2092         if (ret != VC_DB_ERROR_NONE) {
2093                 __vc_db_rollback_transaction(g_db_handle);
2094                 return ret;
2095         }
2096
2097         __vc_db_commit_transaction(g_db_handle);
2098
2099         return VC_DB_ERROR_NONE;
2100 }
2101
2102 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
2103 {
2104         __vc_db_reset_handle();
2105         __vc_db_begin_transaction(g_db_handle);
2106
2107         int ret = __vc_db_get_commands(g_db_handle, pid, type, cmd_list);
2108         if (ret != VC_DB_ERROR_NONE) {
2109                 __vc_db_rollback_transaction(g_db_handle);
2110                 return ret;
2111         }
2112
2113         __vc_db_commit_transaction(g_db_handle);
2114         return VC_DB_ERROR_NONE;
2115 }
2116
2117 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
2118 {
2119         if (NULL == result_text) {
2120                 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
2121                 return VC_DB_ERROR_INVALID_PARAMETER;
2122         }
2123
2124         int ret = vc_db_delete_table(VC_RESULT_TABLE);
2125         if (0 != ret)
2126                 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2127
2128         if (NULL == vc_cmd_list) {
2129                 __vc_db_reset_handle();
2130                 __vc_db_begin_transaction(g_db_handle);
2131                 int ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, NULL);
2132                 if (ret != VC_DB_ERROR_NONE) {
2133                         __vc_db_rollback_transaction(g_db_handle);
2134                         return ret;
2135                 }
2136                 __vc_db_commit_transaction(g_db_handle);
2137                 return VC_DB_ERROR_NONE;
2138         }
2139
2140         /* Make client list node */
2141         vc_cmd_h vc_command = NULL;
2142         vc_cmd_list_first(vc_cmd_list);
2143
2144         while (VC_ERROR_ITERATION_END != ret) {
2145                 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
2146                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
2147                         break;
2148                 }
2149
2150                 if (NULL == vc_command) {
2151                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
2152                         break;
2153                 }
2154
2155                 vc_cmd_s* temp_cmd = NULL;
2156                 temp_cmd = (vc_cmd_s*)vc_command;
2157
2158                 __vc_db_reset_handle();
2159                 __vc_db_begin_transaction(g_db_handle);
2160                 ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, temp_cmd);
2161                 if (ret != VC_DB_ERROR_NONE) {
2162                         __vc_db_rollback_transaction(g_db_handle);
2163                         return ret;
2164                 }
2165                 __vc_db_commit_transaction(g_db_handle);
2166
2167                 ret = vc_cmd_list_next(vc_cmd_list);
2168         }
2169
2170         return VC_DB_ERROR_NONE;
2171 }
2172
2173 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
2174 {
2175         __vc_db_reset_handle();
2176         __vc_db_begin_transaction(g_db_handle);
2177
2178         int ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
2179         if (ret != VC_DB_ERROR_NONE) {
2180                 __vc_db_rollback_transaction(g_db_handle);
2181                 return VC_DB_ERROR_OPERATION_FAILED;
2182         }
2183
2184         if (NULL == msg) {
2185                 int count = 0;
2186                 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
2187                 if (ret != VC_DB_ERROR_NONE) {
2188                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
2189                 }
2190                 if (0 == count) {
2191                         char* appid = NULL;
2192                         // Get appid by pid using app control
2193                         ret = app_manager_get_app_id(pid, &appid);
2194                         if (APP_MANAGER_ERROR_NONE != ret) {
2195                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
2196                         }
2197                         ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
2198                         if (ret != VC_DB_ERROR_NONE) {
2199                                 __vc_db_rollback_transaction(g_db_handle);
2200                                 return ret;
2201                         }
2202                         if (NULL != appid) {
2203                                 free(appid);
2204                                 appid = NULL;
2205                         }
2206                 }
2207         }
2208         __vc_db_commit_transaction(g_db_handle);
2209         return VC_DB_ERROR_NONE;
2210 }
2211
2212 int vc_db_get_appid_list(const char* result, GSList** app_list)
2213 {
2214         __vc_db_reset_handle();
2215         __vc_db_begin_transaction(g_db_handle);
2216
2217         int ret = __vc_db_get_appid(g_db_handle, result, app_list);
2218         if (ret != VC_DB_ERROR_NONE) {
2219                 __vc_db_rollback_transaction(g_db_handle);
2220                 return ret;
2221         }
2222
2223         __vc_db_commit_transaction(g_db_handle);
2224         return VC_DB_ERROR_NONE;
2225 }
2226
2227 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
2228 {
2229         __vc_db_reset_handle();
2230         __vc_db_begin_transaction(g_db_handle);
2231
2232         int ret = __vc_db_get_result_pid_list(g_db_handle, result, pid_list);
2233         if (ret != VC_DB_ERROR_NONE) {
2234                 __vc_db_rollback_transaction(g_db_handle);
2235                 return ret;
2236         }
2237
2238         __vc_db_commit_transaction(g_db_handle);
2239         return VC_DB_ERROR_NONE;
2240 }
2241
2242 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
2243 {
2244         __vc_db_reset_handle();
2245         __vc_db_begin_transaction(g_db_handle);
2246
2247         int ret = __vc_db_append_commands(g_db_handle, pid, type, vc_cmd_list);
2248         if (ret != VC_DB_ERROR_NONE) {
2249                 __vc_db_rollback_transaction(g_db_handle);
2250                 return ret;
2251         }
2252
2253         __vc_db_commit_transaction(g_db_handle);
2254         return VC_DB_ERROR_NONE;
2255 }
2256
2257 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
2258 {
2259         __vc_db_reset_handle();
2260         __vc_db_begin_transaction(g_db_handle);
2261
2262         int ret = 0;
2263         ret = __vc_db_delete_commands(g_db_handle, pid, type, appid);
2264         if (ret != VC_DB_ERROR_NONE) {
2265                 __vc_db_rollback_transaction(g_db_handle);
2266                 return ret;
2267         }
2268
2269         __vc_db_commit_transaction(g_db_handle);
2270         return VC_DB_ERROR_NONE;
2271 }
2272
2273 int vc_db_backup_command()
2274 {
2275         GSList* list = NULL;
2276
2277         __vc_db_reset_handle();
2278         __vc_db_begin_transaction(g_db_backup_handle);
2279
2280         int ret = __vc_db_delete_table(g_db_backup_handle, VC_INFO_TABLE);
2281         if (0 != ret) {
2282                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2283                 __vc_db_rollback_transaction(g_db_backup_handle);
2284                 return ret;
2285         }
2286         __vc_db_commit_transaction(g_db_backup_handle);
2287
2288         ret = vc_db_get_commands(-1, VC_COMMAND_TYPE_BACKGROUND, &list);
2289         if (ret != VC_DB_ERROR_NONE) {
2290                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands");
2291                 return ret;
2292         }
2293
2294         __vc_db_begin_transaction(g_db_backup_handle);
2295
2296         ret = __vc_db_insert_commands_list(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2297         if (ret != VC_DB_ERROR_NONE) {
2298                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list to backup db");
2299                 __vc_db_rollback_transaction(g_db_backup_handle);
2300                 return ret;
2301         }
2302
2303         __vc_db_commit_transaction(g_db_backup_handle);
2304
2305         SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
2306         return VC_DB_ERROR_NONE;
2307 }
2308
2309 int vc_db_restore_command()
2310 {
2311         GSList* list = NULL;
2312
2313         __vc_db_reset_handle();
2314         __vc_db_begin_transaction(g_db_backup_handle);
2315
2316         int ret = __vc_db_get_commands(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, &list);
2317         if (ret != VC_DB_ERROR_NONE) {
2318                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands from backup db");
2319                 __vc_db_rollback_transaction(g_db_backup_handle);
2320                 return ret;
2321         }
2322         __vc_db_commit_transaction(g_db_backup_handle);
2323
2324         ret = vc_db_insert_commands_list(-1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2325         if (ret != VC_DB_ERROR_NONE) {
2326                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list");
2327                 return ret;
2328         }
2329
2330         SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
2331         return VC_DB_ERROR_NONE;
2332 }