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.
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*)g_malloc0 (sizeof(tts_client_s));
42 tts_h temp = (tts_h)g_malloc0(sizeof(struct tts_s));
43 temp->handle = __client_generate_uid(getpid());
45 /* initialize client data */
47 client->pid = getpid();
48 client->uid = temp->handle;
49 client->current_utt_id = 0;
51 client->state_changed_cb = NULL;
52 client->state_changed_user_data = NULL;
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;
59 client->error_cb = NULL;
60 client->error_user_data = NULL;
62 client->before_state = TTS_STATE_CREATED;
63 client->current_state = TTS_STATE_CREATED;
65 client->cb_ref_count = 0;
67 g_client_list = g_list_append(g_client_list, client);
71 SLOG(LOG_DEBUG, TAG_TTSC, "[Success] Create client object : uid(%d)", client->uid);
76 int tts_client_destroy(tts_h tts)
79 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
84 tts_client_s *data = NULL;
86 /* if list have item */
87 if (g_list_length(g_client_list) > 0) {
88 /* Get a first item */
89 iter = g_list_first(g_client_list);
91 while (NULL != iter) {
93 if (tts->handle == data->tts->handle) {
94 g_client_list = g_list_remove_link(g_client_list, iter);
96 while (0 != data->cb_ref_count)
98 /* wait for release callback function */
107 iter = g_list_next(iter);
110 SLOG(LOG_ERROR, TAG_TTSC, "Fail to destroy client : handle is not valid");
115 tts_client_s* tts_client_get(tts_h tts)
118 SLOG(LOG_ERROR, TAG_TTSC, "Input parameter is NULL");
123 tts_client_s *data = NULL;
125 if (g_list_length(g_client_list) > 0) {
126 /* Get a first item */
127 iter = g_list_first(g_client_list);
129 while (NULL != iter) {
132 if (tts->handle == data->tts->handle)
136 iter = g_list_next(iter);
140 SLOG(LOG_ERROR, TAG_TTSC, "handle is not valid");
145 tts_client_s* tts_client_get_by_uid(const int uid)
148 SLOG(LOG_ERROR, TAG_TTSC, "out of range : handle");
153 tts_client_s *data = NULL;
155 if (g_list_length(g_client_list) > 0) {
156 /* Get a first item */
157 iter = g_list_first(g_client_list);
159 while (NULL != iter) {
161 if (uid == data->uid) {
166 iter = g_list_next(iter);
170 SLOG(LOG_WARN, TAG_TTSC, "uid is not valid");
175 int tts_client_get_size()
177 return g_list_length(g_client_list);
180 int tts_client_use_callback(tts_client_s* client)
182 client->cb_ref_count++;
186 int tts_client_not_use_callback(tts_client_s* client)
188 client->cb_ref_count--;