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