Fix pkgmgr status and move hello_timer into client handle
[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         client->hello_timer = NULL;
86
87         client->credential = NULL;
88         client->credential_needed = false;
89         client->internal = false;
90
91         client->text_repeat = NULL;
92
93         g_client_list = g_list_append(g_client_list, client);
94
95         *tts = temp;
96
97         SLOG(LOG_ERROR, TAG_TTSC, "[Success] Create client object : uid(%d)", client->uid);
98
99         return TTS_ERROR_NONE;
100 }
101
102 int tts_client_destroy(tts_h tts)
103 {
104         if (tts == NULL) {
105                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
106                 return TTS_ERROR_INVALID_PARAMETER;
107         }
108
109         GList *iter = NULL;
110         tts_client_s *data = NULL;
111
112         /* if list have item */
113         if (g_list_length(g_client_list) > 0) {
114                 /* Get a first item */
115                 iter = g_list_first(g_client_list);
116
117                 while (NULL != iter) {
118                         data = iter->data;
119                         if (tts->handle == data->tts->handle) {
120                                 g_client_list = g_list_remove_link(g_client_list, iter);
121
122                                 while (0 != data->cb_ref_count) {
123                                         /* wait for release callback function */
124                                 }
125
126                                 if (NULL != data->err_msg) {
127                                         free(data->err_msg);
128                                         data->err_msg = NULL;
129                                 }
130
131                                 if (NULL != data->credential) {
132                                         free(data->credential);
133                                         data->credential = NULL;
134                                 }
135
136                                 if (NULL != data->text_repeat) {
137                                         free(data->text_repeat);
138                                         data->text_repeat = NULL;
139                                 }
140
141                                 int uid = data->uid;
142                                 free(data);
143                                 free(tts);
144
145                                 data = NULL;
146                                 tts = NULL;
147
148                                 g_list_free(iter);
149
150                                 SLOG(LOG_ERROR, TAG_TTSC, "Client destroy : uid(%d)", uid);
151                                 return TTS_ERROR_NONE;
152                         }
153
154                         /* Next item */
155                         iter = g_list_next(iter);
156                 }
157         }
158         SLOG(LOG_ERROR, TAG_TTSC, "Fail to destroy client : handle is not valid");
159
160         return TTS_ERROR_INVALID_PARAMETER;
161 }
162
163 tts_client_s* tts_client_get(tts_h tts)
164 {
165         if (tts == NULL) {
166                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
167                 return NULL;
168         }
169
170         GList *iter = NULL;
171         tts_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
180                         if (tts->handle == data->tts->handle) 
181                                 return data;
182
183                         /* Next item */
184                         iter = g_list_next(iter);
185                 }
186         }
187
188         SLOG(LOG_ERROR, TAG_TTSC, "handle is not valid");
189
190         return NULL;
191 }
192
193 tts_client_s* tts_client_get_by_uid(const int uid)
194 {
195         if (uid < 0) {
196                 SLOG(LOG_ERROR, TAG_TTSC, "out of range : handle");
197                 return NULL;
198         }
199
200         GList *iter = NULL;
201         tts_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_WARN, TAG_TTSC, "uid is not valid");
219
220         return NULL;
221 }
222
223 int tts_client_get_size()
224 {
225         return g_list_length(g_client_list);
226 }
227
228 int tts_client_use_callback(tts_client_s* client)
229 {
230         client->cb_ref_count++;
231         return 0;
232 }
233
234 int tts_client_not_use_callback(tts_client_s* client)
235 {
236         client->cb_ref_count--;
237         return 0;
238 }
239
240 int tts_client_get_use_callback(tts_client_s* client)
241 {
242         return client->cb_ref_count;
243 }
244
245 //LCOV_EXCL_START
246 int tts_client_get_connected_client_count()
247 {
248         GList *iter = NULL;
249         tts_client_s *data = NULL;
250         int number = 0;
251
252         if (g_list_length(g_client_list) > 0) {
253                 /* Get a first item */
254                 iter = g_list_first(g_client_list);
255
256                 while (NULL != iter) {
257                         data = iter->data;
258                         if (0 < data->current_state) {
259                                 number++;
260                         }
261
262                         /* Next item */
263                         iter = g_list_next(iter);
264                 }
265         }
266         return number;
267 }
268
269 int tts_client_get_mode_client_count(tts_mode_e mode)
270 {
271         GList *iter = NULL;
272         tts_client_s *data = NULL;
273         int number = 0;
274
275         if (g_list_length(g_client_list) > 0) {
276                 /* Get a first item */
277                 iter = g_list_first(g_client_list);
278
279                 while (NULL != iter) {
280                         data = iter->data;
281                         if (0 < data->current_state && data->mode == mode) {
282                                 number++;
283                         }
284
285                         /* Next item */
286                         iter = g_list_next(iter);
287                 }
288         }
289         return number;
290 }
291
292 GList* tts_client_get_client_list()
293 {
294         return g_client_list;
295 }
296 //LCOV_EXCL_STOP