Merge with Tizen 2.3
[platform/core/uifw/tts.git] / client / tts_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 "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
42         tts_h temp = (tts_h)calloc(1, sizeof(struct tts_s));
43         temp->handle = __client_generate_uid(getpid()); 
44
45         /* initialize client data */
46         client->tts = temp;
47         client->pid = getpid(); 
48         client->uid = temp->handle;
49         client->current_utt_id = 0;
50
51         client->state_changed_cb = NULL;
52         client->state_changed_user_data = NULL;
53         
54         client->utt_started_cb = NULL;
55         client->utt_started_user_data = NULL;
56         client->utt_completeted_cb = NULL;
57         client->utt_completed_user_data = NULL;
58
59         client->error_cb = NULL;
60         client->error_user_data = NULL;
61         client->default_voice_changed_cb = NULL;
62         client->default_voice_changed_user_data = NULL;
63         client->supported_voice_cb = NULL;
64         client->supported_voice_user_data = NULL;
65
66         client->mode = TTS_MODE_DEFAULT;
67         client->before_state = TTS_STATE_CREATED; 
68         client->current_state = TTS_STATE_CREATED; 
69
70         client->cb_ref_count = 0;
71
72         g_client_list = g_list_append(g_client_list, client);
73
74         *tts = temp;
75
76         SECURE_SLOG(LOG_DEBUG, TAG_TTSC, "[Success] Create client object : uid(%d)", client->uid); 
77
78         return 0;
79 }
80
81 int tts_client_destroy(tts_h tts)
82 {
83         if (tts == NULL) {
84                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
85                 return 0;
86         }       
87
88         GList *iter = NULL;
89         tts_client_s *data = NULL;
90
91         /* if list have item */
92         if (g_list_length(g_client_list) > 0) {
93                 /* Get a first item */
94                 iter = g_list_first(g_client_list);
95
96                 while (NULL != iter) {
97                         data = iter->data;
98                         if (tts->handle == data->tts->handle) {
99                                 g_client_list = g_list_remove_link(g_client_list, iter);
100
101                                 while (0 != data->cb_ref_count)
102                                 {
103                                         /* wait for release callback function */
104                                 }
105                                 free(data);
106                                 free(tts);
107
108                                 return 0;
109                         }
110
111                         /* Next item */
112                         iter = g_list_next(iter);
113                 }
114         }
115         SLOG(LOG_ERROR, TAG_TTSC, "Fail to destroy client : handle is not valid");
116
117         return -1;
118 }
119
120 tts_client_s* tts_client_get(tts_h tts)
121 {
122         if (tts == NULL) {
123                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
124                 return NULL;
125         }
126
127         GList *iter = NULL;
128         tts_client_s *data = NULL;
129
130         if (g_list_length(g_client_list) > 0) {
131                 /* Get a first item */
132                 iter = g_list_first(g_client_list);
133
134                 while (NULL != iter) {
135                         data = iter->data;
136
137                         if (tts->handle == data->tts->handle) 
138                                 return data;
139
140                         /* Next item */
141                         iter = g_list_next(iter);
142                 }
143         }
144
145         SLOG(LOG_ERROR, TAG_TTSC, "handle is not valid");
146
147         return NULL;
148 }
149
150 tts_client_s* tts_client_get_by_uid(const int uid)
151 {
152         if (uid < 0) {
153                 SLOG(LOG_ERROR, TAG_TTSC, "out of range : handle");
154                 return NULL;
155         }
156
157         GList *iter = NULL;
158         tts_client_s *data = NULL;
159
160         if (g_list_length(g_client_list) > 0) {
161                 /* Get a first item */
162                 iter = g_list_first(g_client_list);
163
164                 while (NULL != iter) {
165                         data = iter->data;
166                         if (uid == data->uid) {
167                                 return data;
168                         }
169
170                         /* Next item */
171                         iter = g_list_next(iter);
172                 }
173         }
174
175         SLOG(LOG_WARN, TAG_TTSC, "uid is not valid");
176
177         return NULL;
178 }
179
180 int tts_client_get_size()
181 {
182         return g_list_length(g_client_list);
183 }
184
185 int tts_client_use_callback(tts_client_s* client)
186 {
187         client->cb_ref_count++;
188         return 0;
189 }
190
191 int tts_client_not_use_callback(tts_client_s* client)
192 {
193         client->cb_ref_count--;
194         return 0;
195 }
196
197 int tts_client_get_use_callback(tts_client_s* client)
198 {
199         return client->cb_ref_count;
200 }
201
202 int tts_client_get_connected_client_count()
203 {
204         GList *iter = NULL;
205         tts_client_s *data = NULL;
206         int number = 0;
207
208         if (g_list_length(g_client_list) > 0) {
209                 /* Get a first item */
210                 iter = g_list_first(g_client_list);
211
212                 while (NULL != iter) {
213                         data = iter->data;
214                         if (0 < data->current_state) {
215                                 number++;
216                         }
217
218                         /* Next item */
219                         iter = g_list_next(iter);
220                 }
221         }
222         return number;
223 }
224
225 int tts_client_get_mode_client_count(tts_mode_e mode)
226 {
227         GList *iter = NULL;
228         tts_client_s *data = NULL;
229         int number = 0;
230
231         if (g_list_length(g_client_list) > 0) {
232                 /* Get a first item */
233                 iter = g_list_first(g_client_list);
234
235                 while (NULL != iter) {
236                         data = iter->data;
237                         if (0 < data->current_state && data->mode == mode) {
238                                 number++;
239                         }
240
241                         /* Next item */
242                         iter = g_list_next(iter);
243                 }
244         }
245         return number;
246 }
247
248 GList* tts_client_get_client_list()
249 {
250         return g_client_list;
251 }