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.
15 #include "tts_client.h"
17 /* Max number of handle */
18 static const int g_max_handle = 999;
19 /* allocated handle */
20 static int g_allocated_handle = 0;
22 static GList *g_client_list = NULL;
24 /* private functions */
25 static int __client_generate_uid(int pid)
29 if (g_allocated_handle > g_max_handle) {
30 g_allocated_handle = 1;
33 /* generate uid, handle number should be smaller than 1000 */
34 return pid * 1000 + g_allocated_handle;
37 int tts_client_new(tts_h* tts)
39 tts_client_s* client = NULL;
40 client = (tts_client_s*)calloc(1, sizeof(tts_client_s));
42 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
43 return TTS_ERROR_OUT_OF_MEMORY;
45 tts_h temp = (tts_h)calloc(1, sizeof(struct tts_s));
47 SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
49 return TTS_ERROR_OUT_OF_MEMORY;
51 temp->handle = __client_generate_uid(getpid());
53 /* initialize client data */
55 client->pid = getpid();
56 client->uid = temp->handle;
57 client->current_utt_id = 0;
59 client->state_changed_cb = NULL;
60 client->state_changed_user_data = NULL;
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;
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;
74 client->mode = TTS_MODE_DEFAULT;
75 client->before_state = TTS_STATE_CREATED;
76 client->current_state = TTS_STATE_CREATED;
78 client->cb_ref_count = 0;
82 client->err_msg = NULL;
84 client->conn_timer = NULL;
86 client->credential = NULL;
87 client->credential_needed = false;
88 client->internal = false;
90 g_client_list = g_list_append(g_client_list, client);
94 SLOG(LOG_DEBUG, TAG_TTSC, "[Success] Create client object : uid(%d)", client->uid);
96 return TTS_ERROR_NONE;
99 int tts_client_destroy(tts_h tts)
102 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
103 return TTS_ERROR_INVALID_PARAMETER;
107 tts_client_s *data = NULL;
109 /* if list have item */
110 if (g_list_length(g_client_list) > 0) {
111 /* Get a first item */
112 iter = g_list_first(g_client_list);
114 while (NULL != iter) {
116 if (tts->handle == data->tts->handle) {
117 g_client_list = g_list_remove_link(g_client_list, iter);
119 while (0 != data->cb_ref_count) {
120 /* wait for release callback function */
123 if (NULL != data->err_msg) {
125 data->err_msg = NULL;
128 if (NULL != data->credential) {
129 free(data->credential);
130 data->credential = NULL;
139 SLOG(LOG_DEBUG, TAG_TTSC, "Client destroy");
142 return TTS_ERROR_NONE;
146 iter = g_list_next(iter);
149 SLOG(LOG_ERROR, TAG_TTSC, "Fail to destroy client : handle is not valid");
151 return TTS_ERROR_INVALID_PARAMETER;
154 tts_client_s* tts_client_get(tts_h tts)
157 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
162 tts_client_s *data = NULL;
164 if (g_list_length(g_client_list) > 0) {
165 /* Get a first item */
166 iter = g_list_first(g_client_list);
168 while (NULL != iter) {
171 if (tts->handle == data->tts->handle)
175 iter = g_list_next(iter);
179 SLOG(LOG_ERROR, TAG_TTSC, "handle is not valid");
184 tts_client_s* tts_client_get_by_uid(const int uid)
187 SLOG(LOG_ERROR, TAG_TTSC, "out of range : handle");
192 tts_client_s *data = NULL;
194 if (g_list_length(g_client_list) > 0) {
195 /* Get a first item */
196 iter = g_list_first(g_client_list);
198 while (NULL != iter) {
200 if (uid == data->uid) {
205 iter = g_list_next(iter);
209 SLOG(LOG_WARN, TAG_TTSC, "uid is not valid");
214 int tts_client_get_size()
216 return g_list_length(g_client_list);
219 int tts_client_use_callback(tts_client_s* client)
221 client->cb_ref_count++;
225 int tts_client_not_use_callback(tts_client_s* client)
227 client->cb_ref_count--;
231 int tts_client_get_use_callback(tts_client_s* client)
233 return client->cb_ref_count;
236 int tts_client_get_connected_client_count()
239 tts_client_s *data = NULL;
242 if (g_list_length(g_client_list) > 0) {
243 /* Get a first item */
244 iter = g_list_first(g_client_list);
246 while (NULL != iter) {
248 if (0 < data->current_state) {
253 iter = g_list_next(iter);
259 int tts_client_get_mode_client_count(tts_mode_e mode)
262 tts_client_s *data = NULL;
265 if (g_list_length(g_client_list) > 0) {
266 /* Get a first item */
267 iter = g_list_first(g_client_list);
269 while (NULL != iter) {
271 if (0 < data->current_state && data->mode == mode) {
276 iter = g_list_next(iter);
282 GList* tts_client_get_client_list()
284 return g_client_list;