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 "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*)calloc(1, sizeof(stt_client_s));
46 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
47 return STT_ERROR_OUT_OF_MEMORY;
50 stt_h temp = (stt_h)calloc(1, sizeof(struct stt_s));
52 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
54 return STT_ERROR_OUT_OF_MEMORY;
56 temp->handle = __client_generate_uid(getpid());
58 /* initialize client data */
60 client->pid = getpid();
61 client->uid = temp->handle;
63 client->recognition_result_cb = NULL;
64 client->recognition_result_user_data = NULL;
65 client->state_changed_cb = NULL;
66 client->state_changed_user_data = NULL;
67 client->error_cb = NULL;
68 client->error_user_data = NULL;
69 client->default_lang_changed_cb = NULL;
70 client->default_lang_changed_user_data = NULL;
71 client->speech_status_cb = NULL;
72 client->speech_status_user_data = NULL;
74 client->current_engine_id = NULL;
75 client->credential = NULL;
77 client->silence_supported = false;
78 client->silence = STT_OPTION_SILENCE_DETECTION_AUTO;
81 client->data_list = NULL;
82 client->data_count = 0;
86 client->err_msg = NULL;
88 client->before_state = STT_STATE_CREATED;
89 client->current_state = STT_STATE_CREATED;
91 client->internal_state = STT_INTERNAL_STATE_NONE;
93 client->speech_status = -1;
95 client->cb_ref_count = 0;
97 g_client_list = g_list_append(g_client_list, client);
104 int stt_client_destroy(stt_h stt)
107 SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL");
112 stt_client_s *data = NULL;
114 /* if list have item */
115 if (g_list_length(g_client_list) > 0) {
116 /* Get a first item */
117 iter = g_list_first(g_client_list);
119 while (NULL != iter) {
121 if (stt->handle == data->stt->handle) {
122 g_client_list = g_list_remove_link(g_client_list, iter);
124 while (0 != data->cb_ref_count) {
125 /* wait for release callback function */
128 if (NULL != data->current_engine_id) {
129 free(data->current_engine_id);
132 if (NULL != data->err_msg) {
134 data->err_msg = NULL;
137 if (NULL != data->credential) {
138 free(data->credential);
139 data->credential = NULL;
151 iter = g_list_next(iter);
155 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] client Not founded");
161 stt_client_s* stt_client_get(stt_h stt)
164 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
169 stt_client_s *data = NULL;
171 if (g_list_length(g_client_list) > 0) {
172 /* Get a first item */
173 iter = g_list_first(g_client_list);
175 while (NULL != iter) {
178 if (stt->handle == data->stt->handle)
182 iter = g_list_next(iter);
186 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by stt");
191 stt_client_s* stt_client_get_by_uid(const int uid)
194 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] out of range : handle");
199 stt_client_s *data = NULL;
201 if (g_list_length(g_client_list) > 0) {
202 /* Get a first item */
203 iter = g_list_first(g_client_list);
205 while (NULL != iter) {
207 if (uid == data->uid) {
212 iter = g_list_next(iter);
216 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by uid");
221 int stt_client_get_size()
223 return g_list_length(g_client_list);
226 int stt_client_use_callback(stt_client_s* client)
228 client->cb_ref_count++;
232 int stt_client_not_use_callback(stt_client_s* client)
234 client->cb_ref_count--;
238 int stt_client_get_use_callback(stt_client_s* client)
240 return client->cb_ref_count;
243 GList* stt_client_get_client_list()
245 return g_client_list;