Remove unused or unreachable code
[platform/core/uifw/voice-control.git] / client / vc_mgr_core.c
1 /*
2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 #include <aul.h>
17 #include <system_info.h>
18 #include <pthread.h>
19 #include <sound_manager.h>
20 #include <sound_manager_internal.h>
21 #include <math.h>
22
23 #include "vc_cmd_db.h"
24 #include "vc_command.h"
25 #include "vc_config_mgr.h"
26 #include "vc_info_parser.h"
27 #include "vc_json_parser.h"
28 #include "vc_main.h"
29 #include "vc_mgr_core.h"
30 #include "vc_mgr_client.h"
31 #include "vc_mgr_tidl.h"
32 #include "vc_mgr_data.h"
33 #include "voice_control_command.h"
34 #include "voice_control_command_expand.h"
35 #include "voice_control_common.h"
36 #include "voice_control_manager.h"
37 #include "voice_control_manager_internal.h"
38
39
40 static Ecore_Timer* g_m_set_volume_timer = NULL;
41
42 static GSList* g_demandable_client_list = NULL;
43
44 static float g_volume_db = 0;
45
46 static float g_prev_volume_db = 0;
47
48 static float g_cur_volume_db = 0;
49
50 static int g_volume_timer_cnt = 0;
51
52 static bool g_err_callback_status = false;
53
54 /* for TTS feedback */
55 static int g_feedback_rate = 16000;
56 static vc_audio_channel_e g_feedback_audio_channel = 0;
57 static vc_audio_type_e g_feedback_audio_type = 0;
58
59 static Eina_Bool __vc_mgr_core_set_volume_timer_cb(void* data)
60 {
61         g_volume_timer_cnt++;
62         g_volume_db = g_prev_volume_db + (g_cur_volume_db - g_prev_volume_db) / 5 * g_volume_timer_cnt;
63
64         SLOG(LOG_INFO, TAG_VCM, "Set volume (%f)(%f)", g_volume_db, g_cur_volume_db);
65
66         if (0 == g_volume_timer_cnt % 5) {
67                 return EINA_FALSE;
68         }
69         return EINA_TRUE;
70 }
71
72 //LCOV_EXCL_START
73 static Eina_Bool __vc_mgr_core_set_select_result(void *data)
74 {
75         // TODO: remove vc_mgr dependency by separate vc_mgr_set_selected_results core logic to core layer
76         vc_mgr_set_selected_results(NULL);
77         return EINA_FALSE;
78 }
79 //LCOV_EXCL_STOP
80
81 static void __vc_mgr_core_notify_all_result(vc_result_type_e result_type)
82 {
83         char* temp_text = NULL;
84         int event;
85         char* temp_message = NULL;
86         vc_cmd_list_h vc_cmd_list = NULL;
87
88         vc_mgr_all_result_cb all_callback = NULL;
89         void* all_user_data = NULL;
90
91         vc_mgr_client_get_all_result_cb(&all_callback, &all_user_data);
92         RETM_IF(NULL == all_callback, TAG_VCM, "[ERROR] All result callback is NULL");
93
94         if (VC_ERROR_NONE != vc_cmd_list_create(&vc_cmd_list)) {
95                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create command list");
96                 return;
97         }
98
99         vc_info_parser_get_result(&temp_text, &event, &temp_message, -1, vc_cmd_list, vc_mgr_client_get_exclusive_command());
100
101         SECURE_SLOG(LOG_INFO, TAG_VCM, "Result info : result type(%d) result text(%s) event(%d) result_message(%s)",
102                 result_type, temp_text, event, temp_message);
103
104         vc_cmd_print_list(vc_cmd_list);
105
106         bool cb_ret = false;
107
108         vc_mgr_client_set_all_result(event, temp_text);
109
110         vc_mgr_client_use_callback();
111         cb_ret = all_callback(event, vc_cmd_list, temp_text, temp_message, all_user_data);
112         vc_mgr_client_not_use_callback();
113
114         if (true == vc_mgr_client_get_exclusive_command()) {
115                 //LCOV_EXCL_START
116                 /* exclusive */
117                 vc_result_cb callback = NULL;
118                 void* user_data = NULL;
119
120                 vc_mgr_client_get_result_cb(&callback, &user_data);
121                 RETM_IF(NULL == callback, TAG_VCM, "[ERROR] Client result callback is NULL");
122
123                 vc_mgr_client_use_callback();
124                 callback(event, vc_cmd_list, temp_text, user_data);
125                 vc_mgr_client_not_use_callback();
126                 SLOG(LOG_DEBUG, TAG_VCM, "Exclusive result callback called");
127
128                 /* Release result */
129                 free(temp_text);
130                 temp_text = NULL;
131
132                 free(temp_message);
133                 temp_message = NULL;
134
135                 /* Release list */
136                 if (vc_cmd_list)
137                         vc_cmd_list_destroy(vc_cmd_list, true);
138                 vc_cmd_list = NULL;
139
140                 vc_mgr_client_set_exclusive_command(false);
141
142                 return;
143                 //LCOV_EXCL_STOP
144         }
145
146         int count = 0;
147         vc_cmd_list_get_count(vc_cmd_list, &count);
148         if (0 < count) {
149                 if (true == cb_ret) {
150                         SLOG(LOG_INFO, TAG_VCM, "Callback result is true");
151                         if (VC_RESULT_TYPE_NOTIFICATION != result_type)
152                                 ecore_idler_add(__vc_mgr_core_set_select_result, NULL);
153                 } else {
154                         SLOG(LOG_INFO, TAG_VCM, "Callback result is false");
155                         /* need to select conflicted result */
156                 }
157         } else {
158                 if (VC_RESULT_TYPE_NOTIFICATION != result_type)
159                         ecore_idler_add(__vc_mgr_core_set_select_result, NULL);
160
161                 vc_mgr_client_set_exclusive_command(false);
162                 vc_mgr_client_unset_all_result();
163         }
164
165         /* Release result */
166         free(temp_text);
167         temp_text = NULL;
168
169         free(temp_message);
170         temp_message = NULL;
171
172         /* Release list */
173         if (vc_cmd_list)
174                 vc_cmd_list_destroy(vc_cmd_list, true);
175         vc_cmd_list = NULL;
176 }
177
178 static void __vc_mgr_core_notify_result()
179 {
180         char* temp_text = NULL;
181         int event;
182         vc_cmd_list_h vc_cmd_list = NULL;
183
184         vc_result_cb callback = NULL;
185         void* user_data = NULL;
186
187         vc_mgr_client_get_result_cb(&callback, &user_data);
188         RETM_IF(NULL == callback, TAG_VCM, "[ERROR] Client result callback is NULL");
189
190         if (VC_ERROR_NONE != vc_cmd_list_create(&vc_cmd_list)) {
191                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create command list");
192                 return;
193         }
194
195         vc_info_parser_get_result(&temp_text, &event, NULL, getpid(), vc_cmd_list, false);
196
197         SECURE_SLOG(LOG_INFO, TAG_VCM, "Result : result text(%s) event(%d)", temp_text, event);
198
199         vc_cmd_print_list(vc_cmd_list);
200
201         vc_mgr_client_use_callback();
202         callback(event, vc_cmd_list, temp_text, user_data);
203         vc_mgr_client_not_use_callback();
204         SLOG(LOG_INFO, TAG_VCM, "Result callback called");
205
206         if (vc_cmd_list)
207                 vc_cmd_list_destroy(vc_cmd_list, true);
208         vc_cmd_list = NULL;
209
210         /* Release result */
211         free(temp_text);
212         temp_text = NULL;
213 }
214
215 void vc_mgr_core_send_all_result(vc_result_type_e type)
216 {
217         if (false == vc_mgr_client_get_exclusive_command()) {
218                 __vc_mgr_core_notify_all_result(type);
219         } else {
220                 __vc_mgr_core_notify_result();
221         }
222 }
223
224 //LCOV_EXCL_START
225 void vc_mgr_core_send_pre_result(vc_pre_result_event_e event, const char* pre_result)
226 {
227         vc_mgr_pre_result_cb callback = NULL;
228         void* user_data = NULL;
229
230         vc_mgr_client_get_pre_result_cb(&callback, &user_data);
231         RETM_IF(NULL == callback, TAG_VCM, "[ERROR] Client pre result callback is NULL");
232
233         vc_mgr_client_use_callback();
234         callback(event, pre_result, user_data);
235         vc_mgr_client_not_use_callback();
236         SLOG(LOG_INFO, TAG_VCM, "Pre result callback is called");
237 }
238 //LCOV_EXCL_STOP
239
240 void vc_mgr_core_send_system_result()
241 {
242         __vc_mgr_core_notify_result();
243 }
244
245 void vc_mgr_core_send_speech_detected()
246 {
247         vc_mgr_begin_speech_detected_cb callback = NULL;
248         void* user_data = NULL;
249
250         vc_mgr_client_get_speech_detected_cb(&callback, &user_data);
251         RETM_IF(NULL == callback, TAG_VCM, "[ERROR] Client speech detected callback is NULL");
252
253         vc_mgr_client_use_callback();
254         callback(user_data);
255         vc_mgr_client_not_use_callback();
256         SLOG(LOG_INFO, TAG_VCM, "Speech detected callback called");
257 }
258
259 void vc_mgr_core_notify_error()
260 {
261         vc_error_cb callback = NULL;
262         void* user_data = NULL;
263         int reason;
264
265         vc_mgr_client_get_error_cb(&callback, &user_data);
266         vc_mgr_client_get_error(&reason);
267
268         if (NULL != callback) {
269                 vc_mgr_client_use_callback();
270                 g_err_callback_status = true;
271                 callback(reason, user_data);
272                 g_err_callback_status = false;
273                 vc_mgr_client_not_use_callback();
274                 SLOG(LOG_INFO, TAG_VCM, "Error callback is called");
275         } else {
276                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Error callback is null");
277         }
278 }
279
280 int vc_mgr_core_set_selected_results(vc_cmd_list_h vc_cmd_list, int pid)
281 {
282         if (NULL != vc_cmd_list) {
283                 int event = 0;
284                 char* result_text = NULL;
285
286                 vc_mgr_client_get_all_result(&event, &result_text);
287
288                 vc_info_parser_set_result(result_text, event, NULL, vc_cmd_list, false);
289
290                 free(result_text);
291                 result_text = NULL;
292         }
293
294         /* Request */
295         int ret = vc_mgr_tidl_send_result_selection(pid);
296         if (0 != ret) {
297                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to send result selection : %d", ret);
298                 return ret;
299         } else {
300                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] result selection");
301         }
302
303         vc_mgr_client_unset_all_result();
304
305         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Select result DONE");
306
307         return VC_ERROR_NONE;
308 }
309
310 static void vc_mgr_core_notify_state_changed()
311 {
312         vc_state_changed_cb changed_callback = NULL;
313         void* user_data;
314
315         vc_mgr_client_get_state_changed_cb(&changed_callback, &user_data);
316
317         vc_state_e current_state;
318         vc_state_e previous_state;
319
320         vc_mgr_client_get_previous_state(&current_state, &previous_state);
321
322         if (NULL != changed_callback) {
323                 vc_mgr_client_use_callback();
324                 changed_callback(previous_state, current_state, user_data);
325                 vc_mgr_client_not_use_callback();
326                 SLOG(LOG_INFO, TAG_VCM, "State changed callback is called");
327         } else {
328                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] State changed callback is null");
329         }
330 }
331
332 int vc_mgr_core_send_error(int reason, int daemon_pid, char* msg)
333 {
334         vc_state_e state;
335         if (VC_ERROR_NONE != vc_mgr_client_get_client_state(&state)) {
336                 SLOG(LOG_ERROR, TAG_VCM, "[WARNING] Invalid client");
337                 return VC_ERROR_INVALID_PARAMETER;
338         }
339
340         /* check state */
341         if (state != VC_STATE_INITIALIZED && state != VC_STATE_READY) {
342                 SLOG(LOG_ERROR, TAG_VCM, "[WARNING] not connected client yet");
343                 return VC_ERROR_INVALID_STATE;
344         }
345
346         vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_NONE);
347
348         if (VC_ERROR_SERVICE_RESET == reason) {
349                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] VC daemon reset");
350
351                 vc_service_state_e service_state = -1;
352                 vc_mgr_client_get_service_state(&service_state);
353                 if (VC_SERVICE_STATE_UPDATING == service_state) {
354                         SLOG(LOG_INFO, TAG_VCM, "[INFO] VC daemon is terminated by update manager");
355                         return VC_ERROR_NONE;
356                 }
357
358                 vc_mgr_client_set_client_state(VC_STATE_INITIALIZED);
359                 vc_mgr_core_notify_state_changed();
360
361                 if (VC_ERROR_NONE != vc_mgr_prepare()) {
362                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to prepare");
363                 }
364         }
365
366         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Error reason(%d), msg(%s)", reason, msg);
367
368         vc_mgr_client_set_error(reason);
369         vc_mgr_client_set_error_message(msg);
370         vc_mgr_core_notify_error();
371
372         return VC_ERROR_NONE;
373 }
374
375 int vc_mgr_core_get_error_message(char** err_msg)
376 {
377         if (false == g_err_callback_status) {
378                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Not in error callback");
379                 return VC_ERROR_OPERATION_FAILED;
380         }
381
382         int ret = vc_mgr_client_get_error_message(err_msg);
383         if (0 != ret) {
384                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get error message");
385         }
386
387         return ret;
388 }
389
390 int vc_mgr_core_send_service_state(int state)
391 {
392         vc_service_state_e current_state = (vc_service_state_e)state;
393         vc_service_state_e previous_state;
394         vc_mgr_client_get_service_state(&previous_state);
395
396         SLOG(LOG_INFO, TAG_VCM, "Service State changed : previous(%d) current(%d)",
397                 previous_state, current_state);
398
399         vc_internal_state_e internal_state = -1;
400         vc_mgr_client_get_internal_state(&internal_state);
401         if ((VC_INTERNAL_STATE_STARTING == internal_state && VC_SERVICE_STATE_RECORDING == current_state) ||
402                 (VC_INTERNAL_STATE_STOPPING == internal_state && VC_SERVICE_STATE_PROCESSING == current_state) ||
403                 (VC_INTERNAL_STATE_CANCELING == internal_state && VC_SERVICE_STATE_READY == current_state)) {
404                         SLOG(LOG_INFO, TAG_VCM, "Internal state is changed to NONE");
405                         vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_NONE);
406         }
407         if (VC_SERVICE_STATE_UPDATING == current_state) {
408                 SLOG(LOG_INFO, TAG_VCM, "Internal state is changed to NONE by updating");
409                 vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_NONE);
410         }
411
412         if (current_state == previous_state) {
413                 SLOG(LOG_WARN, TAG_VCM, "Service State NOT changed : previous(%d) current(%d)",
414                         previous_state, current_state);
415                 return VC_ERROR_NONE;
416         }
417
418         /* Save service state */
419         vc_mgr_client_set_service_state(current_state);
420
421         vc_service_state_changed_cb callback = NULL;
422         void* service_user_data = NULL;
423         vc_mgr_client_get_service_state_changed_cb(&callback, &service_user_data);
424
425         if (NULL != callback) {
426                 vc_mgr_client_use_callback();
427                 callback(previous_state, current_state, service_user_data);
428                 vc_mgr_client_not_use_callback();
429                 SLOG(LOG_INFO, TAG_VCM, "Service state changed callback is called");
430         } else {
431                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Service state changed callback is null");
432         }
433
434         return VC_ERROR_NONE;
435 }
436
437 //LCOV_EXCL_START
438 int vc_mgr_core_send_dialog(int pid, const char* disp_text, const char* utt_text, bool continuous)
439 {
440         vc_mgr_dialog_request_cb callback = NULL;
441         void* user_data = NULL;
442
443         vc_mgr_client_get_dialog_request_cb(&callback, &user_data);
444
445         if (NULL != callback) {
446                 vc_mgr_client_use_callback();
447                 callback(pid, disp_text, utt_text, continuous, user_data);
448                 vc_mgr_client_not_use_callback();
449                 SLOG(LOG_INFO, TAG_VCM, "Dialog callback is called, disp_text(%s), utt)text(%s), continuous(%d)", disp_text, utt_text, continuous);
450         } else {
451                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Error callback is null");
452         }
453
454         return VC_ERROR_NONE;
455 }
456
457 int vc_mgr_core_send_private_data_set(const char* key, const char* data)
458 {
459         vc_mgr_private_data_set_cb callback = NULL;
460         void* user_data = NULL;
461         int ret = -1;
462
463         vc_mgr_client_get_private_data_set_cb(&callback, &user_data);
464
465         if (NULL != callback) {
466                 vc_mgr_client_use_callback();
467                 ret = callback(key, data, user_data);
468                 vc_mgr_client_not_use_callback();
469                 SLOG(LOG_INFO, TAG_VCM, "Private data set callback is called");
470         } else {
471                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Private data set callback is null");
472         }
473
474         return ret;
475 }
476
477 int vc_mgr_core_send_private_data_requested(const char* key, char** data)
478 {
479         vc_mgr_private_data_requested_cb callback = NULL;
480         void* user_data = NULL;
481         int ret = -1;
482
483         vc_mgr_client_get_private_data_requested_cb(&callback, &user_data);
484
485         if (NULL != callback) {
486                 vc_mgr_client_use_callback();
487                 ret = callback(key, data, user_data);
488                 vc_mgr_client_not_use_callback();
489                 SLOG(LOG_INFO, TAG_VCM, "Private data requested callback is called");
490         } else {
491                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Private data requested callback is null");
492         }
493
494         return ret;
495 }
496
497 void vc_mgr_core_send_specific_engine_result(const char* engine_app_id, const char* event, const char* result)
498 {
499         vc_mgr_specific_engine_result_cb callback = NULL;
500         void* user_data = NULL;
501
502         vc_mgr_client_get_specific_engine_result_cb(&callback, &user_data);
503         RETM_IF(NULL == callback, TAG_VCM, "[ERROR] Client specific engine result callback is NULL");
504
505         vc_mgr_client_use_callback();
506         callback(engine_app_id, event, result, user_data);
507         vc_mgr_client_not_use_callback();
508         SLOG(LOG_INFO, TAG_VCM, "Specific engine result callback is called, engine app id(%s), event(%s), result(%s)", engine_app_id, event, result);
509 }
510 //LCOV_EXCL_STOP
511
512 int vc_mgr_core_send_set_volume(float volume)
513 {
514         g_prev_volume_db = g_volume_db;
515         g_cur_volume_db = volume;
516
517         g_volume_db = g_prev_volume_db + (g_cur_volume_db - g_prev_volume_db) / 5;
518
519         if (NULL != g_m_set_volume_timer) {
520                 ecore_timer_del(g_m_set_volume_timer);
521         }
522
523         g_volume_timer_cnt = 1;
524         g_m_set_volume_timer = ecore_timer_add(0.05, __vc_mgr_core_set_volume_timer_cb, NULL);
525
526         return VC_ERROR_NONE;
527 }
528
529 void vc_mgr_core_set_volume(float volume)
530 {
531         g_volume_db = volume;
532 }
533
534 float vc_mgr_core_get_volume()
535 {
536         return g_volume_db;
537 }
538
539 void vc_mgr_core_initialize_volume_variable()
540 {
541         g_volume_db = 0;
542         g_prev_volume_db = 0;
543         g_cur_volume_db = 0;
544 }
545
546 //LCOV_EXCL_START
547 int vc_mgr_core_send_set_foreground(int pid, bool value)
548 {
549         vc_mgr_client_set_foreground(pid, value);
550
551         /* get authorized valid app */
552         int tmp_pid;
553         if (VC_ERROR_NONE != vc_mgr_client_get_valid_authorized_client(&tmp_pid)) {
554                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get authorized valid app");
555                 return VC_ERROR_INVALID_PARAMETER;
556         }
557
558         if (true == value) {
559                 /* compare & set valid */
560                 if (tmp_pid != pid) {
561                         SLOG(LOG_INFO, TAG_VCM, "Authority(%d) changed to invalid", tmp_pid);
562
563                         /* set authorized valid */
564                         if (true == vc_mgr_client_is_authorized_client(pid)) {
565                                 SLOG(LOG_DEBUG, TAG_VCM, "Authority(%d) change to valid", pid);
566                                 vc_mgr_client_set_valid_authorized_client(pid);
567                         } else {
568                                 SLOG(LOG_DEBUG, TAG_VCM, "No valid Authority");
569                                 vc_mgr_client_set_valid_authorized_client(-1);
570                         }
571                 }
572         } else {
573                 if (tmp_pid == pid) {
574                         SLOG(LOG_INFO, TAG_VCM, "Authority(%d) changed to invalid", tmp_pid);
575                         vc_mgr_client_set_valid_authorized_client(-1);
576                 }
577         }
578
579         return VC_ERROR_NONE;
580 }
581
582 /* for TTS feedback */
583 int vc_mgr_core_send_feedback_audio_format(int rate, vc_audio_channel_e channel, vc_audio_type_e audio_type)
584 {
585         vc_mgr_feedback_audio_format_cb callback = NULL;
586         void* user_data = NULL;
587
588         /* set global audio formats */
589         g_feedback_rate = rate;
590         g_feedback_audio_channel = channel;
591         g_feedback_audio_type = audio_type;
592
593         vc_mgr_client_get_feedback_audio_format_cb(&callback, &user_data);
594
595         if (NULL != callback) {
596                 vc_mgr_client_use_callback();
597                 callback(rate, channel, audio_type, user_data);
598                 vc_mgr_client_not_use_callback();
599                 SLOG(LOG_INFO, TAG_VCM, "TTS feedback audio format callback is called");
600         } else {
601                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] TTS feedback audio format callback is null");
602         }
603
604         return VC_ERROR_NONE;
605 }
606
607 int vc_mgr_core_send_feedback_streaming(int pid, int utt_id, vc_feedback_event_e event, char* buffer, int len)
608 {
609         /* add feedback data */
610         vc_feedback_data_s* temp_feedback_data = NULL;
611         temp_feedback_data = (vc_feedback_data_s*)calloc(1, sizeof(vc_feedback_data_s));
612         RETVM_IF(NULL == temp_feedback_data, VC_ERROR_OUT_OF_MEMORY, TAG_VCM, "[ERROR] Out of memory");
613         SLOG(LOG_INFO, TAG_VCM, "[INFO] feedback streaming before queuing");
614
615         temp_feedback_data->data = NULL;
616         temp_feedback_data->rate = g_feedback_rate;
617         temp_feedback_data->data_size = 0;
618
619         if (0 < len) {
620                 temp_feedback_data->data = (char*)calloc(len + 5, sizeof(char));
621                 if (NULL != temp_feedback_data->data) {
622                         memcpy(temp_feedback_data->data, buffer, len);
623                         temp_feedback_data->data_size = len;
624                         SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG][memcpy] data(%p) size(%d)",
625                                         temp_feedback_data->data, temp_feedback_data->data_size);
626                 } else {
627                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] fail to allocate memory");
628                 }
629         } else {
630                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] feedback data is NULL");
631         }
632
633         temp_feedback_data->pid = pid;
634         temp_feedback_data->utt_id = utt_id;
635         temp_feedback_data->event = event;
636         temp_feedback_data->audio_type = g_feedback_audio_type;
637         temp_feedback_data->channel = g_feedback_audio_channel;
638
639         SLOG(LOG_INFO, TAG_VCM, "[INFO] add feedback data, pid(%d), utt_id(%d), event(%d), audio_type(%d), channel(%d)", pid, utt_id, event, g_feedback_audio_type, g_feedback_audio_channel);
640
641         int ret = vc_mgr_data_add_feedback_data(temp_feedback_data);
642         if (VC_ERROR_NONE != ret) {
643                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to add feedback data");
644                 free(temp_feedback_data->data);
645                 temp_feedback_data->data = NULL;
646
647                 free(temp_feedback_data);
648                 temp_feedback_data = NULL;
649         }
650
651         return ret;
652 }
653
654 /* Authority */
655 static bool __vc_mgr_core_check_demandable_client(int pid)
656 {
657         if (0 == g_slist_length(g_demandable_client_list)) {
658                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] No demandable clients");
659                 return false;
660         }
661
662         char appid[1024] = {'\0', };
663         if (VC_ERROR_NONE != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
664                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get callee appid by pid");
665                 return false;
666         }
667         SLOG(LOG_INFO, TAG_VCM, "[CHECK] Appid - %s", appid);
668
669         GSList *iter = NULL;
670         vc_demandable_client_s* temp_client;
671         iter = g_slist_nth(g_demandable_client_list, 0);
672
673         while (NULL != iter) {
674                 temp_client = iter->data;
675
676                 if (NULL != temp_client) {
677                         if (NULL != temp_client->appid) {
678                                 if (!strcmp(temp_client->appid, appid)) {
679                                         SLOG(LOG_DEBUG, TAG_VCM, "pid(%d) is available", pid);
680                                         return true;
681                                 }
682                         }
683                 }
684
685                 iter = g_slist_next(iter);
686         }
687
688         return false;
689 }
690
691 int vc_mgr_core_set_demandable_client_rule(const char* rule)
692 {
693         if (0 != vc_info_parser_set_demandable_client(rule)) {
694                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] rule is NOT valid");
695                 SLOG(LOG_DEBUG, TAG_VCM, "@@@");
696                 return VC_ERROR_INVALID_PARAMETER;
697         }
698
699         if (0 != vc_info_parser_get_demandable_clients(&g_demandable_client_list)) {
700                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get demandable clients");
701                 return VC_ERROR_OPERATION_FAILED;
702         }
703
704         return VC_ERROR_NONE;
705 }
706
707 int vc_mgr_core_request_auth_enable(int pid)
708 {
709         if (false == __vc_mgr_core_check_demandable_client(pid)) {
710                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Not demandable client");
711                 return VC_ERROR_INVALID_PARAMETER;
712         }
713
714         /* check already authorized */
715         if (true == vc_mgr_client_is_authorized_client(pid)) {
716                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Already authorized");
717                 return VC_ERROR_INVALID_PARAMETER;
718         }
719
720         /* add authorized list */
721         if (VC_ERROR_NONE != vc_mgr_client_add_authorized_client(pid)) {
722                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to add authorized client");
723                 return VC_ERROR_OPERATION_FAILED;
724         }
725
726         /* foreground check */
727         int fore_pid = 0;
728         if (VC_ERROR_NONE != vc_mgr_client_get_foreground(&fore_pid)) {
729                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get foreground");
730                 return VC_ERROR_OPERATION_FAILED;
731         }
732
733         if (pid == fore_pid) {
734                 vc_mgr_client_set_valid_authorized_client(pid);
735         }
736
737         return VC_ERROR_NONE;
738 }
739
740 int vc_mgr_core_request_auth_disable(int pid)
741 {
742         /* check authorized */
743         if (false == vc_mgr_client_is_authorized_client(pid)) {
744                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No authorized");
745                 return VC_ERROR_INVALID_PARAMETER;
746         }
747
748         /* remove authorized list */
749         if (VC_ERROR_NONE != vc_mgr_client_remove_authorized_client(pid)) {
750                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to remove authorized client");
751                 return VC_ERROR_OPERATION_FAILED;
752         }
753
754         /* check authority valid */
755         if (true == vc_mgr_client_is_valid_authorized_client(pid)) {
756                 SLOG(LOG_DEBUG, TAG_VCM, "Valid authorized client is removed");
757                 if (VC_ERROR_NONE != vc_mgr_client_set_valid_authorized_client(-1)) {
758                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set valid authorized client");
759                         return VC_ERROR_OPERATION_FAILED;
760                 }
761         }
762
763         return VC_ERROR_NONE;
764 }
765
766 static Eina_Bool __request_auth_start(void* data)
767 {
768         SLOG(LOG_INFO, TAG_VCM, "Request Start");
769
770         if (VC_ERROR_NONE != vc_mgr_client_set_start_by_client(true)) {
771                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set start by client");
772         }
773
774         if (VC_ERROR_NONE != vc_mgr_start(false)) {
775                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Request start is failed");
776                 /* TODO - Error handling? */
777         }
778
779         if (VC_ERROR_NONE != vc_mgr_client_set_start_by_client(false)) {
780                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set start by client");
781         }
782
783
784         return EINA_FALSE;
785 }
786
787 int vc_mgr_core_request_auth_start(int pid)
788 {
789         /* check authorized */
790         if (false == vc_mgr_client_is_valid_authorized_client(pid)) {
791                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No valid authorized client");
792                 return VC_ERROR_INVALID_PARAMETER;
793         }
794
795         /* add timer for start recording */
796         ecore_timer_add(0, __request_auth_start, NULL);
797
798         return VC_ERROR_NONE;
799 }
800
801 static Eina_Bool __request_auth_stop(void* data)
802 {
803         SLOG(LOG_INFO, TAG_VCM, "Request Stop");
804
805         if (VC_ERROR_NONE != vc_mgr_stop()) {
806                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Request stop is failed");
807                 /* TODO - Error handling? */
808         }
809
810         return EINA_FALSE;
811 }
812
813 int vc_mgr_core_request_auth_stop(int pid)
814 {
815         /* check authorized */
816         if (false == vc_mgr_client_is_valid_authorized_client(pid)) {
817                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No valid authorized client");
818                 return VC_ERROR_INVALID_PARAMETER;
819         }
820
821         /* add timer for start recording */
822         ecore_timer_add(0, __request_auth_stop, NULL);
823
824         return VC_ERROR_NONE;
825 }
826
827 static Eina_Bool __request_auth_cancel(void* data)
828 {
829         SLOG(LOG_INFO, TAG_VCM, "Request Cancel");
830
831         if (VC_ERROR_NONE != vc_mgr_cancel()) {
832                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Request cancel is failed");
833                 /* TODO - Error handling? */
834         }
835
836         return EINA_FALSE;
837 }
838
839 int vc_mgr_core_request_auth_cancel(int pid)
840 {
841         /* check authorized */
842         if (false == vc_mgr_client_is_valid_authorized_client(pid)) {
843                 SLOG(LOG_ERROR,  TAG_VCM, "[ERROR] No valid authorized client");
844                 return VC_ERROR_INVALID_PARAMETER;
845         }
846
847         /* add timer for start recording */
848         ecore_timer_add(0, __request_auth_cancel, NULL);
849
850         return VC_ERROR_NONE;
851 }
852 //LCOV_EXCL_STOP