fix merge duplication
[platform/core/uifw/stt.git] / client / stt_client.c
1 /*
2 *  Copyright (c) 2011-2016 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 #include "stt_internal.h"
17
18 #define MUTEX_TIME 3
19
20 /* Max number of handle */
21 static const int g_max_handle = 999;
22 /* allocated handle */
23 static unsigned int g_allocated_handle = 0;
24 /* client list */
25 static GList *g_client_list = NULL;
26
27
28 /* private functions */
29 static unsigned int __client_generate_uid(unsigned int pid)
30 {
31         g_allocated_handle++;
32
33         if (g_allocated_handle > g_max_handle) {
34                 g_allocated_handle = 1;
35         }
36
37         /* generate uid, handle number should be smaller than 1000 */
38         return pid * 1000u + g_allocated_handle;
39 }
40
41 int stt_client_new(stt_h* stt)
42 {
43         stt_client_s *client = NULL;
44
45         client = (stt_client_s*)calloc(1, sizeof(stt_client_s));
46         if (NULL == client) {
47                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
48                 return STT_ERROR_OUT_OF_MEMORY;
49         }
50
51         stt_h temp = (stt_h)calloc(1, sizeof(struct stt_s));
52         if (NULL == temp) {
53                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
54                 free(client);
55                 return STT_ERROR_OUT_OF_MEMORY;
56         }
57         temp->handle = __client_generate_uid((unsigned int)getpid());
58
59         /* initialize client data */
60         client->stt = temp;
61         client->pid = getpid();
62         client->uid = temp->handle;
63
64         client->recognition_result_cb = NULL;
65         client->recognition_result_user_data = NULL;
66         client->state_changed_cb = NULL;
67         client->state_changed_user_data = NULL;
68         client->error_cb = NULL;
69         client->error_user_data = NULL;
70         client->default_lang_changed_cb = NULL;
71         client->default_lang_changed_user_data = NULL;
72         client->speech_status_cb = NULL;
73         client->speech_status_user_data = NULL;
74
75         client->current_engine_id = NULL;
76         client->credential = NULL;
77 #ifdef TV_PRODUCT
78         client->audio_id = strdup(STT_AUDIO_ID_BLUETOOTH);
79 #else
80         client->audio_id = strdup(STT_AUDIO_ID_NONE);
81 #endif
82         client->silence_supported = false;
83         client->silence = STT_OPTION_SILENCE_DETECTION_AUTO;
84
85         client->reason = 0;
86         client->err_msg = NULL;
87
88         client->before_state = STT_STATE_CREATED;
89         client->current_state = STT_STATE_CREATED;
90
91         client->internal_state = STT_INTERNAL_STATE_NONE;
92
93         client->speech_status = -1;
94
95         client->cb_ref_count = 0;
96
97         client->internal = false;
98         client->is_streaming = false;
99
100         g_client_list = g_list_append(g_client_list, client);
101
102         *stt = temp;
103
104         return 0;
105 }
106
107 int stt_client_destroy(stt_h stt)
108 {
109         if (stt == NULL) {
110                 SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL"); //LCOV_EXCL_LINE
111                 return 0;
112         }
113
114         GList *iter = NULL;
115         stt_client_s *data = NULL;
116
117         /* if list have item */
118         if (g_list_length(g_client_list) > 0) {
119                 /* Get a first item */
120                 iter = g_list_first(g_client_list);
121
122                 while (NULL != iter) {
123                         data = iter->data;
124                         if (stt->handle == data->stt->handle) {
125                                 g_client_list = g_list_remove_link(g_client_list, iter);
126
127                                 while (0 != data->cb_ref_count) {
128                                         /* wait for release callback function */
129                                 }
130
131                                 if (NULL != data->current_engine_id) {
132                                         free(data->current_engine_id);
133                                 }
134
135                                 if (NULL != data->err_msg) {
136                                         free(data->err_msg);
137                                         data->err_msg = NULL;
138                                 }
139
140                                 if (NULL != data->credential) {
141                                         free(data->credential);
142                                         data->credential = NULL;
143                                 }
144
145                                 if (NULL != data->audio_id) {
146                                         free(data->audio_id);
147                                         data->audio_id = NULL;
148                                 }
149
150                                 free(data);
151                                 free(stt);
152                                 data = NULL;
153                                 stt = NULL;
154
155                                 g_list_free(iter);
156
157                                 return 0;
158                         }
159
160                         /* Next item */
161                         iter = g_list_next(iter);
162                 }
163         }
164
165         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] client Not founded"); //LCOV_EXCL_LINE
166
167         return -1;
168 }
169
170
171 stt_client_s* stt_client_get(stt_h stt)
172 {
173         if (NULL == stt) {
174                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
175                 return NULL;
176         }
177
178         GList *iter = NULL;
179         stt_client_s *data = NULL;
180
181         if (g_list_length(g_client_list) > 0) {
182                 /* Get a first item */
183                 iter = g_list_first(g_client_list);
184
185                 while (NULL != iter) {
186                         data = iter->data;
187                         if (NULL != data) {
188                                 if (stt->handle == data->stt->handle)
189                                         return data;
190                         }
191                         /* Next item */
192                         iter = g_list_next(iter);
193                 }
194         }
195
196         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by stt");
197
198         return NULL;
199 }
200
201 stt_client_s* stt_client_get_by_uid(unsigned int uid)
202 {
203         if (STT_INVALID_UID == uid) {
204                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] out of range : handle"); //LCOV_EXCL_LINE
205                 return NULL;
206         }
207
208         GList *iter = NULL;
209         stt_client_s *data = NULL;
210
211         if (g_list_length(g_client_list) > 0) {
212                 /* Get a first item */
213                 iter = g_list_first(g_client_list);
214
215                 while (NULL != iter) {
216                         data = iter->data;
217                         if (uid == data->uid) {
218                                 return data;
219                         }
220
221                         /* Next item */
222                         iter = g_list_next(iter);
223                 }
224         }
225
226         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by uid"); //LCOV_EXCL_LINE
227
228         return NULL;
229 }
230
231 unsigned int stt_client_get_size()
232 {
233         return g_list_length(g_client_list);
234 }
235
236 int stt_client_use_callback(stt_client_s* client)
237 {
238         client->cb_ref_count++;
239         return 0;
240 }
241
242 int stt_client_not_use_callback(stt_client_s* client)
243 {
244         client->cb_ref_count--;
245         return 0;
246 }
247
248 int stt_client_get_use_callback(stt_client_s* client)
249 {
250         return client->cb_ref_count;
251 }
252
253 GList* stt_client_get_client_list()
254 {
255         return g_client_list;
256 }