Merge with Tizen 2.3
[platform/core/uifw/stt.git] / client / stt_client.c
1 /*
2 *  Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved 
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14
15 #include "stt_client.h"
16
17 #define MUTEX_TIME 3
18
19 /* Max number of handle */
20 static const int g_max_handle = 999;
21 /* allocated handle */
22 static int g_allocated_handle = 0;
23 /* client list */
24 static GList *g_client_list = NULL;
25
26
27 /* private functions */
28 static int __client_generate_uid(int pid)
29 {
30         g_allocated_handle++;
31
32         if (g_allocated_handle > g_max_handle) {
33                 g_allocated_handle = 1;
34         }
35
36         /* generate uid, handle number should be smaller than 1000 */
37         return pid * 1000 + g_allocated_handle;
38 }
39
40 int stt_client_new(stt_h* stt)
41 {
42         stt_client_s *client = NULL;
43
44         client = (stt_client_s*)g_malloc0 (sizeof(stt_client_s));
45
46         stt_h temp = (stt_h)g_malloc0(sizeof(struct stt_s));
47         temp->handle = __client_generate_uid(getpid()); 
48
49         /* initialize client data */
50         client->stt = temp;
51         client->pid = getpid(); 
52         client->uid = temp->handle;
53         
54         client->recognition_result_cb = NULL;
55         client->recognition_result_user_data = NULL;
56         client->state_changed_cb = NULL;
57         client->state_changed_user_data = NULL;
58         client->error_cb = NULL;
59         client->error_user_data = NULL;
60         client->default_lang_changed_cb = NULL;
61         client->default_lang_changed_user_data = NULL;
62
63         client->current_engine_id = NULL;
64
65         client->silence_supported = false;
66         client->silence = STT_OPTION_SILENCE_DETECTION_AUTO;
67
68         client->event = 0;
69         client->data_list = NULL;
70         client->data_count = 0;
71         client->msg = NULL;
72
73         client->before_state = STT_STATE_CREATED;
74         client->current_state = STT_STATE_CREATED;
75
76         client->internal_state = STT_INTERNAL_STATE_NONE;
77
78         client->cb_ref_count = 0;
79
80         g_client_list = g_list_append(g_client_list, client);
81
82         *stt = temp;
83
84         return 0;       
85 }
86
87 int stt_client_destroy(stt_h stt)
88 {
89         if (stt == NULL) {
90                 SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL");
91                 return 0;
92         }
93
94         GList *iter = NULL;
95         stt_client_s *data = NULL;
96
97         /* if list have item */
98         if (g_list_length(g_client_list) > 0) {
99                 /* Get a first item */
100                 iter = g_list_first(g_client_list);
101
102                 while (NULL != iter) {
103                         data = iter->data;
104                         if (stt->handle == data->stt->handle) {
105                                 g_client_list = g_list_remove_link(g_client_list, iter);
106
107                                 while (0 != data->cb_ref_count)
108                                 {
109                                         /* wait for release callback function */
110                                 }
111                                 
112                                 if (NULL != data->current_engine_id) {
113                                         free(data->current_engine_id);
114                                 }
115
116                                 free(data);
117                                 free(stt);
118
119                                 return 0;
120                         }
121
122                         /* Next item */
123                         iter = g_list_next(iter);
124                 }
125         }
126
127         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] client Not founded");
128
129         return -1;
130 }
131
132
133 stt_client_s* stt_client_get(stt_h stt)
134 {
135         if (NULL == stt) {
136                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
137                 return NULL;
138         }
139
140         GList *iter = NULL;
141         stt_client_s *data = NULL;
142
143         if (g_list_length(g_client_list) > 0) {
144                 /* Get a first item */
145                 iter = g_list_first(g_client_list);
146
147                 while (NULL != iter) {
148                         data = iter->data;
149                         if (NULL != data) {
150                                 if (stt->handle == data->stt->handle) 
151                                         return data;
152                         }
153                         /* Next item */
154                         iter = g_list_next(iter);
155                 }
156         }
157
158         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by stt");
159
160         return NULL;
161 }
162
163 stt_client_s* stt_client_get_by_uid(const int uid)
164 {
165         if (uid < 0) {
166                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] out of range : handle");
167                 return NULL;
168         }
169
170         GList *iter = NULL;
171         stt_client_s *data = NULL;
172
173         if (g_list_length(g_client_list) > 0) {
174                 /* Get a first item */
175                 iter = g_list_first(g_client_list);
176
177                 while (NULL != iter) {
178                         data = iter->data;
179                         if (uid == data->uid) {
180                                 return data;
181                         }
182
183                         /* Next item */
184                         iter = g_list_next(iter);
185                 }
186         }
187
188         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by uid");
189
190         return NULL;
191 }
192
193 int stt_client_get_size()
194 {
195         return g_list_length(g_client_list);
196 }
197
198 int stt_client_use_callback(stt_client_s* client)
199 {
200         client->cb_ref_count++;
201         return 0;
202 }
203
204 int stt_client_not_use_callback(stt_client_s* client)
205 {
206         client->cb_ref_count--;
207         return 0;
208 }
209
210 int stt_client_get_use_callback(stt_client_s* client)
211 {
212         return client->cb_ref_count;
213 }
214
215 GList* stt_client_get_client_list()
216 {
217         return g_client_list;
218 }