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 "stt_client.h"
19 /* Max number of handle */
20 static const int g_max_handle = 999;
21 /* allocated handle */
22 static int g_allocated_handle = 0;
24 static GList *g_client_list = NULL;
27 /* private functions */
28 static int __client_generate_uid(int pid)
32 if (g_allocated_handle > g_max_handle) {
33 g_allocated_handle = 1;
36 /* generate uid, handle number should be smaller than 1000 */
37 return pid * 1000 + g_allocated_handle;
40 int stt_client_new(stt_h* stt)
42 stt_client_s *client = NULL;
44 client = (stt_client_s*)g_malloc0 (sizeof(stt_client_s));
46 stt_h temp = (stt_h)g_malloc0(sizeof(struct stt_s));
47 temp->handle = __client_generate_uid(getpid());
49 /* initialize client data */
51 client->pid = getpid();
52 client->uid = temp->handle;
54 client->result_cb = NULL;
55 client->result_user_data = NULL;
56 client->partial_result_cb = NULL;
57 client->partial_result_user_data = NULL;
58 client->state_changed_cb = NULL;
59 client->state_changed_user_data = NULL;
60 client->error_cb = NULL;
61 client->error_user_data = NULL;
63 client->silence_supported = false;
64 client->profanity_supported = false;
65 client->punctuation_supported = false;
67 client->profanity = STT_OPTION_PROFANITY_AUTO;
68 client->punctuation = STT_OPTION_PUNCTUATION_AUTO;
69 client->silence = STT_OPTION_SILENCE_DETECTION_AUTO;
72 client->data_list = NULL;
73 client->data_count = 0;
76 client->before_state = STT_STATE_CREATED;
77 client->current_state = STT_STATE_CREATED;
79 client->cb_ref_count = 0;
81 g_client_list = g_list_append(g_client_list, client);
88 int stt_client_destroy(stt_h stt)
91 SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL");
96 stt_client_s *data = NULL;
98 /* if list have item */
99 if (g_list_length(g_client_list) > 0) {
100 /* Get a first item */
101 iter = g_list_first(g_client_list);
103 while (NULL != iter) {
105 if (stt->handle == data->stt->handle) {
106 g_client_list = g_list_remove_link(g_client_list, iter);
108 while (0 != data->cb_ref_count)
110 /* wait for release callback function */
119 iter = g_list_next(iter);
123 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] client Not founded");
129 stt_client_s* stt_client_get(stt_h stt)
132 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
137 stt_client_s *data = NULL;
139 if (g_list_length(g_client_list) > 0) {
140 /* Get a first item */
141 iter = g_list_first(g_client_list);
143 while (NULL != iter) {
146 if (stt->handle == data->stt->handle)
150 iter = g_list_next(iter);
154 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by stt");
159 stt_client_s* stt_client_get_by_uid(const int uid)
162 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] out of range : handle");
167 stt_client_s *data = NULL;
169 if (g_list_length(g_client_list) > 0) {
170 /* Get a first item */
171 iter = g_list_first(g_client_list);
173 while (NULL != iter) {
175 if (uid == data->uid) {
180 iter = g_list_next(iter);
184 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by uid");
189 int stt_client_get_size()
191 return g_list_length(g_client_list);
194 int stt_client_use_callback(stt_client_s* client)
196 client->cb_ref_count++;
200 int stt_client_not_use_callback(stt_client_s* client)
202 client->cb_ref_count--;
206 int stt_client_get_use_callback(stt_client_s* client)
208 return client->cb_ref_count;
211 int stt_client_set_option_supported(stt_h stt, bool silence, bool profanity, bool punctuation)
213 stt_client_s* client = stt_client_get(stt);
217 return STT_ERROR_INVALID_PARAMETER;
219 client->silence_supported = silence;
220 client->profanity_supported = profanity;
221 client->punctuation_supported = punctuation;