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;
61 static char* g_path = NULL;
62 static char* g_backup_path = NULL;
63 static int g_ref_cnt = 0;
64 static bool is_db_corrupted = false;
67 int g_backup_db_cnt = 0;
69 static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist);
70 static int __vc_db_begin_transaction(sqlite3* db_handle);
71 static int __vc_db_create_table(sqlite3* db_handle, const char* table);
72 static int __vc_db_rollback_transaction(sqlite3* db_handle);
73 static int __vc_db_commit_transaction(sqlite3* db_handle);
76 int __vc_db_reset_handle(void)
78 if (!g_db_handle || !g_db_backup_handle) {
85 static int __vc_db_transaction(sqlite3* db_handle, const char* transaction)
87 sqlite3_stmt* pStmt = NULL;
91 ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
92 if (ret != SQLITE_OK) {
93 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
94 return VC_DB_ERROR_OPERATION_FAILED;
97 if (sqlite3_step(pStmt) != SQLITE_DONE) {
98 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
99 sqlite3_finalize(pStmt);
100 return VC_DB_ERROR_OPERATION_FAILED;
103 sqlite3_finalize(pStmt);
104 return VC_DB_ERROR_NONE;
107 static int __vc_db_begin_transaction(sqlite3* db_handle)
109 int ret = __vc_db_transaction(db_handle, "BEGIN IMMEDIATE TRANSACTION");
113 static int __vc_db_rollback_transaction(sqlite3* db_handle)
115 int ret = __vc_db_transaction(db_handle, "ROLLBACK TRANSACTION");
119 static int __vc_db_commit_transaction(sqlite3* db_handle)
121 int ret = __vc_db_transaction(db_handle, "COMMIT TRANSACTION");
125 static int __vc_db_exec_query(sqlite3* db_handle, const char* sql)
127 char* err_msg = NULL;
129 int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
130 if (ret != SQLITE_OK) {
131 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
132 sqlite3_free(err_msg);
133 return VC_DB_ERROR_OPERATION_FAILED;
135 return VC_DB_ERROR_NONE;
138 static int __vc_db_check_table(sqlite3* db_handle, const char* table, bool *is_exist)
142 if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
143 sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_result';");
144 } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
145 sql = strdup("SELECT COUNT(*) FROM sqlite_master WHERE name='vc_info';");
147 return VC_DB_ERROR_INVALID_PARAMETER;
151 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
152 return VC_DB_ERROR_OUT_OF_MEMORY;
155 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
157 char* err_msg = NULL;
158 char** results = NULL;
159 int ret = sqlite3_get_table(db_handle, sql, &results, NULL, NULL, &err_msg);
160 if (ret != SQLITE_OK) {
161 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_get_table return fail, ret(%d), err(%s)", ret, err_msg);
162 sqlite3_free(err_msg);
163 return VC_DB_ERROR_OPERATION_FAILED;
166 if (0 != atoi(results[1])) {
168 SLOG(LOG_INFO, vc_db_tag(), "[INFO] table exists (%d)", atoi(results[1]));
170 SLOG(LOG_INFO, vc_db_tag(), "[INFO] table doesn't exist (%d)", atoi(results[1]));
173 sqlite3_free_table(results);
176 return VC_DB_ERROR_NONE;
179 static int __vc_db_delete_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, const char* appid)
181 sqlite3_stmt* stmt = NULL;
184 if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid)
185 sql = strdup("DELETE FROM vc_info WHERE type = ?;");
186 else if (NULL != appid)
187 sql = strdup("DELETE FROM vc_info WHERE type = ? AND appid = ?;");
189 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
192 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
193 return VC_DB_ERROR_OUT_OF_MEMORY;
197 /* check whether there is a table or not */
198 bool is_exist = false;
199 ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
200 if (VC_DB_ERROR_NONE != ret || false == is_exist) {
201 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
202 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
203 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
206 return VC_DB_ERROR_OPERATION_FAILED;
209 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
212 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
213 if (ret != SQLITE_OK) {
214 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
217 return VC_DB_ERROR_OPERATION_FAILED;
219 ret = sqlite3_bind_int(stmt, 1, (int)type);
220 if (ret != SQLITE_OK) {
221 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
222 sqlite3_finalize(stmt);
225 return VC_DB_ERROR_OPERATION_FAILED;
228 if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid) {
229 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d;", type);
230 } else if (NULL != appid) {
231 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
232 if (ret != SQLITE_OK) {
233 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
234 sqlite3_clear_bindings(stmt);
235 sqlite3_finalize(stmt);
238 return VC_DB_ERROR_OPERATION_FAILED;
240 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
242 ret = sqlite3_bind_int(stmt, 2, pid);
243 if (ret != SQLITE_OK) {
244 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
245 sqlite3_clear_bindings(stmt);
246 sqlite3_finalize(stmt);
249 return VC_DB_ERROR_OPERATION_FAILED;
251 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
254 ret = sqlite3_step(stmt);
255 if (ret != SQLITE_DONE) {
256 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned, sql(%s), ret(%d), err(%s)", sql, ret, sqlite3_errmsg(db_handle));
257 sqlite3_clear_bindings(stmt);
258 sqlite3_finalize(stmt);
261 return VC_DB_ERROR_OPERATION_FAILED;
265 sqlite3_clear_bindings(stmt);
266 sqlite3_finalize(stmt);
270 return VC_DB_ERROR_NONE;
273 static int __vc_db_insert_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
275 SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
277 sqlite3_stmt* stmt = NULL;
278 const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
280 /* check whether there is a table or not */
281 bool is_exist = false;
282 ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
283 if (VC_DB_ERROR_NONE != ret || false == is_exist) {
284 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
285 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
286 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
287 return VC_DB_ERROR_OPERATION_FAILED;
290 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
294 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
295 if (ret != SQLITE_OK) {
296 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
297 return VC_DB_ERROR_OPERATION_FAILED;
299 ret = sqlite3_bind_int(stmt, 2, pid);
300 if (ret != SQLITE_OK) {
301 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
302 sqlite3_finalize(stmt);
303 return VC_DB_ERROR_OPERATION_FAILED;
305 ret = sqlite3_bind_int(stmt, 3, cmd->type);
306 if (ret != SQLITE_OK) {
307 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
308 sqlite3_clear_bindings(stmt);
309 sqlite3_finalize(stmt);
310 return VC_DB_ERROR_OPERATION_FAILED;
312 ret = sqlite3_bind_int(stmt, 4, cmd->format);
313 if (ret != SQLITE_OK) {
314 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
315 sqlite3_clear_bindings(stmt);
316 sqlite3_finalize(stmt);
317 return VC_DB_ERROR_OPERATION_FAILED;
319 ret = sqlite3_bind_int(stmt, 5, cmd->domain);
320 if (ret != SQLITE_OK) {
321 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
322 sqlite3_clear_bindings(stmt);
323 sqlite3_finalize(stmt);
324 return VC_DB_ERROR_OPERATION_FAILED;
326 ret = sqlite3_bind_text(stmt, 6, cmd->command, -1, SQLITE_TRANSIENT);
327 if (ret != SQLITE_OK) {
328 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
329 sqlite3_clear_bindings(stmt);
330 sqlite3_finalize(stmt);
331 return VC_DB_ERROR_OPERATION_FAILED;
335 if (NULL == cmd->appid) {
336 // Get appid by pid using app control
337 ret = app_manager_get_app_id(pid, &appid);
338 if (APP_MANAGER_ERROR_NONE != ret) {
339 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
342 cmd->appid = strdup(appid);
346 ret = sqlite3_bind_text(stmt, 8, cmd->appid, -1, SQLITE_TRANSIENT);
347 if (ret != SQLITE_OK) {
348 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
349 sqlite3_clear_bindings(stmt);
350 sqlite3_finalize(stmt);
351 return VC_DB_ERROR_OPERATION_FAILED;
353 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
354 ret = sqlite3_bind_text(stmt, 9, cmd->invocation_name, -1, SQLITE_TRANSIENT);
355 if (ret != SQLITE_OK) {
356 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
357 sqlite3_clear_bindings(stmt);
358 sqlite3_finalize(stmt);
359 return VC_DB_ERROR_OPERATION_FAILED;
362 ret = sqlite3_bind_text(stmt, 10, cmd->fixed, -1, SQLITE_TRANSIENT);
363 if (ret != SQLITE_OK) {
364 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
365 sqlite3_clear_bindings(stmt);
366 sqlite3_finalize(stmt);
367 return VC_DB_ERROR_OPERATION_FAILED;
369 ret = sqlite3_step(stmt);
370 if (ret != SQLITE_DONE) {
371 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
372 sqlite3_clear_bindings(stmt);
373 sqlite3_finalize(stmt);
374 return VC_DB_ERROR_OPERATION_FAILED;
377 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);
378 SLOG(LOG_INFO, vc_db_tag(), "[SQL] @@ appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
381 sqlite3_clear_bindings(stmt);
382 sqlite3_finalize(stmt);
383 return VC_DB_ERROR_NONE;
386 static int __vc_db_get_commands(sqlite3* db_handle, int pid, vc_cmd_type_e type, GSList** cmd_list)
388 SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
391 sqlite3_stmt* stmt = NULL;
395 if (VC_COMMAND_TYPE_BACKGROUND == type) {
396 /* For background command */
397 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
399 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
403 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
404 return VC_DB_ERROR_OUT_OF_MEMORY;
407 /* check whether there is a table or not */
408 bool is_exist = false;
409 ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
410 if (VC_DB_ERROR_NONE != ret || false == is_exist) {
411 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
412 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
413 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
416 return VC_DB_ERROR_OPERATION_FAILED;
419 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
423 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
424 if (ret != SQLITE_OK) {
425 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
428 return VC_DB_ERROR_OPERATION_FAILED;
431 ret = sqlite3_bind_int(stmt, 1, (int)type);
432 if (ret != SQLITE_OK) {
433 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
434 sqlite3_finalize(stmt);
437 return VC_DB_ERROR_OPERATION_FAILED;
439 if (VC_COMMAND_TYPE_BACKGROUND != type) {
440 ret = sqlite3_bind_int(stmt, 2, pid);
441 if (ret != SQLITE_OK) {
442 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
443 sqlite3_clear_bindings(stmt);
444 sqlite3_finalize(stmt);
447 return VC_DB_ERROR_OPERATION_FAILED;
451 if (VC_COMMAND_TYPE_BACKGROUND == type)
452 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
454 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
456 ret = sqlite3_step(stmt);
457 if (ret == SQLITE_DONE) {
458 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
459 sqlite3_clear_bindings(stmt);
460 sqlite3_finalize(stmt);
463 return VC_DB_ERROR_NONE;
466 if (VC_COMMAND_TYPE_BACKGROUND == type && -1 != pid) {
467 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
468 SLOG(LOG_WARN, vc_db_tag(), "[WARN] fail to get app id, pid(%d)", pid);
472 while (SQLITE_ROW == ret) {
475 char* temp_text = NULL;
477 ret = vc_cmd_create(&temp_cmd);
480 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
486 sqlite3_clear_bindings(stmt);
487 sqlite3_finalize(stmt);
490 return VC_DB_ERROR_OUT_OF_MEMORY;
493 temp_text = (char*)sqlite3_column_text(stmt, 7);
494 if (NULL != temp_text)
495 vc_cmd_set_appid(temp_cmd, temp_text);
497 ret = vc_cmd_get_appid(temp_cmd, &temp_text);
498 if (NULL != appid && 0 == ret) {
499 if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_text, strlen(appid))) {
500 SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
505 vc_cmd_destroy(temp_cmd);
508 ret = sqlite3_step(stmt);
509 if (SQLITE_DONE == ret)
516 if (NULL != temp_text) {
521 temp = sqlite3_column_int(stmt, 0);
522 vc_cmd_set_id(temp_cmd, temp);
524 temp = sqlite3_column_int(stmt, 1);
525 vc_cmd_set_pid(temp_cmd, temp);
527 temp = sqlite3_column_int(stmt, 2);
528 vc_cmd_set_type(temp_cmd, temp);
530 temp = sqlite3_column_int(stmt, 3);
531 vc_cmd_set_format(temp_cmd, temp);
533 temp = sqlite3_column_int(stmt, 4);
534 vc_cmd_set_domain(temp_cmd, temp);
536 temp_text = (char*)sqlite3_column_text(stmt, 5);
537 if (NULL != temp_text)
538 vc_cmd_set_command(temp_cmd, temp_text);
540 temp_text = (char*)sqlite3_column_text(stmt, 6);
541 if (NULL != temp_text)
542 vc_cmd_set_unfixed_command(temp_cmd, temp_text);
544 temp_text = (char*)sqlite3_column_text(stmt, 8);
545 if (NULL != temp_text)
546 vc_cmd_set_invocation_name(temp_cmd, temp_text);
548 temp_text = (char*)sqlite3_column_text(stmt, 9);
549 if (NULL != temp_text)
550 vc_cmd_set_fixed_command(temp_cmd, temp_text);
552 ret = vc_cmd_get_type(temp_cmd, &temp);
553 if (0 == ret && type == temp) {
554 *cmd_list = g_slist_append(*cmd_list, temp_cmd);
556 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp, type);
558 vc_cmd_destroy(temp_cmd);
561 ret = sqlite3_step(stmt);
562 if (SQLITE_DONE == ret)
572 sqlite3_clear_bindings(stmt);
573 sqlite3_finalize(stmt);
577 return VC_DB_ERROR_NONE;
580 static int __vc_db_get_pid(const char* appid, int* pid)
582 bool running = false;
583 int ret = app_manager_is_running(appid, &running);
584 if (APP_MANAGER_ERROR_NONE != ret) {
585 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to check running with appid(%s)", appid);
586 return VC_DB_ERROR_OPERATION_FAILED;
588 if (true == running) {
589 app_context_h app_context = NULL;
590 ret = app_manager_get_app_context(appid, &app_context);
591 if (APP_MANAGER_ERROR_NONE != ret) {
592 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
593 return VC_DB_ERROR_OPERATION_FAILED;
596 ret = app_context_get_pid(app_context, pid);
597 if (APP_MANAGER_ERROR_NONE != ret) {
598 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get pid, ret(%d), appid(%s)", ret, appid);
599 return VC_DB_ERROR_OPERATION_FAILED;
602 ret = app_context_destroy(app_context);
603 if (APP_MANAGER_ERROR_NONE != ret) {
604 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to destroy app context, ret(%d), appid(%s)", ret, appid);
605 return VC_DB_ERROR_OPERATION_FAILED;
608 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
610 return VC_DB_ERROR_NONE;
613 static int __vc_db_insert_result(sqlite3* db_handle, const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
615 SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
617 sqlite3_stmt* stmt = NULL;
618 const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
621 /* check whether there is a table or not */
622 bool is_exist = false;
623 ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
624 if (VC_DB_ERROR_NONE != ret || false == is_exist) {
625 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
626 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
627 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
628 return VC_DB_ERROR_OPERATION_FAILED;
631 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
635 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
636 if (ret != SQLITE_OK) {
637 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
638 return VC_DB_ERROR_OPERATION_FAILED;
640 ret = sqlite3_bind_text(stmt, 2, result_text, -1, SQLITE_TRANSIENT);
641 if (ret != SQLITE_OK) {
642 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
643 sqlite3_finalize(stmt);
644 return VC_DB_ERROR_OPERATION_FAILED;
646 ret = sqlite3_bind_int(stmt, 3, event);
647 if (ret != SQLITE_OK) {
648 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
649 sqlite3_clear_bindings(stmt);
650 sqlite3_finalize(stmt);
651 return VC_DB_ERROR_OPERATION_FAILED;
653 ret = sqlite3_bind_text(stmt, 4, msg, -1, SQLITE_TRANSIENT);
654 if (ret != SQLITE_OK) {
655 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
656 sqlite3_clear_bindings(stmt);
657 sqlite3_finalize(stmt);
658 return VC_DB_ERROR_OPERATION_FAILED;
660 ret = sqlite3_bind_int(stmt, 5, exclusive);
661 if (ret != SQLITE_OK) {
662 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
663 sqlite3_clear_bindings(stmt);
664 sqlite3_finalize(stmt);
665 return VC_DB_ERROR_OPERATION_FAILED;
669 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d))", result_text, event, msg, exclusive);
671 ret = sqlite3_step(stmt);
672 if (ret != SQLITE_DONE) {
673 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
674 sqlite3_clear_bindings(stmt);
675 sqlite3_finalize(stmt);
676 return VC_DB_ERROR_OPERATION_FAILED;
679 sqlite3_clear_bindings(stmt);
680 sqlite3_finalize(stmt);
681 return VC_DB_ERROR_NONE;
684 if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
686 ret = __vc_db_get_pid(cmd->appid, &pid);
688 SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
690 SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
695 ret = sqlite3_bind_int(stmt, 6, cmd->pid);
696 if (ret != SQLITE_OK) {
697 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
698 sqlite3_clear_bindings(stmt);
699 sqlite3_finalize(stmt);
700 return VC_DB_ERROR_OPERATION_FAILED;
702 ret = sqlite3_bind_int(stmt, 7, cmd->type);
703 if (ret != SQLITE_OK) {
704 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
705 sqlite3_clear_bindings(stmt);
706 sqlite3_finalize(stmt);
707 return VC_DB_ERROR_OPERATION_FAILED;
709 ret = sqlite3_bind_int(stmt, 8, cmd->format);
710 if (ret != SQLITE_OK) {
711 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
712 sqlite3_clear_bindings(stmt);
713 sqlite3_finalize(stmt);
714 return VC_DB_ERROR_OPERATION_FAILED;
716 ret = sqlite3_bind_int(stmt, 9, cmd->domain);
717 if (ret != SQLITE_OK) {
718 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
719 sqlite3_clear_bindings(stmt);
720 sqlite3_finalize(stmt);
721 return VC_DB_ERROR_OPERATION_FAILED;
723 ret = sqlite3_bind_text(stmt, 10, cmd->command, -1, SQLITE_TRANSIENT);
724 if (ret != SQLITE_OK) {
725 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
726 sqlite3_clear_bindings(stmt);
727 sqlite3_finalize(stmt);
728 return VC_DB_ERROR_OPERATION_FAILED;
730 ret = sqlite3_bind_text(stmt, 11, cmd->parameter, -1, SQLITE_TRANSIENT);
731 if (ret != SQLITE_OK) {
732 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
733 sqlite3_clear_bindings(stmt);
734 sqlite3_finalize(stmt);
735 return VC_DB_ERROR_OPERATION_FAILED;
737 ret = sqlite3_bind_text(stmt, 12, cmd->appid, -1, SQLITE_TRANSIENT);
738 if (ret != SQLITE_OK) {
739 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
740 sqlite3_clear_bindings(stmt);
741 sqlite3_finalize(stmt);
742 return VC_DB_ERROR_OPERATION_FAILED;
744 ret = sqlite3_bind_text(stmt, 13, cmd->invocation_name, -1, SQLITE_TRANSIENT);
745 if (ret != SQLITE_OK) {
746 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
747 sqlite3_clear_bindings(stmt);
748 sqlite3_finalize(stmt);
749 return VC_DB_ERROR_OPERATION_FAILED;
751 ret = sqlite3_bind_text(stmt, 14, cmd->fixed, -1, SQLITE_TRANSIENT);
752 if (ret != SQLITE_OK) {
753 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
754 sqlite3_clear_bindings(stmt);
755 sqlite3_finalize(stmt);
756 return VC_DB_ERROR_OPERATION_FAILED;
758 ret = sqlite3_step(stmt);
759 if (ret != SQLITE_DONE) {
760 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
761 sqlite3_clear_bindings(stmt);
762 sqlite3_finalize(stmt);
763 return VC_DB_ERROR_OPERATION_FAILED;
766 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)",
767 result_text, event, msg, exclusive, cmd->pid, cmd->type, cmd->format, cmd->domain, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
770 sqlite3_clear_bindings(stmt);
771 sqlite3_finalize(stmt);
773 return VC_DB_ERROR_NONE;
776 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
778 if (NULL == org_cmd || NULL == new_cmd) {
779 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
780 return VC_DB_ERROR_INVALID_PARAMETER;
783 if (NULL == invocation_name) {
784 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, invocation name is NULL, org_cmd(%s)", org_cmd);
785 return VC_DB_ERROR_INVALID_PARAMETER;
788 if (strlen(org_cmd) <= strlen(invocation_name)) {
789 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
790 return VC_DB_ERROR_INVALID_PARAMETER;
793 if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
794 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
796 SLOG(LOG_DEBUG, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, *new_cmd, invocation_name);
797 return VC_DB_ERROR_NONE;
800 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
802 if (NULL == command || NULL == temp_unfixed) {
803 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
804 return VC_DB_ERROR_INVALID_PARAMETER;
808 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, fixed cmd is NULL, org_cmd(%s)", command);
809 return VC_DB_ERROR_INVALID_PARAMETER;
812 if (strlen(command) <= strlen(fixed)) {
813 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to extract unfixed command, cmd(%s) fixed(%s)", command, fixed);
814 return VC_DB_ERROR_INVALID_PARAMETER;
817 char* temp = (char*)calloc(256, sizeof(char));
819 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
820 return VC_DB_ERROR_OUT_OF_MEMORY;
822 if (0 == strncasecmp(command, fixed, strlen(fixed))) {
823 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
824 SLOG(LOG_WARN, vc_db_tag(), "@@@");
825 } else if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
826 strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
827 SLOG(LOG_WARN, vc_db_tag(), "@@@");
830 SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
832 *temp_unfixed = strdup(temp);
836 return VC_DB_ERROR_NONE;
839 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)
842 sqlite3_stmt* stmt = NULL;
845 sql = strdup("SELECT * FROM vc_result;");
846 else if (NULL != appid)
847 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
849 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
852 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
853 return VC_DB_ERROR_OUT_OF_MEMORY;
856 /* check whether there is a table or not */
857 bool is_exist = false;
858 ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
859 if (VC_DB_ERROR_NONE != ret || false == is_exist) {
860 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
861 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
862 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
865 return VC_DB_ERROR_OPERATION_FAILED;
868 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
872 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
873 if (ret != SQLITE_OK) {
874 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
877 return VC_DB_ERROR_OPERATION_FAILED;
880 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
881 if (ret != SQLITE_OK) {
882 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
883 sqlite3_finalize(stmt);
886 return VC_DB_ERROR_OPERATION_FAILED;
888 } else if (-1 != pid) {
889 ret = sqlite3_bind_int(stmt, 1, pid);
890 if (ret != SQLITE_OK) {
891 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
892 sqlite3_finalize(stmt);
895 return VC_DB_ERROR_OPERATION_FAILED;
898 ret = sqlite3_step(stmt);
899 if (ret == SQLITE_DONE) {
900 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
901 sqlite3_clear_bindings(stmt);
902 sqlite3_finalize(stmt);
905 return VC_DB_ERROR_NONE;
909 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
910 else if (NULL != appid)
911 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %s;", appid);
914 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
916 vc_cmd_h temp_cmd = NULL;
917 while (SQLITE_ROW == ret) {
919 char* temp_text = NULL;
920 const char* invocation_name = NULL;
922 temp_text = (char*)sqlite3_column_text(stmt, 1);
923 if (NULL != temp_text)
924 *result_text = strdup(temp_text);
926 temp = sqlite3_column_int(stmt, 2);
930 temp_text = (char*)sqlite3_column_text(stmt, 3);
931 if (NULL != temp_text)
932 *msg = strdup(temp_text);
935 if (0 != vc_cmd_create(&temp_cmd)) {
936 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
937 if (NULL != *result_text) {
942 if (NULL != msg && NULL != *msg) {
948 sqlite3_clear_bindings(stmt);
949 sqlite3_finalize(stmt);
955 temp = sqlite3_column_int(stmt, 0);
956 vc_cmd_set_id(temp_cmd, temp);
958 temp = sqlite3_column_int(stmt, 5);
959 vc_cmd_set_pid(temp_cmd, temp);
961 temp = sqlite3_column_int(stmt, 6);
962 vc_cmd_set_type(temp_cmd, temp);
964 temp = sqlite3_column_int(stmt, 7);
965 vc_cmd_set_format(temp_cmd, temp);
967 temp = sqlite3_column_int(stmt, 8);
968 vc_cmd_set_domain(temp_cmd, temp);
971 invocation_name = (char*)sqlite3_column_text(stmt, 12);
973 temp_text = (char*)sqlite3_column_text(stmt, 9);
974 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
976 if (NULL != temp_text) {
977 char* temp_command = NULL;
978 char* temp_unfixed = NULL;
979 char* temp_fixed = NULL;
981 if (NULL != invocation_name)
982 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
984 if (NULL == temp_command) {
985 temp_command = strdup(temp_text);
987 // remove invocation name from result_text
989 *result_text = strdup(temp_command);
993 temp_fixed = (char*)sqlite3_column_text(stmt, 13);
994 if (NULL != temp_fixed)
995 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
998 temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
1000 if (NULL != temp_unfixed) {
1001 char merge_result[256] = {0, };
1002 vc_cmd_set_command(temp_cmd, temp_command);
1003 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
1004 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
1005 if (NULL != *result_text)
1007 *result_text = strdup(merge_result);
1008 } else if (NULL != temp_fixed) {
1009 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
1010 vc_cmd_set_command(temp_cmd, temp_fixed);
1011 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
1012 if (NULL != temp_unfixed) {
1014 temp_unfixed = NULL;
1017 vc_cmd_set_command(temp_cmd, temp_command);
1020 temp_command = NULL;
1023 temp_text = (char*)sqlite3_column_text(stmt, 11);
1024 if (NULL != temp_text)
1025 vc_cmd_set_appid(temp_cmd, temp_text);
1027 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1028 SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1029 vc_cmd_destroy(temp_cmd);
1030 vc_cmd_list_destroy(vc_cmd_list, true);
1031 if (NULL != *result_text) {
1033 *result_text = NULL;
1036 if (NULL != msg && NULL != *msg) {
1041 sqlite3_reset(stmt);
1042 sqlite3_clear_bindings(stmt);
1043 sqlite3_finalize(stmt);
1046 return VC_DB_ERROR_OPERATION_FAILED;
1049 ret = sqlite3_step(stmt);
1050 if (SQLITE_DONE == ret)
1054 sqlite3_reset(stmt);
1055 sqlite3_clear_bindings(stmt);
1056 sqlite3_finalize(stmt);
1060 return VC_DB_ERROR_NONE;
1063 void __vc_db_demandable_client_free(void* data)
1065 vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data;
1067 if (NULL != d_app) {
1068 if (NULL != d_app->appid) {
1070 d_app->appid = NULL;
1077 static int __vc_db_get_appid(sqlite3* db_handle, const char* result, GSList** app_list)
1079 GSList* temp_app_list = NULL;
1082 sqlite3_stmt* stmt = NULL;
1083 const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
1085 /* check whether there is a table or not */
1086 bool is_exist = false;
1087 ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
1088 if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1089 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
1090 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
1091 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1092 return VC_DB_ERROR_OPERATION_FAILED;
1095 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
1099 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1100 if (ret != SQLITE_OK) {
1101 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1102 return VC_DB_ERROR_OPERATION_FAILED;
1104 ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
1105 if (ret != SQLITE_OK) {
1106 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1107 sqlite3_finalize(stmt);
1108 return VC_DB_ERROR_OPERATION_FAILED;
1110 ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
1111 if (ret != SQLITE_OK) {
1112 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1113 sqlite3_clear_bindings(stmt);
1114 sqlite3_finalize(stmt);
1115 return VC_DB_ERROR_OPERATION_FAILED;
1117 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
1118 ret = sqlite3_step(stmt);
1119 if (ret == SQLITE_DONE) {
1120 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1121 sqlite3_clear_bindings(stmt);
1122 sqlite3_finalize(stmt);
1123 return VC_DB_ERROR_NONE;
1126 while (SQLITE_ROW == ret) {
1127 char* temp_text = NULL;
1128 vc_deactivated_app_s* temp_app = NULL;
1129 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
1130 if (NULL == temp_app) {
1131 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allocation fail");
1133 if (NULL != temp_app_list) {
1134 g_slist_free_full(temp_app_list, __vc_db_demandable_client_free);
1135 temp_app_list = NULL;
1138 sqlite3_reset(stmt);
1139 sqlite3_clear_bindings(stmt);
1140 sqlite3_finalize(stmt);
1141 return VC_DB_ERROR_OUT_OF_MEMORY;
1144 temp_text = (char*)sqlite3_column_text(stmt, 11);
1145 if (NULL != temp_text)
1146 temp_app->appid = strdup(temp_text);
1148 temp_app_list = g_slist_append(temp_app_list, temp_app);
1150 ret = sqlite3_step(stmt);
1151 if (SQLITE_DONE == ret)
1155 *app_list = temp_app_list;
1157 sqlite3_reset(stmt);
1158 sqlite3_clear_bindings(stmt);
1159 sqlite3_finalize(stmt);
1160 return VC_DB_ERROR_NONE;
1163 int __vc_db_get_result_pid_list(sqlite3* db_handle, const char* result, GSList** pid_list)
1165 GSList* temp_pid_list = NULL;
1168 sqlite3_stmt* stmt = NULL;
1169 const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
1172 /* check whether there is a table or not */
1173 bool is_exist = false;
1174 ret = __vc_db_check_table(db_handle, VC_RESULT_TABLE, &is_exist);
1175 if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1176 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_RESULT_TABLE, ret);
1177 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_RESULT_TABLE)) {
1178 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1179 return VC_DB_ERROR_OPERATION_FAILED;
1182 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_RESULT_TABLE);
1186 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1187 if (ret != SQLITE_OK) {
1188 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1189 return VC_DB_ERROR_OPERATION_FAILED;
1191 ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
1192 if (ret != SQLITE_OK) {
1193 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1194 sqlite3_finalize(stmt);
1195 return VC_DB_ERROR_OPERATION_FAILED;
1197 ret = sqlite3_step(stmt);
1198 if (ret == SQLITE_DONE) {
1199 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1200 sqlite3_clear_bindings(stmt);
1201 sqlite3_finalize(stmt);
1202 return VC_DB_ERROR_NONE;
1205 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
1207 while (SQLITE_ROW == ret) {
1209 vc_cmd_s* temp_cmd = NULL;
1210 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
1211 if (NULL == temp_cmd) {
1212 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allocation fail");
1214 if (NULL != temp_pid_list) {
1215 g_slist_free_full(temp_pid_list, free);
1216 temp_pid_list = NULL;
1219 sqlite3_reset(stmt);
1220 sqlite3_clear_bindings(stmt);
1221 sqlite3_finalize(stmt);
1222 return VC_DB_ERROR_OUT_OF_MEMORY;
1225 temp = sqlite3_column_int(stmt, 0);
1226 temp_cmd->pid = temp;
1228 temp = sqlite3_column_int(stmt, 1);
1229 temp_cmd->type = temp;
1231 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
1233 ret = sqlite3_step(stmt);
1234 if (SQLITE_DONE == ret)
1238 *pid_list = temp_pid_list;
1240 sqlite3_reset(stmt);
1241 sqlite3_clear_bindings(stmt);
1242 sqlite3_finalize(stmt);
1243 return VC_DB_ERROR_NONE;
1246 static int __vc_db_append_commands(sqlite3* db_handle, int pid, int type, vc_cmd_list_h vc_cmd_list)
1248 SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
1251 sqlite3_stmt* stmt = NULL;
1252 const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
1254 /* check whether there is a table or not */
1255 bool is_exist = false;
1256 ret = __vc_db_check_table(db_handle, VC_INFO_TABLE, &is_exist);
1257 if (VC_DB_ERROR_NONE != ret || false == is_exist) {
1258 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] There is no table (%s) (%d). Make a table", VC_INFO_TABLE, ret);
1259 if (VC_DB_ERROR_NONE != __vc_db_create_table(db_handle, VC_INFO_TABLE)) {
1260 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1261 return VC_DB_ERROR_OPERATION_FAILED;
1264 SLOG(LOG_INFO, vc_db_tag(), "[INFO] There is the table (%s)", VC_INFO_TABLE);
1268 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1269 if (ret != SQLITE_OK) {
1270 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1271 return VC_DB_ERROR_OPERATION_FAILED;
1273 ret = sqlite3_bind_int(stmt, 1, pid);
1274 if (ret != SQLITE_OK) {
1275 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1276 sqlite3_finalize(stmt);
1277 return VC_DB_ERROR_OPERATION_FAILED;
1279 ret = sqlite3_bind_int(stmt, 2, type);
1280 if (ret != SQLITE_OK) {
1281 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1282 sqlite3_clear_bindings(stmt);
1283 sqlite3_finalize(stmt);
1284 return VC_DB_ERROR_OPERATION_FAILED;
1286 ret = sqlite3_step(stmt);
1287 if (ret == SQLITE_DONE) {
1288 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1289 sqlite3_clear_bindings(stmt);
1290 sqlite3_finalize(stmt);
1291 return VC_DB_ERROR_NONE;
1294 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
1296 vc_cmd_h temp_cmd = NULL;
1298 while (SQLITE_ROW == ret) {
1300 char* temp_text = NULL;
1302 if (0 != vc_cmd_create(&temp_cmd)) {
1303 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
1304 sqlite3_reset(stmt);
1305 sqlite3_clear_bindings(stmt);
1306 sqlite3_finalize(stmt);
1309 temp = sqlite3_column_int(stmt, 0);
1310 vc_cmd_set_id(temp_cmd, temp);
1312 temp = sqlite3_column_int(stmt, 1);
1313 vc_cmd_set_pid(temp_cmd, temp);
1315 temp = sqlite3_column_int(stmt, 2);
1316 vc_cmd_set_type(temp_cmd, temp);
1318 temp = sqlite3_column_int(stmt, 3);
1319 vc_cmd_set_format(temp_cmd, temp);
1321 temp = sqlite3_column_int(stmt, 4);
1322 vc_cmd_set_domain(temp_cmd, temp);
1324 temp_text = (char*)sqlite3_column_text(stmt, 5);
1325 if (NULL != temp_text)
1326 vc_cmd_set_command(temp_cmd, temp_text);
1328 temp_text = (char*)sqlite3_column_text(stmt, 6);
1329 if (NULL != temp_text)
1330 vc_cmd_set_unfixed_command(temp_cmd, temp_text);
1332 temp_text = (char*)sqlite3_column_text(stmt, 7);
1333 if (NULL != temp_text)
1334 vc_cmd_set_appid(temp_cmd, temp_text);
1336 temp_text = (char*)sqlite3_column_text(stmt, 8);
1337 if (NULL != temp_text)
1338 vc_cmd_set_invocation_name(temp_cmd, temp_text);
1340 temp_text = (char*)sqlite3_column_text(stmt, 9);
1341 if (NULL != temp_text)
1342 vc_cmd_set_fixed_command(temp_cmd, temp_text);
1344 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1345 SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1346 vc_cmd_destroy(temp_cmd);
1347 vc_cmd_list_destroy(vc_cmd_list, true);
1348 sqlite3_reset(stmt);
1349 sqlite3_clear_bindings(stmt);
1350 sqlite3_finalize(stmt);
1351 return VC_DB_ERROR_OPERATION_FAILED;
1354 ret = sqlite3_step(stmt);
1355 if (SQLITE_DONE == ret)
1359 sqlite3_reset(stmt);
1360 sqlite3_clear_bindings(stmt);
1361 sqlite3_finalize(stmt);
1362 return VC_DB_ERROR_NONE;
1365 static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
1367 if (NULL == src_cmd) {
1368 SLOG(LOG_WARN, vc_db_tag(), "[Client Data] Input command is NULL");
1372 vc_cmd_s* temp_cmd = NULL;
1373 temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
1374 if (NULL == temp_cmd) {
1375 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1379 temp_cmd->id = src_cmd->id;
1380 temp_cmd->pid = src_cmd->pid;
1381 temp_cmd->index = src_cmd->index;
1382 temp_cmd->type = src_cmd->type;
1383 temp_cmd->format = src_cmd->format;
1384 temp_cmd->domain = src_cmd->domain;
1386 if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
1387 else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
1388 else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_WIDGET;
1389 else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1390 else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
1391 else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
1393 if (NULL != src_cmd->command) {
1394 temp_cmd->command = strdup(src_cmd->command);
1397 if (NULL != src_cmd->parameter) {
1398 temp_cmd->parameter = strdup(src_cmd->parameter);
1401 if (NULL != src_cmd->appid) {
1402 temp_cmd->appid = strdup(src_cmd->appid);
1405 if (NULL != src_cmd->invocation_name) {
1406 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
1409 if (NULL != src_cmd->fixed) {
1410 temp_cmd->fixed = strdup(src_cmd->fixed);
1413 temp_cmd->key = src_cmd->key;
1414 temp_cmd->modifier = src_cmd->modifier;
1419 static int __vc_db_create_table(sqlite3* db_handle, const char* table)
1421 SLOG(LOG_INFO, vc_db_tag(), "[INFO] Create DB table (%s)", table);
1424 if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1425 sql = strdup("CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
1426 pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
1427 } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1428 sql = strdup("CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
1429 command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);");
1431 return VC_DB_ERROR_INVALID_PARAMETER;
1434 int ret = __vc_db_exec_query(db_handle, sql);
1435 if (ret != VC_DB_ERROR_NONE) {
1436 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", table, ret);
1437 return VC_DB_ERROR_OPERATION_FAILED;
1439 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1443 return VC_DB_ERROR_NONE;
1446 int __vc_db_open_db(char** path, sqlite3** db_handle)
1449 int ret = db_util_open(*path, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1450 if (ret != SQLITE_OK) {
1451 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", *path, ret, sqlite3_errmsg(*db_handle));
1453 db_util_close(*db_handle);
1459 return VC_DB_ERROR_OPERATION_FAILED;
1462 if (lstat(*path, &stat) < 0) {
1464 SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
1466 db_util_close(*db_handle);
1471 return VC_DB_ERROR_OPERATION_FAILED;
1474 if (!S_ISREG(stat.st_mode)) {
1475 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1477 db_util_close(*db_handle);
1482 return VC_DB_ERROR_OPERATION_FAILED;
1485 if (!stat.st_size) {
1486 __vc_db_begin_transaction(*db_handle);
1489 ret = __vc_db_create_table(*db_handle, VC_INFO_TABLE);
1490 if (ret != VC_DB_ERROR_NONE) {
1491 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1492 __vc_db_rollback_transaction(*db_handle);
1493 return VC_DB_ERROR_OPERATION_FAILED;
1495 ret = __vc_db_create_table(*db_handle, VC_RESULT_TABLE);
1496 if (ret != VC_DB_ERROR_NONE) {
1497 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1498 __vc_db_rollback_transaction(*db_handle);
1499 return VC_DB_ERROR_OPERATION_FAILED;
1502 __vc_db_commit_transaction(*db_handle);
1506 char* err_msg = NULL;
1507 static const const char* sql = "PRAGMA journal_mode = WAL";
1508 int ret = sqlite3_exec(*db_handle, sql, NULL, NULL, &err_msg);
1509 if (ret != SQLITE_OK) {
1510 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1513 return VC_DB_ERROR_NONE;
1516 static int __vc_db_integrity_check_cb(void *NotUsed, int argc, char **argv, char **azColName)
1518 SLOG(LOG_INFO, vc_db_tag(), "integrity check cb is called");
1520 char *check_str = "ok";
1521 if (0 != strncmp(argv[0], check_str, strlen(check_str))) {
1522 SLOG(LOG_ERROR, vc_db_tag(), "db integrity result : %s", argv[0]);
1523 is_db_corrupted = true;
1527 SLOG(LOG_INFO, vc_db_tag(), "db integrity result : %s", argv[0]);
1531 bool __vc_db_connect_db(char** path, sqlite3** db_handle)
1533 bool is_connect = false;
1534 int ret = __vc_db_open_db(path, db_handle);
1538 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1541 if (0 == __vc_db_open_db(path, db_handle)) {
1542 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
1549 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to open DB");
1555 int vc_db_initialize(void)
1557 SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
1559 if (0 < g_ref_cnt) {
1561 return VC_DB_ERROR_NONE;
1564 /* For voice control DB */
1565 g_path = (char*)calloc(256, sizeof(char));
1566 if (NULL == g_path) {
1567 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1568 return VC_DB_ERROR_OUT_OF_MEMORY;
1570 /* This should be changed to general DB space - TZ_USER_DB */
1571 snprintf(g_path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1574 g_backup_path = (char*)calloc(256, sizeof(char));
1575 if (NULL == g_backup_path) {
1576 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1577 return VC_DB_ERROR_OUT_OF_MEMORY;
1579 snprintf(g_backup_path, 256, "%s/.vc_backup.db", VC_RUNTIME_INFO_ROOT);
1581 bool is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1582 if (false == is_connect) {
1583 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB, retry to connect after removing file");
1584 if (0 != remove(g_path)) {
1585 SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1586 g_db_cnt = (g_db_cnt + 1) % 1000;
1587 snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1589 is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1590 if (true == is_connect) {
1591 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
1592 is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1593 if (true == is_connect) {
1594 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
1595 if (0 != vc_db_restore_command()) {
1596 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1600 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
1601 return VC_DB_ERROR_OPERATION_FAILED;
1605 int ret = sqlite3_exec(g_db_handle, "pragma integrity_check", __vc_db_integrity_check_cb, NULL, NULL);
1606 if (true == is_db_corrupted || SQLITE_CORRUPT == ret) {
1607 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open DB");
1609 ret = db_util_close(g_db_handle);
1610 if (ret != SQLITE_OK) {
1611 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to close db, ret %d: %s", ret, sqlite3_errmsg(g_db_handle));
1615 if (0 != remove(g_path)) {
1616 SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_path);
1617 g_db_cnt = (g_db_cnt + 1) % 1000;
1618 snprintf(g_path, 256, "%s/.vc_info_%d.db", VC_RUNTIME_INFO_ROOT, g_db_cnt);
1620 is_connect = __vc_db_connect_db(&g_path, &g_db_handle);
1621 if (true == is_connect) {
1622 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect main DB");
1623 is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1624 if (true == is_connect) {
1625 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
1626 if (0 != vc_db_restore_command()) {
1627 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1631 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to connect main DB");
1632 return VC_DB_ERROR_OPERATION_FAILED;
1636 SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization after restore");
1640 is_connect = __vc_db_connect_db(&g_backup_path, &g_db_backup_handle);
1641 if (false == is_connect) {
1642 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open backup DB, retry to connect after removing file");
1643 if (0 != remove(g_backup_path)) {
1644 g_backup_db_cnt = (g_backup_db_cnt + 1) % 1000;
1645 SLOG(LOG_ERROR, vc_db_tag(), "[Error] remove file(%s) is failed", g_backup_path);
1646 snprintf(g_backup_path, 256, "%s/.vc_backup_%d.db", VC_RUNTIME_INFO_ROOT, g_backup_db_cnt);
1648 is_connect = __vc_db_connect_db(&g_path, &g_db_backup_handle);
1649 if (true == is_connect) {
1650 SLOG(LOG_ERROR, vc_db_tag(), "[INFO] Success to connect backup DB");
1651 if (0 != vc_db_restore_command()) {
1652 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to restore command");
1659 SLOG(LOG_INFO, vc_db_tag(), "[SUCCESS] DB initialization");
1660 return VC_DB_ERROR_NONE;
1663 int vc_db_finalize(void)
1665 if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
1666 if (0 != --g_ref_cnt)
1667 return VC_DB_ERROR_NONE;
1669 if (NULL != g_path) {
1674 if (NULL != g_backup_path) {
1675 free(g_backup_path);
1676 g_backup_path = NULL;
1681 db_util_close(g_db_handle);
1684 if (!g_db_backup_handle)
1686 db_util_close(g_db_backup_handle);
1687 g_db_backup_handle = NULL;
1689 return VC_DB_ERROR_NONE;
1692 int vc_db_create_table()
1694 __vc_db_reset_handle();
1695 __vc_db_begin_transaction(g_db_handle);
1697 int ret = __vc_db_create_table(g_db_handle, VC_INFO_TABLE);
1698 if (ret != VC_DB_ERROR_NONE) {
1699 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_INFO_TABLE, ret);
1700 __vc_db_rollback_transaction(g_db_handle);
1701 return VC_DB_ERROR_OPERATION_FAILED;
1703 ret = __vc_db_create_table(g_db_handle, VC_RESULT_TABLE);
1704 if (ret != VC_DB_ERROR_NONE) {
1705 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table(%s), %d", VC_RESULT_TABLE, ret);
1706 __vc_db_rollback_transaction(g_db_handle);
1707 return VC_DB_ERROR_OPERATION_FAILED;
1710 __vc_db_commit_transaction(g_db_handle);
1711 return VC_DB_ERROR_NONE;
1714 int __vc_db_delete_table(sqlite3* db_handle, const char* table)
1717 if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1718 sql = strdup("DELETE FROM vc_result;");
1719 } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1720 sql = strdup("DELETE FROM vc_info;");
1722 return VC_DB_ERROR_INVALID_PARAMETER;
1726 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1727 return VC_DB_ERROR_OUT_OF_MEMORY;
1730 int ret = __vc_db_exec_query(db_handle, sql);
1731 if (ret != VC_DB_ERROR_NONE) {
1732 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1741 if (0 == strncmp(table, VC_RESULT_TABLE, strlen(table))) {
1742 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1743 } else if (0 == strncmp(table, VC_INFO_TABLE, strlen(table))) {
1744 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1748 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1749 return VC_DB_ERROR_OUT_OF_MEMORY;
1752 ret = __vc_db_exec_query(db_handle, sql);
1753 if (ret != VC_DB_ERROR_NONE) {
1754 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1760 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1764 return VC_DB_ERROR_NONE;
1767 int vc_db_delete_table(const char* table)
1769 __vc_db_reset_handle();
1770 __vc_db_begin_transaction(g_db_handle);
1772 int ret = __vc_db_delete_table(g_db_handle, table);
1774 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1775 __vc_db_rollback_transaction(g_db_handle);
1779 __vc_db_commit_transaction(g_db_handle);
1781 return VC_DB_ERROR_NONE;
1784 int vc_db_begin_transaction(void)
1786 __vc_db_reset_handle();
1787 int ret = __vc_db_begin_transaction(g_db_handle);
1791 int vc_db_rollback_transaction(void)
1793 __vc_db_reset_handle();
1794 int ret = __vc_db_rollback_transaction(g_db_handle);
1798 int vc_db_commit_transaction(void)
1800 __vc_db_reset_handle();
1801 int ret = __vc_db_commit_transaction(g_db_handle);
1805 static void __vc_db_remove_space(char** string)
1807 if (NULL == string || NULL == *string)
1810 char* temp = *string;
1812 //remove previous space
1814 memmove(temp, temp + 1, strlen(temp));
1816 // remove next space
1817 if (' ' == temp[strlen(temp) - 1])
1818 temp[strlen(temp) - 1] = '\0';
1821 static bool __vc_db_is_valid_vfixed_string(char* string)
1823 char* temp = strchr(string, '}');
1827 temp = strchr(string, '{');
1831 temp = strchr(string, '|');
1837 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1839 if (NULL == cmd || NULL == cmd->command) {
1840 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
1841 return VC_DB_ERROR_INVALID_PARAMETER;
1844 GSList* temp_list = NULL;
1846 char* src_cmd = strdup(cmd->command);
1847 char* dst_cmd = NULL;
1848 char merge_cmd[256] = {0, };
1850 if (NULL == src_cmd) {
1851 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1852 return VC_DB_ERROR_OUT_OF_MEMORY;
1855 if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
1856 // check string validation
1857 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1858 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1861 return VC_DB_ERROR_INVALID_PARAMETER;
1864 // remove close brace, '}'
1865 char* temp_close = strchr(src_cmd, '}');
1866 if (NULL != temp_close) {
1867 temp_close[0] = '\0';
1870 // extract fixed command and remove space in front of '{'
1871 char *tok_ptr = NULL;
1872 temp = strtok_r(src_cmd, "{", &tok_ptr);
1874 __vc_db_remove_space(&temp);
1875 *fixed_cmd = strdup(temp);
1877 // merge command with fixed and vfixed
1878 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1879 __vc_db_remove_space(&temp);
1881 snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
1882 dst_cmd = strdup(merge_cmd);
1883 temp_list = g_slist_append(temp_list, dst_cmd);
1884 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1887 *fixed_cmd = strdup(cmd->command);
1889 } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
1890 // check string validation
1891 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1892 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1895 return VC_DB_ERROR_INVALID_PARAMETER;
1898 // extract fixed command
1899 char* temp_fixed = strchr(src_cmd, '}') + 1;
1900 __vc_db_remove_space(&temp_fixed);
1901 *fixed_cmd = strdup(temp_fixed);
1903 // remove close brace, '}'
1904 char *tok_ptr = NULL;
1905 temp = strtok_r(src_cmd, "}", &tok_ptr);
1907 // remove open brace, '{'
1908 temp = strchr(src_cmd, '{') + 1;
1911 temp = strtok_r(temp, "|", &tok_ptr);
1912 __vc_db_remove_space(&temp);
1914 // merge command with fixed and vfixed
1915 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1916 dst_cmd = strdup(merge_cmd);
1917 temp_list = g_slist_append(temp_list, dst_cmd);
1918 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1920 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1921 __vc_db_remove_space(&temp);
1923 // merge command with fixed and vfixed
1924 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1925 dst_cmd = strdup(merge_cmd);
1926 temp_list = g_slist_append(temp_list, dst_cmd);
1927 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1929 } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
1930 dst_cmd = strdup(src_cmd);
1931 temp_list = g_slist_append(temp_list, dst_cmd);
1932 *fixed_cmd = strdup(src_cmd);
1935 dst_cmd = strdup(src_cmd);
1936 temp_list = g_slist_append(temp_list, dst_cmd);
1939 *cmd_list = temp_list;
1943 return VC_DB_ERROR_NONE;
1946 static int __vc_db_insert_command(sqlite3* db_handle, int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
1948 GSList* cmd_list = NULL;
1949 char* fixed_cmd = NULL;
1950 vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
1952 int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
1954 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
1956 if (NULL != tmp_cmd) {
1957 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1962 if (0 != g_slist_length(cmd_list)) {
1963 GSList *iter = NULL;
1964 char* temp_command = NULL;
1965 iter = g_slist_nth(cmd_list, 0);
1967 while (NULL != iter) {
1968 temp_command = iter->data;
1970 if (NULL != temp_command) {
1971 if (NULL != tmp_cmd->command) {
1972 free(tmp_cmd->command);
1973 tmp_cmd->command = NULL;
1975 tmp_cmd->command = strdup(temp_command);
1976 if (NULL != fixed_cmd)
1977 tmp_cmd->fixed = strdup(fixed_cmd);
1979 tmp_cmd->fixed = NULL;
1981 ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
1982 if (ret != VC_DB_ERROR_NONE) {
1983 if (NULL != fixed_cmd) {
1987 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1991 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name && false == skip_invocation) {
1992 char temp[256] = {0, };
1993 snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
1994 if (NULL != tmp_cmd->command)
1995 free(tmp_cmd->command);
1997 tmp_cmd->command = strdup(temp);
1999 ret = __vc_db_insert_commands(db_handle, pid, type, tmp_cmd);
2000 if (ret != VC_DB_ERROR_NONE) {
2001 if (NULL != fixed_cmd) {
2005 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
2009 cmd_list = g_slist_remove(cmd_list, temp_command);
2011 temp_command = NULL;
2013 iter = g_slist_nth(cmd_list, 0);
2016 if (VC_DB_ERROR_NONE != ret) {
2017 while (NULL != iter) {
2018 temp_command = iter->data;
2020 if (NULL != temp_command) {
2021 cmd_list = g_slist_remove(cmd_list, temp_command);
2023 temp_command = NULL;
2026 iter = g_slist_nth(cmd_list, 0);
2029 g_slist_free(cmd_list);
2030 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
2037 if (NULL != fixed_cmd) {
2041 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
2042 return VC_DB_ERROR_NONE;
2045 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd, bool skip_invocation)
2047 int ret = __vc_db_insert_command(g_db_handle, pid, type, cmd, skip_invocation);
2049 SLOG(LOG_DEBUG, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
2054 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)
2056 GSList *iter = NULL;
2060 int count = g_slist_length(cmd_list);
2061 iter = g_slist_nth(cmd_list, 0);
2063 SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
2065 for (i = 0; i < count; i++) {
2069 temp_cmd = iter->data;
2071 if (NULL == temp_cmd) {
2072 SLOG(LOG_ERROR, vc_db_tag(), "command is NULL");
2076 if (type == temp_cmd->type) {
2077 if (NULL != invocation_name)
2078 temp_cmd->invocation_name = strdup(invocation_name);
2080 int ret = __vc_db_insert_command(db_handle, pid, type, temp_cmd, skip_invocation);
2081 if (ret != VC_DB_ERROR_NONE) {
2082 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
2086 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
2088 iter = g_slist_next(iter);
2091 return VC_DB_ERROR_NONE;
2094 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name, bool skip_invocation)
2096 __vc_db_reset_handle();
2097 __vc_db_begin_transaction(g_db_handle);
2099 int ret = __vc_db_insert_commands_list(g_db_handle, pid, type, cmd_list, invocation_name, false);
2100 if (ret != VC_DB_ERROR_NONE) {
2101 __vc_db_rollback_transaction(g_db_handle);
2105 __vc_db_commit_transaction(g_db_handle);
2107 return VC_DB_ERROR_NONE;
2110 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
2112 __vc_db_reset_handle();
2113 __vc_db_begin_transaction(g_db_handle);
2115 int ret = __vc_db_get_commands(g_db_handle, pid, type, cmd_list);
2116 if (ret != VC_DB_ERROR_NONE) {
2117 __vc_db_rollback_transaction(g_db_handle);
2121 __vc_db_commit_transaction(g_db_handle);
2122 return VC_DB_ERROR_NONE;
2125 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
2127 if (NULL == result_text) {
2128 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
2129 return VC_DB_ERROR_INVALID_PARAMETER;
2132 int ret = vc_db_delete_table(VC_RESULT_TABLE);
2134 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2136 if (NULL == vc_cmd_list) {
2137 __vc_db_reset_handle();
2138 __vc_db_begin_transaction(g_db_handle);
2139 int ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, NULL);
2140 if (ret != VC_DB_ERROR_NONE) {
2141 __vc_db_rollback_transaction(g_db_handle);
2144 __vc_db_commit_transaction(g_db_handle);
2145 return VC_DB_ERROR_NONE;
2148 /* Make client list node */
2149 vc_cmd_h vc_command = NULL;
2150 vc_cmd_list_first(vc_cmd_list);
2152 while (VC_ERROR_ITERATION_END != ret) {
2153 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
2154 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
2158 if (NULL == vc_command) {
2159 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
2163 vc_cmd_s* temp_cmd = NULL;
2164 temp_cmd = (vc_cmd_s*)vc_command;
2166 __vc_db_reset_handle();
2167 __vc_db_begin_transaction(g_db_handle);
2168 ret = __vc_db_insert_result(g_db_handle, result_text, event, msg, exclusive, temp_cmd);
2169 if (ret != VC_DB_ERROR_NONE) {
2170 __vc_db_rollback_transaction(g_db_handle);
2173 __vc_db_commit_transaction(g_db_handle);
2175 ret = vc_cmd_list_next(vc_cmd_list);
2178 return VC_DB_ERROR_NONE;
2181 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
2183 __vc_db_reset_handle();
2184 __vc_db_begin_transaction(g_db_handle);
2186 int ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
2187 if (ret != VC_DB_ERROR_NONE) {
2188 __vc_db_rollback_transaction(g_db_handle);
2189 return VC_DB_ERROR_OPERATION_FAILED;
2194 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
2195 if (ret != VC_DB_ERROR_NONE) {
2196 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
2200 // Get appid by pid using app control
2201 ret = app_manager_get_app_id(pid, &appid);
2202 if (APP_MANAGER_ERROR_NONE != ret) {
2203 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
2205 ret = __vc_db_get_result(g_db_handle, result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
2206 if (ret != VC_DB_ERROR_NONE) {
2207 __vc_db_rollback_transaction(g_db_handle);
2210 if (NULL != appid) {
2216 __vc_db_commit_transaction(g_db_handle);
2217 return VC_DB_ERROR_NONE;
2220 int vc_db_get_appid_list(const char* result, GSList** app_list)
2222 __vc_db_reset_handle();
2223 __vc_db_begin_transaction(g_db_handle);
2225 int ret = __vc_db_get_appid(g_db_handle, result, app_list);
2226 if (ret != VC_DB_ERROR_NONE) {
2227 __vc_db_rollback_transaction(g_db_handle);
2231 __vc_db_commit_transaction(g_db_handle);
2232 return VC_DB_ERROR_NONE;
2235 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
2237 __vc_db_reset_handle();
2238 __vc_db_begin_transaction(g_db_handle);
2240 int ret = __vc_db_get_result_pid_list(g_db_handle, result, pid_list);
2241 if (ret != VC_DB_ERROR_NONE) {
2242 __vc_db_rollback_transaction(g_db_handle);
2246 __vc_db_commit_transaction(g_db_handle);
2247 return VC_DB_ERROR_NONE;
2250 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
2252 __vc_db_reset_handle();
2253 __vc_db_begin_transaction(g_db_handle);
2255 int ret = __vc_db_append_commands(g_db_handle, pid, type, vc_cmd_list);
2256 if (ret != VC_DB_ERROR_NONE) {
2257 __vc_db_rollback_transaction(g_db_handle);
2261 __vc_db_commit_transaction(g_db_handle);
2262 return VC_DB_ERROR_NONE;
2265 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
2267 __vc_db_reset_handle();
2268 __vc_db_begin_transaction(g_db_handle);
2271 ret = __vc_db_delete_commands(g_db_handle, pid, type, appid);
2272 if (ret != VC_DB_ERROR_NONE) {
2273 __vc_db_rollback_transaction(g_db_handle);
2277 __vc_db_commit_transaction(g_db_handle);
2278 return VC_DB_ERROR_NONE;
2281 int vc_db_backup_command()
2283 GSList* list = NULL;
2285 __vc_db_reset_handle();
2286 __vc_db_begin_transaction(g_db_backup_handle);
2288 int ret = __vc_db_delete_table(g_db_backup_handle, VC_INFO_TABLE);
2290 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
2291 __vc_db_rollback_transaction(g_db_backup_handle);
2294 __vc_db_commit_transaction(g_db_backup_handle);
2296 ret = vc_db_get_commands(-1, VC_COMMAND_TYPE_BACKGROUND, &list);
2297 if (ret != VC_DB_ERROR_NONE) {
2298 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands");
2302 __vc_db_begin_transaction(g_db_backup_handle);
2304 ret = __vc_db_insert_commands_list(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2305 if (ret != VC_DB_ERROR_NONE) {
2306 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list to backup db");
2307 __vc_db_rollback_transaction(g_db_backup_handle);
2311 __vc_db_commit_transaction(g_db_backup_handle);
2313 SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Backup commands");
2314 return VC_DB_ERROR_NONE;
2317 int vc_db_restore_command()
2319 GSList* list = NULL;
2321 __vc_db_reset_handle();
2322 __vc_db_begin_transaction(g_db_backup_handle);
2324 int ret = __vc_db_get_commands(g_db_backup_handle, -1, VC_COMMAND_TYPE_BACKGROUND, &list);
2325 if (ret != VC_DB_ERROR_NONE) {
2326 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get commands from backup db");
2327 __vc_db_rollback_transaction(g_db_backup_handle);
2330 __vc_db_commit_transaction(g_db_backup_handle);
2332 ret = vc_db_insert_commands_list(-1, VC_COMMAND_TYPE_BACKGROUND, list, NULL, true);
2333 if (ret != VC_DB_ERROR_NONE) {
2334 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command list");
2338 SLOG(LOG_ERROR, vc_db_tag(), "[SUCCESS] Restore commands");
2339 return VC_DB_ERROR_NONE;