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"
16 #include "stt_internal.h"
20 /* Max number of handle */
21 static const int g_max_handle = 999;
22 /* allocated handle */
23 static unsigned int g_allocated_handle = 0;
25 static GList *g_client_list = NULL;
28 /* private functions */
29 static unsigned int __client_generate_uid(unsigned int pid)
33 if (g_allocated_handle > g_max_handle) {
34 g_allocated_handle = 1;
37 /* generate uid, handle number should be smaller than 1000 */
38 return pid * 1000u + g_allocated_handle;
41 int stt_client_new(stt_h* stt)
43 stt_client_s *client = NULL;
45 client = (stt_client_s*)calloc(1, sizeof(stt_client_s));
47 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
48 return STT_ERROR_OUT_OF_MEMORY;
51 stt_h temp = (stt_h)calloc(1, sizeof(struct stt_s));
53 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
55 return STT_ERROR_OUT_OF_MEMORY;
57 temp->handle = __client_generate_uid((unsigned int)getpid());
59 /* initialize client data */
61 client->pid = getpid();
62 client->uid = temp->handle;
64 client->recognition_result_cb = NULL;
65 client->recognition_result_user_data = NULL;
66 client->state_changed_cb = NULL;
67 client->state_changed_user_data = NULL;
68 client->error_cb = NULL;
69 client->error_user_data = NULL;
70 client->default_lang_changed_cb = NULL;
71 client->default_lang_changed_user_data = NULL;
72 client->speech_status_cb = NULL;
73 client->speech_status_user_data = NULL;
75 client->current_engine_id = NULL;
76 client->credential = NULL;
78 client->audio_id = strdup(STT_AUDIO_ID_BLUETOOTH);
80 client->audio_id = strdup(STT_AUDIO_ID_NONE);
82 client->silence_supported = false;
83 client->silence = STT_OPTION_SILENCE_DETECTION_AUTO;
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 client->internal = false;
98 client->is_streaming = false;
100 g_client_list = g_list_append(g_client_list, client);
107 int stt_client_destroy(stt_h stt)
110 SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL"); //LCOV_EXCL_LINE
115 stt_client_s *data = NULL;
117 /* if list have item */
118 if (g_list_length(g_client_list) > 0) {
119 /* Get a first item */
120 iter = g_list_first(g_client_list);
122 while (NULL != iter) {
124 if (stt->handle == data->stt->handle) {
125 g_client_list = g_list_remove_link(g_client_list, iter);
127 while (0 != data->cb_ref_count) {
128 /* wait for release callback function */
131 if (NULL != data->current_engine_id) {
132 free(data->current_engine_id);
135 if (NULL != data->err_msg) {
137 data->err_msg = NULL;
140 if (NULL != data->credential) {
141 free(data->credential);
142 data->credential = NULL;
145 if (NULL != data->audio_id) {
146 free(data->audio_id);
147 data->audio_id = NULL;
161 iter = g_list_next(iter);
165 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] client Not founded"); //LCOV_EXCL_LINE
171 stt_client_s* stt_client_get(stt_h stt)
174 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
179 stt_client_s *data = NULL;
181 if (g_list_length(g_client_list) > 0) {
182 /* Get a first item */
183 iter = g_list_first(g_client_list);
185 while (NULL != iter) {
188 if (stt->handle == data->stt->handle)
192 iter = g_list_next(iter);
196 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by stt");
201 stt_client_s* stt_client_get_by_uid(unsigned int uid)
203 if (STT_INVALID_UID == uid) {
204 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] out of range : handle"); //LCOV_EXCL_LINE
209 stt_client_s *data = NULL;
211 if (g_list_length(g_client_list) > 0) {
212 /* Get a first item */
213 iter = g_list_first(g_client_list);
215 while (NULL != iter) {
217 if (uid == data->uid) {
222 iter = g_list_next(iter);
226 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by uid"); //LCOV_EXCL_LINE
231 unsigned int stt_client_get_size()
233 return g_list_length(g_client_list);
236 int stt_client_use_callback(stt_client_s* client)
238 client->cb_ref_count++;
242 int stt_client_not_use_callback(stt_client_s* client)
244 client->cb_ref_count--;
248 int stt_client_get_use_callback(stt_client_s* client)
250 return client->cb_ref_count;
253 GList* stt_client_get_client_list()
255 return g_client_list;