Add private interface between engine and manager client
[platform/core/uifw/voice-control.git] / client / vc_mgr_client.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #include "vc_main.h"
19 #include "vc_mgr_client.h"
20
21 typedef struct {
22         /* base info */
23         vc_h    vc;
24         int     pid;
25         int     uid;            /*<< unique id = pid + handle */
26
27         vc_mgr_all_result_cb            all_result_cb;
28         void*                           all_result_user_data;
29         vc_result_cb                    result_cb;
30         void*                           result_user_data;
31         vc_mgr_pre_result_cb            pre_result_cb;
32         void*                           pre_result_user_data;
33
34         vc_error_cb                     error_cb;
35         void*                           error_user_data;
36         vc_service_state_changed_cb     service_state_changed_cb;
37         void*                           service_state_changed_user_data;
38         vc_state_changed_cb             state_changed_cb;
39         void*                           state_changed_user_data;
40         vc_mgr_begin_speech_detected_cb speech_detected_cb;
41         void*                           speech_detected_user_data;
42         vc_current_language_changed_cb  current_lang_changed_cb;
43         void*                           current_lang_changed_user_data;
44         vc_mgr_dialog_request_cb        diaglog_requst_cb;
45         void*                           dialog_request_user_data;
46         vc_mgr_private_data_set_cb      private_data_set_cb;
47         void*                           private_data_set_user_data;
48         vc_mgr_private_data_requested_cb        private_data_requested_cb;
49         void*                                   private_data_requested_user_data;
50
51         /* All result */
52         vc_result_event_e       all_result_event;
53         char*                   all_result_text;
54
55         /* exclusive command flag */
56         bool                    exclusive_cmd_option;
57
58         /* system result */
59         int                     result_event;
60         char*                   result_text;
61
62         /* service state */
63         vc_service_state_e      service_state;
64
65         vc_internal_state_e     internal_state;
66
67         /* state */
68         vc_state_e              before_state;
69         vc_state_e              current_state;
70
71         /* language */
72         char*                   before_language;
73         char*                   current_language;
74
75         /* audio type */
76         char*                   audio_id;
77
78         /* recognition mode */
79         vc_recognition_mode_e   recognition_mode;
80
81         /* mutex */
82         int                     cb_ref_count;
83
84         /* error data */
85         int                     reason;
86         char*                   err_msg;
87
88         /* Authorized */
89         GSList*                 authorized_client_list;
90         int                     valid_authorized_pid;
91         bool                    start_by_client;
92
93         /* foreground pid */
94         int                     foreground_pid;
95 } vc_mgr_client_s;
96
97 typedef struct {
98         int pid;
99 } vc_authorized_client_s;
100
101 static GSList *g_mgr_client_list = NULL;
102
103 static vc_mgr_client_s* __mgr_client_get(vc_h vc)
104 {
105         if (vc == NULL) {
106                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Input parameter is NULL");
107                 return NULL;
108         }
109
110         vc_mgr_client_s *data = NULL;
111
112         int count = g_slist_length(g_mgr_client_list);
113         int i;
114
115         for (i = 0; i < count; i++) {
116                 data = g_slist_nth_data(g_mgr_client_list, i);
117
118                 if (NULL != data) {
119                         if (vc->handle == data->vc->handle) {
120                                 return data;
121                         }
122                 }
123         }
124
125         SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] Fail to get client by vc");
126
127         return NULL;
128 }
129
130 int vc_mgr_client_create(vc_h* vc)
131 {
132         vc_mgr_client_s *client = NULL;
133
134         client = (vc_mgr_client_s*)calloc(1, sizeof(vc_mgr_client_s));
135         if (NULL == client) {
136                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to allocate memory");
137                 return VC_ERROR_OUT_OF_MEMORY;
138         }
139
140         vc_h temp = (vc_h)calloc(1, sizeof(struct vc_s));
141         if (NULL == temp) {
142                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to allocate memory");
143                 free(client);
144                 return VC_ERROR_OUT_OF_MEMORY;
145         }
146
147         temp->handle = getpid();
148
149         /* initialize client data */
150         client->vc = temp;
151         client->pid = getpid();
152         client->uid = temp->handle;
153
154         client->all_result_cb = NULL;
155         client->all_result_user_data = NULL;
156         client->result_cb = NULL;
157         client->result_user_data = NULL;
158         client->pre_result_cb = NULL;
159         client->pre_result_user_data = NULL;
160
161         client->error_cb = NULL;
162         client->error_user_data = NULL;
163         client->service_state_changed_cb = NULL;
164         client->service_state_changed_user_data = NULL;
165         client->state_changed_cb = NULL;
166         client->state_changed_user_data = NULL;
167         client->speech_detected_cb = NULL;
168         client->speech_detected_user_data = NULL;
169         client->current_lang_changed_cb = NULL;
170         client->current_lang_changed_user_data = NULL;
171         client->diaglog_requst_cb = NULL;
172         client->dialog_request_user_data = NULL;
173         client->private_data_set_cb = NULL;
174         client->private_data_set_user_data = NULL;
175         client->private_data_requested_cb = NULL;
176         client->private_data_requested_user_data = NULL;
177
178         client->exclusive_cmd_option = false;
179
180         client->all_result_event = 0;
181         client->all_result_text = NULL;
182
183         client->result_event = -1;
184         client->result_text = NULL;
185
186         client->service_state = 0;
187
188         client->internal_state = VC_INTERNAL_STATE_NONE;
189
190         client->before_state = VC_STATE_INITIALIZED;
191         client->current_state = VC_STATE_INITIALIZED;
192
193         client->before_language = NULL;
194         client->current_language = NULL;
195
196         client->audio_id = NULL;
197         client->recognition_mode = VC_RECOGNITION_MODE_STOP_BY_SILENCE;
198
199         client->reason = 0;
200         client->err_msg = NULL;
201
202         client->cb_ref_count = 0;
203
204         /* Authoriry */
205         client->authorized_client_list = NULL;
206         client->valid_authorized_pid = -1;
207         client->start_by_client = false;
208
209         client->foreground_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
210
211         g_mgr_client_list = g_slist_append(g_mgr_client_list, client);
212
213         *vc = temp;
214
215         return 0;
216 }
217
218 int vc_mgr_client_destroy(vc_h vc)
219 {
220         if (vc == NULL) {
221                 SLOG(LOG_ERROR, TAG_VCM, "Input parameter is NULL");
222                 return 0;
223         }
224
225         vc_mgr_client_s *data = NULL;
226
227         int count = g_slist_length(g_mgr_client_list);
228         int i;
229
230         for (i = 0; i < count; i++) {
231                 data = g_slist_nth_data(g_mgr_client_list, i);
232
233                 if (NULL != data) {
234                         if (vc->handle == data->vc->handle) {
235                                 g_mgr_client_list =  g_slist_remove(g_mgr_client_list, data);
236
237                                 while (0 != data->cb_ref_count) {
238                                         /* wait for release callback function */
239                                 }
240
241                                 if (NULL != data->audio_id) {
242                                         free(data->audio_id);
243                                 }
244
245                                 if (NULL != data->all_result_text) {
246                                         free(data->all_result_text);
247                                 }
248
249                                 if (NULL != data->err_msg) {
250                                         free(data->err_msg);
251                                 }
252
253                                 free(data);     
254                                 free(vc);       
255
256                                 data = NULL;
257                                 vc = NULL;
258
259                                 return 0;
260                         }
261                 }
262         }
263
264         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] client Not found");
265
266         return -1;
267 }
268
269 bool vc_mgr_client_is_valid(vc_h vc)
270 {
271         vc_mgr_client_s* client = __mgr_client_get(vc);
272
273         /* check handle */
274         if (NULL == client) {
275                 SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] vc is not valid");
276                 return false;
277         }
278
279         return true;
280 }
281
282 bool vc_mgr_client_is_valid_by_uid(int uid)
283 {
284         vc_mgr_client_s *data = NULL;
285
286         int count = g_slist_length(g_mgr_client_list);
287         int i;
288
289         for (i = 0; i < count; i++) {
290                 data = g_slist_nth_data(g_mgr_client_list, i);
291
292                 if (NULL != data) {
293                         if (uid == data->vc->handle)
294                                 return true;
295                 }
296         }
297
298         SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] Fail to get client by vc");
299
300         return false;
301 }
302
303 int vc_mgr_client_get_handle(int uid, vc_h* vc)
304 {
305         vc_mgr_client_s *data = NULL;
306
307         int count = g_slist_length(g_mgr_client_list);
308         int i;
309
310         for (i = 0; i < count; i++) {
311                 data = g_slist_nth_data(g_mgr_client_list, i);
312
313                 if (NULL != data) {
314                         if (uid == data->vc->handle) {
315                                 *vc = data->vc;
316                                 return 0;
317                         }
318                 }
319         }
320
321         return -1;
322 }
323
324 int vc_mgr_client_get_pid(vc_h vc, int* pid)
325 {
326         vc_mgr_client_s* client = __mgr_client_get(vc);
327
328         /* check handle */
329         if (NULL == client) {
330                 SLOG(LOG_DEBUG, TAG_VCM, "[DEBUG] vc is not valid");
331                 return -1;
332         }
333
334         *pid = client->pid;
335         return 0;
336 }
337
338 /* set/get callback function */
339 int vc_mgr_client_set_all_result_cb(vc_h vc, vc_mgr_all_result_cb callback, void* user_data)
340 {
341         vc_mgr_client_s* client = __mgr_client_get(vc);
342
343         /* check handle */
344         if (NULL == client)
345                 return VC_ERROR_INVALID_PARAMETER;
346
347         client->all_result_cb = callback;
348         client->all_result_user_data = user_data;
349
350         return 0;
351 }
352
353 int vc_mgr_client_get_all_result_cb(vc_h vc, vc_mgr_all_result_cb* callback, void** user_data)
354 {
355         vc_mgr_client_s* client = __mgr_client_get(vc);
356
357         /* check handle */
358         if (NULL == client)
359                 return VC_ERROR_INVALID_PARAMETER;
360
361         *callback = client->all_result_cb;
362         *user_data = client->all_result_user_data;
363
364         return 0;
365 }
366
367 int vc_mgr_client_set_result_cb(vc_h vc, vc_result_cb callback, void* user_data)
368 {
369         vc_mgr_client_s* client = __mgr_client_get(vc);
370
371         /* check handle */
372         if (NULL == client)
373                 return VC_ERROR_INVALID_PARAMETER;
374
375         client->result_cb = callback;
376         client->result_user_data = user_data;
377
378         return 0;
379 }
380
381 int vc_mgr_client_get_result_cb(vc_h vc, vc_result_cb* callback, void** user_data)
382 {
383         vc_mgr_client_s* client = __mgr_client_get(vc);
384
385         /* check handle */
386         if (NULL == client)
387                 return VC_ERROR_INVALID_PARAMETER;
388
389         *callback = client->result_cb;
390         *user_data = client->result_user_data;
391
392         return 0;
393 }
394
395 int vc_mgr_client_set_pre_result_cb(vc_h vc, vc_mgr_pre_result_cb callback, void* user_data)
396 {
397         vc_mgr_client_s* client = __mgr_client_get(vc);
398
399         /* check handle */
400         if (NULL == client)
401                 return VC_ERROR_INVALID_PARAMETER;
402
403         client->pre_result_cb = callback;
404         client->pre_result_user_data = user_data;
405
406         return 0;
407 }
408
409 int vc_mgr_client_get_pre_result_cb(vc_h vc, vc_mgr_pre_result_cb* callback, void** user_data)
410 {
411         vc_mgr_client_s* client = __mgr_client_get(vc);
412
413         /* check handle */
414         if (NULL == client)
415                 return VC_ERROR_INVALID_PARAMETER;
416
417         *callback = client->pre_result_cb;
418         *user_data = client->pre_result_user_data;
419
420         return 0;
421 }
422
423 int vc_mgr_client_set_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb callback, void* user_data)
424 {
425         vc_mgr_client_s* client = __mgr_client_get(vc);
426
427         /* check handle */
428         if (NULL == client)
429                 return VC_ERROR_INVALID_PARAMETER;
430
431         client->service_state_changed_cb = callback;
432         client->service_state_changed_user_data = user_data;
433
434         return 0;
435 }
436
437 int vc_mgr_client_get_service_state_changed_cb(vc_h vc, vc_service_state_changed_cb* callback, void** user_data)
438 {
439         vc_mgr_client_s* client = __mgr_client_get(vc);
440
441         /* check handle */
442         if (NULL == client)
443                 return VC_ERROR_INVALID_PARAMETER;
444
445         *callback = client->service_state_changed_cb;
446         *user_data = client->service_state_changed_user_data;
447
448         return 0;
449 }
450
451 int vc_mgr_client_set_state_changed_cb(vc_h vc, vc_state_changed_cb callback, void* user_data)
452 {
453         vc_mgr_client_s* client = __mgr_client_get(vc);
454
455         /* check handle */
456         if (NULL == client)
457                 return VC_ERROR_INVALID_PARAMETER;
458
459         client->state_changed_cb = callback;
460         client->state_changed_user_data = user_data;
461
462         return 0;
463 }
464
465 int vc_mgr_client_get_state_changed_cb(vc_h vc, vc_state_changed_cb* callback, void** user_data)
466 {
467         vc_mgr_client_s* client = __mgr_client_get(vc);
468
469         /* check handle */
470         if (NULL == client)
471                 return VC_ERROR_INVALID_PARAMETER;
472
473         *callback = client->state_changed_cb;
474         *user_data = client->state_changed_user_data;
475
476         return 0;
477 }
478
479 int vc_mgr_client_set_speech_detected_cb(vc_h vc, vc_mgr_begin_speech_detected_cb callback, void* user_data)
480 {
481         vc_mgr_client_s* client = __mgr_client_get(vc);
482
483         /* check handle */
484         if (NULL == client)
485                 return VC_ERROR_INVALID_PARAMETER;
486
487         client->speech_detected_cb = callback;
488         client->speech_detected_user_data = user_data;
489
490         return 0;
491 }
492
493 int vc_mgr_client_get_speech_detected_cb(vc_h vc, vc_mgr_begin_speech_detected_cb* callback, void** user_data)
494 {
495         vc_mgr_client_s* client = __mgr_client_get(vc);
496
497         /* check handle */
498         if (NULL == client)
499                 return VC_ERROR_INVALID_PARAMETER;
500
501         *callback = client->speech_detected_cb;
502         *user_data = client->speech_detected_user_data;
503
504         return 0;
505 }
506
507 int vc_mgr_client_set_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb callback, void* user_data)
508 {
509         vc_mgr_client_s* client = __mgr_client_get(vc);
510
511         /* check handle */
512         if (NULL == client)
513                 return VC_ERROR_INVALID_PARAMETER;
514
515         client->current_lang_changed_cb = callback;
516         client->current_lang_changed_user_data = user_data;
517
518         return 0;
519 }
520
521 int vc_mgr_client_get_current_lang_changed_cb(vc_h vc, vc_current_language_changed_cb* callback, void** user_data)
522 {
523         vc_mgr_client_s* client = __mgr_client_get(vc);
524
525         /* check handle */
526         if (NULL == client)
527                 return VC_ERROR_INVALID_PARAMETER;
528
529         *callback = client->current_lang_changed_cb;
530         *user_data = client->current_lang_changed_user_data;
531
532         return 0;
533 }
534
535 int vc_mgr_client_set_error_cb(vc_h vc, vc_error_cb callback, void* user_data)
536 {
537         vc_mgr_client_s* client = __mgr_client_get(vc);
538
539         /* check handle */
540         if (NULL == client)
541                 return VC_ERROR_INVALID_PARAMETER;
542
543         client->error_cb = callback;
544         client->error_user_data = user_data;
545
546         return 0;
547 }
548
549 int vc_mgr_client_get_error_cb(vc_h vc, vc_error_cb* callback, void** user_data)
550 {
551         vc_mgr_client_s* client = __mgr_client_get(vc);
552
553         /* check handle */
554         if (NULL == client)
555                 return VC_ERROR_INVALID_PARAMETER;
556
557         *callback = client->error_cb;
558         *user_data = client->error_user_data;
559
560         return 0;
561 }
562
563 int vc_mgr_client_set_dialog_request_cb(vc_h vc, vc_mgr_dialog_request_cb callback, void* user_data)
564 {
565         vc_mgr_client_s* client = __mgr_client_get(vc);
566
567         /* check handle */
568         if (NULL == client)
569                 return VC_ERROR_INVALID_PARAMETER;
570
571         client->diaglog_requst_cb = callback;
572         client->dialog_request_user_data = user_data;
573
574         return 0;
575 }
576
577 int vc_mgr_client_get_dialog_request_cb(vc_h vc, vc_mgr_dialog_request_cb* callback, void** user_data)
578 {
579         vc_mgr_client_s* client = __mgr_client_get(vc);
580
581         /* check handle */
582         if (NULL == client)
583                 return VC_ERROR_INVALID_PARAMETER;
584
585         *callback = client->diaglog_requst_cb;
586         *user_data = client->dialog_request_user_data;
587
588         return 0;
589 }
590
591
592 int vc_mgr_client_set_private_data_set_cb(vc_h vc, vc_mgr_private_data_set_cb callback, void* user_data)
593 {
594         vc_mgr_client_s* client = __mgr_client_get(vc);
595
596         /* check handle */
597         if (NULL == client)
598                 return VC_ERROR_INVALID_PARAMETER;
599
600         client->private_data_set_cb = callback;
601         client->private_data_set_user_data = user_data;
602
603         return 0;
604 }
605
606 int vc_mgr_client_get_private_data_set_cb(vc_h vc, vc_mgr_private_data_set_cb* callback, void** user_data)
607 {
608         vc_mgr_client_s* client = __mgr_client_get(vc);
609
610         /* check handle */
611         if (NULL == client)
612                 return VC_ERROR_INVALID_PARAMETER;
613
614         *callback = client->private_data_set_cb;
615         *user_data = client->private_data_set_user_data;
616
617         return 0;
618 }
619
620 int vc_mgr_client_set_private_data_requested_cb(vc_h vc, vc_mgr_private_data_requested_cb callback, void* user_data)
621 {
622         vc_mgr_client_s* client = __mgr_client_get(vc);
623
624         /* check handle */
625         if (NULL == client)
626                 return VC_ERROR_INVALID_PARAMETER;
627
628         client->private_data_requested_cb = callback;
629         client->private_data_requested_user_data = user_data;
630
631         return 0;
632 }
633
634 int vc_mgr_client_get_private_data_requested_cb(vc_h vc, vc_mgr_private_data_requested_cb* callback, void** user_data)
635 {
636         vc_mgr_client_s* client = __mgr_client_get(vc);
637
638         /* check handle */
639         if (NULL == client)
640                 return VC_ERROR_INVALID_PARAMETER;
641
642         *callback = client->private_data_requested_cb;
643         *user_data = client->private_data_requested_user_data;
644
645         return 0;
646 }
647
648 /* set/get option */
649 int vc_mgr_client_set_service_state(vc_h vc, vc_service_state_e state)
650 {
651         vc_mgr_client_s* client = __mgr_client_get(vc);
652
653         /* check handle */
654         if (NULL == client)
655                 return VC_ERROR_INVALID_PARAMETER;
656
657         client->service_state = state;
658
659         return 0;
660 }
661
662 int vc_mgr_client_get_service_state(vc_h vc, vc_service_state_e* state)
663 {
664         vc_mgr_client_s* client = __mgr_client_get(vc);
665
666         /* check handle */
667         if (NULL == client)
668                 return VC_ERROR_INVALID_PARAMETER;
669
670         *state = client->service_state;
671
672         return 0;
673 }
674
675 int vc_mgr_client_set_internal_state(vc_h vc, vc_internal_state_e state)
676 {
677         vc_mgr_client_s* client = __mgr_client_get(vc);
678
679         /* check handle */
680         if (NULL == client)
681                 return VC_ERROR_INVALID_PARAMETER;
682
683         client->internal_state = state;
684
685         return 0;
686 }
687
688 int vc_mgr_client_get_internal_state(vc_h vc, vc_internal_state_e* state)
689 {
690         vc_mgr_client_s* client = __mgr_client_get(vc);
691
692         /* check handle */
693         if (NULL == client)
694                 return VC_ERROR_INVALID_PARAMETER;
695
696         *state = client->internal_state;
697
698         return 0;
699 }
700
701 int vc_mgr_client_set_client_state(vc_h vc, vc_state_e state)
702 {
703         vc_mgr_client_s* client = __mgr_client_get(vc);
704
705         /* check handle */
706         if (NULL == client)
707                 return VC_ERROR_INVALID_PARAMETER;
708
709         client->before_state = client->current_state;
710         client->current_state = state;
711
712         return 0;
713 }
714
715 int vc_mgr_client_get_client_state(vc_h vc, vc_state_e* state)
716 {
717         vc_mgr_client_s* client = __mgr_client_get(vc);
718
719         /* check handle */
720         if (NULL == client)
721                 return VC_ERROR_INVALID_PARAMETER;
722
723         *state = client->current_state;
724
725         return 0;
726 }
727
728 int vc_mgr_client_get_client_state_by_uid(int uid, vc_state_e* state)
729 {
730         vc_mgr_client_s *data = NULL;
731
732         int count = g_slist_length(g_mgr_client_list);
733         int i;
734
735         for (i = 0; i < count; i++) {
736                 data = g_slist_nth_data(g_mgr_client_list, i);
737
738                 if (NULL != data) {
739                         if (uid == data->vc->handle) {
740                                 *state = data->current_state;
741                                 return 0;
742                         }
743                 }
744         }
745
746         return -1;
747 }
748
749 int vc_mgr_client_get_before_state(vc_h vc, vc_state_e* state, vc_state_e* before_state)
750 {
751         vc_mgr_client_s* client = __mgr_client_get(vc);
752
753         /* check handle */
754         if (NULL == client)
755                 return VC_ERROR_INVALID_PARAMETER;
756
757         *before_state = client->before_state;
758         *state = client->current_state;
759
760         return 0;
761 }
762
763 int vc_mgr_client_set_error(vc_h vc, int reason)
764 {
765         vc_mgr_client_s* client = __mgr_client_get(vc);
766
767         /* check handle */
768         if (NULL == client)
769                 return VC_ERROR_INVALID_PARAMETER;
770
771         client->reason = reason;
772
773         return 0;
774 }
775
776 int vc_mgr_client_get_error(vc_h vc, int* reason)
777 {
778         vc_mgr_client_s* client = __mgr_client_get(vc);
779
780         /* check handle */
781         if (NULL == client)
782                 return VC_ERROR_INVALID_PARAMETER;
783
784         *reason = client->reason;
785
786         return 0;
787 }
788
789 int vc_mgr_client_set_error_message(vc_h vc, const char* err_msg)
790 {
791         vc_mgr_client_s* client = __mgr_client_get(vc);
792
793         /* check handle */
794         if (NULL == client)
795                 return VC_ERROR_INVALID_PARAMETER;
796
797         if (NULL != client->err_msg) {
798                 free(client->err_msg);
799                 client->err_msg = NULL;
800         }
801
802         if (NULL != err_msg) {
803                 client->err_msg = strdup(err_msg);
804                 if (NULL == client->err_msg) {
805                         return VC_ERROR_OUT_OF_MEMORY;
806                 }
807         }
808
809         return 0;
810 }
811
812 int vc_mgr_client_get_error_message(vc_h vc, char** err_msg)
813 {
814         vc_mgr_client_s* client = __mgr_client_get(vc);
815
816         /* check handle */
817         if (NULL == client)
818                 return VC_ERROR_INVALID_PARAMETER;
819
820         if (NULL != client->err_msg) {
821                 *err_msg = strdup(client->err_msg);
822                 if (NULL == *err_msg) {
823                         return VC_ERROR_OUT_OF_MEMORY;
824                 }
825         }
826
827         return 0;
828 }
829
830 int vc_mgr_client_set_exclusive_command(vc_h vc, bool value)
831 {
832         vc_mgr_client_s* client = __mgr_client_get(vc);
833
834         /* check handle */
835         if (NULL == client)
836                 return VC_ERROR_INVALID_PARAMETER;
837
838         client->exclusive_cmd_option = value;
839
840         return 0;
841 }
842
843 bool vc_mgr_client_get_exclusive_command(vc_h vc)
844 {
845         vc_mgr_client_s* client = __mgr_client_get(vc);
846
847         /* check handle */
848         if (NULL == client)
849                 return VC_ERROR_INVALID_PARAMETER;
850
851         return client->exclusive_cmd_option;
852 }
853
854 int vc_mgr_client_set_all_result(vc_h vc, int event, const char* result_text)
855 {
856         vc_mgr_client_s* client = __mgr_client_get(vc);
857
858         /* check handle */
859         if (NULL == client)
860                 return VC_ERROR_INVALID_PARAMETER;
861
862         client->all_result_event = event;
863
864         if (NULL != client->all_result_text) {
865                 free(client->all_result_text);
866                 client->all_result_text = NULL;
867         }
868         if (NULL != result_text) {
869                 client->all_result_text = strdup(result_text);
870         }
871
872         return 0;
873 }
874
875 int vc_mgr_client_get_all_result(vc_h vc, int* event, char** result_text)
876 {
877         vc_mgr_client_s* client = __mgr_client_get(vc);
878
879         /* check handle */
880         if (NULL == client)
881                 return VC_ERROR_INVALID_PARAMETER;
882
883         *event = client->all_result_event;
884         if (NULL != result_text) {
885                 if (NULL != client->all_result_text) {
886                         *result_text = strdup(client->all_result_text);
887                 }
888         }
889
890         return 0;
891 }
892
893 int vc_mgr_client_unset_all_result(vc_h vc)
894 {
895         vc_mgr_client_s* client = __mgr_client_get(vc);
896
897         /* check handle */
898         if (NULL == client)
899                 return VC_ERROR_INVALID_PARAMETER;
900
901         client->all_result_event = -1;
902
903         if (NULL != client->all_result_text) {
904                 free(client->all_result_text);
905                 client->all_result_text = NULL;
906         }
907
908         return 0;
909 }
910
911 int vc_mgr_client_set_audio_type(vc_h vc, const char* audio_id)
912 {
913         vc_mgr_client_s* client = __mgr_client_get(vc);
914
915         /* check handle */
916         if (NULL == client)
917                 return VC_ERROR_INVALID_PARAMETER;
918
919         if (NULL != audio_id) {
920                 if (NULL != client->audio_id) {
921                         free(client->audio_id);
922                         client->audio_id = NULL;
923                 }
924                 client->audio_id = strdup(audio_id);
925         }
926
927         return 0;
928 }
929
930 int vc_mgr_client_get_audio_type(vc_h vc, char** audio_id)
931 {
932         if (NULL == audio_id)   {
933                 return -1;
934         }
935
936         vc_mgr_client_s* client = __mgr_client_get(vc);
937
938         /* check handle */
939         if (NULL == client)
940                 return VC_ERROR_INVALID_PARAMETER;
941
942         if (NULL != client->audio_id)
943                 *audio_id = strdup(client->audio_id);
944         else
945                 *audio_id = NULL;
946
947         return 0;
948 }
949
950 int vc_mgr_client_set_recognition_mode(vc_h vc, vc_recognition_mode_e mode)
951 {
952         vc_mgr_client_s* client = __mgr_client_get(vc);
953
954         /* check handle */
955         if (NULL == client)
956                 return VC_ERROR_INVALID_PARAMETER;
957
958         client->recognition_mode = mode;
959
960         return 0;
961 }
962
963 int vc_mgr_client_get_recognition_mode(vc_h vc, vc_recognition_mode_e* mode)
964 {
965         if (NULL == mode) {
966                 return -1;
967         }
968
969         vc_mgr_client_s* client = __mgr_client_get(vc);
970
971         /* check handle */
972         if (NULL == client)
973                 return VC_ERROR_INVALID_PARAMETER;
974
975         *mode = client->recognition_mode;
976         return 0;
977 }
978
979 int vc_mgr_client_set_foreground(vc_h vc, int pid, bool value)
980 {
981         vc_mgr_client_s* client = __mgr_client_get(vc);
982
983         /* check handle */
984         if (NULL == client)
985                 return VC_ERROR_INVALID_PARAMETER;
986
987         if (true == value) {
988                 client->foreground_pid = pid;
989         } else {
990                 if (pid == client->foreground_pid) {
991                         client->foreground_pid = VC_RUNTIME_INFO_NO_FOREGROUND;
992                 }
993         }
994
995         return 0;
996 }
997
998 int vc_mgr_client_get_foreground(vc_h vc, int* pid)
999 {
1000         vc_mgr_client_s* client = __mgr_client_get(vc);
1001
1002         /* check handle */
1003         if (NULL == client)
1004                 return VC_ERROR_INVALID_PARAMETER;
1005
1006         *pid = client->foreground_pid;
1007         return 0;
1008 }
1009
1010 /* utils */
1011 int vc_mgr_client_get_count()
1012 {
1013         return g_slist_length(g_mgr_client_list);
1014 }
1015
1016 int vc_mgr_client_use_callback(vc_h vc)
1017 {
1018         vc_mgr_client_s* client = __mgr_client_get(vc);
1019
1020         /* check handle */
1021         if (NULL == client)
1022                 return VC_ERROR_INVALID_PARAMETER;
1023
1024         client->cb_ref_count++;
1025         return 0;
1026 }
1027
1028 int vc_mgr_client_not_use_callback(vc_h vc)
1029 {
1030         vc_mgr_client_s* client = __mgr_client_get(vc);
1031
1032         /* check handle */
1033         if (NULL == client)
1034                 return VC_ERROR_INVALID_PARAMETER;
1035
1036         client->cb_ref_count--;
1037         return 0;
1038 }
1039
1040 /* Authority */
1041 int vc_mgr_client_add_authorized_client(vc_h vc, int pid)
1042 {
1043         vc_mgr_client_s* client = __mgr_client_get(vc);
1044
1045         /* check handle */
1046         if (NULL == client)
1047                 return VC_ERROR_INVALID_PARAMETER;
1048
1049         vc_authorized_client_s *authorized_client = NULL;
1050
1051         authorized_client = (vc_authorized_client_s*)calloc(1, sizeof(vc_authorized_client_s));
1052         if (NULL == authorized_client) {
1053                 SLOG(LOG_ERROR, TAG_VCM, "[ERROR] Fail to make authorized client");
1054                 return VC_ERROR_OPERATION_FAILED;
1055         }
1056
1057         authorized_client->pid = pid;
1058
1059         client->authorized_client_list = g_slist_append(client->authorized_client_list, authorized_client);
1060
1061         SLOG(LOG_DEBUG, TAG_VCM, "Add authorized client - %d", pid);
1062
1063         return 0;
1064 }
1065
1066 int vc_mgr_client_remove_authorized_client(vc_h vc, int pid)
1067 {
1068         vc_mgr_client_s* client = __mgr_client_get(vc);
1069
1070         /* check handle */
1071         if (NULL == client)
1072                 return VC_ERROR_INVALID_PARAMETER;
1073
1074         vc_authorized_client_s *data = NULL;
1075
1076         int count = g_slist_length(client->authorized_client_list);
1077         int i;
1078
1079         for (i = 0; i < count; i++) {
1080                 data = g_slist_nth_data(client->authorized_client_list, i);
1081
1082                 if (NULL != data) {
1083                         if (pid == data->pid) {
1084                                 client->authorized_client_list = g_slist_remove(client->authorized_client_list, data);
1085
1086                                 free(data);
1087                                 data = NULL;
1088
1089                                 SLOG(LOG_DEBUG, TAG_VCM, "Remove authorized client - %d", pid);
1090                                 return 0;
1091                         }
1092                 }
1093         }
1094
1095         SLOG(LOG_ERROR, TAG_VCM, "[ERROR] client Not found");
1096
1097         return VC_ERROR_OPERATION_FAILED;
1098 }
1099
1100 bool vc_mgr_client_is_authorized_client(vc_h vc, int pid)
1101 {
1102         vc_mgr_client_s* client = __mgr_client_get(vc);
1103
1104         /* check handle */
1105         if (NULL == client)
1106                 return VC_ERROR_INVALID_PARAMETER;
1107
1108         vc_authorized_client_s *data = NULL;
1109
1110         int count = g_slist_length(client->authorized_client_list);
1111         int i;
1112
1113         for (i = 0; i < count; i++) {
1114                 data = g_slist_nth_data(client->authorized_client_list, i);
1115
1116                 if (NULL != data) {
1117                         if (pid == data->pid) {
1118                                 SLOG(LOG_DEBUG, TAG_VCM, "Authorized client - %d", pid);
1119                                 return true;
1120                         }
1121                 }
1122         }
1123
1124         SLOG(LOG_DEBUG, TAG_VCM, "Un-Authorized client - %d", pid);
1125
1126         return false;
1127 }
1128
1129 int vc_mgr_client_set_valid_authorized_client(vc_h vc, int pid)
1130 {
1131         vc_mgr_client_s* client = __mgr_client_get(vc);
1132
1133         /* check handle */
1134         if (NULL == client)
1135                 return VC_ERROR_INVALID_PARAMETER;
1136
1137         client->valid_authorized_pid = pid;
1138
1139         return 0;
1140 }
1141
1142 int vc_mgr_client_get_valid_authorized_client(vc_h vc, int* pid)
1143 {
1144         vc_mgr_client_s* client = __mgr_client_get(vc);
1145
1146         /* check handle */
1147         if (NULL == client)
1148                 return VC_ERROR_INVALID_PARAMETER;
1149
1150         *pid = client->valid_authorized_pid;
1151
1152         return 0;
1153 }
1154
1155 bool vc_mgr_client_is_valid_authorized_client(vc_h vc, int pid)
1156 {
1157         vc_mgr_client_s* client = __mgr_client_get(vc);
1158
1159         /* check handle */
1160         if (NULL == client)
1161                 return VC_ERROR_INVALID_PARAMETER;
1162
1163         if (pid == client->valid_authorized_pid)
1164                 return true;
1165         else
1166                 return false;
1167 }
1168
1169 int vc_mgr_client_set_start_by_client(vc_h vc, bool option)
1170 {
1171         vc_mgr_client_s* client = __mgr_client_get(vc);
1172
1173         /* check handle */
1174         if (NULL == client)
1175                 return VC_ERROR_INVALID_PARAMETER;
1176
1177         client->start_by_client = option;
1178
1179         return 0;
1180 }
1181
1182 int vc_mgr_client_get_start_by_client(vc_h vc, bool* option)
1183 {
1184         vc_mgr_client_s* client = __mgr_client_get(vc);
1185
1186         /* check handle */
1187         if (NULL == client)
1188                 return VC_ERROR_INVALID_PARAMETER;
1189
1190         *option = client->start_by_client;
1191
1192         return 0;
1193 }