Add critical section to avoid thread safety issue
[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
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*)calloc(1, sizeof(stt_client_s));
45         if (NULL == client) {
46                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
47                 return STT_ERROR_OUT_OF_MEMORY;
48         }
49
50         stt_h temp = (stt_h)calloc(1, sizeof(struct stt_s));
51         if (NULL == temp) {
52                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
53                 free(client);
54                 return STT_ERROR_OUT_OF_MEMORY;
55         }
56         temp->handle = __client_generate_uid(getpid());
57
58         /* initialize client data */
59         client->stt = temp;
60         client->pid = getpid();
61         client->uid = temp->handle;
62
63         client->recognition_result_cb = NULL;
64         client->recognition_result_user_data = NULL;
65         client->state_changed_cb = NULL;
66         client->state_changed_user_data = NULL;
67         client->error_cb = NULL;
68         client->error_user_data = NULL;
69         client->default_lang_changed_cb = NULL;
70         client->default_lang_changed_user_data = NULL;
71         client->speech_status_cb = NULL;
72         client->speech_status_user_data = NULL;
73
74         client->current_engine_id = NULL;
75         client->credential = NULL;
76
77         client->silence_supported = false;
78         client->silence = STT_OPTION_SILENCE_DETECTION_AUTO;
79
80         client->event = 0;
81         client->data_list = NULL;
82         client->data_count = 0;
83         client->msg = NULL;
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
99         g_client_list = g_list_append(g_client_list, client);
100
101         *stt = temp;
102
103         return 0;
104 }
105
106 int stt_client_destroy(stt_h stt)
107 {
108         if (stt == NULL) {
109                 SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL");
110                 return 0;
111         }
112
113         GList *iter = NULL;
114         stt_client_s *data = NULL;
115
116         /* if list have item */
117         if (g_list_length(g_client_list) > 0) {
118                 /* Get a first item */
119                 iter = g_list_first(g_client_list);
120
121                 while (NULL != iter) {
122                         data = iter->data;
123                         if (stt->handle == data->stt->handle) {
124                                 g_client_list = g_list_remove_link(g_client_list, iter);
125
126                                 while (0 != data->cb_ref_count) {
127                                         /* wait for release callback function */
128                                 }
129
130                                 if (NULL != data->current_engine_id) {
131                                         free(data->current_engine_id);
132                                 }
133
134                                 if (NULL != data->err_msg) {
135                                         free(data->err_msg);
136                                         data->err_msg = NULL;
137                                 }
138
139                                 if (NULL != data->credential) {
140                                         free(data->credential);
141                                         data->credential = NULL;
142                                 }
143
144                                 free(data);
145                                 free(stt);
146                                 data = NULL;
147                                 stt = NULL;
148
149                                 return 0;
150                         }
151
152                         /* Next item */
153                         iter = g_list_next(iter);
154                 }
155         }
156
157         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] client Not founded");
158
159         return -1;
160 }
161
162
163 stt_client_s* stt_client_get(stt_h stt)
164 {
165         if (NULL == stt) {
166                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
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 (NULL != data) {
180                                 if (stt->handle == data->stt->handle)
181                                         return data;
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 stt");
189
190         return NULL;
191 }
192
193 stt_client_s* stt_client_get_by_uid(const int uid)
194 {
195         if (uid < 0) {
196                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] out of range : handle");
197                 return NULL;
198         }
199
200         GList *iter = NULL;
201         stt_client_s *data = NULL;
202
203         if (g_list_length(g_client_list) > 0) {
204                 /* Get a first item */
205                 iter = g_list_first(g_client_list);
206
207                 while (NULL != iter) {
208                         data = iter->data;
209                         if (uid == data->uid) {
210                                 return data;
211                         }
212
213                         /* Next item */
214                         iter = g_list_next(iter);
215                 }
216         }
217
218         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by uid");
219
220         return NULL;
221 }
222
223 int stt_client_get_size()
224 {
225         return g_list_length(g_client_list);
226 }
227
228 int stt_client_use_callback(stt_client_s* client)
229 {
230         client->cb_ref_count++;
231         return 0;
232 }
233
234 int stt_client_not_use_callback(stt_client_s* client)
235 {
236         client->cb_ref_count--;
237         return 0;
238 }
239
240 int stt_client_get_use_callback(stt_client_s* client)
241 {
242         return client->cb_ref_count;
243 }
244
245 GList* stt_client_get_client_list()
246 {
247         return g_client_list;
248 }