2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <app_manager.h>
21 #include <tzplatform_config.h>
23 #include "vc_cmd_db.h"
25 #include "vc_command.h"
26 #include "voice_control_command_expand.h"
29 const char* vc_db_tag()
39 * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
40 * | id | pid | type | format | domain | command | parameter | appid | invocation_name | fixed |
41 * +-----+------+------+--------+--------+---------+-----------+-------+-----------------+-------+
43 * CREATE TABLE vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, appid TEXT);
46 * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
47 * | id | result | event | msg | exclusive | pid | type | format | domain | command | parameter | appid | invocation_name | fixed |
48 * +-----+--------+-------+-----+-----------+-----+------+--------+--------+---------+-----------+-------+-----------------+-------+
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,\
55 #define FREE_MEM(X) {if (NULL != X) {free(X); X = NULL; } }
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;
62 char* g_backup_path = NULL;
66 static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
68 sqlite3_stmt* pStmt = NULL;
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;
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;
82 sqlite3_finalize(pStmt);
83 return VC_DB_ERROR_NONE;
86 static int __vc_db_begin_transaction(sqlite3* db_handle)
88 int ret = __vc_db_transaction(db_handle, "BEGIN TRANSACTION");
92 static int __vc_db_rollback_transaction(sqlite3* db_handle)
94 int ret = __vc_db_transaction(db_handle, "ROLLBACK TRANSACTION");
98 static int __vc_db_commit_transaction(sqlite3* db_handle)
100 int ret = __vc_db_transaction(db_handle, "COMMIT TRANSACTION");
104 static int __vc_db_exec_query(sqlite3* db_handle, const char* sql)
106 char* err_msg = NULL;
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;
114 return VC_DB_ERROR_NONE;
117 static int __vc_db_delete_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, const char* appid)
119 sqlite3_stmt* stmt = NULL;
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 = ?;");
127 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
130 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
131 return VC_DB_ERROR_OUT_OF_MEMORY;
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));
140 return VC_DB_ERROR_OPERATION_FAILED;
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);
148 return VC_DB_ERROR_OPERATION_FAILED;
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);
161 return VC_DB_ERROR_OPERATION_FAILED;
163 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
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);
172 return VC_DB_ERROR_OPERATION_FAILED;
174 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
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);
184 return VC_DB_ERROR_OPERATION_FAILED;
188 sqlite3_clear_bindings(stmt);
189 sqlite3_finalize(stmt);
193 return VC_DB_ERROR_NONE;
196 static int __vc_db_insert_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
198 SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
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;
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;
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;
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;
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;
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;
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);
251 cmd->appid = strdup(appid);
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;
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;
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;
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;
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);
290 sqlite3_clear_bindings(stmt);
291 sqlite3_finalize(stmt);
292 return VC_DB_ERROR_NONE;
295 static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList** cmd_list)
297 SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
300 sqlite3_stmt* stmt = NULL;
304 if (VC_COMMAND_TYPE_BACKGROUND == type) {
305 /* For background command */
306 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
308 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
312 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
313 return VC_DB_ERROR_OUT_OF_MEMORY;
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));
320 return VC_DB_ERROR_OPERATION_FAILED;
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);
329 return VC_DB_ERROR_OPERATION_FAILED;
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);
339 return VC_DB_ERROR_OPERATION_FAILED;
343 if (VC_COMMAND_TYPE_BACKGROUND == type)
344 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
346 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
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);
355 return VC_DB_ERROR_NONE;
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);
364 while (SQLITE_ROW == ret) {
367 char* temp_text = NULL;
369 ret = vc_cmd_create(&temp_cmd);
372 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
378 sqlite3_clear_bindings(stmt);
379 sqlite3_finalize(stmt);
382 return VC_DB_ERROR_OUT_OF_MEMORY;
385 temp_text = (char*)sqlite3_column_text(stmt, 7);
386 if (NULL != temp_text)
387 vc_cmd_set_appid(temp_cmd, temp_text);
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);
397 vc_cmd_destroy(temp_cmd);
400 ret = sqlite3_step(stmt);
401 if (SQLITE_DONE == ret)
408 if (NULL != temp_text) {
413 temp = sqlite3_column_int(stmt, 0);
414 vc_cmd_set_id(temp_cmd, temp);
416 temp = sqlite3_column_int(stmt, 1);
417 vc_cmd_set_pid(temp_cmd, temp);
419 temp = sqlite3_column_int(stmt, 2);
420 vc_cmd_set_type(temp_cmd, temp);
422 temp = sqlite3_column_int(stmt, 3);
423 vc_cmd_set_format(temp_cmd, temp);
425 temp = sqlite3_column_int(stmt, 4);
426 vc_cmd_set_domain(temp_cmd, temp);
428 temp_text = (char*)sqlite3_column_text(stmt, 5);
429 if (NULL != temp_text)
430 vc_cmd_set_command(temp_cmd, temp_text);
432 temp_text = (char*)sqlite3_column_text(stmt, 6);
433 if (NULL != temp_text)
434 vc_cmd_set_unfixed_command(temp_cmd, temp_text);
436 temp_text = (char*)sqlite3_column_text(stmt, 8);
437 if (NULL != temp_text)
438 vc_cmd_set_invocation_name(temp_cmd, temp_text);
440 temp_text = (char*)sqlite3_column_text(stmt, 9);
441 if (NULL != temp_text)
442 vc_cmd_set_fixed_command(temp_cmd, temp_text);
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);
448 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp, type);
450 vc_cmd_destroy(temp_cmd);
453 ret = sqlite3_step(stmt);
454 if (SQLITE_DONE == ret)
464 sqlite3_clear_bindings(stmt);
465 sqlite3_finalize(stmt);
469 return VC_DB_ERROR_NONE;
472 static int __vc_db_get_pid(const char* appid, int* pid)
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;
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;
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;
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;
500 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
502 return VC_DB_ERROR_NONE;
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)
507 SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
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;
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;
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;
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;
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;
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);
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;
556 sqlite3_clear_bindings(stmt);
557 sqlite3_finalize(stmt);
558 return VC_DB_ERROR_NONE;
561 if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
563 ret = __vc_db_get_pid(cmd->appid, &pid);
565 SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
567 SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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);
647 sqlite3_clear_bindings(stmt);
648 sqlite3_finalize(stmt);
650 return VC_DB_ERROR_NONE;
653 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
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;
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;
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;
670 if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
671 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
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;
677 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
679 if (NULL == command || NULL == temp_unfixed) {
680 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
681 return VC_DB_ERROR_INVALID_PARAMETER;
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;
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;
694 char* temp = (char*)calloc(256, sizeof(char));
695 if (0 == strncasecmp(command, fixed, strlen(fixed))) {
696 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
697 SLOG(LOG_WARN, vc_db_tag(), "@@@");
698 } else if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
699 strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
700 SLOG(LOG_WARN, vc_db_tag(), "@@@");
703 SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
705 *temp_unfixed = strdup(temp);
709 return VC_DB_ERROR_NONE;
712 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)
715 sqlite3_stmt* stmt = NULL;
718 sql = strdup("SELECT * FROM vc_result;");
719 else if (NULL != appid)
720 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
722 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
725 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
726 return VC_DB_ERROR_OUT_OF_MEMORY;
729 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
730 if (ret != SQLITE_OK) {
731 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
734 return VC_DB_ERROR_OPERATION_FAILED;
737 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
738 if (ret != SQLITE_OK) {
739 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
740 sqlite3_finalize(stmt);
743 return VC_DB_ERROR_OPERATION_FAILED;
745 } else if (-1 != pid) {
746 ret = sqlite3_bind_int(stmt, 1, pid);
747 if (ret != SQLITE_OK) {
748 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
749 sqlite3_finalize(stmt);
752 return VC_DB_ERROR_OPERATION_FAILED;
755 ret = sqlite3_step(stmt);
756 if (ret == SQLITE_DONE) {
757 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
758 sqlite3_clear_bindings(stmt);
759 sqlite3_finalize(stmt);
762 return VC_DB_ERROR_NONE;
766 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
767 else if (NULL != appid)
768 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %s;", appid);
771 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
773 vc_cmd_h temp_cmd = NULL;
774 while (SQLITE_ROW == ret) {
776 char* temp_text = NULL;
777 const char* invocation_name = NULL;
779 temp_text = (char*)sqlite3_column_text(stmt, 1);
780 if (NULL != temp_text)
781 *result_text = strdup(temp_text);
783 temp = sqlite3_column_int(stmt, 2);
787 temp_text = (char*)sqlite3_column_text(stmt, 3);
788 if (NULL != temp_text)
789 *msg = strdup(temp_text);
792 if (0 != vc_cmd_create(&temp_cmd)) {
793 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
794 if (NULL != *result_text) {
799 if (NULL != msg && NULL != *msg) {
805 sqlite3_clear_bindings(stmt);
806 sqlite3_finalize(stmt);
812 temp = sqlite3_column_int(stmt, 0);
813 vc_cmd_set_id(temp_cmd, temp);
815 temp = sqlite3_column_int(stmt, 5);
816 vc_cmd_set_pid(temp_cmd, temp);
818 temp = sqlite3_column_int(stmt, 6);
819 vc_cmd_set_type(temp_cmd, temp);
821 temp = sqlite3_column_int(stmt, 7);
822 vc_cmd_set_format(temp_cmd, temp);
824 temp = sqlite3_column_int(stmt, 8);
825 vc_cmd_set_domain(temp_cmd, temp);
828 invocation_name = (char*)sqlite3_column_text(stmt, 12);
830 temp_text = (char*)sqlite3_column_text(stmt, 9);
831 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
833 if (NULL != temp_text) {
834 char* temp_command = NULL;
835 char* temp_unfixed = NULL;
836 char* temp_fixed = NULL;
838 if (NULL != invocation_name)
839 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
841 if (NULL == temp_command) {
842 temp_command = strdup(temp_text);
844 // remove invocation name from result_text
846 *result_text = strdup(temp_command);
850 temp_fixed = (char*)sqlite3_column_text(stmt, 13);
851 if (NULL != temp_fixed)
852 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
855 temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
857 if (NULL != temp_unfixed) {
858 char merge_result[256] = {0, };
859 vc_cmd_set_command(temp_cmd, temp_command);
860 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
861 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
862 if (NULL != *result_text)
864 *result_text = strdup(merge_result);
865 } else if (NULL != temp_fixed) {
866 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
867 vc_cmd_set_command(temp_cmd, temp_fixed);
868 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
869 if (NULL != temp_unfixed) {
874 vc_cmd_set_command(temp_cmd, temp_command);
880 temp_text = (char*)sqlite3_column_text(stmt, 11);
881 if (NULL != temp_text)
882 vc_cmd_set_appid(temp_cmd, temp_text);
884 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
885 SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
886 vc_cmd_destroy(temp_cmd);
887 vc_cmd_list_destroy(vc_cmd_list, true);
888 if (NULL != *result_text) {
893 if (NULL != msg && NULL != *msg) {
899 sqlite3_clear_bindings(stmt);
900 sqlite3_finalize(stmt);
903 return VC_DB_ERROR_OPERATION_FAILED;
906 ret = sqlite3_step(stmt);
907 if (SQLITE_DONE == ret)
912 sqlite3_clear_bindings(stmt);
913 sqlite3_finalize(stmt);
917 return VC_DB_ERROR_NONE;
920 void __vc_db_demandable_client_free(void* data)
922 vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data;
925 if (NULL != d_app->appid) {
934 static int __vc_db_get_appid(sqlite3* db_handle, const char* result, GSList** app_list)
936 GSList* temp_app_list = NULL;
939 sqlite3_stmt* stmt = NULL;
940 const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
942 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
943 if (ret != SQLITE_OK) {
944 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
945 return VC_DB_ERROR_OPERATION_FAILED;
947 ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
948 if (ret != SQLITE_OK) {
949 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
950 sqlite3_finalize(stmt);
951 return VC_DB_ERROR_OPERATION_FAILED;
953 ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
954 if (ret != SQLITE_OK) {
955 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
956 sqlite3_clear_bindings(stmt);
957 sqlite3_finalize(stmt);
958 return VC_DB_ERROR_OPERATION_FAILED;
960 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
961 ret = sqlite3_step(stmt);
962 if (ret == SQLITE_DONE) {
963 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
964 sqlite3_clear_bindings(stmt);
965 sqlite3_finalize(stmt);
966 return VC_DB_ERROR_NONE;
969 while (SQLITE_ROW == ret) {
970 char* temp_text = NULL;
971 vc_deactivated_app_s* temp_app = NULL;
972 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
973 if (NULL == temp_app) {
974 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
976 if (NULL != temp_app_list) {
977 g_slist_free_full(temp_app_list, __vc_db_demandable_client_free);
978 temp_app_list = NULL;
982 sqlite3_clear_bindings(stmt);
983 sqlite3_finalize(stmt);
984 return VC_DB_ERROR_OUT_OF_MEMORY;
987 temp_text = (char*)sqlite3_column_text(stmt, 11);
988 if (NULL != temp_text)
989 temp_app->appid = strdup(temp_text);
991 temp_app_list = g_slist_append(temp_app_list, temp_app);
993 ret = sqlite3_step(stmt);
994 if (SQLITE_DONE == ret)
998 *app_list = temp_app_list;
1000 sqlite3_reset(stmt);
1001 sqlite3_clear_bindings(stmt);
1002 sqlite3_finalize(stmt);
1003 return VC_DB_ERROR_NONE;
1006 int __vc_db_get_result_pid_list(sqlite3* db_handle, const char* result, GSList** pid_list)
1008 GSList* temp_pid_list = NULL;
1011 sqlite3_stmt* stmt = NULL;
1012 const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
1015 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1016 if (ret != SQLITE_OK) {
1017 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1018 return VC_DB_ERROR_OPERATION_FAILED;
1020 ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
1021 if (ret != SQLITE_OK) {
1022 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1023 sqlite3_finalize(stmt);
1024 return VC_DB_ERROR_OPERATION_FAILED;
1026 ret = sqlite3_step(stmt);
1027 if (ret == SQLITE_DONE) {
1028 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1029 sqlite3_clear_bindings(stmt);
1030 sqlite3_finalize(stmt);
1031 return VC_DB_ERROR_NONE;
1034 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
1036 while (SQLITE_ROW == ret) {
1038 vc_cmd_s* temp_cmd = NULL;
1039 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
1040 if (NULL == temp_cmd) {
1041 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
1043 if (NULL != temp_pid_list) {
1044 g_slist_free_full(temp_pid_list, free);
1045 temp_pid_list = NULL;
1048 sqlite3_reset(stmt);
1049 sqlite3_clear_bindings(stmt);
1050 sqlite3_finalize(stmt);
1051 return VC_DB_ERROR_OUT_OF_MEMORY;
1054 temp = sqlite3_column_int(stmt, 0);
1055 temp_cmd->pid = temp;
1057 temp = sqlite3_column_int(stmt, 1);
1058 temp_cmd->type = temp;
1060 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
1062 ret = sqlite3_step(stmt);
1063 if (SQLITE_DONE == ret)
1067 *pid_list = temp_pid_list;
1069 sqlite3_reset(stmt);
1070 sqlite3_clear_bindings(stmt);
1071 sqlite3_finalize(stmt);
1072 return VC_DB_ERROR_NONE;
1075 static int __vc_db_append_commands(sqlite3* db_handle, int pid, int type, vc_cmd_list_h vc_cmd_list)
1077 SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
1080 sqlite3_stmt* stmt = NULL;
1081 const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
1083 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1084 if (ret != SQLITE_OK) {
1085 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1086 return VC_DB_ERROR_OPERATION_FAILED;
1088 ret = sqlite3_bind_int(stmt, 1, pid);
1089 if (ret != SQLITE_OK) {
1090 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1091 sqlite3_finalize(stmt);
1092 return VC_DB_ERROR_OPERATION_FAILED;
1094 ret = sqlite3_bind_int(stmt, 2, type);
1095 if (ret != SQLITE_OK) {
1096 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1097 sqlite3_clear_bindings(stmt);
1098 sqlite3_finalize(stmt);
1099 return VC_DB_ERROR_OPERATION_FAILED;
1101 ret = sqlite3_step(stmt);
1102 if (ret == SQLITE_DONE) {
1103 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1104 sqlite3_clear_bindings(stmt);
1105 sqlite3_finalize(stmt);
1106 return VC_DB_ERROR_NONE;
1109 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
1111 vc_cmd_h temp_cmd = NULL;
1113 while (SQLITE_ROW == ret) {
1115 char* temp_text = NULL;
1117 if (0 != vc_cmd_create(&temp_cmd)) {
1118 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
1119 sqlite3_reset(stmt);
1120 sqlite3_clear_bindings(stmt);
1121 sqlite3_finalize(stmt);
1124 temp = sqlite3_column_int(stmt, 0);
1125 vc_cmd_set_id(temp_cmd, temp);
1127 temp = sqlite3_column_int(stmt, 1);
1128 vc_cmd_set_pid(temp_cmd, temp);
1130 temp = sqlite3_column_int(stmt, 2);
1131 vc_cmd_set_type(temp_cmd, temp);
1133 temp = sqlite3_column_int(stmt, 3);
1134 vc_cmd_set_format(temp_cmd, temp);
1136 temp = sqlite3_column_int(stmt, 4);
1137 vc_cmd_set_domain(temp_cmd, temp);
1139 temp_text = (char*)sqlite3_column_text(stmt, 5);
1140 if (NULL != temp_text)
1141 vc_cmd_set_command(temp_cmd, temp_text);
1143 temp_text = (char*)sqlite3_column_text(stmt, 6);
1144 if (NULL != temp_text)
1145 vc_cmd_set_unfixed_command(temp_cmd, temp_text);
1147 temp_text = (char*)sqlite3_column_text(stmt, 7);
1148 if (NULL != temp_text)
1149 vc_cmd_set_appid(temp_cmd, temp_text);
1151 temp_text = (char*)sqlite3_column_text(stmt, 8);
1152 if (NULL != temp_text)
1153 vc_cmd_set_invocation_name(temp_cmd, temp_text);
1155 temp_text = (char*)sqlite3_column_text(stmt, 9);
1156 if (NULL != temp_text)
1157 vc_cmd_set_fixed_command(temp_cmd, temp_text);
1159 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1160 SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1161 vc_cmd_destroy(temp_cmd);
1162 vc_cmd_list_destroy(vc_cmd_list, true);
1163 sqlite3_reset(stmt);
1164 sqlite3_clear_bindings(stmt);
1165 sqlite3_finalize(stmt);
1166 return VC_DB_ERROR_OPERATION_FAILED;
1169 ret = sqlite3_step(stmt);
1170 if (SQLITE_DONE == ret)
1174 sqlite3_reset(stmt);
1175 sqlite3_clear_bindings(stmt);
1176 sqlite3_finalize(stmt);
1177 return VC_DB_ERROR_NONE;
1180 static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
1182 if (NULL == src_cmd) {
1183 SLOG(LOG_WARN, vc_db_tag(), "[Client Data] Input command is NULL");
1187 vc_cmd_s* temp_cmd = NULL;
1188 temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
1189 if (NULL == temp_cmd) {
1190 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1194 temp_cmd->id = src_cmd->id;
1195 temp_cmd->pid = src_cmd->pid;
1196 temp_cmd->index = src_cmd->index;
1197 temp_cmd->type = src_cmd->type;
1198 temp_cmd->format = src_cmd->format;
1199 temp_cmd->domain = src_cmd->domain;
1201 if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
1202 else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
1203 else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1204 else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1205 else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
1206 else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
1208 if (NULL != src_cmd->command) {
1209 temp_cmd->command = strdup(src_cmd->command);
1212 if (NULL != src_cmd->parameter) {
1213 temp_cmd->parameter = strdup(src_cmd->parameter);
1216 if (NULL != src_cmd->appid) {
1217 temp_cmd->appid = strdup(src_cmd->appid);
1220 if (NULL != src_cmd->invocation_name) {
1221 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
1224 if (NULL != src_cmd->fixed) {
1225 temp_cmd->fixed = strdup(src_cmd->fixed);
1228 temp_cmd->key = src_cmd->key;
1229 temp_cmd->modifier = src_cmd->modifier;
1234 int __vc_db_create_table(sqlite3* db_handle)
1236 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, \
1237 command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1238 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,\
1239 pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1241 int ret = __vc_db_exec_query(db_handle, vc_info_sql);
1242 if (ret != VC_DB_ERROR_NONE) {
1243 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1244 return VC_DB_ERROR_OPERATION_FAILED;
1246 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_info_sql);
1248 ret = __vc_db_exec_query(db_handle, vc_result_sql);
1249 if (ret != VC_DB_ERROR_NONE) {
1250 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1253 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_result_sql);
1255 return VC_DB_ERROR_NONE;
1258 int __vc_db_open_db(char** path, sqlite3** db_handle)
1261 int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1262 if (ret != SQLITE_OK) {
1263 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", *path, ret, sqlite3_errmsg(*db_handle));
1265 db_util_close(*db_handle);
1271 return VC_DB_ERROR_OPERATION_FAILED;
1274 if (lstat(*path, &stat) < 0) {
1276 SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
1278 db_util_close(*db_handle);
1283 return VC_DB_ERROR_OPERATION_FAILED;
1286 if (!S_ISREG(stat.st_mode)) {
1287 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1289 db_util_close(*db_handle);
1294 return VC_DB_ERROR_OPERATION_FAILED;
1297 if (!stat.st_size) {
1298 __vc_db_begin_transaction(*db_handle);
1300 int ret = __vc_db_create_table(*db_handle);
1301 if (ret != VC_DB_ERROR_NONE) {
1302 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1303 __vc_db_rollback_transaction(*db_handle);
1304 return VC_DB_ERROR_OPERATION_FAILED;
1307 __vc_db_commit_transaction(*db_handle);
1311 char* err_msg = NULL;
1312 static const const char* sql = "PRAGMA journal_mode = WAL";
1313 int ret = sqlite3_exec(*db_handle, sql, NULL, NULL, &err_msg);
1314 if (ret != SQLITE_OK) {
1315 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1318 return VC_DB_ERROR_NONE;
1321 static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
1323 SLOG(LOG_INFO, vc_db_tag(), "integrity check cb is called");
1327 int vc_db_initialize(void)
1329 SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
1331 if (0 < g_ref_cnt) {
1333 return VC_DB_ERROR_NONE;
1336 /* For voice control DB */
1337 g_path = (char*)calloc(256, sizeof(char));
1338 if (NULL == g_path) {
1339 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1340 return VC_DB_ERROR_OUT_OF_MEMORY;
1342 /* This should be changed to general DB space - TZ_USER_DB */
1343 snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1346 g_backup_path = (char*)calloc(256, sizeof(char));
1347 if (NULL == g_backup_path) {
1348 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1349 return VC_DB_ERROR_OUT_OF_MEMORY;
1351 snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
1353 if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
1355 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1356 if (0 != remove(g_path)) {
1357 SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1359 if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
1360 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1363 if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
1364 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
1370 if (0 != vc_db_restore_command()) {
1371 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1375 if (SQLITE_CORRUPT == sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL)) {
1377 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1378 if (0 != remove(g_path)) {
1379 SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1381 if (0 != __vc_db_open_db(&g_path, &g_db_handle)) {
1382 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1385 if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
1386 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
1392 if (0 != vc_db_restore_command()) {
1393 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1397 if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
1398 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
1399 if (0 != remove(g_backup_path)) {
1400 SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
1402 if (0 != __vc_db_open_db(&g_backup_path, &g_db_backup_handle)) {
1403 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB");
1405 if (0 != vc_db_backup_command()) {
1406 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to backup command");
1411 return VC_DB_ERROR_NONE;
1414 int vc_db_finalize(void)
1416 if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
1417 if (0 != --g_ref_cnt)
1418 return VC_DB_ERROR_NONE;
1420 if (NULL != g_path) {
1425 if (NULL != g_backup_path) {
1426 free(g_backup_path);
1427 g_backup_path = NULL;
1432 db_util_close(g_db_handle);
1435 if (!g_db_backup_handle)
1437 db_util_close(g_db_backup_handle);
1438 g_db_backup_handle = NULL;
1440 return VC_DB_ERROR_NONE;
1443 int vc_db_create_table()
1445 __vc_db_begin_transaction(g_db_handle);
1447 int ret = __vc_db_create_table(g_db_handle);
1448 if (ret != VC_DB_ERROR_NONE) {
1449 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1450 __vc_db_rollback_transaction(g_db_handle);
1451 return VC_DB_ERROR_OPERATION_FAILED;
1454 __vc_db_commit_transaction(g_db_handle);
1455 return VC_DB_ERROR_NONE;
1458 int __vc_db_delete_table(sqlite3* db_handle, const char* table)
1461 if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1462 sql = strdup("DELETE FROM vc_result;");
1463 } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1464 sql = strdup("DELETE FROM vc_info;");
1466 return VC_DB_ERROR_INVALID_PARAMETER;
1470 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1471 return VC_DB_ERROR_OUT_OF_MEMORY;
1474 int ret = __vc_db_exec_query(db_handle, sql);
1475 if (ret != VC_DB_ERROR_NONE) {
1476 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1485 if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1486 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1487 } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1488 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1492 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1493 return VC_DB_ERROR_OUT_OF_MEMORY;
1496 ret = __vc_db_exec_query(db_handle, sql);
1497 if (ret != VC_DB_ERROR_NONE) {
1498 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1504 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1508 return VC_DB_ERROR_NONE;
1511 int vc_db_delete_table(const char* table)
1513 __vc_db_begin_transaction(g_db_handle);
1515 int ret = __vc_db_delete_table(g_db_handle, table);
1517 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1518 __vc_db_rollback_transaction(g_db_handle);
1522 __vc_db_commit_transaction(g_db_handle);
1524 return VC_DB_ERROR_NONE;
1527 int vc_db_begin_transaction(void)
1529 int ret = __vc_db_begin_transaction(g_db_handle);
1533 int vc_db_rollback_transaction(void)
1535 int ret = __vc_db_rollback_transaction(g_db_handle);
1539 int vc_db_commit_transaction(void)
1541 int ret = __vc_db_commit_transaction(g_db_handle);
1545 static void __vc_db_remove_space(char** string)
1547 if (NULL == string || NULL == *string)
1550 char* temp = *string;
1552 //remove previous space
1554 memmove(temp, temp + 1, strlen(temp));
1556 // remove next space
1557 if (' ' == temp[strlen(temp) - 1])
1558 temp[strlen(temp) - 1] = '\0';
1561 static bool __vc_db_is_valid_vfixed_string(char* string)
1563 char* temp = strchr(string, '}');
1567 temp = strchr(string, '{');
1571 temp = strchr(string, '|');
1577 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1579 if (NULL == cmd || NULL == cmd->command) {
1580 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
1581 return VC_DB_ERROR_INVALID_PARAMETER;
1584 GSList* temp_list = NULL;
1586 char* src_cmd = strdup(cmd->command);
1587 char* dst_cmd = NULL;
1588 char merge_cmd[256] = {0, };
1590 if (NULL == src_cmd) {
1591 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1592 return VC_DB_ERROR_OUT_OF_MEMORY;
1595 if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
1596 // check string validation
1597 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1598 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1601 return VC_DB_ERROR_INVALID_PARAMETER;
1604 // remove close brace, '}'
1605 char* temp_close = strchr(src_cmd, '}');
1606 if (NULL != temp_close) {
1607 temp_close[0] = '\0';
1610 // extract fixed command and remove space in front of '{'
1611 char *tok_ptr = NULL;
1612 temp = strtok_r(src_cmd, "{", &tok_ptr);
1613 __vc_db_remove_space(&temp);
1614 *fixed_cmd = strdup(temp);
1616 // merge command with fixed and vfixed
1617 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1618 __vc_db_remove_space(&temp);
1620 snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
1621 dst_cmd = strdup(merge_cmd);
1622 temp_list = g_slist_append(temp_list, dst_cmd);
1623 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1625 } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
1626 // check string validation
1627 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1628 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1631 return VC_DB_ERROR_INVALID_PARAMETER;
1634 // extract fixed command
1635 char* temp_fixed = strchr(src_cmd, '}') + 1;
1636 __vc_db_remove_space(&temp_fixed);
1637 *fixed_cmd = strdup(temp_fixed);
1639 // remove close brace, '}'
1640 char *tok_ptr = NULL;
1641 temp = strtok_r(src_cmd, "}", &tok_ptr);
1643 // remove open brace, '{'
1644 temp = strchr(src_cmd, '{') + 1;
1647 temp = strtok_r(temp, "|", &tok_ptr);
1648 __vc_db_remove_space(&temp);
1650 // merge command with fixed and vfixed
1651 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1652 dst_cmd = strdup(merge_cmd);
1653 temp_list = g_slist_append(temp_list, dst_cmd);
1654 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1656 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1657 __vc_db_remove_space(&temp);
1659 // merge command with fixed and vfixed
1660 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1661 dst_cmd = strdup(merge_cmd);
1662 temp_list = g_slist_append(temp_list, dst_cmd);
1663 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1665 } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
1666 dst_cmd = strdup(src_cmd);
1667 temp_list = g_slist_append(temp_list, dst_cmd);
1668 *fixed_cmd = strdup(src_cmd);
1671 dst_cmd = strdup(src_cmd);
1672 temp_list = g_slist_append(temp_list, dst_cmd);
1675 *cmd_list = temp_list;
1679 return VC_DB_ERROR_NONE;
1682 static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
1684 GSList* cmd_list = NULL;
1685 char* fixed_cmd = NULL;
1686 vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
1688 int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
1690 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
1692 if (NULL != tmp_cmd) {
1693 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1698 if (0 != g_slist_length(cmd_list)) {
1699 GSList *iter = NULL;
1700 char* temp_command = NULL;
1701 iter = g_slist_nth(cmd_list, 0);
1703 while (NULL != iter) {
1704 temp_command = iter->data;
1706 if (NULL != temp_command) {
1707 if (NULL != tmp_cmd->command) {
1708 free(tmp_cmd->command);
1709 tmp_cmd->command = NULL;
1711 tmp_cmd->command = strdup(temp_command);
1712 if (NULL != fixed_cmd)
1713 tmp_cmd->fixed = strdup(fixed_cmd);
1715 tmp_cmd->fixed = NULL;
1717 ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
1718 if (ret != VC_DB_ERROR_NONE) {
1719 if (NULL != fixed_cmd) {
1723 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1727 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name && false == skip_invocation) {
1728 char temp[256] = {0, };
1729 snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
1730 if (NULL != tmp_cmd->command)
1731 free(tmp_cmd->command);
1733 tmp_cmd->command = strdup(temp);
1735 ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
1736 if (ret != VC_DB_ERROR_NONE) {
1737 if (NULL != fixed_cmd) {
1741 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1745 cmd_list = g_slist_remove(cmd_list, temp_command);
1747 temp_command = NULL;
1749 iter = g_slist_nth(cmd_list, 0);
1752 if (VC_DB_ERROR_NONE != ret) {
1753 while (NULL != iter) {
1754 temp_command = iter->data;
1756 if (NULL != temp_command) {
1757 cmd_list = g_slist_remove(cmd_list, temp_command);
1759 temp_command = NULL;
1762 iter = g_slist_nth(cmd_list, 0);
1765 g_slist_free(cmd_list);
1766 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
1773 if (NULL != fixed_cmd) {
1777 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1778 return VC_DB_ERROR_NONE;
1781 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
1783 int ret = __vc_db_insert_command(g_db_handle, pid, type, cmd, skip_invocation);
1785 SLOG(LOG_DEBUG, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
1791 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)
1793 GSList *iter = NULL;
1797 int count = g_slist_length(cmd_list);
1798 iter = g_slist_nth(cmd_list, 0);
1800 SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
1802 for (i = 0; i < count; i++) {
1806 temp_cmd = iter->data;
1808 if (NULL == temp_cmd) {
1809 SLOG(LOG_ERROR, vc_db_tag(), "comamnd is NULL");
1813 if (type == temp_cmd->type) {
1814 if (NULL != invocation_name)
1815 temp_cmd->invocation_name = strdup(invocation_name);
1817 int ret = __vc_db_insert_command(db_handle, pid, type, temp_cmd, skip_invocation);
1818 if (ret != VC_DB_ERROR_NONE) {
1819 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
1823 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
1825 iter = g_slist_next(iter);
1828 return VC_DB_ERROR_NONE;
1831 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
1833 __vc_db_begin_transaction(g_db_handle);
1835 int ret = __vc_db_insert_commands_list(g_db_handle, pid, type, cmd_list, invocation_name, false);
1836 if (ret != VC_DB_ERROR_NONE) {
1837 __vc_db_rollback_transaction(g_db_handle);
1841 __vc_db_commit_transaction(g_db_handle);
1843 return VC_DB_ERROR_NONE;
1846 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
1848 __vc_db_begin_transaction(g_db_handle);
1850 int ret = __vc_db_get_commands(g_db_handle, pid, type, cmd_list);
1851 if (ret != VC_DB_ERROR_NONE) {
1852 __vc_db_rollback_transaction(g_db_handle);
1856 __vc_db_commit_transaction(g_db_handle);
1857 return VC_DB_ERROR_NONE;
1860 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
1862 if (NULL == result_text) {
1863 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
1864 return VC_DB_ERROR_INVALID_PARAMETER;
1867 int ret = vc_db_delete_table(VC_RESULT_TABLE);
1869 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1871 if (NULL == vc_cmd_list) {
1872 __vc_db_begin_transaction(g_db_handle);
1873 int ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, NULL);
1874 if (ret != VC_DB_ERROR_NONE) {
1875 __vc_db_rollback_transaction(g_db_handle);
1878 __vc_db_commit_transaction(g_db_handle);
1879 return VC_DB_ERROR_NONE;
1882 /* Make client list node */
1883 vc_cmd_h vc_command = NULL;
1884 vc_cmd_list_first(vc_cmd_list);
1886 while (VC_ERROR_ITERATION_END != ret) {
1887 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
1888 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
1892 if (NULL == vc_command) {
1893 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
1897 vc_cmd_s* temp_cmd = NULL;
1898 temp_cmd = (vc_cmd_s*)vc_command;
1900 __vc_db_begin_transaction(g_db_handle);
1901 ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, temp_cmd);
1902 if (ret != VC_DB_ERROR_NONE) {
1903 __vc_db_rollback_transaction(g_db_handle);
1906 __vc_db_commit_transaction(g_db_handle);
1908 ret = vc_cmd_list_next(vc_cmd_list);
1911 return VC_DB_ERROR_NONE;
1914 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
1916 __vc_db_begin_transaction(g_db_handle);
1918 int ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
1919 if (ret != VC_DB_ERROR_NONE) {
1920 __vc_db_rollback_transaction(g_db_handle);
1921 return VC_DB_ERROR_OPERATION_FAILED;
1926 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
1927 if (ret != VC_DB_ERROR_NONE) {
1928 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
1932 // Get appid by pid using app control
1933 ret = app_manager_get_app_id(pid, &appid);
1934 if (APP_MANAGER_ERROR_NONE != ret) {
1935 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
1937 ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
1938 if (ret != VC_DB_ERROR_NONE) {
1939 __vc_db_rollback_transaction(g_db_handle);
1942 if (NULL != appid) {
1948 __vc_db_commit_transaction(g_db_handle);
1949 return VC_DB_ERROR_NONE;
1952 int vc_db_get_appid_list(const char* result, GSList** app_list)
1954 __vc_db_begin_transaction(g_db_handle);
1956 int ret = __vc_db_get_appid(g_db_handle, result, app_list);
1957 if (ret != VC_DB_ERROR_NONE) {
1958 __vc_db_rollback_transaction(g_db_handle);
1962 __vc_db_commit_transaction(g_db_handle);
1963 return VC_DB_ERROR_NONE;
1966 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
1968 __vc_db_begin_transaction(g_db_handle);
1970 int ret = __vc_db_get_result_pid_list(g_db_handle, result, pid_list);
1971 if (ret != VC_DB_ERROR_NONE) {
1972 __vc_db_rollback_transaction(g_db_handle);
1976 __vc_db_commit_transaction(g_db_handle);
1977 return VC_DB_ERROR_NONE;
1980 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
1982 __vc_db_begin_transaction(g_db_handle);
1984 int ret = __vc_db_append_commands(g_db_handle, pid, type, vc_cmd_list);
1985 if (ret != VC_DB_ERROR_NONE) {
1986 __vc_db_rollback_transaction(g_db_handle);
1990 __vc_db_commit_transaction(g_db_handle);
1991 return VC_DB_ERROR_NONE;
1994 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
1996 __vc_db_begin_transaction(g_db_handle);
1999 ret = __vc_db_delete_commands(g_db_handle, pid, type, appid);
2000 if (ret != VC_DB_ERROR_NONE) {
2001 __vc_db_rollback_transaction(g_db_handle);
2005 __vc_db_commit_transaction(g_db_handle);
2006 return VC_DB_ERROR_NONE;
2009 int vc_db_backup_command()
2011 GSList* list = NULL;
2013 __vc_db_begin_transaction(g_db_backup_handle);
2015 int ret = __vc_db_delete_table(g_db_backup_handle, VC_INFO_TABLE);
2017 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2018 __vc_db_rollback_transaction(g_db_backup_handle);
2021 __vc_db_commit_transaction(g_db_backup_handle);
2023 ret = vc_db_get_commands(-1, VC_COMMAND_TYPE_BACKGROUND, &list);
2024 if (ret != VC_DB_ERROR_NONE) {
2025 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands");
2029 __vc_db_begin_transaction(g_db_backup_handle);
2031 ret = __vc_db_insert_commands_list(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2032 if (ret != VC_DB_ERROR_NONE) {
2033 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list to backup db");
2034 __vc_db_rollback_transaction(g_db_backup_handle);
2038 __vc_db_commit_transaction(g_db_backup_handle);
2040 SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
2041 return VC_DB_ERROR_NONE;
2044 int vc_db_restore_command()
2046 GSList* list = NULL;
2048 __vc_db_begin_transaction(g_db_backup_handle);
2050 int ret = __vc_db_get_commands(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, &list);
2051 if (ret != VC_DB_ERROR_NONE) {
2052 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands from backup db");
2053 __vc_db_rollback_transaction(g_db_backup_handle);
2056 __vc_db_commit_transaction(g_db_backup_handle);
2058 ret = vc_db_insert_commands_list(-1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2059 if (ret != VC_DB_ERROR_NONE) {
2060 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list");
2064 SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
2065 return VC_DB_ERROR_NONE;