Unify bundle data in client, mgr, and widget
[platform/core/uifw/voice-control.git] / server / vcd_tidl.c
1 /*
2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <bundle.h>
18
19 #include "vcd_client_data.h"
20 #include "vcd_main.h"
21 #include "vcd_server.h"
22 #include "vcd_config.h"
23
24 #include "vcd_tidl.h"
25 #include "vcd_stub.h"
26 #include "vcd_setting_stub.h"
27 #include "vcd_mgr_stub.h"
28 #include "vcd_mgr_proxy.h"
29 #include "vcd_widget_stub.h"
30 #include "vcd_widget_proxy.h"
31
32 static rpc_port_stub_vcd_mgr_stub_vc_mgr_callback_s g_mgr_callback;
33
34 static pthread_mutex_t g_mgr_tidl_info_mutex = PTHREAD_MUTEX_INITIALIZER;
35
36 static rpc_port_stub_vcd_widget_stub_vc_widget_callback_s g_widget_callback;
37
38 static pthread_mutex_t g_widget_tidl_info_mutex = PTHREAD_MUTEX_INITIALIZER;
39
40 static rpc_port_stub_vcd_stub_vc_callback_s g_client_callback;
41
42 static pthread_mutex_t g_client_tidl_info_mutex = PTHREAD_MUTEX_INITIALIZER;
43
44 static rpc_port_stub_vcd_setting_stub_vc_setting_callback_s g_setting_callback;
45
46 static pthread_mutex_t g_setting_tidl_info_mutex = PTHREAD_MUTEX_INITIALIZER;
47
48 static int g_volume_count = 0;
49
50 extern int __client_tidl_open_connection();
51 extern int __client_tidl_close_connection();
52 extern int __mgr_tidl_open_connection();
53 extern int __mgr_tidl_close_connection();
54 extern int __widget_tidl_open_connection();
55 extern int __widget_tidl_close_connection();
56 extern int __setting_tidl_open_connection();
57 extern int __setting_tidl_close_connection();
58
59
60 /* For common request */
61 static void __request_tidl_connect(vcd_client_type_e type, int pid)
62 {
63         char* type_str = NULL;
64         int ret = -1;
65
66         if (VCD_CLIENT_TYPE_MANAGER == type) {
67                 manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
68                 if (NULL == mgr_tidl_info) {
69                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] manager tidl info not allocated");
70                         return;
71                 }
72
73                 if (mgr_tidl_info->connection_requesting) {
74                         return;
75                 }
76
77                 ret = rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_connect(mgr_tidl_info->rpc_h);
78
79                 if (0 == ret) {
80                         mgr_tidl_info->connection_requesting = true;
81                 }
82                 type_str = "manager";
83         // TODO: uncomment after client done
84         // } else if (VCD_CLIENT_TYPE_NORMAL == type) {
85         //      client_tidl_info_s* client_tidl_info = vcd_client_client_get_tidl_info(pid);
86         //      if (NULL == client_tidl_info) {
87         //              SLOG(LOG_ERROR, TAG_VCD, "[TIDL] client tidl info not allocated");
88         //              return;
89         //      }
90
91         //      if (client_tidl_info->connection_requesting) {
92         //              return;
93         //      }
94
95         //      ret = rpc_port_proxy_vcd_client_connect(client_tidl_info->rpc_h);
96
97         //      if (0 == ret) {
98         //      SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
99         //              client_tidl_info->connection_requesting = true;
100         //      }
101         //      type_str = "client";
102         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
103                 widget_tidl_info_s* widget_tidl_info = vcd_client_widget_get_tidl_info(pid);
104                 if (NULL == widget_tidl_info) {
105                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] widget tidl info not allocated");
106                         return;
107                 }
108
109                 if (widget_tidl_info->connection_requesting) {
110                         return;
111                 }
112
113                 ret = rpc_port_proxy_vcd_widget_proxy_vcd_widget_connect(widget_tidl_info->rpc_h);
114
115                 if (0 == ret) {
116                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
117                         widget_tidl_info->connection_requesting = true;
118                 }
119                 type_str = "widget";
120         } else {
121                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] client type is NOT valid(%d)", type);
122                 return;
123         }
124
125         if (RPC_PORT_ERROR_NONE != ret) {
126                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Request connection to %s stub. ret(%d)", type_str, ret);
127         } else {
128                 SLOG(LOG_INFO, TAG_VCD, "[INFO] Request connection to %s stub. ret(%d)", type_str, ret);
129         }
130 }
131
132 void __send_msg(bundle* msg, vcd_client_type_e type, int pid)
133 {
134         SLOG(LOG_INFO, TAG_VCD, "[TIDL] send msg start");
135
136         if (VCD_CLIENT_TYPE_MANAGER == type) {
137                 pthread_mutex_lock(&g_mgr_tidl_info_mutex);
138
139                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] message to manager");
140                 manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
141                 if (NULL == mgr_tidl_info) {
142                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
143                         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
144                         return;
145                 }
146
147                 rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_h handle = mgr_tidl_info->notify_cb;
148                 if (NULL == handle) {
149                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] notify callback handle is null");
150                         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
151                         return;
152                 }
153
154                 if (0 != rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_invoke(handle, msg)) {
155                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send msg");
156                         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
157                         return;
158                 }
159                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
160         } else if (VCD_CLIENT_TYPE_NORMAL == type) {
161                 pthread_mutex_lock(&g_client_tidl_info_mutex);
162
163                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] message to client");
164                 client_tidl_info_s* client_tidl_info = vcd_client_get_tidl_info(pid);
165                 if (NULL == client_tidl_info) {
166                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
167                         pthread_mutex_unlock(&g_client_tidl_info_mutex);
168                         return;
169                 }
170
171                 rpc_port_stub_vcd_stub_vc_notify_cb_h handle = client_tidl_info->notify_cb;
172                 if (NULL == handle) {
173                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] notify callback handle is null");
174                         pthread_mutex_unlock(&g_client_tidl_info_mutex);
175                         return;
176                 }
177
178                 if (0 != rpc_port_stub_vcd_stub_vc_notify_cb_invoke(handle, pid, msg)) {
179                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send msg");
180                         pthread_mutex_unlock(&g_client_tidl_info_mutex);
181                         return;
182                 }
183                 pthread_mutex_unlock(&g_client_tidl_info_mutex);
184         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
185                 pthread_mutex_lock(&g_widget_tidl_info_mutex);
186
187                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] message to widget");
188                 widget_tidl_info_s* widget_tidl_info = vcd_client_widget_get_tidl_info(pid);
189                 if (NULL == widget_tidl_info) {
190                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
191                         pthread_mutex_unlock(&g_widget_tidl_info_mutex);
192                         return;
193                 }
194
195                 rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_h handle = widget_tidl_info->notify_cb;
196                 if (NULL == handle) {
197                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] notify callback handle is null");
198                         pthread_mutex_unlock(&g_widget_tidl_info_mutex);
199                         return;
200                 }
201
202                 if (0 != rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_invoke(handle, msg)) {
203                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send msg");
204                         pthread_mutex_unlock(&g_widget_tidl_info_mutex);
205                         return;
206                 }
207                 pthread_mutex_unlock(&g_widget_tidl_info_mutex);
208         } else {
209                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] client type is NOT valid(%d)", type);
210                 return;
211         }
212 }
213
214 void __send_signal(bundle* msg, vcd_client_type_e type)
215 {
216         SLOG(LOG_INFO, TAG_VCD, "[TIDL] send signal start");
217
218         if (VCD_CLIENT_TYPE_MANAGER == type) {
219                 pthread_mutex_lock(&g_mgr_tidl_info_mutex);
220
221                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] message to manager");
222                 manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
223                 if (NULL == mgr_tidl_info) {
224                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
225                         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
226                         return;
227                 }
228
229                 rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_h handle = mgr_tidl_info->notify_cb;
230                 if (NULL == handle) {
231                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] notify callback handle is null");
232                         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
233                         return;
234                 }
235
236                 if (0 != rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_invoke(handle, msg)) {
237                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send msg");
238                         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
239                         return;
240                 }
241                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
242         } /* signal to MANAGER */
243         else if (VCD_CLIENT_TYPE_NORMAL == type) {
244                 pthread_mutex_lock(&g_client_tidl_info_mutex);
245                 // get all pids
246                 int* client_list = NULL;
247                 int client_count = 0;
248                 int ret = -1;
249                 ret = vcd_client_get_tidl_list(&client_list, &client_count);
250
251                 client_tidl_info_s* client_tidl_info = NULL;
252                 int pid = -1;
253
254                 if (0 == ret && 0 < client_count) {
255                         for (int i = 0 ; i < client_count ; i++) {
256                                 pid = client_list[i];
257
258                                 client_tidl_info = vcd_client_get_tidl_info(pid);
259                                 if (NULL == client_tidl_info) {
260                                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get tidl info. pid(%d)", pid);
261                                         continue;
262                                 }
263
264                                 if (0 != rpc_port_stub_vcd_stub_vc_notify_cb_invoke(client_tidl_info->notify_cb, pid, msg)) {
265                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send msg");
266                                         continue;
267                                 }
268                         }
269                 }
270                 pthread_mutex_unlock(&g_client_tidl_info_mutex);
271         } /* signal to clients */
272         else if (VCD_CLIENT_TYPE_WIDGET == type) {
273                 pthread_mutex_lock(&g_widget_tidl_info_mutex);
274                 GSList *iter = NULL;
275                 GSList* widget_list = vcd_client_widget_get_tidl_info_list();
276
277                 int count = g_slist_length(widget_list);
278                 int i;
279
280                 widget_tidl_info_s *widget_tidl_info = NULL;
281
282                 if (0 < count) {
283                         iter = g_slist_nth(widget_list, 0);
284                         for (i = 0; i < count; i++) {
285                                 if (NULL == iter) {
286                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] widget list iterator broken");
287                                         break;
288                                 }
289
290                                 widget_tidl_info = iter->data;
291
292                                 if (NULL == widget_tidl_info) {
293                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] widget list data broken");
294                                         break;
295                                 }
296
297                                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] message to widget");
298
299                                 rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_h handle = widget_tidl_info->notify_cb;
300                                 if (NULL == handle) {
301                                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] notify callback handle is null");
302                                         break;
303                                 }
304
305                                 if (0 != rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_invoke(handle, msg)) {
306                                         SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send msg");
307                                         break;
308                                 }
309
310                                 iter = g_slist_next(iter);
311                         }
312                 }
313                 pthread_mutex_unlock(&g_widget_tidl_info_mutex);
314         } /* signal to widgets */
315 }
316
317
318 /**
319  * TIDL callback functions for VC client
320  */
321 static void __vc_create_cb(rpc_port_stub_vcd_stub_vc_context_h context, void *user_data)
322 {
323         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_create_cb");
324
325         char *sender = NULL;
326
327         rpc_port_stub_vcd_stub_vc_context_get_sender(context, &sender);
328         if (!sender) {
329                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Sender is NULL");
330                 return;
331         }
332
333         SLOG(LOG_INFO, TAG_VCD, "[TIDL] sender(%s)", sender);           // sender (app_id)
334         free(sender);
335 }
336
337 static void __vc_terminate_cb(rpc_port_stub_vcd_stub_vc_context_h context, void *user_data)
338 {
339         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_terminate_cb");
340
341         void* tag = NULL;
342         rpc_port_stub_vcd_stub_vc_context_get_tag(context, &tag);
343
344         if (NULL != tag) {
345                 int pid = (uintptr_t)tag;
346                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ VC FINALIZE. pid(%u)", pid);
347
348                 pthread_mutex_lock(&g_client_tidl_info_mutex);
349                 client_tidl_info_s* tidl_info = vcd_client_get_tidl_info(pid);
350                 if (NULL == tidl_info) {
351                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get client tidl info.");
352                         pthread_mutex_unlock(&g_client_tidl_info_mutex);
353                         return;
354                 }
355
356                 if (0 != vcd_client_unset_tidl_notify_cb(pid)) {
357                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to unset notify callback");
358                 }
359                 if (0 != vcd_client_unset_tidl_feedback_cb(pid)) {
360                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to unset feedback callback");
361                 }
362
363                 if (0 != vcd_client_delete_tidl_info(pid)) {
364                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to delete client tidl info");
365                 }
366                 tidl_info = NULL;
367
368                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
369
370                 pthread_mutex_unlock(&g_client_tidl_info_mutex);
371
372         }
373         rpc_port_stub_vcd_stub_vc_context_set_tag(context, NULL);
374 }
375
376 static void __vc_register_notify_cb_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, rpc_port_stub_vcd_stub_vc_notify_cb_h callback, void *user_data)
377 {
378         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_register_notify_cb_cb. pid(%d)", pid);
379
380         if (NULL == callback) {
381                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] callback is null.");
382                 return;
383         }
384
385         int ret = -1;
386         ret = vcd_client_add_tidl_info(pid);
387         if (0 != ret) {
388                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to add tidl info.");
389                 return;
390         }
391
392         ret = vcd_client_set_tidl_notify_cb(pid, callback, user_data);
393         if (0 != ret) {
394                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set notify callback.");
395         } else {
396                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set notify callback.");
397         }
398 }
399
400 static int __vc_register_notify_cb_sync_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, rpc_port_stub_vcd_stub_vc_notify_cb_h callback, void *user_data)
401 {
402         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_register_notify_cb_sync_cb. pid(%d)", pid);
403
404         if (NULL == callback) {
405                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] callback is null.");
406                 return VCD_ERROR_INVALID_PARAMETER;
407         }
408
409         int ret = -1;
410         ret = vcd_client_add_tidl_info(pid);
411         if (0 != ret) {
412                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to add tidl info.");
413                 return ret;
414         }
415         
416         ret = vcd_client_set_tidl_notify_cb(pid, callback, user_data);
417         if (0 != ret) {
418                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set notify callback.");
419         } else {
420                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set notify callback.");
421         }
422
423         return ret;
424 }
425
426 static void __vc_register_feedback_cb_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, rpc_port_stub_vcd_stub_vc_feedback_cb_h callback, void *user_data)
427 {
428         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_register_feedback_cb_cb. pid(%d)", pid);
429
430         if (NULL == callback) {
431                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] callback is null.");
432                 return;
433         }
434
435         int ret = -1;
436         ret = vcd_client_add_tidl_info(pid);
437         if (0 != ret) {
438                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to add tidl info.");
439                 return;
440         }
441
442         ret = vcd_client_set_tidl_feedback_cb(pid, callback, user_data);
443         if (0 != ret) {
444                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set feedback callback.");
445         } else {
446                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set feedback callback.");
447         }
448 }
449
450 static int __vc_register_feedback_cb_sync_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, rpc_port_stub_vcd_stub_vc_feedback_cb_h callback, void *user_data)
451 {
452         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_register_feedback_cb_sync_cb. pid(%d)", pid);
453
454         if (NULL == callback) {
455                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] callback is null.");
456                 return VCD_ERROR_INVALID_PARAMETER;
457         }
458
459         int ret = -1;
460         ret = vcd_client_add_tidl_info(pid);
461         if (0 != ret) {
462                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to add tidl info.");
463                 return ret;
464         }
465         
466         ret = vcd_client_set_tidl_feedback_cb(pid, callback, user_data);
467         if (0 != ret) {
468                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set feedback callback.");
469         } else {
470                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set feedback callback.");
471         }
472
473         return ret;
474 }
475
476 static int __vc_initialize_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int *mgr_pid, int *service_state, int *daemon_pid, void *user_data)
477 {
478         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_initialize_cb. pid(%d)", pid);
479
480         int ret = VCD_ERROR_OPERATION_FAILED;
481
482         uintptr_t ptr_pid = pid;
483         rpc_port_stub_vcd_stub_vc_context_set_tag(context, (void*)ptr_pid);
484
485         ret = vcd_server_initialize(pid);
486         *service_state = vcd_server_get_service_state();
487         *daemon_pid = getpid();
488         *mgr_pid = vcd_client_manager_get_pid();
489
490         if (VCD_ERROR_NONE == ret) {
491                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd initialize : pid(%d) daemon_pid(%d)", pid, *daemon_pid);
492         } else {
493                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd initialize : pid(%d) daemon_pid(%d) ret(%d)", pid, *daemon_pid, ret);
494         }
495
496         return ret;
497 }
498
499 static int __vc_finalize_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, void *user_data)
500 {
501         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_finalize_cb");
502
503         int ret = VCD_ERROR_OPERATION_FAILED;
504
505         ret = vcd_server_finalize(pid);
506         if (VCD_ERROR_NONE == ret) {
507                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd finalize : pid(%d)", pid);
508         } else {
509                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd finalize : pid(%d) ret(%d)", pid, ret);
510         }
511
512         return ret;
513 }
514
515 static int __vc_set_command_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int cmd_type, void *user_data)
516 {
517         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_set_command_cb");
518
519         int ret = VCD_ERROR_OPERATION_FAILED;
520
521         ret = vcd_server_set_command(pid, (vc_cmd_type_e)cmd_type);
522         if (VCD_ERROR_NONE == ret) {
523                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd set command : pid(%d), cmd_type(%d)", pid, cmd_type);
524         } else {
525                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd set command : pid(%d), cmd_type(%d) ret(%d)", pid, cmd_type, ret);
526         }
527
528         return ret;
529 }
530
531 static int __vc_unset_command_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int cmd_type, void *user_data)
532 {
533         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_unset_command_cb");
534
535         int ret = VCD_ERROR_OPERATION_FAILED;
536
537         ret = vcd_server_unset_command(pid, (vc_cmd_type_e)cmd_type);
538         if (VCD_ERROR_NONE == ret) {
539                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd unset command : pid(%d), cmd_type(%d)", pid, cmd_type);
540         } else {
541                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd unset command : pid(%d), cmd_type(%d) ret(%d)", pid, cmd_type, ret);
542         }
543         
544         return ret;
545 }
546
547 static int __vc_set_foreground_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, bool value, void *user_data)
548 {
549         // check more...
550         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_set_foreground_cb");
551
552         int ret = VCD_ERROR_OPERATION_FAILED;
553
554         ret = vcd_server_set_foreground(pid, value);
555         if (VCD_ERROR_NONE != ret) {
556                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd set foreground : pid(%d) ret(%d)", pid, ret);
557                 return ret;
558         }
559         
560         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd set foreground : pid(%d)", pid);
561         vcdc_send_request_set_foreground(pid, value);
562
563         return ret;
564 }
565
566 static int __vc_set_server_dialog_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, const char *app_id, const char *credential, void *user_data)
567 {
568         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_set_server_dialog_cb");
569
570         int ret = VCD_ERROR_OPERATION_FAILED;
571
572         ret = vcd_server_set_server_dialog(pid, app_id, credential);
573         if (VCD_ERROR_NONE == ret) {
574                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd set server dialog : pid(%d), app_id(%s)", pid, app_id);
575         } else {
576                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd set server dialog : pid(%d), app_id(%s) ret(%d)", pid, app_id, ret);
577         }
578         
579         return ret;
580 }
581
582 static int __vc_request_dialog_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, const char *disp_text, const char *utt_text, bool continuous, void *user_data)
583 {
584         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_request_dialog_cb");
585
586         int ret = VCD_ERROR_OPERATION_FAILED;
587
588         ret = vcd_server_dialog(pid, disp_text, utt_text, continuous);
589         if (VCD_ERROR_NONE == ret) {
590                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd request dialog : pid(%d), disp_text(%s), utt_text(%s), continuous(%d)", pid, disp_text, utt_text, continuous);
591         } else {
592                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd request dialog : pid(%d), disp_text(%s), utt_text(%s), continuous(%d), ret(%d)", pid, disp_text, utt_text, continuous, ret);
593         }
594         
595         return ret;
596 }
597
598 static int __vc_is_system_command_valid_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, bool *is_sys_cmd_valid, void *user_data)
599 {
600         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_is_system_command_valid_cb");
601
602         int ret = VCD_ERROR_OPERATION_FAILED;
603
604         ret = vcd_server_is_system_command_valid(pid, (int*)is_sys_cmd_valid);
605         if (VCD_ERROR_NONE == ret) {
606                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd check system command is valid : pid(%d), is_sys_cmd_valid(%d)", pid, *is_sys_cmd_valid);
607         } else {
608                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd check system command is valid : pid(%d), ret(%d)", pid, ret);
609         }
610         
611         return ret;
612 }
613
614 static int __vc_auth_enable_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int mgr_pid, void *user_data)
615 {
616         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_auth_enable_cb. Send a request to vc_mgr");
617
618         int ret = VCD_ERROR_OPERATION_FAILED;
619         ret = vcdc_send_request_auth_enable(pid);
620         if (VCD_ERROR_NONE == ret) {
621                 SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] Send request auth enable. pid(%d), mgr_pid(%d)", pid, mgr_pid);
622         } else {
623                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send request auth enable. ret(%d), pid(%d), mgr_pid(%d)", ret, pid, mgr_pid);
624         }
625
626         return ret;
627 }
628
629 static int __vc_auth_disable_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int mgr_pid, void *user_data)
630 {
631         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_auth_disable_cb. Send a request to vc_mgr");
632
633         int ret = VCD_ERROR_OPERATION_FAILED;
634         ret = vcdc_send_request_auth_disable(pid);
635         if (VCD_ERROR_NONE == ret) {
636                 SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] Send request auth disable. pid(%d), mgr_pid(%d)", pid, mgr_pid);
637         } else {
638                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send request auth disable. ret(%d), pid(%d), mgr_pid(%d)", ret, pid, mgr_pid);
639         }
640
641         return ret;
642 }
643
644 static int __vc_auth_start_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int mgr_pid, void *user_data)
645 {
646         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_auth_start_cb. Send a request to vc_mgr");
647
648         int ret = VCD_ERROR_OPERATION_FAILED;
649         ret = vcdc_send_request_auth_start(pid);
650         if (VCD_ERROR_NONE == ret) {
651                 SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] Send request auth start. pid(%d), mgr_pid(%d)", pid, mgr_pid);
652         } else {
653                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send request auth start. ret(%d), pid(%d), mgr_pid(%d)", ret, pid, mgr_pid);
654         }
655
656         return ret;
657 }
658
659 static int __vc_auth_stop_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int mgr_pid, void *user_data)
660 {
661         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_auth_stop_cb. Send a request to vc_mgr");
662
663         int ret = VCD_ERROR_OPERATION_FAILED;
664         ret = vcdc_send_request_auth_stop(pid);
665         if (VCD_ERROR_NONE == ret) {
666                 SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] Send request auth stop. pid(%d), mgr_pid(%d)", pid, mgr_pid);
667         } else {
668                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send request auth stop. ret(%d), pid(%d), mgr_pid(%d)", ret, pid, mgr_pid);
669         }
670
671         return ret;
672 }
673
674 static int __vc_auth_cancel_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int mgr_pid, void *user_data)
675 {
676         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_auth_cancel_cb. Send a request to vc_mgr");
677
678         int ret = VCD_ERROR_OPERATION_FAILED;
679         ret = vcdc_send_request_auth_cancel(pid);
680         if (VCD_ERROR_NONE == ret) {
681                 SLOG(LOG_INFO, TAG_VCD, "[SUCCESS] Send request auth cancel. pid(%d), mgr_pid(%d)", pid, mgr_pid);
682         } else {
683                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send request auth cancel. ret(%d), pid(%d), mgr_pid(%d)", ret, pid, mgr_pid);
684         }
685
686         return ret;
687 }
688
689 static int __vc_request_tts_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, const char *text, const char *lang, bool to_vcm, int *utt_id, void *user_data)
690 {
691         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_request_tts_cb");
692
693         int ret = VCD_ERROR_OPERATION_FAILED;
694
695         ret = vcd_server_request_tts(pid, text, lang, to_vcm, utt_id);
696         if (VCD_ERROR_NONE == ret) {
697                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd request tts : pid(%d), text(%s), language(%s), to_vcm(%d)", pid, text, lang, to_vcm);
698         } else {
699                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd request tts : pid(%d), text(%s), language(%s), to_vcm(%d), ret(%d)", pid, text, lang, to_vcm, ret);
700         }
701         
702         return ret;
703 }
704
705 static int __vc_cancel_tts_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int utt_id, void *user_data)
706 {
707         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_cancel_tts_cb");
708
709         int ret = VCD_ERROR_OPERATION_FAILED;
710
711         ret = vcd_server_cancel_tts(pid, utt_id);
712         if (VCD_ERROR_NONE == ret) {
713                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd cancel tts : pid(%d), utt_id(%d)", pid, utt_id);
714         } else {
715                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd cancel tts : pid(%d), utt_id(%d), ret(%d)", pid, utt_id, ret);
716         }
717         
718         return ret;
719 }
720
721 static int __vc_get_tts_audio_format_cb(rpc_port_stub_vcd_stub_vc_context_h context, int pid, int *rate, int *channel, int *audio_type, void *user_data)
722 {
723         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_get_tts_audio_format_cb");
724
725         int ret = VCD_ERROR_OPERATION_FAILED;
726
727         ret = vcd_server_get_tts_audio_format(pid, rate, channel, audio_type);
728         if (VCD_ERROR_NONE == ret) {
729                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd get tts audio format : pid(%d), rate(%d), channel(%d), audio_type(%d)", pid, *rate, *channel, *audio_type);
730         } else {
731                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to vcd get tts audio format : pid(%d) ret(%d)", pid, ret);
732         }
733         
734         return ret;
735 }
736
737 int __client_tidl_open_connection()
738 {
739         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __client_tidl_open_connection");
740
741         g_client_callback.create = __vc_create_cb;
742         g_client_callback.terminate = __vc_terminate_cb;
743         g_client_callback.register_notify_cb = __vc_register_notify_cb_cb;
744         g_client_callback.register_notify_cb_sync = __vc_register_notify_cb_sync_cb;
745         g_client_callback.register_feedback_cb = __vc_register_feedback_cb_cb;
746         g_client_callback.register_feedback_cb_sync = __vc_register_feedback_cb_sync_cb;
747         g_client_callback.initialize = __vc_initialize_cb;
748         g_client_callback.finalize = __vc_finalize_cb;
749         g_client_callback.set_command = __vc_set_command_cb;
750         g_client_callback.unset_command = __vc_unset_command_cb;
751         g_client_callback.set_foreground = __vc_set_foreground_cb;
752         g_client_callback.set_server_dialog = __vc_set_server_dialog_cb;
753         g_client_callback.request_dialog = __vc_request_dialog_cb;
754         g_client_callback.is_system_command_valid = __vc_is_system_command_valid_cb;
755         g_client_callback.auth_enable = __vc_auth_enable_cb;
756         g_client_callback.auth_disable = __vc_auth_disable_cb;
757         g_client_callback.auth_start = __vc_auth_start_cb;
758         g_client_callback.auth_stop = __vc_auth_stop_cb;
759         g_client_callback.auth_cancel = __vc_auth_cancel_cb;
760         g_client_callback.request_tts = __vc_request_tts_cb;
761         g_client_callback.cancel_tts = __vc_cancel_tts_cb;
762         g_client_callback.get_tts_audio_format = __vc_get_tts_audio_format_cb;
763
764         int ret = -1;
765         ret = rpc_port_stub_vcd_stub_vc_register(&g_client_callback, NULL);
766         if (VCD_ERROR_NONE != ret) {
767                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to register TIDL callbacks. ret(%d)", ret);
768                 return VCD_ERROR_OPERATION_FAILED;
769         }
770
771         return VCD_ERROR_NONE;
772 }
773
774 int __client_tidl_close_connection()
775 {
776         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __client_tidl_close_connection");
777         rpc_port_stub_vcd_stub_vc_unregister();
778
779         return VCD_ERROR_NONE;
780 }
781
782 /**
783  * TIDL callback functions for VC manager
784  */
785 static void __mgr_on_connected(rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_h h, void *user_data)
786 {
787         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
788
789         if (NULL == mgr_tidl_info) {
790                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
791                 return;
792         }
793
794         mgr_tidl_info->connected = true;
795         mgr_tidl_info->connection_requesting = false;
796
797         SLOG(LOG_INFO, TAG_VCD, "Connected to manager");
798 }
799
800 static void __mgr_on_disconnected(rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_h h, void *user_data)
801 {
802         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
803
804         if (NULL == mgr_tidl_info) {
805                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
806                 return;
807         }
808
809         mgr_tidl_info->connected = false;
810         mgr_tidl_info->connection_requesting = false;
811
812         SLOG(LOG_INFO, TAG_VCD, "Disonnected to manager");
813 }
814
815 static void __mgr_on_rejected(rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_h h, void *user_data)
816 {
817         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
818
819         if (NULL == mgr_tidl_info) {
820                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
821                 return;
822         }
823
824         mgr_tidl_info->connection_requesting = false;
825
826         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Rejected from manager");
827 }
828
829 static rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_h __mgr_create_rpc_port(const char* engine_app_id)
830 {
831         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] __mgr_create_rpc_port");
832         rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_callback_s rpc_callback = {
833                 .connected = __mgr_on_connected,
834                 .disconnected = __mgr_on_disconnected,
835                 .rejected = __mgr_on_rejected
836         };
837
838         rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_h handle = NULL;
839         if (0 != rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_create(engine_app_id, &rpc_callback, NULL, &handle)) {
840                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create proxy");
841                 return NULL;
842         }
843
844         return handle;
845 }
846
847 static void __vc_mgr_create_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, void *user_data)
848 {
849         char *sender = NULL;
850
851         rpc_port_stub_vcd_mgr_stub_vc_mgr_context_get_sender(context, &sender);
852         if (!sender){
853                 SLOG(LOG_ERROR, TAG_VCD, "Sender is NULL");
854                 return;
855         }
856
857         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Manager connect. appid(%s)", sender);
858
859         free(sender);
860 }
861
862 static void __vc_mgr_terminate_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, void *user_data)
863 {
864         pthread_mutex_lock(&g_mgr_tidl_info_mutex);
865         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
866         if (NULL == mgr_tidl_info) {
867                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get manager tidl info.");
868                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
869                 return;
870         }
871
872         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VC MANAGER FINALIZE. pid(%u)", mgr_tidl_info->pid);
873
874         if (0 != vcd_client_manager_unset_tidl_notify_cb()) {
875                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to unset notify callback");
876         }
877
878         if (0 != vcd_client_manager_unset_tidl_send_buffer_cb()) {
879                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to unset send buffer callback");
880         }
881
882         if (0 != vcd_client_manager_delete_tidl_info()) {
883                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to delete manager tidl info");
884         }
885         mgr_tidl_info = NULL;
886
887         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
888
889         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
890
891
892         char *sender = NULL;
893         rpc_port_stub_vcd_mgr_stub_vc_mgr_context_get_sender(context, &sender);
894         if (!sender)
895                 return;
896
897         SLOG(LOG_INFO, TAG_VCD, "@@@ Manager disconnect. appid(%s)", sender);
898
899         free(sender);
900 }
901
902 static void  __vc_mgr_register_cb_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, rpc_port_stub_vcd_mgr_stub_vc_mgr_notify_cb_h notify_callback, rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_h send_buffer_callback, void *user_data)
903 {
904         pthread_mutex_lock(&g_mgr_tidl_info_mutex);
905         SLOG(LOG_INFO, TAG_VCD, "@@@ VC MANAGER REGISTER CALLBACK");
906
907         int ret = -1;
908         ret = vcd_client_manager_create_tidl_info(pid);
909
910         if (0 != ret) {
911                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create manager tidl info.");
912                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
913                 return;
914         }
915
916         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
917
918         if (NULL == mgr_tidl_info) {
919                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get manager tidl info. pid(%d)", pid);
920                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
921                 return;
922         }
923
924         char *sender = NULL;
925         rpc_port_stub_vcd_mgr_stub_vc_mgr_context_get_sender(context, &sender);
926
927         if (!sender){
928                 SLOG(LOG_ERROR, TAG_VCD, "Sender is NULL");
929                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
930                 return;
931         }
932
933         mgr_tidl_info->rpc_h = __mgr_create_rpc_port(sender);
934
935         if (NULL == mgr_tidl_info->rpc_h) {
936                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create server proxy");
937         } else {
938                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] rpc_h(%p), engine_app_id(%s)", mgr_tidl_info->rpc_h, sender);
939         }
940
941         free(sender);
942
943         if (0 != vcd_client_manager_set_tidl_notify_cb(notify_callback, user_data)) {
944                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set notify callback");
945         } else {
946                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set notify callback.");
947         }
948
949         if (0 != vcd_client_manager_set_tidl_send_buffer_cb(send_buffer_callback, user_data)) {
950                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set send buffer callback");
951         } else {
952                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set send buffer callback.");
953         }
954
955         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
956
957         __request_tidl_connect(VCD_CLIENT_TYPE_MANAGER, pid);
958
959         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
960 }
961
962 static int  __vc_mgr_initialize_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, int audio_streaming_mode, int *service_state, int *foreground, int *daemon_pid, void *user_data)
963 {
964         SLOG(LOG_INFO, TAG_VCD, "@@@ VCD Manager Initialize");
965
966         int ret = -1;
967
968         vcd_config_set_audio_streaming_mode((vcd_audio_streaming_mode_e)audio_streaming_mode);
969         ret = vcd_server_mgr_initialize(pid, audio_streaming_mode);
970         if (0 != ret) {
971                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
972                 return ret;
973         }
974         *service_state = vcd_server_get_service_state();
975         *foreground = vcd_server_get_foreground();
976         *daemon_pid = getpid();
977
978         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr initialize : pid(%d) service state(%d) foreground(%d) daemon_pid(%d)", pid, *service_state, *foreground, *daemon_pid);
979         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
980         return ret;
981 }
982
983 static int  __vc_mgr_finalize_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, void *user_data)
984 {
985         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager Finalize");
986         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr finalize : pid(%d)", pid);
987
988         int ret = -1;
989
990         ret = vcd_server_mgr_finalize(pid);
991         if (0 != ret) {
992                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
993         }
994
995         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
996
997         return ret;
998 }
999
1000 static int  __vc_mgr_set_command_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, void *user_data)
1001 {
1002         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager Set command");
1003         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr set command : pid(%d)", pid);
1004
1005         int ret = -1;
1006
1007         ret = vcd_server_mgr_set_command(pid);
1008         if (0 != ret) {
1009                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1010                 return ret;
1011         }
1012
1013         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1014
1015         return ret;
1016 }
1017
1018 static int  __vc_mgr_unset_command_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, void *user_data)
1019 {
1020         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager unset command");
1021         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr unset command : pid(%d)", pid);
1022
1023         int ret = -1;
1024
1025         ret = vcd_server_mgr_unset_command(pid);
1026         if (0 != ret) {
1027                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1028                 return ret;
1029         }
1030
1031         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1032
1033         return ret;
1034 }
1035
1036 static int  __vc_mgr_set_demandable_client_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, void *user_data)
1037 {
1038         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager Set demandable client");
1039         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr set demandable client : pid(%d)", pid);
1040
1041         int ret = -1;
1042
1043         ret = vcd_server_mgr_set_demandable_client(pid);
1044         if (0 != ret) {
1045                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1046                 return ret;
1047         }
1048
1049         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1050
1051         return ret;
1052 }
1053
1054 static int  __vc_mgr_set_audio_type_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, const char *audio_type, void *user_data)
1055 {
1056         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager set audio type");
1057         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr set audio type : pid(%d), audio type(%s)", pid, audio_type);
1058
1059         int ret = -1;
1060
1061         ret = vcd_server_mgr_set_audio_type(pid, audio_type);
1062         if (0 != ret) {
1063                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1064                 return ret;
1065         }
1066
1067         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1068
1069         return ret;
1070 }
1071
1072 static int  __vc_mgr_get_audio_type_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, char **audio_type, void *user_data)
1073 {
1074         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager get audio type");
1075         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr set audio type : pid(%d)", pid);
1076
1077         int ret = -1;
1078         char* tmp_audio_type = NULL;
1079
1080         ret = vcd_server_mgr_get_audio_type(pid, &tmp_audio_type);
1081         if (0 != ret) {
1082                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1083                 free(tmp_audio_type);
1084                 return ret;
1085         }
1086
1087         *audio_type = strdup(tmp_audio_type);
1088         free(tmp_audio_type);
1089
1090         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1091
1092         return ret;
1093 }
1094
1095 static void  __vc_mgr_set_private_data_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, const char *key, const char *data, void *user_data)
1096 {
1097         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager set private data");
1098         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr set domain : pid(%d), key(%s), data(%s)", pid, key, data);
1099
1100         int ret = -1;
1101
1102         ret = vcd_server_mgr_set_private_data(pid, key, data);
1103         if (0 != ret) {
1104                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set private data");
1105                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1106         }
1107
1108         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1109 }
1110
1111 static int  __vc_mgr_get_private_data_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, const char *key, char **data, void *user_data)
1112 {
1113         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager get private data");
1114         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr get private data : pid(%d), key(%s)", pid, key);
1115
1116         int ret = -1;
1117         char *temp_data = NULL;
1118
1119         ret = vcd_server_mgr_get_private_data(pid, key, &temp_data);
1120         if (0 != ret) {
1121                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1122                 return ret;
1123         }
1124         if (NULL == temp_data) {
1125                 SLOG(LOG_INFO, TAG_VCD, "data parameter is NULL");
1126                 temp_data = strdup("#NULL");
1127         }
1128
1129         *data = strdup(temp_data);
1130         free(temp_data);
1131
1132         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1133
1134         return ret;
1135 }
1136
1137 static int  __vc_mgr_set_client_info_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, void *user_data)
1138 {
1139         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager set client info");
1140         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr set client info : pid(%d)", pid);
1141
1142         int ret = -1;
1143
1144         ret = vcd_server_mgr_set_client_info(pid);
1145         if (0 != ret) {
1146                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1147                 return ret;
1148         }
1149
1150         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1151
1152         return ret;
1153 }
1154
1155 static int  __vc_mgr_set_domain_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, const char *domain, void *user_data)
1156 {
1157         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager set domain type");
1158         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr set domain : pid(%d), domain(%s)", pid, domain);
1159
1160         int ret = -1;
1161
1162         ret = vcd_server_mgr_set_domain(pid, domain);
1163         if (0 != ret) {
1164                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1165                 return ret;
1166         }
1167
1168         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1169
1170         return ret;
1171 }
1172
1173 static void  __vc_mgr_do_action_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, int type, const char *send_event, void *user_data)
1174 {
1175         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager request to do action");
1176         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr request to do action : pid(%d), type(%d) send_event(%s)", pid, type, send_event);
1177
1178         int ret = -1;
1179
1180         ret = vcd_server_mgr_do_action(pid, type, send_event);
1181         if (0 != ret) {
1182                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1183         }
1184
1185         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1186 }
1187
1188 static int  __vc_mgr_start_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, int recognition_mode, bool exclusive_command_option, bool start_by_client, int disabled_cmd_type, void *user_data)
1189 {
1190         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager start");
1191         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd check enabled or disabled command types. disabled cmd type(%d)", disabled_cmd_type);
1192
1193         int ret = -1;
1194
1195         vcd_server_mgr_set_disabled_command_type(pid, disabled_cmd_type);
1196
1197         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr start : pid(%d) recognition_mode(%d) exclusive(%d) start by client(%d)", pid, recognition_mode, exclusive_command_option, start_by_client);
1198         ret = vcd_server_mgr_start((vcd_recognition_mode_e)recognition_mode, exclusive_command_option, start_by_client);
1199         if (0 != ret) {
1200                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1201                 return ret;
1202         }
1203
1204         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1205
1206         return ret;
1207 }
1208
1209 static int  __vc_mgr_stop_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, void *user_data)
1210 {
1211         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager stop");
1212         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr stop : pid(%d)", pid);
1213
1214         int ret = -1;
1215
1216         ret = vcd_server_mgr_stop();
1217
1218         if (0 != ret) {
1219                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1220                 return ret;
1221         }
1222
1223         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1224
1225         return ret;
1226 }
1227
1228 static int  __vc_mgr_cancel_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, void *user_data)
1229 {
1230         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager cancel");
1231         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr cancel : pid(%d)", pid);
1232
1233         int ret = -1;
1234
1235         ret = vcd_server_mgr_cancel();
1236         if (0 != ret) {
1237                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1238                 return ret;
1239         }
1240
1241         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1242
1243         return ret;
1244 }
1245
1246 static void  __vc_mgr_set_audio_streaming_mode_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, int mode, void *user_data)
1247 {
1248         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager set audio streaming mode");
1249         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr set audio streaming mode : pid(%d) mode(%d)", pid, mode);
1250
1251         vcd_config_set_audio_streaming_mode((vcd_audio_streaming_mode_e)mode);
1252
1253         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1254 }
1255
1256 static void  __vc_mgr_send_specific_engine_request_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, const char *engine_app_id, const char *event, const char *request, void *user_data)
1257 {
1258         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager specific engine request");
1259
1260         int ret = -1;
1261         gsize decodingSize = 0;
1262         gchar *gDecodedRequest = (gchar *)g_base64_decode((const gchar *)request, &decodingSize);
1263
1264         if (gDecodedRequest) {
1265                 ret = vcd_server_mgr_send_specific_engine_request(pid, engine_app_id, event, gDecodedRequest);
1266                 if (0 != ret) {
1267                         SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1268                 }
1269
1270                 SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr specific engine request : pid(%d), engine_app_id(%s), event(%s), request(%s), ret(%d)", pid, engine_app_id, event, gDecodedRequest, ret);
1271                 g_free(gDecodedRequest);
1272         }
1273
1274         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1275 }
1276
1277 static void  __vc_mgr_send_result_selection_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, void *user_data)
1278 {
1279         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager result selection");
1280         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr result selection : pid(%d)", pid);
1281
1282         vcd_server_mgr_result_select();
1283
1284         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1285 }
1286
1287 static void  __vc_mgr_send_utterance_status_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, int utt_id, int utt_status, void *user_data)
1288 {
1289         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager send utterance status");
1290         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr send utterance status : pid(%d), utt_id(%d), utt_status(%d)", pid, utt_id, utt_status);
1291
1292         pthread_mutex_lock(&g_client_tidl_info_mutex);
1293
1294         SLOG(LOG_INFO, TAG_VCD, "[TIDL] message to client");
1295         client_tidl_info_s* client_tidl_info = vcd_client_get_tidl_info(pid);
1296         if (NULL == client_tidl_info) {
1297                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL] tidl proxy info not allocated");
1298                 pthread_mutex_unlock(&g_client_tidl_info_mutex);
1299                 return;
1300         }
1301
1302         rpc_port_stub_vcd_stub_vc_notify_cb_h handle = client_tidl_info->notify_cb;
1303         if (NULL == handle) {
1304                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL] notify callback handle is null");
1305                 pthread_mutex_unlock(&g_client_tidl_info_mutex);
1306                 return;
1307         }
1308
1309         bundle* msg = bundle_create();
1310         if (NULL == msg) {
1311                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create bundle data");
1312                 pthread_mutex_unlock(&g_client_tidl_info_mutex);
1313                 return;
1314         }
1315
1316         char pid_char[10] = {0};
1317         char uttid_char[10] = {0};
1318         char uttstatus_char[10] = {0};
1319         snprintf(pid_char, 10, "%d", pid);
1320         snprintf(uttid_char, 10, "%d", utt_id);
1321         snprintf(uttstatus_char, 10, "%d", utt_status);
1322
1323         bundle_add_str(msg, VC_BUNDLE_METHOD, VC_MANAGER_METHOD_UTTERANCE_STATUS);
1324         bundle_add_str(msg, VC_BUNDLE_PID, pid_char);
1325         bundle_add_str(msg, VC_BUNDLE_REASON, uttid_char);
1326         bundle_add_str(msg, VC_BUNDLE_ERR_MSG, uttstatus_char);
1327
1328         if (0 != rpc_port_stub_vcd_stub_vc_notify_cb_invoke(handle, pid, msg)) {
1329                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send msg");
1330                 bundle_free(msg);
1331                 pthread_mutex_unlock(&g_client_tidl_info_mutex);
1332                 return;
1333         }
1334
1335         bundle_free(msg);
1336         pthread_mutex_unlock(&g_client_tidl_info_mutex);
1337
1338         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1339 }
1340
1341 static void  __vc_mgr_send_audio_streaming_cb(rpc_port_stub_vcd_mgr_stub_vc_mgr_context_h context, int pid, int event, rpc_port_stub_vcd_mgr_stub_array_char_h data, void *user_data)
1342 {
1343         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Manager send audio streaming");
1344
1345         char* buffer = NULL;
1346         int len = 0;
1347         int ret = -1;
1348         rpc_port_stub_vcd_mgr_stub_array_char_get(data, &buffer, &len);
1349
1350         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd mgr send audio streaming : pid(%d), event(%d), buffer(%p), len(%d)", pid, event, buffer, len);
1351
1352         ret = vcd_server_mgr_send_audio_streaming(pid, event, buffer, (unsigned int)len);
1353         if (0 != ret) {
1354                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1355         }
1356
1357         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1358 }
1359
1360 int __mgr_tidl_open_connection()
1361 {
1362         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] __mgr_tidl_open_connection");
1363
1364         g_mgr_callback.create = __vc_mgr_create_cb;
1365         g_mgr_callback.terminate = __vc_mgr_terminate_cb;
1366         g_mgr_callback.register_cb = __vc_mgr_register_cb_cb;
1367         g_mgr_callback.initialize = __vc_mgr_initialize_cb;
1368         g_mgr_callback.finalize = __vc_mgr_finalize_cb;
1369         g_mgr_callback.set_command = __vc_mgr_set_command_cb;
1370         g_mgr_callback.unset_command = __vc_mgr_unset_command_cb;
1371         g_mgr_callback.demandable_client = __vc_mgr_set_demandable_client_cb;
1372         g_mgr_callback.set_audio_type = __vc_mgr_set_audio_type_cb;
1373         g_mgr_callback.get_audio_type = __vc_mgr_get_audio_type_cb;
1374         g_mgr_callback.set_private_data = __vc_mgr_set_private_data_cb;
1375         g_mgr_callback.get_private_data = __vc_mgr_get_private_data_cb;
1376         g_mgr_callback.set_client_info = __vc_mgr_set_client_info_cb;
1377         g_mgr_callback.set_domain = __vc_mgr_set_domain_cb;
1378         g_mgr_callback.do_action = __vc_mgr_do_action_cb;
1379         g_mgr_callback.start = __vc_mgr_start_cb;
1380         g_mgr_callback.stop = __vc_mgr_stop_cb;
1381         g_mgr_callback.cancel = __vc_mgr_cancel_cb;
1382         g_mgr_callback.set_audio_streaming_mode = __vc_mgr_set_audio_streaming_mode_cb;
1383         g_mgr_callback.send_specific_engine_request = __vc_mgr_send_specific_engine_request_cb;
1384         g_mgr_callback.send_result_selection = __vc_mgr_send_result_selection_cb;
1385         g_mgr_callback.send_utterance_status = __vc_mgr_send_utterance_status_cb;
1386         g_mgr_callback.send_audio_streaming = __vc_mgr_send_audio_streaming_cb;
1387
1388         int ret = -1;
1389         int count = 0;
1390         while (VC_RETRY_MIN_COUNT >= count) {
1391                 ret = rpc_port_stub_vcd_mgr_stub_vc_mgr_register(&g_mgr_callback, NULL);
1392                 if (0 == ret) {
1393                         SLOG(LOG_DEBUG, TAG_VCD, "register callback");
1394                         return VCD_ERROR_NONE;
1395                 }
1396                 usleep(100000);
1397                 count++;
1398         }
1399
1400         SLOG(LOG_ERROR, TAG_VCD, "Fail to register callback(%d)", ret);
1401         return VCD_ERROR_OPERATION_FAILED;
1402 }
1403
1404 int __mgr_tidl_close_connection()
1405 {
1406         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Close connection");
1407         rpc_port_stub_vcd_mgr_stub_vc_mgr_unregister();
1408
1409         return VCD_ERROR_NONE;
1410 }
1411
1412 /**
1413  * TIDL callback functions for VC widget
1414  */
1415 static void __widget_on_connected(rpc_port_proxy_vcd_widget_proxy_vcd_widget_h h, void *user_data)
1416 {
1417         widget_tidl_info_s* widget_tidl_info = vcd_client_widget_get_tidl_info((int)user_data);
1418
1419         widget_tidl_info->connected = true;
1420         widget_tidl_info->connection_requesting = false;
1421
1422         SLOG(LOG_INFO, TAG_VCD, "Connected to widget");
1423 }
1424
1425 static void __widget_on_disconnected(rpc_port_proxy_vcd_widget_proxy_vcd_widget_h h, void *user_data)
1426 {
1427         widget_tidl_info_s* widget_tidl_info = vcd_client_widget_get_tidl_info((int)user_data);
1428
1429         widget_tidl_info->connected = false;
1430         widget_tidl_info->connection_requesting = false;
1431
1432         SLOG(LOG_INFO, TAG_VCD, "Disonnected to widget");
1433 }
1434
1435 static void __widget_on_rejected(rpc_port_proxy_vcd_widget_proxy_vcd_widget_h h, void *user_data)
1436 {
1437         widget_tidl_info_s* widget_tidl_info = vcd_client_widget_get_tidl_info((int)user_data);
1438
1439         widget_tidl_info->connection_requesting = false;
1440
1441         SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Rejected from widget");
1442 }
1443
1444 static rpc_port_proxy_vcd_widget_proxy_vcd_widget_h __widget_create_rpc_port(const char* engine_app_id, int pid)
1445 {
1446         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] __widget_create_rpc_port");
1447         rpc_port_proxy_vcd_widget_proxy_vcd_widget_callback_s rpc_callback = {
1448                 .connected = __widget_on_connected,
1449                 .disconnected = __widget_on_disconnected,
1450                 .rejected = __widget_on_rejected
1451         };
1452
1453         rpc_port_proxy_vcd_widget_proxy_vcd_widget_h handle = NULL;
1454         if (0 != rpc_port_proxy_vcd_widget_proxy_vcd_widget_create(engine_app_id, &rpc_callback, (void *)pid, &handle)) {
1455                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create proxy");
1456                 return NULL;
1457         }
1458
1459         return handle;
1460 }
1461
1462 static void __vc_widget_create_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, void *user_data)
1463 {
1464         char *sender = NULL;
1465
1466         rpc_port_stub_vcd_widget_stub_vc_widget_context_get_sender(context, &sender);
1467         if (!sender) {
1468                 SLOG(LOG_ERROR, TAG_VCD, "Sender is NULL");
1469                 return;
1470         }
1471
1472         SLOG(LOG_DEBUG, TAG_VCD, "@@@ Widget connect. appid(%s)", sender);
1473         free(sender);
1474 }
1475
1476 static void __vc_widget_terminate_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, void *user_data)
1477 {
1478         void* tag = NULL;
1479         rpc_port_stub_vcd_widget_stub_vc_widget_context_get_tag(context, &tag);
1480
1481         if (NULL != tag) {
1482                 int pid = (intptr_t)tag;
1483                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ VC WIDGET FINALIZE. pid(%u)", pid);
1484
1485                 pthread_mutex_lock(&g_widget_tidl_info_mutex);
1486                 widget_tidl_info_s* widget_tidl_info = vcd_client_widget_get_tidl_info(pid);
1487                 if (NULL == widget_tidl_info) {
1488                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get widget tidl info.");
1489                         pthread_mutex_unlock(&g_widget_tidl_info_mutex);
1490                         return;
1491                 }
1492
1493                 if (0 != vcd_client_widget_unset_tidl_notify_cb(pid)) {
1494                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to unset notify callback");
1495                 }
1496
1497                 if (0 != vcd_client_widget_delete_tidl_info(pid)) {
1498                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to delete widget tidl info");
1499                 }
1500                 widget_tidl_info = NULL;
1501
1502                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1503
1504                 pthread_mutex_unlock(&g_widget_tidl_info_mutex);
1505
1506                 char *sender = NULL;
1507                 rpc_port_stub_vcd_widget_stub_vc_widget_context_get_sender(context, &sender);
1508                 if (!sender)
1509                         return;
1510
1511                 SLOG(LOG_INFO, TAG_VCD, "@@@ Manager disconnect. appid(%s)", sender);
1512
1513                 free(sender);
1514         }
1515         rpc_port_stub_vcd_widget_stub_vc_widget_context_set_tag(context, NULL);
1516 }
1517
1518 static void  __vc_widget_register_cb_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, rpc_port_stub_vcd_widget_stub_vc_widget_notify_cb_h notify_callback, void *user_data)
1519 {
1520         pthread_mutex_lock(&g_widget_tidl_info_mutex);
1521         SLOG(LOG_INFO, TAG_VCD, "@@@ VC WIDGET REGISTER CALLBACK");
1522
1523         int ret = -1;
1524         ret = vcd_client_widget_add_tidl_info(pid);
1525
1526         if (0 != ret) {
1527                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to add widget tidl info.");
1528                 pthread_mutex_unlock(&g_widget_tidl_info_mutex);
1529                 return;
1530         }
1531         widget_tidl_info_s* widget_tidl_info = vcd_client_widget_get_tidl_info(pid);
1532
1533         if (NULL == widget_tidl_info) {
1534                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get widget tidl info. pid(%d)", pid);
1535                 pthread_mutex_unlock(&g_widget_tidl_info_mutex);
1536                 return;
1537         }
1538
1539         char *sender = NULL;
1540         rpc_port_stub_vcd_widget_stub_vc_widget_context_get_sender(context, &sender);
1541
1542         if (!sender){
1543                 SLOG(LOG_ERROR, TAG_VCD, "Sender is NULL");
1544                 pthread_mutex_unlock(&g_widget_tidl_info_mutex);
1545                 return;
1546         }
1547
1548         widget_tidl_info->rpc_h = __widget_create_rpc_port(sender, pid);
1549
1550         if (NULL == widget_tidl_info->rpc_h) {
1551                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create server proxy");
1552         } else {
1553                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] rpc_h(%p), engine_app_id(%s)", widget_tidl_info->rpc_h, sender);
1554         }
1555
1556         free(sender);
1557
1558         if (0 != vcd_client_widget_set_tidl_notify_cb(pid, notify_callback, user_data)) {
1559                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set notify callback");
1560         } else {
1561                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set notify callback.");
1562         }
1563
1564         pthread_mutex_unlock(&g_widget_tidl_info_mutex);
1565
1566         __request_tidl_connect(VCD_CLIENT_TYPE_WIDGET, pid);
1567
1568         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1569 }
1570
1571 static int  __vc_widget_initialize_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, int *service_state, int *daemon_pid, void *user_data)
1572 {
1573         SLOG(LOG_INFO, TAG_VCD, "@@@ VCD Widget Initialize");
1574
1575         uintptr_t ptr_pid = pid;
1576         int ret = -1;
1577
1578         rpc_port_stub_vcd_widget_stub_vc_widget_context_set_tag(context, (void*)ptr_pid);
1579
1580         ret = vcd_server_widget_initialize(pid);
1581         if (0 != ret) {
1582                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1583                 return ret;
1584         }
1585         *service_state = vcd_server_get_service_state();
1586         *daemon_pid = getpid();
1587
1588         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd widget initialize : pid(%d) service state(%d) daemon_pid(%d)", pid, *service_state, *daemon_pid);
1589         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1590         return ret;
1591 }
1592
1593 static int  __vc_widget_finalize_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, void *user_data)
1594 {
1595         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Widget Finalize");
1596         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd widget finalize : pid(%d)", pid);
1597
1598         int ret = -1;
1599
1600         ret = vcd_server_widget_finalize(pid);
1601         if (0 != ret) {
1602                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1603                 return ret;
1604         }
1605
1606         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1607
1608         return ret;
1609 }
1610
1611 static int __vc_widget_start_recording_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, int command, void *user_data)
1612 {
1613         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Widget Start Recording");
1614         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd widget start recording : pid(%d) command(%d)", pid, command);
1615
1616         int ret = -1;
1617
1618         ret = vcd_server_widget_start_recording(pid, command);
1619         if (0 != ret) {
1620                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1621                 return ret;
1622         }
1623
1624         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1625
1626         return ret;
1627 }
1628
1629 static int __vc_widget_start_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, int silence, void *user_data)
1630 {
1631         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Widget Start");
1632         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd widget start : pid(%d) silence(%d)", pid, silence);
1633
1634         int ret = -1;
1635
1636         ret = vcd_server_widget_start(pid, (bool)silence);
1637         if (0 != ret) {
1638                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1639                 return ret;
1640         }
1641
1642         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1643
1644         return ret;
1645 }
1646
1647 static int __vc_widget_stop_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, void *user_data)
1648 {
1649         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Widget Stop");
1650         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd widget stop : pid(%d)", pid);
1651
1652         int ret = -1;
1653
1654         ret = vcd_server_widget_stop(pid);
1655         if (0 != ret) {
1656                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1657                 return ret;
1658         }
1659
1660         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1661
1662         return ret;
1663 }
1664
1665 static int __vc_widget_cancel_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, void *user_data)
1666 {
1667         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Widget Cancel");
1668         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd widget cancel : pid(%d)", pid);
1669
1670         int ret = -1;
1671
1672         ret = vcd_server_widget_cancel(pid);
1673         if (0 != ret) {
1674                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1675                 return ret;
1676         }
1677
1678         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1679
1680         return ret;
1681 }
1682
1683 static int __vc_widget_enable_asr_result_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, int enable, void *user_data)
1684 {
1685         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Widget Enable Asr Result");
1686         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd widget enable asr result : pid(%d) enable(%d)", pid, enable);
1687
1688         int ret = -1;
1689
1690         ret = vcd_server_widget_enable_asr_result(pid, enable);
1691         if (0 != ret) {
1692                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1693                 return ret;
1694         }
1695
1696         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1697
1698         return ret;
1699 }
1700
1701 static void __vc_widget_set_foreground_cb(rpc_port_stub_vcd_widget_stub_vc_widget_context_h context, int pid, int value, void *user_data)
1702 {
1703         SLOG(LOG_DEBUG, TAG_VCD, "@@@ VCD Widget Set Foreground");
1704         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd widget set foreground : pid(%d) value(%d)", pid, value);
1705
1706         int ret = -1;
1707
1708         ret = vcd_server_set_foreground(pid, value);
1709         if (0 != ret) {
1710                 SLOG(LOG_ERROR, TAG_VCD, "[OUT ERROR] Result(%d)", ret);
1711                 return;
1712         }
1713
1714         SLOG(LOG_DEBUG, TAG_VCD, "@@@ send request set foreground to manager");
1715         vcdc_send_request_set_foreground(pid, value);
1716
1717         SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1718
1719         return;
1720 }
1721
1722 int __widget_tidl_open_connection()
1723 {
1724         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] __widget_tidl_open_connection");
1725
1726         g_widget_callback.create = __vc_widget_create_cb;
1727         g_widget_callback.terminate = __vc_widget_terminate_cb;
1728         g_widget_callback.register_cb = __vc_widget_register_cb_cb;
1729         g_widget_callback.initialize = __vc_widget_initialize_cb;
1730         g_widget_callback.finalize = __vc_widget_finalize_cb;
1731         g_widget_callback.start_recording = __vc_widget_start_recording_cb;
1732         g_widget_callback.start = __vc_widget_start_cb;
1733         g_widget_callback.stop = __vc_widget_stop_cb;
1734         g_widget_callback.cancel = __vc_widget_cancel_cb;
1735         g_widget_callback.enable_asr_result = __vc_widget_enable_asr_result_cb;
1736         g_widget_callback.set_foreground = __vc_widget_set_foreground_cb;
1737
1738         int ret = -1;
1739         int count = 0;
1740         while (VC_RETRY_MIN_COUNT >= count) {
1741                 ret = rpc_port_stub_vcd_widget_stub_vc_widget_register(&g_widget_callback, NULL);
1742                 if (0 == ret) {
1743                         SLOG(LOG_DEBUG, TAG_VCD, "register callback");
1744                         return VCD_ERROR_NONE;
1745                 }
1746                 usleep(100000);
1747                 count++;
1748         }
1749
1750         SLOG(LOG_ERROR, TAG_VCD, "Fail to register callback(%d)", ret);
1751         return VCD_ERROR_OPERATION_FAILED;
1752 }
1753
1754 int __widget_tidl_close_connection()
1755 {
1756         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Close connection");
1757         rpc_port_stub_vcd_widget_stub_vc_widget_unregister();
1758
1759         return VCD_ERROR_NONE;
1760 }
1761
1762 /**
1763  * TIDL callback functions for VC setting client
1764  */
1765 static void __vc_setting_create_cb(rpc_port_stub_vcd_setting_stub_vc_setting_context_h context, void *user_data)
1766 {
1767         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_setting_create_cb");
1768
1769         char *sender = NULL;
1770
1771         rpc_port_stub_vcd_setting_stub_vc_setting_context_get_sender(context, &sender);
1772         if (!sender) {
1773                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Sender is NULL");
1774                 return;
1775         }
1776
1777         SLOG(LOG_INFO, TAG_VCD, "[TIDL] sender(%s)", sender);           // sender (app_id)
1778         free(sender);
1779 }
1780
1781 static void __vc_setting_terminate_cb(rpc_port_stub_vcd_setting_stub_vc_setting_context_h context, void *user_data)
1782 {
1783         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_setting_terminate_cb");
1784         void* tag = NULL;
1785         rpc_port_stub_vcd_setting_stub_vc_setting_context_get_tag(context, &tag);
1786
1787         if (NULL != tag) {
1788                 int pid = (intptr_t)tag;
1789                 SLOG(LOG_DEBUG, TAG_VCD, "@@@ VC SETTING FINALIZE. pid(%u)", pid);
1790
1791                 pthread_mutex_lock(&g_setting_tidl_info_mutex);
1792                 setting_tidl_info_s* setting_tidl_info = vcd_client_setting_get_tidl_info(pid);
1793                 if (NULL == setting_tidl_info) {
1794                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get setting tidl info.");
1795                         pthread_mutex_unlock(&g_setting_tidl_info_mutex);
1796                         return;
1797                 }
1798
1799                 if (0 != vcd_client_setting_unset_tidl_notify_cb(pid)) {
1800                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to unset notify callback");
1801                 }
1802
1803                 if (0 != vcd_client_setting_delete_tidl_info(pid)) {
1804                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to delete setting tidl info");
1805                 }
1806                 setting_tidl_info = NULL;
1807
1808                 SLOG(LOG_DEBUG, TAG_VCD, "@@@");
1809
1810                 pthread_mutex_unlock(&g_setting_tidl_info_mutex);
1811         }
1812         rpc_port_stub_vcd_setting_stub_vc_setting_context_set_tag(context, NULL);
1813 }
1814
1815 static void __vc_setting_register_notify_cb_cb(rpc_port_stub_vcd_setting_stub_vc_setting_context_h context, int pid, rpc_port_stub_vcd_setting_stub_vc_setting_notify_cb_h callback, void *user_data)
1816 {
1817         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_setting_register_notify_cb_cb. pid(%d)", pid);
1818
1819         if (NULL == callback) {
1820                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] callback is null.");
1821                 return;
1822         }
1823
1824         int ret = -1;
1825         ret = vcd_client_setting_add_tidl_info(pid);
1826         if (0 != ret) {
1827                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to add tidl info.");
1828                 return;
1829         }
1830
1831         ret = vcd_client_setting_set_tidl_notify_cb(pid, callback, user_data);
1832         if (0 != ret) {
1833                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set notify callback.");
1834         } else {
1835                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set notify callback.");
1836         }
1837 }
1838
1839 static int __vc_setting_register_notify_cb_sync_cb(rpc_port_stub_vcd_setting_stub_vc_setting_context_h context, int pid, rpc_port_stub_vcd_setting_stub_vc_setting_notify_cb_h callback, void *user_data)
1840 {
1841         SLOG(LOG_INFO, TAG_VCD, "[TIDL] __vc_setting_register_notify_cb_sync_cb. pid(%d)", pid);
1842
1843         if (NULL == callback) {
1844                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] callback is null.");
1845                 return VCD_ERROR_INVALID_PARAMETER;
1846         }
1847
1848         int ret = -1;
1849         ret = vcd_client_setting_add_tidl_info(pid);
1850         if (0 != ret) {
1851                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to add tidl info.");
1852                 return ret;
1853         }
1854         
1855         ret = vcd_client_setting_set_tidl_notify_cb(pid, callback, user_data);
1856         if (0 != ret) {
1857                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set notify callback.");
1858         } else {
1859                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set notify callback.");
1860         }
1861
1862         return ret;
1863 }
1864
1865 int  __vc_setting_set_language_cb(rpc_port_stub_vcd_setting_stub_vc_setting_context_h context, int pid, const char *language, void *user_data)
1866 {
1867         if (NULL == language) {
1868                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Invalid parameter");
1869                 return VCD_ERROR_INVALID_PARAMETER;
1870         }
1871
1872         SLOG(LOG_INFO, TAG_VCD, "[IN] vcd server set language : language(%s)", language);
1873
1874         int ret = vcd_server_set_language(language);
1875         if (0 != ret) {
1876                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to set language(%d).", ret);
1877         } else {
1878                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Succeed to set language.");
1879         }
1880
1881         return ret;
1882 }
1883
1884 int __setting_tidl_open_connection()
1885 {
1886         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] __setting_tidl_open_connection");
1887
1888         g_setting_callback.create = __vc_setting_create_cb;
1889         g_setting_callback.terminate = __vc_setting_terminate_cb;
1890         g_setting_callback.register_notify_cb = __vc_setting_register_notify_cb_cb;
1891         g_setting_callback.register_notify_cb_sync = __vc_setting_register_notify_cb_sync_cb;
1892         g_setting_callback.set_language = __vc_setting_set_language_cb;
1893
1894         int ret = -1;
1895         int count = 0;
1896         while (VC_RETRY_MIN_COUNT >= count) {
1897                 ret = rpc_port_stub_vcd_setting_stub_vc_setting_register(&g_setting_callback, NULL);
1898                 if (0 == ret) {
1899                         SLOG(LOG_DEBUG, TAG_VCD, "register callback");
1900                         return VCD_ERROR_NONE;
1901                 }
1902                 usleep(100000);
1903                 count++;
1904         }
1905
1906         SLOG(LOG_ERROR, TAG_VCD, "Fail to register callback(%d)", ret);
1907         return VCD_ERROR_OPERATION_FAILED;
1908 }
1909
1910 int __setting_tidl_close_connection()
1911 {
1912         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Close connection");
1913         rpc_port_stub_vcd_setting_stub_vc_setting_unregister();
1914
1915         return VCD_ERROR_NONE;
1916 }
1917
1918
1919
1920 int vcdc_send_error_signal_to_app(int pid, int reason, char *err_msg)
1921 {
1922         if (NULL == err_msg) {
1923                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Input parameter is NULL");
1924                 return VCD_ERROR_INVALID_PARAMETER;
1925         }
1926
1927         client_tidl_info_s* info = vcd_client_get_tidl_info(pid);
1928         if (NULL == info) {
1929                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get tidl info. pid(%d)", pid);
1930                 return VCD_ERROR_INVALID_PARAMETER;
1931         }
1932
1933         bundle* msg = bundle_create();
1934         if (NULL == msg) {
1935                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create bundle data");
1936                 return VCD_ERROR_OUT_OF_MEMORY;
1937         }
1938
1939         SLOG(LOG_INFO, TAG_VCD, "[TIDL] Send error signal to app(%d)", pid);
1940
1941         char pid_char[10] = {0};
1942         char reason_char[10] = {0};
1943         snprintf(pid_char, 10, "%d", pid);
1944         snprintf(reason_char, 10, "%d", reason);
1945
1946         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_METHOD_ERROR_TO_APP);
1947         bundle_add_str(msg, VC_BUNDLE_PID, pid_char);
1948         bundle_add_str(msg, VC_BUNDLE_REASON, reason_char);
1949         bundle_add_str(msg, VC_BUNDLE_ERR_MSG, err_msg);
1950
1951         rpc_port_stub_vcd_stub_vc_notify_cb_invoke(info->notify_cb, pid, msg);
1952
1953         return VCD_ERROR_NONE;
1954 }
1955
1956 int vcdc_send_manager_pid(int manager_pid)
1957 {
1958         // send signal to all pid
1959
1960         // get all pids
1961         int* client_list = NULL;
1962         int client_count = 0;
1963         int ret = -1;
1964         ret = vcd_client_get_tidl_list(&client_list, &client_count);
1965         if (0 != ret || 0 == client_count) {
1966                 SLOG(LOG_WARN, TAG_VCD, "[WARNING] There is no client");
1967                 return VCD_ERROR_NONE;
1968         }
1969
1970         client_tidl_info_s* info;
1971         int pid = -1;
1972         bundle* msg;
1973         char pid_char[10] = {0};
1974         char mgrpid_char[10] = {0};
1975         for (int i = 0 ; i < client_count ; i++) {
1976                 pid = client_list[i];
1977
1978                 info = vcd_client_get_tidl_info(pid);
1979                 if (NULL == info) {
1980                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get tidl info. pid(%d)", pid);
1981                         continue;
1982                 }
1983
1984                 msg = bundle_create();
1985                 if (NULL == msg) {
1986                         SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create bundle data");
1987                         continue;
1988                 }
1989
1990                 SLOG(LOG_INFO, TAG_VCD, "[TIDL] Send manager pid(%d) to app(%d)", manager_pid, pid);
1991
1992                 snprintf(pid_char, 10, "%d", pid);
1993                 snprintf(mgrpid_char, 10, "%d", manager_pid);
1994
1995                 bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_METHOD_SEND_MANAGER_PID);
1996                 bundle_add_str(msg, VC_BUNDLE_PID, pid_char);
1997                 bundle_add_str(msg, VC_BUNDLE_MANAGER_PID, mgrpid_char);
1998
1999                 rpc_port_stub_vcd_stub_vc_notify_cb_invoke(info->notify_cb, pid, msg);
2000
2001                 bundle_free(msg);
2002         }
2003
2004         return VCD_ERROR_NONE;
2005 }
2006
2007 int vcdc_send_feedback_streaming(int pid, int utt_id, vc_feedback_event_e event, char* buffer, int len)
2008 {
2009         if (NULL == buffer || 0 >= len) {
2010                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Invalid parameter");
2011                 return VCD_ERROR_INVALID_PARAMETER;
2012         }
2013
2014         client_tidl_info_s* info = vcd_client_get_tidl_info(pid);
2015         if (NULL == info) {
2016                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get tidl info. pid(%d)", pid);
2017                 return VCD_ERROR_INVALID_PARAMETER;
2018         }
2019
2020         bundle* msg = bundle_create();
2021         if (NULL == msg) {
2022                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create bundle data");
2023                 return VCD_ERROR_OUT_OF_MEMORY;
2024         }
2025
2026         SLOG(LOG_INFO, TAG_VCD, "[TIDL] Send feedback streaming to app(%d), utt_id(%d), event(%d), length(%d)", pid, utt_id, event, len);
2027
2028         rpc_port_stub_vcd_stub_array_char_h arr_char = NULL;
2029         rpc_port_stub_vcd_stub_array_char_create(&arr_char);
2030         rpc_port_stub_vcd_stub_array_char_set(arr_char, buffer, len);
2031
2032         rpc_port_stub_vcd_stub_vc_feedback_cb_invoke(info->feedback_cb, utt_id, event, arr_char, len);
2033
2034         rpc_port_stub_vcd_stub_array_char_destroy(arr_char);
2035
2036         bundle_free(msg);
2037
2038         return VCD_ERROR_NONE;
2039 }
2040
2041 int vcdc_send_hello(int pid, vcd_client_type_e type)
2042 {
2043         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send hello");
2044
2045         char tmp_pid[10] = {0, };
2046
2047         bundle* msg = bundle_create();
2048         snprintf(tmp_pid, 10, "%d", pid);
2049
2050         if (VCD_CLIENT_TYPE_NORMAL == type) {
2051                 bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_METHOD_HELLO);
2052                 bundle_add_str(msg, VC_BUNDLE_PID, tmp_pid);
2053         } else if (VCD_CLIENT_TYPE_MANAGER == type) {
2054                 bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_HELLO);
2055                 bundle_add_str(msg, VC_BUNDLE_PID, tmp_pid);
2056         } else if (VCD_CLIENT_TYPE_WIDGET == type) {
2057                 bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_WIDGET_METHOD_HELLO);
2058                 bundle_add_str(msg, VC_BUNDLE_PID, tmp_pid);
2059         } else {
2060                 SLOG(LOG_ERROR, TAG_VCD, "[Dbus ERROR] Client type is NOT valid");
2061                 bundle_free(msg);
2062                 return VCD_ERROR_INVALID_PARAMETER;
2063         }
2064
2065         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] VCD SEND MESSAGE");
2066
2067         // TODO: VCD_CLIENT_TYPE_MANAGER?? type??
2068         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, pid);
2069         bundle_free(msg);
2070
2071         return VCD_ERROR_NONE;
2072 }
2073
2074 int vcd_tidl_open_connection()
2075 {
2076         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] vcd_tidl_open_connection");
2077
2078         if (0 != __mgr_tidl_open_connection()) {
2079                 SLOG(LOG_ERROR, TAG_VCD, "Fail to open connection to manager");
2080                 return VCD_ERROR_OPERATION_FAILED;
2081         }
2082
2083         if (0 != __client_tidl_open_connection()) {
2084                 SLOG(LOG_ERROR, TAG_VCD, "Fail to open connection to client");
2085                 return VCD_ERROR_OPERATION_FAILED;
2086         }
2087
2088         if (0 != __widget_tidl_open_connection()) {
2089                 SLOG(LOG_ERROR, TAG_VCD, "Fail to open connection to widget");
2090                 return VCD_ERROR_OPERATION_FAILED;
2091         }
2092
2093         if (0 != __setting_tidl_open_connection()) {
2094                 SLOG(LOG_ERROR, TAG_VCD, "Fail to open connection to setting");
2095                 return VCD_ERROR_OPERATION_FAILED;
2096         }
2097
2098         return VCD_ERROR_NONE;
2099 }
2100
2101 int vcd_tidl_close_connection()
2102 {
2103         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] vcd_tidl_close_connection");
2104
2105         if (0 != __mgr_tidl_close_connection()) {
2106                 SLOG(LOG_ERROR, TAG_VCD, "Fail to close connection to manager");
2107                 return VCD_ERROR_OPERATION_FAILED;
2108         }
2109
2110         if (0 != __client_tidl_close_connection()) {
2111                 SLOG(LOG_ERROR, TAG_VCD, "Fail to close connection to client");
2112                 return VCD_ERROR_OPERATION_FAILED;
2113         }
2114
2115         if (0 != __widget_tidl_close_connection()) {
2116                 SLOG(LOG_ERROR, TAG_VCD, "Fail to close connection to widget");
2117                 return VCD_ERROR_OPERATION_FAILED;
2118         }
2119
2120         if (0 != __setting_tidl_close_connection()) {
2121                 SLOG(LOG_ERROR, TAG_VCD, "Fail to close connection to setting");
2122                 return VCD_ERROR_OPERATION_FAILED;
2123         }
2124
2125         return VCD_ERROR_NONE;
2126 }
2127
2128 int vcdc_send_set_volume(int manager_pid, float volume)
2129 {
2130         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Set volume");
2131
2132         char tmp_volume[20] = {0, };
2133
2134         bundle* msg = bundle_create();
2135
2136         snprintf(tmp_volume, 20, "%.6f", volume);
2137         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_SET_VOLUME);
2138         bundle_add_str(msg, VC_BUNDLE_VOLUME, tmp_volume);
2139
2140
2141         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2142         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, manager_pid);
2143         if (20 == g_volume_count) {
2144                 SLOG(LOG_INFO, TAG_VCD, "@@ Send set volume : pid(%d), volume(%f)", manager_pid, volume);
2145                 g_volume_count = 0;
2146         }
2147         g_volume_count++;
2148
2149         bundle_free(msg);
2150
2151         return VCD_ERROR_NONE;
2152 }
2153
2154 int vcdc_send_show_tooltip(int pid, bool show)
2155 {
2156         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send pre result to manager");
2157
2158         char tmp_pid[10] = {0, };
2159         char tmp_show[10] = {0, };
2160
2161         bundle* msg = bundle_create();
2162
2163         snprintf(tmp_pid, 10, "%d", pid);
2164         snprintf(tmp_show, 10, "%d", (int)show);
2165
2166         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_WIDGET_METHOD_SHOW_TOOLTIP);
2167         bundle_add_str(msg, VC_BUNDLE_PID, tmp_pid);
2168         bundle_add_str(msg, VC_BUNDLE_SHOW, tmp_show);
2169
2170         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2171
2172         __send_msg(msg, VCD_CLIENT_TYPE_WIDGET, pid);
2173         bundle_free(msg);
2174
2175         return VCD_ERROR_NONE;
2176 }
2177
2178 int vcdc_send_result(int pid, int manager_pid, int cmd_type)
2179 {
2180         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send result");
2181
2182         vcd_client_type_e type;
2183         bundle* msg = bundle_create();
2184         if (NULL == msg) {
2185                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to create bundle data");
2186                 return VCD_ERROR_OUT_OF_MEMORY;
2187         }
2188
2189         SLOG(LOG_INFO, TAG_VCD, "[TIDL] Result command type(%d)", cmd_type);
2190
2191         switch (cmd_type) {
2192         case VC_COMMAND_TYPE_FOREGROUND:
2193         case VC_COMMAND_TYPE_BACKGROUND:
2194                 if (pid == manager_pid) {
2195                         type = VCD_CLIENT_TYPE_MANAGER;
2196                         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_RESULT);
2197                 } else {
2198                         type = VCD_CLIENT_TYPE_NORMAL;
2199                         char pid_char[10] = {0};
2200                         snprintf(pid_char, 10, "%d", pid);
2201                         bundle_add_str(msg, VC_BUNDLE_PID, pid_char);
2202                         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_METHOD_RESULT);
2203                 }
2204                 break;
2205         case VC_COMMAND_TYPE_WIDGET:
2206                 type = VCD_CLIENT_TYPE_WIDGET;
2207                 bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_WIDGET_METHOD_RESULT);
2208                 break;
2209         case VC_COMMAND_TYPE_SYSTEM:
2210         case VC_COMMAND_TYPE_SYSTEM_BACKGROUND:
2211         case VC_COMMAND_TYPE_EXCLUSIVE:
2212                 type = VCD_CLIENT_TYPE_MANAGER;
2213                 bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_RESULT);
2214                 break;
2215
2216         default:
2217                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Command type is NOT valid(%d)", cmd_type);
2218                 bundle_free(msg);
2219                 return VCD_ERROR_INVALID_PARAMETER;
2220         }
2221
2222         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2223
2224         __send_msg(msg, type, pid);
2225         bundle_free(msg);
2226
2227         return VCD_ERROR_NONE;
2228 }
2229
2230 int vcdc_send_pre_result_to_manager(int manager_pid, int event, const char* pre_result)
2231 {
2232         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send pre result to manager");
2233
2234         char tmp_event[10] = {0, };
2235
2236         bundle* msg = bundle_create();
2237         snprintf(tmp_event, 10, "%d", event);
2238
2239         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_PRE_RESULT);
2240         bundle_add_str(msg, VC_BUNDLE_EVENT, tmp_event);
2241         bundle_add_str(msg, VC_BUNDLE_PRE_RESULT, pre_result);
2242
2243         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2244
2245         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, manager_pid);
2246         bundle_free(msg);
2247
2248         return VCD_ERROR_NONE;
2249 }
2250
2251 int vcdc_send_specific_engine_result_to_manager(int manager_pid, const char* engine_app_id, const char* event, const char* result)
2252 {
2253         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send specific engine result to manager");
2254
2255         bundle* msg = bundle_create();
2256
2257         gchar *gEncodedResult = g_base64_encode((const guchar*)result, strlen(result));
2258
2259         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_SPECIFIC_ENGINE_RESULT);
2260         bundle_add_str(msg, VC_BUNDLE_ENGINE_APP_ID, engine_app_id);
2261         bundle_add_str(msg, VC_BUNDLE_EVENT, event);
2262         bundle_add_str(msg, VC_BUNDLE_RESULT, gEncodedResult);
2263
2264         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2265
2266         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, manager_pid);
2267         bundle_free(msg);
2268
2269         if (gEncodedResult)
2270                 g_free(gEncodedResult);
2271
2272         return VCD_ERROR_NONE;
2273 }
2274
2275 int vcdc_send_result_to_manager(int manager_pid, int result_type)
2276 {
2277         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send result to manager");
2278
2279         char tmp_result[10] = {0, };
2280
2281         bundle* msg = bundle_create();
2282         snprintf(tmp_result, 10, "%d", result_type);
2283
2284         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_ALL_RESULT);
2285         bundle_add_str(msg, VC_BUNDLE_RESULT, tmp_result);
2286
2287
2288         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2289
2290         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, manager_pid);
2291         bundle_free(msg);
2292
2293         return VCD_ERROR_NONE;
2294 }
2295
2296 int vcdc_send_speech_detected(int manager_pid)
2297 {
2298         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send speech detected");
2299
2300         bundle* msg = bundle_create();
2301
2302         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_SPEECH_DETECTED);
2303
2304         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2305
2306         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, manager_pid);
2307         bundle_free(msg);
2308
2309         return VCD_ERROR_NONE;
2310 }
2311
2312 int vcdc_send_service_state(vcd_state_e state)
2313 {
2314         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send service state");
2315
2316         char tmp_state[10] = {0, };
2317
2318         bundle* msg = bundle_create();
2319         snprintf(tmp_state, 10, "%d", (int)state);
2320
2321         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_SET_SERVICE_STATE);
2322         bundle_add_str(msg, VC_BUNDLE_SERVICE_STATE, tmp_state);
2323         __send_signal(msg, VCD_CLIENT_TYPE_MANAGER);
2324
2325         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_METHOD_SET_SERVICE_STATE);
2326         bundle_add_str(msg, VC_BUNDLE_SERVICE_STATE, tmp_state);
2327         __send_signal(msg, VCD_CLIENT_TYPE_NORMAL);
2328
2329         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_WIDGET_METHOD_SET_SERVICE_STATE);
2330         bundle_add_str(msg, VC_BUNDLE_SERVICE_STATE, tmp_state);
2331         __send_signal(msg, VCD_CLIENT_TYPE_WIDGET);
2332
2333         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND SIGNAL");
2334         bundle_free(msg);
2335
2336         return VCD_ERROR_NONE;
2337 }
2338
2339 int vcdc_send_dialog(int manager_pid, int pid, const char* disp_text, const char* utt_text, int continuous)
2340 {
2341         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send dialog");
2342
2343         char tmp_pid[10] = {0, };
2344         char tmp_continuous[10] = {0, };
2345         char* tmp_disp_text = NULL;
2346         char* tmp_utt_text = NULL;
2347         bundle* msg = bundle_create();
2348         snprintf(tmp_pid, 10, "%d", pid);
2349         snprintf(tmp_continuous, 10, "%d", continuous);
2350
2351         if (NULL == disp_text) {
2352                 tmp_disp_text = strdup("#NULL");
2353         } else {
2354                 tmp_disp_text = strdup(disp_text);
2355         }
2356
2357         if (NULL == utt_text) {
2358                 tmp_utt_text = strdup("#NULL");
2359         } else {
2360                 tmp_utt_text = strdup(utt_text);
2361         }
2362
2363         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_DIALOG);
2364         bundle_add_str(msg, VC_BUNDLE_PID, tmp_pid);
2365         bundle_add_str(msg, VC_BUNDLE_DISP_TEXT, tmp_disp_text);
2366         bundle_add_str(msg, VC_BUNDLE_UTT_TEXT, tmp_utt_text);
2367         bundle_add_str(msg, VC_BUNDLE_CONTINUOUS, tmp_continuous);
2368
2369         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2370
2371         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, manager_pid);
2372         bundle_free(msg);
2373         free(tmp_disp_text);
2374         free(tmp_utt_text);
2375
2376         return VCD_ERROR_NONE;
2377 }
2378
2379 int vcdc_tidl_send_error_to_manager(int manager_pid, int reason, char *err_msg)
2380 {
2381         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] Send error signal to manager");
2382
2383         char tmp_reason[10] = {0, };
2384         char tmp_daemon_pid[10] = {0, };
2385         char *temp_msg = NULL;
2386
2387         if (NULL == err_msg) {
2388                 SLOG(LOG_WARN, TAG_VCD, "[TIDL ERROR] Input parameter is NULL");
2389                 temp_msg = strdup("#NULL");
2390         } else {
2391                 temp_msg = strdup(err_msg);
2392         }
2393
2394         bundle* msg = bundle_create();
2395         snprintf(tmp_reason, 10, "%d", reason);
2396         snprintf(tmp_daemon_pid, 10, "%d", getpid());
2397
2398         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_ERROR);
2399         bundle_add_str(msg, VC_BUNDLE_REASON, tmp_reason);
2400         bundle_add_str(msg, VC_BUNDLE_DAEMON_PID, tmp_daemon_pid);
2401         bundle_add_str(msg, VC_BUNDLE_ERR_MSG, temp_msg);
2402
2403         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2404
2405         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, manager_pid);
2406         bundle_free(msg);
2407
2408         free(temp_msg);
2409         temp_msg = NULL;
2410
2411         return VCD_ERROR_NONE;
2412 }
2413
2414 /*
2415 // TODO: remove _tidl after remove dbus
2416 // TODO: make client, widget error signal
2417 // But... it seems there is no call of this function.. Need to check.
2418 int vcdc_tidl_send_error(int reason, char *err_msg)
2419 {
2420         SLOG(LOG_ERROR, TAG_VCD, "[TIDL] Send error signal");
2421
2422         char tmp_reason[10] = {0, };
2423         char tmp_daemon_pid[10] = {0, };
2424         char *temp_msg = NULL;
2425
2426         if (NULL == err_msg) {
2427                 SLOG(LOG_WARN, TAG_VCD, "[TIDL ERROR] Input parameter is NULL");
2428                 temp_msg = strdup("#NULL");
2429         } else {
2430                 temp_msg = strdup(err_msg);
2431         }
2432
2433         bundle* msg = bundle_create();
2434         snprintf(tmp_reason, 10, "%d", reason);
2435         snprintf(tmp_daemon_pid, 10, "%d", getpid());
2436
2437         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_ERROR);
2438         bundle_add_str(msg, VC_BUNDLE_REASON, tmp_reason);
2439         bundle_add_str(msg, VC_BUNDLE_DAEMON_PID, tmp_daemon_pid);
2440         bundle_add_str(msg, VC_BUNDLE_ERR_MSG, temp_msg);
2441
2442         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_WIDGET_METHOD_ERROR);
2443         bundle_add_str(msg, VC_BUNDLE_REASON, tmp_reason);
2444         bundle_add_str(msg, VC_BUNDLE_DAEMON_PID, tmp_daemon_pid);
2445         bundle_add_str(msg, VC_BUNDLE_ERR_MSG, temp_msg);
2446
2447         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND SIGNAL");
2448
2449         __send_signal(msg);
2450
2451         bundle_free(msg);
2452
2453         free(temp_msg);
2454         temp_msg = NULL;
2455
2456         return VCD_ERROR_NONE;
2457 }
2458 */
2459 int vcdc_send_request_set_private_data(int pid, const char* key, const char* data)
2460 {
2461         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Set private data");
2462
2463         char tmp_pid[10] = { 0, };
2464
2465         bundle* msg = bundle_create();
2466         snprintf(tmp_pid, 10, "%d", pid);
2467
2468         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_SET_PRIVATE_DATA);
2469         bundle_add_str(msg, VC_BUNDLE_PID, tmp_pid);
2470         bundle_add_str(msg, VC_BUNDLE_KEY, key);
2471         bundle_add_str(msg, VC_BUNDLE_PRIVATE_DATA, data);
2472
2473         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2474
2475         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, pid);
2476         bundle_free(msg);
2477
2478         return VCD_ERROR_NONE;
2479 }
2480
2481 int vcdc_send_feedback_audio_format_to_manager(int manager_pid, int rate, vc_audio_channel_e channel, vce_audio_type_e audio_type)
2482 {
2483         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send feedback audio format to manager");
2484
2485         char tmp_rate[10] = {0, };
2486         char tmp_channel[10] = {0, };
2487         char tmp_audio_type[10] = {0, };
2488
2489         bundle* msg = bundle_create();
2490         snprintf(tmp_rate, 10, "%d", rate);
2491         snprintf(tmp_channel, 10, "%d", (int)channel);
2492         snprintf(tmp_audio_type, 10, "%d", (int)audio_type);
2493         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_FEEDBACK_AUDIO_FORMAT);
2494         bundle_add_str(msg, VC_BUNDLE_AUDIO_RATE, tmp_rate);
2495         bundle_add_str(msg, VC_BUNDLE_AUDIO_CHANNEL, tmp_channel);
2496         bundle_add_str(msg, VC_BUNDLE_AUDIO_TYPE, tmp_audio_type);
2497
2498         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2499
2500         __send_msg(msg, VCD_CLIENT_TYPE_MANAGER, manager_pid);
2501         bundle_free(msg);
2502
2503         return VCD_ERROR_NONE;
2504 }
2505
2506 int vcdc_send_feedback_streaming_to_manager(int manager_pid, int pid, int utt_id, vc_feedback_event_e event, char* buffer, int len)
2507 {
2508         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send feedback streaming to manager");
2509
2510         char tmp_pid[10] = {0, };
2511         char tmp_utt_id[10] = {0, };
2512         char tmp_event[10] = {0, };
2513
2514         bundle* msg = bundle_create();
2515         snprintf(tmp_pid, 10, "%d", pid);
2516         snprintf(tmp_utt_id, 10, "%d", utt_id);
2517         snprintf(tmp_event, 10, "%d", (int)event);
2518         bundle_add_str(msg, VC_BUNDLE_METHOD, VCD_MANAGER_METHOD_FEEDBACK_STREAMING);
2519         bundle_add_str(msg, VC_BUNDLE_PID, tmp_pid);
2520         bundle_add_str(msg, VC_BUNDLE_UTT_ID, tmp_utt_id);
2521         bundle_add_str(msg, VC_BUNDLE_EVENT, tmp_event);
2522
2523         rpc_port_stub_vcd_mgr_stub_array_char_h streaming_data = NULL;
2524         rpc_port_stub_vcd_mgr_stub_array_char_create(&streaming_data);
2525         if (NULL == streaming_data) {
2526                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to create data handle");
2527                 return VCD_ERROR_OUT_OF_MEMORY;
2528         }
2529
2530         if (NULL != buffer && 0 < len) {
2531                 rpc_port_stub_vcd_mgr_stub_array_char_set(streaming_data, (char*)buffer, len);
2532         } else {
2533                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL] buffer is empty");
2534         }
2535
2536         SLOG(LOG_DEBUG, TAG_VCD, ">>>> VCD SEND MESSAGE");
2537
2538         pthread_mutex_lock(&g_mgr_tidl_info_mutex);
2539         SLOG(LOG_INFO, TAG_VCD, "[TIDL] message to manager");
2540         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
2541
2542         if (NULL == mgr_tidl_info) {
2543                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Fail to get manager tidl info. pid(%d)", pid);
2544                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
2545                 rpc_port_stub_vcd_mgr_stub_array_char_destroy(streaming_data);
2546                 bundle_free(msg);
2547                 return VCD_ERROR_OPERATION_FAILED;
2548         }
2549
2550         rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_h handle = mgr_tidl_info->send_buffer_cb;
2551         if (NULL == handle) {
2552                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL] notify callback handle is null");
2553                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
2554                 rpc_port_stub_vcd_mgr_stub_array_char_destroy(streaming_data);
2555                 bundle_free(msg);
2556                 return VCD_ERROR_OPERATION_FAILED;
2557         }
2558
2559         if (0 != rpc_port_stub_vcd_mgr_stub_vc_mgr_send_buffer_cb_invoke(handle, streaming_data, msg)) {
2560                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to send msg");
2561                 pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
2562                 rpc_port_stub_vcd_mgr_stub_array_char_destroy(streaming_data);
2563                 bundle_free(msg);
2564                 return VCD_ERROR_OPERATION_FAILED;
2565         }
2566         pthread_mutex_unlock(&g_mgr_tidl_info_mutex);
2567         rpc_port_stub_vcd_mgr_stub_array_char_destroy(streaming_data);
2568         bundle_free(msg);
2569
2570         return VCD_ERROR_NONE;
2571 }
2572
2573 void vcdc_send_request_set_foreground(int pid, int value)
2574 {
2575         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Set foreground");
2576
2577         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
2578
2579         if (NULL == mgr_tidl_info) {
2580                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get tidl info");
2581                 return;
2582         }
2583
2584         if (!mgr_tidl_info->connected) {
2585                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Not Connected");
2586                 return;
2587         }
2588
2589         rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_invoke_set_foreground(mgr_tidl_info->rpc_h, pid, value);
2590
2591         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Request set foreground");
2592 }
2593
2594 int vcdc_send_request_get_private_data(int pid, const char* key, char** data)
2595 {
2596         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Get private data");
2597
2598         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
2599
2600         if (NULL == mgr_tidl_info) {
2601                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get tidl info");
2602                 return VC_ERROR_OPERATION_FAILED;
2603         }
2604
2605         if (!mgr_tidl_info->connected) {
2606                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Not Connected");
2607                 return VC_ERROR_OPERATION_FAILED;
2608         }
2609
2610         char *tmp = NULL;
2611         int ret = rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_invoke_get_private_data(mgr_tidl_info->rpc_h, pid, key, &tmp);
2612         if (RPC_PORT_ERROR_NONE != ret) {
2613                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Request vcd manager get private data : Fail to invoke message, error(%d)", ret);
2614                 return ret;
2615         }
2616         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Request vcd manager get private data : Success");
2617
2618         *data = tmp;
2619
2620         return VC_ERROR_NONE;
2621 }
2622
2623 int vcdc_send_request_auth_enable(int pid)
2624 {
2625         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] auth enable");
2626
2627         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
2628
2629         if (NULL == mgr_tidl_info) {
2630                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get tidl info");
2631                 return VC_ERROR_OPERATION_FAILED;
2632         }
2633
2634         if (!mgr_tidl_info->connected) {
2635                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Not Connected");
2636                 return VC_ERROR_OPERATION_FAILED;
2637         }
2638
2639         int ret = rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_invoke_auth_enable(mgr_tidl_info->rpc_h, pid);
2640         if (RPC_PORT_ERROR_NONE != ret) {
2641                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Request auth enable : Fail to invoke message, error(%d)", ret);
2642                 return ret;
2643         }
2644         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Request auth enable : Success");
2645
2646         return VC_ERROR_NONE;
2647 }
2648
2649 int vcdc_send_request_auth_disable(int pid)
2650 {
2651         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] auth disable");
2652
2653         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
2654
2655         if (NULL == mgr_tidl_info) {
2656                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get tidl info");
2657                 return VC_ERROR_OPERATION_FAILED;
2658         }
2659
2660         if (!mgr_tidl_info->connected) {
2661                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Not Connected");
2662                 return VC_ERROR_OPERATION_FAILED;
2663         }
2664
2665         int ret = rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_invoke_auth_disable(mgr_tidl_info->rpc_h, pid);
2666         if (RPC_PORT_ERROR_NONE != ret) {
2667                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Request auth disable : Fail to invoke message, error(%d)", ret);
2668                 return ret;
2669         }
2670         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Request auth disable : Success");
2671
2672         return VC_ERROR_NONE;
2673 }
2674
2675 int vcdc_send_request_auth_start(int pid)
2676 {
2677         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] auth start");
2678
2679         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
2680
2681         if (NULL == mgr_tidl_info) {
2682                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get tidl info");
2683                 return VC_ERROR_OPERATION_FAILED;
2684         }
2685
2686         if (!mgr_tidl_info->connected) {
2687                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Not Connected");
2688                 return VC_ERROR_OPERATION_FAILED;
2689         }
2690
2691         int ret = rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_invoke_auth_start(mgr_tidl_info->rpc_h, pid);
2692         if (RPC_PORT_ERROR_NONE != ret) {
2693                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Request auth start : Fail to invoke message, error(%d)", ret);
2694                 return ret;
2695         }
2696         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Request auth start : Success");
2697
2698         return VC_ERROR_NONE;
2699 }
2700
2701 int vcdc_send_request_auth_stop(int pid)
2702 {
2703         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] auth stop");
2704
2705         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
2706
2707         if (NULL == mgr_tidl_info) {
2708                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get tidl info");
2709                 return VC_ERROR_OPERATION_FAILED;
2710         }
2711
2712         if (!mgr_tidl_info->connected) {
2713                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Not Connected");
2714                 return VC_ERROR_OPERATION_FAILED;
2715         }
2716
2717         int ret = rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_invoke_auth_stop(mgr_tidl_info->rpc_h, pid);
2718         if (RPC_PORT_ERROR_NONE != ret) {
2719                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Request auth stop : Fail to invoke message, error(%d)", ret);
2720                 return ret;
2721         }
2722         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Request auth stop : Success");
2723
2724         return VC_ERROR_NONE;
2725 }
2726
2727 int vcdc_send_request_auth_cancel(int pid)
2728 {
2729         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] auth cancel");
2730
2731         manager_tidl_info_s* mgr_tidl_info = vcd_client_manager_get_tidl_info();
2732
2733         if (NULL == mgr_tidl_info) {
2734                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get tidl info");
2735                 return VC_ERROR_OPERATION_FAILED;
2736         }
2737
2738         if (!mgr_tidl_info->connected) {
2739                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Not Connected");
2740                 return VC_ERROR_OPERATION_FAILED;
2741         }
2742
2743         int ret = rpc_port_proxy_vcd_mgr_proxy_vcd_mgr_invoke_auth_cancel(mgr_tidl_info->rpc_h, pid);
2744         if (RPC_PORT_ERROR_NONE != ret) {
2745                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Request auth cancel : Fail to invoke message, error(%d)", ret);
2746                 return ret;
2747         }
2748         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Request auth cancel : Success");
2749
2750         return VC_ERROR_NONE;
2751 }
2752
2753 int vcdc_send_asr_result(int pid, int event, const char* asr_result, int cmd_type, bool* is_consumed)
2754 {
2755         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Send asr result");
2756
2757         widget_tidl_info_s* widget_tidl_info = vcd_client_widget_get_tidl_info(pid);
2758
2759         if (NULL == widget_tidl_info) {
2760                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get tidl info");
2761                 return VC_ERROR_OPERATION_FAILED;
2762         }
2763
2764         if (!widget_tidl_info->connected) {
2765                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Not Connected");
2766                 return VC_ERROR_OPERATION_FAILED;
2767         }
2768
2769         bool temp;
2770         int ret = rpc_port_proxy_vcd_widget_proxy_vcd_widget_invoke_send_asr_result(widget_tidl_info->rpc_h, pid, event, asr_result, &temp);
2771         if (RPC_PORT_ERROR_NONE != ret) {
2772                 SLOG(LOG_ERROR, TAG_VCD, "[TIDL ERROR] Request vcd widget send asr result : Fail to invoke message, error(%d)", ret);
2773                 return ret;
2774         }
2775         SLOG(LOG_DEBUG, TAG_VCD, "[TIDL] Request vcd widget send asr result : Success");
2776
2777         *is_consumed = temp;
2778
2779         return VC_ERROR_NONE;
2780 }
2781