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;
103 info->is_streaming = false;
105 /* Add item to global list */
106 g_client_list = g_slist_append(g_client_list, info);
108 if (NULL == g_client_list) {
109 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to add new client");
114 return STTD_ERROR_OPERATION_FAILED;
117 #ifdef CLIENT_DATA_DEBUG
123 int sttd_client_delete(unsigned int uid)
126 client_info_s* hnd = NULL;
129 tmp = __client_get_item(uid);
131 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
132 return STTD_ERROR_INVALID_PARAMETER;
135 /*Free client structure*/
138 if (NULL != hnd->start_beep) free(hnd->start_beep);
139 if (NULL != hnd->stop_beep) free(hnd->stop_beep);
143 /*Remove handle from list*/
144 g_client_list = g_slist_remove_link(g_client_list, tmp);
146 #ifdef CLIENT_DATA_DEBUG
153 int sttd_client_get_start_sound(unsigned int uid, char** filename)
155 if (NULL == filename) {
156 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Filename is NULL");
157 return STTD_ERROR_INVALID_PARAMETER;
161 client_info_s* hnd = NULL;
163 tmp = __client_get_item(uid);
165 return STTD_ERROR_INVALID_PARAMETER;
169 if (NULL != hnd->start_beep) {
170 *filename = strdup(hnd->start_beep);
178 int sttd_client_set_start_sound(unsigned int uid, const char* filename)
181 client_info_s* hnd = NULL;
183 tmp = __client_get_item(uid);
185 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
186 return STTD_ERROR_INVALID_PARAMETER;
190 if (NULL != hnd->start_beep) {
191 free(hnd->start_beep);
194 if (NULL != filename) {
195 hnd->start_beep = strdup(filename);
196 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Start sound file : %s", hnd->start_beep);
198 hnd->start_beep = NULL;
204 int sttd_client_get_stop_sound(unsigned int uid, char** filename)
206 if (NULL == filename) {
207 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Filename is NULL");
208 return STTD_ERROR_INVALID_PARAMETER;
212 client_info_s* hnd = NULL;
214 tmp = __client_get_item(uid);
216 return STTD_ERROR_INVALID_PARAMETER;
220 if (NULL != hnd->stop_beep) {
221 *filename = strdup(hnd->stop_beep);
229 int sttd_client_set_stop_sound(unsigned int uid, const char* filename)
232 client_info_s* hnd = NULL;
234 tmp = __client_get_item(uid);
236 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
237 return STTD_ERROR_INVALID_PARAMETER;
241 if (NULL != hnd->stop_beep) {
242 free(hnd->stop_beep);
245 if (NULL != filename) {
246 hnd->stop_beep = strdup(filename);
247 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Stop sound file : %s", hnd->stop_beep);
249 hnd->stop_beep = NULL;
255 int sttd_client_get_state(unsigned int uid, app_state_e* state)
258 client_info_s* hnd = NULL;
260 tmp = __client_get_item(uid);
262 return STTD_ERROR_INVALID_PARAMETER;
271 int sttd_client_set_state(unsigned int uid, app_state_e state)
274 client_info_s* hnd = NULL;
276 tmp = __client_get_item(uid);
278 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
279 return STTD_ERROR_INVALID_PARAMETER;
288 int sttd_client_get_ref_count()
290 int count = g_slist_length(g_client_list);
295 int sttd_client_get_pid(unsigned int uid)
298 client_info_s* hnd = NULL;
300 tmp = __client_get_item(uid);
302 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] sttd_client_get_pid : uid(%u) is not found", uid);
303 return STTD_ERROR_INVALID_PARAMETER;
312 int sttd_client_get_current_recording()
315 client_info_s *data = NULL;
317 if (0 < g_slist_length(g_client_list)) {
318 iter = g_slist_nth(g_client_list, 0);
320 while (NULL != iter) {
321 /* Get handle data from list */
324 if (APP_STATE_RECORDING == data->state)
327 iter = g_slist_next(iter);
334 int sttd_client_get_current_thinking()
337 client_info_s *data = NULL;
339 if (0 < g_slist_length(g_client_list)) {
340 iter = g_slist_nth(g_client_list, 0);
342 while (NULL != iter) {
343 /* Get handle data from list */
346 if (APP_STATE_PROCESSING == data->state)
349 iter = g_slist_next(iter);
356 int sttd_cliet_set_timer(unsigned int uid, Ecore_Timer* timer)
359 client_info_s* hnd = NULL;
361 tmp = __client_get_item(uid);
363 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
364 return STTD_ERROR_INVALID_PARAMETER;
373 int sttd_cliet_get_timer(unsigned int uid, Ecore_Timer** timer)
376 client_info_s* hnd = NULL;
378 tmp = __client_get_item(uid);
380 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
381 return STTD_ERROR_INVALID_PARAMETER;
391 int sttd_client_get_list(unsigned int** uids, int* uid_count)
393 if (NULL == uids || NULL == uid_count)
396 int count = g_slist_length(g_client_list);
401 unsigned int* tmp = (unsigned int*)calloc(count, sizeof(unsigned int));
403 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to allocate memory");
404 return STTD_ERROR_OUT_OF_MEMORY;
408 client_info_s *data = NULL;
411 iter = g_slist_nth(g_client_list, 0);
412 while (NULL != iter) {
413 if (NULL == iter->data) {
415 iter = g_slist_next(iter);
421 iter = g_slist_next(iter);
432 int stt_client_set_current_recognition(unsigned int uid)
434 if (STT_INVALID_UID != g_cur_recog_uid) {
438 g_cur_recog_uid = uid;
443 unsigned int stt_client_get_current_recognition()
445 return g_cur_recog_uid;
448 int stt_client_unset_current_recognition()
450 g_cur_recog_uid = STT_INVALID_UID;
454 int stt_client_set_app_agreed(unsigned int uid)
457 client_info_s* hnd = NULL;
459 tmp = __client_get_item(uid);
461 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
462 return STTD_ERROR_INVALID_PARAMETER;
466 hnd->app_agreed = true;
471 bool stt_client_get_app_agreed(unsigned int uid)
474 client_info_s* hnd = NULL;
476 tmp = __client_get_item(uid);
478 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
479 return STTD_ERROR_INVALID_PARAMETER;
483 return hnd->app_agreed;
486 int sttd_client_get_audio_id(unsigned int uid, char** audio_id)
488 if (NULL == audio_id) {
489 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] audio_id is NULL");
490 return STTD_ERROR_INVALID_PARAMETER;
494 client_info_s* hnd = NULL;
496 tmp = __client_get_item(uid);
498 return STTD_ERROR_INVALID_PARAMETER;
502 if (NULL != hnd->audio_id) {
503 *audio_id = strdup(hnd->audio_id);
511 int sttd_client_set_audio_id(unsigned int uid, const char* audio_id)
514 client_info_s* hnd = NULL;
516 tmp = __client_get_item(uid);
518 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
519 return STTD_ERROR_INVALID_PARAMETER;
523 if (NULL != hnd->audio_id) {
527 if (NULL != audio_id) {
528 hnd->audio_id = strdup(audio_id);
529 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Audio ID : %s", hnd->audio_id);
531 hnd->audio_id = NULL;
537 bool sttd_client_is_streaming(unsigned int uid)
539 GSList *tmp = __client_get_item(uid);
540 if (NULL == tmp || NULL == tmp->data) {
541 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
545 client_info_s* hnd = tmp->data;
546 return hnd->is_streaming;
550 int sttd_client_set_streaming(unsigned int uid, bool is_streaming)
552 GSList *tmp = __client_get_item(uid);
553 if (NULL == tmp || NULL == tmp->data) {
554 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
555 return STTD_ERROR_INVALID_PARAMETER;
558 client_info_s* hnd = tmp->data;
559 hnd->is_streaming = is_streaming;
560 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Is streaming : %s", hnd->is_streaming ? "True": "False");
562 return STTD_ERROR_NONE;