e974664704f40beb30041b6ca542fb2fd77c3de8
[platform/core/uifw/voice-control.git] / client / vc_mgr.c
1 /*
2 * Copyright (c) 2011-2015 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
17 #include <aul.h>
18 #include <system_info.h>
19 #include <cynara-client.h>
20 #include <cynara-error.h>
21 #include <cynara-session.h>
22 #include <pthread.h>
23 #include <math.h>
24
25 #include "vc_cmd_db.h"
26 #include "vc_config_mgr.h"
27 #include "vc_command.h"
28 #include "vc_info_parser.h"
29 #include "vc_json_parser.h"
30 #include "vc_main.h"
31 #include "vc_mgr_core.h"
32 #include "vc_mgr_client.h"
33 #include "vc_mgr_tidl.h"
34 #include "vc_mgr_data.h"
35 #include "vc_mgr_player.h"
36 #include "vc_mgr_ducking.h"
37 #include "voice_control_command.h"
38 #include "voice_control_command_expand.h"
39 #include "voice_control_common.h"
40 #include "voice_control_manager.h"
41 #include "voice_control_manager_internal.h"
42
43
44 #define VC_MANAGER_CONFIG_HANDLE        10000000
45
46 static Ecore_Timer* g_send_hello_timer = NULL;
47 static Ecore_Timer* g_request_init_timer = NULL;
48 static int g_tidl_send_hello_count = 0;
49
50 static int g_pid;
51
52 static int g_daemon_pid = 0;
53
54 static int g_feature_enabled = -1;
55
56 static bool g_privilege_allowed = false;
57
58 static cynara *p_cynara = NULL;
59 static pthread_mutex_t g_cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
60
61 /* for TTS feedback */
62 static Ecore_Thread* g_feedback_thread = NULL;
63
64 static pthread_mutex_t g_feedback_streaming_cb_mutex = PTHREAD_MUTEX_INITIALIZER;
65 static pthread_mutex_t g_vc_tts_streaming_cb_mutex = PTHREAD_MUTEX_INITIALIZER;
66
67 static Eina_Bool __vc_mgr_notify_state_changed(void *data);
68
69 static const char* __vc_mgr_get_error_code(vc_error_e err)
70 {
71         switch (err) {
72         case VC_ERROR_NONE:                     return "VC_ERROR_NONE";
73         case VC_ERROR_OUT_OF_MEMORY:            return "VC_ERROR_OUT_OF_MEMORY";
74         case VC_ERROR_IO_ERROR:                 return "VC_ERROR_IO_ERROR";
75         case VC_ERROR_INVALID_PARAMETER:        return "VC_ERROR_INVALID_PARAMETER";
76         case VC_ERROR_TIMED_OUT:                return "VC_ERROR_TIMED_OUT";
77         case VC_ERROR_RECORDER_BUSY:            return "VC_ERROR_RECORDER_BUSY";
78         case VC_ERROR_INVALID_STATE:            return "VC_ERROR_INVALID_STATE";
79         case VC_ERROR_INVALID_LANGUAGE:         return "VC_ERROR_INVALID_LANGUAGE";
80         case VC_ERROR_ENGINE_NOT_FOUND:         return "VC_ERROR_ENGINE_NOT_FOUND";
81         case VC_ERROR_OPERATION_FAILED:         return "VC_ERROR_OPERATION_FAILED";
82         default:                                return "Invalid error code";
83         }
84         return NULL;
85 }
86
87 static void __vc_mgr_lang_changed_cb(const char* previous_lang, const char* current_lang)
88 {
89         SLOG(LOG_INFO, TAG_VCM, "Lang changed : previous lang(%s) current lang(%s)",
90                                 previous_lang, current_lang);
91
92         vc_current_language_changed_cb callback = NULL;
93         void* lang_user_data;
94         vc_mgr_client_get_current_lang_changed_cb(&callback, &lang_user_data);
95
96         if (NULL != callback) {
97                 vc_mgr_client_use_callback();
98                 callback(previous_lang, current_lang, lang_user_data);
99                 vc_mgr_client_not_use_callback();
100                 SLOG(LOG_INFO, TAG_VCM, "Language changed callback is called");
101         } else {
102                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Language changed callback is null");
103         }
104
105         return;
106 }
107
108 static int __vc_mgr_get_feature_enabled()
109 {
110         if (0 == g_feature_enabled) {
111                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Voice control feature NOT supported");
112                 return VC_ERROR_NOT_SUPPORTED;
113         } else if (-1 == g_feature_enabled) {
114                 bool vc_supported = false;
115                 bool mic_supported = false;
116                 if (0 == system_info_get_platform_bool(VC_MGR_FEATURE_PATH, &vc_supported)) {
117                         if (0 == system_info_get_platform_bool(VC_MIC_FEATURE_PATH, &mic_supported)) {
118                                 if (false == vc_supported || false == mic_supported) {
119                                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Voice control feature NOT supported");
120                                         g_feature_enabled = 0;
121                                         return VC_ERROR_NOT_SUPPORTED;
122                                 }
123
124                                 g_feature_enabled = 1;
125                         }
126                 }
127         }
128
129         return VC_ERROR_NONE;
130 }
131
132 static int __check_privilege_initialize()
133 {
134         int ret = cynara_initialize(&p_cynara, NULL);
135         if (NULL == p_cynara || CYNARA_API_SUCCESS != ret) {
136                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] fail to initialize(%d)", ret);
137                 return ret;
138         }
139
140         return ret == CYNARA_API_SUCCESS;
141 }
142
143 static int __check_privilege(const char* uid, const char * privilege)
144 {
145         FILE *fp = NULL;
146         char label_path[1024] = "/proc/self/attr/current";
147         char smack_label[1024] = {'\0',};
148
149         if (!p_cynara) {
150                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] p_cynara is NULL");
151                 return false;
152         }
153
154         fp = fopen(label_path, "r");
155         if (fp != NULL) {
156                 if (fread(smack_label, 1, sizeof(smack_label), fp) <= 0)
157                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] fail to fread");
158
159                 fclose(fp);
160         }
161
162         pid_t pid = getpid();
163         char *session = cynara_session_from_pid(pid);
164         int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
165         if (session)
166                 free(session);
167         session = NULL;
168
169         if (ret != CYNARA_API_ACCESS_ALLOWED) {
170                 SLOG(LOG_INFO, TAG_VCM, "[Client]cynara_check returned %d(Denied), p_cynara(%p), label(%s), session(%s), uid(%s), priv(%s)", ret, p_cynara, smack_label, session, uid, privilege);
171                 return false;
172         }
173
174         return true;
175 }
176
177 static void __check_privilege_deinitialize()
178 {
179         if (p_cynara) {
180                 int ret = cynara_finish(p_cynara);
181                 if (ret != CYNARA_API_SUCCESS)
182                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] cynara finish %d", ret);
183         }
184         p_cynara = NULL;
185 }
186
187 static int __vc_mgr_check_privilege()
188 {
189         if (true == g_privilege_allowed)
190                 return VC_ERROR_NONE;
191
192         pthread_mutex_lock(&g_cynara_mutex);
193
194         if (false == g_privilege_allowed) {
195                 bool ret = true;
196                 ret = __check_privilege_initialize();
197                 if (false == ret) {
198                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] privilege initialize is failed");
199                         g_privilege_allowed = false;
200                         pthread_mutex_unlock(&g_cynara_mutex);
201                         return VC_ERROR_PERMISSION_DENIED;
202                 }
203
204                 char uid[32];
205                 snprintf(uid, 32, "%d", getuid());
206                 ret = true;
207                 ret = __check_privilege(uid, VC_PRIVILEGE_RECORDER);
208                 if (false == ret) {
209                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Permission is denied(%s)(%s)", VC_PRIVILEGE_RECORDER, uid);
210                         __check_privilege_deinitialize();
211                         g_privilege_allowed = false;
212                         pthread_mutex_unlock(&g_cynara_mutex);
213                         return VC_ERROR_PERMISSION_DENIED;
214                 }
215
216                 ret = __check_privilege(uid, VC_MGR_PRIVILEGE);
217                 if (false == ret) {
218                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Permission is denied(%s)(%s)", VC_MGR_PRIVILEGE, uid);
219                         __check_privilege_deinitialize();
220                         g_privilege_allowed = false;
221                         pthread_mutex_unlock(&g_cynara_mutex);
222                         return VC_ERROR_PERMISSION_DENIED;
223                 }
224
225                 __check_privilege_deinitialize();
226         }
227
228         g_privilege_allowed = true;
229         pthread_mutex_unlock(&g_cynara_mutex);
230         return VC_ERROR_NONE;
231 }
232
233 int __check_mgr_feature_privilege()
234 {
235         if (0 != __vc_mgr_get_feature_enabled()) {
236                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
237                 return VC_ERROR_NOT_SUPPORTED;
238         }
239
240         if (0 != __vc_mgr_check_privilege()) {
241                 return VC_ERROR_PERMISSION_DENIED;
242         }
243
244         return VC_ERROR_NONE;
245 }
246
247 int vc_mgr_initialize(void)
248 {
249         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Initialize");
250
251         int ret;
252         ret = __check_mgr_feature_privilege();
253         if (VC_ERROR_NONE != ret)
254                 return ret;
255
256         /* check handle */
257         if (true == vc_mgr_client_is_valid()) {
258                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Already initialized");
259                 return VC_ERROR_NONE;
260         }
261
262         if (0 != vc_mgr_tidl_open_connection()) {
263                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to open tidl connection");
264                 return VC_ERROR_OPERATION_FAILED;
265         }
266
267         if (0 != vc_mgr_client_create()) {
268                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create client!!!!!");
269                 return VC_ERROR_OUT_OF_MEMORY;
270         }
271
272         g_pid = getpid();
273         ret = vc_config_mgr_initialize(g_pid + VC_MANAGER_CONFIG_HANDLE);
274         if (0 != ret) {
275                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to init config manager : %d", ret);
276                 vc_mgr_client_destroy();
277                 return VC_ERROR_OPERATION_FAILED;
278         }
279
280         ret = vc_config_mgr_set_lang_cb(g_pid + VC_MANAGER_CONFIG_HANDLE, __vc_mgr_lang_changed_cb);
281         if (0 != ret) {
282                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set callback : %d", ret);
283                 vc_config_mgr_finalize(g_pid + VC_MANAGER_CONFIG_HANDLE);
284                 vc_mgr_client_destroy();
285                 return VC_ERROR_OPERATION_FAILED;
286         }
287
288         ret = vc_mgr_client_set_audio_streaming_mode(VC_AUDIO_STREAMING_MODE_VC_SERVICE);
289         if (0 != ret) {
290                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set audio streaming mode : %d", ret);
291                 vc_config_mgr_finalize(g_pid + VC_MANAGER_CONFIG_HANDLE);
292                 vc_mgr_client_destroy();
293                 return ret;
294         }
295
296         /* for TTS feedback */
297 /*      ret = vc_mgr_player_init();
298         if (0 != ret) {
299                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to initialize VC mgr player : %d", ret);
300         }
301 */
302
303         ret = vc_mgr_ducking_create();
304         if (VC_ERROR_NONE != ret) {
305                 SLOG(LOG_INFO, TAG_VCM, "[INFO] Fail to ducking create : %d", ret);
306         }
307
308         SLOG(LOG_ERROR, TAG_VCM, "[Success] pid(%d)", g_pid);
309
310         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Initialize DONE");
311
312         return VC_ERROR_NONE;
313 }
314
315 static void __vc_mgr_internal_unprepare()
316 {
317         int ret = vc_mgr_tidl_request_finalize(g_pid);
318         if (0 != ret) {
319                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request finalize : %s", __vc_mgr_get_error_code(ret));
320         }
321
322         vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_NONE);
323         return;
324 }
325
326 int vc_mgr_deinitialize(void)
327 {
328         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Deinitialize");
329
330         int ret;
331         ret = __check_mgr_feature_privilege();
332         if (VC_ERROR_NONE != ret)
333                 return ret;
334
335         if (false == vc_mgr_client_is_valid()) {
336                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] NOT initialized");
337                 return VC_ERROR_INVALID_STATE;
338         }
339
340         vc_state_e state;
341         vc_mgr_client_get_client_state(&state);
342
343         /* check state */
344         switch (state) {
345         case VC_STATE_READY:
346                 __vc_mgr_internal_unprepare();
347                 /* no break. need to next step*/
348         case VC_STATE_INITIALIZED:
349                 if (NULL != g_send_hello_timer) {
350                         SLOG(LOG_DEBUG, TAG_VCM, "Connect Timer is deleted");
351                         ecore_timer_del(g_send_hello_timer);
352                         g_send_hello_timer = NULL;
353                 }
354                 if (g_request_init_timer) {
355                         SLOG(LOG_DEBUG, TAG_VCM, "Connect idler is deleted");
356                         ecore_timer_del(g_request_init_timer);
357                         g_request_init_timer = NULL;
358                 }
359
360                 vc_config_mgr_unset_lang_cb(g_pid + VC_MANAGER_CONFIG_HANDLE);
361                 vc_config_mgr_finalize(g_pid + VC_MANAGER_CONFIG_HANDLE);
362
363                 /* Free client resources */
364                 vc_mgr_client_destroy();
365                 g_pid = 0;
366                 break;
367         case VC_STATE_NONE:
368                 break;
369         }
370
371         SLOG(LOG_DEBUG, TAG_VCM, "Success: destroy");
372
373         int cnt = VC_COMMAND_TYPE_FOREGROUND;
374         do {
375                 ret = vc_cmd_parser_delete_file(getpid(), cnt);
376                 if (0 != ret)
377                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to delete file, type(%d), ret(%d)", cnt, ret);
378         } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
379
380         ret = vc_db_finalize();
381         if (0 != ret) {
382                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to finalize DB, ret(%d)", ret);
383         }
384
385         /* for TTS feedback */
386 /*      ret = vc_mgr_player_release();
387         if (0 != ret) {
388                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to release VC mgr player(%d)", ret);
389         }
390 */
391
392         ret = vc_mgr_ducking_destroy();
393         if (VC_ERROR_NONE != ret) {
394                 SLOG(LOG_INFO, TAG_VCM, "[INFO] Fail to ducking destroy : %d", ret);
395         }
396
397         if (0 != vc_mgr_tidl_close_connection()) {
398                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to close connection");
399         }
400
401         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Deinitialize DONE");
402
403         return VC_ERROR_NONE;
404 }
405
406 static void __notify_error(void *data)
407 {
408         vc_mgr_core_notify_error();
409 }
410
411 static Eina_Bool __request_initialize(void *data)
412 {
413         /* request initialization */
414         int ret = -1;
415         int service_state = 0;
416         int foreground = VC_RUNTIME_INFO_NO_FOREGROUND;
417
418
419         /* check handle */
420         if (true == vc_mgr_client_is_valid()) {
421                 SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG]  is valid");
422
423                 /* Initialize DB */
424                 ret = vc_db_initialize();
425                 if (0 != ret) {
426                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to initialize DB : %d", ret);
427                         return EINA_TRUE;
428                 }
429
430                 vc_audio_streaming_mode_e streaming_mode;
431                 vc_mgr_client_get_audio_streaming_mode(&streaming_mode);
432
433                 ret = vc_mgr_tidl_request_initialize(g_pid, (int)streaming_mode, &service_state, &foreground, &g_daemon_pid);
434
435                 if (VC_ERROR_ENGINE_NOT_FOUND == ret) {
436                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to initialize : %s", __vc_mgr_get_error_code(ret));
437
438                         vc_mgr_client_set_error(VC_ERROR_ENGINE_NOT_FOUND);
439                         ecore_main_loop_thread_safe_call_async(__notify_error, NULL);
440
441                         SLOG(LOG_DEBUG, TAG_VCM, "@@@");
442                         g_request_init_timer = NULL;
443                         return EINA_FALSE;
444
445                 } else if (0 != ret) {
446                         SLOG(LOG_ERROR, TAG_VCM, "[WARNING] Fail to connection. Retry to connect : %s", __vc_mgr_get_error_code(ret));
447                         ret = vc_db_finalize();
448                         if (0 != ret) {
449                                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to finalize DB : %d", ret);
450                         }
451                         return EINA_TRUE;
452                 } else {
453                         /* Success to connect */
454                 }
455
456                 /* Set service state */
457                 vc_service_state_e previous_service_state;
458                 vc_mgr_client_get_service_state(&previous_service_state);
459                 vc_mgr_client_set_service_state((vc_service_state_e)service_state);
460
461                 vc_service_state_changed_cb service_changed_callback = NULL;
462                 void* user_data = NULL;
463                 vc_mgr_client_get_service_state_changed_cb(&service_changed_callback, &user_data);
464
465                 if (NULL != service_changed_callback) {
466                         vc_mgr_client_use_callback();
467                         service_changed_callback(previous_service_state, service_state, user_data);
468                         vc_mgr_client_not_use_callback();
469                         SLOG(LOG_INFO, TAG_VCM, "Service state changed callback is called");
470                 } else {
471                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] Service state changed callback is null");
472                 }
473
474                 /* Set foreground */
475                 vc_mgr_client_set_foreground(foreground, true);
476
477                 SLOG(LOG_ERROR, TAG_VCM, "[SUCCESS] Connected daemon");
478
479                 /* Set client state */
480                 vc_mgr_client_set_client_state(VC_STATE_READY);
481
482                 vc_state_changed_cb changed_callback = NULL;
483                 user_data = NULL;
484                 vc_mgr_client_get_state_changed_cb(&changed_callback, &user_data);
485
486                 vc_state_e current_state;
487                 vc_state_e previous_state;
488
489                 vc_mgr_client_get_previous_state(&current_state, &previous_state);
490
491                 if (NULL != changed_callback) {
492                         vc_mgr_client_use_callback();
493                         changed_callback(previous_state, current_state, user_data);
494                         vc_mgr_client_not_use_callback();
495                         SLOG(LOG_INFO, TAG_VCM, "State changed callback is called");
496                 } else {
497                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] State changed callback is null");
498                 }
499         } else {
500                 SLOG(LOG_ERROR, TAG_VCM, "[Not ERROR]  is not valid. It is destroyed."); //LCOV_EXCL_LINE
501                 g_request_init_timer = NULL;
502                 return EINA_FALSE;
503         }
504
505         SLOG(LOG_ERROR, TAG_VCM, "@@@");
506
507         g_request_init_timer = NULL;
508         return EINA_FALSE;
509 }
510
511
512 static Eina_Bool __send_hello_message(void *data)
513 {
514         /* Send hello */
515         if (0 != vc_mgr_tidl_request_hello()) {
516                 if (g_tidl_send_hello_count == VCM_TIDL_RETRY_COUNT) {
517                         g_tidl_send_hello_count = 0;
518                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request hello !! send error to manager");
519                         vc_mgr_core_send_error(VC_ERROR_TIMED_OUT, -1, "voice_framework.error.vcfw.connect_engine_fail");
520                         g_send_hello_timer = NULL;
521                         return EINA_FALSE;
522                 } else {
523                         g_tidl_send_hello_count++;
524                         return EINA_TRUE;
525                 }
526         }
527
528         SLOG(LOG_DEBUG, TAG_VCM, "===== [Manager] Connect daemon");
529
530         if (NULL == g_request_init_timer) {
531                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Create a new idler for preparation");
532                 g_request_init_timer = ecore_timer_add(0.0, __request_initialize, NULL);
533         } else {
534                 SLOG(LOG_INFO, TAG_VCM, "[INFO] idler handle is already created");
535         }
536
537         g_send_hello_timer = NULL;
538         return EINA_FALSE;
539 }
540
541 int vc_mgr_prepare(void)
542 {
543         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Prepare");
544
545         int ret;
546         ret = __check_mgr_feature_privilege();
547         if (VC_ERROR_NONE != ret)
548                 return ret;
549
550         vc_state_e state;
551         if (0 != vc_mgr_client_get_client_state(&state)) {
552                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
553                 return VC_ERROR_INVALID_STATE;
554         }
555
556         /* check state */
557         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
558
559         if (NULL == g_send_hello_timer) {
560                 g_tidl_send_hello_count = 0;
561                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Create a new timer for preparation");
562                 ecore_thread_main_loop_begin();
563                 g_send_hello_timer = ecore_timer_add(0.1, __send_hello_message, NULL);
564                 ecore_thread_main_loop_end();
565         } else {
566                 SLOG(LOG_INFO, TAG_VCM, "[INFO] timer handle is already created");
567         }
568
569         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Calling Prepare DONE");
570
571         return VC_ERROR_NONE;
572 }
573
574 int vc_mgr_unprepare(void)
575 {
576         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Unprepare");
577
578         int ret;
579         ret = __check_mgr_feature_privilege();
580         if (VC_ERROR_NONE != ret)
581                 return ret;
582
583         vc_state_e state;
584         if (0 != vc_mgr_client_get_client_state(&state)) {
585                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
586                 return VC_ERROR_INVALID_STATE;
587         }
588
589         /* check state */
590         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
591
592         __vc_mgr_internal_unprepare();
593
594         vc_mgr_client_set_client_state(VC_STATE_INITIALIZED);
595         ecore_timer_add(0, __vc_mgr_notify_state_changed, NULL);
596
597         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Unprepare DONE");
598
599         return VC_ERROR_NONE;
600 }
601
602 int vc_mgr_foreach_supported_languages(vc_supported_language_cb callback, void* user_data)
603 {
604         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Foreach Supported Language");
605
606         int ret;
607         ret = __check_mgr_feature_privilege();
608         if (VC_ERROR_NONE != ret)
609                 return ret;
610
611         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
612
613         vc_state_e state;
614         if (0 != vc_mgr_client_get_client_state(&state)) {
615                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
616                 return VC_ERROR_INVALID_STATE;
617         }
618
619         ret = vc_config_mgr_get_language_list(callback, user_data);
620         if (0 != ret) {
621                 ret = vc_config_convert_error_code((vc_config_error_e)ret);
622                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get languages : %s", __vc_mgr_get_error_code(ret));
623         }
624
625         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Foreach Supported Language DONE");
626
627         return VC_ERROR_NONE;
628 }
629
630 int vc_mgr_get_current_language(char** language)
631 {
632         int ret;
633         ret = __check_mgr_feature_privilege();
634         if (VC_ERROR_NONE != ret)
635                 return ret;
636
637         RETVM_IF(NULL == language, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
638
639         vc_state_e state;
640         if (0 != vc_mgr_client_get_client_state(&state)) {
641                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
642                 return VC_ERROR_INVALID_STATE;
643         }
644
645         ret = vc_config_mgr_get_default_language(language);
646         if (0 != ret) {
647                 ret = vc_config_convert_error_code((vc_config_error_e)ret);
648                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get current languages : %s", __vc_mgr_get_error_code(ret));
649         } else {
650                 SLOG(LOG_ERROR, TAG_VCM, "[Get current language] language : %s", *language);
651         }
652
653         return ret;
654 }
655
656 int vc_mgr_get_state(vc_state_e* state)
657 {
658         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get State");
659
660         int ret;
661         ret = __check_mgr_feature_privilege();
662         if (VC_ERROR_NONE != ret)
663                 return ret;
664
665         RETVM_IF(NULL == state, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
666
667         vc_state_e temp;
668         if (0 != vc_mgr_client_get_client_state(&temp)) {
669                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
670                 return VC_ERROR_INVALID_STATE;
671         }
672
673         *state = temp;
674
675         switch (*state) {
676         case VC_STATE_NONE:             SLOG(LOG_INFO, TAG_VCM, "Current state is 'None'");             break;
677         case VC_STATE_INITIALIZED:      SLOG(LOG_INFO, TAG_VCM, "Current state is 'Created'");          break;
678         case VC_STATE_READY:            SLOG(LOG_INFO, TAG_VCM, "Current state is 'Ready'");            break;
679         default:                        SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid state");
680         }
681
682         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get State DONE");
683
684         return VC_ERROR_NONE;
685 }
686
687 int vc_mgr_get_service_state(vc_service_state_e* state)
688 {
689         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get Service State");
690
691         int ret;
692         ret = __check_mgr_feature_privilege();
693         if (VC_ERROR_NONE != ret)
694                 return ret;
695
696         RETVM_IF(NULL == state, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
697
698         vc_state_e client_state;
699         if (0 != vc_mgr_client_get_client_state(&client_state)) {
700                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
701                 return VC_ERROR_INVALID_STATE;
702         }
703
704         /* check state */
705         RETVM_IF(client_state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Current state is not 'READY'");
706
707         /* get service state */
708         vc_service_state_e service_state;
709         if (0 != vc_mgr_client_get_service_state(&service_state)) {
710                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get service state");
711                 return VC_ERROR_OPERATION_FAILED;
712         }
713
714         *state = service_state;
715
716         switch (*state) {
717         case VC_SERVICE_STATE_NONE:             SLOG(LOG_DEBUG, TAG_VCM, "Current service state is 'None'");            break;
718         case VC_SERVICE_STATE_READY:            SLOG(LOG_DEBUG, TAG_VCM, "Current service state is 'Ready'");           break;
719         case VC_SERVICE_STATE_RECORDING:        SLOG(LOG_DEBUG, TAG_VCM, "Current service state is 'Recording'");       break;
720         case VC_SERVICE_STATE_PROCESSING:       SLOG(LOG_DEBUG, TAG_VCM, "Current service state is 'Processing'");      break;
721         default:                                SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid state");
722         }
723
724         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get Service State DONE");
725
726         return VC_ERROR_NONE;
727 }
728
729 int vc_mgr_set_demandable_client_rule(const char* rule)
730 {
731         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Set Demandable client rule(%s)", rule);
732
733         int ret;
734         ret = __check_mgr_feature_privilege();
735         if (VC_ERROR_NONE != ret)
736                 return ret;
737
738         RETVM_IF(NULL == rule, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
739
740         vc_state_e state;
741         if (0 != vc_mgr_client_get_client_state(&state)) {
742                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
743                 return VC_ERROR_INVALID_STATE;
744         }
745
746         /* check state */
747         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
748
749         ret = vc_mgr_core_set_demandable_client_rule(rule);
750
751         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Set Demandable client rule(%s) DONE", rule);
752
753         return VC_ERROR_NONE;
754 }
755
756 int vc_mgr_unset_demandable_client_rule(void)
757 {
758         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Unset Demandable client");
759
760         int ret;
761         ret = __check_mgr_feature_privilege();
762         if (VC_ERROR_NONE != ret)
763                 return ret;
764
765         vc_state_e state;
766         if (0 != vc_mgr_client_get_client_state(&state)) {
767                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
768                 return VC_ERROR_INVALID_STATE;
769         }
770
771         /* check state */
772         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
773
774         vc_info_parser_set_demandable_client(NULL);
775
776         ret = vc_mgr_tidl_request_demandable_client(g_pid);
777         if (0 != ret) {
778                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request unset client rule to daemon : %s", __vc_mgr_get_error_code(ret));
779         }
780
781         // TODO: check return value correct or not
782         return VC_ERROR_NONE;
783 }
784
785 int vc_mgr_is_command_format_supported(int format, bool* support)
786 {
787         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Is command type supported");
788
789         int ret;
790         ret = __check_mgr_feature_privilege();
791         if (VC_ERROR_NONE != ret)
792                 return ret;
793
794         RETVM_IF(NULL == support, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
795
796         vc_state_e state;
797         if (0 != vc_mgr_client_get_client_state(&state)) {
798                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
799                 return VC_ERROR_INVALID_STATE;
800         }
801
802         /* check support */
803         bool non_fixed_support = false;
804         if (0 != vc_config_mgr_get_nonfixed_support(&non_fixed_support)) {
805                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get nonfixed support info");
806         }
807
808         switch (format) {
809         case VC_CMD_FORMAT_FIXED:               *support = true;                break;
810         case VC_CMD_FORMAT_FIXED_AND_VFIXED:    *support = true;                break;
811         case VC_CMD_FORMAT_VFIXED_AND_FIXED:    *support = true;                break;
812         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:  *support = non_fixed_support;   break;
813         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:  *support = non_fixed_support;   break;
814         default:                                *support = false;               break;
815         }
816
817         SLOG(LOG_ERROR, TAG_VCM, "[DEBUG] Format(%d) support(%s)", format, *support ? "true" : "false");
818
819         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Is command type supported DONE");
820
821         return VC_ERROR_NONE;
822 }
823
824 int vc_mgr_enable_command_type(int cmd_type)
825 {
826         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Enable Command Type, cmd_type(%d)", cmd_type);
827
828         int ret;
829         ret = __check_mgr_feature_privilege();
830         if (VC_ERROR_NONE != ret)
831                 return ret;
832
833         if (VC_COMMAND_TYPE_FOREGROUND > cmd_type || VC_COMMAND_TYPE_EXCLUSIVE < cmd_type) {
834                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] cmd_type is not valid. (%d)", cmd_type);
835                 return VC_ERROR_INVALID_PARAMETER;
836         }
837
838         vc_state_e state;
839         if (0 != vc_mgr_client_get_client_state(&state)) {
840                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
841                 return VC_ERROR_INVALID_STATE;
842         }
843
844         /* check state */
845         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
846
847         /* Check service state */
848         vc_service_state_e service_state = -1;
849         vc_mgr_client_get_service_state(&service_state);
850         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
851
852         ret = vc_mgr_client_enable_command_type((vc_cmd_type_e)cmd_type);               // enable the cmd type
853         if (0 != ret) {
854                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to enable the command type(%d). ret(%d)", cmd_type, ret);
855         } else {
856                 SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] Success to enable the command type(%d)", cmd_type);
857         }
858
859         SLOG(LOG_ERROR, TAG_VCM, "@@@  [Manager] Enable Command Type DONE, ret(%d)", ret);
860
861         return ret;
862 }
863
864 int vc_mgr_disable_command_type(int cmd_type)
865 {
866         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Disable Command Type, cmd_type(%d)", cmd_type);
867
868         int ret;
869         ret = __check_mgr_feature_privilege();
870         if (VC_ERROR_NONE != ret)
871                 return ret;
872
873         if (VC_COMMAND_TYPE_FOREGROUND > cmd_type || VC_COMMAND_TYPE_EXCLUSIVE < cmd_type) {
874                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] cmd type is not valid, (%d)", cmd_type);
875                 return VC_ERROR_INVALID_PARAMETER;
876         }
877
878         vc_state_e state;
879         if (0 != vc_mgr_client_get_client_state(&state)) {
880                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
881                 return VC_ERROR_INVALID_STATE;
882         }
883
884         /* check state */
885         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
886
887         /* Check service state */
888         vc_service_state_e service_state = -1;
889         vc_mgr_client_get_service_state(&service_state);
890         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
891
892         ret = vc_mgr_client_disable_command_type((vc_cmd_type_e)cmd_type);              // enable the cmd type
893         if (0 != ret) {
894                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to disable the command type(%d). ret(%d)", cmd_type, ret);
895         } else {
896                 SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] Success to disable the command type(%d)", cmd_type);
897         }
898
899         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Disable Command Type DONE, ret(%d)", ret);
900
901         return ret;
902 }
903
904 int vc_mgr_set_command_list(vc_cmd_list_h vc_cmd_list)
905 {
906         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set Command list");
907
908         int ret;
909         ret = __check_mgr_feature_privilege();
910         if (VC_ERROR_NONE != ret)
911                 return ret;
912
913         vc_state_e state;
914         if (0 != vc_mgr_client_get_client_state(&state)) {
915                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
916                 return VC_ERROR_INVALID_STATE;
917         }
918
919         /* check state */
920         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
921
922         /* Check service state */
923         vc_service_state_e service_state = -1;
924         vc_mgr_client_get_service_state(&service_state);
925         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: service state(%d) is not 'READY'", service_state);
926
927         RETVM_IF(NULL == vc_cmd_list, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
928
929         vc_cmd_list_s* list = NULL;
930         list = (vc_cmd_list_s*)vc_cmd_list;
931         SLOG(LOG_INFO, TAG_VCM, "[List] (%p) (%p)", list, list->list);
932
933         RETVM_IF(NULL == list->list, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input command list");
934
935         int i;
936         bool success_save = false;
937         for (i = VC_COMMAND_TYPE_FOREGROUND; i <= VC_COMMAND_TYPE_EXCLUSIVE; i++) {
938                 ret = vc_cmd_parser_delete_file(getpid(), i);
939                 if (0 != ret)
940                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to delete file, type(%d), ret(%d)", i, ret);
941
942                 ret = vc_cmd_parser_save_file(getpid(), i, list->list, NULL);
943                 if (0 != ret) {
944                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to save file, type(%d), ret(%d)", i, ret);
945                 } else {
946                         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Save file, type(%d)", i);
947                         success_save = true;
948                 }
949         }
950
951         if (true != success_save) {
952                 ret = VC_ERROR_INVALID_PARAMETER;
953                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to save command group : %s", __vc_mgr_get_error_code(ret));
954         } else {
955                 ret = vc_mgr_tidl_request_set_command(g_pid);
956                 if (0 != ret) {
957                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request set command to daemon : %s", __vc_mgr_get_error_code(ret));
958                 }
959         }
960
961         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set Command list DONE, ret(%d)", ret);
962
963         return ret;
964 }
965
966 int vc_mgr_unset_command_list(void)
967 {
968         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Unset Command list");
969
970         int ret;
971         ret = __check_mgr_feature_privilege();
972         if (VC_ERROR_NONE != ret)
973                 return ret;
974
975         vc_state_e state;
976         if (0 != vc_mgr_client_get_client_state(&state)) {
977                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
978                 return VC_ERROR_INVALID_STATE;
979         }
980
981         /* check state */
982         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
983
984         ret = vc_mgr_tidl_request_unset_command(g_pid);
985         if (0 != ret) {
986                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request unset command to daemon : %s", __vc_mgr_get_error_code(ret));
987         }
988
989         int i;
990         for (i = VC_COMMAND_TYPE_FOREGROUND; i <= VC_COMMAND_TYPE_EXCLUSIVE; i++) {
991                 ret = vc_cmd_parser_delete_file(getpid(), i);
992                 if (0 != ret)
993                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to delete file, type(%d), ret(%d)", i, ret);
994         }
995
996         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Unset Command list DONE");
997
998         // TODO: check return value correct or not
999         return VC_ERROR_NONE;
1000 }
1001
1002 int vc_mgr_set_command_list_from_file(const char* file_path, int type)
1003 {
1004         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set Command list from file, type(%d)", type);
1005
1006         int ret;
1007         ret = __check_mgr_feature_privilege();
1008         if (VC_ERROR_NONE != ret)
1009                 return ret;
1010
1011         RETVM_IF(NULL == file_path, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1012         SLOG(LOG_INFO, TAG_VCM, "@@@ File path: %s", file_path);
1013
1014         /* check type */
1015         if (VC_COMMAND_TYPE_FOREGROUND > type || VC_COMMAND_TYPE_EXCLUSIVE < type) {
1016                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid command type: input type is %d", type);
1017                 return VC_ERROR_INVALID_PARAMETER;
1018         }
1019
1020         vc_state_e state;
1021         if (0 != vc_mgr_client_get_client_state(&state)) {
1022                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1023                 return VC_ERROR_INVALID_STATE;
1024         }
1025
1026         /* check state */
1027         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1028
1029         /* Check service state */
1030         vc_service_state_e service_state = -1;
1031         vc_mgr_client_get_service_state(&service_state);
1032         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1033
1034         ret = vc_cmd_parser_delete_file(getpid(), type);
1035         if (0 != ret)
1036                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to delete file, type(4), ret(%d)", ret);
1037
1038         if (0 != vc_json_set_commands_from_file(file_path, type, NULL)) {
1039                 ret = VC_ERROR_INVALID_PARAMETER;
1040                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to save command list (%d)", ret);
1041         } else {
1042                 ret = vc_mgr_tidl_request_set_command(g_pid);
1043                 if (0 != ret) {
1044                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request set command to daemon : %s", __vc_mgr_get_error_code(ret));
1045                 }
1046         }
1047
1048         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set Command list from file DONE, ret(%d)", ret);
1049
1050         return ret;
1051 }
1052
1053 int vc_mgr_set_preloaded_commands_from_file(const char* file_path)
1054 {
1055         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set preloaded command list");
1056
1057         int ret;
1058         ret = __check_mgr_feature_privilege();
1059         if (VC_ERROR_NONE != ret)
1060                 return ret;
1061
1062         RETVM_IF(NULL == file_path, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1063         SLOG(LOG_INFO, TAG_VCM, "@@@ File path: %s", file_path);
1064
1065         vc_state_e state;
1066         if (0 != vc_mgr_client_get_client_state(&state)) {
1067                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1068                 return VC_ERROR_INVALID_STATE;
1069         }
1070
1071         /* check state */
1072         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1073
1074         /* Check service state */
1075         vc_service_state_e service_state = -1;
1076         vc_mgr_client_get_service_state(&service_state);
1077         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1078
1079         /* Only support to set background commands for preloaded application */
1080         ret = vc_json_set_commands_from_file(file_path, VC_COMMAND_TYPE_BACKGROUND, NULL);
1081         if (0 != ret)
1082                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to insert preloaded commands into db");
1083
1084         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set preloaded command list DONE, ret(%d)", ret);
1085         return ret;
1086 }
1087
1088 int vc_mgr_set_audio_type(const char* audio_id)
1089 {
1090         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set audio type, audio_id(%s)", audio_id);
1091
1092         int ret;
1093         ret = __check_mgr_feature_privilege();
1094         if (VC_ERROR_NONE != ret)
1095                 return ret;
1096
1097         if (NULL == audio_id || !strncmp(audio_id, "", strlen(audio_id))) {
1098                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Input parameter is NULL");
1099                 return VC_ERROR_INVALID_PARAMETER;
1100         }
1101
1102         vc_state_e state;
1103         if (0 != vc_mgr_client_get_client_state(&state)) {
1104                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1105                 return VC_ERROR_INVALID_STATE;
1106         }
1107
1108         /* check state */
1109         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1110
1111         /* Check service state */
1112         vc_service_state_e service_state = -1;
1113         vc_mgr_client_get_service_state(&service_state);
1114         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1115
1116         /* Request */
1117         ret = vc_mgr_tidl_request_set_audio_type(g_pid, audio_id);
1118         if (0 != ret) {
1119                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set audio type : %s", __vc_mgr_get_error_code(ret));
1120         } else {
1121                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set audio type");
1122                 /* Save */
1123                 vc_mgr_client_set_audio_type(audio_id);
1124         }
1125
1126         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set audio type DONE, ret(%d)", ret);
1127
1128         return ret;
1129 }
1130
1131 int vc_mgr_get_audio_type(char** audio_id)
1132 {
1133         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Get audio type");
1134
1135         int ret;
1136         ret = __check_mgr_feature_privilege();
1137         if (VC_ERROR_NONE != ret)
1138                 return ret;
1139
1140         RETVM_IF(NULL == audio_id, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1141
1142         vc_state_e state;
1143         if (0 != vc_mgr_client_get_client_state(&state)) {
1144                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1145                 return VC_ERROR_INVALID_STATE;
1146         }
1147
1148         /* check state */
1149         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1150
1151         /* Check service state */
1152         vc_service_state_e service_state = -1;
1153         vc_mgr_client_get_service_state(&service_state);
1154         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1155
1156         char* temp = NULL;
1157
1158         vc_mgr_client_get_audio_type(&temp);
1159
1160         if (NULL == temp) {
1161                 /* Not initiallized */
1162                 ret = -1;
1163                 while (0 != ret) {
1164                         ret = vc_mgr_tidl_request_get_audio_type(g_pid, &temp);
1165                         if (0 != ret) {
1166                                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get audio type : %s", __vc_mgr_get_error_code(ret));
1167                         } else {
1168                                 SLOG(LOG_ERROR, TAG_VCM, "[SUCCESS] Get audio type : %s", temp);
1169                                 /* Save */
1170                                 vc_mgr_client_set_audio_type(temp);
1171                         }
1172                 }
1173         }
1174
1175         if (NULL != temp) {
1176                 *audio_id = strdup(temp);
1177                 free(temp);
1178                 temp = NULL;
1179         }
1180
1181         // TODO: check return value correct or not
1182         return VC_ERROR_NONE;
1183 }
1184
1185 int vc_mgr_get_current_commands(vc_cmd_list_h* vc_cmd_list)
1186 {
1187         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Foreach current commands");
1188
1189         int ret;
1190         ret = __check_mgr_feature_privilege();
1191         if (VC_ERROR_NONE != ret)
1192                 return ret;
1193
1194         vc_state_e state;
1195         if (0 != vc_mgr_client_get_client_state(&state)) {
1196                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1197                 return VC_ERROR_INVALID_STATE;
1198         }
1199
1200         /* check state */
1201         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1202
1203         /* Check service state */
1204         vc_service_state_e service_state = -1;
1205         vc_mgr_client_get_service_state(&service_state);
1206         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: service state(%d) is not 'READY'", service_state);
1207
1208         RETVM_IF(NULL == vc_cmd_list, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid Parameter");
1209
1210         vc_cmd_list_h temp_list = NULL;
1211         if (0 != vc_cmd_list_create(&temp_list)) {
1212                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create list");
1213                 return VC_ERROR_INVALID_PARAMETER;
1214         }
1215
1216         *vc_cmd_list = temp_list;
1217
1218         int fg_pid = 0;
1219         int mgr_pid = 0;
1220
1221         /* Get foreground pid */
1222         if (0 != vc_mgr_client_get_foreground(&fg_pid)) {
1223                 /* There is no foreground app for voice control */
1224                 SLOG(LOG_WARN, TAG_VCM, "[Manager WARNING] No foreground pid for voice control");
1225         } else {
1226                 SLOG(LOG_INFO, TAG_VCM, "[Manager] Foreground pid(%d)", fg_pid);
1227         }
1228
1229         if (0 != vc_mgr_client_get_pid(&mgr_pid)) {
1230                 SLOG(LOG_ERROR, TAG_VCM, "[Manager] Fail to get manager pid");
1231                 return VC_ERROR_OPERATION_FAILED;
1232         } else {
1233                 SLOG(LOG_INFO, TAG_VCM, "[Manager] Manager pid(%d)", mgr_pid);
1234         }
1235
1236         /* Get system command */
1237         ret = vc_cmd_parser_append_commands(mgr_pid, VC_COMMAND_TYPE_SYSTEM, temp_list);
1238         if (0 != ret) {
1239                 SLOG(LOG_INFO, TAG_VCM, "[Manager] No system commands");
1240         }
1241
1242         /* Request */
1243         ret = vc_mgr_tidl_request_set_client_info(g_pid);
1244         if (0 != ret) {
1245                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set client info : %s", __vc_mgr_get_error_code(ret));
1246         } else {
1247                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set client info");
1248         }
1249
1250         GSList *iter = NULL;
1251         GSList* client_info_list = NULL;
1252         vc_client_info_s *client_info = NULL;
1253         bool is_fgpid = false;
1254
1255         if (0 != vc_info_parser_get_client_info(&client_info_list)) {
1256                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] No client");
1257                 return VC_ERROR_NONE;
1258         }
1259
1260         if (VC_NO_FOREGROUND_PID != fg_pid) {
1261                 iter = g_slist_nth(client_info_list, 0);
1262                 while (NULL != iter) {
1263                         client_info = iter->data;
1264                         if (NULL != client_info) {
1265                                 if (fg_pid == client_info->pid) {
1266                                         is_fgpid = true;
1267                                         break;
1268                                 }
1269                         }
1270                         iter = g_slist_next(iter);
1271                 }
1272         }
1273
1274         /* Get foreground commands and widget */
1275         if (true == is_fgpid) {
1276                 /* Get handle */
1277                 SLOG(LOG_INFO, TAG_VCM, "[Manager] fore cmd(%d)", client_info->fg_cmd);
1278
1279                 /* Get foreground command */
1280                 if (true == client_info->fg_cmd) {
1281                         ret = vc_cmd_parser_append_commands(fg_pid, VC_COMMAND_TYPE_FOREGROUND, temp_list);
1282                         if (0 != ret) {
1283                                 SLOG(LOG_ERROR, TAG_VCM, "[Client Data ERROR] Fail to get the fg command list");
1284                         }
1285                 } else {
1286                         SLOG(LOG_INFO, TAG_VCM, "[Manager] No foreground commands");
1287                 }
1288
1289                 /* Check exclusive option */
1290                 if (true == client_info->exclusive_cmd) {
1291                         SLOG(LOG_INFO, TAG_VCM, "[Manager] Exclusive command is ON");
1292
1293                         /* Set background command for exclusive option */
1294                         if (true == client_info->bg_cmd) {
1295                                 SLOG(LOG_INFO, TAG_VCM, "[Manager] Set background command");
1296                                 ret = vc_cmd_parser_append_commands(client_info->pid, VC_COMMAND_TYPE_BACKGROUND, temp_list);
1297                                 if (0 != ret) {
1298                                         SLOG(LOG_ERROR, TAG_VCM, "[Client Data ERROR] Fail to get the bg command list : pid(%d)", client_info->pid);
1299                                 }
1300                         }
1301
1302                         /* need to release client info */
1303                         iter = g_slist_nth(client_info_list, 0);
1304
1305                         while (NULL != iter) {
1306                                 client_info = iter->data;
1307                                 if (NULL != client_info) {
1308                                         free(client_info);
1309                                 }
1310                                 client_info_list = g_slist_remove_link(client_info_list, iter);
1311                                 iter = g_slist_nth(client_info_list, 0);
1312                         }
1313
1314                         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Foreach current commands DONE");
1315
1316                         return VC_ERROR_NONE;
1317                 }
1318         } else {
1319                 SLOG(LOG_INFO, TAG_VCM, "[Manager] No foreground app");
1320         }
1321
1322         /* Get background commands */
1323         if (0 < g_slist_length(client_info_list)) {
1324                 iter = g_slist_nth(client_info_list, 0);
1325
1326                 while (NULL != iter) {
1327                         client_info = iter->data;
1328
1329                         if (NULL != client_info) {
1330                                 SLOG(LOG_INFO, TAG_VCM, "[Manager] Pid(%d) Back cmd(%d)", client_info->pid, client_info->bg_cmd);
1331                                 if (true == client_info->bg_cmd) {
1332                                         ret = vc_cmd_parser_append_commands(client_info->pid, VC_COMMAND_TYPE_BACKGROUND, temp_list);
1333                                         if (0 != ret) {
1334                                                 SLOG(LOG_ERROR, TAG_VCM, "[Client Data ERROR] Fail to get the bg command list : pid(%d)", client_info->pid);
1335                                         }
1336                                 }
1337                                 free(client_info);
1338                         }
1339                         client_info_list = g_slist_remove_link(client_info_list, iter);
1340
1341                         iter = g_slist_nth(client_info_list, 0);
1342                 }
1343         } else {
1344                 /* NO client */
1345                 SLOG(LOG_INFO, TAG_VCM, "[Manager] No background commands");
1346         }
1347
1348         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Foreach current commands DONE");
1349
1350         // TODO: check return value correct or not
1351         return VC_ERROR_NONE;
1352 }
1353
1354 int vc_mgr_set_recognition_mode(vc_recognition_mode_e mode)
1355 {
1356         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Set recognition mode = %d", mode);
1357
1358         int ret;
1359         ret = __check_mgr_feature_privilege();
1360         if (VC_ERROR_NONE != ret)
1361                 return ret;
1362
1363         /* check vc recognition mode */
1364         if (mode < VC_RECOGNITION_MODE_STOP_BY_SILENCE || mode > VC_RECOGNITION_MODE_MANUAL) {
1365                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] invalid parameter: 'mode' is not supported mode (%d)", mode);
1366                 return VC_ERROR_INVALID_PARAMETER;
1367         }
1368
1369         vc_state_e state;
1370         if (0 != vc_mgr_client_get_client_state(&state)) {
1371                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1372                 return VC_ERROR_INVALID_STATE;
1373         }
1374
1375         /* check state */
1376         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1377
1378         /* Check service state */
1379         vc_service_state_e service_state = -1;
1380         vc_mgr_client_get_service_state(&service_state);
1381         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1382
1383         vc_mgr_client_set_recognition_mode(mode);
1384         return VC_ERROR_NONE;
1385 }
1386
1387 int vc_mgr_get_recognition_mode(vc_recognition_mode_e* mode)
1388 {
1389         int ret;
1390         ret = __check_mgr_feature_privilege();
1391         if (VC_ERROR_NONE != ret)
1392                 return ret;
1393
1394         RETVM_IF(NULL == mode, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter, mode is NULL ptr");
1395
1396         vc_state_e state;
1397         if (0 != vc_mgr_client_get_client_state(&state)) {
1398                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1399                 return VC_ERROR_INVALID_STATE;
1400         }
1401
1402         ret = vc_mgr_client_get_recognition_mode(mode);
1403         if (0 != ret) {
1404                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get recognition mode");
1405                 return ret;
1406         }
1407
1408         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Get recognition mode = %d", *mode);
1409         return VC_ERROR_NONE;
1410 }
1411
1412 int vc_mgr_set_private_data(const char* key, const char* data)
1413 {
1414         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Set private data");
1415
1416         int ret;
1417         ret = __check_mgr_feature_privilege();
1418         if (VC_ERROR_NONE != ret)
1419                 return ret;
1420
1421         RETVM_IF(NULL == key, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1422
1423         vc_state_e state;
1424         if (0 != vc_mgr_client_get_client_state(&state)) {
1425                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1426                 return VC_ERROR_INVALID_STATE;
1427         }
1428
1429         /* check state */
1430         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1431
1432         /* Check service state */
1433         vc_service_state_e service_state = -1;
1434         vc_mgr_client_get_service_state(&service_state);
1435         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1436
1437         ret = vc_mgr_tidl_request_set_private_data(g_pid, key, data);
1438         if (0 != ret) {
1439                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set private data : %s", __vc_mgr_get_error_code(ret));
1440                 return ret;
1441         } else {
1442                 SLOG(LOG_DEBUG, TAG_VCM, "[SUCCESS] Set private data");
1443         }
1444
1445         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Set private data DONE");
1446
1447         return VC_ERROR_NONE;
1448 }
1449
1450 int vc_mgr_get_private_data(const char* key, char** data)
1451 {
1452         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get private data");
1453
1454         int ret;
1455         ret = __check_mgr_feature_privilege();
1456         if (VC_ERROR_NONE != ret)
1457                 return ret;
1458
1459         if (NULL == key || NULL == data) {
1460                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid parameter");
1461                 return VC_ERROR_INVALID_PARAMETER;
1462         }
1463
1464         vc_state_e state;
1465         if (0 != vc_mgr_client_get_client_state(&state)) {
1466                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1467                 return VC_ERROR_INVALID_STATE;
1468         }
1469
1470         /* check state */
1471         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1472
1473         /* Check service state */
1474         vc_service_state_e service_state = -1;
1475         vc_mgr_client_get_service_state(&service_state);
1476         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1477
1478         char* temp = NULL;
1479         ret = vc_mgr_tidl_request_get_private_data(g_pid, key, &temp);
1480         if (0 != ret) {
1481                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get private data request : %s", __vc_mgr_get_error_code(ret));
1482         } else {
1483                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Get private data, key(%s), data(%s)", key, temp);
1484         }
1485
1486         if (NULL != temp) {
1487                 *data = strdup(temp);
1488                 free(temp);
1489                 temp = NULL;
1490         }
1491
1492         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get private data DONE");
1493
1494         // TODO: check return value correct or not
1495         return VC_ERROR_NONE;
1496 }
1497
1498 int vc_mgr_set_domain(const char* domain)
1499 {
1500         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Set domain");
1501
1502         int ret;
1503         ret = __check_mgr_feature_privilege();
1504         if (VC_ERROR_NONE != ret)
1505                 return ret;
1506
1507         RETVM_IF(NULL == domain, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1508
1509         vc_state_e state;
1510         if (0 != vc_mgr_client_get_client_state(&state)) {
1511                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1512                 return VC_ERROR_INVALID_STATE;
1513         }
1514
1515         /* check state */
1516         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1517
1518         /* Check service state */
1519         vc_service_state_e service_state = -1;
1520         vc_mgr_client_get_service_state(&service_state);
1521         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1522
1523         ret = vc_mgr_tidl_request_set_domain(g_pid, domain);
1524         if (0 != ret) {
1525                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set domain request : %s", __vc_mgr_get_error_code(ret));
1526         } else {
1527                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set domain");
1528         }
1529
1530         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Set domain DONE");
1531
1532         return VC_ERROR_NONE;
1533 }
1534
1535 int vc_mgr_do_action(vc_send_event_type_e type, char* send_event)
1536 {
1537         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] do action");
1538
1539         int ret;
1540         ret = __check_mgr_feature_privilege();
1541         if (VC_ERROR_NONE != ret)
1542                 return ret;
1543
1544         if (type < VC_SEND_EVENT_TYPE_TEXT || VC_SEND_EVENT_TYPE_HAPTIC_EVENT < type) {
1545                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] send event type is not valid, (%d)", type);
1546                 return VC_ERROR_INVALID_PARAMETER;
1547         }
1548
1549         RETVM_IF(NULL == send_event, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1550
1551         vc_state_e state;
1552         if (0 != vc_mgr_client_get_client_state(&state)) {
1553                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1554                 return VC_ERROR_INVALID_STATE;
1555         }
1556
1557         /* check state */
1558         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1559
1560         /* Check service state */
1561         vc_service_state_e service_state = -1;
1562         vc_mgr_client_get_service_state(&service_state);
1563         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1564
1565         ret = vc_mgr_tidl_request_do_action(g_pid, type, send_event);
1566         if (0 != ret) {
1567                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to do action request : %s", __vc_mgr_get_error_code(ret));
1568                 return ret;
1569         } else {
1570                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] do action");
1571         }
1572         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] do action DONE");
1573
1574         return VC_ERROR_NONE;
1575 }
1576
1577 int vc_mgr_send_specific_engine_request(const char* engine_app_id, const char* event, const char* request)
1578 {
1579         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] send specific engine request");
1580
1581         int ret;
1582         ret = __check_mgr_feature_privilege();
1583         if (VC_ERROR_NONE != ret)
1584                 return ret;
1585
1586         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] specific engine request. engine app id(%s), event(%s), request(%s)", engine_app_id, event, request);
1587
1588         if (NULL == engine_app_id || NULL == event) {
1589                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid parameter");
1590                 return VC_ERROR_INVALID_PARAMETER;
1591         }
1592
1593         if (NULL == request) {
1594                 SLOG(LOG_ERROR, TAG_VCM, "[INFO] Input parameter is NULL. (no request)");
1595         }
1596
1597         vc_state_e state;
1598         if (0 != vc_mgr_client_get_client_state(&state)) {
1599                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1600                 return VC_ERROR_INVALID_STATE;
1601         }
1602
1603         /* check state */
1604         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1605
1606         /* Check service state */
1607         vc_service_state_e service_state = -1;
1608         vc_mgr_client_get_service_state(&service_state);
1609         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1610
1611         ret = vc_mgr_tidl_send_specific_engine_request(g_pid, engine_app_id, event, request);
1612         if (0 != ret) {
1613                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to specific engine request : %s", __vc_mgr_get_error_code(ret));
1614                 return ret;
1615         } else {
1616                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] specific engine request");
1617         }
1618         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] send specific engine request DONE");
1619
1620         return VC_ERROR_NONE;
1621 }
1622
1623 int vc_mgr_start(bool exclusive_command_option)
1624 {
1625         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Request start");
1626
1627         int ret;
1628         ret = __check_mgr_feature_privilege();
1629         if (VC_ERROR_NONE != ret)
1630                 return ret;
1631
1632         vc_state_e state;
1633         if (0 != vc_mgr_client_get_client_state(&state)) {
1634                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1635                 return VC_ERROR_INVALID_STATE;
1636         }
1637
1638         /* check state */
1639         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1640
1641         /* Check service state */
1642         vc_service_state_e service_state = -1;
1643         vc_mgr_client_get_service_state(&service_state);
1644         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
1645
1646         /* Check internal state for async */
1647         vc_internal_state_e internal_state = -1;
1648         vc_mgr_client_get_internal_state(&internal_state);
1649         if (internal_state != VC_INTERNAL_STATE_NONE) {
1650                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is NOT none : %d", internal_state);
1651                 return VC_ERROR_IN_PROGRESS_TO_RECORDING;
1652         }
1653
1654         vc_mgr_client_set_exclusive_command(exclusive_command_option);
1655
1656         bool start_by_client = false;
1657         if (0 != vc_mgr_client_get_start_by_client(&start_by_client)) {
1658                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get start by client");
1659         }
1660
1661         int disabled_cmd_type = 0;
1662         if (0 != vc_mgr_client_get_disabled_command_type(&disabled_cmd_type)) {
1663                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get disabled command type");
1664         }
1665
1666         vc_recognition_mode_e recognition_mode = VC_RECOGNITION_MODE_STOP_BY_SILENCE;
1667         if (0 != vc_mgr_get_recognition_mode(&recognition_mode)) {
1668                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get recognition mode");
1669         }
1670
1671         /* Request */
1672         ret = -1;
1673         vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_STARTING);
1674         ret = vc_mgr_tidl_request_start(g_pid, (int)recognition_mode, exclusive_command_option, start_by_client, disabled_cmd_type);
1675         if (0 != ret) {
1676                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to start request start : %s", __vc_mgr_get_error_code(ret));
1677                 vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_NONE);
1678         } else {
1679                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] start recognition");
1680         }
1681
1682         vc_mgr_core_initialize_volume_variable();
1683
1684         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Request start DONE, ret(%d)", ret);
1685
1686         return ret;
1687 }
1688
1689 int vc_mgr_stop(void)
1690 {
1691         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Request stop");
1692
1693         int ret;
1694         ret = __check_mgr_feature_privilege();
1695         if (VC_ERROR_NONE != ret)
1696                 return ret;
1697
1698         vc_state_e state;
1699         if (0 != vc_mgr_client_get_client_state(&state)) {
1700                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1701                 return VC_ERROR_INVALID_STATE;
1702         }
1703
1704         /* check state */
1705         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1706
1707         /* Check service state */
1708         vc_service_state_e service_state = -1;
1709         vc_mgr_client_get_service_state(&service_state);
1710         RETVM_IF(service_state != VC_SERVICE_STATE_RECORDING, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'RECORDING'", service_state);
1711
1712         /* Check internal state for async */
1713         vc_internal_state_e internal_state = -1;
1714         vc_mgr_client_get_internal_state(&internal_state);
1715         if (VC_INTERNAL_STATE_STARTING == internal_state) {
1716                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is STARTING");
1717                 return VC_ERROR_IN_PROGRESS_TO_RECORDING;
1718         } else if (VC_INTERNAL_STATE_STOPPING == internal_state) {
1719                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is STOPPING");
1720                 return VC_ERROR_IN_PROGRESS_TO_PROCESSING;
1721         } else if (VC_INTERNAL_STATE_CANCELING == internal_state) {
1722                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is CANCELING");
1723                 return VC_ERROR_IN_PROGRESS_TO_READY;
1724         }
1725
1726         ret = -1;
1727         /* do request */
1728         vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_STOPPING);
1729         ret = vc_mgr_tidl_request_stop(g_pid);
1730         if (0 != ret) {
1731                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to stop request : %s", __vc_mgr_get_error_code(ret));
1732                 vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_NONE);
1733         } else {
1734                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Stop recognition");
1735         }
1736
1737         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Request stop DONE");
1738
1739         return ret;
1740 }
1741
1742 int vc_mgr_cancel(void)
1743 {
1744         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Request cancel");
1745
1746         int ret;
1747         ret = __check_mgr_feature_privilege();
1748         if (VC_ERROR_NONE != ret)
1749                 return ret;
1750
1751         vc_state_e state;
1752         if (0 != vc_mgr_client_get_client_state(&state)) {
1753                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1754                 return VC_ERROR_INVALID_STATE;
1755         }
1756
1757         /* check state */
1758         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1759
1760         /* Check service state */
1761         vc_service_state_e service_state = -1;
1762         vc_mgr_client_get_service_state(&service_state);
1763         RETVM_IF(service_state == VC_SERVICE_STATE_NONE, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state is NONE");
1764
1765         vc_internal_state_e internal_state = -1;
1766         vc_mgr_client_get_internal_state(&internal_state);
1767         if (VC_INTERNAL_STATE_STARTING == internal_state) {
1768                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is STARTING");
1769                 return VC_ERROR_IN_PROGRESS_TO_RECORDING;
1770         } else if (VC_INTERNAL_STATE_STOPPING == internal_state) {
1771                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is STOPPING");
1772                 return VC_ERROR_IN_PROGRESS_TO_PROCESSING;
1773         } else if (VC_INTERNAL_STATE_CANCELING == internal_state) {
1774                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is CANCELING");
1775                 return VC_ERROR_IN_PROGRESS_TO_READY;
1776         }
1777
1778         ret = -1;
1779         vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_CANCELING);
1780         ret = vc_mgr_tidl_request_cancel(g_pid);
1781         if (0 != ret) {
1782                 SLOG(LOG_DEBUG, TAG_VCM, "[ERROR] Fail to cancel request : %s", __vc_mgr_get_error_code(ret));
1783                 vc_mgr_client_set_internal_state(VC_INTERNAL_STATE_NONE);
1784         } else {
1785                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Cancel recognition");
1786         }
1787
1788         vc_mgr_client_set_exclusive_command(false);
1789
1790         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Request cancel DONE");
1791
1792         return ret;
1793 }
1794
1795 // TODO: check called by app direactly meaning
1796 int __vc_mgr_set_volume(float volume)
1797 {
1798         // called by app directly
1799         vc_mgr_core_set_volume(volume);
1800         
1801         return VC_ERROR_NONE;
1802 }
1803
1804 int vc_mgr_get_recording_volume(float* volume)
1805 {
1806         int ret;
1807         ret = __check_mgr_feature_privilege();
1808         if (VC_ERROR_NONE != ret)
1809                 return ret;
1810
1811         RETVM_IF(NULL == volume, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1812
1813         vc_service_state_e service_state = -1;
1814         if (0 != vc_mgr_client_get_service_state(&service_state)) {
1815                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1816                 return VC_ERROR_INVALID_STATE;
1817         }
1818
1819         /* check state */
1820         RETVM_IF(VC_SERVICE_STATE_RECORDING != service_state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Service state is not 'RECORDING'");
1821
1822         *volume = vc_mgr_core_get_volume();
1823
1824         return VC_ERROR_NONE;
1825 }
1826
1827 int vc_mgr_set_selected_results(vc_cmd_list_h vc_cmd_list)
1828 {
1829         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Select result");
1830
1831         int ret;
1832         ret = __check_mgr_feature_privilege();
1833         if (VC_ERROR_NONE != ret)
1834                 return ret;
1835
1836         /* Do not check state for 'restart continuously' mode */
1837
1838         vc_service_state_e service_state = -1;
1839         vc_mgr_client_get_service_state(&service_state);
1840         if (service_state != VC_SERVICE_STATE_PROCESSING) {
1841                 vc_recognition_mode_e recognition_mode;
1842                 vc_mgr_get_recognition_mode(&recognition_mode);
1843
1844                 if (VC_RECOGNITION_MODE_RESTART_CONTINUOUSLY != recognition_mode) {
1845                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: service state is not 'PROCESSING' and mode is not 'Restart continuously'");
1846                         return VC_ERROR_INVALID_STATE;
1847                 }
1848         }
1849
1850         ret = vc_mgr_core_set_selected_results(vc_cmd_list, g_pid);
1851
1852         return ret;
1853 }
1854
1855 int vc_mgr_set_all_result_cb(vc_mgr_all_result_cb callback, void* user_data)
1856 {
1857         int ret;
1858         ret = __check_mgr_feature_privilege();
1859         if (VC_ERROR_NONE != ret)
1860                 return ret;
1861
1862         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1863
1864         vc_state_e state;
1865         if (0 != vc_mgr_client_get_client_state(&state)) {
1866                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set result callback : A handle is not available");
1867                 return VC_ERROR_INVALID_STATE;
1868         }
1869
1870         /* check state */
1871         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
1872
1873         vc_mgr_client_set_all_result_cb(callback, user_data);
1874
1875         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set all result callback");
1876
1877         return VC_ERROR_NONE;
1878 }
1879
1880 int vc_mgr_unset_all_result_cb(void)
1881 {
1882         int ret;
1883         ret = __check_mgr_feature_privilege();
1884         if (VC_ERROR_NONE != ret)
1885                 return ret;
1886
1887         vc_state_e state;
1888         if (0 != vc_mgr_client_get_client_state(&state)) {
1889                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset result callback : A handle is not available");
1890                 return VC_ERROR_INVALID_STATE;
1891         }
1892
1893         /* check state */
1894         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
1895
1896         vc_mgr_client_set_all_result_cb(NULL, NULL);
1897
1898         return VC_ERROR_NONE;
1899 }
1900
1901 int vc_mgr_set_result_cb(vc_result_cb callback, void* user_data)
1902 {
1903         int ret;
1904         ret = __check_mgr_feature_privilege();
1905         if (VC_ERROR_NONE != ret)
1906                 return ret;
1907
1908         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1909
1910         vc_state_e state;
1911         if (0 != vc_mgr_client_get_client_state(&state)) {
1912                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set result callback : A handle is not available");
1913                 return VC_ERROR_INVALID_STATE;
1914         }
1915
1916         /* check state */
1917         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
1918
1919         vc_mgr_client_set_result_cb(callback, user_data);
1920
1921         return VC_ERROR_NONE;
1922 }
1923
1924 int vc_mgr_unset_result_cb(void)
1925 {
1926         int ret;
1927         ret = __check_mgr_feature_privilege();
1928         if (VC_ERROR_NONE != ret)
1929                 return ret;
1930
1931         vc_state_e state;
1932         if (0 != vc_mgr_client_get_client_state(&state)) {
1933                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset result callback : A handle is not available");
1934                 return VC_ERROR_INVALID_STATE;
1935         }
1936
1937         /* check state */
1938         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
1939
1940         vc_mgr_client_set_result_cb(NULL, NULL);
1941
1942         return VC_ERROR_NONE;
1943 }
1944
1945 int vc_mgr_set_pre_result_cb(vc_mgr_pre_result_cb callback, void* user_data)
1946 {
1947         int ret;
1948         ret = __check_mgr_feature_privilege();
1949         if (VC_ERROR_NONE != ret)
1950                 return ret;
1951
1952         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1953
1954         vc_state_e state;
1955         if (0 != vc_mgr_client_get_client_state(&state)) {
1956                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set pre result callback : A handle is not available");
1957                 return VC_ERROR_INVALID_STATE;
1958         }
1959
1960         /* check state */
1961         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
1962
1963         vc_mgr_client_set_pre_result_cb(callback, user_data);
1964
1965         return VC_ERROR_NONE;
1966 }
1967
1968 int vc_mgr_unset_pre_result_cb(void)
1969 {
1970         int ret;
1971         ret = __check_mgr_feature_privilege();
1972         if (VC_ERROR_NONE != ret)
1973                 return ret;
1974
1975         vc_state_e state;
1976         if (0 != vc_mgr_client_get_client_state(&state)) {
1977                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset pre result callback : A handle is not available");
1978                 return VC_ERROR_INVALID_STATE;
1979         }
1980
1981         /* check state */
1982         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
1983
1984         vc_mgr_client_set_pre_result_cb(NULL, NULL);
1985
1986         return VC_ERROR_NONE;
1987 }
1988
1989 int vc_mgr_get_error_message(char** err_msg)
1990 {
1991         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get error message");
1992
1993         int ret;
1994         ret = __check_mgr_feature_privilege();
1995         if (VC_ERROR_NONE != ret)
1996                 return ret;
1997
1998         /* check state */
1999         vc_state_e state;
2000         if (0 != vc_mgr_client_get_client_state(&state)) {
2001                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] vc_mgr handle is not available");
2002                 return VC_ERROR_INVALID_STATE;
2003         }
2004
2005         RETVM_IF(NULL == err_msg, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2006
2007         ret = vc_mgr_core_get_error_message(err_msg);
2008
2009         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get error message DONE");
2010
2011         return ret;
2012 }
2013
2014 static Eina_Bool __vc_mgr_notify_state_changed(void *data)
2015 {
2016         vc_state_changed_cb changed_callback = NULL;
2017         void* user_data;
2018
2019         vc_mgr_client_get_state_changed_cb(&changed_callback, &user_data);
2020
2021         vc_state_e current_state;
2022         vc_state_e previous_state;
2023
2024         vc_mgr_client_get_previous_state(&current_state, &previous_state);
2025
2026         if (NULL != changed_callback) {
2027                 vc_mgr_client_use_callback();
2028                 changed_callback(previous_state, current_state, user_data);
2029                 vc_mgr_client_not_use_callback();
2030                 SLOG(LOG_INFO, TAG_VCM, "State changed callback is called");
2031         } else {
2032                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] State changed callback is null");
2033         }
2034
2035         return EINA_FALSE;
2036 }
2037
2038 int vc_mgr_set_state_changed_cb(vc_state_changed_cb callback, void* user_data)
2039 {
2040         int ret;
2041         ret = __check_mgr_feature_privilege();
2042         if (VC_ERROR_NONE != ret)
2043                 return ret;
2044
2045         if (callback == NULL)
2046                 return VC_ERROR_INVALID_PARAMETER;
2047
2048         vc_state_e state;
2049         if (0 != vc_mgr_client_get_client_state(&state)) {
2050                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set state changed callback : A handle is not available");
2051                 return VC_ERROR_INVALID_STATE;
2052         }
2053
2054         /* check state */
2055         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2056
2057         vc_mgr_client_set_state_changed_cb(callback, user_data);
2058
2059         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set state changed callback");
2060         return VC_ERROR_NONE;
2061 }
2062
2063 int vc_mgr_unset_state_changed_cb(void)
2064 {
2065         int ret;
2066         ret = __check_mgr_feature_privilege();
2067         if (VC_ERROR_NONE != ret)
2068                 return ret;
2069
2070         vc_state_e state;
2071         if (0 != vc_mgr_client_get_client_state(&state)) {
2072                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset state changed callback : A handle is not available");
2073                 return VC_ERROR_INVALID_STATE;
2074         }
2075
2076         /* check state */
2077         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2078
2079         vc_mgr_client_set_state_changed_cb(NULL, NULL);
2080
2081         return VC_ERROR_NONE;
2082 }
2083
2084 int vc_mgr_set_service_state_changed_cb(vc_service_state_changed_cb callback, void* user_data)
2085 {
2086         int ret;
2087         ret = __check_mgr_feature_privilege();
2088         if (VC_ERROR_NONE != ret)
2089                 return ret;
2090
2091         if (callback == NULL)
2092                 return VC_ERROR_INVALID_PARAMETER;
2093
2094         vc_state_e state;
2095         if (0 != vc_mgr_client_get_client_state(&state)) {
2096                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set state changed callback : A handle is not available");
2097                 return VC_ERROR_INVALID_STATE;
2098         }
2099
2100         /* check state */
2101         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2102
2103         vc_mgr_client_set_service_state_changed_cb(callback, user_data);
2104
2105         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set service state changed callback");
2106         return VC_ERROR_NONE;
2107 }
2108
2109 int vc_mgr_unset_service_state_changed_cb(void)
2110 {
2111         int ret;
2112         ret = __check_mgr_feature_privilege();
2113         if (VC_ERROR_NONE != ret)
2114                 return ret;
2115
2116         vc_state_e state;
2117         if (0 != vc_mgr_client_get_client_state(&state)) {
2118                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset state changed callback : A handle is not available");
2119                 return VC_ERROR_INVALID_STATE;
2120         }
2121
2122         /* check state */
2123         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2124
2125         vc_mgr_client_set_service_state_changed_cb(NULL, NULL);
2126         return VC_ERROR_NONE;
2127 }
2128
2129 int vc_mgr_set_speech_detected_cb(vc_mgr_begin_speech_detected_cb callback, void* user_data)
2130 {
2131         int ret;
2132         ret = __check_mgr_feature_privilege();
2133         if (VC_ERROR_NONE != ret)
2134                 return ret;
2135
2136         if (callback == NULL)
2137                 return VC_ERROR_INVALID_PARAMETER;
2138
2139         vc_state_e state;
2140         if (0 != vc_mgr_client_get_client_state(&state)) {
2141                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set state changed callback : A handle is not available");
2142                 return VC_ERROR_INVALID_STATE;
2143         }
2144
2145         /* check state */
2146         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2147
2148         vc_mgr_client_set_speech_detected_cb(callback, user_data);
2149
2150         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set speech detected callback");
2151         return VC_ERROR_NONE;
2152 }
2153
2154 int vc_mgr_unset_speech_detected_cb(void)
2155 {
2156         int ret;
2157         ret = __check_mgr_feature_privilege();
2158         if (VC_ERROR_NONE != ret)
2159                 return ret;
2160
2161         vc_state_e state;
2162         if (0 != vc_mgr_client_get_client_state(&state)) {
2163                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset state changed callback : A handle is not available");
2164                 return VC_ERROR_INVALID_STATE;
2165         }
2166
2167         /* check state */
2168         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2169
2170         vc_mgr_client_set_speech_detected_cb(NULL, NULL);
2171         return VC_ERROR_NONE;
2172 }
2173
2174 int vc_mgr_set_current_language_changed_cb(vc_current_language_changed_cb callback, void* user_data)
2175 {
2176         int ret;
2177         ret = __check_mgr_feature_privilege();
2178         if (VC_ERROR_NONE != ret)
2179                 return ret;
2180
2181         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2182
2183         vc_state_e state;
2184         if (0 != vc_mgr_client_get_client_state(&state)) {
2185                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set current language changed : A handle is not available");
2186                 return VC_ERROR_INVALID_STATE;
2187         }
2188
2189         /* check state */
2190         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2191
2192         vc_mgr_client_set_current_lang_changed_cb(callback, user_data);
2193
2194         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set current language changed callback");
2195         return VC_ERROR_NONE;
2196 }
2197
2198 int vc_mgr_unset_current_language_changed_cb(void)
2199 {
2200         int ret;
2201         ret = __check_mgr_feature_privilege();
2202         if (VC_ERROR_NONE != ret)
2203                 return ret;
2204
2205         vc_state_e state;
2206         if (0 != vc_mgr_client_get_client_state(&state)) {
2207                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset current language changed : A handle is not available");
2208                 return VC_ERROR_INVALID_STATE;
2209         }
2210
2211         /* check state */
2212         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2213
2214         vc_mgr_client_set_current_lang_changed_cb(NULL, NULL);
2215
2216         return VC_ERROR_NONE;
2217 }
2218
2219 int vc_mgr_set_error_cb(vc_error_cb callback, void* user_data)
2220 {
2221         int ret;
2222         ret = __check_mgr_feature_privilege();
2223         if (VC_ERROR_NONE != ret)
2224                 return ret;
2225
2226         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2227
2228         vc_state_e state;
2229         if (0 != vc_mgr_client_get_client_state(&state)) {
2230                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set error callback : A handle is not available");
2231                 return VC_ERROR_INVALID_STATE;
2232         }
2233
2234         /* check state */
2235         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2236
2237         vc_mgr_client_set_error_cb(callback,  user_data);
2238
2239         return VC_ERROR_NONE;
2240 }
2241
2242 int vc_mgr_unset_error_cb(void)
2243 {
2244         int ret;
2245         ret = __check_mgr_feature_privilege();
2246         if (VC_ERROR_NONE != ret)
2247                 return ret;
2248
2249         vc_state_e state;
2250         if (0 != vc_mgr_client_get_client_state(&state)) {
2251                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset error callback : A handle is not available");
2252                 return VC_ERROR_INVALID_STATE;
2253         }
2254
2255         /* check state */
2256         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2257
2258         vc_mgr_client_set_error_cb(NULL, NULL);
2259
2260         return VC_ERROR_NONE;
2261 }
2262
2263 int vc_mgr_set_dialog_request_cb(vc_mgr_dialog_request_cb callback, void* user_data)
2264 {
2265         int ret;
2266         ret = __check_mgr_feature_privilege();
2267         if (VC_ERROR_NONE != ret)
2268                 return ret;
2269
2270         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2271
2272         vc_state_e state;
2273         if (0 != vc_mgr_client_get_client_state(&state)) {
2274                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set error callback : A handle is not available");
2275                 return VC_ERROR_INVALID_STATE;
2276         }
2277
2278         /* check state */
2279         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2280
2281         vc_mgr_client_set_dialog_request_cb(callback,  user_data);
2282
2283         return VC_ERROR_NONE;
2284 }
2285
2286 int vc_mgr_unset_dialog_request_cb(void)
2287 {
2288         int ret;
2289         ret = __check_mgr_feature_privilege();
2290         if (VC_ERROR_NONE != ret)
2291                 return ret;
2292
2293         vc_state_e state;
2294         if (0 != vc_mgr_client_get_client_state(&state)) {
2295                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset error callback : A handle is not available");
2296                 return VC_ERROR_INVALID_STATE;
2297         }
2298
2299         /* check state */
2300         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2301
2302         vc_mgr_client_set_dialog_request_cb(NULL, NULL);
2303
2304         return VC_ERROR_NONE;
2305 }
2306
2307 int vc_mgr_set_private_data_set_cb(vc_mgr_private_data_set_cb callback, void* user_data)
2308 {
2309         int ret;
2310         ret = __check_mgr_feature_privilege();
2311         if (VC_ERROR_NONE != ret)
2312                 return ret;
2313
2314         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2315
2316         vc_state_e state;
2317         if (0 != vc_mgr_client_get_client_state(&state)) {
2318                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set private data set callback : A handle is not available");
2319                 return VC_ERROR_INVALID_STATE;
2320         }
2321
2322         /* check state */
2323         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Set private data set callback : Current state is not 'Initialized'");
2324
2325         vc_mgr_client_set_private_data_set_cb(callback, user_data);
2326
2327         return VC_ERROR_NONE;
2328 }
2329
2330 int vc_mgr_unset_private_data_set_cb(void)
2331 {
2332         int ret;
2333         ret = __check_mgr_feature_privilege();
2334         if (VC_ERROR_NONE != ret)
2335                 return ret;
2336
2337         vc_state_e state;
2338         if (0 != vc_mgr_client_get_client_state(&state)) {
2339                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset private data set callback : A handle is not available");
2340                 return VC_ERROR_INVALID_STATE;
2341         }
2342
2343         /* check state */
2344         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Unset private data set callback : Current state is not 'Initialized'");
2345
2346         vc_mgr_client_set_private_data_set_cb(NULL, NULL);
2347
2348         return VC_ERROR_NONE;
2349 }
2350
2351 int vc_mgr_set_private_data_requested_cb(vc_mgr_private_data_requested_cb callback, void* user_data)
2352 {
2353         int ret;
2354         ret = __check_mgr_feature_privilege();
2355         if (VC_ERROR_NONE != ret)
2356                 return ret;
2357
2358         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2359
2360         vc_state_e state;
2361         if (0 != vc_mgr_client_get_client_state(&state)) {
2362                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set private data requested callback : A handle is not available");
2363                 return VC_ERROR_INVALID_STATE;
2364         }
2365
2366         /* check state*/
2367         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Set private data requested callback : Current state is not 'Initialized'");
2368
2369         vc_mgr_client_set_private_data_requested_cb(callback, user_data);
2370
2371         return VC_ERROR_NONE;
2372 }
2373
2374 int vc_mgr_unset_private_data_requested_cb(void)
2375 {
2376         int ret;
2377         ret = __check_mgr_feature_privilege();
2378         if (VC_ERROR_NONE != ret)
2379                 return ret;
2380
2381         vc_state_e state;
2382         if (0 != vc_mgr_client_get_client_state(&state)) {
2383                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset private data requested callback : A handle is not available");
2384                 return VC_ERROR_INVALID_STATE;
2385         }
2386
2387         /* check state */
2388         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Unset private data requested callback : Current state is not 'Initialized'");
2389
2390         vc_mgr_client_set_private_data_requested_cb(NULL, NULL);
2391
2392         return VC_ERROR_NONE;
2393 }
2394
2395 int vc_mgr_set_specific_engine_result_cb(vc_mgr_specific_engine_result_cb callback, void* user_data)
2396 {
2397         int ret;
2398         ret = __check_mgr_feature_privilege();
2399         if (VC_ERROR_NONE != ret)
2400                 return ret;
2401
2402         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2403
2404         vc_state_e state;
2405         if (0 != vc_mgr_client_get_client_state(&state)) {
2406                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set specific engine result callback : A handle is not available");
2407                 return VC_ERROR_INVALID_STATE;
2408         }
2409
2410         /* check state */
2411         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2412
2413         vc_mgr_client_set_specific_engine_result_cb(callback, user_data);
2414
2415         return VC_ERROR_NONE;
2416 }
2417 int vc_mgr_unset_specific_engine_result_cb(void)
2418 {
2419         if (0 != __vc_mgr_get_feature_enabled()) {
2420                 return VC_ERROR_NOT_SUPPORTED;
2421         }
2422
2423         vc_state_e state;
2424         if (0 != vc_mgr_client_get_client_state(&state)) {
2425                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset specific engine result callback : A handle is not available");
2426                 return VC_ERROR_INVALID_STATE;
2427         }
2428
2429         /* check state */
2430         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2431
2432         vc_mgr_client_set_specific_engine_result_cb(NULL, NULL);
2433
2434         return VC_ERROR_NONE;
2435 }
2436
2437 /* for TTS feedback */
2438 int vc_mgr_set_feedback_audio_format_cb(vc_mgr_feedback_audio_format_cb callback, void* user_data)
2439 {
2440         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] set feedback audio format");
2441
2442         int ret;
2443         ret = __check_mgr_feature_privilege();
2444         if (VC_ERROR_NONE != ret)
2445                 return ret;
2446
2447         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2448
2449         vc_state_e state;
2450         if (0 != vc_mgr_client_get_client_state(&state)) {
2451                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback audio format callback : A handle is not available");
2452                 return VC_ERROR_INVALID_STATE;
2453         }
2454
2455         /* check state */
2456         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Set feedback audio format callback : Current state is not 'Initialized'");
2457
2458         vc_mgr_client_set_feedback_audio_format_cb(callback, user_data);
2459
2460         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set feedback audio format callback");
2461
2462         return VC_ERROR_NONE;
2463 }
2464
2465 int vc_mgr_unset_feedback_audio_format_cb()
2466 {
2467         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] unset feedback audio format");
2468
2469         int ret;
2470         ret = __check_mgr_feature_privilege();
2471         if (VC_ERROR_NONE != ret)
2472                 return ret;
2473
2474         vc_state_e state;
2475         if (0 != vc_mgr_client_get_client_state(&state)) {
2476                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback audio format callback : A handle is not available");
2477                 return VC_ERROR_INVALID_STATE;
2478         }
2479
2480         /* check state */
2481         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Unset feedback audio format callback : Current state is not 'Initialized'");
2482
2483         vc_mgr_client_set_feedback_audio_format_cb(NULL, NULL);
2484
2485         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Unset feedback audio format callback");
2486
2487         return VC_ERROR_NONE;
2488 }
2489
2490 int vc_mgr_set_feedback_streaming_cb(vc_mgr_feedback_streaming_cb callback, void* user_data)
2491 {
2492         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] set feedback streaming");
2493
2494         int ret;
2495         ret = __check_mgr_feature_privilege();
2496         if (VC_ERROR_NONE != ret)
2497                 return ret;
2498
2499         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2500
2501         pthread_mutex_lock(&g_feedback_streaming_cb_mutex);
2502
2503         vc_state_e state;
2504         if (0 != vc_mgr_client_get_client_state(&state)) {
2505                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback streaming callback : A handle is not available");
2506                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
2507                 return VC_ERROR_INVALID_STATE;
2508         }
2509
2510         /* check state */
2511         if (VC_STATE_INITIALIZED != state) {
2512                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback streaming callback : Current state is not 'Initialized' (%d)", state);
2513                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
2514                 return VC_ERROR_INVALID_STATE;
2515         }
2516
2517         vc_mgr_client_set_feedback_streaming_cb(callback, user_data);
2518
2519         pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
2520
2521         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set feedback streaming callback");
2522
2523         return VC_ERROR_NONE;
2524 }
2525
2526 int vc_mgr_unset_feedback_streaming_cb()
2527 {
2528         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] unset feedback streaming");
2529
2530         int ret;
2531         ret = __check_mgr_feature_privilege();
2532         if (VC_ERROR_NONE != ret)
2533                 return ret;
2534
2535         pthread_mutex_lock(&g_feedback_streaming_cb_mutex);
2536
2537         vc_state_e state;
2538         if (0 != vc_mgr_client_get_client_state(&state)) {
2539                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback streaming callback : A handle is not available");
2540                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
2541                 return VC_ERROR_INVALID_STATE;
2542         }
2543
2544         /* check state */
2545         if (VC_STATE_INITIALIZED != state) {
2546                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback streaming callback : Current state is not 'Initialized' (%d)", state);
2547                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
2548                 return VC_ERROR_INVALID_STATE;
2549         }
2550
2551         vc_mgr_client_set_feedback_streaming_cb(NULL, NULL);
2552
2553         pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
2554
2555         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Unset feedback streaming callback");
2556
2557         return VC_ERROR_NONE;
2558 }
2559
2560 int vc_mgr_set_vc_tts_streaming_cb(vc_mgr_vc_tts_streaming_cb callback, void* user_data)
2561 {
2562         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] set vc tts streaming callback");
2563
2564         int ret;
2565         ret = __check_mgr_feature_privilege();
2566         if (VC_ERROR_NONE != ret)
2567                 return ret;
2568
2569         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2570
2571         pthread_mutex_lock(&g_vc_tts_streaming_cb_mutex);
2572
2573         vc_state_e state;
2574         if (0 != vc_mgr_client_get_client_state(&state)) {
2575                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback streaming callback : A handle is not available");
2576                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
2577                 return VC_ERROR_INVALID_STATE;
2578         }
2579
2580         /* check state */
2581         if (VC_STATE_INITIALIZED != state) {
2582                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback streaming callback : Current state is not 'Initialized' (%d)", state);
2583                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
2584                 return VC_ERROR_INVALID_STATE;
2585         }
2586
2587         vc_mgr_client_set_vc_tts_streaming_cb(callback, user_data);
2588
2589         pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
2590
2591         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set feedback streaming callback");
2592
2593         return VC_ERROR_NONE;
2594 }
2595
2596 int vc_mgr_unset_vc_tts_streaming_cb()
2597 {
2598         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] unset vc tts streaming callback");
2599
2600         int ret;
2601         ret = __check_mgr_feature_privilege();
2602         if (VC_ERROR_NONE != ret)
2603                 return ret;
2604
2605         pthread_mutex_lock(&g_vc_tts_streaming_cb_mutex);
2606
2607         vc_state_e state;
2608         if (0 != vc_mgr_client_get_client_state(&state)) {
2609                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback streaming callback : A handle is not available");
2610                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
2611                 return VC_ERROR_INVALID_STATE;
2612         }
2613
2614         /* check state */
2615         if (VC_STATE_INITIALIZED != state) {
2616                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback streaming callback : Current state is not 'Initialized' (%d)", state);
2617                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
2618                 return VC_ERROR_INVALID_STATE;
2619         }
2620
2621         vc_mgr_client_set_vc_tts_streaming_cb(NULL, NULL);
2622
2623         pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
2624
2625         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Unset feedback streaming callback");
2626
2627         return VC_ERROR_NONE;
2628 }
2629
2630 static void __tts_feedback_thread(void* data, Ecore_Thread* thread)
2631 {
2632         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Start thread");
2633
2634         vc_feedback_data_s* feedback_data = NULL;
2635
2636         while (1) {
2637                 int ret = -1;
2638                 int cnt = 0;
2639
2640                 /* get feedback data */
2641                 ret = vc_mgr_data_get_feedback_data(&feedback_data);
2642                 if (0 != ret || NULL == feedback_data) {
2643                         /* empty queue */
2644                         SLOG(LOG_INFO, TAG_VCM, "[DEBUG] No feedback data. Waiting mode");
2645
2646                         /* waiting */
2647                         while (1) {
2648                                 usleep(10000);
2649                                 if (0 < vc_mgr_data_get_feedback_data_size()) {
2650                                         SLOG(LOG_INFO, TAG_VCM, "[INFO] Resume thread");
2651                                         break;
2652                                 }
2653                                 if (200 < cnt) {
2654                                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Wrong request, there's no pcm data");
2655                                         vc_mgr_data_clear_feedback_data(&feedback_data);
2656                                         return;
2657                                 }
2658                                 cnt++;
2659                         }
2660                         SLOG(LOG_INFO, TAG_VCM, "[INFO] Finish to wait for new feedback data come");
2661
2662                         /* resume feedback thread */
2663                         vc_mgr_data_clear_feedback_data(&feedback_data);
2664                         continue;
2665                 }
2666
2667                 if (NULL != feedback_data) {
2668                         if (getpid() == feedback_data->pid) {
2669                                 vc_mgr_feedback_streaming_cb callback = NULL;
2670                                 void* user_data = NULL;
2671
2672                                 pthread_mutex_lock(&g_feedback_streaming_cb_mutex);
2673
2674                                 vc_mgr_client_get_feedback_streaming_cb(&callback, &user_data);
2675                                 if (NULL == callback) {
2676                                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] TTS feedback streaming callback is null");
2677                                         vc_mgr_data_clear_feedback_data(&feedback_data);
2678                                         pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
2679                                         return;
2680                                 }
2681
2682                                 SLOG(LOG_DEBUG, TAG_VCM, "TTS feedback streaming callback is called");
2683                                 vc_mgr_client_use_callback();
2684                                 callback(feedback_data->event, feedback_data->data, feedback_data->data_size, user_data);
2685                                 vc_mgr_client_not_use_callback();
2686
2687                                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
2688
2689                                 /* If no feedback data and EVENT_FINISH */
2690                                 if (0 >= vc_mgr_data_get_feedback_data_size() && VC_FEEDBACK_EVENT_FINISH == feedback_data->event) {
2691                                         SLOG(LOG_INFO, TAG_VCM, "[INFO] Finish feedback");
2692                                         if (feedback_data) {
2693                                                 free(feedback_data);
2694                                                 feedback_data = NULL;
2695                                         }
2696                                         break;
2697                                 }
2698                         } else {
2699                                 vc_mgr_vc_tts_streaming_cb callback = NULL;
2700                                 void* user_data = NULL;
2701
2702                                 pthread_mutex_lock(&g_vc_tts_streaming_cb_mutex);
2703
2704                                 vc_mgr_client_get_vc_tts_streaming_cb(&callback, &user_data);
2705                                 if (NULL == callback) {
2706                                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] vc tts streaming callback is null");
2707                                         vc_mgr_data_clear_feedback_data(&feedback_data);
2708                                         pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
2709                                         return;
2710                                 }
2711
2712                                 SLOG(LOG_DEBUG, TAG_VCM, "vc tts feedback streaming callback is called");
2713                                 vc_mgr_client_use_callback();
2714                                 callback(feedback_data->pid, feedback_data->utt_id, feedback_data->event, feedback_data->data, feedback_data->data_size, user_data);
2715                                 vc_mgr_client_not_use_callback();
2716
2717                                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
2718
2719                                 /* If no feedback data and EVENT_FINISH */
2720                                 if (0 >= vc_mgr_data_get_feedback_data_size() && VC_FEEDBACK_EVENT_FINISH == feedback_data->event) {
2721                                         SLOG(LOG_INFO, TAG_VCM, "[INFO] Finish vc tts feedback");
2722                                         if (feedback_data) {
2723                                                 free(feedback_data);
2724                                                 feedback_data = NULL;
2725                                         }
2726                                         break;
2727                                 }
2728                         }
2729                         free(feedback_data);
2730                         feedback_data = NULL;
2731                 }
2732         }
2733 }
2734
2735 static void __end_tts_feedback_thread(void* data, Ecore_Thread* thread)
2736 {
2737         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] End thread");
2738         g_feedback_thread = NULL;
2739 }
2740
2741 static void __cancel_tts_feedback_thread(void* data, Ecore_Thread* thread)
2742 {
2743         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Cancel thread");
2744         g_feedback_thread = NULL;
2745 }
2746
2747 int vc_mgr_start_feedback(void)
2748 {
2749         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] start feedback");
2750
2751         int ret;
2752         ret = __check_mgr_feature_privilege();
2753         if (VC_ERROR_NONE != ret)
2754                 return ret;
2755
2756         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Request start TTS feedback");
2757
2758         vc_state_e state;
2759         if (0 != vc_mgr_client_get_client_state(&state)) {
2760                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Start feedback : A handle is not available");
2761                 return VC_ERROR_INVALID_STATE;
2762         }
2763
2764         /* check state */
2765         RETVM_IF(VC_STATE_READY != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Start feedback : Current state is not 'Ready' (%d)", state);
2766
2767 #if 1
2768         bool is_exist = ecore_thread_check(g_feedback_thread);
2769         if (NULL == g_feedback_thread || TRUE == is_exist) {
2770                 SLOG(LOG_INFO, TAG_VCM, "[INFO] ecore thread run : __tts_feedback_thread");
2771                 g_feedback_thread = ecore_thread_run(__tts_feedback_thread, __end_tts_feedback_thread, __cancel_tts_feedback_thread, NULL);
2772         }
2773 #else
2774         /* start playing TTS feedback */
2775         int ret = -1;
2776         ret = vc_mgr_player_play();
2777         if (0 != ret) {
2778                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to player play, ret(%d)", ret);
2779         }
2780 #endif
2781         return VC_ERROR_NONE;
2782 }
2783
2784 int vc_mgr_stop_feedback(void)
2785 {
2786         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] stop feedback");
2787
2788         int ret;
2789         ret = __check_mgr_feature_privilege();
2790         if (VC_ERROR_NONE != ret)
2791                 return ret;
2792
2793         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Request stop TTS feedback");
2794
2795         vc_state_e state;
2796         if (0 != vc_mgr_client_get_client_state(&state)) {
2797                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Stop feedback : A handle is not available");
2798                 return VC_ERROR_INVALID_STATE;
2799         }
2800
2801         /* check state */
2802         RETVM_IF(VC_STATE_READY != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Stop feedback : Current state is not 'Ready' (%d)", state);
2803
2804 #if 1
2805         ret = -1;
2806
2807         while (1) {
2808                 vc_feedback_data_s* feedback_data = NULL;
2809
2810                 /* get feedback data */
2811                 ret = vc_mgr_data_get_feedback_data(&feedback_data);
2812                 if (0 != ret || NULL == feedback_data) {
2813                         /* empty queue */
2814                         SLOG(LOG_INFO, TAG_VCM, "[INFO] No feedback data to stop any more");
2815                         return VC_ERROR_NONE;
2816                 }
2817                 ret = vc_mgr_data_clear_feedback_data(&feedback_data);
2818                 if (0 != ret) {
2819                         SLOG(LOG_INFO, TAG_VCM, "[ERROR] Fail to clear data, ret(%d)", ret);
2820                         break;
2821                 }
2822         }
2823 #else
2824         /* request to stop playing TTS feedback */
2825         int ret = -1;
2826         ret = vc_mgr_player_stop();
2827         if (0 != ret) {
2828                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to player stop, ret(%d)", ret);
2829         }
2830 #endif
2831         return ret;
2832 }
2833
2834 int vc_mgr_send_utterance_status(int pid, int utt_id, vc_tts_utterance_status_e utt_status)
2835 {
2836         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] send utterance status, pid(%d), utt_id(%d), utt_status(%d)", pid, utt_id, utt_status);
2837
2838         int ret;
2839         ret = __check_mgr_feature_privilege();
2840         if (VC_ERROR_NONE != ret)
2841                 return ret;
2842
2843         if (VC_TTS_UTTERANCE_NONE > utt_status || VC_TTS_UTTERANCE_CANCELED < utt_status || 0 == utt_status) {
2844                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] utt_status is not valid. (%d)", utt_status);
2845                 return VC_ERROR_INVALID_PARAMETER;
2846         }
2847
2848         vc_state_e state;
2849         if (0 != vc_mgr_client_get_client_state(&state)) {
2850                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
2851                 return VC_ERROR_INVALID_STATE;
2852         }
2853
2854         /* check state */
2855         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
2856
2857         /* Check service state */
2858         vc_service_state_e service_state = -1;
2859         vc_mgr_client_get_service_state(&service_state);
2860         RETVM_IF(service_state != VC_SERVICE_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current service state(%d) is not 'READY'", service_state);
2861
2862         ret = vc_mgr_tidl_send_utterance_status(pid, utt_id, utt_status);
2863         if (0 != ret)
2864                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to send utterance status : %s", __vc_mgr_get_error_code(ret));
2865         else
2866                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Send utterance status");
2867
2868         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] send utterance status DONE");
2869         return ret;
2870 }
2871
2872 static float __get_volume_decibel(char* data, int size)
2873 {
2874 #define MAX_AMPLITUDE_MEAN_16 32768
2875
2876         int i, depthByte;
2877         int count = 0;
2878
2879         float db = 0.0;
2880         float rms = 0.0;
2881         unsigned long long square_sum = 0;
2882         short pcm16 = 0;
2883
2884         depthByte = 2;
2885
2886         for (i = 0; i < size; i += (depthByte<<1)) {
2887                 pcm16 = 0;
2888                 memcpy(&pcm16, data + i, sizeof(short));
2889                 square_sum += pcm16 * pcm16;
2890                 count++;
2891         }
2892
2893         if (0 == count || 0 == square_sum) {
2894                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No data");
2895                 rms = 1.0;
2896         } else {
2897                 rms = sqrt((float)square_sum/count);
2898         }
2899
2900         db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
2901         return db;
2902 }
2903
2904 int vc_mgr_send_audio_streaming(vc_audio_streaming_event_e event, unsigned char* buffer, unsigned int len)
2905 {
2906         SLOG(LOG_INFO, TAG_VCM, "[Manager] Send audio streaming to the engine service, event(%d)", event);
2907
2908         int ret;
2909         ret = __check_mgr_feature_privilege();
2910         if (VC_ERROR_NONE != ret)
2911                 return ret;
2912
2913         if (VC_AUDIO_STREAMING_EVENT_FAIL > event || VC_AUDIO_STREAMING_EVENT_FINISH < event || 0 == event) {
2914                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] event is invalid parameter (%d)", event);
2915                 return VC_ERROR_INVALID_PARAMETER;
2916         }
2917
2918         RETVM_IF(NULL == buffer, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2919
2920         vc_state_e state = VC_STATE_NONE;
2921         ret = vc_mgr_client_get_client_state(&state);
2922         if (0 != ret) {
2923                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
2924                 return VC_ERROR_INVALID_STATE;
2925         }
2926
2927         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
2928
2929         vc_service_state_e service_state = VC_SERVICE_STATE_NONE;
2930         ret = vc_mgr_client_get_service_state(&service_state);
2931         if (0 != ret) {
2932                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
2933                 return VC_ERROR_INVALID_STATE;
2934         }
2935
2936         if (VC_SERVICE_STATE_READY != service_state && VC_SERVICE_STATE_RECORDING != service_state) {
2937                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: service state is not 'READY' and 'RECORDING', state(%d)", service_state);
2938                 return VC_ERROR_INVALID_STATE;
2939         }
2940
2941         float volume = __get_volume_decibel((char*)buffer, len);
2942         // TODO: check __vc_mgr_set_volume need to move
2943         __vc_mgr_set_volume(volume);
2944
2945         ret = vc_mgr_tidl_send_audio_streaming(g_pid, event, buffer, len);
2946         if (0 != ret)
2947                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] retry to send audio streaming data");
2948         else
2949                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Success to send");
2950         return ret;
2951 }
2952
2953 int vc_mgr_set_audio_streaming_mode(vc_audio_streaming_mode_e mode)
2954 {
2955         SLOG(LOG_INFO, TAG_VCM, "[Manager] Set multi assistant mode, mode(%d)", mode);
2956
2957         int ret;
2958         ret = __check_mgr_feature_privilege();
2959         if (VC_ERROR_NONE != ret)
2960                 return ret;
2961
2962         if (VC_AUDIO_STREAMING_MODE_VC_SERVICE > mode || VC_AUDIO_STREAMING_MODE_OUTSIDE < mode) {
2963                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] mode is invalid parameter (%d)", mode);
2964                 return VC_ERROR_INVALID_PARAMETER;
2965         }
2966
2967         vc_state_e state = VC_STATE_NONE;
2968         ret = vc_mgr_client_get_client_state(&state);
2969         if (0 != ret) {
2970                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
2971                 return VC_ERROR_INVALID_STATE;
2972         }
2973
2974         vc_service_state_e service_state = VC_SERVICE_STATE_NONE;
2975         ret = vc_mgr_client_get_service_state(&service_state);
2976         if (0 != ret) {
2977                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
2978                 return VC_ERROR_INVALID_STATE;
2979         }
2980
2981         if (VC_STATE_INITIALIZED == state && VC_SERVICE_STATE_NONE == service_state) {
2982                 ret = vc_mgr_client_set_audio_streaming_mode(mode);
2983                 if (VC_ERROR_NONE != ret) {
2984                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set multi assistant mode : %s", __vc_mgr_get_error_code(ret));
2985                         return VC_ERROR_INVALID_STATE;
2986                 } else {
2987                         SLOG(LOG_DEBUG, TAG_VCM, "[SUCCESS] Set audio streaming mode in vc mgr client");
2988                 }
2989         } else if (VC_STATE_READY == state && VC_SERVICE_STATE_READY == service_state) {
2990                 ret = vc_mgr_client_set_audio_streaming_mode(mode);
2991                 if (VC_ERROR_NONE != ret) {
2992                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set multi assistant mode : %s", __vc_mgr_get_error_code(ret));
2993                         return VC_ERROR_INVALID_STATE;
2994                 } else {
2995                         SLOG(LOG_DEBUG, TAG_VCM, "[SUCCESS] Set audio streaming mode in vc mgr client");
2996                 }
2997
2998                 ret = vc_mgr_tidl_request_set_audio_streaming_mode(g_pid, mode);
2999                 if (VC_ERROR_NONE != ret) {
3000                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set audio streaming mode : %s", __vc_mgr_get_error_code(ret));
3001                         return VC_ERROR_OPERATION_FAILED;
3002                 } else {
3003                         SLOG(LOG_DEBUG, TAG_VCM, "[SUCCESS] Set audio streaming mode in vc mgr tidl");
3004                 }
3005         } else {
3006                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current state is not 'INITIALIZED', state(%d)", state);
3007                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current service state is not 'READY', service_state(%d)", service_state);
3008                 return VC_ERROR_INVALID_STATE;
3009         }
3010
3011         return VC_ERROR_NONE;
3012 }
3013
3014 //TODO it's internal api, so will remove it.
3015 int vc_mgr_change_system_volume(vc_system_volume_event_e event)
3016 {
3017         return vc_mgr_change_background_volume(event);
3018 }
3019
3020 //TODO it's internal api, so will remove it.
3021 int vc_mgr_recover_system_volume(void)
3022 {
3023         return vc_mgr_reset_background_volume();
3024 }
3025
3026 int vc_mgr_change_background_volume(vc_background_volume_event_e event)
3027 {
3028         SLOG(LOG_INFO, TAG_VCM, "[Manager] Change background volume. event(%d)", event);
3029
3030         int ret;
3031         ret = __check_mgr_feature_privilege();
3032         if (VC_ERROR_NONE != ret)
3033                 return ret;
3034
3035         if (VC_BACKGROUND_VOLUME_EVENT_CHANGE_FOR_NEARFIELD > event || VC_BACKGROUND_VOLUME_EVENT_CHANGE_FOR_FARFIELD < event) {
3036                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] event is invalid parameter (%d)", event);
3037                 return VC_ERROR_INVALID_PARAMETER;
3038         }
3039
3040         vc_state_e state = VC_STATE_NONE;
3041         ret = vc_mgr_client_get_client_state(&state);
3042         if (VC_ERROR_NONE != ret) {
3043                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
3044                 return VC_ERROR_INVALID_STATE;
3045         }
3046
3047         if (state != VC_STATE_READY && state != VC_STATE_INITIALIZED) {
3048                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current state is not 'READY' and not 'INITIALIZED', state(%d)", state);
3049                 return VC_ERROR_INVALID_STATE;
3050         }
3051
3052         double ratio = 0.0;
3053         if (VC_BACKGROUND_VOLUME_EVENT_CHANGE_FOR_FARFIELD == event)
3054                 ratio = VC_MGR_DEFAULT_FARFIELD_DUCKING_RATIO;
3055         else if (VC_BACKGROUND_VOLUME_EVENT_CHANGE_FOR_NEARFIELD == event)
3056                 ratio = VC_MGR_DEFAULT_NEARFIELD_DUCKING_RATIO;
3057
3058         ret = vc_mgr_ducking_activate(ratio);
3059         if (VC_ERROR_NONE != ret)
3060                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to change volume");
3061         else
3062                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Success to change volume");
3063         return ret;
3064 }
3065
3066 int vc_mgr_change_background_volume_by_ratio(double ratio)
3067 {
3068         SLOG(LOG_INFO, TAG_VCM, "[Manager] Change background volume. ratio(%f)", ratio);
3069
3070         int ret;
3071         ret = __check_mgr_feature_privilege();
3072         if (VC_ERROR_NONE != ret)
3073                 return ret;
3074
3075         if (VC_MGR_MINIMUM_DUCKING_RATIO > ratio || VC_MGR_MAXIMUM_DUCKING_RATIO < ratio) {
3076                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] ratio is invalid parameter (%lf)", ratio);
3077                 return VC_ERROR_INVALID_PARAMETER;
3078         }
3079
3080         vc_state_e state = VC_STATE_NONE;
3081         ret = vc_mgr_client_get_client_state(&state);
3082         if (VC_ERROR_NONE != ret) {
3083                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
3084                 return VC_ERROR_INVALID_STATE;
3085         }
3086
3087         if (state != VC_STATE_READY && state != VC_STATE_INITIALIZED) {
3088                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current state is not 'READY' and not 'INITIALIZED', state(%d)", state);
3089                 return VC_ERROR_INVALID_STATE;
3090         }
3091
3092         ret = vc_mgr_ducking_activate(ratio);
3093         if (VC_ERROR_NONE != ret)
3094                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set ratio");
3095         else
3096                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Success to set ratio");
3097
3098         return ret;
3099 }
3100
3101 int vc_mgr_reset_background_volume(void)
3102 {
3103         SLOG(LOG_INFO, TAG_VCM, "[Manager] Recover background volume");
3104
3105         int ret;
3106         ret = __check_mgr_feature_privilege();
3107         if (VC_ERROR_NONE != ret)
3108                 return ret;
3109
3110         vc_state_e state = VC_STATE_NONE;
3111         ret = vc_mgr_client_get_client_state(&state);
3112         if (VC_ERROR_NONE != ret) {
3113                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
3114                 return VC_ERROR_INVALID_STATE;
3115         }
3116
3117         if (state != VC_STATE_READY && state != VC_STATE_INITIALIZED) {
3118                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current state is not 'READY' and not 'INITIALIZED', state(%d)", state);
3119                 return VC_ERROR_INVALID_STATE;
3120         }
3121
3122         ret = vc_mgr_ducking_deactivate();
3123         if (VC_ERROR_NONE != ret)
3124                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to recover volume");
3125         else
3126                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Success to recover volume");
3127
3128         return ret;
3129 }