2 * Copyright (c) 2011-2015 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.
19 #include "vcd_client_data.h"
20 #include "vcd_config.h"
24 static GSList* g_client_list = NULL;
26 static GSList* g_widget_list = NULL;
28 static manager_info_s g_manager;
31 static current_commands_list_s g_cur_cmd_list;
33 /* Demandable client list */
34 static GSList* g_demandable_client = NULL;
37 static bool g_silence_detection;
38 static vcd_recognition_mode_e g_recognition_mode;
41 /* Function definitions */
42 widget_info_s* __widget_get_element(int pid);
44 vc_client_info_s* __client_get_element(int pid);
47 int vcd_client_manager_set(int pid)
49 if (-1 != g_manager.pid) {
50 SLOG(LOG_DEBUG, TAG_VCD, "Manager has already registered");
54 g_manager.manager_cmd = false;
55 g_manager.exclusive_cmd_option = false;
60 int vcd_client_manager_unset()
63 g_manager.manager_cmd = false;
64 g_manager.exclusive_cmd_option = false;
69 bool vcd_client_manager_is_valid(int pid)
71 if (-1 == g_manager.pid || pid == g_manager.pid) {
77 int vcd_client_manager_set_command(int pid)
79 if (pid != g_manager.pid) {
80 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
83 g_manager.manager_cmd = true;
87 int vcd_client_manager_unset_command(int pid)
89 if (pid != g_manager.pid) {
90 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
93 g_manager.manager_cmd = false;
97 int vcd_client_manager_set_demandable_client(int pid, GSList* client_list)
99 if (0 != g_slist_length(g_demandable_client)) {
102 vc_demandable_client_s* temp_client;
103 iter = g_slist_nth(g_demandable_client, 0);
105 while (NULL != iter) {
106 temp_client = iter->data;
108 if (NULL != temp_client) {
109 if (NULL != temp_client->appid) free(temp_client->appid);
113 iter = g_slist_next(iter);
115 g_demandable_client = NULL;
118 g_demandable_client = client_list;
123 bool vcd_client_manager_check_demandable_client(int pid)
125 if (0 == g_slist_length(g_demandable_client)) {
126 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] All client is available to request start");
130 /* Check demandable appid */
131 char appid[128] = {0, };
132 aul_app_get_appid_bypid(pid, appid, sizeof(appid));
134 if (0 < strlen(appid)) {
135 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] %s(%d) requests start", appid, pid);
137 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] daemon(%d) requests start", pid);
142 vc_demandable_client_s* temp_client;
143 iter = g_slist_nth(g_demandable_client, 0);
145 while (NULL != iter) {
146 temp_client = iter->data;
148 if (NULL != temp_client) {
150 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] demandable appid(%s)", temp_client->appid);
152 if (NULL != temp_client->appid) {
153 if (0 == strcmp(temp_client->appid, appid)) {
154 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
158 if (0 == strlen(appid)) {
159 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) is available", pid);
165 iter = g_slist_next(iter);
171 bool vcd_client_manager_get_exclusive()
173 return g_manager.exclusive_cmd_option;
176 int vcd_client_manager_set_exclusive(bool value)
178 g_manager.exclusive_cmd_option = value;
182 int vcd_client_manager_get_pid()
184 return g_manager.pid;
187 int __vcd_client_release_commands()
189 g_cur_cmd_list.total_cmd_count = 0;
190 g_cur_cmd_list.foreground = VC_NO_FOREGROUND_PID;
195 if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
196 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
197 while (NULL != iter) {
198 temp_cmd = iter->data;
200 if (NULL != temp_cmd) {
201 if (NULL != temp_cmd->command) free(temp_cmd->command);
202 if (NULL != temp_cmd->parameter) free(temp_cmd->parameter);
206 iter = g_slist_next(iter);
208 g_cur_cmd_list.widget_cmds = NULL;
211 if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
212 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
213 while (NULL != iter) {
214 temp_cmd = iter->data;
216 if (NULL != temp_cmd) {
217 if (NULL != temp_cmd->command) free(temp_cmd->command);
218 if (NULL != temp_cmd->parameter) free(temp_cmd->parameter);
222 iter = g_slist_next(iter);
224 g_cur_cmd_list.foreground_cmds = NULL;
227 if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
228 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
229 while (NULL != iter) {
230 temp_cmd = iter->data;
232 if (NULL != temp_cmd) {
233 if (NULL != temp_cmd->command) free(temp_cmd->command);
234 if (NULL != temp_cmd->parameter) free(temp_cmd->parameter);
238 iter = g_slist_next(iter);
240 g_cur_cmd_list.system_cmds = NULL;
243 if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
244 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
245 while (NULL != iter) {
246 temp_cmd = iter->data;
248 if (NULL != temp_cmd) {
249 if (NULL != temp_cmd->command) free(temp_cmd->command);
250 if (NULL != temp_cmd->parameter) free(temp_cmd->parameter);
254 iter = g_slist_next(iter);
256 g_cur_cmd_list.exclusive_system_cmds = NULL;
259 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
260 background_command_s* back_cmd_info;
261 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
263 GSList* back_iter = NULL;
265 while (NULL != iter) {
266 back_cmd_info = iter->data;
268 if (NULL != back_cmd_info) {
269 back_iter = g_slist_nth(back_cmd_info->cmds, 0);
271 while (NULL != back_iter) {
272 temp_cmd = back_iter->data;
274 if (NULL != temp_cmd) {
275 if (NULL != temp_cmd->command) free(temp_cmd->command);
276 if (NULL != temp_cmd->parameter) free(temp_cmd->parameter);
280 back_iter = g_slist_next(back_iter);
283 back_cmd_info->cmds = NULL;
286 iter = g_slist_next(iter);
289 g_cur_cmd_list.background_cmds = NULL;
292 g_cur_cmd_list.bg_cmd_count = 0;
297 int vcd_client_command_collect_command()
299 /* 1. Get foreground pid */
302 if (0 != vcd_config_get_foreground(&fg_pid)) {
303 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
304 /* There is no foreground app for voice control */
307 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Foreground pid(%d)", fg_pid);
309 /* 2. Clean up command list */
310 __vcd_client_release_commands();
312 /* Check exclusive system command */
313 if (true == g_manager.exclusive_cmd_option) {
314 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive option of manager is ON");
316 GSList* ex_sys_cmd_list = NULL;
317 if (true == g_manager.manager_cmd) {
318 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_EXCLUSIVE, &ex_sys_cmd_list);
320 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
322 g_cur_cmd_list.exclusive_system_cmds = ex_sys_cmd_list;
325 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
331 /* 3. Set system command */
332 GSList* sys_cmd_list = NULL;
333 if (true == g_manager.manager_cmd) {
334 ret = vc_cmd_parser_get_commands(g_manager.pid, VC_COMMAND_TYPE_SYSTEM, &sys_cmd_list);
336 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the system command list");
338 g_cur_cmd_list.system_cmds = sys_cmd_list;
341 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
344 vc_client_info_s* client_info = NULL;
347 /* 4. Set foreground commands and widget */
348 if (VC_NO_FOREGROUND_PID != fg_pid) {
349 GSList* fg_cmd_list = NULL;
350 GSList* widget_cmd_list = NULL;
352 g_cur_cmd_list.foreground = fg_pid;
354 /* 4-1. Set widget command */
355 widget_info_s* widget_info = NULL;
356 widget_info = __widget_get_element(fg_pid);
357 if (NULL != widget_info) {
358 if (true == widget_info->widget_cmd) {
359 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_WIDGET, &widget_cmd_list);
361 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the WIDGET command list");
363 g_cur_cmd_list.widget_cmds = widget_cmd_list;
367 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
371 client_info = __client_get_element(fg_pid);
372 if (NULL != client_info) {
373 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] fore cmd(%d)", client_info->fg_cmd);
375 /* 4-2. Set foreground command */
376 if (true == client_info->fg_cmd) {
377 ret = vc_cmd_parser_get_commands(fg_pid, VC_COMMAND_TYPE_FOREGROUND, &fg_cmd_list);
379 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the fg command list");
381 g_cur_cmd_list.foreground_cmds = fg_cmd_list;
384 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
387 /* 4-3. Check exclusive option */
388 if (true == client_info->exclusive_cmd) {
389 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive command is ON");
391 /* 4-4. Set background command for exclusive option */
392 if (true == client_info->bg_cmd) {
393 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Set background command");
394 GSList* bg_cmd_list = NULL;
395 ret = vc_cmd_parser_get_commands(client_info->pid, VC_COMMAND_TYPE_BACKGROUND, &bg_cmd_list);
397 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the bg command list : pid(%d)", client_info->pid);
399 background_command_s* bg_cmd = (background_command_s*)calloc(1, sizeof(background_command_s));
400 if (NULL == bg_cmd) {
401 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
402 return VCD_ERROR_OUT_OF_MEMORY;
405 bg_cmd->pid = client_info->pid;
406 bg_cmd->cmds = bg_cmd_list;
407 bg_cmd->cmd_count = g_slist_length(bg_cmd_list);
409 /* Add item to global command list */
410 g_cur_cmd_list.background_cmds = g_slist_append(g_cur_cmd_list.background_cmds, bg_cmd);
416 SLOG(LOG_WARN, TAG_VCD, "[Client Data] No foreground client : pid(%d)", fg_pid);
419 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground app");
422 /* 5. Set background commands */
423 if (0 < g_slist_length(g_client_list)) {
424 iter = g_slist_nth(g_client_list, 0);
426 while (NULL != iter) {
427 client_info = iter->data;
428 GSList* bg_cmd_list = NULL;
430 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Pid(%d) Back cmd(%d)", client_info->pid, client_info->bg_cmd);
432 if (true == client_info->bg_cmd) {
433 ret = vc_cmd_parser_get_commands(client_info->pid, VC_COMMAND_TYPE_BACKGROUND, &bg_cmd_list);
435 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to get the bg command list : pid(%d)", client_info->pid);
437 background_command_s* bg_cmd = (background_command_s*)calloc(1, sizeof(background_command_s));
438 if (NULL == bg_cmd) {
439 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
440 return VCD_ERROR_OUT_OF_MEMORY;
443 bg_cmd->pid = client_info->pid;
444 bg_cmd->cmds = bg_cmd_list;
445 bg_cmd->cmd_count = g_slist_length(bg_cmd_list);
447 /* Add item to global command list */
448 g_cur_cmd_list.background_cmds = g_slist_append(g_cur_cmd_list.background_cmds, bg_cmd);
452 iter = g_slist_next(iter);
456 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
462 int vcd_client_get_length()
464 int command_count = 0;
465 command_count += g_slist_length(g_cur_cmd_list.widget_cmds);
466 command_count += g_slist_length(g_cur_cmd_list.foreground_cmds);
467 command_count += g_slist_length(g_cur_cmd_list.system_cmds);
468 command_count += g_slist_length(g_cur_cmd_list.exclusive_system_cmds);
471 background_command_s* back_cmd_info;
473 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
474 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
476 while (NULL != iter) {
477 back_cmd_info = iter->data;
479 command_count += g_slist_length(back_cmd_info->cmds);
481 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] background commands count : %d", g_slist_length(back_cmd_info->cmds));
483 iter = g_slist_next(iter);
487 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
490 g_cur_cmd_list.total_cmd_count = command_count;
492 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Command count : %d ", g_cur_cmd_list.total_cmd_count);
494 return command_count;
497 int vcd_client_foreach_command(client_foreach_command_cb callback, void* user_data)
499 if (NULL == callback) {
500 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] input parameter is NULL");
501 return VCD_ERROR_INVALID_PARAMETER;
508 if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
509 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
510 while (NULL != iter) {
511 temp_cmd = iter->data;
513 temp_cmd->id = id_count;
516 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Widget : id(%d) index(%d) format(%d) command(%s) param(%s) domain(%d)"
517 , temp_cmd->id, temp_cmd->index, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
519 callback(temp_cmd->id, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
521 iter = g_slist_next(iter);
524 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
527 if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
528 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
529 while (NULL != iter) {
530 temp_cmd = iter->data;
532 temp_cmd->id = id_count;
535 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Foreground : id(%d) index(%d) format(%d) command(%s) param(%s) domain(%d)"
536 , temp_cmd->id, temp_cmd->index, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
538 callback(temp_cmd->id, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
540 iter = g_slist_next(iter);
543 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
546 if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
547 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
548 while (NULL != iter) {
549 temp_cmd = iter->data;
551 temp_cmd->id = id_count;
554 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] System : id(%d) index(%d) format(%d) domain(%d) command(%s) param(%s)"
555 , temp_cmd->id, temp_cmd->index, temp_cmd->format, temp_cmd->domain, temp_cmd->command, temp_cmd->parameter);
557 callback(temp_cmd->id, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
559 iter = g_slist_next(iter);
562 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
565 if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
566 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
567 while (NULL != iter) {
568 temp_cmd = iter->data;
570 temp_cmd->id = id_count;
573 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Exclusive system : id(%d) index(%d) format(%d) command(%s) param(%s) domain(%d)"
574 , temp_cmd->id, temp_cmd->index, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
576 callback(temp_cmd->id, temp_cmd->type, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
578 iter = g_slist_next(iter);
581 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
584 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
585 background_command_s* back_cmd_info;
586 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
588 while (NULL != iter) {
589 back_cmd_info = iter->data;
591 GSList* back_iter = NULL;
592 back_iter = g_slist_nth(back_cmd_info->cmds, 0);
594 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) command count(%d)", back_cmd_info->pid, back_cmd_info->cmd_count);
596 while (NULL != back_iter) {
597 temp_cmd = back_iter->data;
599 temp_cmd->id = id_count;
602 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Background : id(%d) index(%d) format(%d) command(%s) param(%s) domain(%d)"
603 , temp_cmd->id, temp_cmd->index, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain);
605 callback(temp_cmd->id, temp_cmd->format, temp_cmd->command, temp_cmd->parameter, temp_cmd->domain, user_data);
607 back_iter = g_slist_next(back_iter);
610 iter = g_slist_next(iter);
613 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
619 static vc_cmd_s* __command_copy(vc_cmd_s* src_cmd)
621 if (NULL == src_cmd) {
622 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Input command is NULL");
626 vc_cmd_s* temp_cmd = NULL;
627 temp_cmd = (vc_cmd_s*)calloc(sizeof(vc_cmd_s), 1);
628 if (NULL == temp_cmd) {
629 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
633 temp_cmd->id = src_cmd->id;
634 temp_cmd->pid = src_cmd->pid;
635 temp_cmd->index = src_cmd->index;
636 temp_cmd->type = src_cmd->type;
637 temp_cmd->format = src_cmd->format;
638 temp_cmd->domain = src_cmd->domain;
640 if (NULL != src_cmd->command) {
641 temp_cmd->command = strdup(src_cmd->command);
644 if (NULL != src_cmd->parameter) {
645 temp_cmd->parameter = strdup(src_cmd->parameter);
648 temp_cmd->key = src_cmd->key;
649 temp_cmd->modifier = src_cmd->modifier;
654 int vcd_client_get_cmd_from_result_id(int result_id, vc_cmd_s** result)
659 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Result id(%d)", result_id);
661 if (0 < g_slist_length(g_cur_cmd_list.widget_cmds)) {
662 iter = g_slist_nth(g_cur_cmd_list.widget_cmds, 0);
663 while (NULL != iter) {
664 temp_cmd = iter->data;
666 if (result_id == temp_cmd->id) {
667 /**pid = g_cur_cmd_list.foreground; */
668 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_UI_CONTROL; */
669 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
671 *result = __command_copy(temp_cmd);
676 iter = g_slist_next(iter);
679 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No widget commands");
682 if (0 < g_slist_length(g_cur_cmd_list.foreground_cmds)) {
683 iter = g_slist_nth(g_cur_cmd_list.foreground_cmds, 0);
685 while (NULL != iter) {
686 temp_cmd = iter->data;
688 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] command id (%d)", temp_cmd->id);
690 if (result_id == temp_cmd->id) {
691 /**pid = g_cur_cmd_list.foreground; */
692 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_FOREGROUND; */
693 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
695 *result = __command_copy(temp_cmd);
699 iter = g_slist_next(iter);
702 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No foreground commands");
705 if (0 < g_slist_length(g_cur_cmd_list.system_cmds)) {
706 iter = g_slist_nth(g_cur_cmd_list.system_cmds, 0);
707 while (NULL != iter) {
708 temp_cmd = iter->data;
710 if (result_id == temp_cmd->id) {
711 /**pid = g_manager.pid; */
712 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM; */
713 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
715 *result = __command_copy(temp_cmd);
719 iter = g_slist_next(iter);
722 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No system commands");
725 if (0 < g_slist_length(g_cur_cmd_list.exclusive_system_cmds)) {
726 iter = g_slist_nth(g_cur_cmd_list.exclusive_system_cmds, 0);
727 while (NULL != iter) {
728 temp_cmd = iter->data;
730 if (result_id == temp_cmd->id) {
731 /**pid = g_manager.pid; */
732 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_SYSTEM_EXCLUSIVE; */
733 /*SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] Find result pid(%d) type(%d)", *pid, *cmd_type); */
735 *result = __command_copy(temp_cmd);
739 iter = g_slist_next(iter);
742 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No exclusive system commands");
745 if (0 < g_slist_length(g_cur_cmd_list.background_cmds)) {
746 background_command_s* back_cmd_info;
747 iter = g_slist_nth(g_cur_cmd_list.background_cmds, 0);
749 while (NULL != iter) {
750 back_cmd_info = iter->data;
752 GSList* back_iter = NULL;
753 back_iter = g_slist_nth(back_cmd_info->cmds, 0);
755 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] pid(%d) command count(%d)", back_cmd_info->pid, back_cmd_info->cmd_count);
757 while (NULL != back_iter) {
758 temp_cmd = back_iter->data;
760 if (result_id == temp_cmd->id) {
761 /**pid = back_cmd_info->pid; */
762 /**cmd_type = VCD_CLIENT_COMMAND_GROUP_TYPE_BACKGROUND; */
764 *result = __command_copy(temp_cmd);
767 back_iter = g_slist_next(back_iter);
770 iter = g_slist_next(iter);
773 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] No background commands");
776 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Not find matched result");
781 int vcd_client_set_slience_detection(bool value)
783 g_silence_detection = value;
787 bool vcd_client_get_slience_detection()
789 return g_silence_detection;
792 int vcd_client_set_recognition_mode(vcd_recognition_mode_e mode)
794 g_recognition_mode = mode;
798 vcd_recognition_mode_e vcd_client_get_recognition_mode()
800 return g_recognition_mode;
803 int __show_client_list()
806 vc_client_info_s *data = NULL;
808 SLOG(LOG_DEBUG, TAG_VCD, "----- client list");
810 int count = g_slist_length(g_client_list);
814 SLOG(LOG_DEBUG, TAG_VCD, "No Client");
816 iter = g_slist_nth(g_client_list, 0);
817 for (i = 0; i < count; i++) {
820 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, data->pid);
821 iter = g_slist_next(iter);
825 SLOG(LOG_DEBUG, TAG_VCD, "-----");
827 SLOG(LOG_DEBUG, TAG_VCD, "----- widget list");
829 widget_info_s *widget_data = NULL;
831 count = g_slist_length(g_widget_list);
834 SLOG(LOG_DEBUG, TAG_VCD, "No widget");
836 iter = g_slist_nth(g_widget_list, 0);
837 for (i = 0; i < count; i++) {
838 widget_data = iter->data;
840 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] pid(%d)", i, widget_data->pid);
841 iter = g_slist_next(iter);
845 SLOG(LOG_DEBUG, TAG_VCD, "-----");
850 int __show_command_list(GSList* cmd_group)
853 vc_cmd_s *data = NULL;
855 SLOG(LOG_DEBUG, TAG_VCD, "----- command group");
857 int count = g_slist_length(cmd_group);
861 SLOG(LOG_DEBUG, TAG_VCD, "No command");
863 iter = g_slist_nth(cmd_group, 0);
864 for (i = 0; i < count; i++) {
867 if (NULL != data->parameter) {
868 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) parameter(%s) key(%d)", i, data->command, data->parameter, data->key);
870 SLOG(LOG_DEBUG, TAG_VCD, "[%dth] command(%s) key(%d)", i, data->command, data->key);
872 iter = g_slist_next(iter);
876 SLOG(LOG_DEBUG, TAG_VCD, "-----");
881 GSList* __client_get_item(const int pid)
884 vc_client_info_s *data = NULL;
886 int count = g_slist_length(g_client_list);
890 iter = g_slist_nth(g_client_list, 0);
891 for (i = 0; i < count; i++) {
894 if (pid == data->pid)
897 iter = g_slist_next(iter);
904 vc_client_info_s* __client_get_element(int pid)
907 vc_client_info_s *data = NULL;
909 int count = g_slist_length(g_client_list);
913 iter = g_slist_nth(g_client_list, 0);
914 for (i = 0; i < count; i++) {
917 if (pid == data->pid)
920 iter = g_slist_next(iter);
927 int vcd_client_add(int pid)
929 /*Check pid is duplicated*/
931 tmp = __client_get_item(pid);
934 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Client pid is already registered");
935 return VCD_ERROR_INVALID_PARAMETER;
938 vc_client_info_s *info = (vc_client_info_s*)calloc(1, sizeof(vc_client_info_s));
940 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
941 return VCD_ERROR_OUT_OF_MEMORY;
946 info->fg_cmd = false;
947 info->bg_cmd = false;
948 info->exclusive_cmd = false;
950 /* Add item to global list */
951 g_client_list = g_slist_append(g_client_list, info);
953 if (NULL == g_client_list) {
954 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new client");
957 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new client");
960 #ifdef CLIENT_DATA_DEBUG
961 __show_client_list();
966 int vcd_client_delete(int pid)
969 vc_client_info_s* client_info = NULL;
972 tmp = __client_get_item(pid);
974 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
975 return VCD_ERROR_INVALID_PARAMETER;
978 /*Free client structure*/
979 client_info = tmp->data;
980 if (NULL != client_info) {
984 /*Remove handle from list*/
985 g_client_list = g_slist_remove_link(g_client_list, tmp);
988 #ifdef CLIENT_DATA_DEBUG
989 __show_client_list();
995 bool vcd_client_is_available(int pid)
997 vc_client_info_s* client_info = NULL;
999 client_info = __client_get_element(pid);
1000 if (NULL == client_info) {
1001 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1008 int vcd_client_get_ref_count()
1012 count = g_slist_length(g_client_list) + g_slist_length(g_widget_list);
1013 if (0 < g_manager.pid) {
1017 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data] client count : %d", count);
1022 int vcd_client_get_list(int** pids, int* pid_count)
1024 if (NULL == pids || NULL == pid_count)
1027 int count = g_slist_length(g_client_list);
1033 tmp = (int*)calloc(count, sizeof(int));
1035 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1036 return VCD_ERROR_OUT_OF_MEMORY;
1039 GSList *iter = NULL;
1040 vc_client_info_s *data = NULL;
1043 iter = g_slist_nth(g_client_list, 0);
1045 while (NULL != iter) {
1052 iter = g_slist_next(iter);
1062 int vcd_client_set_command_type(int pid, int type)
1064 vc_client_info_s* client_info = NULL;
1066 client_info = __client_get_element(pid);
1067 if (NULL == client_info) {
1068 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1069 return VCD_ERROR_INVALID_PARAMETER;
1073 case VC_COMMAND_TYPE_FOREGROUND:
1074 client_info->fg_cmd = true;
1076 case VC_COMMAND_TYPE_BACKGROUND:
1077 client_info->bg_cmd = true;
1080 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1087 int vcd_client_unset_command_type(int pid, int type)
1089 vc_client_info_s* client_info = NULL;
1091 client_info = __client_get_element(pid);
1092 if (NULL == client_info) {
1093 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] vcd_client_get_pid : pid(%d) is not found", pid);
1094 return VCD_ERROR_INVALID_PARAMETER;
1098 case VC_COMMAND_TYPE_FOREGROUND:
1099 client_info->fg_cmd = false;
1101 case VC_COMMAND_TYPE_BACKGROUND:
1102 client_info->bg_cmd = false;
1105 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] not supported command type(%d)", type);
1112 int vcd_client_set_exclusive_command(int pid)
1114 vc_client_info_s* client_info = NULL;
1116 client_info = __client_get_element(pid);
1117 if (NULL == client_info) {
1118 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1119 return VCD_ERROR_INVALID_PARAMETER;
1122 client_info->exclusive_cmd = true;
1127 int vcd_client_unset_exclusive_command(int pid)
1129 vc_client_info_s* client_info = NULL;
1131 client_info = __client_get_element(pid);
1132 if (NULL == client_info) {
1133 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is not found", pid);
1134 return VCD_ERROR_INVALID_PARAMETER;
1137 client_info->exclusive_cmd = false;
1142 int vcd_client_save_client_info()
1144 if (0 != vc_info_parser_set_client_info(g_client_list)) {
1145 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to save client info");
1153 * Functions for widget
1155 GSList* __widget_get_item(int pid)
1157 GSList *iter = NULL;
1158 widget_info_s *data = NULL;
1160 int count = g_slist_length(g_widget_list);
1164 iter = g_slist_nth(g_widget_list, 0);
1165 for (i = 0; i < count; i++) {
1168 if (pid == data->pid)
1171 iter = g_slist_next(iter);
1178 widget_info_s* __widget_get_element(int pid)
1180 GSList *iter = NULL;
1181 widget_info_s *data = NULL;
1183 int count = g_slist_length(g_widget_list);
1187 iter = g_slist_nth(g_widget_list, 0);
1188 for (i = 0; i < count; i++) {
1192 if (pid == data->pid)
1196 iter = g_slist_next(iter);
1203 int vcd_client_widget_get_list(int** pids, int* pid_count)
1205 if (NULL == pids || NULL == pid_count)
1208 int count = g_slist_length(g_widget_list);
1214 tmp = (int*)calloc(count, sizeof(int));
1216 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1217 return VCD_ERROR_OUT_OF_MEMORY;
1220 GSList *iter = NULL;
1221 widget_info_s *data = NULL;
1224 iter = g_slist_nth(g_widget_list, 0);
1225 while (NULL != iter) {
1232 iter = g_slist_next(iter);
1242 int vcd_client_widget_add(int pid)
1244 /*Check pid is duplicated*/
1246 tmp = __widget_get_item(pid);
1249 SLOG(LOG_WARN, TAG_VCD, "[Client Data] widget pid is already registered");
1250 return VCD_ERROR_INVALID_PARAMETER;
1253 widget_info_s *info = (widget_info_s*)calloc(1, sizeof(widget_info_s));
1255 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to allocate memory");
1256 return VCD_ERROR_OUT_OF_MEMORY;
1260 info->widget_cmd = false;
1262 /* Add item to global list */
1263 g_widget_list = g_slist_append(g_widget_list, info);
1265 if (NULL == g_widget_list) {
1266 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] Fail to add new widget");
1269 SLOG(LOG_DEBUG, TAG_VCD, "[Client Data SUCCESS] Add new widget");
1272 #ifdef CLIENT_DATA_DEBUG
1273 __show_client_list();
1278 int vcd_client_widget_delete(int pid)
1281 widget_info_s* widget_info = NULL;
1284 tmp = __widget_get_item(pid);
1286 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1287 return VCD_ERROR_INVALID_PARAMETER;
1290 /*Free client structure*/
1291 widget_info = tmp->data;
1292 if (NULL != widget_info) {
1296 /*Remove handle from list*/
1297 g_widget_list = g_slist_remove_link(g_widget_list, tmp);
1300 #ifdef CLIENT_DATA_DEBUG
1301 __show_client_list();
1307 int vcd_client_widget_get_foreground_pid()
1309 /* 1. Get foreground pid */
1311 if (0 != vcd_config_get_foreground(&fg_pid)) {
1312 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get foreground pid");
1313 /* There is no foreground app for voice control */
1316 widget_info_s* widget = NULL;
1318 widget = __widget_get_element(fg_pid);
1319 if (NULL == widget) {
1320 SLOG(LOG_WARN, TAG_VCD, "[Client Data] Not found foreground pid of widget");
1328 bool vcd_client_widget_is_available(int pid)
1330 widget_info_s* widget_info = NULL;
1332 widget_info = __widget_get_element(pid);
1333 if (NULL == widget_info) {
1334 SLOG(LOG_WARN, TAG_VCD, "[Client Data] pid(%d) is NOT valid", pid);
1341 int vcd_client_widget_set_command(int pid)
1343 widget_info_s *info = NULL;
1344 info = __widget_get_element(pid);
1346 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1347 return VCD_ERROR_INVALID_PARAMETER;
1350 info->widget_cmd = true;
1355 int vcd_client_widget_unset_command(int pid)
1357 widget_info_s *info = NULL;
1358 info = __widget_get_element(pid);
1360 SLOG(LOG_ERROR, TAG_VCD, "[Client Data ERROR] pid(%d) is NOT valid", pid);
1361 return VCD_ERROR_INVALID_PARAMETER;
1364 info->widget_cmd = false;