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* db_handle = NULL;
64 static int __vc_db_transaction(const char* transaction)
66 sqlite3_stmt* pStmt = NULL;
68 int ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
69 if (ret != SQLITE_OK) {
70 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_prepare_v2: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
71 return VC_DB_ERROR_OPERATION_FAILED;
74 if (sqlite3_step(pStmt) != SQLITE_DONE) {
75 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
76 sqlite3_finalize(pStmt);
77 return VC_DB_ERROR_OPERATION_FAILED;
80 sqlite3_finalize(pStmt);
81 return VC_DB_ERROR_NONE;
84 static int __vc_db_begin_transaction(void)
86 int ret = __vc_db_transaction("BEGIN TRANSACTION");
90 static int __vc_db_rollback_transaction(void)
92 int ret = __vc_db_transaction("ROLLBACK TRANSACTION");
96 static int __vc_db_commit_transaction(void)
98 int ret = __vc_db_transaction("COMMIT TRANSACTION");
102 static int __vc_db_exec_query(const char* sql)
104 char* err_msg = NULL;
106 int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
107 if (ret != SQLITE_OK) {
108 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
109 sqlite3_free(err_msg);
110 return VC_DB_ERROR_OPERATION_FAILED;
112 return VC_DB_ERROR_NONE;
115 static int __vc_db_delete_commands(int pid, vc_cmd_type_e type, const char* appid)
117 sqlite3_stmt* stmt = NULL;
120 if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid)
121 sql = strdup("DELETE FROM vc_info WHERE type = ?;");
122 else if (NULL != appid)
123 sql = strdup("DELETE FROM vc_info WHERE type = ? AND appid = ?;");
125 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
128 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
129 return VC_DB_ERROR_OUT_OF_MEMORY;
133 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
134 if (ret != SQLITE_OK) {
135 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
138 return VC_DB_ERROR_OPERATION_FAILED;
140 ret = sqlite3_bind_int(stmt, 1, (int)type);
141 if (ret != SQLITE_OK) {
142 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
143 sqlite3_finalize(stmt);
146 return VC_DB_ERROR_OPERATION_FAILED;
149 if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid) {
150 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d;", type);
151 } else if (NULL != appid) {
152 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
153 if (ret != SQLITE_OK) {
154 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
155 sqlite3_clear_bindings(stmt);
156 sqlite3_finalize(stmt);
159 return VC_DB_ERROR_OPERATION_FAILED;
161 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
163 ret = sqlite3_bind_int(stmt, 2, pid);
164 if (ret != SQLITE_OK) {
165 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
166 sqlite3_clear_bindings(stmt);
167 sqlite3_finalize(stmt);
170 return VC_DB_ERROR_OPERATION_FAILED;
172 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
175 ret = sqlite3_step(stmt);
176 if (ret != SQLITE_DONE) {
177 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned, sql(%s), ret(%d), err(%s)", sql, ret, sqlite3_errmsg(db_handle));
178 sqlite3_clear_bindings(stmt);
179 sqlite3_finalize(stmt);
182 return VC_DB_ERROR_OPERATION_FAILED;
186 sqlite3_clear_bindings(stmt);
187 sqlite3_finalize(stmt);
191 return VC_DB_ERROR_NONE;
194 static int __vc_db_insert_commands(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
196 SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
198 sqlite3_stmt* stmt = NULL;
199 const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
201 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
202 if (ret != SQLITE_OK) {
203 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
204 return VC_DB_ERROR_OPERATION_FAILED;
206 ret = sqlite3_bind_int(stmt, 2, pid);
207 if (ret != SQLITE_OK) {
208 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
209 sqlite3_finalize(stmt);
210 return VC_DB_ERROR_OPERATION_FAILED;
212 ret = sqlite3_bind_int(stmt, 3, cmd->type);
213 if (ret != SQLITE_OK) {
214 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
215 sqlite3_clear_bindings(stmt);
216 sqlite3_finalize(stmt);
217 return VC_DB_ERROR_OPERATION_FAILED;
219 ret = sqlite3_bind_int(stmt, 4, cmd->format);
220 if (ret != SQLITE_OK) {
221 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
222 sqlite3_clear_bindings(stmt);
223 sqlite3_finalize(stmt);
224 return VC_DB_ERROR_OPERATION_FAILED;
226 ret = sqlite3_bind_int(stmt, 5, cmd->domain);
227 if (ret != SQLITE_OK) {
228 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
229 sqlite3_clear_bindings(stmt);
230 sqlite3_finalize(stmt);
231 return VC_DB_ERROR_OPERATION_FAILED;
233 ret = sqlite3_bind_text(stmt, 6, cmd->command, -1, SQLITE_TRANSIENT);
234 if (ret != SQLITE_OK) {
235 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
236 sqlite3_clear_bindings(stmt);
237 sqlite3_finalize(stmt);
238 return VC_DB_ERROR_OPERATION_FAILED;
242 if (NULL == cmd->appid) {
243 // Get appid by pid using app control
244 ret = app_manager_get_app_id(pid, &appid);
245 if (APP_MANAGER_ERROR_NONE != ret) {
246 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
249 cmd->appid = strdup(appid);
253 ret = sqlite3_bind_text(stmt, 8, cmd->appid, -1, SQLITE_TRANSIENT);
254 if (ret != SQLITE_OK) {
255 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
256 sqlite3_clear_bindings(stmt);
257 sqlite3_finalize(stmt);
258 return VC_DB_ERROR_OPERATION_FAILED;
260 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
261 ret = sqlite3_bind_text(stmt, 9, cmd->invocation_name, -1, SQLITE_TRANSIENT);
262 if (ret != SQLITE_OK) {
263 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
264 sqlite3_clear_bindings(stmt);
265 sqlite3_finalize(stmt);
266 return VC_DB_ERROR_OPERATION_FAILED;
269 ret = sqlite3_bind_text(stmt, 10, cmd->fixed, -1, SQLITE_TRANSIENT);
270 if (ret != SQLITE_OK) {
271 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
272 sqlite3_clear_bindings(stmt);
273 sqlite3_finalize(stmt);
274 return VC_DB_ERROR_OPERATION_FAILED;
276 ret = sqlite3_step(stmt);
277 if (ret != SQLITE_DONE) {
278 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
279 sqlite3_clear_bindings(stmt);
280 sqlite3_finalize(stmt);
281 return VC_DB_ERROR_OPERATION_FAILED;
284 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);
285 SLOG(LOG_INFO, vc_db_tag(), "[SQL] @@ appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
288 sqlite3_clear_bindings(stmt);
289 sqlite3_finalize(stmt);
290 return VC_DB_ERROR_NONE;
293 static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
295 SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
298 sqlite3_stmt* stmt = NULL;
302 if (VC_COMMAND_TYPE_BACKGROUND == type) {
303 /* For background command */
304 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
306 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
310 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
311 return VC_DB_ERROR_OUT_OF_MEMORY;
313 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
314 if (ret != SQLITE_OK) {
315 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
318 return VC_DB_ERROR_OPERATION_FAILED;
321 ret = sqlite3_bind_int(stmt, 1, (int)type);
322 if (ret != SQLITE_OK) {
323 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
324 sqlite3_finalize(stmt);
327 return VC_DB_ERROR_OPERATION_FAILED;
329 if (VC_COMMAND_TYPE_BACKGROUND != type) {
330 ret = sqlite3_bind_int(stmt, 2, pid);
331 if (ret != SQLITE_OK) {
332 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
333 sqlite3_clear_bindings(stmt);
334 sqlite3_finalize(stmt);
337 return VC_DB_ERROR_OPERATION_FAILED;
341 if (VC_COMMAND_TYPE_BACKGROUND == type)
342 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
344 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
346 ret = sqlite3_step(stmt);
347 if (ret == SQLITE_DONE) {
348 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
349 sqlite3_clear_bindings(stmt);
350 sqlite3_finalize(stmt);
353 return VC_DB_ERROR_NONE;
356 if (VC_COMMAND_TYPE_BACKGROUND == type && -1 != pid) {
357 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
358 SLOG(LOG_WARN, vc_db_tag(), "[WARN] fail to get app id, pid(%d)", pid);
362 while (SQLITE_ROW == ret) {
365 char* temp_text = NULL;
367 ret = vc_cmd_create(&temp_cmd);
370 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
376 sqlite3_clear_bindings(stmt);
377 sqlite3_finalize(stmt);
380 return VC_DB_ERROR_OUT_OF_MEMORY;
383 temp_text = (char*)sqlite3_column_text(stmt, 7);
384 if (NULL != temp_text)
385 vc_cmd_set_appid(temp_cmd, temp_text);
387 ret = vc_cmd_get_appid(temp_cmd, &temp_text);
388 if (NULL != appid && 0 == ret) {
389 if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_text, strlen(appid))) {
390 SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
395 ret = sqlite3_step(stmt);
396 if (SQLITE_DONE == ret)
403 if (NULL != temp_text) {
408 temp = sqlite3_column_int(stmt, 0);
409 vc_cmd_set_id(temp_cmd, temp);
411 temp = sqlite3_column_int(stmt, 1);
412 vc_cmd_set_pid(temp_cmd, temp);
414 temp = sqlite3_column_int(stmt, 2);
415 vc_cmd_set_type(temp_cmd, temp);
417 temp = sqlite3_column_int(stmt, 3);
418 vc_cmd_set_format(temp_cmd, temp);
420 temp = sqlite3_column_int(stmt, 4);
421 vc_cmd_set_domain(temp_cmd, temp);
423 temp_text = (char*)sqlite3_column_text(stmt, 5);
424 if (NULL != temp_text)
425 vc_cmd_set_command(temp_cmd, temp_text);
427 temp_text = (char*)sqlite3_column_text(stmt, 6);
428 if (NULL != temp_text)
429 vc_cmd_set_unfixed_command(temp_cmd, temp_text);
431 temp_text = (char*)sqlite3_column_text(stmt, 8);
432 if (NULL != temp_text)
433 vc_cmd_set_invocation_name(temp_cmd, temp_text);
435 temp_text = (char*)sqlite3_column_text(stmt, 9);
436 if (NULL != temp_text)
437 vc_cmd_set_fixed_command(temp_cmd, temp_text);
439 ret = vc_cmd_get_type(temp_cmd, &temp);
440 if (0 == ret && type == temp) {
441 *cmd_list = g_slist_append(*cmd_list, temp_cmd);
443 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp, type);
445 vc_cmd_destroy((vc_cmd_h)temp_cmd);
447 ret = sqlite3_step(stmt);
448 if (SQLITE_DONE == ret)
458 sqlite3_clear_bindings(stmt);
459 sqlite3_finalize(stmt);
463 return VC_DB_ERROR_NONE;
466 static int __vc_db_get_pid(const char* appid, int* pid)
468 bool running = false;
469 int ret = app_manager_is_running(appid, &running);
470 if (APP_MANAGER_ERROR_NONE != ret) {
471 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to check running with appid(%s)", appid);
472 return VC_DB_ERROR_OPERATION_FAILED;
474 if (true == running) {
475 app_context_h app_context = NULL;
476 ret = app_manager_get_app_context(appid, &app_context);
477 if (APP_MANAGER_ERROR_NONE != ret) {
478 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
479 return VC_DB_ERROR_OPERATION_FAILED;
482 ret = app_context_get_pid(app_context, pid);
483 if (APP_MANAGER_ERROR_NONE != ret) {
484 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get pid, ret(%d), appid(%s)", ret, appid);
485 return VC_DB_ERROR_OPERATION_FAILED;
488 ret = app_context_destroy(app_context);
489 if (APP_MANAGER_ERROR_NONE != ret) {
490 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to destroy app context, ret(%d), appid(%s)", ret, appid);
491 return VC_DB_ERROR_OPERATION_FAILED;
494 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
496 return VC_DB_ERROR_NONE;
499 static int __vc_db_insert_result(const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
501 SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
503 sqlite3_stmt* stmt = NULL;
504 const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
506 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
507 if (ret != SQLITE_OK) {
508 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
509 return VC_DB_ERROR_OPERATION_FAILED;
511 ret = sqlite3_bind_text(stmt, 2, result_text, -1, SQLITE_TRANSIENT);
512 if (ret != SQLITE_OK) {
513 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
514 sqlite3_finalize(stmt);
515 return VC_DB_ERROR_OPERATION_FAILED;
517 ret = sqlite3_bind_int(stmt, 3, event);
518 if (ret != SQLITE_OK) {
519 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
520 sqlite3_clear_bindings(stmt);
521 sqlite3_finalize(stmt);
522 return VC_DB_ERROR_OPERATION_FAILED;
524 ret = sqlite3_bind_text(stmt, 4, msg, -1, SQLITE_TRANSIENT);
525 if (ret != SQLITE_OK) {
526 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
527 sqlite3_clear_bindings(stmt);
528 sqlite3_finalize(stmt);
529 return VC_DB_ERROR_OPERATION_FAILED;
531 ret = sqlite3_bind_int(stmt, 5, exclusive);
532 if (ret != SQLITE_OK) {
533 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
534 sqlite3_clear_bindings(stmt);
535 sqlite3_finalize(stmt);
536 return VC_DB_ERROR_OPERATION_FAILED;
540 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d))", result_text, event, msg, exclusive);
542 ret = sqlite3_step(stmt);
543 if (ret != SQLITE_DONE) {
544 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
545 sqlite3_clear_bindings(stmt);
546 sqlite3_finalize(stmt);
547 return VC_DB_ERROR_OPERATION_FAILED;
550 sqlite3_clear_bindings(stmt);
551 sqlite3_finalize(stmt);
552 return VC_DB_ERROR_NONE;
555 if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
557 ret = __vc_db_get_pid(cmd->appid, &pid);
559 SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
561 SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
566 ret = sqlite3_bind_int(stmt, 6, cmd->pid);
567 if (ret != SQLITE_OK) {
568 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
569 sqlite3_clear_bindings(stmt);
570 sqlite3_finalize(stmt);
571 return VC_DB_ERROR_OPERATION_FAILED;
573 ret = sqlite3_bind_int(stmt, 7, cmd->type);
574 if (ret != SQLITE_OK) {
575 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
576 sqlite3_clear_bindings(stmt);
577 sqlite3_finalize(stmt);
578 return VC_DB_ERROR_OPERATION_FAILED;
580 ret = sqlite3_bind_int(stmt, 8, cmd->format);
581 if (ret != SQLITE_OK) {
582 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
583 sqlite3_clear_bindings(stmt);
584 sqlite3_finalize(stmt);
585 return VC_DB_ERROR_OPERATION_FAILED;
587 ret = sqlite3_bind_int(stmt, 9, cmd->domain);
588 if (ret != SQLITE_OK) {
589 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
590 sqlite3_clear_bindings(stmt);
591 sqlite3_finalize(stmt);
592 return VC_DB_ERROR_OPERATION_FAILED;
594 ret = sqlite3_bind_text(stmt, 10, cmd->command, -1, SQLITE_TRANSIENT);
595 if (ret != SQLITE_OK) {
596 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
597 sqlite3_clear_bindings(stmt);
598 sqlite3_finalize(stmt);
599 return VC_DB_ERROR_OPERATION_FAILED;
601 ret = sqlite3_bind_text(stmt, 11, cmd->parameter, -1, SQLITE_TRANSIENT);
602 if (ret != SQLITE_OK) {
603 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
604 sqlite3_clear_bindings(stmt);
605 sqlite3_finalize(stmt);
606 return VC_DB_ERROR_OPERATION_FAILED;
608 ret = sqlite3_bind_text(stmt, 12, cmd->appid, -1, SQLITE_TRANSIENT);
609 if (ret != SQLITE_OK) {
610 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
611 sqlite3_clear_bindings(stmt);
612 sqlite3_finalize(stmt);
613 return VC_DB_ERROR_OPERATION_FAILED;
615 ret = sqlite3_bind_text(stmt, 13, cmd->invocation_name, -1, SQLITE_TRANSIENT);
616 if (ret != SQLITE_OK) {
617 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
618 sqlite3_clear_bindings(stmt);
619 sqlite3_finalize(stmt);
620 return VC_DB_ERROR_OPERATION_FAILED;
622 ret = sqlite3_bind_text(stmt, 14, cmd->fixed, -1, SQLITE_TRANSIENT);
623 if (ret != SQLITE_OK) {
624 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
625 sqlite3_clear_bindings(stmt);
626 sqlite3_finalize(stmt);
627 return VC_DB_ERROR_OPERATION_FAILED;
629 ret = sqlite3_step(stmt);
630 if (ret != SQLITE_DONE) {
631 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
632 sqlite3_clear_bindings(stmt);
633 sqlite3_finalize(stmt);
634 return VC_DB_ERROR_OPERATION_FAILED;
637 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d), pid(%d), type(%d), format(%d), domain(%d), command(%s), parameter(%s), appid(%s), invocation(%s), fixed(%s)",
638 result_text, event, msg, exclusive, cmd->pid, cmd->type, cmd->format, cmd->domain, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
641 sqlite3_clear_bindings(stmt);
642 sqlite3_finalize(stmt);
644 return VC_DB_ERROR_NONE;
647 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
649 if (NULL == org_cmd || NULL == new_cmd) {
650 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
651 return VC_DB_ERROR_INVALID_PARAMETER;
654 if (NULL == invocation_name) {
655 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, invocation name is NULL, org_cmd(%s)", org_cmd);
656 return VC_DB_ERROR_INVALID_PARAMETER;
659 if (strlen(org_cmd) <= strlen(invocation_name)) {
660 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
661 return VC_DB_ERROR_INVALID_PARAMETER;
664 if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
665 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
667 SLOG(LOG_DEBUG, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, *new_cmd, invocation_name);
668 return VC_DB_ERROR_NONE;
671 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
673 if (NULL == command || NULL == temp_unfixed) {
674 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
675 return VC_DB_ERROR_INVALID_PARAMETER;
679 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, fixed cmd is NULL, org_cmd(%s)", command);
680 return VC_DB_ERROR_INVALID_PARAMETER;
683 if (strlen(command) <= strlen(fixed)) {
684 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to extract unfixed command, cmd(%s) fixed(%s)", command, fixed);
685 return VC_DB_ERROR_INVALID_PARAMETER;
688 char* temp = (char*)calloc(256, sizeof(char));
689 if (0 == strncasecmp(command, fixed, strlen(fixed))) {
690 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
691 SLOG(LOG_WARN, vc_db_tag(), "@@@");
692 } else if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
693 strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
694 SLOG(LOG_WARN, vc_db_tag(), "@@@");
697 SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
699 *temp_unfixed = strdup(temp);
703 return VC_DB_ERROR_NONE;
706 static int __vc_db_get_result(char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
709 sqlite3_stmt* stmt = NULL;
712 sql = strdup("SELECT * FROM vc_result;");
713 else if (NULL != appid)
714 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
716 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
719 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
720 return VC_DB_ERROR_OUT_OF_MEMORY;
723 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
724 if (ret != SQLITE_OK) {
725 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
728 return VC_DB_ERROR_OPERATION_FAILED;
731 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
732 if (ret != SQLITE_OK) {
733 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
734 sqlite3_finalize(stmt);
737 return VC_DB_ERROR_OPERATION_FAILED;
739 } else if (-1 != pid) {
740 ret = sqlite3_bind_int(stmt, 1, pid);
741 if (ret != SQLITE_OK) {
742 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
743 sqlite3_finalize(stmt);
746 return VC_DB_ERROR_OPERATION_FAILED;
749 ret = sqlite3_step(stmt);
750 if (ret == SQLITE_DONE) {
751 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
752 sqlite3_clear_bindings(stmt);
753 sqlite3_finalize(stmt);
756 return VC_DB_ERROR_NONE;
760 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
761 else if (NULL != appid)
762 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %s;", appid);
765 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
767 vc_cmd_h temp_cmd = NULL;
768 while (SQLITE_ROW == ret) {
770 char* temp_text = NULL;
771 const char* invocation_name = NULL;
773 temp_text = (char*)sqlite3_column_text(stmt, 1);
774 if (NULL != temp_text)
775 *result_text = strdup(temp_text);
777 temp = sqlite3_column_int(stmt, 2);
781 temp_text = (char*)sqlite3_column_text(stmt, 3);
782 if (NULL != temp_text)
783 *msg = strdup(temp_text);
786 if (0 != vc_cmd_create(&temp_cmd)) {
787 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
788 if (NULL != *result_text) {
793 if (NULL != msg && NULL != *msg) {
799 sqlite3_clear_bindings(stmt);
800 sqlite3_finalize(stmt);
806 temp = sqlite3_column_int(stmt, 0);
807 vc_cmd_set_id(temp_cmd, temp);
809 temp = sqlite3_column_int(stmt, 5);
810 vc_cmd_set_pid(temp_cmd, temp);
812 temp = sqlite3_column_int(stmt, 6);
813 vc_cmd_set_type(temp_cmd, temp);
815 temp = sqlite3_column_int(stmt, 7);
816 vc_cmd_set_format(temp_cmd, temp);
818 temp = sqlite3_column_int(stmt, 8);
819 vc_cmd_set_domain(temp_cmd, temp);
822 invocation_name = (char*)sqlite3_column_text(stmt, 12);
824 temp_text = (char*)sqlite3_column_text(stmt, 9);
825 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
827 if (NULL != temp_text) {
828 char* temp_command = NULL;
829 char* temp_unfixed = NULL;
830 char* temp_fixed = NULL;
832 if (NULL != invocation_name)
833 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
835 if (NULL == temp_command) {
836 temp_command = strdup(temp_text);
838 // remove invocation name from result_text
840 *result_text = strdup(temp_command);
844 temp_fixed = (char*)sqlite3_column_text(stmt, 13);
845 if (NULL != temp_fixed)
846 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
849 temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
851 if (NULL != temp_unfixed) {
852 char merge_result[256] = {0, };
853 vc_cmd_set_command(temp_cmd, temp_command);
854 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
855 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
856 if (NULL != *result_text)
858 *result_text = strdup(merge_result);
859 } else if (NULL != temp_fixed) {
860 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
861 vc_cmd_set_command(temp_cmd, temp_fixed);
862 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
863 if (NULL != temp_unfixed) {
868 vc_cmd_set_command(temp_cmd, temp_command);
874 temp_text = (char*)sqlite3_column_text(stmt, 11);
875 if (NULL != temp_text)
876 vc_cmd_set_appid(temp_cmd, temp_text);
878 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
879 SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
880 vc_cmd_destroy(temp_cmd);
881 vc_cmd_list_destroy(vc_cmd_list, true);
882 if (NULL != *result_text) {
887 if (NULL != msg && NULL != *msg) {
893 sqlite3_clear_bindings(stmt);
894 sqlite3_finalize(stmt);
897 return VC_DB_ERROR_OPERATION_FAILED;
900 ret = sqlite3_step(stmt);
901 if (SQLITE_DONE == ret)
906 sqlite3_clear_bindings(stmt);
907 sqlite3_finalize(stmt);
911 return VC_DB_ERROR_NONE;
914 void __vc_db_demandable_client_free(void* data)
916 vc_deactivated_app_s* d_app = (vc_deactivated_app_s*)data;
919 if (NULL != d_app->appid) {
928 static int __vc_db_get_appid(const char* result, GSList** app_list)
930 GSList* temp_app_list = NULL;
933 sqlite3_stmt* stmt = NULL;
934 const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
936 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
937 if (ret != SQLITE_OK) {
938 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
939 return VC_DB_ERROR_OPERATION_FAILED;
941 ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
942 if (ret != SQLITE_OK) {
943 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
944 sqlite3_finalize(stmt);
945 return VC_DB_ERROR_OPERATION_FAILED;
947 ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
948 if (ret != SQLITE_OK) {
949 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
950 sqlite3_clear_bindings(stmt);
951 sqlite3_finalize(stmt);
952 return VC_DB_ERROR_OPERATION_FAILED;
954 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
955 ret = sqlite3_step(stmt);
956 if (ret == SQLITE_DONE) {
957 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
958 sqlite3_clear_bindings(stmt);
959 sqlite3_finalize(stmt);
960 return VC_DB_ERROR_NONE;
963 while (SQLITE_ROW == ret) {
964 char* temp_text = NULL;
965 vc_deactivated_app_s* temp_app = NULL;
966 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
967 if (NULL == temp_app) {
968 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
970 if (NULL != temp_app_list) {
971 g_slist_free_full(temp_app_list, __vc_db_demandable_client_free);
972 temp_app_list = NULL;
976 sqlite3_clear_bindings(stmt);
977 sqlite3_finalize(stmt);
978 return VC_DB_ERROR_OUT_OF_MEMORY;
981 temp_text = (char*)sqlite3_column_text(stmt, 11);
982 if (NULL != temp_text)
983 temp_app->appid = strdup(temp_text);
985 temp_app_list = g_slist_append(temp_app_list, temp_app);
987 ret = sqlite3_step(stmt);
988 if (SQLITE_DONE == ret)
992 *app_list = temp_app_list;
995 sqlite3_clear_bindings(stmt);
996 sqlite3_finalize(stmt);
997 return VC_DB_ERROR_NONE;
1000 int __vc_db_get_result_pid_list(const char* result, GSList** pid_list)
1002 GSList* temp_pid_list = NULL;
1005 sqlite3_stmt* stmt = NULL;
1006 const char* sql = "SELECT DISTINCT pid, type FROM vc_result WHERE result = ? COLLATE NOCASE ORDER BY pid ASC;";
1009 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1010 if (ret != SQLITE_OK) {
1011 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1012 return VC_DB_ERROR_OPERATION_FAILED;
1014 ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
1015 if (ret != SQLITE_OK) {
1016 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1017 sqlite3_finalize(stmt);
1018 return VC_DB_ERROR_OPERATION_FAILED;
1020 ret = sqlite3_step(stmt);
1021 if (ret == SQLITE_DONE) {
1022 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1023 sqlite3_clear_bindings(stmt);
1024 sqlite3_finalize(stmt);
1025 return VC_DB_ERROR_NONE;
1028 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
1030 while (SQLITE_ROW == ret) {
1032 vc_cmd_s* temp_cmd = NULL;
1033 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
1034 if (NULL == temp_cmd) {
1035 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
1037 if (NULL != temp_pid_list) {
1038 g_slist_free_full(temp_pid_list, free);
1039 temp_pid_list = NULL;
1042 sqlite3_reset(stmt);
1043 sqlite3_clear_bindings(stmt);
1044 sqlite3_finalize(stmt);
1045 return VC_DB_ERROR_OUT_OF_MEMORY;
1048 temp = sqlite3_column_int(stmt, 0);
1049 temp_cmd->pid = temp;
1051 temp = sqlite3_column_int(stmt, 1);
1052 temp_cmd->type = temp;
1054 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
1056 ret = sqlite3_step(stmt);
1057 if (SQLITE_DONE == ret)
1061 *pid_list = temp_pid_list;
1063 sqlite3_reset(stmt);
1064 sqlite3_clear_bindings(stmt);
1065 sqlite3_finalize(stmt);
1066 return VC_DB_ERROR_NONE;
1069 static int __vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
1071 SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
1074 sqlite3_stmt* stmt = NULL;
1075 const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
1077 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
1078 if (ret != SQLITE_OK) {
1079 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
1080 return VC_DB_ERROR_OPERATION_FAILED;
1082 ret = sqlite3_bind_int(stmt, 1, pid);
1083 if (ret != SQLITE_OK) {
1084 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1085 sqlite3_finalize(stmt);
1086 return VC_DB_ERROR_OPERATION_FAILED;
1088 ret = sqlite3_bind_int(stmt, 2, type);
1089 if (ret != SQLITE_OK) {
1090 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
1091 sqlite3_clear_bindings(stmt);
1092 sqlite3_finalize(stmt);
1093 return VC_DB_ERROR_OPERATION_FAILED;
1095 ret = sqlite3_step(stmt);
1096 if (ret == SQLITE_DONE) {
1097 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
1098 sqlite3_clear_bindings(stmt);
1099 sqlite3_finalize(stmt);
1100 return VC_DB_ERROR_NONE;
1103 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
1105 vc_cmd_h temp_cmd = NULL;
1107 while (SQLITE_ROW == ret) {
1109 char* temp_text = NULL;
1111 if (0 != vc_cmd_create(&temp_cmd)) {
1112 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
1113 sqlite3_reset(stmt);
1114 sqlite3_clear_bindings(stmt);
1115 sqlite3_finalize(stmt);
1118 temp = sqlite3_column_int(stmt, 0);
1119 vc_cmd_set_id(temp_cmd, temp);
1121 temp = sqlite3_column_int(stmt, 1);
1122 vc_cmd_set_pid(temp_cmd, temp);
1124 temp = sqlite3_column_int(stmt, 2);
1125 vc_cmd_set_type(temp_cmd, temp);
1127 temp = sqlite3_column_int(stmt, 3);
1128 vc_cmd_set_format(temp_cmd, temp);
1130 temp = sqlite3_column_int(stmt, 4);
1131 vc_cmd_set_domain(temp_cmd, temp);
1133 temp_text = (char*)sqlite3_column_text(stmt, 5);
1134 if (NULL != temp_text)
1135 vc_cmd_set_command(temp_cmd, temp_text);
1137 temp_text = (char*)sqlite3_column_text(stmt, 6);
1138 if (NULL != temp_text)
1139 vc_cmd_set_unfixed_command(temp_cmd, temp_text);
1141 temp_text = (char*)sqlite3_column_text(stmt, 7);
1142 if (NULL != temp_text)
1143 vc_cmd_set_appid(temp_cmd, temp_text);
1145 temp_text = (char*)sqlite3_column_text(stmt, 8);
1146 if (NULL != temp_text)
1147 vc_cmd_set_invocation_name(temp_cmd, temp_text);
1149 temp_text = (char*)sqlite3_column_text(stmt, 9);
1150 if (NULL != temp_text)
1151 vc_cmd_set_fixed_command(temp_cmd, temp_text);
1153 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
1154 SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
1155 vc_cmd_destroy(temp_cmd);
1156 vc_cmd_list_destroy(vc_cmd_list, true);
1157 sqlite3_reset(stmt);
1158 sqlite3_clear_bindings(stmt);
1159 sqlite3_finalize(stmt);
1160 return VC_DB_ERROR_OPERATION_FAILED;
1163 ret = sqlite3_step(stmt);
1164 if (SQLITE_DONE == ret)
1168 sqlite3_reset(stmt);
1169 sqlite3_clear_bindings(stmt);
1170 sqlite3_finalize(stmt);
1171 return VC_DB_ERROR_NONE;
1174 static vc_cmd_s* __vc_db_command_copy(vc_cmd_s* src_cmd)
1176 if (NULL == src_cmd) {
1177 SLOG(LOG_WARN, vc_db_tag(), "[Client Data] Input command is NULL");
1181 vc_cmd_s* temp_cmd = NULL;
1182 temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
1183 if (NULL == temp_cmd) {
1184 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1188 temp_cmd->id = src_cmd->id;
1189 temp_cmd->pid = src_cmd->pid;
1190 temp_cmd->index = src_cmd->index;
1191 temp_cmd->type = src_cmd->type;
1192 temp_cmd->format = src_cmd->format;
1193 temp_cmd->domain = src_cmd->domain;
1195 if (VC_COMMAND_TYPE_SYSTEM == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM;
1196 else if (VC_COMMAND_TYPE_EXCLUSIVE == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_EXCLUSIVE;
1197 else if (VC_COMMAND_TYPE_FOREGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1198 else if (VC_COMMAND_TYPE_WIDGET == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_FOREGROUND;
1199 else if (VC_COMMAND_TYPE_SYSTEM_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_SYSTEM_BACKGROUND;
1200 else if (VC_COMMAND_TYPE_BACKGROUND == temp_cmd->type) temp_cmd->priority = VC_COMMAND_PRIORITY_BACKGROUND;
1202 if (NULL != src_cmd->command) {
1203 temp_cmd->command = strdup(src_cmd->command);
1206 if (NULL != src_cmd->parameter) {
1207 temp_cmd->parameter = strdup(src_cmd->parameter);
1210 if (NULL != src_cmd->appid) {
1211 temp_cmd->appid = strdup(src_cmd->appid);
1214 if (NULL != src_cmd->invocation_name) {
1215 temp_cmd->invocation_name = strdup(src_cmd->invocation_name);
1218 if (NULL != src_cmd->fixed) {
1219 temp_cmd->fixed = strdup(src_cmd->fixed);
1222 temp_cmd->key = src_cmd->key;
1223 temp_cmd->modifier = src_cmd->modifier;
1228 int vc_db_initialize(void)
1230 SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
1232 if (0 < g_ref_cnt) {
1234 return VC_DB_ERROR_NONE;
1237 path = (char*)calloc(256, sizeof(char));
1239 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1240 return VC_DB_ERROR_OUT_OF_MEMORY;
1243 /* This should be changed to general DB space - TZ_USER_DB */
1244 snprintf(path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1247 int ret = db_util_open(path, &db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1248 if (ret != SQLITE_OK) {
1249 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", path, ret, sqlite3_errmsg(db_handle));
1251 db_util_close(db_handle);
1257 return VC_DB_ERROR_OPERATION_FAILED;
1260 if (lstat(path, &stat) < 0) {
1262 SLOG(LOG_ERROR, vc_db_tag(), "%d", strerror_r(errno, buf_err, sizeof(buf_err)));
1264 db_util_close(db_handle);
1269 return VC_DB_ERROR_OPERATION_FAILED;
1272 if (!S_ISREG(stat.st_mode)) {
1273 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1275 db_util_close(db_handle);
1280 return VC_DB_ERROR_OPERATION_FAILED;
1283 if (!stat.st_size) {
1284 vc_db_create_table();
1288 char* err_msg = NULL;
1289 static const const char* sql = "PRAGMA journal_mode = WAL";
1290 int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
1291 if (ret != SQLITE_OK) {
1292 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1296 return VC_DB_ERROR_NONE;
1299 int vc_db_finalize(void)
1301 if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
1302 if (0 != --g_ref_cnt)
1303 return VC_DB_ERROR_NONE;
1313 db_util_close(db_handle);
1316 return VC_DB_ERROR_NONE;
1319 int vc_db_create_table(void)
1321 __vc_db_begin_transaction();
1323 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, \
1324 command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1325 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,\
1326 pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1328 int ret = __vc_db_exec_query(vc_info_sql);
1329 if (ret != VC_DB_ERROR_NONE) {
1330 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1331 __vc_db_rollback_transaction();
1332 return VC_DB_ERROR_OPERATION_FAILED;
1334 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_info_sql);
1336 ret = __vc_db_exec_query(vc_result_sql);
1337 if (ret != VC_DB_ERROR_NONE) {
1338 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1339 __vc_db_rollback_transaction();
1342 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_result_sql);
1344 __vc_db_commit_transaction();
1345 return VC_DB_ERROR_NONE;
1348 int vc_db_delete_table(const char* table)
1351 if (0 == strncmp(table, "result", strlen(table))) {
1352 sql = strdup("DELETE FROM vc_result;");
1353 } else if (0 == strncmp(table, "command", strlen(table))) {
1354 sql = strdup("DELETE_FROM vc_info;");
1356 return VC_DB_ERROR_INVALID_PARAMETER;
1360 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1361 return VC_DB_ERROR_OUT_OF_MEMORY;
1364 __vc_db_begin_transaction();
1366 int ret = __vc_db_exec_query(sql);
1367 if (ret != VC_DB_ERROR_NONE) {
1368 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1369 __vc_db_rollback_transaction();
1378 if (0 == strncmp(table, "result", strlen(table))) {
1379 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1380 } else if (0 == strncmp(table, "command", strlen(table))) {
1381 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1385 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1386 return VC_DB_ERROR_OUT_OF_MEMORY;
1389 ret = __vc_db_exec_query(sql);
1390 if (ret != VC_DB_ERROR_NONE) {
1391 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1392 __vc_db_rollback_transaction();
1398 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1399 __vc_db_commit_transaction();
1403 return VC_DB_ERROR_NONE;
1406 int vc_db_begin_transaction(void)
1408 int ret = __vc_db_begin_transaction();
1412 int vc_db_rollback_transaction(void)
1414 int ret = __vc_db_rollback_transaction();
1418 int vc_db_commit_transaction(void)
1420 int ret = __vc_db_commit_transaction();
1424 static void __vc_db_remove_space(char** string)
1426 if (NULL == string || NULL == *string)
1429 char* temp = *string;
1431 //remove previous space
1433 strncpy(temp, temp + 1, strlen(temp));
1435 // remove next space
1436 if (' ' == temp[strlen(temp) - 1])
1437 temp[strlen(temp) - 1] = '\0';
1440 static bool __vc_db_is_valid_vfixed_string(char* string)
1442 char* temp = strchr(string, '}');
1446 temp = strchr(string, '{');
1450 temp = strchr(string, '|');
1456 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1458 if (NULL == cmd || NULL == cmd->command) {
1459 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
1460 return VC_DB_ERROR_INVALID_PARAMETER;
1463 GSList* temp_list = NULL;
1465 char* src_cmd = strdup(cmd->command);
1466 char* dst_cmd = NULL;
1467 char merge_cmd[256] = {0, };
1469 if (NULL == src_cmd) {
1470 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1471 return VC_DB_ERROR_OUT_OF_MEMORY;
1474 if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
1475 // check string validation
1476 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1477 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1480 return VC_DB_ERROR_INVALID_PARAMETER;
1483 // remove close brace, '}'
1484 char* temp_close = strchr(src_cmd, '}');
1485 if (NULL != temp_close) {
1486 temp_close[0] = '\0';
1489 // extract fixed command and remove space in front of '{'
1490 char *tok_ptr = NULL;
1491 temp = strtok_r(src_cmd, "{", &tok_ptr);
1492 __vc_db_remove_space(&temp);
1493 *fixed_cmd = strdup(temp);
1495 // merge command with fixed and vfixed
1496 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1497 __vc_db_remove_space(&temp);
1499 snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
1500 dst_cmd = strdup(merge_cmd);
1501 temp_list = g_slist_append(temp_list, dst_cmd);
1502 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1504 } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
1505 // check string validation
1506 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1507 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1510 return VC_DB_ERROR_INVALID_PARAMETER;
1513 // extract fixed command
1514 char* temp_fixed = strchr(src_cmd, '}') + 1;
1515 __vc_db_remove_space(&temp_fixed);
1516 *fixed_cmd = strdup(temp_fixed);
1518 // remove close brace, '}'
1519 char *tok_ptr = NULL;
1520 temp = strtok_r(src_cmd, "}", &tok_ptr);
1522 // remove open brace, '{'
1523 temp = strchr(src_cmd, '{') + 1;
1526 temp = strtok_r(temp, "|", &tok_ptr);
1527 __vc_db_remove_space(&temp);
1529 // merge command with fixed and vfixed
1530 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1531 dst_cmd = strdup(merge_cmd);
1532 temp_list = g_slist_append(temp_list, dst_cmd);
1533 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1535 while (NULL != (temp = strtok_r(NULL, "|", &tok_ptr))) {
1536 __vc_db_remove_space(&temp);
1538 // merge command with fixed and vfixed
1539 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1540 dst_cmd = strdup(merge_cmd);
1541 temp_list = g_slist_append(temp_list, dst_cmd);
1542 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1544 } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
1545 dst_cmd = strdup(src_cmd);
1546 temp_list = g_slist_append(temp_list, dst_cmd);
1547 *fixed_cmd = strdup(src_cmd);
1550 dst_cmd = strdup(src_cmd);
1551 temp_list = g_slist_append(temp_list, dst_cmd);
1554 *cmd_list = temp_list;
1558 return VC_DB_ERROR_NONE;
1561 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
1563 GSList* cmd_list = NULL;
1564 char* fixed_cmd = NULL;
1565 vc_cmd_s* tmp_cmd = __vc_db_command_copy(cmd);
1567 int ret = __vc_db_generate_command(tmp_cmd, &fixed_cmd, &cmd_list);
1569 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
1571 if (NULL != tmp_cmd) {
1572 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1577 if (0 != g_slist_length(cmd_list)) {
1578 GSList *iter = NULL;
1579 char* temp_command = NULL;
1580 iter = g_slist_nth(cmd_list, 0);
1582 while (NULL != iter) {
1583 temp_command = iter->data;
1585 if (NULL != temp_command) {
1586 if (NULL != tmp_cmd->command) {
1587 free(tmp_cmd->command);
1588 tmp_cmd->command = NULL;
1590 tmp_cmd->command = strdup(temp_command);
1591 if (NULL != fixed_cmd)
1592 tmp_cmd->fixed = strdup(fixed_cmd);
1594 tmp_cmd->fixed = NULL;
1596 ret = __vc_db_insert_commands(pid, type, tmp_cmd);
1597 if (ret != VC_DB_ERROR_NONE) {
1598 if (NULL != fixed_cmd) {
1602 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1606 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != tmp_cmd->invocation_name) {
1607 char temp[256] = {0, };
1608 snprintf(temp, 256, "%s %s", tmp_cmd->invocation_name, tmp_cmd->command);
1609 if (NULL != tmp_cmd->command)
1610 free(tmp_cmd->command);
1612 tmp_cmd->command = strdup(temp);
1614 ret = __vc_db_insert_commands(pid, type, tmp_cmd);
1615 if (ret != VC_DB_ERROR_NONE) {
1616 if (NULL != fixed_cmd) {
1620 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1624 cmd_list = g_slist_remove(cmd_list, temp_command);
1626 temp_command = NULL;
1628 iter = g_slist_nth(cmd_list, 0);
1631 if (VC_DB_ERROR_NONE != ret) {
1632 while (NULL != iter) {
1633 temp_command = iter->data;
1635 if (NULL != temp_command) {
1636 cmd_list = g_slist_remove(cmd_list, temp_command);
1638 temp_command = NULL;
1641 iter = g_slist_nth(cmd_list, 0);
1644 g_slist_free(cmd_list);
1645 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to insert command, %d", ret);
1652 if (NULL != fixed_cmd) {
1656 vc_cmd_destroy((vc_cmd_h)tmp_cmd);
1657 return VC_DB_ERROR_NONE;
1660 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name)
1662 GSList *iter = NULL;
1666 int count = g_slist_length(cmd_list);
1667 iter = g_slist_nth(cmd_list, 0);
1669 SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
1671 __vc_db_begin_transaction();
1673 for (i = 0; i < count; i++) {
1677 temp_cmd = iter->data;
1679 if (NULL == temp_cmd) {
1680 SLOG(LOG_ERROR, vc_db_tag(), "comamnd is NULL");
1684 if (type == temp_cmd->type) {
1685 if (NULL != invocation_name)
1686 temp_cmd->invocation_name = strdup(invocation_name);
1688 int ret = vc_db_insert_command(pid, type, temp_cmd);
1689 if (ret != VC_DB_ERROR_NONE) {
1690 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
1691 __vc_db_rollback_transaction();
1695 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
1697 iter = g_slist_next(iter);
1700 __vc_db_commit_transaction();
1702 return VC_DB_ERROR_NONE;
1705 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
1707 __vc_db_begin_transaction();
1709 int ret = __vc_db_get_commands(pid, type, cmd_list);
1710 if (ret != VC_DB_ERROR_NONE) {
1711 __vc_db_rollback_transaction();
1715 __vc_db_commit_transaction();
1716 return VC_DB_ERROR_NONE;
1719 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
1721 if (NULL == result_text) {
1722 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
1723 return VC_DB_ERROR_INVALID_PARAMETER;
1726 int ret = vc_db_delete_table("result");
1728 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1730 if (NULL == vc_cmd_list) {
1731 __vc_db_begin_transaction();
1732 int ret = __vc_db_insert_result(result_text, event, msg, exclusive, NULL);
1733 if (ret != VC_DB_ERROR_NONE) {
1734 __vc_db_rollback_transaction();
1737 __vc_db_commit_transaction();
1738 return VC_DB_ERROR_NONE;
1741 /* Make client list node */
1742 vc_cmd_h vc_command = NULL;
1743 vc_cmd_list_first(vc_cmd_list);
1745 while (VC_ERROR_ITERATION_END != ret) {
1746 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
1747 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
1751 if (NULL == vc_command) {
1752 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
1756 vc_cmd_s* temp_cmd = NULL;
1757 temp_cmd = (vc_cmd_s*)vc_command;
1759 __vc_db_begin_transaction();
1760 ret = __vc_db_insert_result(result_text, event, msg, exclusive, temp_cmd);
1761 if (ret != VC_DB_ERROR_NONE) {
1762 __vc_db_rollback_transaction();
1765 __vc_db_commit_transaction();
1767 ret = vc_cmd_list_next(vc_cmd_list);
1770 return VC_DB_ERROR_NONE;
1773 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
1775 __vc_db_begin_transaction();
1777 int ret = __vc_db_get_result(result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
1778 if (ret != VC_DB_ERROR_NONE) {
1779 __vc_db_rollback_transaction();
1780 return VC_DB_ERROR_OPERATION_FAILED;
1785 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
1786 if (ret != VC_DB_ERROR_NONE) {
1787 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
1791 // Get appid by pid using app control
1792 ret = app_manager_get_app_id(pid, &appid);
1793 if (APP_MANAGER_ERROR_NONE != ret) {
1794 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
1796 ret = __vc_db_get_result(result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
1797 if (ret != VC_DB_ERROR_NONE) {
1798 __vc_db_rollback_transaction();
1801 if (NULL != appid) {
1807 __vc_db_commit_transaction();
1808 return VC_DB_ERROR_NONE;
1811 int vc_db_get_appid_list(const char* result, GSList** app_list)
1813 __vc_db_begin_transaction();
1815 int ret = __vc_db_get_appid(result, app_list);
1816 if (ret != VC_DB_ERROR_NONE) {
1817 __vc_db_rollback_transaction();
1821 __vc_db_commit_transaction();
1822 return VC_DB_ERROR_NONE;
1825 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
1827 __vc_db_begin_transaction();
1829 int ret = __vc_db_get_result_pid_list(result, pid_list);
1830 if (ret != VC_DB_ERROR_NONE) {
1831 __vc_db_rollback_transaction();
1835 __vc_db_commit_transaction();
1836 return VC_DB_ERROR_NONE;
1839 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
1841 __vc_db_begin_transaction();
1843 int ret = __vc_db_append_commands(pid, type, vc_cmd_list);
1844 if (ret != VC_DB_ERROR_NONE) {
1845 __vc_db_rollback_transaction();
1849 __vc_db_commit_transaction();
1850 return VC_DB_ERROR_NONE;
1853 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
1855 __vc_db_begin_transaction();
1858 ret = __vc_db_delete_commands(pid, type, appid);
1859 if (ret != VC_DB_ERROR_NONE) {
1860 __vc_db_rollback_transaction();
1864 __vc_db_commit_transaction();
1865 return VC_DB_ERROR_NONE;