Fix bug on resume tts and add code for file message(IPC)
[platform/core/uifw/tts.git] / client / tts_client.c
1 /*
2 *  Copyright (c) 2012, 2013 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*)g_malloc0 (sizeof(tts_client_s));
41
42         tts_h temp = (tts_h)g_malloc0(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
62         client->mode = TTS_MODE_DEFAULT;
63         client->before_state = TTS_STATE_CREATED; 
64         client->current_state = TTS_STATE_CREATED; 
65
66         client->cb_ref_count = 0;
67
68         g_client_list = g_list_append(g_client_list, client);
69
70         *tts = temp;
71
72         SLOG(LOG_DEBUG, TAG_TTSC, "[Success] Create client object : uid(%d)", client->uid); 
73
74         return 0;
75 }
76
77 int tts_client_destroy(tts_h tts)
78 {
79         if (tts == NULL) {
80                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
81                 return 0;
82         }       
83
84         GList *iter = NULL;
85         tts_client_s *data = NULL;
86
87         /* if list have item */
88         if (g_list_length(g_client_list) > 0) {
89                 /* Get a first item */
90                 iter = g_list_first(g_client_list);
91
92                 while (NULL != iter) {
93                         data = iter->data;
94                         if (tts->handle == data->tts->handle) {
95                                 g_client_list = g_list_remove_link(g_client_list, iter);
96
97                                 while (0 != data->cb_ref_count)
98                                 {
99                                         /* wait for release callback function */
100                                 }
101                                 free(data);
102                                 free(tts);
103
104                                 return 0;
105                         }
106
107                         /* Next item */
108                         iter = g_list_next(iter);
109                 }
110         }
111         SLOG(LOG_ERROR, TAG_TTSC, "Fail to destroy client : handle is not valid");
112
113         return -1;
114 }
115
116 tts_client_s* tts_client_get(tts_h tts)
117 {
118         if (tts == NULL) {
119                 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
120                 return NULL;
121         }
122
123         GList *iter = NULL;
124         tts_client_s *data = NULL;
125
126         if (g_list_length(g_client_list) > 0) {
127                 /* Get a first item */
128                 iter = g_list_first(g_client_list);
129
130                 while (NULL != iter) {
131                         data = iter->data;
132
133                         if (tts->handle == data->tts->handle) 
134                                 return data;
135
136                         /* Next item */
137                         iter = g_list_next(iter);
138                 }
139         }
140
141         SLOG(LOG_ERROR, TAG_TTSC, "handle is not valid");
142
143         return NULL;
144 }
145
146 tts_client_s* tts_client_get_by_uid(const int uid)
147 {
148         if (uid < 0) {
149                 SLOG(LOG_ERROR, TAG_TTSC, "out of range : handle");
150                 return NULL;
151         }
152
153         GList *iter = NULL;
154         tts_client_s *data = NULL;
155
156         if (g_list_length(g_client_list) > 0) {
157                 /* Get a first item */
158                 iter = g_list_first(g_client_list);
159
160                 while (NULL != iter) {
161                         data = iter->data;
162                         if (uid == data->uid) {
163                                 return data;
164                         }
165
166                         /* Next item */
167                         iter = g_list_next(iter);
168                 }
169         }
170
171         SLOG(LOG_WARN, TAG_TTSC, "uid is not valid");
172
173         return NULL;
174 }
175
176 int tts_client_get_size()
177 {
178         return g_list_length(g_client_list);
179 }
180
181 int tts_client_use_callback(tts_client_s* client)
182 {
183         client->cb_ref_count++;
184         return 0;
185 }
186
187 int tts_client_not_use_callback(tts_client_s* client)
188 {
189         client->cb_ref_count--;
190         return 0;
191 }
192
193 int tts_client_get_use_callback(tts_client_s* client)
194 {
195         return client->cb_ref_count;
196 }
197
198 int tts_client_get_connected_client_count()
199 {
200         GList *iter = NULL;
201         tts_client_s *data = NULL;
202         int number = 0;
203
204         if (g_list_length(g_client_list) > 0) {
205                 /* Get a first item */
206                 iter = g_list_first(g_client_list);
207
208                 while (NULL != iter) {
209                         data = iter->data;
210                         if (0 < data->current_state) {
211                                 number++;
212                         }
213
214                         /* Next item */
215                         iter = g_list_next(iter);
216                 }
217         }
218         return number;
219 }