[ACR-1216][tts][Add tts_repeat()]
[platform/core/uifw/tts.git] / client / tts_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 "tts_client.h"
16
17 /* Max number of handle */
18 static const int g_max_handle = 999;
19 /* allocated handle */
20 static int g_allocated_handle = 0;
21 /* client list */
22 static GList *g_client_list = NULL;
23
24 /* private functions */
25 static int __client_generate_uid(int pid)
26 {
27         g_allocated_handle++;
28
29         if (g_allocated_handle > g_max_handle) {
30                 g_allocated_handle = 1;
31         }
32
33         /* generate uid, handle number should be smaller than 1000 */
34         return pid * 1000 + g_allocated_handle;
35 }
36
37 int tts_client_new(tts_h* tts)
38 {
39         tts_client_s* client = NULL;
40         client = (tts_client_s*)calloc(1, sizeof(tts_client_s));
41         if (NULL == client) {
42                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
43                 return TTS_ERROR_OUT_OF_MEMORY;
44         }
45         tts_h temp = (tts_h)calloc(1, sizeof(struct tts_s));
46         if (NULL == temp) {
47                 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
48                 free(client);
49                 return TTS_ERROR_OUT_OF_MEMORY;
50         }
51         temp->handle = __client_generate_uid(getpid()); 
52
53         /* initialize client data */
54         client->tts = temp;
55         client->pid = getpid(); 
56         client->uid = temp->handle;
57         client->current_utt_id = 0;
58
59         client->state_changed_cb = NULL;
60         client->state_changed_user_data = NULL;
61
62         client->utt_started_cb = NULL;
63         client->utt_started_user_data = NULL;
64         client->utt_completeted_cb = NULL;
65         client->utt_completed_user_data = NULL;
66
67         client->error_cb = NULL;
68         client->error_user_data = NULL;
69         client->default_voice_changed_cb = NULL;
70         client->default_voice_changed_user_data = NULL;
71         client->supported_voice_cb = NULL;
72         client->supported_voice_user_data = NULL;
73
74         client->mode = TTS_MODE_DEFAULT;
75         client->before_state = TTS_STATE_CREATED; 
76         client->current_state = TTS_STATE_CREATED; 
77
78         client->cb_ref_count = 0;
79
80         client->utt_id = 0;
81         client->reason = 0;
82         client->err_msg = NULL;
83
84         client->conn_timer = NULL;
85
86         client->credential = NULL;
87         client->credential_needed = false;
88         client->internal = false;
89
90         client->text_repeat = NULL;
91
92         g_client_list = g_list_append(g_client_list, client);
93
94         *tts = temp;
95
96         SLOG(LOG_DEBUG, TAG_TTSC, "[Success] Create client object : uid(%d)", client->uid); 
97
98         return TTS_ERROR_NONE;
99 }
100
101 int tts_client_destroy(tts_h tts)
102 {
103         if (tts == NULL) {
104                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
105                 return TTS_ERROR_INVALID_PARAMETER;
106         }
107
108         GList *iter = NULL;
109         tts_client_s *data = NULL;
110
111         /* if list have item */
112         if (g_list_length(g_client_list) > 0) {
113                 /* Get a first item */
114                 iter = g_list_first(g_client_list);
115
116                 while (NULL != iter) {
117                         data = iter->data;
118                         if (tts->handle == data->tts->handle) {
119                                 g_client_list = g_list_remove_link(g_client_list, iter);
120
121                                 while (0 != data->cb_ref_count) {
122                                         /* wait for release callback function */
123                                 }
124
125                                 if (NULL != data->err_msg) {
126                                         free(data->err_msg);
127                                         data->err_msg = NULL;
128                                 }
129
130                                 if (NULL != data->credential) {
131                                         free(data->credential);
132                                         data->credential = NULL;
133                                 }
134
135                                 if (NULL != data->text_repeat) {
136                                         free(data->text_repeat);
137                                         data->text_repeat = NULL;
138                                 }
139
140                                 free(data);
141                                 free(tts);
142
143                                 data = NULL;
144                                 tts = NULL;
145
146                                 SLOG(LOG_DEBUG, TAG_TTSC, "Client destroy");
147                                 g_list_free(iter);
148
149                                 return TTS_ERROR_NONE;
150                         }
151
152                         /* Next item */
153                         iter = g_list_next(iter);
154                 }
155         }
156         SLOG(LOG_ERROR, TAG_TTSC, "Fail to destroy client : handle is not valid");
157
158         return TTS_ERROR_INVALID_PARAMETER;
159 }
160
161 tts_client_s* tts_client_get(tts_h tts)
162 {
163         if (tts == NULL) {
164                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
165                 return NULL;
166         }
167
168         GList *iter = NULL;
169         tts_client_s *data = NULL;
170
171         if (g_list_length(g_client_list) > 0) {
172                 /* Get a first item */
173                 iter = g_list_first(g_client_list);
174
175                 while (NULL != iter) {
176                         data = iter->data;
177
178                         if (tts->handle == data->tts->handle) 
179                                 return data;
180
181                         /* Next item */
182                         iter = g_list_next(iter);
183                 }
184         }
185
186         SLOG(LOG_ERROR, TAG_TTSC, "handle is not valid");
187
188         return NULL;
189 }
190
191 tts_client_s* tts_client_get_by_uid(const int uid)
192 {
193         if (uid < 0) {
194                 SLOG(LOG_ERROR, TAG_TTSC, "out of range : handle");
195                 return NULL;
196         }
197
198         GList *iter = NULL;
199         tts_client_s *data = NULL;
200
201         if (g_list_length(g_client_list) > 0) {
202                 /* Get a first item */
203                 iter = g_list_first(g_client_list);
204
205                 while (NULL != iter) {
206                         data = iter->data;
207                         if (uid == data->uid) {
208                                 return data;
209                         }
210
211                         /* Next item */
212                         iter = g_list_next(iter);
213                 }
214         }
215
216         SLOG(LOG_WARN, TAG_TTSC, "uid is not valid");
217
218         return NULL;
219 }
220
221 int tts_client_get_size()
222 {
223         return g_list_length(g_client_list);
224 }
225
226 int tts_client_use_callback(tts_client_s* client)
227 {
228         client->cb_ref_count++;
229         return 0;
230 }
231
232 int tts_client_not_use_callback(tts_client_s* client)
233 {
234         client->cb_ref_count--;
235         return 0;
236 }
237
238 int tts_client_get_use_callback(tts_client_s* client)
239 {
240         return client->cb_ref_count;
241 }
242
243 int tts_client_get_connected_client_count()
244 {
245         GList *iter = NULL;
246         tts_client_s *data = NULL;
247         int number = 0;
248
249         if (g_list_length(g_client_list) > 0) {
250                 /* Get a first item */
251                 iter = g_list_first(g_client_list);
252
253                 while (NULL != iter) {
254                         data = iter->data;
255                         if (0 < data->current_state) {
256                                 number++;
257                         }
258
259                         /* Next item */
260                         iter = g_list_next(iter);
261                 }
262         }
263         return number;
264 }
265
266 int tts_client_get_mode_client_count(tts_mode_e mode)
267 {
268         GList *iter = NULL;
269         tts_client_s *data = NULL;
270         int number = 0;
271
272         if (g_list_length(g_client_list) > 0) {
273                 /* Get a first item */
274                 iter = g_list_first(g_client_list);
275
276                 while (NULL != iter) {
277                         data = iter->data;
278                         if (0 < data->current_state && data->mode == mode) {
279                                 number++;
280                         }
281
282                         /* Next item */
283                         iter = g_list_next(iter);
284                 }
285         }
286         return number;
287 }
288
289 GList* tts_client_get_client_list()
290 {
291         return g_client_list;
292 }