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 "voice_control_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;
63 static int __vc_db_transaction(const char* transaction)
65 sqlite3_stmt* pStmt = NULL;
67 int ret = sqlite3_prepare_v2(db_handle, transaction, -1, &pStmt, NULL);
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)
76 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: transaction(%s), ret(%d), err(%s)", transaction, ret, sqlite3_errmsg(db_handle));
77 sqlite3_finalize(pStmt);
78 return VC_DB_ERROR_OPERATION_FAILED;
81 sqlite3_finalize(pStmt);
82 return VC_DB_ERROR_NONE;
85 static int __vc_db_begin_transaction(void)
87 int ret = __vc_db_transaction("BEGIN TRANSACTION");
91 static int __vc_db_rollback_transaction(void)
93 int ret = __vc_db_transaction("ROLLBACK TRANSACTION");
97 static int __vc_db_commit_transaction(void)
99 int ret = __vc_db_transaction("COMMIT TRANSACTION");
103 static int __vc_db_exec_query(const char* sql)
105 char* err_msg = NULL;
107 int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
108 if (ret != SQLITE_OK) {
109 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_exec return fail, ret(%d), err(%s)", ret, err_msg);
110 sqlite3_free(err_msg);
111 return VC_DB_ERROR_OPERATION_FAILED;
113 return VC_DB_ERROR_NONE;
116 static int __vc_db_delete_commands(int pid, vc_cmd_type_e type, const char* appid)
118 sqlite3_stmt* stmt = NULL;
121 if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid)
122 sql = strdup("DELETE FROM vc_info WHERE type = ?;");
123 else if (NULL != appid)
124 sql = strdup("DELETE FROM vc_info WHERE type = ? AND appid = ?;");
126 sql = strdup("DELETE FROM vc_info WHERE type = ? AND pid = ?;");
129 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
130 return VC_DB_ERROR_OUT_OF_MEMORY;
134 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
135 if (ret != SQLITE_OK) {
136 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
137 return VC_DB_ERROR_OPERATION_FAILED;
139 ret = sqlite3_bind_int(stmt, 1, (int)type);
140 if (ret != SQLITE_OK) {
141 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
142 return VC_DB_ERROR_OPERATION_FAILED;
145 if (VC_COMMAND_TYPE_BACKGROUND != type && -1 == pid) {
146 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d;", type);
147 } else if (NULL != appid) {
148 ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT);
149 if (ret != SQLITE_OK) {
150 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
151 return VC_DB_ERROR_OPERATION_FAILED;
153 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND appid = %s", type, appid);
155 ret = sqlite3_bind_int(stmt, 2, pid);
156 if (ret != SQLITE_OK) {
157 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
158 return VC_DB_ERROR_OPERATION_FAILED;
160 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] DELETE FROM vc_info WHERE type = %d AND pid = %d", type, pid);
163 ret = sqlite3_step(stmt);
164 if (ret != SQLITE_DONE) {
165 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned, sql(%s), ret(%d), err(%s)", sql, ret, sqlite3_errmsg(db_handle));
166 return VC_DB_ERROR_OPERATION_FAILED;
170 sqlite3_clear_bindings(stmt);
171 sqlite3_finalize(stmt);
175 return VC_DB_ERROR_NONE;
178 static int __vc_db_insert_commands(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
180 SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
182 sqlite3_stmt* stmt = NULL;
183 const char* sql = "INSERT INTO vc_info (id, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
185 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
186 if (ret != SQLITE_OK) {
187 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: err(%s)", sqlite3_errmsg(db_handle));
188 return VC_DB_ERROR_OPERATION_FAILED;
190 ret = sqlite3_bind_int(stmt, 2, pid);
191 if (ret != SQLITE_OK) {
192 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
193 return VC_DB_ERROR_OPERATION_FAILED;
195 ret = sqlite3_bind_int(stmt, 3, cmd->type);
196 if (ret != SQLITE_OK) {
197 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
198 return VC_DB_ERROR_OPERATION_FAILED;
200 ret = sqlite3_bind_int(stmt, 4, cmd->format);
201 if (ret != SQLITE_OK) {
202 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
203 return VC_DB_ERROR_OPERATION_FAILED;
205 ret = sqlite3_bind_int(stmt, 5, cmd->domain);
206 if (ret != SQLITE_OK) {
207 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: err(%s)", sqlite3_errmsg(db_handle));
208 return VC_DB_ERROR_OPERATION_FAILED;
210 ret = sqlite3_bind_text(stmt, 6, cmd->command, -1, SQLITE_TRANSIENT);
211 if (ret != SQLITE_OK) {
212 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
213 return VC_DB_ERROR_OPERATION_FAILED;
217 if (NULL == cmd->appid) {
218 // Get appid by pid using app control
219 ret = app_manager_get_app_id(pid, &appid);
220 if (APP_MANAGER_ERROR_NONE != ret) {
221 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
224 cmd->appid = strdup(appid);
228 ret = sqlite3_bind_text(stmt, 8, cmd->appid, -1, SQLITE_TRANSIENT);
229 if (ret != SQLITE_OK) {
230 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
231 return VC_DB_ERROR_OPERATION_FAILED;
233 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
234 ret = sqlite3_bind_text(stmt, 9, cmd->invocation_name, -1, SQLITE_TRANSIENT);
235 if (ret != SQLITE_OK) {
236 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
237 return VC_DB_ERROR_OPERATION_FAILED;
240 ret = sqlite3_bind_text(stmt, 10, cmd->fixed, -1, SQLITE_TRANSIENT);
241 if (ret != SQLITE_OK) {
242 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: err(%s)", sqlite3_errmsg(db_handle));
243 return VC_DB_ERROR_OPERATION_FAILED;
245 ret = sqlite3_step(stmt);
246 if (ret != SQLITE_DONE) {
247 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
248 return VC_DB_ERROR_OPERATION_FAILED;
251 SLOG(LOG_ERROR, vc_db_tag(), "[SQL] INSERT INTO vc_info (id, pid(%d), type(%d), format(%d), domain(%d), command(%s)", pid, cmd->type, cmd->format, cmd->domain, cmd->command);
252 SLOG(LOG_ERROR, vc_db_tag(), "[SQL] ==== appid(%s), invocation(%s), fixed(%s)", cmd->appid, cmd->invocation_name, cmd->fixed);
255 sqlite3_clear_bindings(stmt);
256 sqlite3_finalize(stmt);
257 return VC_DB_ERROR_NONE;
260 static int __vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
262 SLOG(LOG_DEBUG, vc_db_tag(), "pid(%d), type(%d)", pid, type);
265 sqlite3_stmt* stmt = NULL;
269 if (VC_COMMAND_TYPE_BACKGROUND == type) {
270 /* For background command */
271 sql = strdup("SELECT * FROM vc_info WHERE type = ?;");
273 sql = strdup("SELECT * FROM vc_info WHERE type = ? AND pid = ?;");
277 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
278 return VC_DB_ERROR_OUT_OF_MEMORY;
280 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
281 if (ret != SQLITE_OK) {
282 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
285 return VC_DB_ERROR_OPERATION_FAILED;
288 ret = sqlite3_bind_int(stmt, 1, (int)type);
289 if (ret != SQLITE_OK) {
290 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
291 return VC_DB_ERROR_OPERATION_FAILED;
293 if (VC_COMMAND_TYPE_BACKGROUND != type ) {
294 ret = sqlite3_bind_int(stmt, 2, pid);
295 if (ret != SQLITE_OK) {
296 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
297 return VC_DB_ERROR_OPERATION_FAILED;
301 if (VC_COMMAND_TYPE_BACKGROUND == type)
302 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d", type);
304 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE type = %d and pid = %d", type, pid);
306 ret = sqlite3_step(stmt);
307 if (ret == SQLITE_DONE) {
308 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
309 return VC_DB_ERROR_NONE;
312 if (VC_COMMAND_TYPE_BACKGROUND == type && -1 != pid) {
313 if (APP_MANAGER_ERROR_NONE != app_manager_get_app_id(pid, &appid)) {
314 SLOG(LOG_WARN, vc_db_tag(), "[WARN] fail to get app id, pid(%d)", pid);
318 while (SQLITE_ROW == ret) {
320 char* temp_text = NULL;
321 vc_cmd_s* temp_cmd = NULL;
322 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
323 if (NULL == temp_cmd) {
324 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
329 return VC_DB_ERROR_OUT_OF_MEMORY;
332 temp_text = (char*)sqlite3_column_text(stmt, 7);
333 if (NULL != temp_text)
334 temp_cmd->appid = strdup(temp_text);
336 if (NULL != appid && NULL != temp_cmd->appid) {
337 if (VC_COMMAND_TYPE_BACKGROUND == type && 0 == strncmp(appid, temp_cmd->appid, strlen(appid))) {
338 SLOG(LOG_DEBUG, vc_db_tag(), "Skip get background commands when app is foreground, appid(%s)", appid);
340 ret = sqlite3_step(stmt);
341 if (SQLITE_DONE == ret)
348 temp = sqlite3_column_int(stmt, 0);
351 temp = sqlite3_column_int(stmt, 1);
352 temp_cmd->pid = temp;
354 temp = sqlite3_column_int(stmt, 2);
355 temp_cmd->type = temp;
357 temp = sqlite3_column_int(stmt, 3);
358 temp_cmd->format = temp;
360 temp = sqlite3_column_int(stmt, 4);
361 temp_cmd->domain = temp;
363 temp_text = (char*)sqlite3_column_text(stmt, 5);
364 if (NULL != temp_text)
365 temp_cmd->command = strdup(temp_text);
367 temp_text = (char*)sqlite3_column_text(stmt, 6);
368 if (NULL != temp_text)
369 temp_cmd->parameter = strdup(temp_text);
371 temp_text = (char*)sqlite3_column_text(stmt, 8);
372 if (NULL != temp_text)
373 temp_cmd->invocation_name = strdup(temp_text);
375 temp_text = (char*)sqlite3_column_text(stmt, 9);
376 if (NULL != temp_text)
377 temp_cmd->fixed = strdup(temp_text);
379 if (type == temp_cmd->type) {
380 *cmd_list = g_slist_append(*cmd_list, temp_cmd);
382 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
384 ret = sqlite3_step(stmt);
385 if (SQLITE_DONE == ret)
395 sqlite3_clear_bindings(stmt);
396 sqlite3_finalize(stmt);
400 return VC_DB_ERROR_NONE;
403 static int __vc_db_get_pid(const char* appid, int* pid)
405 bool running = false;
406 int ret = app_manager_is_running(appid, &running);
407 if (APP_MANAGER_ERROR_NONE != ret) {
408 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to check running with appid(%s)", appid);
409 return VC_DB_ERROR_OPERATION_FAILED;
411 if (true == running) {
412 app_context_h app_context = NULL;
413 ret = app_manager_get_app_context(appid, &app_context);
414 if (APP_MANAGER_ERROR_NONE != ret) {
415 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get app_context, ret(%d), appid(%s)", ret, appid);
416 return VC_DB_ERROR_OPERATION_FAILED;
419 ret = app_context_get_pid(app_context, pid);
420 if (APP_MANAGER_ERROR_NONE != ret) {
421 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get pid, ret(%d), appid(%s)", ret, appid);
422 return VC_DB_ERROR_OPERATION_FAILED;
425 ret = app_context_destroy(app_context);
426 if (APP_MANAGER_ERROR_NONE != ret) {
427 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to destroy app context, ret(%d), appid(%s)", ret, appid);
428 return VC_DB_ERROR_OPERATION_FAILED;
431 SLOG(LOG_ERROR, vc_db_tag(), "app is not running, appid(%s)", appid);
433 return VC_DB_ERROR_NONE;
436 static int __vc_db_insert_result(const char* result_text, int event, const char* msg, bool exclusive, vc_cmd_s* cmd)
438 SLOG(LOG_DEBUG, vc_db_tag(), "result_text(%s), event(%d), msg(%s), exclusive(%d), cmd(%p)", result_text, event, msg, exclusive, cmd);
440 sqlite3_stmt* stmt = NULL;
441 const char* sql = "INSERT INTO vc_result (id, result, event, msg, exclusive, pid, type, format, domain, command, parameter, appid, invocation_name, fixed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
443 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
444 if (ret != SQLITE_OK) {
445 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
446 return VC_DB_ERROR_OPERATION_FAILED;
448 ret = sqlite3_bind_text(stmt, 2, result_text, -1, SQLITE_TRANSIENT);
449 if (ret != SQLITE_OK) {
450 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
451 return VC_DB_ERROR_OPERATION_FAILED;
453 ret = sqlite3_bind_int(stmt, 3, event);
454 if (ret != SQLITE_OK) {
455 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
456 return VC_DB_ERROR_OPERATION_FAILED;
458 ret = sqlite3_bind_text(stmt, 4, msg, -1, SQLITE_TRANSIENT);
459 if (ret != SQLITE_OK) {
460 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
461 return VC_DB_ERROR_OPERATION_FAILED;
463 ret = sqlite3_bind_int(stmt, 5, exclusive);
464 if (ret != SQLITE_OK) {
465 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
466 return VC_DB_ERROR_OPERATION_FAILED;
470 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d))", result_text, event, msg, exclusive);
472 ret = sqlite3_step(stmt);
473 if (ret != SQLITE_DONE) {
474 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
475 return VC_DB_ERROR_OPERATION_FAILED;
478 sqlite3_clear_bindings(stmt);
479 sqlite3_finalize(stmt);
480 return VC_DB_ERROR_NONE;
483 if (VC_COMMAND_TYPE_BACKGROUND == cmd->type) {
485 ret = __vc_db_get_pid(cmd->appid, &pid);
487 SLOG(LOG_WARN, vc_db_tag(), "Fail to get pid, appid(%s) ret(%d)", cmd->appid, ret);
489 SLOG(LOG_ERROR, vc_db_tag(), "pid(%d)", pid);
494 ret = sqlite3_bind_int(stmt, 6, cmd->pid);
495 if (ret != SQLITE_OK) {
496 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
497 return VC_DB_ERROR_OPERATION_FAILED;
499 ret = sqlite3_bind_int(stmt, 7, cmd->type);
500 if (ret != SQLITE_OK) {
501 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
502 return VC_DB_ERROR_OPERATION_FAILED;
504 ret = sqlite3_bind_int(stmt, 8, cmd->format);
505 if (ret != SQLITE_OK) {
506 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
507 return VC_DB_ERROR_OPERATION_FAILED;
509 ret = sqlite3_bind_int(stmt, 9, cmd->domain);
510 if (ret != SQLITE_OK) {
511 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
512 return VC_DB_ERROR_OPERATION_FAILED;
514 ret = sqlite3_bind_text(stmt, 10, cmd->command, -1, SQLITE_TRANSIENT);
515 if (ret != SQLITE_OK) {
516 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
517 return VC_DB_ERROR_OPERATION_FAILED;
519 ret = sqlite3_bind_text(stmt, 11, cmd->parameter, -1, SQLITE_TRANSIENT);
520 if (ret != SQLITE_OK) {
521 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
522 return VC_DB_ERROR_OPERATION_FAILED;
524 ret = sqlite3_bind_text(stmt, 12, cmd->appid, -1, SQLITE_TRANSIENT);
525 if (ret != SQLITE_OK) {
526 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
527 return VC_DB_ERROR_OPERATION_FAILED;
529 ret = sqlite3_bind_text(stmt, 13, cmd->invocation_name, -1, SQLITE_TRANSIENT);
530 if (ret != SQLITE_OK) {
531 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
532 return VC_DB_ERROR_OPERATION_FAILED;
534 ret = sqlite3_bind_text(stmt, 14, cmd->fixed, -1, SQLITE_TRANSIENT);
535 if (ret != SQLITE_OK) {
536 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_text: %s", sqlite3_errmsg(db_handle));
537 return VC_DB_ERROR_OPERATION_FAILED;
539 ret = sqlite3_step(stmt);
540 if (ret != SQLITE_DONE) {
541 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_step: fail returned %d, %s", ret, sqlite3_errmsg(db_handle));
542 return VC_DB_ERROR_OPERATION_FAILED;
545 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] INSERT INTO vc_result result(%s), event(%d), msg(%s), exclusive(%d), pid(%d), type(%d), format(%d), domain(%d), command(%s), parameter(%s), appid(%s), invocation(%s), fixed(%s)",
546 result_text, event, msg, exclusive, cmd->pid, cmd->type, cmd->format, cmd->domain, cmd->command, cmd->parameter, cmd->appid, cmd->invocation_name, cmd->fixed);
549 sqlite3_clear_bindings(stmt);
550 sqlite3_finalize(stmt);
552 return VC_DB_ERROR_NONE;
555 static int __vc_db_remove_invocation_name(char* org_cmd, const char* invocation_name, char** new_cmd)
557 if (NULL == org_cmd || NULL == new_cmd) {
558 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
559 return VC_DB_ERROR_INVALID_PARAMETER;
562 if (NULL == invocation_name) {
563 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, invocation name is NULL, org_cmd(%s)", org_cmd);
564 return VC_DB_ERROR_INVALID_PARAMETER;
567 if (strlen(org_cmd) <= strlen(invocation_name)) {
568 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to remove invocation name, org_cmd(%s) invocation(%s)", org_cmd, invocation_name);
569 return VC_DB_ERROR_INVALID_PARAMETER;
572 if (0 == strncasecmp(org_cmd, invocation_name, strlen(invocation_name))) {
573 *new_cmd = strdup(org_cmd + strlen(invocation_name) + 1);
575 SLOG(LOG_DEBUG, vc_db_tag(), "Original cmd[%s], New cmd[%s], Invocation name[%s]", org_cmd, *new_cmd, invocation_name);
576 return VC_DB_ERROR_NONE;
579 static int __vc_db_extract_unfixed_command(char* command, char* fixed, char** temp_unfixed)
581 if (NULL == command || NULL == temp_unfixed) {
582 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
583 return VC_DB_ERROR_INVALID_PARAMETER;
587 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] Invalid parameter, fixed cmd is NULL, org_cmd(%s)", command);
588 return VC_DB_ERROR_INVALID_PARAMETER;
591 if (strlen(command) <= strlen(fixed)) {
592 SLOG(LOG_WARN, vc_db_tag(), "[WARNING] No need to extract unfixed command, cmd(%s) fixed(%s)", command, fixed);
593 return VC_DB_ERROR_INVALID_PARAMETER;
596 char* temp = (char*)calloc(256, sizeof(char));
597 if (0 == strncasecmp(command, fixed, strlen(fixed))) {
598 strncpy(temp, command + strlen(fixed) + 1, strlen(command) - strlen(fixed) - 1);
599 SLOG(LOG_WARN, vc_db_tag(), "==");
600 } else if (0 == strncasecmp(command + strlen(command) - strlen(fixed), fixed, strlen(fixed))) {
601 strncpy(temp, command, strlen(command) - strlen(fixed) - 1);
602 SLOG(LOG_WARN, vc_db_tag(), "==");
605 SLOG(LOG_WARN, vc_db_tag(), "Command(%s) Fixed(%s) Unfixed(%s)", command, fixed, temp);
607 *temp_unfixed = strdup(temp);
611 return VC_DB_ERROR_NONE;
614 static int __vc_db_get_result(char** result_text, int* event, char** msg, int pid, char* appid, vc_cmd_list_h vc_cmd_list, bool exclusive)
617 sqlite3_stmt* stmt = NULL;
620 sql = strdup("SELECT * FROM vc_result;");
621 else if (NULL != appid)
622 sql = strdup("SELECT * FROM vc_result WHERE appid = ?;");
624 sql = strdup("SELECT * FROM vc_result WHERE pid = ?;");
627 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
628 return VC_DB_ERROR_OUT_OF_MEMORY;
631 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
632 if (ret != SQLITE_OK) {
633 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
634 return VC_DB_ERROR_OPERATION_FAILED;
637 ret = sqlite3_bind_text(stmt, 1, appid, -1, SQLITE_TRANSIENT);
638 if (ret != SQLITE_OK) {
639 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
640 return VC_DB_ERROR_OPERATION_FAILED;
642 } else if (-1 != pid) {
643 ret = sqlite3_bind_int(stmt, 1, pid);
644 if (ret != SQLITE_OK) {
645 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
646 return VC_DB_ERROR_OPERATION_FAILED;
649 ret = sqlite3_step(stmt);
650 if (ret == SQLITE_DONE) {
651 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
652 return VC_DB_ERROR_NONE;
656 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] %s", sql);
657 else if (NULL != appid)
658 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE appid = %d;", appid);
661 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE pid = %d;", pid);
663 vc_cmd_h temp_cmd = NULL;
664 while (SQLITE_ROW == ret) {
666 char* temp_text = NULL;
667 const char* invocation_name = NULL;
669 temp_text = (char*)sqlite3_column_text(stmt, 1);
670 if (NULL != temp_text)
671 *result_text = strdup(temp_text);
673 temp = sqlite3_column_int(stmt, 2);
677 temp_text = (char*)sqlite3_column_text(stmt, 3);
678 if (NULL != temp_text)
679 *msg = strdup(temp_text);
682 if (0 != vc_cmd_create(&temp_cmd)) {
683 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
684 if (NULL != *result_text) {
689 if (NULL != msg && NULL != *msg) {
696 temp = sqlite3_column_int(stmt, 0);
697 vc_cmd_set_id(temp_cmd, temp);
699 temp = sqlite3_column_int(stmt, 5);
700 vc_cmd_set_pid(temp_cmd, temp);
702 temp = sqlite3_column_int(stmt, 6);
703 vc_cmd_set_type(temp_cmd, temp);
705 temp = sqlite3_column_int(stmt, 7);
706 vc_cmd_set_format(temp_cmd, temp);
708 temp = sqlite3_column_int(stmt, 8);
709 vc_cmd_set_domain(temp_cmd, temp);
712 invocation_name = (char*)sqlite3_column_text(stmt, 12);
714 temp_text = (char*)sqlite3_column_text(stmt, 9);
715 SLOG(LOG_DEBUG, vc_db_tag(), "org command (%s)", temp_text);
717 if (NULL != temp_text) {
718 char* temp_command = NULL;
719 char* temp_unfixed = NULL;
720 char* temp_fixed = NULL;
722 if (NULL != invocation_name)
723 __vc_db_remove_invocation_name(*result_text, invocation_name, &temp_command);
725 if (NULL == temp_command) {
726 temp_command = strdup(temp_text);
728 // remove invocation name from result_text
730 *result_text = strdup(temp_command);
734 temp_fixed = (char*)sqlite3_column_text(stmt, 13);
735 if (NULL != temp_fixed)
736 vc_cmd_set_fixed_command(temp_cmd, temp_fixed);
739 temp_unfixed = (char*)sqlite3_column_text(stmt, 10);
741 if (NULL != temp_unfixed) {
742 char merge_result[256] = {0, };
743 vc_cmd_set_command(temp_cmd, temp_command);
744 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
745 snprintf(merge_result, 256, "%s %s", temp_command, temp_unfixed);
746 if (NULL != *result_text)
748 *result_text = strdup(merge_result);
749 } else if (NULL != temp_fixed) {
750 __vc_db_extract_unfixed_command(*result_text, temp_fixed, &temp_unfixed);
751 vc_cmd_set_command(temp_cmd, temp_fixed);
752 vc_cmd_set_unfixed_command(temp_cmd, temp_unfixed);
753 if (NULL != temp_unfixed) {
758 vc_cmd_set_command(temp_cmd, temp_command);
764 temp_text = (char*)sqlite3_column_text(stmt, 11);
765 if (NULL != temp_text)
766 vc_cmd_set_appid(temp_cmd, temp_text);
768 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
769 SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
770 vc_cmd_destroy(temp_cmd);
771 vc_cmd_list_destroy(vc_cmd_list, true);
772 if (NULL != *result_text) {
777 if (NULL != msg && NULL != *msg) {
782 return VC_DB_ERROR_OPERATION_FAILED;
785 ret = sqlite3_step(stmt);
786 if (SQLITE_DONE == ret)
791 sqlite3_clear_bindings(stmt);
792 sqlite3_finalize(stmt);
796 return VC_DB_ERROR_NONE;
799 static int __vc_db_get_appid(const char* result, GSList** app_list)
801 GSList* temp_app_list = NULL;
804 sqlite3_stmt* stmt = NULL;
805 const char* sql = "SELECT * FROM vc_result WHERE type = ? AND result = ? COLLATE NOCASE;";
807 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
808 if (ret != SQLITE_OK) {
809 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
810 return VC_DB_ERROR_OPERATION_FAILED;
812 ret = sqlite3_bind_int(stmt, 1, VC_COMMAND_TYPE_BACKGROUND);
813 if (ret != SQLITE_OK) {
814 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
815 return VC_DB_ERROR_OPERATION_FAILED;
817 ret = sqlite3_bind_text(stmt, 2, result, -1, SQLITE_TRANSIENT);
818 if (ret != SQLITE_OK) {
819 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
820 return VC_DB_ERROR_OPERATION_FAILED;
822 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result WHERE type = 2 and result = %s", result);
823 ret = sqlite3_step(stmt);
824 if (ret == SQLITE_DONE) {
825 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
826 return VC_DB_ERROR_NONE;
829 while (SQLITE_ROW == ret) {
830 char* temp_text = NULL;
831 vc_deactivated_app_s* temp_app = NULL;
832 temp_app = (vc_deactivated_app_s*)calloc(1, sizeof(vc_deactivated_app_s));
833 if (NULL == temp_app) {
834 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
835 return VC_DB_ERROR_OUT_OF_MEMORY;
838 temp_text = (char*)sqlite3_column_text(stmt, 11);
839 if (NULL != temp_text)
840 temp_app->appid = strdup(temp_text);
842 temp_app_list = g_slist_append(temp_app_list, temp_app);
844 ret = sqlite3_step(stmt);
845 if (SQLITE_DONE == ret)
849 *app_list = temp_app_list;
852 sqlite3_clear_bindings(stmt);
853 sqlite3_finalize(stmt);
854 return VC_DB_ERROR_NONE;
857 int __vc_db_get_result_pid_list(const char* result, GSList** pid_list)
859 GSList* temp_pid_list = NULL;
862 sqlite3_stmt* stmt = NULL;
863 const char* sql = "SELECT * FROM vc_result WHERE result = ? COLLATE NOCASE;";
865 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
866 if (ret != SQLITE_OK) {
867 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
868 return VC_DB_ERROR_OPERATION_FAILED;
870 ret = sqlite3_bind_text(stmt, 1, result, -1, SQLITE_TRANSIENT);
871 if (ret != SQLITE_OK) {
872 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
873 return VC_DB_ERROR_OPERATION_FAILED;
875 ret = sqlite3_step(stmt);
876 if (ret == SQLITE_DONE) {
877 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
878 return VC_DB_ERROR_NONE;
881 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_result result = %s", result);
883 while (SQLITE_ROW == ret) {
885 vc_cmd_s* temp_cmd = NULL;
886 temp_cmd = (vc_cmd_s*)calloc(1, sizeof(vc_cmd_s));
887 if (NULL == temp_cmd) {
888 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] memory allcation fail");
889 return VC_DB_ERROR_OUT_OF_MEMORY;
892 temp = sqlite3_column_int(stmt, 0);
895 temp = sqlite3_column_int(stmt, 5);
896 temp_cmd->pid = temp;
898 temp = sqlite3_column_int(stmt, 6);
899 temp_cmd->type = temp;
901 temp_pid_list = g_slist_append(temp_pid_list, temp_cmd);
903 ret = sqlite3_step(stmt);
904 if (SQLITE_DONE == ret)
908 *pid_list = temp_pid_list;
911 sqlite3_clear_bindings(stmt);
912 sqlite3_finalize(stmt);
913 return VC_DB_ERROR_NONE;
916 static int __vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
918 SLOG(LOG_ERROR, vc_db_tag(), "pid(%d), type(%d)", pid, type);
921 sqlite3_stmt* stmt = NULL;
922 const char* sql = "SELECT * FROM vc_info WHERE pid = ? AND type = ?;";
924 ret = sqlite3_prepare_v2(db_handle, sql, -1, &stmt, NULL);
925 if (ret != SQLITE_OK) {
926 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] sqlite3_prepare_v2: %s", sqlite3_errmsg(db_handle));
927 return VC_DB_ERROR_OPERATION_FAILED;
929 ret = sqlite3_bind_int(stmt, 1, pid);
930 if (ret != SQLITE_OK) {
931 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
932 return VC_DB_ERROR_OPERATION_FAILED;
934 ret = sqlite3_bind_int(stmt, 2, type);
935 if (ret != SQLITE_OK) {
936 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_bind_int: %s", sqlite3_errmsg(db_handle));
937 return VC_DB_ERROR_OPERATION_FAILED;
939 ret = sqlite3_step(stmt);
940 if (ret == SQLITE_DONE) {
941 SLOG(LOG_DEBUG, vc_db_tag(), "No matched commands");
942 return VC_DB_ERROR_NONE;
945 SLOG(LOG_DEBUG, vc_db_tag(), "[SQL] SELECT * FROM vc_info WHERE pid = %d and type = %d", pid, type);
947 vc_cmd_h temp_cmd = NULL;
949 while (SQLITE_ROW == ret) {
951 char* temp_text = NULL;
953 if (0 != vc_cmd_create(&temp_cmd)) {
954 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create command!!");
957 temp = sqlite3_column_int(stmt, 0);
958 vc_cmd_set_id(temp_cmd, temp);
960 temp = sqlite3_column_int(stmt, 1);
961 vc_cmd_set_pid(temp_cmd, temp);
963 temp = sqlite3_column_int(stmt, 2);
964 vc_cmd_set_type(temp_cmd, temp);
966 temp = sqlite3_column_int(stmt, 3);
967 vc_cmd_set_format(temp_cmd, temp);
969 temp = sqlite3_column_int(stmt, 4);
970 vc_cmd_set_domain(temp_cmd, temp);
972 temp_text = (char*)sqlite3_column_text(stmt, 5);
973 if (NULL != temp_text)
974 vc_cmd_set_command(temp_cmd, temp_text);
976 temp_text = (char*)sqlite3_column_text(stmt, 6);
977 if (NULL != temp_text)
978 vc_cmd_set_unfixed_command(temp_cmd, temp_text);
980 temp_text = (char*)sqlite3_column_text(stmt, 7);
981 if (NULL != temp_text)
982 vc_cmd_set_appid(temp_cmd, temp_text);
984 temp_text = (char*)sqlite3_column_text(stmt, 8);
985 if (NULL != temp_text)
986 vc_cmd_set_invocation_name(temp_cmd, temp_text);
988 temp_text = (char*)sqlite3_column_text(stmt, 9);
989 if (NULL != temp_text)
990 vc_cmd_set_fixed_command(temp_cmd, temp_text);
992 if (0 != vc_cmd_list_add(vc_cmd_list, temp_cmd)) {
993 SLOG(LOG_DEBUG, vc_db_tag(), "Fail to add command to list");
994 vc_cmd_destroy(temp_cmd);
995 vc_cmd_list_destroy(vc_cmd_list, true);
996 return VC_DB_ERROR_OPERATION_FAILED;
999 ret = sqlite3_step(stmt);
1000 if (SQLITE_DONE == ret)
1004 sqlite3_reset(stmt);
1005 sqlite3_clear_bindings(stmt);
1006 sqlite3_finalize(stmt);
1007 return VC_DB_ERROR_NONE;
1010 int vc_db_initialize(void)
1012 SLOG(LOG_ERROR, vc_db_tag(), "DB initialization");
1014 path = (char*)calloc(256, sizeof(char));
1016 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1017 return VC_DB_ERROR_OUT_OF_MEMORY;
1020 /* This should be changed to general DB space - TZ_USER_DB */
1021 snprintf(path, 256, "%s/.vc_info.db", VC_RUNTIME_INFO_ROOT);
1024 int ret = db_util_open(path, &db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
1025 if (ret != SQLITE_OK) {
1026 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to open db, path = %s, ret %d: %s", path, ret, sqlite3_errmsg(db_handle));
1028 db_util_close(db_handle);
1031 return VC_DB_ERROR_OPERATION_FAILED;
1034 if (lstat(path, &stat) < 0) {
1036 SLOG(LOG_ERROR, vc_db_tag(), "%s", strerror_r(errno, buf_err, sizeof (buf_err)));
1038 db_util_close(db_handle);
1040 return VC_DB_ERROR_OPERATION_FAILED;
1043 if (!S_ISREG(stat.st_mode)) {
1044 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] S_ISREG failed");
1046 db_util_close(db_handle);
1048 return VC_DB_ERROR_OPERATION_FAILED;
1051 if (!stat.st_size) {
1052 vc_db_create_table();
1056 char* err_msg = NULL;
1057 static const const char* sql = "PRAGMA journal_mode = WAL";
1058 int ret = sqlite3_exec(db_handle, sql, NULL, NULL, &err_msg);
1059 if (ret != SQLITE_OK) {
1060 SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
1064 return VC_DB_ERROR_NONE;
1067 int vc_db_finalize(void)
1077 db_util_close(db_handle);
1080 return VC_DB_ERROR_NONE;
1083 int vc_db_create_table(void)
1085 __vc_db_begin_transaction();
1087 const char* vc_info_sql = "CREATE TABLE IF NOT EXISTS vc_info (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, \
1088 command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1089 const char* vc_result_sql = "CREATE TABLE IF NOT EXISTS vc_result (id INTEGER PRIMARY KEY AUTOINCREMENT, result TEXT, event INTEGER, msg TEXT, exclusive INTEGER,\
1090 pid INTEGER, type INTEGER, format INTEGER, domain INTEGER, command TEXT, parameter TEXT, appid TEXT, invocation_name TEXT, fixed TEXT);";
1092 int ret = __vc_db_exec_query(vc_info_sql);
1093 if (ret != VC_DB_ERROR_NONE) {
1094 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1095 __vc_db_rollback_transaction();
1096 return VC_DB_ERROR_OPERATION_FAILED;
1098 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_info_sql);
1100 ret = __vc_db_exec_query(vc_result_sql);
1101 if (ret != VC_DB_ERROR_NONE) {
1102 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to create table, %d", ret);
1103 __vc_db_rollback_transaction();
1106 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", vc_result_sql);
1108 __vc_db_commit_transaction();
1109 return VC_DB_ERROR_NONE;
1112 int vc_db_delete_table(const char* table)
1115 if (0 == strncmp(table, "result", strlen(table))) {
1116 sql = strdup("DELETE FROM vc_result;");
1117 } else if (0 == strncmp(table, "command", strlen(table))) {
1118 sql = strdup("DELETE_FROM vc_info;");
1120 return VC_DB_ERROR_INVALID_PARAMETER;
1124 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1125 return VC_DB_ERROR_OUT_OF_MEMORY;
1128 __vc_db_begin_transaction();
1130 int ret = __vc_db_exec_query(sql);
1131 if (ret != VC_DB_ERROR_NONE) {
1132 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1133 __vc_db_rollback_transaction();
1142 if (0 == strncmp(table, "result", strlen(table))) {
1143 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_result';");
1144 } else if (0 == strncmp(table, "command", strlen(table))) {
1145 sql = strdup("UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='vc_info';");
1149 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1150 return VC_DB_ERROR_OUT_OF_MEMORY;
1153 ret = __vc_db_exec_query(sql);
1154 if (ret != VC_DB_ERROR_NONE) {
1155 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table, %d", ret);
1156 __vc_db_rollback_transaction();
1162 SLOG(LOG_WARN, vc_db_tag(), "[SQL] %s", sql);
1163 __vc_db_commit_transaction();
1167 return VC_DB_ERROR_NONE;
1170 int vc_db_begin_transaction(void)
1172 int ret = __vc_db_begin_transaction();
1176 int vc_db_rollback_transaction(void)
1178 int ret = __vc_db_rollback_transaction();
1182 int vc_db_commit_transaction(void)
1184 int ret = __vc_db_commit_transaction();
1188 static void __vc_db_remove_space(char** string)
1190 if (NULL == string || NULL == *string)
1193 char* temp = *string;
1195 //remove previous space
1197 strncpy(temp, temp + 1, strlen(temp));
1199 // remove next space
1200 if (' ' == temp[strlen(temp) - 1])
1201 temp[strlen(temp) - 1] = '\0';
1204 static bool __vc_db_is_valid_vfixed_string(char* string)
1206 char* temp = strchr(string, '}');
1210 temp = strchr(string, '{');
1214 temp = strchr(string, '|');
1220 static int __vc_db_generate_command(vc_cmd_s* cmd, char** fixed_cmd, GSList** cmd_list)
1222 if (NULL == cmd || NULL == cmd->command) {
1223 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter");
1224 return VC_DB_ERROR_INVALID_PARAMETER;
1227 GSList* temp_list = NULL;
1229 char* src_cmd = strdup(cmd->command);
1230 char* dst_cmd = NULL;
1231 char merge_cmd[256] = {0, };
1233 if (NULL == src_cmd) {
1234 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
1235 return VC_DB_ERROR_OUT_OF_MEMORY;
1238 if (VC_CMD_FORMAT_FIXED_AND_VFIXED == cmd->format) {
1239 // check string validation
1240 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1241 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1244 return VC_DB_ERROR_INVALID_PARAMETER;
1247 // remove close brace, '}'
1248 char* temp_close = strchr(src_cmd, '}');
1249 temp_close[0] = '\0';
1251 // extract fixed command and remove space in front of '{'
1252 temp = strtok(src_cmd, "{");
1253 __vc_db_remove_space(&temp);
1254 *fixed_cmd = strdup(temp);
1256 // merge command with fixed and vfixed
1257 while (NULL != (temp = strtok(NULL, "|"))) {
1258 __vc_db_remove_space(&temp);
1260 snprintf(merge_cmd, 256, "%s %s", *fixed_cmd, temp);
1261 dst_cmd = strdup(merge_cmd);
1262 temp_list = g_slist_append(temp_list, dst_cmd);
1263 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1265 } else if (VC_CMD_FORMAT_VFIXED_AND_FIXED == cmd->format) {
1266 // check string validation
1267 if (false == __vc_db_is_valid_vfixed_string(src_cmd)) {
1268 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Invalid parameter, cmd->command(%s)", src_cmd);
1271 return VC_DB_ERROR_INVALID_PARAMETER;
1274 // extract fixed command
1275 char* temp_fixed = strchr(src_cmd, '}') + 1;
1276 __vc_db_remove_space(&temp_fixed);
1277 *fixed_cmd = strdup(temp_fixed);
1279 // remove close brace, '}'
1280 temp = strtok(src_cmd, "}");
1282 // remove open brace, '{'
1283 temp = strchr(src_cmd, '{') + 1;
1285 temp = strtok(temp, "|");
1286 __vc_db_remove_space(&temp);
1288 // merge command with fixed and vfixed
1289 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1290 dst_cmd = strdup(merge_cmd);
1291 temp_list = g_slist_append(temp_list, dst_cmd);
1292 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1294 while (NULL != (temp = strtok(NULL, "|"))) {
1295 __vc_db_remove_space(&temp);
1297 // merge command with fixed and vfixed
1298 snprintf(merge_cmd, 256, "%s %s", temp, *fixed_cmd);
1299 dst_cmd = strdup(merge_cmd);
1300 temp_list = g_slist_append(temp_list, dst_cmd);
1301 SLOG(LOG_ERROR, vc_db_tag(), "New generated cmd: %s", dst_cmd);
1303 } else if (VC_CMD_FORMAT_FIXED_AND_NONFIXED == cmd->format || VC_CMD_FORMAT_NONFIXED_AND_FIXED == cmd->format) {
1304 dst_cmd = strdup(src_cmd);
1305 temp_list = g_slist_append(temp_list, dst_cmd);
1306 *fixed_cmd = strdup(src_cmd);
1309 dst_cmd = strdup(src_cmd);
1310 temp_list = g_slist_append(temp_list, dst_cmd);
1313 *cmd_list = temp_list;
1317 return VC_DB_ERROR_NONE;
1320 int vc_db_insert_command(int pid, vc_cmd_type_e type, vc_cmd_s* cmd)
1322 GSList* cmd_list = NULL;
1323 char* fixed_cmd = NULL;
1325 int ret = __vc_db_generate_command(cmd, &fixed_cmd, &cmd_list);
1327 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to generate command, %d", ret);
1331 if (0 != g_slist_length(cmd_list)) {
1332 GSList *iter = NULL;
1333 char* temp_command = NULL;
1334 iter = g_slist_nth(cmd_list, 0);
1336 while (NULL != iter) {
1337 temp_command = iter->data;
1339 if (NULL != temp_command) {
1340 if (NULL != cmd->command) {
1342 cmd->command = NULL;
1344 cmd->command = strdup(temp_command);
1345 if (NULL != fixed_cmd)
1346 cmd->fixed = strdup(fixed_cmd);
1350 int ret = __vc_db_insert_commands(pid, type, cmd);
1351 if (ret != VC_DB_ERROR_NONE) {
1352 if (NULL != fixed_cmd) {
1359 if (VC_COMMAND_TYPE_BACKGROUND == type && NULL != cmd->invocation_name) {
1360 char temp[256] = {0, };
1361 snprintf(temp, 256, "%s %s", cmd->invocation_name, cmd->command);
1362 if (NULL != cmd->command)
1365 cmd->command = strdup(temp);
1367 ret = __vc_db_insert_commands(pid, type, cmd);
1368 if (ret != VC_DB_ERROR_NONE) {
1369 if (NULL != fixed_cmd) {
1378 if (NULL != temp_command) {
1380 temp_command = NULL;
1382 iter = g_slist_next(iter);
1387 if (NULL != fixed_cmd) {
1391 return VC_DB_ERROR_NONE;
1394 int vc_db_insert_commands_list(int pid, vc_cmd_type_e type, GSList* cmd_list, char* invocation_name)
1396 GSList *iter = NULL;
1400 int count = g_slist_length(cmd_list);
1401 iter = g_slist_nth(cmd_list, 0);
1403 SLOG(LOG_DEBUG, vc_db_tag(), "list count : %d", count);
1405 __vc_db_begin_transaction();
1407 for (i = 0; i < count; i++) {
1411 temp_cmd = iter->data;
1413 if (NULL == temp_cmd) {
1414 SLOG(LOG_ERROR, vc_db_tag(), "comamnd is NULL");
1418 if (type == temp_cmd->type) {
1419 if (NULL != invocation_name)
1420 temp_cmd->invocation_name = strdup(invocation_name);
1422 int ret = vc_db_insert_command(pid, type, temp_cmd);
1423 if (ret != VC_DB_ERROR_NONE) {
1424 SLOG(LOG_ERROR, vc_db_tag(), "Fail to insert command, ret(%d)", ret);
1425 __vc_db_rollback_transaction();
1429 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Command type(%d) is NOT valid : request type(%d)", temp_cmd->type, type);
1431 iter = g_slist_next(iter);
1434 __vc_db_commit_transaction();
1436 return VC_DB_ERROR_NONE;
1439 int vc_db_get_commands(int pid, vc_cmd_type_e type, GSList** cmd_list)
1441 __vc_db_begin_transaction();
1443 int ret = __vc_db_get_commands(pid, type, cmd_list);
1444 if (ret != VC_DB_ERROR_NONE) {
1445 __vc_db_rollback_transaction();
1449 __vc_db_commit_transaction();
1450 return VC_DB_ERROR_NONE;
1453 int vc_db_insert_result(const char* result_text, int event, const char* msg, vc_cmd_list_h vc_cmd_list, bool exclusive)
1455 if (NULL == result_text) {
1456 SLOG(LOG_ERROR, vc_db_tag(), "Invalid parameter, result_text is NULL");
1457 return VC_DB_ERROR_INVALID_PARAMETER;
1460 int ret = vc_db_delete_table("result");
1462 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to delete table");
1464 if (NULL == vc_cmd_list) {
1465 __vc_db_begin_transaction();
1466 int ret = __vc_db_insert_result(result_text, event, msg, exclusive, NULL);
1467 if (ret != VC_DB_ERROR_NONE) {
1468 __vc_db_rollback_transaction();
1471 __vc_db_commit_transaction();
1472 return VC_DB_ERROR_NONE;
1475 /* Make client list node */
1476 vc_cmd_h vc_command = NULL;
1477 vc_cmd_list_first(vc_cmd_list);
1479 while (VC_ERROR_ITERATION_END != ret) {
1480 if (0 != vc_cmd_list_get_current(vc_cmd_list, &vc_command)) {
1481 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get command");
1485 if (NULL == vc_command) {
1486 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] No vc command any more");
1490 vc_cmd_s* temp_cmd = NULL;
1491 temp_cmd = (vc_cmd_s*)vc_command;
1493 __vc_db_begin_transaction();
1494 ret = __vc_db_insert_result(result_text, event, msg, exclusive, temp_cmd);
1495 if (ret != VC_DB_ERROR_NONE) {
1496 __vc_db_rollback_transaction();
1499 __vc_db_commit_transaction();
1501 ret = vc_cmd_list_next(vc_cmd_list);
1504 return VC_DB_ERROR_NONE;
1507 int vc_db_get_result(char** result_text, int* event, char** msg, int pid, vc_cmd_list_h vc_cmd_list, bool exclusive)
1509 __vc_db_begin_transaction();
1511 int ret = __vc_db_get_result(result_text, event, msg, pid, NULL, vc_cmd_list, exclusive);
1512 if (ret != VC_DB_ERROR_NONE) {
1513 __vc_db_rollback_transaction();
1514 return VC_DB_ERROR_OPERATION_FAILED;
1519 ret = vc_cmd_list_get_count(vc_cmd_list, &count);
1520 if (ret != VC_DB_ERROR_NONE) {
1521 LOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to get count");
1525 // Get appid by pid using app control
1526 ret = app_manager_get_app_id(pid, &appid);
1527 if (APP_MANAGER_ERROR_NONE != ret) {
1528 SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] fail to get app id, ret(%d), pid(%d)", ret, pid);
1530 ret = __vc_db_get_result(result_text, event, msg, pid, appid, vc_cmd_list, exclusive);
1531 if (ret != VC_DB_ERROR_NONE) {
1532 __vc_db_rollback_transaction();
1535 if (NULL != appid) {
1541 __vc_db_commit_transaction();
1542 return VC_DB_ERROR_NONE;
1545 int vc_db_get_appid_list(const char* result, GSList** app_list)
1547 __vc_db_begin_transaction();
1549 int ret = __vc_db_get_appid(result, app_list);
1550 if (ret != VC_DB_ERROR_NONE) {
1551 __vc_db_rollback_transaction();
1555 __vc_db_commit_transaction();
1556 return VC_DB_ERROR_NONE;
1559 int vc_db_get_result_pid_list(const char* result, GSList** pid_list)
1561 __vc_db_begin_transaction();
1563 int ret = __vc_db_get_result_pid_list(result, pid_list);
1564 if (ret != VC_DB_ERROR_NONE) {
1565 __vc_db_rollback_transaction();
1569 __vc_db_commit_transaction();
1570 return VC_DB_ERROR_NONE;
1573 int vc_db_append_commands(int pid, int type, vc_cmd_list_h vc_cmd_list)
1575 __vc_db_begin_transaction();
1577 int ret = __vc_db_append_commands(pid, type, vc_cmd_list);
1578 if (ret != VC_DB_ERROR_NONE) {
1579 __vc_db_rollback_transaction();
1583 __vc_db_commit_transaction();
1584 return VC_DB_ERROR_NONE;
1587 int vc_db_delete_commands(int pid, vc_cmd_type_e type, char* appid)
1589 __vc_db_begin_transaction();
1592 ret = __vc_db_delete_commands(pid, type, appid);
1593 if (ret != VC_DB_ERROR_NONE) {
1594 __vc_db_rollback_transaction();
1598 __vc_db_commit_transaction();
1599 return VC_DB_ERROR_NONE;