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 <stdatomic.h>
17 #include "sttd_main.h"
19 #include "sttd_client_data.h"
22 static GSList *g_client_list = NULL;
24 static atomic_uint g_cur_recog_uid = 0;
26 int client_show_list()
29 client_info_s *data = NULL;
31 SLOG(LOG_DEBUG, TAG_STTD, "----- client list");
33 if (g_slist_length(g_client_list) > 0) {
34 /* Get a first item */
35 iter = g_slist_nth(g_client_list, 0);
38 while (NULL != iter) {
39 /*Get handle data from list*/
42 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[%dth] uid(%u), state(%d)", i, data->uid, data->state);
45 iter = g_slist_next(iter);
49 SLOG(LOG_DEBUG, TAG_STTD, "No Client");
52 SLOG(LOG_DEBUG, TAG_STTD, "-----");
57 GSList* __client_get_item(unsigned int uid)
60 client_info_s *data = NULL;
62 if (0 < g_slist_length(g_client_list)) {
63 iter = g_slist_nth(g_client_list, 0);
65 while (NULL != iter) {
66 /* Get handle data from list */
72 iter = g_slist_next(iter);
79 int sttd_client_add(int pid, unsigned int uid)
81 /*Check uid is duplicated*/
83 tmp = __client_get_item(uid);
86 SLOG(LOG_WARN, TAG_STTD, "[Client Data] Client uid is already registered");
87 return STTD_ERROR_INVALID_PARAMETER;
90 client_info_s *info = (client_info_s*)calloc(1, sizeof(client_info_s));
92 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to allocate memory");
93 return STTD_ERROR_OUT_OF_MEMORY;
98 info->start_beep = NULL;
99 info->stop_beep = NULL;
100 info->state = APP_STATE_READY;
102 info->app_agreed = false;
104 /* Add item to global list */
105 g_client_list = g_slist_append(g_client_list, info);
107 if (NULL == g_client_list) {
108 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to add new client");
113 return STTD_ERROR_OPERATION_FAILED;
116 #ifdef CLIENT_DATA_DEBUG
122 int sttd_client_delete(unsigned int uid)
125 client_info_s* hnd = NULL;
128 tmp = __client_get_item(uid);
130 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
131 return STTD_ERROR_INVALID_PARAMETER;
134 /*Free client structure*/
137 if (NULL != hnd->start_beep) free(hnd->start_beep);
138 if (NULL != hnd->stop_beep) free(hnd->stop_beep);
142 /*Remove handle from list*/
143 g_client_list = g_slist_remove_link(g_client_list, tmp);
145 #ifdef CLIENT_DATA_DEBUG
152 int sttd_client_get_start_sound(unsigned int uid, char** filename)
154 if (NULL == filename) {
155 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Filename is NULL");
156 return STTD_ERROR_INVALID_PARAMETER;
160 client_info_s* hnd = NULL;
162 tmp = __client_get_item(uid);
164 return STTD_ERROR_INVALID_PARAMETER;
168 if (NULL != hnd->start_beep) {
169 *filename = strdup(hnd->start_beep);
177 int sttd_client_set_start_sound(unsigned int uid, const char* filename)
180 client_info_s* hnd = NULL;
182 tmp = __client_get_item(uid);
184 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
185 return STTD_ERROR_INVALID_PARAMETER;
189 if (NULL != hnd->start_beep) {
190 free(hnd->start_beep);
193 if (NULL != filename) {
194 hnd->start_beep = strdup(filename);
195 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Start sound file : %s", hnd->start_beep);
197 hnd->start_beep = NULL;
203 int sttd_client_get_stop_sound(unsigned int uid, char** filename)
205 if (NULL == filename) {
206 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Filename is NULL");
207 return STTD_ERROR_INVALID_PARAMETER;
211 client_info_s* hnd = NULL;
213 tmp = __client_get_item(uid);
215 return STTD_ERROR_INVALID_PARAMETER;
219 if (NULL != hnd->stop_beep) {
220 *filename = strdup(hnd->stop_beep);
228 int sttd_client_set_stop_sound(unsigned int uid, const char* filename)
231 client_info_s* hnd = NULL;
233 tmp = __client_get_item(uid);
235 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
236 return STTD_ERROR_INVALID_PARAMETER;
240 if (NULL != hnd->stop_beep) {
241 free(hnd->stop_beep);
244 if (NULL != filename) {
245 hnd->stop_beep = strdup(filename);
246 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Stop sound file : %s", hnd->stop_beep);
248 hnd->stop_beep = NULL;
254 int sttd_client_get_state(unsigned int uid, app_state_e* state)
257 client_info_s* hnd = NULL;
259 tmp = __client_get_item(uid);
261 return STTD_ERROR_INVALID_PARAMETER;
270 int sttd_client_set_state(unsigned int uid, app_state_e state)
273 client_info_s* hnd = NULL;
275 tmp = __client_get_item(uid);
277 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
278 return STTD_ERROR_INVALID_PARAMETER;
287 int sttd_client_get_ref_count()
289 int count = g_slist_length(g_client_list);
294 int sttd_client_get_pid(unsigned int uid)
297 client_info_s* hnd = NULL;
299 tmp = __client_get_item(uid);
301 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] sttd_client_get_pid : uid(%u) is not found", uid);
302 return STTD_ERROR_INVALID_PARAMETER;
311 int sttd_client_get_current_recording()
314 client_info_s *data = NULL;
316 if (0 < g_slist_length(g_client_list)) {
317 iter = g_slist_nth(g_client_list, 0);
319 while (NULL != iter) {
320 /* Get handle data from list */
323 if (APP_STATE_RECORDING == data->state)
326 iter = g_slist_next(iter);
333 int sttd_client_get_current_thinking()
336 client_info_s *data = NULL;
338 if (0 < g_slist_length(g_client_list)) {
339 iter = g_slist_nth(g_client_list, 0);
341 while (NULL != iter) {
342 /* Get handle data from list */
345 if (APP_STATE_PROCESSING == data->state)
348 iter = g_slist_next(iter);
355 int sttd_cliet_set_timer(unsigned int uid, Ecore_Timer* timer)
358 client_info_s* hnd = NULL;
360 tmp = __client_get_item(uid);
362 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
363 return STTD_ERROR_INVALID_PARAMETER;
372 int sttd_cliet_get_timer(unsigned int uid, Ecore_Timer** timer)
375 client_info_s* hnd = NULL;
377 tmp = __client_get_item(uid);
379 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
380 return STTD_ERROR_INVALID_PARAMETER;
390 int sttd_client_get_list(unsigned int** uids, int* uid_count)
392 if (NULL == uids || NULL == uid_count)
395 int count = g_slist_length(g_client_list);
400 unsigned int* tmp = (unsigned int*)calloc(count, sizeof(unsigned int));
402 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to allocate memory");
403 return STTD_ERROR_OUT_OF_MEMORY;
407 client_info_s *data = NULL;
410 iter = g_slist_nth(g_client_list, 0);
411 while (NULL != iter) {
412 if (NULL == iter->data) {
414 iter = g_slist_next(iter);
420 iter = g_slist_next(iter);
431 int stt_client_set_current_recognition(unsigned int uid)
433 if (STT_INVALID_UID != g_cur_recog_uid) {
437 g_cur_recog_uid = uid;
442 unsigned int stt_client_get_current_recognition()
444 return g_cur_recog_uid;
447 int stt_client_unset_current_recognition()
449 g_cur_recog_uid = STT_INVALID_UID;
453 int stt_client_set_app_agreed(unsigned int uid)
456 client_info_s* hnd = NULL;
458 tmp = __client_get_item(uid);
460 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
461 return STTD_ERROR_INVALID_PARAMETER;
465 hnd->app_agreed = true;
470 bool stt_client_get_app_agreed(unsigned int uid)
473 client_info_s* hnd = NULL;
475 tmp = __client_get_item(uid);
477 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
478 return STTD_ERROR_INVALID_PARAMETER;
482 return hnd->app_agreed;
485 int sttd_client_get_audio_id(unsigned int uid, char** audio_id)
487 if (NULL == audio_id) {
488 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] audio_id is NULL");
489 return STTD_ERROR_INVALID_PARAMETER;
493 client_info_s* hnd = NULL;
495 tmp = __client_get_item(uid);
497 return STTD_ERROR_INVALID_PARAMETER;
501 if (NULL != hnd->audio_id) {
502 *audio_id = strdup(hnd->audio_id);
510 int sttd_client_set_audio_id(unsigned int uid, const char* audio_id)
513 client_info_s* hnd = NULL;
515 tmp = __client_get_item(uid);
517 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
518 return STTD_ERROR_INVALID_PARAMETER;
522 if (NULL != hnd->audio_id) {
526 if (NULL != audio_id) {
527 hnd->audio_id = strdup(audio_id);
528 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Audio ID : %s", hnd->audio_id);
530 hnd->audio_id = NULL;