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