Remove unnecessary logs
[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 <sound_manager.h>
24 #include <sound_manager_internal.h>
25 #include <math.h>
26
27 #include "vc_cmd_db.h"
28 #include "vc_config_mgr.h"
29 #include "vc_command.h"
30 #include "vc_info_parser.h"
31 #include "vc_json_parser.h"
32 #include "vc_main.h"
33 #include "vc_mgr_client.h"
34 #include "vc_mgr_tidl.h"
35 #include "vc_mgr_data.h"
36 #include "vc_mgr_player.h"
37 #include "voice_control.h"
38 #include "voice_control_command.h"
39 #include "voice_control_command_expand.h"
40 #include "voice_control_common.h"
41 #include "voice_control_manager.h"
42 #include "voice_control_manager_internal.h"
43
44
45 #define VC_MANAGER_CONFIG_HANDLE        10000000
46
47 static Ecore_Timer* g_send_hello_timer = NULL;
48 static Ecore_Timer* g_request_init_timer = NULL;
49 static int g_tidl_send_hello_count = 0;
50
51 static Ecore_Timer* g_m_set_volume_timer = NULL;
52
53 static vc_h g_vc_m = NULL;
54
55 static GSList* g_demandable_client_list = NULL;
56
57 static float g_volume_db = 0;
58
59 static float g_prev_volume_db = 0;
60
61 static float g_cur_volume_db = 0;
62
63 static int g_daemon_pid = 0;
64
65 static int g_feature_enabled = -1;
66
67 static bool g_privilege_allowed = false;
68
69 static cynara *p_cynara = NULL;
70 static pthread_mutex_t g_cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
71
72 static bool g_err_callback_status = false;
73
74 /* for changing volume on each sound stream */
75 static sound_stream_info_h      g_stream_for_volume_h = NULL;
76 static virtual_sound_stream_h   g_virtual_sound_stream_h = NULL;
77
78 /* for TTS feedback */
79 static int g_feedback_rate = 16000;
80 static vc_audio_channel_e g_feedback_audio_channel = 0;
81 static vc_audio_type_e g_feedback_audio_type = 0;
82 static Ecore_Thread* g_feedback_thread = NULL;
83
84 static pthread_mutex_t g_feedback_streaming_cb_mutex = PTHREAD_MUTEX_INITIALIZER;
85 static pthread_mutex_t g_vc_tts_streaming_cb_mutex = PTHREAD_MUTEX_INITIALIZER;
86
87 static Eina_Bool __vc_mgr_notify_state_changed(void *data);
88 static void __vc_mgr_notify_error(void *data);
89 static Eina_Bool __vc_mgr_notify_result(void *data);
90
91 int __vc_mgr_cb_error(int reason, int daemon_pid, char* msg);
92
93
94 static const char* __vc_mgr_get_error_code(vc_error_e err)
95 {
96         switch (err) {
97         case VC_ERROR_NONE:                     return "VC_ERROR_NONE";
98         case VC_ERROR_OUT_OF_MEMORY:            return "VC_ERROR_OUT_OF_MEMORY";
99         case VC_ERROR_IO_ERROR:                 return "VC_ERROR_IO_ERROR";
100         case VC_ERROR_INVALID_PARAMETER:        return "VC_ERROR_INVALID_PARAMETER";
101         case VC_ERROR_TIMED_OUT:                return "VC_ERROR_TIMED_OUT";
102         case VC_ERROR_RECORDER_BUSY:            return "VC_ERROR_RECORDER_BUSY";
103         case VC_ERROR_INVALID_STATE:            return "VC_ERROR_INVALID_STATE";
104         case VC_ERROR_INVALID_LANGUAGE:         return "VC_ERROR_INVALID_LANGUAGE";
105         case VC_ERROR_ENGINE_NOT_FOUND:         return "VC_ERROR_ENGINE_NOT_FOUND";
106         case VC_ERROR_OPERATION_FAILED:         return "VC_ERROR_OPERATION_FAILED";
107         default:                                return "Invalid error code";
108         }
109         return NULL;
110 }
111
112 static void __vc_mgr_lang_changed_cb(const char* before_lang, const char* current_lang)
113 {
114         SLOG(LOG_INFO, TAG_VCM, "Lang changed : Before lang(%s) Current lang(%s)",
115                                 before_lang, current_lang);
116
117         vc_current_language_changed_cb callback = NULL;
118         void* lang_user_data;
119         vc_mgr_client_get_current_lang_changed_cb(g_vc_m, &callback, &lang_user_data);
120
121         if (NULL != callback) {
122                 vc_mgr_client_use_callback(g_vc_m);
123                 callback(before_lang, current_lang, lang_user_data);
124                 vc_mgr_client_not_use_callback(g_vc_m);
125                 SLOG(LOG_INFO, TAG_VCM, "Language changed callback is called");
126         } else {
127                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Language changed callback is null");
128         }
129
130         return;
131 }
132
133 static int __vc_mgr_get_feature_enabled()
134 {
135         if (0 == g_feature_enabled) {
136                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Voice control feature NOT supported");
137                 return VC_ERROR_NOT_SUPPORTED;
138         } else if (-1 == g_feature_enabled) {
139                 bool vc_supported = false;
140                 bool mic_supported = false;
141                 if (0 == system_info_get_platform_bool(VC_MGR_FEATURE_PATH, &vc_supported)) {
142                         if (0 == system_info_get_platform_bool(VC_MIC_FEATURE_PATH, &mic_supported)) {
143                                 if (false == vc_supported || false == mic_supported) {
144                                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Voice control feature NOT supported");
145                                         g_feature_enabled = 0;
146                                         return VC_ERROR_NOT_SUPPORTED;
147                                 }
148
149                                 g_feature_enabled = 1;
150                         }
151                 }
152         }
153
154         return VC_ERROR_NONE;
155 }
156
157 static int __check_privilege_initialize()
158 {
159         int ret = cynara_initialize(&p_cynara, NULL);
160         if (NULL == p_cynara || CYNARA_API_SUCCESS != ret) {
161                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] fail to initialize(%d)", ret);
162                 return ret;
163         }
164
165         return ret == CYNARA_API_SUCCESS;
166 }
167
168 static int __check_privilege(const char* uid, const char * privilege)
169 {
170         FILE *fp = NULL;
171         char label_path[1024] = "/proc/self/attr/current";
172         char smack_label[1024] = {'\0',};
173
174         if (!p_cynara) {
175                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] p_cynara is NULL");
176                 return false;
177         }
178
179         fp = fopen(label_path, "r");
180         if (fp != NULL) {
181                 if (fread(smack_label, 1, sizeof(smack_label), fp) <= 0)
182                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] fail to fread");
183
184                 fclose(fp);
185         }
186
187         pid_t pid = getpid();
188         char *session = cynara_session_from_pid(pid);
189         int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
190         if (session)
191                 free(session);
192         session = NULL;
193
194         if (ret != CYNARA_API_ACCESS_ALLOWED) {
195                 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);
196                 return false;
197         }
198
199         return true;
200 }
201
202 static void __check_privilege_deinitialize()
203 {
204         if (p_cynara) {
205                 int ret = cynara_finish(p_cynara);
206                 if (ret != CYNARA_API_SUCCESS)
207                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] cynara finish %d", ret);
208         }
209         p_cynara = NULL;
210 }
211
212 static int __vc_mgr_check_privilege()
213 {
214         if (true == g_privilege_allowed)
215                 return VC_ERROR_NONE;
216
217         pthread_mutex_lock(&g_cynara_mutex);
218
219         if (false == g_privilege_allowed) {
220                 bool ret = true;
221                 ret = __check_privilege_initialize();
222                 if (false == ret) {
223                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] privilege initialize is failed");
224                         g_privilege_allowed = false;
225                         pthread_mutex_unlock(&g_cynara_mutex);
226                         return VC_ERROR_PERMISSION_DENIED;
227                 }
228
229                 char uid[32];
230                 snprintf(uid, 32, "%d", getuid());
231                 ret = true;
232                 ret = __check_privilege(uid, VC_PRIVILEGE_RECORDER);
233                 if (false == ret) {
234                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Permission is denied(%s)(%s)", VC_PRIVILEGE_RECORDER, uid);
235                         __check_privilege_deinitialize();
236                         g_privilege_allowed = false;
237                         pthread_mutex_unlock(&g_cynara_mutex);
238                         return VC_ERROR_PERMISSION_DENIED;
239                 }
240
241                 ret = __check_privilege(uid, VC_MGR_PRIVILEGE);
242                 if (false == ret) {
243                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Permission is denied(%s)(%s)", VC_MGR_PRIVILEGE, uid);
244                         __check_privilege_deinitialize();
245                         g_privilege_allowed = false;
246                         pthread_mutex_unlock(&g_cynara_mutex);
247                         return VC_ERROR_PERMISSION_DENIED;
248                 }
249
250                 ret = __check_privilege(uid, VC_PRIVILEGE_DATASHARING);
251                 if (false == ret) {
252                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Permission is denied(%s)(%s)", VC_PRIVILEGE_DATASHARING, uid);
253                         __check_privilege_deinitialize();
254                         g_privilege_allowed = false;
255                         pthread_mutex_unlock(&g_cynara_mutex);
256                         return VC_ERROR_PERMISSION_DENIED;
257                 }
258
259                 ret = __check_privilege(uid, VC_PRIVILEGE_APPMGR);
260                 if (false == ret) {
261                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Permission is denied(%s)(%s)", VC_PRIVILEGE_APPMGR, uid);
262                         __check_privilege_deinitialize();
263                         g_privilege_allowed = false;
264                         pthread_mutex_unlock(&g_cynara_mutex);
265                         return VC_ERROR_PERMISSION_DENIED;
266                 }
267
268                 __check_privilege_deinitialize();
269         }
270
271         g_privilege_allowed = true;
272         pthread_mutex_unlock(&g_cynara_mutex);
273         return VC_ERROR_NONE;
274 }
275
276 int vc_mgr_initialize(void)
277 {
278         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Initialize");
279
280
281         if (0 != __vc_mgr_get_feature_enabled()) {
282                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
283                 return VC_ERROR_NOT_SUPPORTED;
284         }
285
286         if (0 != __vc_mgr_check_privilege()) {
287                 return VC_ERROR_PERMISSION_DENIED;
288         }
289
290         /* check handle */
291         if (true == vc_mgr_client_is_valid(g_vc_m)) {
292                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Already initialized");
293                 return VC_ERROR_NONE;
294         }
295
296         if (0 != vc_mgr_tidl_open_connection()) {
297                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to open tidl connection");
298                 return VC_ERROR_OPERATION_FAILED;
299         }
300
301         if (0 != vc_mgr_client_create(&g_vc_m)) {
302                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create client!!!!!");
303                 return VC_ERROR_OUT_OF_MEMORY;
304         }
305
306         int ret = vc_config_mgr_initialize(g_vc_m->handle + VC_MANAGER_CONFIG_HANDLE);
307         if (0 != ret) {
308                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to init config manager : %d", ret);
309                 vc_mgr_client_destroy(g_vc_m);
310                 return VC_ERROR_OPERATION_FAILED;
311         }
312
313         ret = vc_config_mgr_set_lang_cb(g_vc_m->handle + VC_MANAGER_CONFIG_HANDLE, __vc_mgr_lang_changed_cb);
314         if (0 != ret) {
315                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set callback : %d", ret);
316                 vc_config_mgr_finalize(g_vc_m->handle + VC_MANAGER_CONFIG_HANDLE);
317                 vc_mgr_client_destroy(g_vc_m);
318                 return VC_ERROR_OPERATION_FAILED;
319         }
320
321         ret = vc_mgr_client_set_audio_streaming_mode(g_vc_m, VC_AUDIO_STREAMING_MODE_VC_SERVICE);
322         if (0 != ret) {
323                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set audio streaming mode : %d", ret);
324                 vc_config_mgr_finalize(g_vc_m->handle + VC_MANAGER_CONFIG_HANDLE);
325                 vc_mgr_client_destroy(g_vc_m);
326                 return ret;
327         }
328
329         /* for TTS feedback */
330 /*      ret = vc_mgr_player_init();
331         if (0 != ret) {
332                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to initialize VC mgr player : %d", ret);
333         }
334 */
335         SLOG(LOG_ERROR, TAG_VCM, "[Success] pid(%d)", g_vc_m->handle);
336
337         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Initialize DONE");
338
339         return VC_ERROR_NONE;
340 }
341
342 static void __vc_mgr_internal_unprepare()
343 {
344         int ret = vc_mgr_tidl_request_finalize(g_vc_m->handle);
345         if (0 != ret) {
346                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request finalize : %s", __vc_mgr_get_error_code(ret));
347         }
348
349         vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_NONE);
350         return;
351 }
352
353 int vc_mgr_deinitialize(void)
354 {
355         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Deinitialize");
356
357         if (0 != __vc_mgr_get_feature_enabled()) {
358                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
359                 return VC_ERROR_NOT_SUPPORTED;
360         }
361
362         if (0 != __vc_mgr_check_privilege()) {
363                 return VC_ERROR_PERMISSION_DENIED;
364         }
365
366         if (false == vc_mgr_client_is_valid(g_vc_m)) {
367                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] NOT initialized");
368                 return VC_ERROR_INVALID_STATE;
369         }
370
371         vc_state_e state;
372         vc_mgr_client_get_client_state(g_vc_m, &state);
373
374         /* check state */
375         switch (state) {
376         case VC_STATE_READY:
377                 __vc_mgr_internal_unprepare();
378                 /* no break. need to next step*/
379         case VC_STATE_INITIALIZED:
380                 if (NULL != g_send_hello_timer) {
381                         SLOG(LOG_DEBUG, TAG_VCM, "Connect Timer is deleted");
382                         ecore_timer_del(g_send_hello_timer);
383                         g_send_hello_timer = NULL;
384                 }
385                 if (g_request_init_timer) {
386                         SLOG(LOG_DEBUG, TAG_VCM, "Connect idler is deleted");
387                         ecore_timer_del(g_request_init_timer);
388                         g_request_init_timer = NULL;
389                 }
390
391                 vc_config_mgr_unset_lang_cb(g_vc_m->handle + VC_MANAGER_CONFIG_HANDLE);
392                 vc_config_mgr_finalize(g_vc_m->handle + VC_MANAGER_CONFIG_HANDLE);
393
394                 /* Free client resources */
395                 vc_mgr_client_destroy(g_vc_m);
396                 g_vc_m = NULL;
397                 break;
398         case VC_STATE_NONE:
399                 break;
400         }
401
402         SLOG(LOG_DEBUG, TAG_VCM, "Success: destroy");
403
404         int cnt = VC_COMMAND_TYPE_FOREGROUND;
405         do {
406                 int ret = vc_cmd_parser_delete_file(getpid(), cnt);
407                 if (0 != ret)
408                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to delete file, type(%d), ret(%d)", cnt, ret);
409         } while (VC_COMMAND_TYPE_EXCLUSIVE >= ++cnt);
410
411         int ret = vc_db_finalize();
412         if (0 != ret) {
413                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to finalize DB, ret(%d)", ret);
414         }
415
416         /* for TTS feedback */
417 /*      ret = vc_mgr_player_release();
418         if (0 != ret) {
419                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to release VC mgr player(%d)", ret);
420         }
421 */
422
423         if (0 != vc_mgr_tidl_close_connection()) {
424                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to close connection");
425         }
426
427         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Deinitialize DONE");
428
429         return VC_ERROR_NONE;
430 }
431
432 static Eina_Bool __request_initialize(void *data)
433 {
434         /* request initialization */
435         int ret = -1;
436         int service_state = 0;
437         int foreground = VC_RUNTIME_INFO_NO_FOREGROUND;
438
439
440         /* check handle */
441         if (true == vc_mgr_client_is_valid(g_vc_m)) {
442                 SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] g_vc_m is valid");
443
444                 /* Initialize DB */
445                 ret = vc_db_initialize();
446                 if (0 != ret) {
447                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to initialize DB : %d", ret);
448                         return EINA_TRUE;
449                 }
450
451                 vc_audio_streaming_mode_e streaming_mode;
452                 vc_mgr_client_get_audio_streaming_mode(g_vc_m, &streaming_mode);
453
454                 ret = vc_mgr_tidl_request_initialize(g_vc_m->handle, (int)streaming_mode, &service_state, &foreground, &g_daemon_pid);
455
456                 if (VC_ERROR_ENGINE_NOT_FOUND == ret) {
457                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to initialize : %s", __vc_mgr_get_error_code(ret));
458
459                         vc_mgr_client_set_error(g_vc_m, VC_ERROR_ENGINE_NOT_FOUND);
460                         ecore_main_loop_thread_safe_call_async(__vc_mgr_notify_error, (void*)g_vc_m);
461
462                         SLOG(LOG_DEBUG, TAG_VCM, "@@@");
463                         g_request_init_timer = NULL;
464                         return EINA_FALSE;
465
466                 } else if (0 != ret) {
467                         SLOG(LOG_ERROR, TAG_VCM, "[WARNING] Fail to connection. Retry to connect : %s", __vc_mgr_get_error_code(ret));
468                         ret = vc_db_finalize();
469                         if (0 != ret) {
470                                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to finalize DB : %d", ret);
471                         }
472                         return EINA_TRUE;
473                 } else {
474                         /* Success to connect */
475                 }
476
477                 /* Set service state */
478                 vc_service_state_e previous_service_state;
479                 vc_mgr_client_get_service_state(g_vc_m, &previous_service_state);
480                 vc_mgr_client_set_service_state(g_vc_m, (vc_service_state_e)service_state);
481
482                 vc_service_state_changed_cb service_changed_callback = NULL;
483                 void* user_data = NULL;
484                 vc_mgr_client_get_service_state_changed_cb(g_vc_m, &service_changed_callback, &user_data);
485
486                 if (NULL != service_changed_callback) {
487                         vc_mgr_client_use_callback(g_vc_m);
488                         service_changed_callback(previous_service_state, service_state, user_data);
489                         vc_mgr_client_not_use_callback(g_vc_m);
490                         SLOG(LOG_INFO, TAG_VCM, "Service state changed callback is called");
491                 } else {
492                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] Service state changed callback is null");
493                 }
494
495                 /* Set foreground */
496                 vc_mgr_client_set_foreground(g_vc_m, foreground, true);
497
498                 SLOG(LOG_ERROR, TAG_VCM, "[SUCCESS] Connected daemon");
499
500                 /* Set client state */
501                 vc_mgr_client_set_client_state(g_vc_m, VC_STATE_READY);
502
503                 vc_state_changed_cb changed_callback = NULL;
504                 user_data = NULL;
505                 vc_mgr_client_get_state_changed_cb(g_vc_m, &changed_callback, &user_data);
506
507                 vc_state_e current_state;
508                 vc_state_e before_state;
509
510                 vc_mgr_client_get_before_state(g_vc_m, &current_state, &before_state);
511
512                 if (NULL != changed_callback) {
513                         vc_mgr_client_use_callback(g_vc_m);
514                         changed_callback(before_state, current_state, user_data);
515                         vc_mgr_client_not_use_callback(g_vc_m);
516                         SLOG(LOG_INFO, TAG_VCM, "State changed callback is called");
517                 } else {
518                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] State changed callback is null");
519                 }
520         } else {
521                 SLOG(LOG_ERROR, TAG_VCM, "[Not ERROR] g_vc_m is not valid. It is destroyed."); //LCOV_EXCL_LINE
522                 g_request_init_timer = NULL;
523                 return EINA_FALSE;
524         }
525
526         SLOG(LOG_ERROR, TAG_VCM, "@@@");
527
528         g_request_init_timer = NULL;
529         return EINA_FALSE;
530 }
531
532
533 static Eina_Bool __send_hello_message(void *data)
534 {
535         /* Send hello */
536         if (0 != vc_mgr_tidl_request_hello()) {
537                 if (g_tidl_send_hello_count == VC_TIDL_RETRY_COUNT) {
538                         g_tidl_send_hello_count = 0;
539                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request hello !! send error to manager");
540                         __vc_mgr_cb_error(VC_ERROR_TIMED_OUT, -1, "voice_framework.error.vcfw.connect_engine_fail");
541                         g_send_hello_timer = NULL;
542                         return EINA_FALSE;
543                 } else {
544                         // TODO: discuss when updating connect/reconnect
545                         g_tidl_send_hello_count++;
546                         usleep(200000);
547                         return EINA_TRUE;
548                 }
549         }
550
551         SLOG(LOG_DEBUG, TAG_VCM, "===== [Manager] Connect daemon");
552
553         if (NULL == g_request_init_timer) {
554                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Create a new idler for preparation");
555                 g_request_init_timer = ecore_timer_add(0.0, __request_initialize, NULL);
556         } else {
557                 SLOG(LOG_INFO, TAG_VCM, "[INFO] idler handle is already created");
558         }
559
560         g_send_hello_timer = NULL;
561         return EINA_FALSE;
562 }
563
564 int vc_mgr_prepare(void)
565 {
566         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Prepare");
567
568         if (0 != __vc_mgr_get_feature_enabled()) {
569                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
570                 return VC_ERROR_NOT_SUPPORTED;
571         }
572
573         if (0 != __vc_mgr_check_privilege()) {
574                 return VC_ERROR_PERMISSION_DENIED;
575         }
576
577         vc_state_e state;
578         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
579                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
580                 return VC_ERROR_INVALID_STATE;
581         }
582
583         /* check state */
584         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
585
586         if (NULL == g_send_hello_timer) {
587                 g_tidl_send_hello_count = 0;
588                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Create a new timer for preparation");
589                 ecore_thread_main_loop_begin();
590                 g_send_hello_timer = ecore_timer_add(0.02, __send_hello_message, NULL);
591                 ecore_thread_main_loop_end();
592         } else {
593                 SLOG(LOG_INFO, TAG_VCM, "[INFO] timer handle is already created");
594         }
595
596         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Calling Prepare DONE");
597
598         return VC_ERROR_NONE;
599 }
600
601 int vc_mgr_unprepare(void)
602 {
603         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Unprepare");
604
605         if (0 != __vc_mgr_get_feature_enabled()) {
606                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
607                 return VC_ERROR_NOT_SUPPORTED;
608         }
609
610         if (0 != __vc_mgr_check_privilege()) {
611                 return VC_ERROR_PERMISSION_DENIED;
612         }
613
614         vc_state_e state;
615         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
616                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
617                 return VC_ERROR_INVALID_STATE;
618         }
619
620         /* check state */
621         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
622
623         __vc_mgr_internal_unprepare();
624
625         vc_mgr_client_set_client_state(g_vc_m, VC_STATE_INITIALIZED);
626         ecore_timer_add(0, __vc_mgr_notify_state_changed, g_vc_m);
627
628         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Unprepare DONE");
629
630         return VC_ERROR_NONE;
631 }
632
633 int vc_mgr_foreach_supported_languages(vc_supported_language_cb callback, void* user_data)
634 {
635         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Foreach Supported Language");
636
637         if (0 != __vc_mgr_get_feature_enabled()) {
638                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
639                 return VC_ERROR_NOT_SUPPORTED;
640         }
641
642         if (0 != __vc_mgr_check_privilege()) {
643                 return VC_ERROR_PERMISSION_DENIED;
644         }
645
646         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
647
648         vc_state_e state;
649         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
650                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
651                 return VC_ERROR_INVALID_STATE;
652         }
653
654         int ret = -1;
655         ret = vc_config_mgr_get_language_list(callback, user_data);
656         if (0 != ret) {
657                 ret = vc_config_convert_error_code((vc_config_error_e)ret);
658                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get languages : %s", __vc_mgr_get_error_code(ret));
659         }
660
661         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Foreach Supported Language DONE");
662
663         return VC_ERROR_NONE;
664 }
665
666 int vc_mgr_get_current_language(char** language)
667 {
668         if (0 != __vc_mgr_get_feature_enabled()) {
669                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
670                 return VC_ERROR_NOT_SUPPORTED;
671         }
672
673         if (0 != __vc_mgr_check_privilege()) {
674                 return VC_ERROR_PERMISSION_DENIED;
675         }
676
677         RETVM_IF(NULL == language, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
678
679         vc_state_e state;
680         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
681                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
682                 return VC_ERROR_INVALID_STATE;
683         }
684
685         int ret = -1;
686         ret = vc_config_mgr_get_default_language(language);
687         if (0 != ret) {
688                 ret = vc_config_convert_error_code((vc_config_error_e)ret);
689                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get current languages : %s", __vc_mgr_get_error_code(ret));
690         } else {
691                 SLOG(LOG_ERROR, TAG_VCM, "[Get current language] language : %s", *language);
692         }
693
694         return ret;
695 }
696
697 int vc_mgr_get_state(vc_state_e* state)
698 {
699         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get State");
700
701         if (0 != __vc_mgr_get_feature_enabled()) {
702                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
703                 return VC_ERROR_NOT_SUPPORTED;
704         }
705
706         if (0 != __vc_mgr_check_privilege()) {
707                 return VC_ERROR_PERMISSION_DENIED;
708         }
709
710         RETVM_IF(NULL == state, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
711
712         vc_state_e temp;
713         if (NULL == g_vc_m) {
714                 temp = VC_STATE_NONE;
715         } else if (0 != vc_mgr_client_get_client_state(g_vc_m, &temp)) {
716                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
717                 return VC_ERROR_INVALID_STATE;
718         }
719
720         *state = temp;
721
722         switch (*state) {
723         case VC_STATE_NONE:             SLOG(LOG_INFO, TAG_VCM, "Current state is 'None'");             break;
724         case VC_STATE_INITIALIZED:      SLOG(LOG_INFO, TAG_VCM, "Current state is 'Created'");          break;
725         case VC_STATE_READY:            SLOG(LOG_INFO, TAG_VCM, "Current state is 'Ready'");            break;
726         default:                        SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid state");
727         }
728
729         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get State DONE");
730
731         return VC_ERROR_NONE;
732 }
733
734 int vc_mgr_get_service_state(vc_service_state_e* state)
735 {
736         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get Service State");
737
738         if (0 != __vc_mgr_get_feature_enabled()) {
739                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
740                 return VC_ERROR_NOT_SUPPORTED;
741         }
742
743         if (0 != __vc_mgr_check_privilege()) {
744                 return VC_ERROR_PERMISSION_DENIED;
745         }
746
747         RETVM_IF(NULL == state, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
748
749         vc_state_e client_state;
750         if (0 != vc_mgr_client_get_client_state(g_vc_m, &client_state)) {
751                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
752                 return VC_ERROR_INVALID_STATE;
753         }
754
755         /* check state */
756         RETVM_IF(client_state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Current state is not 'READY'");
757
758         /* get service state */
759         vc_service_state_e service_state;
760         if (0 != vc_mgr_client_get_service_state(g_vc_m, &service_state)) {
761                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get service state");
762                 return VC_ERROR_OPERATION_FAILED;
763         }
764
765         *state = service_state;
766
767         switch (*state) {
768         case VC_SERVICE_STATE_NONE:             SLOG(LOG_DEBUG, TAG_VCM, "Current service state is 'None'");            break;
769         case VC_SERVICE_STATE_READY:            SLOG(LOG_DEBUG, TAG_VCM, "Current service state is 'Ready'");           break;
770         case VC_SERVICE_STATE_RECORDING:        SLOG(LOG_DEBUG, TAG_VCM, "Current service state is 'Recording'");       break;
771         case VC_SERVICE_STATE_PROCESSING:       SLOG(LOG_DEBUG, TAG_VCM, "Current service state is 'Processing'");      break;
772         default:                                SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid state");
773         }
774
775         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get Service State DONE");
776
777         return VC_ERROR_NONE;
778 }
779
780 int vc_mgr_set_demandable_client_rule(const char* rule)
781 {
782         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Set Demandable client rule(%s)", rule);
783
784         if (0 != __vc_mgr_get_feature_enabled()) {
785                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
786                 return VC_ERROR_NOT_SUPPORTED;
787         }
788
789         if (0 != __vc_mgr_check_privilege()) {
790                 return VC_ERROR_PERMISSION_DENIED;
791         }
792
793         vc_state_e state;
794         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
795                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
796                 return VC_ERROR_INVALID_STATE;
797         }
798
799         /* check state */
800         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
801
802         int ret = -1;
803         ret = vc_info_parser_set_demandable_client(rule);
804         if (0 != ret) {
805                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] rule is NOT valid");
806                 SLOG(LOG_DEBUG, TAG_VCM, "@@@");
807                 return VC_ERROR_INVALID_PARAMETER;
808         }
809
810         if (0 != vc_info_parser_get_demandable_clients(&g_demandable_client_list)) {
811                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get demandable clients");
812                 return VC_ERROR_OPERATION_FAILED;
813         }
814
815         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Set Demandable client rule(%s) DONE", rule);
816
817         return VC_ERROR_NONE;
818 }
819
820 int vc_mgr_unset_demandable_client_rule(void)
821 {
822         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Unset Demandable client");
823
824         if (0 != __vc_mgr_get_feature_enabled()) {
825                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
826                 return VC_ERROR_NOT_SUPPORTED;
827         }
828
829         if (0 != __vc_mgr_check_privilege()) {
830                 return VC_ERROR_PERMISSION_DENIED;
831         }
832
833         vc_info_parser_set_demandable_client(NULL);
834
835         int ret = vc_mgr_tidl_request_demandable_client(g_vc_m->handle);
836         if (0 != ret) {
837                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request unset client rule to daemon : %s", __vc_mgr_get_error_code(ret));
838         }
839
840         // TODO: check return value correct or not
841         return VC_ERROR_NONE;
842 }
843
844 int vc_mgr_is_command_format_supported(int format, bool* support)
845 {
846         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Is command type supported");
847
848         if (0 != __vc_mgr_get_feature_enabled()) {
849                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
850                 return VC_ERROR_NOT_SUPPORTED;
851         }
852
853         if (0 != __vc_mgr_check_privilege()) {
854                 return VC_ERROR_PERMISSION_DENIED;
855         }
856
857         RETVM_IF(NULL == support, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
858
859         vc_state_e state;
860         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
861                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
862                 return VC_ERROR_INVALID_STATE;
863         }
864
865         /* check support */
866         bool non_fixed_support = false;
867         if (0 != vc_config_mgr_get_nonfixed_support(&non_fixed_support)) {
868                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get nonfixed support info");
869         }
870
871         switch (format) {
872         case VC_CMD_FORMAT_FIXED:               *support = true;                break;
873         case VC_CMD_FORMAT_FIXED_AND_VFIXED:    *support = true;                break;
874         case VC_CMD_FORMAT_VFIXED_AND_FIXED:    *support = true;                break;
875         case VC_CMD_FORMAT_FIXED_AND_NONFIXED:  *support = non_fixed_support;   break;
876         case VC_CMD_FORMAT_NONFIXED_AND_FIXED:  *support = non_fixed_support;   break;
877         default:                                *support = false;               break;
878         }
879
880         SLOG(LOG_ERROR, TAG_VCM, "[DEBUG] Format(%d) support(%s)", format, *support ? "true" : "false");
881
882         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Is command type supported DONE");
883
884         return VC_ERROR_NONE;
885 }
886
887 int vc_mgr_enable_command_type(int cmd_type)
888 {
889         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Enable Command Type, cmd_type(%d)", cmd_type);
890
891         if (0 != __vc_mgr_get_feature_enabled()) {
892                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
893                 return VC_ERROR_NOT_SUPPORTED;
894         }
895
896         if (0 != __vc_mgr_check_privilege()) {
897                 return VC_ERROR_PERMISSION_DENIED;
898         }
899
900         if (VC_COMMAND_TYPE_FOREGROUND > cmd_type || VC_COMMAND_TYPE_EXCLUSIVE < cmd_type) {
901                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] cmd_type is not valid. (%d)", cmd_type);
902                 return VC_ERROR_INVALID_PARAMETER;
903         }
904
905         vc_state_e state;
906         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
907                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
908                 return VC_ERROR_INVALID_STATE;
909         }
910
911         /* check state */
912         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
913
914         /* Check service state */
915         vc_service_state_e service_state = -1;
916         vc_mgr_client_get_service_state(g_vc_m, &service_state);
917         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);
918
919         int ret;
920
921         ret = vc_mgr_client_enable_command_type(g_vc_m, (vc_cmd_type_e)cmd_type);               // enable the cmd type
922         if (0 != ret) {
923                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to enable the command type(%d). ret(%d)", cmd_type, ret);
924         } else {
925                 SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] Success to enable the command type(%d)", cmd_type);
926         }
927
928         SLOG(LOG_ERROR, TAG_VCM, "@@@  [Manager] Enable Command Type DONE, ret(%d)", ret);
929
930         return ret;
931 }
932
933 int vc_mgr_disable_command_type(int cmd_type)
934 {
935         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Disable Command Type, cmd_type(%d)", cmd_type);
936
937         if (0 != __vc_mgr_get_feature_enabled()) {
938                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
939                 return VC_ERROR_NOT_SUPPORTED;
940         }
941
942         if (0 != __vc_mgr_check_privilege()) {
943                 return VC_ERROR_PERMISSION_DENIED;
944         }
945
946         if (VC_COMMAND_TYPE_FOREGROUND > cmd_type || VC_COMMAND_TYPE_EXCLUSIVE < cmd_type) {
947                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] cmd type is not valid, (%d)", cmd_type);
948                 return VC_ERROR_INVALID_PARAMETER;
949         }
950
951         vc_state_e state;
952         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
953                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
954                 return VC_ERROR_INVALID_STATE;
955         }
956
957         /* check state */
958         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
959
960         /* Check service state */
961         vc_service_state_e service_state = -1;
962         vc_mgr_client_get_service_state(g_vc_m, &service_state);
963         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);
964
965         int ret;
966
967         ret = vc_mgr_client_disable_command_type(g_vc_m, (vc_cmd_type_e)cmd_type);              // enable the cmd type
968         if (0 != ret) {
969                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to disable the command type(%d). ret(%d)", cmd_type, ret);
970         } else {
971                 SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] Success to disable the command type(%d)", cmd_type);
972         }
973
974         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Disable Command Type DONE, ret(%d)", ret);
975
976         return ret;
977 }
978
979 int vc_mgr_set_command_list(vc_cmd_list_h vc_cmd_list)
980 {
981         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set Command list");
982
983         if (0 != __vc_mgr_get_feature_enabled()) {
984                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
985                 return VC_ERROR_NOT_SUPPORTED;
986         }
987
988         if (0 != __vc_mgr_check_privilege()) {
989                 return VC_ERROR_PERMISSION_DENIED;
990         }
991
992         vc_state_e state;
993         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
994                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
995                 return VC_ERROR_INVALID_STATE;
996         }
997
998         /* check state */
999         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1000
1001         /* Check service state */
1002         vc_service_state_e service_state = -1;
1003         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1004         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);
1005
1006         RETVM_IF(NULL == vc_cmd_list, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input parameter is NULL");
1007
1008         vc_cmd_list_s* list = NULL;
1009         list = (vc_cmd_list_s*)vc_cmd_list;
1010         SLOG(LOG_INFO, TAG_VCM, "[List] (%p) (%p)", list, list->list);
1011
1012         RETVM_IF(NULL == list->list, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Input command list");
1013
1014         int i;
1015         int ret;
1016         bool success_save = false;
1017         for (i = VC_COMMAND_TYPE_FOREGROUND; i <= VC_COMMAND_TYPE_EXCLUSIVE; i++) {
1018                 ret = vc_cmd_parser_delete_file(getpid(), i);
1019                 if (0 != ret)
1020                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to delete file, type(%d), ret(%d)", i, ret);
1021
1022                 ret = vc_cmd_parser_save_file(getpid(), i, list->list, NULL);
1023                 if (0 != ret) {
1024                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to save file, type(%d), ret(%d)", i, ret);
1025                 } else {
1026                         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Save file, type(%d)", i);
1027                         success_save = true;
1028                 }
1029         }
1030
1031         if (true != success_save) {
1032                 ret = VC_ERROR_INVALID_PARAMETER;
1033                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to save command group : %s", __vc_mgr_get_error_code(ret));
1034         } else {
1035                 ret = vc_mgr_tidl_request_set_command(g_vc_m->handle);
1036                 if (0 != ret) {
1037                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request set command to daemon : %s", __vc_mgr_get_error_code(ret));
1038                 }
1039         }
1040
1041         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set Command list DONE, ret(%d)", ret);
1042
1043         return ret;
1044 }
1045
1046 int vc_mgr_unset_command_list(void)
1047 {
1048         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Unset Command list");
1049
1050         if (0 != __vc_mgr_get_feature_enabled()) {
1051                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1052                 return VC_ERROR_NOT_SUPPORTED;
1053         }
1054
1055         if (0 != __vc_mgr_check_privilege()) {
1056                 return VC_ERROR_PERMISSION_DENIED;
1057         }
1058
1059         vc_state_e state;
1060         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1061                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1062                 return VC_ERROR_INVALID_STATE;
1063         }
1064
1065         /* check state */
1066         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1067
1068
1069         int ret = -1;
1070         ret = vc_mgr_tidl_request_unset_command(g_vc_m->handle);
1071         if (0 != ret) {
1072                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request unset command to daemon : %s", __vc_mgr_get_error_code(ret));
1073         }
1074
1075         int i;
1076         for (i = VC_COMMAND_TYPE_FOREGROUND; i <= VC_COMMAND_TYPE_EXCLUSIVE; i++) {
1077                 ret = vc_cmd_parser_delete_file(getpid(), i);
1078                 if (0 != ret)
1079                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to delete file, type(%d), ret(%d)", i, ret);
1080         }
1081
1082         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Unset Command list DONE");
1083
1084         // TODO: check return value correct or not
1085         return VC_ERROR_NONE;
1086 }
1087
1088 int vc_mgr_set_command_list_from_file(const char* file_path, int type)
1089 {
1090         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set Command list from file, type(%d)", type);
1091
1092         if (0 != __vc_mgr_get_feature_enabled()) {
1093                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1094                 return VC_ERROR_NOT_SUPPORTED;
1095         }
1096
1097         if (0 != __vc_mgr_check_privilege()) {
1098                 return VC_ERROR_PERMISSION_DENIED;
1099         }
1100
1101         RETVM_IF(NULL == file_path, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1102         SLOG(LOG_INFO, TAG_VCM, "@@@ File path: %s", file_path);
1103
1104         /* check type */
1105         if (VC_COMMAND_TYPE_FOREGROUND > type || VC_COMMAND_TYPE_EXCLUSIVE < type) {
1106                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid command type: input type is %d", type);
1107                 return VC_ERROR_INVALID_PARAMETER;
1108         }
1109
1110         vc_state_e state;
1111         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1112                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1113                 return VC_ERROR_INVALID_STATE;
1114         }
1115
1116         /* check state */
1117         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1118
1119         /* Check service state */
1120         vc_service_state_e service_state = -1;
1121         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1122         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);
1123
1124         int ret = vc_cmd_parser_delete_file(getpid(), type);
1125         if (0 != ret)
1126                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to delete file, type(4), ret(%d)", ret);
1127
1128         if (0 != vc_json_set_commands_from_file(file_path, type, NULL)) {
1129                 ret = VC_ERROR_INVALID_PARAMETER;
1130                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to save command list (%d)", ret);
1131         } else {
1132                 ret = vc_mgr_tidl_request_set_command(g_vc_m->handle);
1133                 if (0 != ret) {
1134                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to request set command to daemon : %s", __vc_mgr_get_error_code(ret));
1135                 }
1136         }
1137
1138         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set Command list from file DONE, ret(%d)", ret);
1139
1140         return ret;
1141 }
1142
1143 int vc_mgr_set_preloaded_commands_from_file(const char* file_path)
1144 {
1145         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set preloaded command list");
1146
1147         if (0 != __vc_mgr_get_feature_enabled()) {
1148                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1149                 return VC_ERROR_NOT_SUPPORTED;
1150         }
1151
1152         if (0 != __vc_mgr_check_privilege()) {
1153                 return VC_ERROR_PERMISSION_DENIED;
1154         }
1155
1156         RETVM_IF(NULL == file_path, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1157         SLOG(LOG_INFO, TAG_VCM, "@@@ File path: %s", file_path);
1158
1159         vc_state_e state;
1160         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1161                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1162                 return VC_ERROR_INVALID_STATE;
1163         }
1164
1165         /* check state */
1166         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1167
1168         /* Check service state */
1169         vc_service_state_e service_state = -1;
1170         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1171         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);
1172
1173         /* Only support to set background commands for preloaded application */
1174         int ret = vc_json_set_commands_from_file(file_path, VC_COMMAND_TYPE_BACKGROUND, NULL);
1175         if (0 != ret)
1176                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to insert preloaded commands into db");
1177
1178         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set preloaded command list DONE, ret(%d)", ret);
1179         return ret;
1180 }
1181
1182 int vc_mgr_set_audio_type(const char* audio_id)
1183 {
1184         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set audio type, audio_id(%s)", audio_id);
1185
1186         if (0 != __vc_mgr_get_feature_enabled()) {
1187                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1188                 return VC_ERROR_NOT_SUPPORTED;
1189         }
1190
1191         if (0 != __vc_mgr_check_privilege()) {
1192                 return VC_ERROR_PERMISSION_DENIED;
1193         }
1194
1195         if (NULL == audio_id || !strncmp(audio_id, "", strlen(audio_id))) {
1196                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Input parameter is NULL");
1197                 return VC_ERROR_INVALID_PARAMETER;
1198         }
1199
1200         vc_state_e state;
1201         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1202                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1203                 return VC_ERROR_INVALID_STATE;
1204         }
1205
1206         /* check state */
1207         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1208
1209         /* Check service state */
1210         vc_service_state_e service_state = -1;
1211         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1212         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);
1213
1214         int ret = -1;
1215         /* Request */
1216         ret = vc_mgr_tidl_request_set_audio_type(g_vc_m->handle, audio_id);
1217         if (0 != ret) {
1218                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set audio type : %s", __vc_mgr_get_error_code(ret));
1219         } else {
1220                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set audio type");
1221                 /* Save */
1222                 vc_mgr_client_set_audio_type(g_vc_m, audio_id);
1223         }
1224
1225         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Set audio type DONE, ret(%d)", ret);
1226
1227         return ret;
1228 }
1229
1230 int vc_mgr_get_audio_type(char** audio_id)
1231 {
1232         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Get audio type");
1233
1234         if (0 != __vc_mgr_get_feature_enabled()) {
1235                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1236                 return VC_ERROR_NOT_SUPPORTED;
1237         }
1238
1239         if (0 != __vc_mgr_check_privilege()) {
1240                 return VC_ERROR_PERMISSION_DENIED;
1241         }
1242
1243         RETVM_IF(NULL == audio_id, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1244
1245         vc_state_e state;
1246         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1247                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1248                 return VC_ERROR_INVALID_STATE;
1249         }
1250
1251         /* check state */
1252         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1253
1254         /* Check service state */
1255         vc_service_state_e service_state = -1;
1256         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1257         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);
1258
1259         char* temp = NULL;
1260
1261         vc_mgr_client_get_audio_type(g_vc_m, &temp);
1262
1263         if (NULL == temp) {
1264                 /* Not initiallized */
1265                 int ret = -1;
1266                 while (0 != ret) {
1267                         ret = vc_mgr_tidl_request_get_audio_type(g_vc_m->handle, &temp);
1268                         if (0 != ret) {
1269                                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get audio type : %s", __vc_mgr_get_error_code(ret));
1270                         } else {
1271                                 SLOG(LOG_ERROR, TAG_VCM, "[SUCCESS] Get audio type : %s", temp);
1272                                 /* Save */
1273                                 vc_mgr_client_set_audio_type(g_vc_m, temp);
1274                         }
1275                 }
1276         }
1277
1278         if (NULL != temp) {
1279                 *audio_id = strdup(temp);
1280                 free(temp);
1281                 temp = NULL;
1282         }
1283
1284         // TODO: check return value correct or not
1285         return VC_ERROR_NONE;
1286 }
1287
1288 int vc_mgr_get_current_commands(vc_cmd_list_h* vc_cmd_list)
1289 {
1290         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Foreach current commands");
1291
1292         if (0 != __vc_mgr_get_feature_enabled()) {
1293                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1294                 return VC_ERROR_NOT_SUPPORTED;
1295         }
1296
1297         if (0 != __vc_mgr_check_privilege()) {
1298                 return VC_ERROR_PERMISSION_DENIED;
1299         }
1300
1301         vc_state_e state;
1302         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1303                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1304                 return VC_ERROR_INVALID_STATE;
1305         }
1306
1307         /* check state */
1308         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1309
1310         /* Check service state */
1311         vc_service_state_e service_state = -1;
1312         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1313         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);
1314
1315         RETVM_IF(NULL == vc_cmd_list, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid Parameter");
1316
1317         vc_cmd_list_h temp_list = NULL;
1318         if (0 != vc_cmd_list_create(&temp_list)) {
1319                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create list");
1320                 return VC_ERROR_INVALID_PARAMETER;
1321         }
1322
1323         *vc_cmd_list = temp_list;
1324
1325         int fg_pid = 0;
1326         int mgr_pid = 0;
1327         int ret = -1;
1328
1329         /* Get foreground pid */
1330         if (0 != vc_mgr_client_get_foreground(g_vc_m, &fg_pid)) {
1331                 /* There is no foreground app for voice control */
1332                 SLOG(LOG_WARN, TAG_VCM, "[Manager WARNING] No foreground pid for voice control");
1333         } else {
1334                 SLOG(LOG_INFO, TAG_VCM, "[Manager] Foreground pid(%d)", fg_pid);
1335         }
1336
1337         if (0 != vc_mgr_client_get_pid(g_vc_m, &mgr_pid)) {
1338                 SLOG(LOG_ERROR, TAG_VCM, "[Manager] Fail to get manager pid");
1339                 return VC_ERROR_OPERATION_FAILED;
1340         } else {
1341                 SLOG(LOG_INFO, TAG_VCM, "[Manager] Manager pid(%d)", mgr_pid);
1342         }
1343
1344         /* Get system command */
1345         ret = vc_cmd_parser_append_commands(mgr_pid, VC_COMMAND_TYPE_SYSTEM, temp_list);
1346         if (0 != ret) {
1347                 SLOG(LOG_INFO, TAG_VCM, "[Manager] No system commands");
1348         }
1349
1350         /* Request */
1351         ret = vc_mgr_tidl_request_set_client_info(g_vc_m->handle);
1352         if (0 != ret) {
1353                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set client info : %s", __vc_mgr_get_error_code(ret));
1354         } else {
1355                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set client info");
1356         }
1357
1358         GSList *iter = NULL;
1359         GSList* client_info_list = NULL;
1360         vc_client_info_s *client_info = NULL;
1361         bool is_fgpid = false;
1362
1363         if (0 != vc_info_parser_get_client_info(&client_info_list)) {
1364                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] No client");
1365                 return VC_ERROR_NONE;
1366         }
1367
1368         if (VC_NO_FOREGROUND_PID != fg_pid) {
1369                 iter = g_slist_nth(client_info_list, 0);
1370                 while (NULL != iter) {
1371                         client_info = iter->data;
1372                         if (NULL != client_info) {
1373                                 if (fg_pid == client_info->pid) {
1374                                         is_fgpid = true;
1375                                         break;
1376                                 }
1377                         }
1378                         iter = g_slist_next(iter);
1379                 }
1380         }
1381
1382         /* Get foreground commands and widget */
1383         if (true == is_fgpid) {
1384                 /* Get handle */
1385                 SLOG(LOG_INFO, TAG_VCM, "[Manager] fore cmd(%d)", client_info->fg_cmd);
1386
1387                 /* Get foreground command */
1388                 if (true == client_info->fg_cmd) {
1389                         ret = vc_cmd_parser_append_commands(fg_pid, VC_COMMAND_TYPE_FOREGROUND, temp_list);
1390                         if (0 != ret) {
1391                                 SLOG(LOG_ERROR, TAG_VCM, "[Client Data ERROR] Fail to get the fg command list");
1392                         }
1393                 } else {
1394                         SLOG(LOG_INFO, TAG_VCM, "[Manager] No foreground commands");
1395                 }
1396
1397                 /* Check exclusive option */
1398                 if (true == client_info->exclusive_cmd) {
1399                         SLOG(LOG_INFO, TAG_VCM, "[Manager] Exclusive command is ON");
1400
1401                         /* Set background command for exclusive option */
1402                         if (true == client_info->bg_cmd) {
1403                                 SLOG(LOG_INFO, TAG_VCM, "[Manager] Set background command");
1404                                 ret = vc_cmd_parser_append_commands(client_info->pid, VC_COMMAND_TYPE_BACKGROUND, temp_list);
1405                                 if (0 != ret) {
1406                                         SLOG(LOG_ERROR, TAG_VCM, "[Client Data ERROR] Fail to get the bg command list : pid(%d)", client_info->pid);
1407                                 }
1408                         }
1409
1410                         /* need to release client info */
1411                         iter = g_slist_nth(client_info_list, 0);
1412
1413                         while (NULL != iter) {
1414                                 client_info = iter->data;
1415                                 if (NULL != client_info) {
1416                                         free(client_info);
1417                                 }
1418                                 client_info_list = g_slist_remove_link(client_info_list, iter);
1419                                 iter = g_slist_nth(client_info_list, 0);
1420                         }
1421
1422                         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Foreach current commands DONE");
1423
1424                         return VC_ERROR_NONE;
1425                 }
1426         } else {
1427                 SLOG(LOG_INFO, TAG_VCM, "[Manager] No foreground app");
1428         }
1429
1430         /* Get background commands */
1431         if (0 < g_slist_length(client_info_list)) {
1432                 iter = g_slist_nth(client_info_list, 0);
1433
1434                 while (NULL != iter) {
1435                         client_info = iter->data;
1436
1437                         if (NULL != client_info) {
1438                                 SLOG(LOG_INFO, TAG_VCM, "[Manager] Pid(%d) Back cmd(%d)", client_info->pid, client_info->bg_cmd);
1439                                 if (true == client_info->bg_cmd) {
1440                                         ret = vc_cmd_parser_append_commands(client_info->pid, VC_COMMAND_TYPE_BACKGROUND, temp_list);
1441                                         if (0 != ret) {
1442                                                 SLOG(LOG_ERROR, TAG_VCM, "[Client Data ERROR] Fail to get the bg command list : pid(%d)", client_info->pid);
1443                                         }
1444                                 }
1445                                 free(client_info);
1446                         }
1447                         client_info_list = g_slist_remove_link(client_info_list, iter);
1448
1449                         iter = g_slist_nth(client_info_list, 0);
1450                 }
1451         } else {
1452                 /* NO client */
1453                 SLOG(LOG_INFO, TAG_VCM, "[Manager] No background commands");
1454         }
1455
1456         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Foreach current commands DONE");
1457
1458         // TODO: check return value correct or not
1459         return VC_ERROR_NONE;
1460 }
1461
1462 int vc_mgr_set_recognition_mode(vc_recognition_mode_e mode)
1463 {
1464         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Set recognition mode = %d", mode);
1465
1466         if (0 != __vc_mgr_get_feature_enabled()) {
1467                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1468                 return VC_ERROR_NOT_SUPPORTED;
1469         }
1470
1471         if (0 != __vc_mgr_check_privilege()) {
1472                 return VC_ERROR_PERMISSION_DENIED;
1473         }
1474
1475         /* check vc recognition mode */
1476         if (mode < VC_RECOGNITION_MODE_STOP_BY_SILENCE || mode > VC_RECOGNITION_MODE_MANUAL) {
1477                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] invalid parameter: 'mode' is not supported mode (%d)", mode);
1478                 return VC_ERROR_INVALID_PARAMETER;
1479         }
1480
1481         vc_state_e state;
1482         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1483                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1484                 return VC_ERROR_INVALID_STATE;
1485         }
1486
1487         /* check state */
1488         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1489
1490         /* Check service state */
1491         vc_service_state_e service_state = -1;
1492         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1493         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);
1494
1495         vc_mgr_client_set_recognition_mode(g_vc_m, mode);
1496         return VC_ERROR_NONE;
1497 }
1498
1499 int vc_mgr_get_recognition_mode(vc_recognition_mode_e* mode)
1500 {
1501         if (0 != __vc_mgr_get_feature_enabled()) {
1502                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1503                 return VC_ERROR_NOT_SUPPORTED;
1504         }
1505
1506         if (0 != __vc_mgr_check_privilege()) {
1507                 return VC_ERROR_PERMISSION_DENIED;
1508         }
1509
1510         int ret = -1;
1511
1512         RETVM_IF(NULL == mode, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter, mode is NULL ptr");
1513
1514         vc_state_e state;
1515         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1516                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1517                 return VC_ERROR_INVALID_STATE;
1518         }
1519
1520         ret = vc_mgr_client_get_recognition_mode(g_vc_m, mode);
1521         if (0 != ret) {
1522                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get recognition mode");
1523                 return ret;
1524         }
1525
1526         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Get recognition mode = %d", *mode);
1527         return VC_ERROR_NONE;
1528 }
1529
1530 int vc_mgr_set_private_data(const char* key, const char* data)
1531 {
1532         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Set private data");
1533
1534         if (0 != __vc_mgr_get_feature_enabled()) {
1535                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1536                 return VC_ERROR_NOT_SUPPORTED;
1537         }
1538
1539         if (0 != __vc_mgr_check_privilege()) {
1540                 return VC_ERROR_PERMISSION_DENIED;
1541         }
1542
1543         RETVM_IF(NULL == key, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1544
1545         vc_state_e state;
1546         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1547                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1548                 return VC_ERROR_INVALID_STATE;
1549         }
1550
1551         /* check state */
1552         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1553
1554         /* Check service state */
1555         vc_service_state_e service_state = -1;
1556         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1557         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);
1558
1559         int ret = -1;
1560         ret = vc_mgr_tidl_request_set_private_data(g_vc_m->handle, key, data);
1561         if (0 != ret) {
1562                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set private data : %s", __vc_mgr_get_error_code(ret));
1563                 return ret;
1564         } else {
1565                 SLOG(LOG_DEBUG, TAG_VCM, "[SUCCESS] Set private data");
1566         }
1567
1568         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Set private data DONE");
1569
1570         return VC_ERROR_NONE;
1571 }
1572
1573 int vc_mgr_get_private_data(const char* key, char** data)
1574 {
1575         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get private data");
1576
1577         if (0 != __vc_mgr_get_feature_enabled()) {
1578                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1579                 return VC_ERROR_NOT_SUPPORTED;
1580         }
1581
1582         if (0 != __vc_mgr_check_privilege()) {
1583                 return VC_ERROR_PERMISSION_DENIED;
1584         }
1585
1586         if (NULL == key || NULL == data) {
1587                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid parameter");
1588                 return VC_ERROR_INVALID_PARAMETER;
1589         }
1590
1591         vc_state_e state;
1592         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1593                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1594                 return VC_ERROR_INVALID_STATE;
1595         }
1596
1597         /* check state */
1598         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1599
1600         /* Check service state */
1601         vc_service_state_e service_state = -1;
1602         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1603         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);
1604
1605         int ret = -1;
1606         char* temp = NULL;
1607
1608
1609         ret = vc_mgr_tidl_request_get_private_data(g_vc_m->handle, key, &temp);
1610         if (0 != ret) {
1611                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get private data request : %s", __vc_mgr_get_error_code(ret));
1612         } else {
1613                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Get private data, key(%s), data(%s)", key, temp);
1614         }
1615
1616
1617         if (NULL != temp) {
1618                 *data = strdup(temp);
1619                 free(temp);
1620                 temp = NULL;
1621         }
1622
1623         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get private data DONE");
1624
1625         // TODO: check return value correct or not
1626         return VC_ERROR_NONE;
1627 }
1628
1629 int vc_mgr_set_domain(const char* domain)
1630 {
1631         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Set domain");
1632
1633         if (0 != __vc_mgr_get_feature_enabled()) {
1634                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1635                 return VC_ERROR_NOT_SUPPORTED;
1636         }
1637
1638         if (0 != __vc_mgr_check_privilege()) {
1639                 return VC_ERROR_PERMISSION_DENIED;
1640         }
1641
1642         RETVM_IF(NULL == domain, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1643
1644         vc_state_e state;
1645         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1646                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1647                 return VC_ERROR_INVALID_STATE;
1648         }
1649
1650         /* check state */
1651         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1652
1653         /* Check service state */
1654         vc_service_state_e service_state = -1;
1655         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1656         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);
1657
1658         int ret = -1;
1659
1660         ret = vc_mgr_tidl_request_set_domain(g_vc_m->handle, domain);
1661         if (0 != ret) {
1662                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set domain request : %s", __vc_mgr_get_error_code(ret));
1663         } else {
1664                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set domain");
1665         }
1666
1667         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Set domain DONE");
1668
1669         return VC_ERROR_NONE;
1670 }
1671
1672 int vc_mgr_do_action(vc_send_event_type_e type, char* send_event)
1673 {
1674         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] do action");
1675
1676         if (0 != __vc_mgr_get_feature_enabled()) {
1677                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1678                 return VC_ERROR_NOT_SUPPORTED;
1679         }
1680
1681         if (0 != __vc_mgr_check_privilege()) {
1682                 return VC_ERROR_PERMISSION_DENIED;
1683         }
1684
1685         if (type < VC_SEND_EVENT_TYPE_TEXT || VC_SEND_EVENT_TYPE_HAPTIC_EVENT < type) {
1686                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] send event type is not valid, (%d)", type);
1687                 return VC_ERROR_INVALID_PARAMETER;
1688         }
1689
1690         RETVM_IF(NULL == send_event, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
1691
1692         vc_state_e state;
1693         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1694                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1695                 return VC_ERROR_INVALID_STATE;
1696         }
1697
1698         /* check state */
1699         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1700
1701         /* Check service state */
1702         vc_service_state_e service_state = -1;
1703         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1704         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);
1705
1706         int ret = -1;
1707         ret = vc_mgr_tidl_request_do_action(g_vc_m->handle, type, send_event);
1708         if (0 != ret) {
1709                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to do action request : %s", __vc_mgr_get_error_code(ret));
1710                 return ret;
1711         } else {
1712                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] do action");
1713         }
1714         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] do action DONE");
1715
1716         return VC_ERROR_NONE;
1717 }
1718
1719 int vc_mgr_send_specific_engine_request(const char* engine_app_id, const char* event, const char* request)
1720 {
1721         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] send specific engine request");
1722
1723         if (0 != __vc_mgr_get_feature_enabled()) {
1724                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1725                 return VC_ERROR_NOT_SUPPORTED;
1726         }
1727
1728         if (0 != __vc_mgr_check_privilege()) {
1729                 return VC_ERROR_PERMISSION_DENIED;
1730         }
1731
1732         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] specific engine request. engine app id(%s), event(%s), request(%s)", engine_app_id, event, request);
1733
1734         if (NULL == engine_app_id || NULL == event) {
1735                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid parameter");
1736                 return VC_ERROR_INVALID_PARAMETER;
1737         }
1738
1739         if (NULL == request) {
1740                 SLOG(LOG_ERROR, TAG_VCM, "[INFO] Input parameter is NULL. (no request)");
1741         }
1742
1743         vc_state_e state;
1744         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1745                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1746                 return VC_ERROR_INVALID_STATE;
1747         }
1748
1749         /* check state */
1750         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1751
1752         /* Check service state */
1753         vc_service_state_e service_state = -1;
1754         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1755         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);
1756
1757         int ret = -1;
1758         ret = vc_mgr_tidl_send_specific_engine_request(g_vc_m->handle, engine_app_id, event, request);
1759         if (0 != ret) {
1760                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to specific engine request : %s", __vc_mgr_get_error_code(ret));
1761                 return ret;
1762         } else {
1763                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] specific engine request");
1764         }
1765         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] send specific engine request DONE");
1766
1767         return VC_ERROR_NONE;
1768 }
1769
1770 int vc_mgr_start(bool exclusive_command_option)
1771 {
1772         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Request start");
1773
1774         if (0 != __vc_mgr_get_feature_enabled()) {
1775                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1776                 return VC_ERROR_NOT_SUPPORTED;
1777         }
1778
1779         if (0 != __vc_mgr_check_privilege()) {
1780                 return VC_ERROR_PERMISSION_DENIED;
1781         }
1782
1783         vc_state_e state;
1784         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1785                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1786                 return VC_ERROR_INVALID_STATE;
1787         }
1788
1789         /* check state */
1790         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1791
1792         /* Check service state */
1793         vc_service_state_e service_state = -1;
1794         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1795         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);
1796
1797         /* Check internal state for async */
1798         vc_internal_state_e internal_state = -1;
1799         vc_mgr_client_get_internal_state(g_vc_m, &internal_state);
1800         if (internal_state != VC_INTERNAL_STATE_NONE) {
1801                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is NOT none : %d", internal_state);
1802                 return VC_ERROR_IN_PROGRESS_TO_RECORDING;
1803         }
1804
1805         vc_mgr_client_set_exclusive_command(g_vc_m, exclusive_command_option);
1806
1807         bool start_by_client = false;
1808         if (0 != vc_mgr_client_get_start_by_client(g_vc_m, &start_by_client)) {
1809                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get start by client");
1810         }
1811
1812         int disabled_cmd_type = 0;
1813         if (0 != vc_mgr_client_get_disabled_command_type(g_vc_m, &disabled_cmd_type)) {
1814                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get disabled command type");
1815         }
1816
1817         int ret;
1818         vc_recognition_mode_e recognition_mode = VC_RECOGNITION_MODE_STOP_BY_SILENCE;
1819         if (0 != vc_mgr_get_recognition_mode(&recognition_mode)) {
1820                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get recognition mode");
1821         }
1822
1823         /* Request */
1824         ret = -1;
1825         vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_STARTING);
1826         ret = vc_mgr_tidl_request_start(g_vc_m->handle, (int)recognition_mode, exclusive_command_option, start_by_client, disabled_cmd_type);
1827         if (0 != ret) {
1828                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to start request start : %s", __vc_mgr_get_error_code(ret));
1829                 vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_NONE);
1830         } else {
1831                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] start recognition");
1832         }
1833
1834         g_volume_db = 0;
1835         g_prev_volume_db = 0;
1836         g_cur_volume_db = 0;
1837
1838         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Request start DONE, ret(%d)", ret);
1839
1840         return ret;
1841 }
1842
1843 int vc_mgr_stop(void)
1844 {
1845         SLOG(LOG_ERROR, TAG_VCM, "@@@ [Manager] Request stop");
1846
1847         if (0 != __vc_mgr_get_feature_enabled()) {
1848                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1849                 return VC_ERROR_NOT_SUPPORTED;
1850         }
1851
1852         if (0 != __vc_mgr_check_privilege()) {
1853                 return VC_ERROR_PERMISSION_DENIED;
1854         }
1855
1856         vc_state_e state;
1857         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1858                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1859                 return VC_ERROR_INVALID_STATE;
1860         }
1861
1862         /* check state */
1863         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1864
1865         /* Check service state */
1866         vc_service_state_e service_state = -1;
1867         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1868         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);
1869
1870         /* Check internal state for async */
1871         vc_internal_state_e internal_state = -1;
1872         vc_mgr_client_get_internal_state(g_vc_m, &internal_state);
1873         if (VC_INTERNAL_STATE_STARTING == internal_state) {
1874                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is STARTING");
1875                 return VC_ERROR_IN_PROGRESS_TO_RECORDING;
1876         } else if (VC_INTERNAL_STATE_STOPPING == internal_state) {
1877                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is STOPPING");
1878                 return VC_ERROR_IN_PROGRESS_TO_PROCESSING;
1879         } else if (VC_INTERNAL_STATE_CANCELING == internal_state) {
1880                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is CANCELING");
1881                 return VC_ERROR_IN_PROGRESS_TO_READY;
1882         }
1883
1884         int ret = -1;
1885         /* do request */
1886         vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_STOPPING);
1887         ret = vc_mgr_tidl_request_stop(g_vc_m->handle);
1888         if (0 != ret) {
1889                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to stop request : %s", __vc_mgr_get_error_code(ret));
1890                 vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_NONE);
1891         } else {
1892                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Stop recognition");
1893         }
1894
1895         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Request stop DONE");
1896
1897         return ret;
1898 }
1899
1900 int vc_mgr_cancel(void)
1901 {
1902         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Request cancel");
1903
1904         if (0 != __vc_mgr_get_feature_enabled()) {
1905                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
1906                 return VC_ERROR_NOT_SUPPORTED;
1907         }
1908
1909         if (0 != __vc_mgr_check_privilege()) {
1910                 return VC_ERROR_PERMISSION_DENIED;
1911         }
1912
1913         vc_state_e state;
1914         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
1915                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
1916                 return VC_ERROR_INVALID_STATE;
1917         }
1918
1919         /* check state */
1920         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'READY'", state);
1921
1922         /* Check service state */
1923         vc_service_state_e service_state = -1;
1924         vc_mgr_client_get_service_state(g_vc_m, &service_state);
1925         RETVM_IF(service_state == VC_SERVICE_STATE_NONE, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state is NONE");
1926
1927         vc_internal_state_e internal_state = -1;
1928         vc_mgr_client_get_internal_state(g_vc_m, &internal_state);
1929         if (VC_INTERNAL_STATE_STARTING == internal_state) {
1930                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is STARTING");
1931                 return VC_ERROR_IN_PROGRESS_TO_RECORDING;
1932         } else if (VC_INTERNAL_STATE_STOPPING == internal_state) {
1933                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is STOPPING");
1934                 return VC_ERROR_IN_PROGRESS_TO_PROCESSING;
1935         } else if (VC_INTERNAL_STATE_CANCELING == internal_state) {
1936                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State : Internal state is CANCELING");
1937                 return VC_ERROR_IN_PROGRESS_TO_READY;
1938         }
1939
1940         int ret = -1;
1941         vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_CANCELING);
1942         ret = vc_mgr_tidl_request_cancel(g_vc_m->handle);
1943         if (0 != ret) {
1944                 SLOG(LOG_DEBUG, TAG_VCM, "[ERROR] Fail to cancel request : %s", __vc_mgr_get_error_code(ret));
1945                 vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_NONE);
1946         } else {
1947                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Cancel recognition");
1948         }
1949
1950         vc_mgr_client_set_exclusive_command(g_vc_m, false);
1951
1952         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Request cancel DONE");
1953
1954         return ret;
1955 }
1956
1957 static int g_cnt = 0;
1958 static Eina_Bool __vc_mgr_set_volume_timer_cb(void* data)
1959 {
1960         g_cnt++;
1961         g_volume_db = g_prev_volume_db + (g_cur_volume_db - g_prev_volume_db) / 5 * g_cnt;
1962
1963         SLOG(LOG_INFO, TAG_VCM, "Set volume (%f)(%f)", g_volume_db, g_cur_volume_db);
1964
1965         if (0 == g_cnt % 5) {
1966                 return EINA_FALSE;
1967         }
1968         return EINA_TRUE;
1969 }
1970
1971 int __vc_mgr_cb_set_volume(float volume)
1972 {
1973         // called in vc_mgr_tidl
1974         g_prev_volume_db = g_volume_db;
1975         g_cur_volume_db = volume;
1976
1977         g_volume_db = g_prev_volume_db + (g_cur_volume_db - g_prev_volume_db) / 5;
1978
1979         if (NULL != g_m_set_volume_timer) {
1980                 ecore_timer_del(g_m_set_volume_timer);
1981         }
1982
1983         g_cnt = 1;
1984         g_m_set_volume_timer = ecore_timer_add(0.05, __vc_mgr_set_volume_timer_cb, NULL);
1985
1986         return VC_ERROR_NONE;
1987 }
1988
1989 int __vc_mgr_set_volume(float volume)
1990 {
1991         // called by app directly
1992         g_volume_db = volume;
1993
1994         return VC_ERROR_NONE;
1995 }
1996
1997 int vc_mgr_get_recording_volume(float* volume)
1998 {
1999         if (0 != __vc_mgr_get_feature_enabled()) {
2000                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2001                 return VC_ERROR_NOT_SUPPORTED;
2002         }
2003
2004         if (0 != __vc_mgr_check_privilege()) {
2005                 return VC_ERROR_PERMISSION_DENIED;
2006         }
2007
2008         RETVM_IF(NULL == volume, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2009
2010         vc_service_state_e service_state = -1;
2011         if (0 != vc_mgr_client_get_service_state(g_vc_m, &service_state)) {
2012                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
2013                 return VC_ERROR_INVALID_STATE;
2014         }
2015
2016         /* check state */
2017         RETVM_IF(VC_SERVICE_STATE_RECORDING != service_state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Service state is not 'RECORDING'");
2018
2019         *volume = g_volume_db;
2020
2021         return VC_ERROR_NONE;
2022 }
2023
2024 int __vc_mgr_cb_set_foreground(int pid, bool value)
2025 {
2026         vc_mgr_client_set_foreground(g_vc_m, pid, value);
2027
2028         /* get authorized valid app */
2029         int tmp_pid;
2030         if (0 != vc_mgr_client_get_valid_authorized_client(g_vc_m, &tmp_pid)) {
2031                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get authorized valid app");
2032                 return VC_ERROR_INVALID_PARAMETER;
2033         }
2034
2035         if (true == value) {
2036                 /* compare & set valid */
2037                 if (tmp_pid != pid) {
2038                         SLOG(LOG_INFO, TAG_VCM, "Authority(%d) changed to invalid", tmp_pid);
2039
2040                         /* set authorized valid */
2041                         if (true == vc_mgr_client_is_authorized_client(g_vc_m, pid)) {
2042                                 SLOG(LOG_DEBUG, TAG_VCM, "Authority(%d) change to valid", pid);
2043                                 vc_mgr_client_set_valid_authorized_client(g_vc_m, pid);
2044                         } else {
2045                                 SLOG(LOG_DEBUG, TAG_VCM, "No valid Authority");
2046                                 vc_mgr_client_set_valid_authorized_client(g_vc_m, -1);
2047                         }
2048                 }
2049         } else {
2050                 if (tmp_pid == pid) {
2051                         SLOG(LOG_INFO, TAG_VCM, "Authority(%d) changed to invalid", tmp_pid);
2052                         vc_mgr_client_set_valid_authorized_client(g_vc_m, -1);
2053                 }
2054         }
2055
2056         return VC_ERROR_NONE;
2057 }
2058
2059 int vc_mgr_set_selected_results(vc_cmd_list_h vc_cmd_list)
2060 {
2061         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Select result");
2062
2063         if (0 != __vc_mgr_get_feature_enabled()) {
2064                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2065                 return VC_ERROR_NOT_SUPPORTED;
2066         }
2067
2068         if (0 != __vc_mgr_check_privilege()) {
2069                 return VC_ERROR_PERMISSION_DENIED;
2070         }
2071
2072         /* Do not check state for 'restart continuously' mode */
2073
2074         vc_service_state_e service_state = -1;
2075         vc_mgr_client_get_service_state(g_vc_m, &service_state);
2076         if (service_state != VC_SERVICE_STATE_PROCESSING) {
2077                 vc_recognition_mode_e recognition_mode;
2078                 vc_mgr_get_recognition_mode(&recognition_mode);
2079
2080                 if (VC_RECOGNITION_MODE_RESTART_CONTINUOUSLY != recognition_mode) {
2081                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: service state is not 'PROCESSING' and mode is not 'Restart continuously'");
2082                         return VC_ERROR_INVALID_STATE;
2083                 }
2084         }
2085
2086         if (NULL != vc_cmd_list) {
2087                 int event = 0;
2088                 char* result_text = NULL;
2089
2090                 vc_mgr_client_get_all_result(g_vc_m, &event, &result_text);
2091
2092                 vc_info_parser_set_result(result_text, event, NULL, vc_cmd_list, false);
2093
2094                 if (NULL != result_text) {
2095                         free(result_text);
2096                         result_text = NULL;
2097                 }
2098         }
2099
2100         /* Request */
2101         int ret = -1;
2102         ret = vc_mgr_tidl_send_result_selection(g_vc_m->handle);
2103         if (0 != ret) {
2104                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to send result selection : %s", __vc_mgr_get_error_code(ret));
2105                 return ret;
2106         } else {
2107                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] result selection");
2108         }
2109
2110         vc_mgr_client_unset_all_result(g_vc_m);
2111
2112         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Select result DONE");
2113
2114         return VC_ERROR_NONE;
2115 }
2116
2117 static Eina_Bool __vc_mgr_set_select_result(void *data)
2118 {
2119         vc_mgr_set_selected_results(NULL);
2120         return EINA_FALSE;
2121 }
2122
2123 static void __vc_mgr_notify_all_result(vc_result_type_e result_type)
2124 {
2125         char* temp_text = NULL;
2126         int event;
2127         char* temp_message = NULL;
2128         vc_cmd_list_h vc_cmd_list = NULL;
2129
2130         vc_mgr_all_result_cb all_callback = NULL;
2131         void* all_user_data = NULL;
2132
2133         vc_mgr_client_get_all_result_cb(g_vc_m, &all_callback, &all_user_data);
2134         RETM_IF(NULL == all_callback, TAG_VCM, "[ERROR] All result callback is NULL");
2135
2136         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
2137                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create command list");
2138                 return;
2139         }
2140
2141         vc_info_parser_get_result(&temp_text, &event, &temp_message, -1, vc_cmd_list, vc_mgr_client_get_exclusive_command(g_vc_m));
2142
2143         SECURE_SLOG(LOG_INFO, TAG_VCM, "Result info : result type(%d) result text(%s) event(%d) result_message(%s)",
2144                 result_type, temp_text, event, temp_message);
2145
2146         vc_cmd_print_list(vc_cmd_list);
2147
2148         bool cb_ret = false;
2149
2150         vc_mgr_client_set_all_result(g_vc_m, event, temp_text);
2151
2152         vc_mgr_client_use_callback(g_vc_m);
2153         cb_ret = all_callback(event, vc_cmd_list, temp_text, temp_message, all_user_data);
2154         vc_mgr_client_not_use_callback(g_vc_m);
2155
2156         if (true == vc_mgr_client_get_exclusive_command(g_vc_m)) {
2157                 /* exclusive */
2158                 vc_result_cb callback = NULL;
2159                 void* user_data = NULL;
2160
2161                 vc_mgr_client_get_result_cb(g_vc_m, &callback, &user_data);
2162                 RETM_IF(NULL == callback, TAG_VCM, "[ERROR] Client result callback is NULL");
2163
2164                 vc_mgr_client_use_callback(g_vc_m);
2165                 callback(event, vc_cmd_list, temp_text, user_data);
2166                 vc_mgr_client_not_use_callback(g_vc_m);
2167                 SLOG(LOG_DEBUG, TAG_VCM, "Exclusive result callback called");
2168
2169                 /* Release result */
2170                 if (NULL != temp_text) {
2171                         free(temp_text);
2172                         temp_text = NULL;
2173                 }
2174                 if (NULL != temp_message) {
2175                         free(temp_message);
2176                         temp_message = NULL;
2177                 }
2178
2179                 /* Release list */
2180                 if (vc_cmd_list)
2181                         vc_cmd_list_destroy(vc_cmd_list, true);
2182                 vc_cmd_list = NULL;
2183
2184                 vc_mgr_client_set_exclusive_command(g_vc_m, false);
2185
2186                 return;
2187         }
2188
2189         int count = 0;
2190         vc_cmd_list_get_count(vc_cmd_list, &count);
2191         if (0 < count) {
2192                 if (true == cb_ret) {
2193                         SLOG(LOG_INFO, TAG_VCM, "Callback result is true");
2194                         if (VC_RESULT_TYPE_NOTIFICATION != result_type)
2195                                 ecore_idler_add(__vc_mgr_set_select_result, NULL);
2196                 } else {
2197                         SLOG(LOG_INFO, TAG_VCM, "Callback result is false");
2198                         /* need to select conflicted result */
2199                 }
2200         } else {
2201                 if (VC_RESULT_TYPE_NOTIFICATION != result_type)
2202                         ecore_idler_add(__vc_mgr_set_select_result, NULL);
2203
2204                 vc_mgr_client_set_exclusive_command(g_vc_m, false);
2205                 vc_mgr_client_unset_all_result(g_vc_m);
2206         }
2207
2208         /* Release result */
2209         if (NULL != temp_text) {
2210                 free(temp_text);
2211                 temp_text = NULL;
2212         }
2213         if (NULL != temp_message) {
2214                 free(temp_message);
2215                 temp_message = NULL;
2216         }
2217
2218         /* Release list */
2219         if (vc_cmd_list)
2220                 vc_cmd_list_destroy(vc_cmd_list, true);
2221         vc_cmd_list = NULL;
2222
2223         return;
2224 }
2225
2226 static Eina_Bool __vc_mgr_notify_result(void *data)
2227 {
2228         char* temp_text = NULL;
2229         int event;
2230         vc_cmd_list_h vc_cmd_list = NULL;
2231
2232         vc_result_cb callback = NULL;
2233         void* user_data = NULL;
2234
2235         vc_mgr_client_get_result_cb(g_vc_m, &callback, &user_data);
2236         RETVM_IF(NULL == callback, EINA_FALSE, TAG_VCM, "[ERROR] Client result callback is NULL");
2237
2238         if (0 != vc_cmd_list_create(&vc_cmd_list)) {
2239                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create command list");
2240                 return EINA_FALSE;
2241         }
2242
2243         vc_info_parser_get_result(&temp_text, &event, NULL, getpid(), vc_cmd_list, false);
2244
2245         SECURE_SLOG(LOG_INFO, TAG_VCM, "Result : result text(%s) event(%d)", temp_text, event);
2246
2247         vc_cmd_print_list(vc_cmd_list);
2248
2249         vc_mgr_client_use_callback(g_vc_m);
2250         callback(event, vc_cmd_list, temp_text, user_data);
2251         vc_mgr_client_not_use_callback(g_vc_m);
2252         SLOG(LOG_INFO, TAG_VCM, "Result callback called");
2253
2254         if (vc_cmd_list)
2255                 vc_cmd_list_destroy(vc_cmd_list, true);
2256         vc_cmd_list = NULL;
2257
2258         /* Release result */
2259         if (temp_text)
2260                 free(temp_text);
2261         temp_text = NULL;
2262
2263         return EINA_FALSE;
2264 }
2265
2266 void __vc_mgr_cb_all_result(vc_result_type_e type)
2267 {
2268         if (false == vc_mgr_client_get_exclusive_command(g_vc_m)) {
2269                 __vc_mgr_notify_all_result(type);
2270         } else {
2271                 __vc_mgr_notify_result(0);
2272         }
2273
2274         return;
2275 }
2276
2277 void __vc_mgr_cb_pre_result(vc_pre_result_event_e event, const char* pre_result)
2278 {
2279         vc_mgr_pre_result_cb callback = NULL;
2280         void* user_data = NULL;
2281
2282         vc_mgr_client_get_pre_result_cb(g_vc_m, &callback, &user_data);
2283         RETM_IF(NULL == callback, TAG_VCM, "[ERROR] Client pre result callback is NULL");
2284
2285         vc_mgr_client_use_callback(g_vc_m);
2286         callback(event, pre_result, user_data);
2287         vc_mgr_client_not_use_callback(g_vc_m);
2288         SLOG(LOG_INFO, TAG_VCM, "Pre result callback is called");
2289
2290         return;
2291 }
2292
2293 void __vc_mgr_cb_system_result()
2294 {
2295         __vc_mgr_notify_result(NULL);
2296         return;
2297 }
2298
2299 static Eina_Bool __vc_mgr_speech_detected(void *data)
2300 {
2301         vc_mgr_begin_speech_detected_cb callback = NULL;
2302         void* user_data = NULL;
2303
2304         vc_mgr_client_get_speech_detected_cb(g_vc_m, &callback, &user_data);
2305         RETVM_IF(NULL == callback, EINA_FALSE, TAG_VCM, "[ERROR] Client speech detected callback is NULL");
2306
2307         vc_mgr_client_use_callback(g_vc_m);
2308         callback(user_data);
2309         vc_mgr_client_not_use_callback(g_vc_m);
2310         SLOG(LOG_INFO, TAG_VCM, "Speech detected callback called");
2311
2312         return EINA_FALSE;
2313 }
2314
2315 void __vc_mgr_cb_speech_detected()
2316 {
2317         __vc_mgr_speech_detected(NULL);
2318
2319         return;
2320 }
2321
2322 int vc_mgr_set_all_result_cb(vc_mgr_all_result_cb callback, void* user_data)
2323 {
2324         if (0 != __vc_mgr_get_feature_enabled()) {
2325                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2326                 return VC_ERROR_NOT_SUPPORTED;
2327         }
2328
2329         if (0 != __vc_mgr_check_privilege()) {
2330                 return VC_ERROR_PERMISSION_DENIED;
2331         }
2332
2333         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2334
2335         vc_state_e state;
2336         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2337                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set result callback : A handle is not available");
2338                 return VC_ERROR_INVALID_STATE;
2339         }
2340
2341         /* check state */
2342         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2343
2344         vc_mgr_client_set_all_result_cb(g_vc_m, callback, user_data);
2345
2346         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set all result callback");
2347
2348         return VC_ERROR_NONE;
2349 }
2350
2351 int vc_mgr_unset_all_result_cb(void)
2352 {
2353         if (0 != __vc_mgr_get_feature_enabled()) {
2354                 SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] not supported");
2355                 return VC_ERROR_NOT_SUPPORTED;
2356         }
2357
2358         if (0 != __vc_mgr_check_privilege()) {
2359                 return VC_ERROR_PERMISSION_DENIED;
2360         }
2361
2362         vc_state_e state;
2363         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2364                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset result callback : A handle is not available");
2365                 return VC_ERROR_INVALID_STATE;
2366         }
2367
2368         /* check state */
2369         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2370
2371         vc_mgr_client_set_all_result_cb(g_vc_m, NULL, NULL);
2372
2373         return VC_ERROR_NONE;
2374 }
2375
2376 int vc_mgr_set_result_cb(vc_result_cb callback, void* user_data)
2377 {
2378         if (0 != __vc_mgr_get_feature_enabled()) {
2379                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2380                 return VC_ERROR_NOT_SUPPORTED;
2381         }
2382
2383         if (0 != __vc_mgr_check_privilege()) {
2384                 return VC_ERROR_PERMISSION_DENIED;
2385         }
2386
2387         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2388
2389         vc_state_e state;
2390         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2391                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set result callback : A handle is not available");
2392                 return VC_ERROR_INVALID_STATE;
2393         }
2394
2395         /* check state */
2396         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2397
2398         vc_mgr_client_set_result_cb(g_vc_m, callback, user_data);
2399
2400         return VC_ERROR_NONE;
2401 }
2402
2403 int vc_mgr_unset_result_cb(void)
2404 {
2405         if (0 != __vc_mgr_get_feature_enabled()) {
2406                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2407                 return VC_ERROR_NOT_SUPPORTED;
2408         }
2409
2410         if (0 != __vc_mgr_check_privilege()) {
2411                 return VC_ERROR_PERMISSION_DENIED;
2412         }
2413
2414         vc_state_e state;
2415         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2416                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset result callback : A handle is not available");
2417                 return VC_ERROR_INVALID_STATE;
2418         }
2419
2420         /* check state */
2421         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2422
2423         vc_mgr_client_set_result_cb(g_vc_m, NULL, NULL);
2424
2425         return VC_ERROR_NONE;
2426 }
2427
2428 int vc_mgr_set_pre_result_cb(vc_mgr_pre_result_cb callback, void* user_data)
2429 {
2430         if (0 != __vc_mgr_get_feature_enabled()) {
2431                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2432                 return VC_ERROR_NOT_SUPPORTED;
2433         }
2434
2435         if (0 != __vc_mgr_check_privilege()) {
2436                 return VC_ERROR_PERMISSION_DENIED;
2437         }
2438
2439         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2440
2441         vc_state_e state;
2442         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2443                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set pre result callback : A handle is not available");
2444                 return VC_ERROR_INVALID_STATE;
2445         }
2446
2447         /* check state */
2448         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2449
2450         vc_mgr_client_set_pre_result_cb(g_vc_m, callback, user_data);
2451
2452         return VC_ERROR_NONE;
2453 }
2454
2455 int vc_mgr_unset_pre_result_cb(void)
2456 {
2457         if (0 != __vc_mgr_get_feature_enabled()) {
2458                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2459                 return VC_ERROR_NOT_SUPPORTED;
2460         }
2461
2462         if (0 != __vc_mgr_check_privilege()) {
2463                 return VC_ERROR_PERMISSION_DENIED;
2464         }
2465
2466         vc_state_e state;
2467         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2468                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset pre result callback : A handle is not available");
2469                 return VC_ERROR_INVALID_STATE;
2470         }
2471
2472         /* check state */
2473         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2474
2475         vc_mgr_client_set_pre_result_cb(g_vc_m, NULL, NULL);
2476
2477         return VC_ERROR_NONE;
2478 }
2479
2480 int vc_mgr_get_error_message(char** err_msg)
2481 {
2482         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get error message");
2483
2484         if (0 != __vc_mgr_get_feature_enabled()) {
2485                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2486                 return VC_ERROR_NOT_SUPPORTED;
2487         }
2488
2489         if (0 != __vc_mgr_check_privilege()) {
2490                 return VC_ERROR_PERMISSION_DENIED;
2491         }
2492
2493         /* check state */
2494         vc_state_e state;
2495         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2496                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] vc_mgr handle is not available");
2497                 return VC_ERROR_INVALID_STATE;
2498         }
2499
2500         RETVM_IF(NULL == err_msg, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2501
2502         if (false == g_err_callback_status) {
2503                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Not in error callback");
2504                 return VC_ERROR_OPERATION_FAILED;
2505         }
2506
2507         int ret;
2508         ret = vc_mgr_client_get_error_message(g_vc_m, err_msg);
2509         if (0 != ret) {
2510                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get error message");
2511         }
2512
2513         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] Get error message DONE");
2514
2515         return ret;
2516 }
2517
2518 static void __vc_mgr_notify_error(void *data)
2519 {
2520         vc_h vc_m = (vc_h)data;
2521
2522         vc_error_cb callback = NULL;
2523         void* user_data = NULL;
2524         int reason;
2525
2526         vc_mgr_client_get_error_cb(vc_m, &callback, &user_data);
2527         vc_mgr_client_get_error(vc_m, &reason);
2528
2529         if (NULL != callback) {
2530                 vc_mgr_client_use_callback(vc_m);
2531                 g_err_callback_status = true;
2532                 callback(reason, user_data);
2533                 g_err_callback_status = false;
2534                 vc_mgr_client_not_use_callback(vc_m);
2535                 SLOG(LOG_INFO, TAG_VCM, "Error callback is called");
2536         } else {
2537                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Error callback is null");
2538         }
2539 }
2540
2541 int __vc_mgr_cb_error(int reason, int daemon_pid, char* msg)
2542 {
2543         vc_state_e state;
2544         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2545                 SLOG(LOG_ERROR, TAG_VCM, "[WARNING] Invalid client");
2546                 return VC_ERROR_INVALID_PARAMETER;
2547         }
2548
2549         /* check state */
2550         if (state != VC_STATE_INITIALIZED && state != VC_STATE_READY) {
2551                 SLOG(LOG_ERROR, TAG_VCM, "[WARNING] not connected client yet");
2552                 return VC_ERROR_INVALID_STATE;
2553         }
2554
2555         vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_NONE);
2556
2557         if (VC_ERROR_SERVICE_RESET == reason) {
2558                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] VC daemon reset");
2559
2560                 vc_service_state_e service_state = -1;
2561                 vc_mgr_client_get_service_state(g_vc_m, &service_state);
2562                 if (VC_SERVICE_STATE_UPDATING == service_state) {
2563                         SLOG(LOG_INFO, TAG_VCM, "[INFO] VC daemon is terminated by update manager");
2564                         return VC_ERROR_NONE;
2565                 }
2566
2567                 vc_mgr_client_set_client_state(g_vc_m, VC_STATE_INITIALIZED);
2568                 __vc_mgr_notify_state_changed(g_vc_m);
2569
2570                 if (0 != vc_mgr_prepare()) {
2571                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to prepare");
2572                 }
2573         }
2574
2575         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Error reason(%d), msg(%s)", reason, msg);
2576
2577         vc_mgr_client_set_error(g_vc_m, reason);
2578         vc_mgr_client_set_error_message(g_vc_m, msg);
2579         __vc_mgr_notify_error(g_vc_m);
2580
2581         return VC_ERROR_NONE;
2582 }
2583
2584 static Eina_Bool __vc_mgr_notify_state_changed(void *data)
2585 {
2586         vc_state_changed_cb changed_callback = NULL;
2587         void* user_data;
2588
2589         vc_mgr_client_get_state_changed_cb(g_vc_m, &changed_callback, &user_data);
2590
2591         vc_state_e current_state;
2592         vc_state_e before_state;
2593
2594         vc_mgr_client_get_before_state(g_vc_m, &current_state, &before_state);
2595
2596         if (NULL != changed_callback) {
2597                 vc_mgr_client_use_callback(g_vc_m);
2598                 changed_callback(before_state, current_state, user_data);
2599                 vc_mgr_client_not_use_callback(g_vc_m);
2600                 SLOG(LOG_INFO, TAG_VCM, "State changed callback is called");
2601         } else {
2602                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] State changed callback is null");
2603         }
2604
2605         return EINA_FALSE;
2606 }
2607
2608 int vc_mgr_set_state_changed_cb(vc_state_changed_cb callback, void* user_data)
2609 {
2610         if (0 != __vc_mgr_get_feature_enabled()) {
2611                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2612                 return VC_ERROR_NOT_SUPPORTED;
2613         }
2614
2615         if (0 != __vc_mgr_check_privilege()) {
2616                 return VC_ERROR_PERMISSION_DENIED;
2617         }
2618
2619         if (callback == NULL)
2620                 return VC_ERROR_INVALID_PARAMETER;
2621
2622         vc_state_e state;
2623         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2624                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set state changed callback : A handle is not available");
2625                 return VC_ERROR_INVALID_STATE;
2626         }
2627
2628         /* check state */
2629         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2630
2631         vc_mgr_client_set_state_changed_cb(g_vc_m, callback, user_data);
2632
2633         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set state changed callback");
2634         return VC_ERROR_NONE;
2635 }
2636
2637 int vc_mgr_unset_state_changed_cb(void)
2638 {
2639         if (0 != __vc_mgr_get_feature_enabled()) {
2640                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2641                 return VC_ERROR_NOT_SUPPORTED;
2642         }
2643
2644         if (0 != __vc_mgr_check_privilege()) {
2645                 return VC_ERROR_PERMISSION_DENIED;
2646         }
2647
2648         vc_state_e state;
2649         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2650                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset state changed callback : A handle is not available");
2651                 return VC_ERROR_INVALID_STATE;
2652         }
2653
2654         /* check state */
2655         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2656
2657         vc_mgr_client_set_state_changed_cb(g_vc_m, NULL, NULL);
2658
2659         return VC_ERROR_NONE;
2660 }
2661
2662 int __vc_mgr_cb_service_state(int state)
2663 {
2664         vc_service_state_e current_state = (vc_service_state_e)state;
2665         vc_service_state_e before_state;
2666         vc_mgr_client_get_service_state(g_vc_m, &before_state);
2667
2668         SLOG(LOG_INFO, TAG_VCM, "Service State changed : Before(%d) Current(%d)",
2669                 before_state, current_state);
2670
2671         vc_internal_state_e internal_state = -1;
2672         vc_mgr_client_get_internal_state(g_vc_m, &internal_state);
2673         if ((VC_INTERNAL_STATE_STARTING == internal_state && VC_SERVICE_STATE_RECORDING == current_state) ||
2674                 (VC_INTERNAL_STATE_STOPPING == internal_state && VC_SERVICE_STATE_PROCESSING == current_state) ||
2675                 (VC_INTERNAL_STATE_CANCELING == internal_state && VC_SERVICE_STATE_READY == current_state)) {
2676                         SLOG(LOG_INFO, TAG_VCM, "Internal state is changed to NONE");
2677                         vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_NONE);
2678         }
2679         if (VC_SERVICE_STATE_UPDATING == current_state) {
2680                 SLOG(LOG_INFO, TAG_VCM, "Internal state is changed to NONE by updating");
2681                 vc_mgr_client_set_internal_state(g_vc_m, VC_INTERNAL_STATE_NONE);
2682         }
2683
2684         if (current_state == before_state) {
2685                 SLOG(LOG_WARN, TAG_VCM, "Service State NOT changed : Before(%d) Current(%d)",
2686                         before_state, current_state);
2687                 return VC_ERROR_NONE;
2688         }
2689
2690         /* Save service state */
2691         vc_mgr_client_set_service_state(g_vc_m, current_state);
2692
2693         vc_service_state_changed_cb callback = NULL;
2694         void* service_user_data = NULL;
2695         vc_mgr_client_get_service_state_changed_cb(g_vc_m, &callback, &service_user_data);
2696
2697         if (NULL != callback) {
2698                 vc_mgr_client_use_callback(g_vc_m);
2699                 callback(before_state, current_state, service_user_data);
2700                 vc_mgr_client_not_use_callback(g_vc_m);
2701                 SLOG(LOG_INFO, TAG_VCM, "Service state changed callback is called");
2702         } else {
2703                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Service state changed callback is null");
2704         }
2705
2706         return VC_ERROR_NONE;
2707 }
2708
2709 int vc_mgr_set_service_state_changed_cb(vc_service_state_changed_cb callback, void* user_data)
2710 {
2711         if (0 != __vc_mgr_get_feature_enabled()) {
2712                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2713                 return VC_ERROR_NOT_SUPPORTED;
2714         }
2715
2716         if (0 != __vc_mgr_check_privilege()) {
2717                 return VC_ERROR_PERMISSION_DENIED;
2718         }
2719
2720         if (callback == NULL)
2721                 return VC_ERROR_INVALID_PARAMETER;
2722
2723         vc_state_e state;
2724         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2725                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set state changed callback : A handle is not available");
2726                 return VC_ERROR_INVALID_STATE;
2727         }
2728
2729         /* check state */
2730         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2731
2732         vc_mgr_client_set_service_state_changed_cb(g_vc_m, callback, user_data);
2733
2734         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set service state changed callback");
2735         return VC_ERROR_NONE;
2736 }
2737
2738 int vc_mgr_unset_service_state_changed_cb(void)
2739 {
2740         if (0 != __vc_mgr_get_feature_enabled()) {
2741                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2742                 return VC_ERROR_NOT_SUPPORTED;
2743         }
2744
2745         if (0 != __vc_mgr_check_privilege()) {
2746                 return VC_ERROR_PERMISSION_DENIED;
2747         }
2748
2749         vc_state_e state;
2750         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2751                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset state changed callback : A handle is not available");
2752                 return VC_ERROR_INVALID_STATE;
2753         }
2754
2755         /* check state */
2756         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2757
2758         vc_mgr_client_set_service_state_changed_cb(g_vc_m, NULL, NULL);
2759         return VC_ERROR_NONE;
2760 }
2761
2762 int vc_mgr_set_speech_detected_cb(vc_mgr_begin_speech_detected_cb callback, void* user_data)
2763 {
2764         if (0 != __vc_mgr_get_feature_enabled()) {
2765                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2766                 return VC_ERROR_NOT_SUPPORTED;
2767         }
2768
2769         if (0 != __vc_mgr_check_privilege()) {
2770                 return VC_ERROR_PERMISSION_DENIED;
2771         }
2772
2773         if (callback == NULL)
2774                 return VC_ERROR_INVALID_PARAMETER;
2775
2776         vc_state_e state;
2777         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2778                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set state changed callback : A handle is not available");
2779                 return VC_ERROR_INVALID_STATE;
2780         }
2781
2782         /* check state */
2783         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2784
2785         vc_mgr_client_set_speech_detected_cb(g_vc_m, callback, user_data);
2786
2787         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set speech detected callback");
2788         return VC_ERROR_NONE;
2789 }
2790
2791 int vc_mgr_unset_speech_detected_cb(void)
2792 {
2793         if (0 != __vc_mgr_get_feature_enabled()) {
2794                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2795                 return VC_ERROR_NOT_SUPPORTED;
2796         }
2797
2798         if (0 != __vc_mgr_check_privilege()) {
2799                 return VC_ERROR_PERMISSION_DENIED;
2800         }
2801
2802         vc_state_e state;
2803         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2804                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset state changed callback : A handle is not available");
2805                 return VC_ERROR_INVALID_STATE;
2806         }
2807
2808         /* check state */
2809         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2810
2811         vc_mgr_client_set_speech_detected_cb(g_vc_m, NULL, NULL);
2812         return VC_ERROR_NONE;
2813 }
2814
2815 int vc_mgr_set_current_language_changed_cb(vc_current_language_changed_cb callback, void* user_data)
2816 {
2817         if (0 != __vc_mgr_get_feature_enabled()) {
2818                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2819                 return VC_ERROR_NOT_SUPPORTED;
2820         }
2821
2822         if (0 != __vc_mgr_check_privilege()) {
2823                 return VC_ERROR_PERMISSION_DENIED;
2824         }
2825
2826         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2827
2828         vc_state_e state;
2829         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2830                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set current language changed : A handle is not available");
2831                 return VC_ERROR_INVALID_STATE;
2832         }
2833
2834         /* check state */
2835         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2836
2837         vc_mgr_client_set_current_lang_changed_cb(g_vc_m, callback, user_data);
2838
2839         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set current language changed callback");
2840         return VC_ERROR_NONE;
2841 }
2842
2843 int vc_mgr_unset_current_language_changed_cb(void)
2844 {
2845         if (0 != __vc_mgr_get_feature_enabled()) {
2846                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2847                 return VC_ERROR_NOT_SUPPORTED;
2848         }
2849
2850         if (0 != __vc_mgr_check_privilege()) {
2851                 return VC_ERROR_PERMISSION_DENIED;
2852         }
2853
2854         vc_state_e state;
2855         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2856                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset current language changed : A handle is not available");
2857                 return VC_ERROR_INVALID_STATE;
2858         }
2859
2860         /* check state */
2861         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2862
2863         vc_mgr_client_set_current_lang_changed_cb(g_vc_m, NULL, NULL);
2864
2865         return VC_ERROR_NONE;
2866 }
2867
2868 int vc_mgr_set_error_cb(vc_error_cb callback, void* user_data)
2869 {
2870         if (0 != __vc_mgr_get_feature_enabled()) {
2871                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2872                 return VC_ERROR_NOT_SUPPORTED;
2873         }
2874
2875         if (0 != __vc_mgr_check_privilege()) {
2876                 return VC_ERROR_PERMISSION_DENIED;
2877         }
2878
2879         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
2880
2881         vc_state_e state;
2882         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2883                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set error callback : A handle is not available");
2884                 return VC_ERROR_INVALID_STATE;
2885         }
2886
2887         /* check state */
2888         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2889
2890         vc_mgr_client_set_error_cb(g_vc_m, callback,  user_data);
2891
2892         return VC_ERROR_NONE;
2893 }
2894
2895 int vc_mgr_unset_error_cb(void)
2896 {
2897         if (0 != __vc_mgr_get_feature_enabled()) {
2898                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
2899                 return VC_ERROR_NOT_SUPPORTED;
2900         }
2901
2902         if (0 != __vc_mgr_check_privilege()) {
2903                 return VC_ERROR_PERMISSION_DENIED;
2904         }
2905
2906         vc_state_e state;
2907         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
2908                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset error callback : A handle is not available");
2909                 return VC_ERROR_INVALID_STATE;
2910         }
2911
2912         /* check state */
2913         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
2914
2915         vc_mgr_client_set_error_cb(g_vc_m, NULL, NULL);
2916
2917         return VC_ERROR_NONE;
2918 }
2919
2920 int __vc_mgr_cb_dialog(int pid, const char* disp_text, const char* utt_text, bool continuous)
2921 {
2922         vc_mgr_dialog_request_cb callback = NULL;
2923         void* user_data = NULL;
2924
2925         vc_mgr_client_get_dialog_request_cb(g_vc_m, &callback, &user_data);
2926
2927         if (NULL != callback) {
2928                 vc_mgr_client_use_callback(g_vc_m);
2929                 callback(pid, disp_text, utt_text, continuous, user_data);
2930                 vc_mgr_client_not_use_callback(g_vc_m);
2931                 SLOG(LOG_INFO, TAG_VCM, "Dialog callback is called, disp_text(%s), utt)text(%s), continuous(%d)", disp_text, utt_text, continuous);
2932         } else {
2933                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Error callback is null");
2934         }
2935
2936         return VC_ERROR_NONE;
2937 }
2938
2939 int __vc_mgr_cb_private_data_set(const char* key, const char* data)
2940 {
2941         vc_mgr_private_data_set_cb callback = NULL;
2942         void* user_data = NULL;
2943         int ret = -1;
2944
2945         vc_mgr_client_get_private_data_set_cb(g_vc_m, &callback, &user_data);
2946
2947         if (NULL != callback) {
2948                 vc_mgr_client_use_callback(g_vc_m);
2949                 ret = callback(key, data, user_data);
2950                 vc_mgr_client_not_use_callback(g_vc_m);
2951                 SLOG(LOG_INFO, TAG_VCM, "Private data set callback is called");
2952         } else {
2953                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Private data set callback is null");
2954         }
2955
2956         return ret;
2957 }
2958
2959 int __vc_mgr_cb_private_data_requested(const char* key, char** data)
2960 {
2961         vc_mgr_private_data_requested_cb callback = NULL;
2962         void* user_data = NULL;
2963         int ret = -1;
2964
2965         vc_mgr_client_get_private_data_requested_cb(g_vc_m, &callback, &user_data);
2966
2967         if (NULL != callback) {
2968                 vc_mgr_client_use_callback(g_vc_m);
2969                 ret = callback(key, data, user_data);
2970                 vc_mgr_client_not_use_callback(g_vc_m);
2971                 SLOG(LOG_INFO, TAG_VCM, "Private data requested callback is called");
2972         } else {
2973                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] Private data requested callback is null");
2974         }
2975
2976         return ret;
2977 }
2978
2979 /* for TTS feedback */
2980 int __vc_mgr_cb_feedback_audio_format(int rate, vc_audio_channel_e channel, vc_audio_type_e audio_type)
2981 {
2982         vc_mgr_feedback_audio_format_cb callback = NULL;
2983         void* user_data = NULL;
2984
2985         /* set global audio formats */
2986         g_feedback_rate = rate;
2987         g_feedback_audio_channel = channel;
2988         g_feedback_audio_type = audio_type;
2989
2990         vc_mgr_client_get_feedback_audio_format_cb(g_vc_m, &callback, &user_data);
2991
2992         if (NULL != callback) {
2993                 vc_mgr_client_use_callback(g_vc_m);
2994                 callback(rate, channel, audio_type, user_data);
2995                 vc_mgr_client_not_use_callback(g_vc_m);
2996                 SLOG(LOG_INFO, TAG_VCM, "TTS feedback audio format callback is called");
2997         } else {
2998                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] TTS feedback audio format callback is null");
2999         }
3000
3001         return VC_ERROR_NONE;
3002 }
3003
3004 int __vc_mgr_cb_feedback_streaming(int pid, int utt_id, vc_feedback_event_e event, char* buffer, int len)
3005 {
3006         /* add feedback data */
3007         vc_feedback_data_s* temp_feedback_data = NULL;
3008         temp_feedback_data = (vc_feedback_data_s*)calloc(1, sizeof(vc_feedback_data_s));
3009         RETVM_IF(NULL == temp_feedback_data, VC_ERROR_OUT_OF_MEMORY, TAG_VCM, "[ERROR] Out of memory");
3010         SLOG(LOG_INFO, TAG_VCM, "[INFO] feedback streaming before queing");
3011
3012         temp_feedback_data->data = NULL;
3013         temp_feedback_data->rate = g_feedback_rate;
3014         temp_feedback_data->data_size = 0;
3015
3016         if (0 < len) {
3017                 temp_feedback_data->data = (char*)calloc(len + 5, sizeof(char));
3018                 if (NULL != temp_feedback_data->data) {
3019                         memcpy(temp_feedback_data->data, buffer, len);
3020                         temp_feedback_data->data_size = len;
3021                         SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG][memcpy] data(%p) size(%d)",
3022                                         temp_feedback_data->data, temp_feedback_data->data_size);
3023                 } else {
3024                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] fail to allocate memory");
3025                 }
3026         } else {
3027                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] feedback data is NULL");
3028         }
3029
3030         temp_feedback_data->pid = pid;
3031         temp_feedback_data->utt_id = utt_id;
3032         temp_feedback_data->event = event;
3033         temp_feedback_data->audio_type = g_feedback_audio_type;
3034         temp_feedback_data->channel = g_feedback_audio_channel;
3035
3036         SLOG(LOG_INFO, TAG_VCM, "[INFO] add feedback data, pid(%d), utt_id(%d), event(%d), audio_type(%d), channel(%d)", pid, utt_id, event, g_feedback_audio_type, g_feedback_audio_channel);
3037
3038         int ret = vc_mgr_data_add_feedback_data(temp_feedback_data);
3039         if (0 != ret) {
3040                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to add feedback data");
3041                 if (NULL != temp_feedback_data->data) {
3042                         free(temp_feedback_data->data);
3043                         temp_feedback_data->data = NULL;
3044                 }
3045                 if (NULL != temp_feedback_data) {
3046                         free(temp_feedback_data);
3047                         temp_feedback_data = NULL;
3048                 }
3049         }
3050
3051         return ret;
3052 }
3053
3054
3055 int vc_mgr_set_dialog_request_cb(vc_mgr_dialog_request_cb callback, void* user_data)
3056 {
3057         if (0 != __vc_mgr_get_feature_enabled()) {
3058                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3059                 return VC_ERROR_NOT_SUPPORTED;
3060         }
3061
3062         if (0 != __vc_mgr_check_privilege()) {
3063                 return VC_ERROR_PERMISSION_DENIED;
3064         }
3065
3066         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
3067
3068         vc_state_e state;
3069         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3070                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set error callback : A handle is not available");
3071                 return VC_ERROR_INVALID_STATE;
3072         }
3073
3074         /* check state */
3075         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
3076
3077         vc_mgr_client_set_dialog_request_cb(g_vc_m, callback,  user_data);
3078
3079         return VC_ERROR_NONE;
3080 }
3081
3082 int vc_mgr_unset_dialog_request_cb(void)
3083 {
3084         if (0 != __vc_mgr_get_feature_enabled()) {
3085                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3086                 return VC_ERROR_NOT_SUPPORTED;
3087         }
3088
3089         if (0 != __vc_mgr_check_privilege()) {
3090                 return VC_ERROR_PERMISSION_DENIED;
3091         }
3092
3093         vc_state_e state;
3094         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3095                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset error callback : A handle is not available");
3096                 return VC_ERROR_INVALID_STATE;
3097         }
3098
3099         /* check state */
3100         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
3101
3102         vc_mgr_client_set_dialog_request_cb(g_vc_m, NULL, NULL);
3103
3104         return VC_ERROR_NONE;
3105 }
3106
3107 int vc_mgr_set_private_data_set_cb(vc_mgr_private_data_set_cb callback, void* user_data)
3108 {
3109         if (0 != __vc_mgr_get_feature_enabled()) {
3110                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3111                 return VC_ERROR_NOT_SUPPORTED;
3112         }
3113
3114         if (0 != __vc_mgr_check_privilege()) {
3115                 return VC_ERROR_PERMISSION_DENIED;
3116         }
3117
3118         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
3119
3120         vc_state_e state;
3121         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3122                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set private data set callback : A handle is not available");
3123                 return VC_ERROR_INVALID_STATE;
3124         }
3125
3126         /* check state */
3127         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Set private data set callback : Current state is not 'Initialized'");
3128
3129         vc_mgr_client_set_private_data_set_cb(g_vc_m, callback, user_data);
3130
3131         return VC_ERROR_NONE;
3132 }
3133
3134 int vc_mgr_unset_private_data_set_cb(void)
3135 {
3136         if (0 != __vc_mgr_get_feature_enabled()) {
3137                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3138                 return VC_ERROR_NOT_SUPPORTED;
3139         }
3140
3141         if (0 != __vc_mgr_check_privilege()) {
3142                 return VC_ERROR_PERMISSION_DENIED;
3143         }
3144
3145         vc_state_e state;
3146         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3147                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset private data set callback : A handle is not available");
3148                 return VC_ERROR_INVALID_STATE;
3149         }
3150
3151         /* check state */
3152         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Unset private data set callback : Current state is not 'Initialized'");
3153
3154         vc_mgr_client_set_private_data_set_cb(g_vc_m, NULL, NULL);
3155
3156         return VC_ERROR_NONE;
3157 }
3158
3159 int vc_mgr_set_private_data_requested_cb(vc_mgr_private_data_requested_cb callback, void* user_data)
3160 {
3161         if (0 != __vc_mgr_get_feature_enabled()) {
3162                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3163                 return VC_ERROR_NOT_SUPPORTED;
3164         }
3165
3166         if (0 != __vc_mgr_check_privilege()) {
3167                 return VC_ERROR_PERMISSION_DENIED;
3168         }
3169
3170         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
3171
3172         vc_state_e state;
3173         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3174                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set private data requested callback : A handle is not available");
3175                 return VC_ERROR_INVALID_STATE;
3176         }
3177
3178         /* check state*/
3179         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Set private data requested callback : Current state is not 'Initialized'");
3180
3181         vc_mgr_client_set_private_data_requested_cb(g_vc_m, callback, user_data);
3182
3183         return VC_ERROR_NONE;
3184 }
3185
3186 int vc_mgr_unset_private_data_requested_cb(void)
3187 {
3188         if (0 != __vc_mgr_get_feature_enabled()) {
3189                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3190                 return VC_ERROR_NOT_SUPPORTED;
3191         }
3192
3193         if (0 != __vc_mgr_check_privilege()) {
3194                 return VC_ERROR_PERMISSION_DENIED;
3195         }
3196
3197         vc_state_e state;
3198         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3199                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset private data requested callback : A handle is not available");
3200                 return VC_ERROR_INVALID_STATE;
3201         }
3202
3203         /* check state */
3204         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Unset private data requested callback : Current state is not 'Initialized'");
3205
3206         vc_mgr_client_set_private_data_requested_cb(g_vc_m, NULL, NULL);
3207
3208         return VC_ERROR_NONE;
3209 }
3210
3211 static bool __vc_mgr_check_demandable_client(int pid)
3212 {
3213         if (0 == g_slist_length(g_demandable_client_list)) {
3214                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] No demandable clients");
3215                 return false;
3216         }
3217
3218         char appid[1024] = {'\0', };
3219         if (0 != aul_app_get_appid_bypid(pid, appid, sizeof(appid) - 1)) {
3220                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get callee appid by pid");
3221                 return false;
3222         }
3223         SLOG(LOG_INFO, TAG_VCM, "[CHECK] Appid - %s", appid);
3224
3225         GSList *iter = NULL;
3226         vc_demandable_client_s* temp_client;
3227         iter = g_slist_nth(g_demandable_client_list, 0);
3228
3229         while (NULL != iter) {
3230                 temp_client = iter->data;
3231
3232                 if (NULL != temp_client) {
3233                         if (NULL != temp_client->appid) {
3234                                 if (!strcmp(temp_client->appid, appid)) {
3235                                         SLOG(LOG_DEBUG, TAG_VCM, "pid(%d) is available", pid);
3236                                         return true;
3237                                 }
3238                         }
3239                 }
3240
3241                 iter = g_slist_next(iter);
3242         }
3243
3244         return false;
3245 }
3246
3247 /* Authority */
3248 int __vc_mgr_request_auth_enable(int pid)
3249 {
3250         if (false == __vc_mgr_check_demandable_client(pid)) {
3251                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Not demandable client");
3252                 return VC_ERROR_INVALID_PARAMETER;
3253         }
3254
3255         /* check already authorized */
3256         if (true == vc_mgr_client_is_authorized_client(g_vc_m, pid)) {
3257                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Already authorized");
3258                 return VC_ERROR_INVALID_PARAMETER;
3259         }
3260
3261         /* add authorized list */
3262         if (0 != vc_mgr_client_add_authorized_client(g_vc_m, pid)) {
3263                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to add authorized client");
3264                 return VC_ERROR_OPERATION_FAILED;
3265         }
3266
3267         /* foreground check */
3268         int fore_pid = 0;
3269         if (0 != vc_mgr_client_get_foreground(g_vc_m, &fore_pid)) {
3270                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to get foreground");
3271                 return VC_ERROR_OPERATION_FAILED;
3272         }
3273
3274         if (pid == fore_pid) {
3275                 vc_mgr_client_set_valid_authorized_client(g_vc_m, pid);
3276         }
3277
3278         return VC_ERROR_NONE;
3279 }
3280
3281 int __vc_mgr_request_auth_disable(int pid)
3282 {
3283         /* check authorized */
3284         if (false == vc_mgr_client_is_authorized_client(g_vc_m, pid)) {
3285                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No authorized");
3286                 return VC_ERROR_INVALID_PARAMETER;
3287         }
3288
3289         /* remove authorized list */
3290         if (0 != vc_mgr_client_remove_authorized_client(g_vc_m, pid)) {
3291                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to remove authorized client");
3292                 return VC_ERROR_OPERATION_FAILED;
3293         }
3294
3295         /* check authority valid */
3296         if (true == vc_mgr_client_is_valid_authorized_client(g_vc_m, pid)) {
3297                 SLOG(LOG_DEBUG, TAG_VCM, "Valid authorized client is removed");
3298                 if (0 != vc_mgr_client_set_valid_authorized_client(g_vc_m, -1)) {
3299                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set valid authorized client");
3300                         return VC_ERROR_OPERATION_FAILED;
3301                 }
3302         }
3303
3304         return VC_ERROR_NONE;
3305 }
3306
3307 static Eina_Bool __request_auth_start(void* data)
3308 {
3309         SLOG(LOG_INFO, TAG_VCM, "Request Start");
3310
3311         if (0 != vc_mgr_client_set_start_by_client(g_vc_m, true)) {
3312                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set start by client");
3313         }
3314
3315         if (0 != vc_mgr_start(false)) {
3316                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Request start is failed");
3317                 /* TODO - Error handling? */
3318         }
3319
3320         if (0 != vc_mgr_client_set_start_by_client(g_vc_m, false)) {
3321                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set start by client");
3322         }
3323
3324
3325         return EINA_FALSE;
3326 }
3327
3328 int __vc_mgr_request_auth_start(int pid)
3329 {
3330         /* check authorized */
3331         if (false == vc_mgr_client_is_valid_authorized_client(g_vc_m, pid)) {
3332                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No valid authorized client");
3333                 return VC_ERROR_INVALID_PARAMETER;
3334         }
3335
3336         /* add timer for start recording */
3337         ecore_timer_add(0, __request_auth_start, NULL);
3338
3339         return VC_ERROR_NONE;
3340 }
3341
3342 static Eina_Bool __request_auth_stop(void* data)
3343 {
3344         SLOG(LOG_INFO, TAG_VCM, "Request Stop");
3345
3346         if (0 != vc_mgr_stop()) {
3347                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Request stop is failed");
3348                 /* TODO - Error handling? */
3349         }
3350
3351         return EINA_FALSE;
3352 }
3353
3354 int __vc_mgr_request_auth_stop(int pid)
3355 {
3356         /* check authorized */
3357         if (false == vc_mgr_client_is_valid_authorized_client(g_vc_m, pid)) {
3358                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No valid authorized client");
3359                 return VC_ERROR_INVALID_PARAMETER;
3360         }
3361
3362         /* add timer for start recording */
3363         ecore_timer_add(0, __request_auth_stop, NULL);
3364
3365         return VC_ERROR_NONE;
3366 }
3367
3368 static Eina_Bool __request_auth_cancel(void* data)
3369 {
3370         SLOG(LOG_INFO, TAG_VCM, "Request Cancel");
3371
3372         if (0 != vc_mgr_cancel()) {
3373                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Request cancel is failed");
3374                 /* TODO - Error handling? */
3375         }
3376
3377         return EINA_FALSE;
3378 }
3379
3380 int __vc_mgr_request_auth_cancel(int pid)
3381 {
3382         /* check authorized */
3383         if (false == vc_mgr_client_is_valid_authorized_client(g_vc_m, pid)) {
3384                 SLOG(LOG_ERROR,  TAG_VCM, "[ERROR] No valid authorized client");
3385                 return VC_ERROR_INVALID_PARAMETER;
3386         }
3387
3388         /* add timer for start recording */
3389         ecore_timer_add(0, __request_auth_cancel, NULL);
3390
3391         return VC_ERROR_NONE;
3392 }
3393
3394 int vc_mgr_set_specific_engine_result_cb(vc_mgr_specific_engine_result_cb callback, void* user_data)
3395 {
3396         if (0 != __vc_mgr_get_feature_enabled()) {
3397                 return VC_ERROR_NOT_SUPPORTED;
3398         }
3399
3400         if (0 != __vc_mgr_check_privilege()) {
3401                 return VC_ERROR_PERMISSION_DENIED;
3402         }
3403
3404         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
3405
3406         vc_state_e state;
3407         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3408                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set specific engine result callback : A handle is not available");
3409                 return VC_ERROR_INVALID_STATE;
3410         }
3411
3412         /* check state */
3413         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
3414
3415         vc_mgr_client_set_specific_engine_result_cb(g_vc_m, callback, user_data);
3416
3417         return VC_ERROR_NONE;
3418 }
3419 int vc_mgr_unset_specific_engine_result_cb(void)
3420 {
3421         if (0 != __vc_mgr_get_feature_enabled()) {
3422                 return VC_ERROR_NOT_SUPPORTED;
3423         }
3424
3425         vc_state_e state;
3426         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3427                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset specific engine result callback : A handle is not available");
3428                 return VC_ERROR_INVALID_STATE;
3429         }
3430
3431         /* check state */
3432         RETVM_IF(state != VC_STATE_INITIALIZED, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Initialized'", state);
3433
3434         vc_mgr_client_set_specific_engine_result_cb(g_vc_m, NULL, NULL);
3435
3436         return VC_ERROR_NONE;
3437 }
3438
3439 void __vc_mgr_cb_specific_engine_result(const char* engine_app_id, const char* event, const char* result)
3440 {
3441         vc_mgr_specific_engine_result_cb callback = NULL;
3442         void* user_data = NULL;
3443
3444         vc_mgr_client_get_specific_engine_result_cb(g_vc_m, &callback, &user_data);
3445         RETM_IF(NULL == callback, TAG_VCM, "[ERROR] Client specific engine result callback is NULL");
3446
3447         vc_mgr_client_use_callback(g_vc_m);
3448         callback(engine_app_id, event, result, user_data);
3449         vc_mgr_client_not_use_callback(g_vc_m);
3450         SLOG(LOG_INFO, TAG_VCM, "Specific engine result callback is called, engine app id(%s), event(%s), result(%s)", engine_app_id, event, result);
3451
3452         return;
3453 }
3454
3455 /* for TTS feedback */
3456 int vc_mgr_set_feedback_audio_format_cb(vc_mgr_feedback_audio_format_cb callback, void* user_data)
3457 {
3458         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] set feedback audio format");
3459
3460         if (0 != __vc_mgr_get_feature_enabled()) {
3461                 return VC_ERROR_NOT_SUPPORTED;
3462         }
3463
3464         if (0 != __vc_mgr_check_privilege()) {
3465                 return VC_ERROR_PERMISSION_DENIED;
3466         }
3467
3468         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
3469
3470         vc_state_e state;
3471         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3472                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback audio format callback : A handle is not available");
3473                 return VC_ERROR_INVALID_STATE;
3474         }
3475
3476         /* check state */
3477         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Set feedback audio format callback : Current state is not 'Initialized'");
3478
3479         vc_mgr_client_set_feedback_audio_format_cb(g_vc_m, callback, user_data);
3480
3481         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set feedback audio format callback");
3482
3483         return VC_ERROR_NONE;
3484 }
3485
3486 int vc_mgr_unset_feedback_audio_format_cb()
3487 {
3488         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] unset feedback audio format");
3489
3490         if (0 != __vc_mgr_get_feature_enabled()) {
3491                 return VC_ERROR_NOT_SUPPORTED;
3492         }
3493
3494         if (0 != __vc_mgr_check_privilege()) {
3495                 return VC_ERROR_PERMISSION_DENIED;
3496         }
3497
3498         vc_state_e state;
3499         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3500                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback audio format callback : A handle is not available");
3501                 return VC_ERROR_INVALID_STATE;
3502         }
3503
3504         /* check state */
3505         RETVM_IF(VC_STATE_INITIALIZED != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Unset feedback audio format callback : Current state is not 'Initialized'");
3506
3507         vc_mgr_client_set_feedback_audio_format_cb(g_vc_m, NULL, NULL);
3508
3509         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Unset feedback audio format callback");
3510
3511         return VC_ERROR_NONE;
3512 }
3513
3514 int vc_mgr_set_feedback_streaming_cb(vc_mgr_feedback_streaming_cb callback, void* user_data)
3515 {
3516         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] set feedback streaming");
3517
3518         if (0 != __vc_mgr_get_feature_enabled()) {
3519                 return VC_ERROR_NOT_SUPPORTED;
3520         }
3521
3522         if (0 != __vc_mgr_check_privilege()) {
3523                 return VC_ERROR_PERMISSION_DENIED;
3524         }
3525
3526         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
3527
3528         pthread_mutex_lock(&g_feedback_streaming_cb_mutex);
3529
3530         vc_state_e state;
3531         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3532                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback streaming callback : A handle is not available");
3533                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
3534                 return VC_ERROR_INVALID_STATE;
3535         }
3536
3537         /* check state */
3538         if (VC_STATE_INITIALIZED != state) {
3539                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback streaming callback : Current state is not 'Initialized' (%d)", state);
3540                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
3541                 return VC_ERROR_INVALID_STATE;
3542         }
3543
3544         vc_mgr_client_set_feedback_streaming_cb(g_vc_m, callback, user_data);
3545
3546         pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
3547
3548         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set feedback streaming callback");
3549
3550         return VC_ERROR_NONE;
3551 }
3552
3553 int vc_mgr_unset_feedback_streaming_cb()
3554 {
3555         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] unset feedback streaming");
3556
3557         if (0 != __vc_mgr_get_feature_enabled()) {
3558                 return VC_ERROR_NOT_SUPPORTED;
3559         }
3560
3561         if (0 != __vc_mgr_check_privilege()) {
3562                 return VC_ERROR_PERMISSION_DENIED;
3563         }
3564
3565         pthread_mutex_lock(&g_feedback_streaming_cb_mutex);
3566
3567         vc_state_e state;
3568         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3569                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback streaming callback : A handle is not available");
3570                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
3571                 return VC_ERROR_INVALID_STATE;
3572         }
3573
3574         /* check state */
3575         if (VC_STATE_INITIALIZED != state) {
3576                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback streaming callback : Current state is not 'Initialized' (%d)", state);
3577                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
3578                 return VC_ERROR_INVALID_STATE;
3579         }
3580
3581         vc_mgr_client_set_feedback_streaming_cb(g_vc_m, NULL, NULL);
3582
3583         pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
3584
3585         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Unset feedback streaming callback");
3586
3587         return VC_ERROR_NONE;
3588 }
3589
3590 int vc_mgr_set_vc_tts_streaming_cb(vc_mgr_vc_tts_streaming_cb callback, void* user_data)
3591 {
3592         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] set vc tts streaming callback");
3593
3594         if (0 != __vc_mgr_get_feature_enabled()) {
3595                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3596                 return VC_ERROR_NOT_SUPPORTED;
3597         }
3598
3599         if (0 != __vc_mgr_check_privilege()) {
3600                 return VC_ERROR_PERMISSION_DENIED;
3601         }
3602
3603         RETVM_IF(NULL == callback, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
3604
3605         pthread_mutex_lock(&g_vc_tts_streaming_cb_mutex);
3606
3607         vc_state_e state;
3608         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3609                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback streaming callback : A handle is not available");
3610                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
3611                 return VC_ERROR_INVALID_STATE;
3612         }
3613
3614         /* check state */
3615         if (VC_STATE_INITIALIZED != state) {
3616                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Set feedback streaming callback : Current state is not 'Initialized' (%d)", state);
3617                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
3618                 return VC_ERROR_INVALID_STATE;
3619         }
3620
3621         vc_mgr_client_set_vc_tts_streaming_cb(g_vc_m, callback, user_data);
3622
3623         pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
3624
3625         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Set feedback streaming callback");
3626
3627         return VC_ERROR_NONE;
3628 }
3629
3630 int vc_mgr_unset_vc_tts_streaming_cb()
3631 {
3632         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] unset vc tts streaming callback");
3633
3634         if (0 != __vc_mgr_get_feature_enabled()) {
3635                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3636                 return VC_ERROR_NOT_SUPPORTED;
3637         }
3638
3639         if (0 != __vc_mgr_check_privilege()) {
3640                 return VC_ERROR_PERMISSION_DENIED;
3641         }
3642
3643         pthread_mutex_lock(&g_vc_tts_streaming_cb_mutex);
3644
3645         vc_state_e state;
3646         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3647                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback streaming callback : A handle is not available");
3648                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
3649                 return VC_ERROR_INVALID_STATE;
3650         }
3651
3652         /* check state */
3653         if (VC_STATE_INITIALIZED != state) {
3654                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Unset feedback streaming callback : Current state is not 'Initialized' (%d)", state);
3655                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
3656                 return VC_ERROR_INVALID_STATE;
3657         }
3658
3659         vc_mgr_client_set_vc_tts_streaming_cb(g_vc_m, NULL, NULL);
3660
3661         pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
3662
3663         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Unset feedback streaming callback");
3664
3665         return VC_ERROR_NONE;
3666 }
3667
3668 static void __tts_feedback_thread(void* data, Ecore_Thread* thread)
3669 {
3670         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Start thread");
3671
3672         vc_feedback_data_s* feedback_data = NULL;
3673
3674         while (1) {
3675                 int ret = -1;
3676                 int cnt = 0;
3677
3678                 /* get feedback data */
3679                 ret = vc_mgr_data_get_feedback_data(&feedback_data);
3680                 if (0 != ret || NULL == feedback_data) {
3681                         /* empty queue */
3682                         SLOG(LOG_INFO, TAG_VCM, "[DEBUG] No feedback data. Waiting mode");
3683
3684                         /* waiting */
3685                         while (1) {
3686                                 usleep(10000);
3687                                 if (0 < vc_mgr_data_get_feedback_data_size()) {
3688                                         SLOG(LOG_INFO, TAG_VCM, "[INFO] Resume thread");
3689                                         break;
3690                                 }
3691                                 if (200 < cnt) {
3692                                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Wrong request, there's no pcm data");
3693                                         vc_mgr_data_clear_feedback_data(&feedback_data);
3694                                         return;
3695                                 }
3696                                 cnt++;
3697                         }
3698                         SLOG(LOG_INFO, TAG_VCM, "[INFO] Finish to wait for new feedback data come");
3699
3700                         /* resume feedback thread */
3701                         vc_mgr_data_clear_feedback_data(&feedback_data);
3702                         continue;
3703                 }
3704
3705                 if (NULL != feedback_data) {
3706                         if (getpid() == feedback_data->pid) {
3707                                 vc_mgr_feedback_streaming_cb callback = NULL;
3708                                 void* user_data = NULL;
3709
3710                                 pthread_mutex_lock(&g_feedback_streaming_cb_mutex);
3711
3712                                 vc_mgr_client_get_feedback_streaming_cb(g_vc_m, &callback, &user_data);
3713                                 if (NULL == callback) {
3714                                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] TTS feedback streaming callback is null");
3715                                         vc_mgr_data_clear_feedback_data(&feedback_data);
3716                                         pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
3717                                         return;
3718                                 }
3719
3720                                 SLOG(LOG_DEBUG, TAG_VCM, "TTS feedback streaming callback is called");
3721                                 vc_mgr_client_use_callback(g_vc_m);
3722                                 callback(feedback_data->event, feedback_data->data, feedback_data->data_size, user_data);
3723                                 vc_mgr_client_not_use_callback(g_vc_m);
3724
3725                                 pthread_mutex_unlock(&g_feedback_streaming_cb_mutex);
3726
3727                                 /* If no feedback data and EVENT_FINISH */
3728                                 if (0 >= vc_mgr_data_get_feedback_data_size() && VC_FEEDBACK_EVENT_FINISH == feedback_data->event) {
3729                                         SLOG(LOG_INFO, TAG_VCM, "[INFO] Finish feedback");
3730                                         if (feedback_data) {
3731                                                 free(feedback_data);
3732                                                 feedback_data = NULL;
3733                                         }
3734                                         break;
3735                                 }
3736                         } else {
3737                                 vc_mgr_vc_tts_streaming_cb callback = NULL;
3738                                 void* user_data = NULL;
3739
3740                                 pthread_mutex_lock(&g_vc_tts_streaming_cb_mutex);
3741
3742                                 vc_mgr_client_get_vc_tts_streaming_cb(g_vc_m, &callback, &user_data);
3743                                 if (NULL == callback) {
3744                                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] vc tts streaming callback is null");
3745                                         vc_mgr_data_clear_feedback_data(&feedback_data);
3746                                         pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
3747                                         return;
3748                                 }
3749
3750                                 SLOG(LOG_DEBUG, TAG_VCM, "vc tts feedback streaming callback is called");
3751                                 vc_mgr_client_use_callback(g_vc_m);
3752                                 callback(feedback_data->pid, feedback_data->utt_id, feedback_data->event, feedback_data->data, feedback_data->data_size, user_data);
3753                                 vc_mgr_client_not_use_callback(g_vc_m);
3754
3755                                 pthread_mutex_unlock(&g_vc_tts_streaming_cb_mutex);
3756
3757                                 /* If no feedback data and EVENT_FINISH */
3758                                 if (0 >= vc_mgr_data_get_feedback_data_size() && VC_FEEDBACK_EVENT_FINISH == feedback_data->event) {
3759                                         SLOG(LOG_INFO, TAG_VCM, "[INFO] Finish vc tts feedback");
3760                                         if (feedback_data) {
3761                                                 free(feedback_data);
3762                                                 feedback_data = NULL;
3763                                         }
3764                                         break;
3765                                 }
3766                         }
3767                         free(feedback_data);
3768                         feedback_data = NULL;
3769                 }
3770         }
3771 }
3772
3773 static void __end_tts_feedback_thread(void* data, Ecore_Thread* thread)
3774 {
3775         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] End thread");
3776         g_feedback_thread = NULL;
3777 }
3778
3779 int vc_mgr_start_feedback(void)
3780 {
3781         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] start feedback");
3782
3783         if (0 != __vc_mgr_get_feature_enabled()) {
3784                 return VC_ERROR_NOT_SUPPORTED;
3785         }
3786
3787         if (0 != __vc_mgr_check_privilege()) {
3788                 return VC_ERROR_PERMISSION_DENIED;
3789         }
3790
3791         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Request start TTS feedback");
3792
3793         vc_state_e state;
3794         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3795                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Start feedback : A handle is not available");
3796                 return VC_ERROR_INVALID_STATE;
3797         }
3798
3799         /* check state */
3800         RETVM_IF(VC_STATE_READY != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Start feedback : Current state is not 'Ready' (%d)", state);
3801
3802 #if 1
3803         bool is_exist = ecore_thread_check(g_feedback_thread);
3804         if (NULL == g_feedback_thread || TRUE == is_exist) {
3805                 SLOG(LOG_INFO, TAG_VCM, "[INFO] ecore thread run : __tts_feedback_thread");
3806                 g_feedback_thread = ecore_thread_run(__tts_feedback_thread, __end_tts_feedback_thread, NULL, NULL);
3807         }
3808 #else
3809         /* start playing TTS feedback */
3810         int ret = -1;
3811         ret = vc_mgr_player_play();
3812         if (0 != ret) {
3813                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to player play, ret(%d)", ret);
3814         }
3815 #endif
3816         return VC_ERROR_NONE;
3817 }
3818
3819 int vc_mgr_stop_feedback(void)
3820 {
3821         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] stop feedback");
3822
3823         if (0 != __vc_mgr_get_feature_enabled()) {
3824                 return VC_ERROR_NOT_SUPPORTED;
3825         }
3826
3827         if (0 != __vc_mgr_check_privilege()) {
3828                 return VC_ERROR_PERMISSION_DENIED;
3829         }
3830
3831         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] Request stop TTS feedback");
3832
3833         vc_state_e state;
3834         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3835                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Stop feedback : A handle is not available");
3836                 return VC_ERROR_INVALID_STATE;
3837         }
3838
3839         /* check state */
3840         RETVM_IF(VC_STATE_READY != state, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Stop feedback : Current state is not 'Ready' (%d)", state);
3841
3842 #if 1
3843         int ret = -1;
3844
3845         while (1) {
3846                 vc_feedback_data_s* feedback_data = NULL;
3847
3848                 /* get feedback data */
3849                 ret = vc_mgr_data_get_feedback_data(&feedback_data);
3850                 if (0 != ret || NULL == feedback_data) {
3851                         /* empty queue */
3852                         SLOG(LOG_INFO, TAG_VCM, "[INFO] No feedback data to stop any more");
3853                         return VC_ERROR_NONE;
3854                 }
3855                 ret = vc_mgr_data_clear_feedback_data(&feedback_data);
3856                 if (0 != ret) {
3857                         SLOG(LOG_INFO, TAG_VCM, "[ERROR] Fail to clear data, ret(%d)", ret);
3858                         break;
3859                 }
3860         }
3861 #else
3862         /* request to stop playing TTS feedback */
3863         int ret = -1;
3864         ret = vc_mgr_player_stop();
3865         if (0 != ret) {
3866                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to player stop, ret(%d)", ret);
3867         }
3868 #endif
3869         return ret;
3870 }
3871
3872 int vc_mgr_send_utterance_status(int pid, int utt_id, vc_tts_utterance_status_e utt_status)
3873 {
3874         SLOG(LOG_INFO, TAG_VCM, "@@@ [Manager] send utterance status, pid(%d), utt_id(%d), utt_status(%d)", pid, utt_id, utt_status);
3875
3876         if (0 != __vc_mgr_get_feature_enabled()) {
3877                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3878                 return VC_ERROR_NOT_SUPPORTED;
3879         }
3880
3881         if (0 != __vc_mgr_check_privilege()) {
3882                 return VC_ERROR_PERMISSION_DENIED;
3883         }
3884
3885         if (VC_TTS_UTTERANCE_NONE > utt_status || VC_TTS_UTTERANCE_CANCELED < utt_status || 0 == utt_status) {
3886                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] utt_status is not valid. (%d)", utt_status);
3887                 return VC_ERROR_INVALID_PARAMETER;
3888         }
3889
3890         vc_state_e state;
3891         if (0 != vc_mgr_client_get_client_state(g_vc_m, &state)) {
3892                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
3893                 return VC_ERROR_INVALID_STATE;
3894         }
3895
3896         /* check state */
3897         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
3898
3899         /* Check service state */
3900         vc_service_state_e service_state = -1;
3901         vc_mgr_client_get_service_state(g_vc_m, &service_state);
3902         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);
3903
3904         int ret = vc_mgr_tidl_send_utterance_status(pid, utt_id, utt_status);
3905         if (0 != ret)
3906                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to send utterance status : %s", __vc_mgr_get_error_code(ret));
3907         else
3908                 SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Send utterance status");
3909
3910         SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] send utterance status DONE");
3911         return ret;
3912 }
3913
3914 static float __get_volume_decibel(char* data, int size)
3915 {
3916 #define MAX_AMPLITUDE_MEAN_16 32768
3917
3918         int i, depthByte;
3919         int count = 0;
3920
3921         float db = 0.0;
3922         float rms = 0.0;
3923         unsigned long long square_sum = 0;
3924         short pcm16 = 0;
3925
3926         depthByte = 2;
3927
3928         for (i = 0; i < size; i += (depthByte<<1)) {
3929                 pcm16 = 0;
3930                 memcpy(&pcm16, data + i, sizeof(short));
3931                 square_sum += pcm16 * pcm16;
3932                 count++;
3933         }
3934
3935         if (0 == count || 0 == square_sum) {
3936                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] No data");
3937                 rms = 1.0;
3938         } else {
3939                 rms = sqrt((float)square_sum/count);
3940         }
3941
3942         db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
3943         return db;
3944 }
3945
3946 int vc_mgr_send_audio_streaming(vc_audio_streaming_event_e event, unsigned char* buffer, unsigned int len)
3947 {
3948         SLOG(LOG_INFO, TAG_VCM, "[Manager] Send audio streaming to the engine service, event(%d)", event);
3949
3950         if (0 != __vc_mgr_get_feature_enabled()) {
3951                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
3952                 return VC_ERROR_NOT_SUPPORTED;
3953         }
3954
3955         if (0 != __vc_mgr_check_privilege()) {
3956                 return VC_ERROR_PERMISSION_DENIED;
3957         }
3958
3959         if (VC_AUDIO_STREAMING_EVENT_FAIL > event || VC_AUDIO_STREAMING_EVENT_FINISH < event || 0 == event) {
3960                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] event is invalid parameter (%d)", event);
3961                 return VC_ERROR_INVALID_PARAMETER;
3962         }
3963
3964         RETVM_IF(NULL == buffer, VC_ERROR_INVALID_PARAMETER, TAG_VCM, "[ERROR] Invalid parameter");
3965
3966         vc_state_e state = VC_STATE_NONE;
3967         int ret = vc_mgr_client_get_client_state(g_vc_m, &state);
3968         if (0 != ret) {
3969                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
3970                 return VC_ERROR_INVALID_STATE;
3971         }
3972
3973         RETVM_IF(state != VC_STATE_READY, VC_ERROR_INVALID_STATE, TAG_VCM, "[ERROR] Invalid State: Current state(%d) is not 'Ready'", state);
3974
3975         vc_service_state_e service_state = VC_SERVICE_STATE_NONE;
3976         ret = vc_mgr_client_get_service_state(g_vc_m, &service_state);
3977         if (0 != ret) {
3978                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
3979                 return VC_ERROR_INVALID_STATE;
3980         }
3981
3982         if (VC_SERVICE_STATE_READY != service_state && VC_SERVICE_STATE_RECORDING != service_state) {
3983                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: service state is not 'READY' and 'RECORDING', state(%d)", service_state);
3984                 return VC_ERROR_INVALID_STATE;
3985         }
3986
3987         float volume = __get_volume_decibel((char*)buffer, len);
3988         __vc_mgr_set_volume(volume);
3989
3990         ret = vc_mgr_tidl_send_audio_streaming(g_vc_m->handle, event, buffer, len);
3991         if (0 != ret)
3992                 SLOG(LOG_WARN, TAG_VCM, "[WARNING] retry to send audio streaming data");
3993         else
3994                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Success to send");
3995         return ret;
3996 }
3997
3998 int vc_mgr_set_audio_streaming_mode(vc_audio_streaming_mode_e mode)
3999 {
4000         SLOG(LOG_INFO, TAG_VCM, "[Manager] Set multi assistant mode, mode(%d)", mode);
4001
4002         if (0 != __vc_mgr_get_feature_enabled()) {
4003                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
4004                 return VC_ERROR_NOT_SUPPORTED;
4005         }
4006
4007         if (0 != __vc_mgr_check_privilege()) {
4008                 return VC_ERROR_PERMISSION_DENIED;
4009         }
4010
4011         if (VC_AUDIO_STREAMING_MODE_VC_SERVICE > mode || VC_AUDIO_STREAMING_MODE_OUTSIDE < mode) {
4012                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] mode is invalid parameter (%d)", mode);
4013                 return VC_ERROR_INVALID_PARAMETER;
4014         }
4015
4016         vc_state_e state = VC_STATE_NONE;
4017         int ret = vc_mgr_client_get_client_state(g_vc_m, &state);
4018         if (0 != ret) {
4019                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
4020                 return VC_ERROR_INVALID_STATE;
4021         }
4022
4023         vc_service_state_e service_state = VC_SERVICE_STATE_NONE;
4024         ret = vc_mgr_client_get_service_state(g_vc_m, &service_state);
4025         if (0 != ret) {
4026                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
4027                 return VC_ERROR_INVALID_STATE;
4028         }
4029
4030         if (VC_STATE_INITIALIZED == state && VC_SERVICE_STATE_NONE == service_state) {
4031                 ret = vc_mgr_client_set_audio_streaming_mode(g_vc_m, mode);
4032                 if (VC_ERROR_NONE != ret) {
4033                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set multi assistant mode : %s", __vc_mgr_get_error_code(ret));
4034                         return VC_ERROR_INVALID_STATE;
4035                 } else {
4036                         SLOG(LOG_DEBUG, TAG_VCM, "[SUCCESS] Set audio streaming mode in vc mgr client");
4037                 }
4038         } else if (VC_STATE_READY == state && VC_SERVICE_STATE_READY == service_state) {
4039                 ret = vc_mgr_client_set_audio_streaming_mode(g_vc_m, mode);
4040                 if (VC_ERROR_NONE != ret) {
4041                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set multi assistant mode : %s", __vc_mgr_get_error_code(ret));
4042                         return VC_ERROR_INVALID_STATE;
4043                 } else {
4044                         SLOG(LOG_DEBUG, TAG_VCM, "[SUCCESS] Set audio streaming mode in vc mgr client");
4045                 }
4046
4047                 ret = vc_mgr_tidl_request_set_audio_streaming_mode(g_vc_m->handle, mode);
4048                 if (VC_ERROR_NONE != ret) {
4049                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to set audio streaming mode : %s", __vc_mgr_get_error_code(ret));
4050                         return VC_ERROR_OPERATION_FAILED;
4051                 } else {
4052                         SLOG(LOG_DEBUG, TAG_VCM, "[SUCCESS] Set audio streaming mode in vc mgr tidl");
4053                 }
4054         } else {
4055                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current state is not 'INITIALIZED', state(%d)", state);
4056                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current service state is not 'READY', service_state(%d)", service_state);
4057                 return VC_ERROR_INVALID_STATE;
4058         }
4059
4060         return VC_ERROR_NONE;
4061 }
4062
4063 int __vc_change_system_volume(vc_system_volume_event_e volume_event)
4064 {
4065         int stream_type = -1;
4066         if (VC_SYSTEM_VOLUME_EVENT_CHANGE_FOR_FARFIELD == volume_event) {
4067                 stream_type = SOUND_STREAM_TYPE_VOICE_RECOGNITION;
4068         } else if (VC_SYSTEM_VOLUME_EVENT_CHANGE_FOR_NEARFIELD == volume_event) {
4069                 stream_type = SOUND_STREAM_TYPE_VOICE_RECOGNITION_SERVICE;
4070         } else {
4071                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] volume type is invalid, type(%d)", volume_event);
4072                 return VC_ERROR_INVALID_PARAMETER;
4073         }
4074
4075         SLOG(LOG_INFO, TAG_VCM, "[INFO] Change system volume, volume_type(%d)", volume_event);
4076
4077         int ret = VC_ERROR_NONE;
4078         /* destroy virtual - destroy stream info - create stream info - create virtual */
4079         if (g_virtual_sound_stream_h) {
4080                 SLOG(LOG_INFO, TAG_VCM, "[INFO] Virtual stream is already created, destroy virtual stream)");
4081                 ret = sound_manager_stop_virtual_stream(g_virtual_sound_stream_h);
4082                 if (0 != ret) {
4083                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] Fail to stop virtual stream, ret(%d)", ret);
4084                 }
4085                 ret = sound_manager_destroy_virtual_stream(g_virtual_sound_stream_h);
4086                 if (0 != ret) {
4087                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to destroy virtual stream, ret(%d)", ret);
4088                 }
4089                 g_virtual_sound_stream_h = NULL;
4090         }
4091
4092         if (g_stream_for_volume_h) {
4093                 SLOG(LOG_INFO, TAG_VCM, "[INFO] Stream is already created, destroy stream)");
4094                 ret = sound_manager_destroy_stream_information(g_stream_for_volume_h);
4095                 if (0 != ret) {
4096                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to destroy stream information, ret(%d)", ret);
4097                 }
4098                 g_stream_for_volume_h = NULL;
4099         }
4100
4101         ret = sound_manager_create_stream_information_internal(stream_type, NULL, NULL, &g_stream_for_volume_h);
4102         if (0 != ret) {
4103                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create stream information, ret(%d)", ret);
4104                 return VC_ERROR_OPERATION_FAILED;
4105         }
4106
4107         ret = sound_manager_create_virtual_stream(g_stream_for_volume_h, &g_virtual_sound_stream_h);
4108         if (0 != ret) {
4109                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to create virtual stream, ret(%d)", ret);
4110                 ret = sound_manager_destroy_stream_information(g_stream_for_volume_h);
4111                 if (0 != ret) {
4112                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to destroy stream information, ret(%d)", ret);
4113                 }
4114                 g_stream_for_volume_h = NULL;
4115                 return VC_ERROR_OPERATION_FAILED;
4116         }
4117
4118         ret = sound_manager_start_virtual_stream(g_virtual_sound_stream_h);
4119         if (0 != ret) {
4120                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to start virtual stream, ret(%d)", ret);
4121                 ret = sound_manager_destroy_virtual_stream(g_virtual_sound_stream_h);
4122                 if (0 != ret) {
4123                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to destroy virtual stream, ret(%d)", ret);
4124                 }
4125                 g_virtual_sound_stream_h = NULL;
4126
4127                 ret = sound_manager_destroy_stream_information(g_stream_for_volume_h);
4128                 if (0 != ret) {
4129                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to destroy stream information, ret(%d)", ret);
4130                 }
4131                 g_stream_for_volume_h = NULL;
4132
4133                 return VC_ERROR_OPERATION_FAILED;
4134         }
4135
4136         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Change system volume");
4137         return VC_ERROR_NONE;
4138 }
4139
4140 int __vc_recover_system_volume()
4141 {
4142         SLOG(LOG_INFO, TAG_VCM, "[INFO] Recover system volume");
4143
4144         int ret = VC_ERROR_NONE;
4145
4146         if (g_virtual_sound_stream_h) {
4147                 ret = sound_manager_stop_virtual_stream(g_virtual_sound_stream_h);
4148                 if (0 != ret) {
4149                         SLOG(LOG_WARN, TAG_VCM, "[WARNING] Fail to stop virtual stream, ret(%d)", ret);
4150                 }
4151                 ret = sound_manager_destroy_virtual_stream(g_virtual_sound_stream_h);
4152                 if (0 != ret) {
4153                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to destroy virtual stream, ret(%d)", ret);
4154                         return VC_ERROR_OPERATION_FAILED;
4155                 }
4156                 g_virtual_sound_stream_h = NULL;
4157         }
4158
4159         if (g_stream_for_volume_h) {
4160                 ret = sound_manager_destroy_stream_information(g_stream_for_volume_h);
4161                 if (0 != ret) {
4162                         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to destroy stream information, ret(%d)", ret);
4163                         return VC_ERROR_OPERATION_FAILED;
4164                 }
4165                 g_stream_for_volume_h = NULL;
4166         }
4167
4168         SLOG(LOG_INFO, TAG_VCM, "[SUCCESS] Recover system volume");
4169         return VC_ERROR_NONE;
4170 }
4171
4172 int vc_mgr_change_system_volume(vc_system_volume_event_e event)
4173 {
4174         SLOG(LOG_INFO, TAG_VCM, "[Manager] Change system volume");
4175
4176         if (0 != __vc_mgr_get_feature_enabled()) {
4177                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
4178                 return VC_ERROR_NOT_SUPPORTED;
4179         }
4180
4181         if (0 != __vc_mgr_check_privilege()) {
4182                 return VC_ERROR_PERMISSION_DENIED;
4183         }
4184
4185         if (VC_SYSTEM_VOLUME_EVENT_CHANGE_FOR_NEARFIELD > event || VC_SYSTEM_VOLUME_EVENT_CHANGE_FOR_FARFIELD < event) {
4186                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] event is invalid parameter (%d)", event);
4187                 return VC_ERROR_INVALID_PARAMETER;
4188         }
4189
4190         vc_state_e state;
4191         int ret = vc_mgr_client_get_client_state(g_vc_m, &state);
4192         if (0 != ret) {
4193                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
4194                 return VC_ERROR_INVALID_STATE;
4195         }
4196
4197         if (state != VC_STATE_READY && state != VC_STATE_INITIALIZED) {
4198                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current state is not 'READY' and not 'INITIALIZED', state(%d)", state);
4199                 return VC_ERROR_INVALID_STATE;
4200         }
4201
4202         ret = __vc_change_system_volume(event);
4203         if (0 != ret)
4204                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to change volume");
4205         else
4206                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Success to change volume");
4207         return ret;
4208 }
4209
4210 int vc_mgr_recover_system_volume(void)
4211 {
4212         SLOG(LOG_INFO, TAG_VCM, "[Manager] recover system volume");
4213
4214         if (0 != __vc_mgr_get_feature_enabled()) {
4215                 SLOG(LOG_DEBUG, TAG_VCM, "@@@ [Manager] not supported");
4216                 return VC_ERROR_NOT_SUPPORTED;
4217         }
4218
4219         if (0 != __vc_mgr_check_privilege()) {
4220                 return VC_ERROR_PERMISSION_DENIED;
4221         }
4222
4223         vc_state_e state = VC_STATE_NONE;
4224         int ret = vc_mgr_client_get_client_state(g_vc_m, &state);
4225         if (0 != ret) {
4226                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] A handle is not available");
4227                 return VC_ERROR_INVALID_STATE;
4228         }
4229
4230         if (state != VC_STATE_READY && state != VC_STATE_INITIALIZED) {
4231                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Invalid State: Current state is not 'READY' and not 'INITIALIZED', state(%d)", state);
4232                 return VC_ERROR_INVALID_STATE;
4233         }
4234
4235         /* recover volume */
4236         ret = __vc_recover_system_volume();
4237         if (0 != ret)
4238                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to recover volume");
4239         else
4240                 SLOG(LOG_INFO, TAG_VCM, "[DEBUG] Success to recover volume");
4241         return ret;
4242 }