Update dialog and api
[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 "voice_control_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* db_handle = NULL;
60 char* path = NULL;
61 int g_fpid = -1;
62
63 static int __vc_db_transaction(const char* transaction)
64 {
65         sqlite3_stmt* pStmt = NULL;
66
67         int ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
68         if (ret != SQLITE_OK)
69         {
70                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
71                 return VC_DB_ERROR_OPERATION_FAILED;
72         }
73
74         if (sqlite3_step(pStmt) != SQLITE_DONE)
75         {
76                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
77                 sqlite3_finalize(pStmt);
78                 return VC_DB_ERROR_OPERATION_FAILED;
79         }
80
81         sqlite3_finalize(pStmt);
82         return VC_DB_ERROR_NONE;
83 }
84
85 static int __vc_db_begin_transaction(void)
86 {
87         int ret = __vc_db_transaction("BEGIN TRANSACTION");
88         return ret;
89 }
90
91 static int __vc_db_rollback_transaction(void)
92 {
93         int ret = __vc_db_transaction("ROLLBACK TRANSACTION");
94         return ret;
95 }
96
97 static int __vc_db_commit_transaction(void)
98 {
99         int ret = __vc_db_transaction("COMMIT TRANSACTION");
100         return ret;
101 }
102
103 static int __vc_db_exec_query(const char* sql)
104 {
105         char* err_msg = NULL;
106
107         int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
108         if (ret != SQLITE_OK) {
109                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
110                 sqlite3_free(err_msg);
111                 return VC_DB_ERROR_OPERATION_FAILED;
112         }
113         return VC_DB_ERROR_NONE;
114 }
115
116 static int __vc_db_delete_commands(int pid, vc_cmd_type_e type, const char* appid)
117 {
118         sqlite3_stmt* stmt = NULL;
119         char* sql = NULL;
120
121         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid)
122                 sql = strdup("DELETE FROM vc_info WHERE type = ?;");
123         else if (NULL != appid)
124                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND appid = ?;");
125         else
126                 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
127
128         if (NULL == sql) {
129                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
130                 return VC_DB_ERROR_OUT_OF_MEMORY;
131         }
132
133         int ret = 0;
134         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
135         if (ret != SQLITE_OK) {
136                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
137                 return VC_DB_ERROR_OPERATION_FAILED;
138         }
139         ret = sqlite3_bind_int(stmt, 1, (int)type);
140         if (ret != SQLITE_OK) {
141                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
142                 return VC_DB_ERROR_OPERATION_FAILED;
143         }
144
145         if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid) {
146                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d;", type);
147         } else if (NULL != appid) {
148                 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
149                 if (ret != SQLITE_OK) {
150                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
151                         return VC_DB_ERROR_OPERATION_FAILED;
152                 }
153                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
154         } else  {
155                 ret = sqlite3_bind_int(stmt, 2, pid);
156                 if (ret != SQLITE_OK) {
157                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
158                         return VC_DB_ERROR_OPERATION_FAILED;
159                 }
160                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
161         }
162
163         ret = sqlite3_step(stmt);
164         if (ret != SQLITE_DONE) {
165                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned, sql(%s), ret(%d), err(%s)", sql, ret, sqlite3_errmsg(db_handle));
166                 return VC_DB_ERROR_OPERATION_FAILED;
167         }
168
169         sqlite3_reset(stmt);
170         sqlite3_clear_bindings(stmt);
171         sqlite3_finalize(stmt);
172
173         free(sql);
174         sql = NULL;
175         return VC_DB_ERROR_NONE;
176 }
177
178 static int __vc_db_insert_commands(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
179 {
180         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
181
182         sqlite3_stmt* stmt = NULL;
183         const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
184         int ret = 0;
185         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
186         if (ret != SQLITE_OK) {
187                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
188                 return VC_DB_ERROR_OPERATION_FAILED;
189         }
190         ret = sqlite3_bind_int(stmt, 2, pid);
191         if (ret != SQLITE_OK) {
192                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
193                 return VC_DB_ERROR_OPERATION_FAILED;
194         }
195         ret = sqlite3_bind_int(stmt, 3, cmd->type);
196         if (ret != SQLITE_OK) {
197                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
198                 return VC_DB_ERROR_OPERATION_FAILED;
199         }
200         ret = sqlite3_bind_int(stmt, 4, cmd->format);
201         if (ret != SQLITE_OK) {
202                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
203                 return VC_DB_ERROR_OPERATION_FAILED;
204         }
205         ret = sqlite3_bind_int(stmt, 5, cmd->domain);
206         if (ret != SQLITE_OK) {
207                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
208                 return VC_DB_ERROR_OPERATION_FAILED;
209         }
210         ret = sqlite3_bind_text(stmt, 6, cmd->command, -1, SQLITE_TRANSIENT);
211         if (ret != SQLITE_OK) {
212                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
213                 return VC_DB_ERROR_OPERATION_FAILED;
214         }
215
216         char* appid = NULL;
217         if (NULL == cmd->appid) {
218                 // Get appid by pid using app control
219                 ret = app_manager_get_app_id(pid, &appid);
220                 if (APP_MANAGER_ERROR_NONE != ret) {
221                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
222                 }
223                 if (NULL != appid) {
224                         cmd->appid = strdup(appid);
225                         FREE_MEM(appid);
226                 }
227         }
228         ret = sqlite3_bind_text(stmt, 8, cmd->appid, -1, SQLITE_TRANSIENT);
229         if (ret != SQLITE_OK) {
230                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
231                 return VC_DB_ERROR_OPERATION_FAILED;
232         }
233         if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
234                 ret = sqlite3_bind_text(stmt, 9, cmd->invocation_name, -1, SQLITE_TRANSIENT);
235                 if (ret != SQLITE_OK) {
236                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
237                         return VC_DB_ERROR_OPERATION_FAILED;
238                 }
239         }
240         ret = sqlite3_bind_text(stmt, 10, cmd->fixed, -1, SQLITE_TRANSIENT);
241         if (ret != SQLITE_OK) {
242                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
243                 return VC_DB_ERROR_OPERATION_FAILED;
244         }
245         ret = sqlite3_step(stmt);
246         if (ret != SQLITE_DONE) {
247                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
248                 return VC_DB_ERROR_OPERATION_FAILED;
249         }
250
251         SLOG(LOG_ERROR, 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);
252         SLOG(LOG_ERROR, vc_db_tag(), "[SQL] ==== appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
253
254         sqlite3_reset(stmt);
255         sqlite3_clear_bindings(stmt);
256         sqlite3_finalize(stmt);
257         return VC_DB_ERROR_NONE;
258 }
259
260 static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
261 {
262         SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
263
264         int ret = 0;
265         sqlite3_stmt* stmt = NULL;
266         char* sql = NULL;
267         char* appid = NULL;
268
269         if (VC_COMMAND_TYPE_BACKGROUND == type) {
270                 /* For background command */
271                 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
272         } else {
273                 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
274         }
275
276         if (NULL == sql) {
277                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
278                 return VC_DB_ERROR_OUT_OF_MEMORY;
279         }
280         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
281         if (ret != SQLITE_OK) {
282                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
283                 free(sql);
284                 sql = NULL;
285                 return VC_DB_ERROR_OPERATION_FAILED;
286         }
287
288         ret = sqlite3_bind_int(stmt, 1, (int)type);
289         if (ret != SQLITE_OK) {
290                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
291                 return VC_DB_ERROR_OPERATION_FAILED;
292         }
293         if (VC_COMMAND_TYPE_BACKGROUND != type ) {
294                 ret = sqlite3_bind_int(stmt, 2, pid);
295                 if (ret != SQLITE_OK) {
296                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
297                         return VC_DB_ERROR_OPERATION_FAILED;
298                 }
299         }
300
301         if (VC_COMMAND_TYPE_BACKGROUND == type)
302                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
303         else
304                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
305
306         ret = sqlite3_step(stmt);
307         if (ret == SQLITE_DONE) {
308                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
309                 return VC_DB_ERROR_NONE;
310         }
311
312         if (VC_COMMAND_TYPE_BACKGROUND == type && -1 != pid) {
313                 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
314                         SLOG(LOG_WARN, vc_db_tag(), "[WARN] fail to get app id, pid(%d)", pid);
315                 }
316         }
317
318         while (SQLITE_ROW == ret) {
319                 int temp = 0;
320                 char* temp_text = NULL;
321                 vc_cmd_s* temp_cmd = NULL;
322                 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
323                 if (NULL == temp_cmd) {
324                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
325                         if (NULL != appid) {
326                                 free(appid);
327                                 appid = NULL;
328                         }
329                         return VC_DB_ERROR_OUT_OF_MEMORY;
330                 }
331
332                 temp_text = (char*)sqlite3_column_text(stmt, 7);
333                 if (NULL != temp_text)
334                         temp_cmd->appid = strdup(temp_text);
335
336                 if (NULL != appid && NULL != temp_cmd->appid) {
337                         if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_cmd->appid, strlen(appid))) {
338                                 SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
339
340                                 ret = sqlite3_step(stmt);
341                                 if (SQLITE_DONE == ret)
342                                         break;
343
344                                 continue;
345                         }
346                 }
347
348                 temp = sqlite3_column_int(stmt, 0);
349                 temp_cmd->id = temp;
350
351                 temp = sqlite3_column_int(stmt, 1);
352                 temp_cmd->pid = temp;
353
354                 temp = sqlite3_column_int(stmt, 2);
355                 temp_cmd->type = temp;
356
357                 temp = sqlite3_column_int(stmt, 3);
358                 temp_cmd->format = temp;
359
360                 temp  = sqlite3_column_int(stmt, 4);
361                 temp_cmd->domain = temp;
362
363                 temp_text = (char*)sqlite3_column_text(stmt, 5);
364                 if (NULL != temp_text)
365                         temp_cmd->command = strdup(temp_text);
366
367                 temp_text = (char*)sqlite3_column_text(stmt, 6);
368                 if (NULL != temp_text)
369                         temp_cmd->parameter = strdup(temp_text);
370
371                 temp_text = (char*)sqlite3_column_text(stmt, 8);
372                 if (NULL != temp_text)
373                         temp_cmd->invocation_name = strdup(temp_text);
374
375                 temp_text = (char*)sqlite3_column_text(stmt, 9);
376                 if (NULL != temp_text)
377                         temp_cmd->fixed = strdup(temp_text);
378
379                 if (type == temp_cmd->type) {
380                         *cmd_list = g_slist_append(*cmd_list, temp_cmd);
381                 } else {
382                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
383                 }
384                 ret = sqlite3_step(stmt);
385                 if (SQLITE_DONE == ret)
386                         break;
387         }
388
389         if (NULL != appid) {
390                 free(appid);
391                 appid = NULL;
392         }
393
394         sqlite3_reset(stmt);
395         sqlite3_clear_bindings(stmt);
396         sqlite3_finalize(stmt);
397
398         free(sql);
399         sql = NULL;
400         return VC_DB_ERROR_NONE;
401 }
402
403 static int __vc_db_get_pid(const char* appid, int* pid)
404 {
405         bool running = false;
406         int ret = app_manager_is_running(appid, &running);
407         if (APP_MANAGER_ERROR_NONE != ret) {
408                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to check running with appid(%s)", appid);
409                 return VC_DB_ERROR_OPERATION_FAILED;
410         }
411         if (true == running) {
412                 app_context_h app_context = NULL;
413                 ret = app_manager_get_app_context(appid, &app_context);
414                 if (APP_MANAGER_ERROR_NONE != ret) {
415                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
416                         return VC_DB_ERROR_OPERATION_FAILED;
417                 }
418
419                 ret = app_context_get_pid(app_context, pid);
420                 if (APP_MANAGER_ERROR_NONE != ret) {
421                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get pid, ret(%d), appid(%s)", ret, appid);
422                         return VC_DB_ERROR_OPERATION_FAILED;
423                 }
424
425                 ret = app_context_destroy(app_context);
426                 if (APP_MANAGER_ERROR_NONE != ret) {
427                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to destroy app context, ret(%d), appid(%s)", ret, appid);
428                         return VC_DB_ERROR_OPERATION_FAILED;
429                 }
430         } else {
431                 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
432         }
433         return VC_DB_ERROR_NONE;
434 }
435
436 static int __vc_db_insert_result(const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
437 {
438         SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
439
440         sqlite3_stmt* stmt = NULL;
441         const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
442         int ret = 0;
443         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
444         if (ret != SQLITE_OK) {
445                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
446                 return VC_DB_ERROR_OPERATION_FAILED;
447         }
448         ret = sqlite3_bind_text(stmt, 2, result_text, -1, SQLITE_TRANSIENT);
449         if (ret != SQLITE_OK) {
450                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
451                 return VC_DB_ERROR_OPERATION_FAILED;
452         }
453         ret = sqlite3_bind_int(stmt, 3, event);
454         if (ret != SQLITE_OK) {
455                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
456                 return VC_DB_ERROR_OPERATION_FAILED;
457         }
458         ret = sqlite3_bind_text(stmt, 4, msg, -1, SQLITE_TRANSIENT);
459         if (ret != SQLITE_OK) {
460                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
461                 return VC_DB_ERROR_OPERATION_FAILED;
462         }
463         ret = sqlite3_bind_int(stmt, 5, exclusive);
464         if (ret != SQLITE_OK) {
465                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
466                 return VC_DB_ERROR_OPERATION_FAILED;
467         }
468
469         if (NULL == cmd) {
470                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d))", result_text, event, msg, exclusive);
471
472                 ret = sqlite3_step(stmt);
473                 if (ret != SQLITE_DONE) {
474                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
475                         return VC_DB_ERROR_OPERATION_FAILED;
476                 }
477                 sqlite3_reset(stmt);
478                 sqlite3_clear_bindings(stmt);
479                 sqlite3_finalize(stmt);
480                 return VC_DB_ERROR_NONE;
481         }
482
483         if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
484                 int pid = -1;
485                 ret = __vc_db_get_pid(cmd->appid, &pid);
486                 if (0 != ret) {
487                         SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
488                 } else {
489                         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
490                         if (-1 != pid)
491                                 cmd->pid = pid;
492                 }
493         }
494         ret = sqlite3_bind_int(stmt, 6, cmd->pid);
495         if (ret != SQLITE_OK) {
496                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
497                 return VC_DB_ERROR_OPERATION_FAILED;
498         }
499         ret = sqlite3_bind_int(stmt, 7, cmd->type);
500         if (ret != SQLITE_OK) {
501                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
502                 return VC_DB_ERROR_OPERATION_FAILED;
503         }
504         ret = sqlite3_bind_int(stmt, 8, cmd->format);
505         if (ret != SQLITE_OK) {
506                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
507                 return VC_DB_ERROR_OPERATION_FAILED;
508         }
509         ret = sqlite3_bind_int(stmt, 9, cmd->domain);
510         if (ret != SQLITE_OK) {
511                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
512                 return VC_DB_ERROR_OPERATION_FAILED;
513         }
514         ret = sqlite3_bind_text(stmt, 10, cmd->command, -1, SQLITE_TRANSIENT);
515         if (ret != SQLITE_OK) {
516                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
517                 return VC_DB_ERROR_OPERATION_FAILED;
518         }
519         ret = sqlite3_bind_text(stmt, 11, cmd->parameter, -1, SQLITE_TRANSIENT);
520         if (ret != SQLITE_OK) {
521                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
522                 return VC_DB_ERROR_OPERATION_FAILED;
523         }
524         ret = sqlite3_bind_text(stmt, 12, cmd->appid, -1, SQLITE_TRANSIENT);
525         if (ret != SQLITE_OK) {
526                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
527                 return VC_DB_ERROR_OPERATION_FAILED;
528         }
529         ret = sqlite3_bind_text(stmt, 13, cmd->invocation_name, -1, SQLITE_TRANSIENT);
530         if (ret != SQLITE_OK) {
531                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
532                 return VC_DB_ERROR_OPERATION_FAILED;
533         }
534         ret = sqlite3_bind_text(stmt, 14, cmd->fixed, -1, SQLITE_TRANSIENT);
535         if (ret != SQLITE_OK) {
536                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
537                 return VC_DB_ERROR_OPERATION_FAILED;
538         }
539         ret = sqlite3_step(stmt);
540         if (ret != SQLITE_DONE) {
541                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
542                 return VC_DB_ERROR_OPERATION_FAILED;
543         }
544
545         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)",
546                                 result_text, event, msg, exclusive, cmd->pid, cmd->type, cmd->format, cmd->domain, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
547
548         sqlite3_reset(stmt);
549         sqlite3_clear_bindings(stmt);
550         sqlite3_finalize(stmt);
551
552         return VC_DB_ERROR_NONE;
553 }
554
555 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
556 {
557         if (NULL == org_cmd || NULL == new_cmd) {
558                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
559                 return VC_DB_ERROR_INVALID_PARAMETER;
560         }
561
562         if (NULL == invocation_name) {
563                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, invocation name is NULL, org_cmd(%s)", org_cmd);
564                 return VC_DB_ERROR_INVALID_PARAMETER;
565         }
566
567         if (strlen(org_cmd) <= strlen(invocation_name)) {
568                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
569                 return VC_DB_ERROR_INVALID_PARAMETER;
570         }
571
572         if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
573                 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
574         }
575         SLOG(LOG_DEBUG, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, *new_cmd, invocation_name);
576         return VC_DB_ERROR_NONE;
577 }
578
579 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
580 {
581         if (NULL == command || NULL == temp_unfixed) {
582                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
583                 return VC_DB_ERROR_INVALID_PARAMETER;
584         }
585
586         if (NULL == fixed) {
587                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, fixed cmd is NULL, org_cmd(%s)", command);
588                 return VC_DB_ERROR_INVALID_PARAMETER;
589         }
590
591         if (strlen(command) <= strlen(fixed)) {
592                 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to extract unfixed command, cmd(%s) fixed(%s)", command, fixed);
593                 return VC_DB_ERROR_INVALID_PARAMETER;
594         }
595
596         char* temp = (char*)calloc(256, sizeof(char));
597         if (0 == strncasecmp(command, fixed, strlen(fixed))) {
598                 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
599                 SLOG(LOG_WARN, vc_db_tag(), "==");
600         } else  if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
601                 strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
602                 SLOG(LOG_WARN, vc_db_tag(), "==");
603         }
604
605         SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
606         if (NULL != temp) {
607                 *temp_unfixed = strdup(temp);
608                 free(temp);
609                 temp = NULL;
610         }
611         return VC_DB_ERROR_NONE;
612 }
613
614 static int __vc_db_get_result(char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
615 {
616         int ret = 0;
617         sqlite3_stmt* stmt = NULL;
618         char* sql = NULL;
619         if (-1 == pid)
620                 sql = strdup("SELECT * FROM vc_result;");
621         else if (NULL != appid)
622                 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
623         else
624                 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
625
626         if (NULL == sql) {
627                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
628                 return VC_DB_ERROR_OUT_OF_MEMORY;
629         }
630
631         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
632         if (ret != SQLITE_OK) {
633                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
634                 return VC_DB_ERROR_OPERATION_FAILED;
635         }
636         if (NULL != appid) {
637                 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
638                 if (ret != SQLITE_OK) {
639                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
640                         return VC_DB_ERROR_OPERATION_FAILED;
641                 }
642         } else if (-1 != pid) {
643                 ret = sqlite3_bind_int(stmt, 1, pid);
644                 if (ret != SQLITE_OK) {
645                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
646                         return VC_DB_ERROR_OPERATION_FAILED;
647                 }
648         }
649         ret = sqlite3_step(stmt);
650         if (ret == SQLITE_DONE) {
651                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
652                 return VC_DB_ERROR_NONE;
653         }
654
655         if (-1 == pid)
656                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
657         else if (NULL != appid)
658                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %d;", appid);
659
660         else
661                 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
662
663         vc_cmd_h temp_cmd = NULL;
664         while (SQLITE_ROW == ret) {
665                 int temp = 0;
666                 char* temp_text = NULL;
667                 const char* invocation_name = NULL;
668
669                 temp_text = (char*)sqlite3_column_text(stmt, 1);
670                 if (NULL != temp_text)
671                         *result_text = strdup(temp_text);
672
673                 temp = sqlite3_column_int(stmt, 2);
674                 *event = temp;
675
676                 if (NULL != msg) {
677                         temp_text = (char*)sqlite3_column_text(stmt, 3);
678                         if (NULL != temp_text)
679                                 *msg = strdup(temp_text);
680                 }
681
682                 if (0 != vc_cmd_create(&temp_cmd)) {
683                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
684                         if (NULL != *result_text) {
685                                 free(*result_text);
686                                 *result_text = NULL;
687                         }
688
689                         if (NULL != msg && NULL != *msg) {
690                                 free(*msg);
691                                 *msg = NULL;
692                         }
693                         return -1;
694                 }
695
696                 temp = sqlite3_column_int(stmt, 0);
697                 vc_cmd_set_id(temp_cmd, temp);
698
699                 temp = sqlite3_column_int(stmt, 5);
700                 vc_cmd_set_pid(temp_cmd, temp);
701
702                 temp = sqlite3_column_int(stmt, 6);
703                 vc_cmd_set_type(temp_cmd, temp);
704
705                 temp = sqlite3_column_int(stmt, 7);
706                 vc_cmd_set_format(temp_cmd, temp);
707
708                 temp = sqlite3_column_int(stmt, 8);
709                 vc_cmd_set_domain(temp_cmd, temp);
710
711                 // invocation name
712                 invocation_name = (char*)sqlite3_column_text(stmt, 12);
713                 // command name
714                 temp_text = (char*)sqlite3_column_text(stmt, 9);
715                 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
716
717                 if (NULL != temp_text) {
718                         char* temp_command = NULL;
719                         char* temp_unfixed = NULL;
720                         char* temp_fixed = NULL;
721
722                         if (NULL != invocation_name)
723                                 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
724
725                         if (NULL == temp_command) {
726                                 temp_command = strdup(temp_text);
727                         } else {
728                                 // remove invocation name from result_text
729                                 free(*result_text);
730                                 *result_text = strdup(temp_command);
731                         }
732
733                         // fixed
734                         temp_fixed = (char*)sqlite3_column_text(stmt, 13);
735                         if (NULL != temp_fixed)
736                                 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
737
738                         // unfixed
739                         temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
740
741                         if (NULL != temp_unfixed) {
742                                 char merge_result[256] = {0, };
743                                 vc_cmd_set_command(temp_cmd, temp_command);
744                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
745                                 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
746                                 if (NULL != *result_text)
747                                         free(*result_text);
748                                 *result_text = strdup(merge_result);
749                         } else if (NULL != temp_fixed) {
750                                 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
751                                 vc_cmd_set_command(temp_cmd, temp_fixed);
752                                 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
753                                 if (NULL != temp_unfixed) {
754                                         free(temp_unfixed);
755                                         temp_unfixed = NULL;
756                                 }
757                         } else {
758                                 vc_cmd_set_command(temp_cmd, temp_command);
759                         }
760                         free(temp_command);
761                         temp_command = NULL;
762                 }
763
764                 temp_text = (char*)sqlite3_column_text(stmt, 11);
765                 if (NULL != temp_text)
766                         vc_cmd_set_appid(temp_cmd, temp_text);
767
768                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
769                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
770                         vc_cmd_destroy(temp_cmd);
771                         vc_cmd_list_destroy(vc_cmd_list, true);
772                         if (NULL != *result_text) {
773                                 free(*result_text);
774                                 *result_text = NULL;
775                         }
776
777                         if (NULL != msg && NULL != *msg) {
778                                 free(*msg);
779                                 *msg = NULL;
780                         }
781
782                         return VC_DB_ERROR_OPERATION_FAILED;
783                 }
784
785                 ret = sqlite3_step(stmt);
786                 if (SQLITE_DONE == ret)
787                         break;
788         }
789
790         sqlite3_reset(stmt);
791         sqlite3_clear_bindings(stmt);
792         sqlite3_finalize(stmt);
793
794         free(sql);
795         sql = NULL;
796         return VC_DB_ERROR_NONE;
797 }
798
799 static int __vc_db_get_appid(const char* result, GSList** app_list)
800 {
801         GSList* temp_app_list = NULL;
802
803         int ret = 0;
804         sqlite3_stmt* stmt = NULL;
805         const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
806
807         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
808         if (ret != SQLITE_OK) {
809                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
810                 return VC_DB_ERROR_OPERATION_FAILED;
811         }
812         ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
813         if (ret != SQLITE_OK) {
814                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
815                 return VC_DB_ERROR_OPERATION_FAILED;
816         }
817         ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
818         if (ret != SQLITE_OK) {
819                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
820                 return VC_DB_ERROR_OPERATION_FAILED;
821         }
822         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
823         ret = sqlite3_step(stmt);
824         if (ret == SQLITE_DONE) {
825                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
826                 return VC_DB_ERROR_NONE;
827         }
828
829         while (SQLITE_ROW == ret) {
830                 char* temp_text = NULL;
831                 vc_deactivated_app_s* temp_app = NULL;
832                 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
833                 if (NULL == temp_app) {
834                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
835                         return VC_DB_ERROR_OUT_OF_MEMORY;
836                 }
837
838                 temp_text = (char*)sqlite3_column_text(stmt, 11);
839                 if (NULL != temp_text)
840                         temp_app->appid = strdup(temp_text);
841
842                 temp_app_list = g_slist_append(temp_app_list, temp_app);
843
844                 ret = sqlite3_step(stmt);
845                 if (SQLITE_DONE == ret)
846                         break;
847         }
848
849         *app_list = temp_app_list;
850
851         sqlite3_reset(stmt);
852         sqlite3_clear_bindings(stmt);
853         sqlite3_finalize(stmt);
854         return VC_DB_ERROR_NONE;
855 }
856
857 int __vc_db_get_result_pid_list(const char* result, GSList** pid_list)
858 {
859         GSList* temp_pid_list = NULL;
860
861         int ret = 0;
862         sqlite3_stmt* stmt = NULL;
863         const char* sql = "SELECT * FROM vc_result WHERE result = ? COLLATE NOCASE;";
864
865         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
866         if (ret != SQLITE_OK) {
867                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
868                 return VC_DB_ERROR_OPERATION_FAILED;
869         }
870         ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
871         if (ret != SQLITE_OK) {
872                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
873                 return VC_DB_ERROR_OPERATION_FAILED;
874         }
875         ret = sqlite3_step(stmt);
876         if (ret == SQLITE_DONE) {
877                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
878                 return VC_DB_ERROR_NONE;
879         }
880
881         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
882
883         while (SQLITE_ROW == ret) {
884                 int temp = 0;
885                 vc_cmd_s* temp_cmd = NULL;
886                 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
887                 if (NULL == temp_cmd) {
888                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
889                         return VC_DB_ERROR_OUT_OF_MEMORY;
890                 }
891
892                 temp = sqlite3_column_int(stmt, 0);
893                 temp_cmd->id = temp;
894
895                 temp = sqlite3_column_int(stmt, 5);
896                 temp_cmd->pid = temp;
897
898                 temp = sqlite3_column_int(stmt, 6);
899                 temp_cmd->type = temp;
900
901                 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
902
903                 ret = sqlite3_step(stmt);
904                 if (SQLITE_DONE == ret)
905                         break;
906         }
907
908         *pid_list = temp_pid_list;
909
910         sqlite3_reset(stmt);
911         sqlite3_clear_bindings(stmt);
912         sqlite3_finalize(stmt);
913         return VC_DB_ERROR_NONE;
914 }
915
916 static int __vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
917 {
918         SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
919
920         int ret = 0;
921         sqlite3_stmt* stmt = NULL;
922         const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
923
924         ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
925         if (ret != SQLITE_OK) {
926                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
927                 return VC_DB_ERROR_OPERATION_FAILED;
928         }
929         ret = sqlite3_bind_int(stmt, 1, pid);
930         if (ret != SQLITE_OK) {
931                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
932                 return VC_DB_ERROR_OPERATION_FAILED;
933         }
934         ret = sqlite3_bind_int(stmt, 2, type);
935         if (ret != SQLITE_OK) {
936                 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
937                 return VC_DB_ERROR_OPERATION_FAILED;
938         }
939         ret = sqlite3_step(stmt);
940         if (ret == SQLITE_DONE) {
941                 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
942                 return VC_DB_ERROR_NONE;
943         }
944
945         SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
946
947         vc_cmd_h temp_cmd = NULL;
948
949         while (SQLITE_ROW == ret) {
950                 int temp = 0;
951                 char* temp_text = NULL;
952
953                 if (0 != vc_cmd_create(&temp_cmd)) {
954                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
955                         return -1;
956                 }
957                 temp = sqlite3_column_int(stmt, 0);
958                 vc_cmd_set_id(temp_cmd, temp);
959
960                 temp = sqlite3_column_int(stmt, 1);
961                 vc_cmd_set_pid(temp_cmd, temp);
962
963                 temp = sqlite3_column_int(stmt, 2);
964                 vc_cmd_set_type(temp_cmd, temp);
965
966                 temp = sqlite3_column_int(stmt, 3);
967                 vc_cmd_set_format(temp_cmd, temp);
968
969                 temp = sqlite3_column_int(stmt, 4);
970                 vc_cmd_set_domain(temp_cmd, temp);
971
972                 temp_text = (char*)sqlite3_column_text(stmt, 5);
973                 if (NULL != temp_text)
974                         vc_cmd_set_command(temp_cmd, temp_text);
975
976                 temp_text = (char*)sqlite3_column_text(stmt, 6);
977                 if (NULL != temp_text)
978                         vc_cmd_set_unfixed_command(temp_cmd, temp_text);
979
980                 temp_text = (char*)sqlite3_column_text(stmt, 7);
981                 if (NULL != temp_text)
982                         vc_cmd_set_appid(temp_cmd, temp_text);
983
984                 temp_text = (char*)sqlite3_column_text(stmt, 8);
985                 if (NULL != temp_text)
986                         vc_cmd_set_invocation_name(temp_cmd, temp_text);
987
988                 temp_text = (char*)sqlite3_column_text(stmt, 9);
989                 if (NULL != temp_text)
990                         vc_cmd_set_fixed_command(temp_cmd, temp_text);
991
992                 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
993                         SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
994                         vc_cmd_destroy(temp_cmd);
995                         vc_cmd_list_destroy(vc_cmd_list, true);
996                         return VC_DB_ERROR_OPERATION_FAILED;
997                 }
998
999                 ret = sqlite3_step(stmt);
1000                 if (SQLITE_DONE == ret)
1001                         break;
1002         }
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_initialize(void)
1011 {
1012         SLOG(LOG_ERROR, vc_db_tag(), "DB initialization");
1013
1014         path = (char*)calloc(256, sizeof(char));
1015         if (NULL == path) {
1016                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1017                 return VC_DB_ERROR_OUT_OF_MEMORY;
1018         }
1019
1020         /* This should be changed to general DB space - TZ_USER_DB */
1021         snprintf(path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1022
1023         struct stat stat;
1024         int ret = db_util_open(path, &db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1025         if (ret != SQLITE_OK) {
1026                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", path, ret, sqlite3_errmsg(db_handle));
1027                 if (db_handle) {
1028                         db_util_close(db_handle);
1029                         db_handle = NULL;
1030                 }
1031                 return VC_DB_ERROR_OPERATION_FAILED;
1032         }
1033
1034         if (lstat(path, &stat) < 0) {
1035             char buf_err[256];
1036            SLOG(LOG_ERROR, vc_db_tag(), "%s", strerror_r(errno, buf_err, sizeof (buf_err)));
1037             if (db_handle)
1038                 db_util_close(db_handle);
1039             db_handle = NULL;
1040             return VC_DB_ERROR_OPERATION_FAILED;
1041         }
1042
1043         if (!S_ISREG(stat.st_mode)) {
1044             SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1045             if (db_handle)
1046                 db_util_close(db_handle);
1047             db_handle = NULL;
1048             return VC_DB_ERROR_OPERATION_FAILED;
1049         }
1050
1051         if (!stat.st_size) {
1052             vc_db_create_table();
1053         }
1054
1055         if (db_handle) {
1056                 char* err_msg = NULL;
1057                 static const const char* sql = "PRAGMA journal_mode = WAL";
1058                 int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
1059                 if (ret != SQLITE_OK) {
1060                         SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1061                 }
1062         }
1063
1064         return VC_DB_ERROR_NONE;
1065 }
1066
1067 int vc_db_finalize(void)
1068 {
1069         if (NULL != path) {
1070                 free(path);
1071                 path = NULL;
1072         }
1073
1074         if (!db_handle)
1075                 return 0;
1076
1077         db_util_close(db_handle);
1078
1079         db_handle = NULL;
1080         return VC_DB_ERROR_NONE;
1081 }
1082
1083 int vc_db_create_table(void)
1084 {
1085         __vc_db_begin_transaction();
1086
1087         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, \
1088                                         command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1089         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,\
1090                                         pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1091
1092         int ret = __vc_db_exec_query(vc_info_sql);
1093         if (ret != VC_DB_ERROR_NONE) {
1094                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1095                 __vc_db_rollback_transaction();
1096                 return VC_DB_ERROR_OPERATION_FAILED;
1097         }
1098         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_info_sql);
1099
1100         ret = __vc_db_exec_query(vc_result_sql);
1101         if (ret != VC_DB_ERROR_NONE) {
1102                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1103                 __vc_db_rollback_transaction();
1104                 return ret;
1105         }
1106         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_result_sql);
1107
1108         __vc_db_commit_transaction();
1109         return VC_DB_ERROR_NONE;
1110 }
1111
1112 int vc_db_delete_table(const char* table)
1113 {
1114         char* sql = NULL;
1115         if (0 == strncmp(table, "result", strlen(table))) {
1116                 sql = strdup("DELETE FROM vc_result;");
1117         } else if (0 == strncmp(table, "command", strlen(table))) {
1118                 sql = strdup("DELETE_FROM vc_info;");
1119         } else {
1120                 return VC_DB_ERROR_INVALID_PARAMETER;
1121         }
1122
1123         if (NULL == sql) {
1124                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1125                 return VC_DB_ERROR_OUT_OF_MEMORY;
1126         }
1127
1128         __vc_db_begin_transaction();
1129
1130         int ret = __vc_db_exec_query(sql);
1131         if (ret != VC_DB_ERROR_NONE) {
1132                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1133                 __vc_db_rollback_transaction();
1134                 free(sql);
1135                 sql = NULL;
1136                 return ret;
1137         }
1138
1139         free(sql);
1140         sql = NULL;
1141
1142         if (0 == strncmp(table, "result", strlen(table))) {
1143                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1144         } else if (0 == strncmp(table, "command", strlen(table))) {
1145                 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1146         }
1147
1148         if (NULL == sql) {
1149                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1150                 return VC_DB_ERROR_OUT_OF_MEMORY;
1151         }
1152
1153         ret = __vc_db_exec_query(sql);
1154         if (ret != VC_DB_ERROR_NONE) {
1155                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1156                 __vc_db_rollback_transaction();
1157                 free(sql);
1158                 sql = NULL;
1159                 return ret;
1160         }
1161
1162         SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1163         __vc_db_commit_transaction();
1164
1165         free(sql);
1166         sql = NULL;
1167         return VC_DB_ERROR_NONE;
1168 }
1169
1170 int vc_db_begin_transaction(void)
1171 {
1172         int ret = __vc_db_begin_transaction();
1173         return ret;
1174 }
1175
1176 int vc_db_rollback_transaction(void)
1177 {
1178         int ret = __vc_db_rollback_transaction();
1179         return ret;
1180 }
1181
1182 int vc_db_commit_transaction(void)
1183 {
1184         int ret = __vc_db_commit_transaction();
1185         return ret;
1186 }
1187
1188 static void __vc_db_remove_space(char** string)
1189 {
1190         if (NULL == string || NULL == *string)
1191                 return;
1192
1193         char* temp = *string;
1194
1195         //remove previous space
1196         if (' ' == temp[0])
1197                 strncpy(temp, temp + 1, strlen(temp));
1198
1199         // remove next space
1200         if (' ' == temp[strlen(temp) - 1])
1201                 temp[strlen(temp) - 1] = '\0';
1202 }
1203
1204 static bool __vc_db_is_valid_vfixed_string(char* string)
1205 {
1206         char* temp = strchr(string, '}');
1207         if (NULL == temp)
1208                 return false;
1209
1210         temp = strchr(string, '{');
1211         if (NULL == temp)
1212                 return false;
1213
1214         temp = strchr(string, '|');
1215         if (NULL == temp)
1216                 return false;
1217         return true;
1218 }
1219
1220 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1221 {
1222         if (NULL == cmd || NULL == cmd->command) {
1223                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
1224                 return VC_DB_ERROR_INVALID_PARAMETER;
1225         }
1226
1227         GSList* temp_list = NULL;
1228         char* temp = NULL;
1229         char* src_cmd = strdup(cmd->command);
1230         char* dst_cmd = NULL;
1231         char merge_cmd[256] = {0, };
1232
1233         if (NULL == src_cmd) {
1234                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1235                 return VC_DB_ERROR_OUT_OF_MEMORY;
1236         }
1237
1238         if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
1239                 // check string validation
1240                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1241                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1242                         free(src_cmd);
1243                         src_cmd = NULL;
1244                         return VC_DB_ERROR_INVALID_PARAMETER;
1245                 }
1246
1247                 // remove close brace, '}'
1248                 char* temp_close = strchr(src_cmd, '}');
1249                 temp_close[0] = '\0';
1250
1251                 // extract fixed command and remove space in front of  '{'
1252                 temp = strtok(src_cmd, "{");
1253                 __vc_db_remove_space(&temp);
1254                 *fixed_cmd = strdup(temp);
1255
1256                 // merge command with fixed and vfixed
1257                 while (NULL != (temp = strtok(NULL, "|"))) {
1258                         __vc_db_remove_space(&temp);
1259
1260                         snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
1261                         dst_cmd = strdup(merge_cmd);
1262                         temp_list = g_slist_append(temp_list, dst_cmd);
1263                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1264                 }
1265         } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
1266                 // check string validation
1267                 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1268                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1269                         free(src_cmd);
1270                         src_cmd = NULL;
1271                         return VC_DB_ERROR_INVALID_PARAMETER;
1272                 }
1273
1274                 // extract fixed command
1275                 char* temp_fixed = strchr(src_cmd, '}') + 1;
1276                 __vc_db_remove_space(&temp_fixed);
1277                 *fixed_cmd = strdup(temp_fixed);
1278
1279                 // remove close brace, '}'
1280                 temp = strtok(src_cmd, "}");
1281
1282                 // remove open brace, '{'
1283                 temp = strchr(src_cmd, '{') + 1;
1284
1285                 temp = strtok(temp, "|");
1286                 __vc_db_remove_space(&temp);
1287
1288                 // merge command with fixed and vfixed
1289                 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1290                 dst_cmd = strdup(merge_cmd);
1291                 temp_list = g_slist_append(temp_list, dst_cmd);
1292                 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1293
1294                 while (NULL != (temp = strtok(NULL, "|"))) {
1295                         __vc_db_remove_space(&temp);
1296
1297                         // merge command with fixed and vfixed
1298                         snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1299                         dst_cmd = strdup(merge_cmd);
1300                         temp_list = g_slist_append(temp_list, dst_cmd);
1301                         SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1302                         }
1303         } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
1304                 dst_cmd = strdup(src_cmd);
1305                 temp_list = g_slist_append(temp_list, dst_cmd);
1306                 *fixed_cmd = strdup(src_cmd);
1307
1308         } else {
1309                 dst_cmd = strdup(src_cmd);
1310                 temp_list = g_slist_append(temp_list, dst_cmd);
1311         }
1312
1313         *cmd_list = temp_list;
1314
1315         free(src_cmd);
1316         src_cmd = NULL;
1317         return VC_DB_ERROR_NONE;
1318 }
1319
1320 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
1321 {
1322         GSList* cmd_list = NULL;
1323         char* fixed_cmd = NULL;
1324
1325         int ret = __vc_db_generate_command(cmd, &fixed_cmd, &cmd_list);
1326         if (0 != ret) {
1327                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
1328                 return ret;
1329         }
1330
1331         if (0 != g_slist_length(cmd_list)) {
1332                 GSList *iter = NULL;
1333                 char* temp_command = NULL;
1334                 iter = g_slist_nth(cmd_list, 0);
1335
1336                 while (NULL != iter) {
1337                         temp_command = iter->data;
1338
1339                         if (NULL != temp_command) {
1340                                 if (NULL != cmd->command) {
1341                                         free(cmd->command);
1342                                         cmd->command = NULL;
1343                                 }
1344                                 cmd->command = strdup(temp_command);
1345                                 if (NULL != fixed_cmd)
1346                                         cmd->fixed = strdup(fixed_cmd);
1347                                 else
1348                                         cmd->fixed = NULL;
1349
1350                                 int ret = __vc_db_insert_commands(pid, type, cmd);
1351                                 if (ret != VC_DB_ERROR_NONE) {
1352                                         if (NULL != fixed_cmd) {
1353                                                 free(fixed_cmd);
1354                                                 fixed_cmd = NULL;
1355                                         }
1356                                         return ret;
1357                                 }
1358
1359                                 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
1360                                         char temp[256] = {0, };
1361                                         snprintf(temp, 256, "%s %s", cmd->invocation_name, cmd->command);
1362                                         if (NULL != cmd->command)
1363                                                 free(cmd->command);
1364
1365                                         cmd->command = strdup(temp);
1366
1367                                         ret = __vc_db_insert_commands(pid, type, cmd);
1368                                         if (ret != VC_DB_ERROR_NONE) {
1369                                                 if (NULL != fixed_cmd) {
1370                                                         free(fixed_cmd);
1371                                                         fixed_cmd = NULL;
1372                                                 }
1373                                                 return ret;
1374                                         }
1375                                 }
1376
1377                         }
1378                         if (NULL != temp_command) {
1379                                 free(temp_command);
1380                                 temp_command = NULL;
1381                         }
1382                         iter = g_slist_next(iter);
1383                 }
1384                 cmd_list = NULL;
1385         }
1386
1387         if (NULL != fixed_cmd) {
1388                 free(fixed_cmd);
1389                 fixed_cmd = NULL;
1390         }
1391         return VC_DB_ERROR_NONE;
1392 }
1393
1394 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name)
1395 {
1396         GSList *iter = NULL;
1397         vc_cmd_s *temp_cmd;
1398
1399         int i;
1400         int count = g_slist_length(cmd_list);
1401         iter = g_slist_nth(cmd_list, 0);
1402
1403         SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
1404
1405         __vc_db_begin_transaction();
1406
1407         for (i = 0; i < count; i++) {
1408                 if (NULL == iter)
1409                         break;
1410
1411                 temp_cmd = iter->data;
1412
1413                 if (NULL == temp_cmd) {
1414                         SLOG(LOG_ERROR, vc_db_tag(), "comamnd is NULL");
1415                         break;
1416                 }
1417
1418                 if (type == temp_cmd->type) {
1419                         if (NULL != invocation_name)
1420                                 temp_cmd->invocation_name = strdup(invocation_name);
1421
1422                         int ret = vc_db_insert_command(pid, type, temp_cmd);
1423                         if (ret != VC_DB_ERROR_NONE) {
1424                                 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
1425                                 __vc_db_rollback_transaction();
1426                                 return ret;
1427                         }
1428                 } else {
1429                         SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
1430                 }
1431                 iter = g_slist_next(iter);
1432         }
1433
1434         __vc_db_commit_transaction();
1435
1436         return VC_DB_ERROR_NONE;
1437 }
1438
1439 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
1440 {
1441         __vc_db_begin_transaction();
1442
1443         int ret = __vc_db_get_commands(pid, type, cmd_list);
1444         if (ret != VC_DB_ERROR_NONE) {
1445                 __vc_db_rollback_transaction();
1446                 return ret;
1447         }
1448
1449         __vc_db_commit_transaction();
1450         return VC_DB_ERROR_NONE;
1451 }
1452
1453 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
1454 {
1455         if (NULL == result_text) {
1456                 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
1457                 return VC_DB_ERROR_INVALID_PARAMETER;
1458         }
1459
1460         int ret = vc_db_delete_table("result");
1461         if (0 != ret)
1462                 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1463
1464         if (NULL == vc_cmd_list) {
1465                 __vc_db_begin_transaction();
1466                 int ret = __vc_db_insert_result(result_text, event, msg, exclusive, NULL);
1467                 if (ret != VC_DB_ERROR_NONE) {
1468                         __vc_db_rollback_transaction();
1469                         return ret;
1470                 }
1471                 __vc_db_commit_transaction();
1472                 return VC_DB_ERROR_NONE;
1473         }
1474
1475         /* Make client list node */
1476         vc_cmd_h vc_command = NULL;
1477         vc_cmd_list_first(vc_cmd_list);
1478
1479         while (VC_ERROR_ITERATION_END != ret) {
1480                 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
1481                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
1482                         break;
1483                 }
1484
1485                 if (NULL == vc_command) {
1486                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
1487                         break;
1488                 }
1489
1490                 vc_cmd_s* temp_cmd = NULL;
1491                 temp_cmd = (vc_cmd_s*)vc_command;
1492
1493                 __vc_db_begin_transaction();
1494                 ret = __vc_db_insert_result(result_text, event, msg, exclusive, temp_cmd);
1495                 if (ret != VC_DB_ERROR_NONE) {
1496                         __vc_db_rollback_transaction();
1497                         return ret;
1498                 }
1499                 __vc_db_commit_transaction();
1500
1501                 ret = vc_cmd_list_next(vc_cmd_list);
1502         }
1503
1504         return VC_DB_ERROR_NONE;
1505 }
1506
1507 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
1508 {
1509         __vc_db_begin_transaction();
1510
1511         int ret = __vc_db_get_result(result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
1512         if (ret != VC_DB_ERROR_NONE) {
1513                 __vc_db_rollback_transaction();
1514                 return VC_DB_ERROR_OPERATION_FAILED;
1515         }
1516
1517         if (NULL == msg) {
1518                 int count = 0;
1519                 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
1520                 if (ret != VC_DB_ERROR_NONE) {
1521                         LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
1522                 }
1523                 if (0 == count) {
1524                         char* appid = NULL;
1525                         // Get appid by pid using app control
1526                         ret = app_manager_get_app_id(pid, &appid);
1527                         if (APP_MANAGER_ERROR_NONE != ret) {
1528                                 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
1529                         }
1530                         ret = __vc_db_get_result(result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
1531                         if (ret != VC_DB_ERROR_NONE) {
1532                                 __vc_db_rollback_transaction();
1533                                 return ret;
1534                         }
1535                         if (NULL != appid) {
1536                                 free(appid);
1537                                 appid = NULL;
1538                         }
1539                 }
1540         }
1541         __vc_db_commit_transaction();
1542         return VC_DB_ERROR_NONE;
1543 }
1544
1545 int vc_db_get_appid_list(const char* result, GSList** app_list)
1546 {
1547         __vc_db_begin_transaction();
1548
1549         int ret = __vc_db_get_appid(result, app_list);
1550         if (ret != VC_DB_ERROR_NONE) {
1551                 __vc_db_rollback_transaction();
1552                 return ret;
1553         }
1554
1555         __vc_db_commit_transaction();
1556         return VC_DB_ERROR_NONE;
1557 }
1558
1559 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
1560 {
1561         __vc_db_begin_transaction();
1562
1563         int ret = __vc_db_get_result_pid_list(result, pid_list);
1564         if (ret != VC_DB_ERROR_NONE) {
1565                 __vc_db_rollback_transaction();
1566                 return ret;
1567         }
1568
1569         __vc_db_commit_transaction();
1570         return VC_DB_ERROR_NONE;
1571 }
1572
1573 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
1574 {
1575         __vc_db_begin_transaction();
1576
1577         int ret = __vc_db_append_commands(pid, type, vc_cmd_list);
1578         if (ret != VC_DB_ERROR_NONE) {
1579                 __vc_db_rollback_transaction();
1580                 return ret;
1581         }
1582
1583         __vc_db_commit_transaction();
1584         return VC_DB_ERROR_NONE;
1585 }
1586
1587 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
1588 {
1589         __vc_db_begin_transaction();
1590
1591         int ret = 0;
1592         ret = __vc_db_delete_commands(pid, type, appid);
1593         if (ret != VC_DB_ERROR_NONE) {
1594                 __vc_db_rollback_transaction();
1595                 return ret;
1596         }
1597
1598         __vc_db_commit_transaction();
1599         return VC_DB_ERROR_NONE;
1600 }