Update version (1.90.1)
[platform/core/uifw/stt.git] / server / sttd_client_data.c
1 /*
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.
12 */
13
14
15 #include <stdatomic.h>
16
17 #include "sttd_main.h"
18 #include "stt_defs.h"
19 #include "sttd_client_data.h"
20
21 /* Client list */
22 static GSList *g_client_list = NULL;
23
24 static atomic_uint g_cur_recog_uid = 0;
25
26 int client_show_list()
27 {
28         GSList *iter = NULL;
29         client_info_s *data = NULL;
30
31         SLOG(LOG_DEBUG, TAG_STTD, "----- client list");
32
33         if (g_slist_length(g_client_list) > 0) {
34                 /* Get a first item */
35                 iter = g_slist_nth(g_client_list, 0);
36
37                 int i = 1;
38                 while (NULL != iter) {
39                         /*Get handle data from list*/
40                         data = iter->data;
41
42                         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[%dth] uid(%u), state(%d)", i, data->uid, data->state);
43
44                         /*Get next item*/
45                         iter = g_slist_next(iter);
46                         i++;
47                 }
48         } else {
49                 SLOG(LOG_DEBUG, TAG_STTD, "No Client");
50         }
51
52         SLOG(LOG_DEBUG, TAG_STTD, "-----");
53
54         return 0;
55 }
56
57 GSList* __client_get_item(unsigned int uid)
58 {
59         GSList *iter = NULL;
60         client_info_s *data = NULL;
61
62         if (0 < g_slist_length(g_client_list)) {
63                 iter = g_slist_nth(g_client_list, 0);
64
65                 while (NULL != iter) {
66                         /* Get handle data from list */
67                         data = iter->data;
68
69                         if (uid == data->uid)
70                                 return iter;
71
72                         iter = g_slist_next(iter);
73                 }
74         }
75
76         return NULL;
77 }
78
79 int sttd_client_add(int pid, unsigned int uid)
80 {
81         /*Check uid is duplicated*/
82         GSList *tmp = NULL;
83         tmp = __client_get_item(uid);
84
85         if (NULL != tmp) {
86                 SLOG(LOG_WARN, TAG_STTD, "[Client Data] Client uid is already registered");
87                 return STTD_ERROR_INVALID_PARAMETER;
88         }
89
90         client_info_s *info = (client_info_s*)calloc(1, sizeof(client_info_s));
91         if (NULL == info) {
92                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to allocate memory");
93                 return STTD_ERROR_OUT_OF_MEMORY;
94         }
95
96         info->pid = pid;
97         info->uid = uid;
98         info->start_beep = NULL;
99         info->stop_beep = NULL;
100         info->state = APP_STATE_READY;
101
102         info->app_agreed = false;
103         info->is_streaming = false;
104
105         /* Add item to global list */
106         g_client_list = g_slist_append(g_client_list, info);
107
108         if (NULL == g_client_list) {
109                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to add new client");
110
111                 free(info);
112                 info = NULL;
113
114                 return STTD_ERROR_OPERATION_FAILED;
115         }
116
117 #ifdef CLIENT_DATA_DEBUG
118         client_show_list();
119 #endif
120         return 0;
121 }
122
123 int sttd_client_delete(unsigned int uid)
124 {
125         GSList *tmp = NULL;
126         client_info_s* hnd = NULL;
127
128         /*Get handle*/
129         tmp = __client_get_item(uid);
130         if (NULL == tmp) {
131                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
132                 return STTD_ERROR_INVALID_PARAMETER;
133         }
134
135         /*Free client structure*/
136         hnd = tmp->data;
137         if (NULL != hnd) {
138                 if (NULL != hnd->start_beep)    free(hnd->start_beep);
139                 if (NULL != hnd->stop_beep)     free(hnd->stop_beep);
140                 free(hnd);
141         }
142
143         /*Remove handle from list*/
144         g_client_list = g_slist_remove_link(g_client_list, tmp);
145
146 #ifdef CLIENT_DATA_DEBUG
147         client_show_list();
148 #endif
149
150         return 0;
151 }
152
153 int sttd_client_get_start_sound(unsigned int uid, char** filename)
154 {
155         if (NULL == filename) {
156                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Filename is NULL");
157                 return STTD_ERROR_INVALID_PARAMETER;
158         }
159
160         GSList *tmp = NULL;
161         client_info_s* hnd = NULL;
162
163         tmp = __client_get_item(uid);
164         if (NULL == tmp) {
165                 return STTD_ERROR_INVALID_PARAMETER;
166         }
167
168         hnd = tmp->data;
169         if (NULL != hnd->start_beep) {
170                 *filename = strdup(hnd->start_beep);
171         } else {
172                 *filename = NULL;
173         }
174
175         return 0;
176 }
177
178 int sttd_client_set_start_sound(unsigned int uid, const char* filename)
179 {
180         GSList *tmp = NULL;
181         client_info_s* hnd = NULL;
182
183         tmp = __client_get_item(uid);
184         if (NULL == tmp) {
185                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
186                 return STTD_ERROR_INVALID_PARAMETER;
187         }
188
189         hnd = tmp->data;
190         if (NULL != hnd->start_beep) {
191                 free(hnd->start_beep);
192         }
193
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);
197         } else {
198                 hnd->start_beep = NULL;
199         }
200
201         return 0;
202 }
203
204 int sttd_client_get_stop_sound(unsigned int uid, char** filename)
205 {
206         if (NULL == filename) {
207                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Filename is NULL");
208                 return STTD_ERROR_INVALID_PARAMETER;
209         }
210
211         GSList *tmp = NULL;
212         client_info_s* hnd = NULL;
213
214         tmp = __client_get_item(uid);
215         if (NULL == tmp) {
216                 return STTD_ERROR_INVALID_PARAMETER;
217         }
218
219         hnd = tmp->data;
220         if (NULL != hnd->stop_beep) {
221                 *filename = strdup(hnd->stop_beep);
222         } else {
223                 *filename = NULL;
224         }
225
226         return 0;
227 }
228
229 int sttd_client_set_stop_sound(unsigned int uid, const char* filename)
230 {
231         GSList *tmp = NULL;
232         client_info_s* hnd = NULL;
233
234         tmp = __client_get_item(uid);
235         if (NULL == tmp) {
236                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
237                 return STTD_ERROR_INVALID_PARAMETER;
238         }
239
240         hnd = tmp->data;
241         if (NULL != hnd->stop_beep) {
242                 free(hnd->stop_beep);
243         }
244
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);
248         } else {
249                 hnd->stop_beep = NULL;
250         }
251
252         return 0;
253 }
254
255 int sttd_client_get_state(unsigned int uid, app_state_e* state)
256 {
257         GSList *tmp = NULL;
258         client_info_s* hnd = NULL;
259
260         tmp = __client_get_item(uid);
261         if (NULL == tmp) {
262                 return STTD_ERROR_INVALID_PARAMETER;
263         }
264
265         hnd = tmp->data;
266         *state = hnd->state;
267
268         return 0;
269 }
270
271 int sttd_client_set_state(unsigned int uid, app_state_e state)
272 {
273         GSList *tmp = NULL;
274         client_info_s* hnd = NULL;
275
276         tmp = __client_get_item(uid);
277         if (NULL == tmp) {
278                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
279                 return STTD_ERROR_INVALID_PARAMETER;
280         }
281
282         hnd = tmp->data;
283         hnd->state = state ;
284
285         return 0;
286 }
287
288 int sttd_client_get_ref_count()
289 {
290         int count = g_slist_length(g_client_list);
291
292         return count;
293 }
294
295 int sttd_client_get_pid(unsigned int uid)
296 {
297         GSList *tmp = NULL;
298         client_info_s* hnd = NULL;
299
300         tmp = __client_get_item(uid);
301         if (NULL == tmp) {
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;
304         }
305
306         hnd = tmp->data;
307
308         return hnd->pid;
309 }
310
311 #if 0
312 int sttd_client_get_current_recording()
313 {
314         GSList *iter = NULL;
315         client_info_s *data = NULL;
316
317         if (0 < g_slist_length(g_client_list)) {
318                 iter = g_slist_nth(g_client_list, 0);
319
320                 while (NULL != iter) {
321                         /* Get handle data from list */
322                         data = iter->data;
323
324                         if (APP_STATE_RECORDING == data->state)
325                                 return data->uid;
326
327                         iter = g_slist_next(iter);
328                 }
329         }
330
331         return -1;
332 }
333
334 int sttd_client_get_current_thinking()
335 {
336         GSList *iter = NULL;
337         client_info_s *data = NULL;
338
339         if (0 < g_slist_length(g_client_list)) {
340                 iter = g_slist_nth(g_client_list, 0);
341
342                 while (NULL != iter) {
343                         /* Get handle data from list */
344                         data = iter->data;
345
346                         if (APP_STATE_PROCESSING == data->state)
347                                 return data->uid;
348
349                         iter = g_slist_next(iter);
350                 }
351         }
352
353         return -1;
354 }
355
356 int sttd_cliet_set_timer(unsigned int uid, Ecore_Timer* timer)
357 {
358         GSList *tmp = NULL;
359         client_info_s* hnd = NULL;
360
361         tmp = __client_get_item(uid);
362         if (NULL == tmp) {
363                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
364                 return STTD_ERROR_INVALID_PARAMETER;
365         }
366
367         hnd = tmp->data;
368         hnd->timer = timer;
369
370         return 0;
371 }
372
373 int sttd_cliet_get_timer(unsigned int uid, Ecore_Timer** timer)
374 {
375         GSList *tmp = NULL;
376         client_info_s* hnd = NULL;
377
378         tmp = __client_get_item(uid);
379         if (NULL == tmp) {
380                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
381                 return STTD_ERROR_INVALID_PARAMETER;
382         }
383
384         hnd = tmp->data;
385         *timer = hnd->timer;
386
387         return 0;
388 }
389 #endif
390
391 int sttd_client_get_list(unsigned int** uids, int* uid_count)
392 {
393         if (NULL == uids || NULL == uid_count)
394                 return -1;
395
396         int count = g_slist_length(g_client_list);
397
398         if (0 == count)
399                 return -1;
400
401         unsigned int* tmp = (unsigned int*)calloc(count, sizeof(unsigned int));
402         if (NULL == tmp) {
403                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to allocate memory");
404                 return STTD_ERROR_OUT_OF_MEMORY;
405         }
406
407         GSList *iter = NULL;
408         client_info_s *data = NULL;
409         int i = 0;
410
411         iter = g_slist_nth(g_client_list, 0);
412         while (NULL != iter) {
413                 if (NULL == iter->data) {
414                         count--;
415                         iter = g_slist_next(iter);
416                         continue;
417                 }
418
419                 data = iter->data;
420                 tmp[i] = data->uid;
421                 iter = g_slist_next(iter);
422
423                 i++;
424         }
425
426         *uids = tmp;
427         *uid_count = count;
428
429         return 0;
430 }
431
432 int stt_client_set_current_recognition(unsigned int uid)
433 {
434         if (STT_INVALID_UID != g_cur_recog_uid) {
435                 return -1;
436         }
437
438         g_cur_recog_uid = uid;
439
440         return 0;
441 }
442
443 unsigned int stt_client_get_current_recognition()
444 {
445         return g_cur_recog_uid;
446 }
447
448 int stt_client_unset_current_recognition()
449 {
450         g_cur_recog_uid = STT_INVALID_UID;
451         return 0;
452 }
453
454 int stt_client_set_app_agreed(unsigned int uid)
455 {
456         GSList *tmp = NULL;
457         client_info_s* hnd = NULL;
458
459         tmp = __client_get_item(uid);
460         if (NULL == tmp) {
461                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
462                 return STTD_ERROR_INVALID_PARAMETER;
463         }
464
465         hnd = tmp->data;
466         hnd->app_agreed = true;
467
468         return 0;
469 }
470
471 bool stt_client_get_app_agreed(unsigned int uid)
472 {
473         GSList *tmp = NULL;
474         client_info_s* hnd = NULL;
475
476         tmp = __client_get_item(uid);
477         if (NULL == tmp) {
478                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
479                 return STTD_ERROR_INVALID_PARAMETER;
480         }
481
482         hnd = tmp->data;
483         return hnd->app_agreed;
484 }
485
486 int sttd_client_get_audio_id(unsigned int uid, char** audio_id)
487 {
488         if (NULL == audio_id) {
489                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] audio_id is NULL");
490                 return STTD_ERROR_INVALID_PARAMETER;
491         }
492
493         GSList *tmp = NULL;
494         client_info_s* hnd = NULL;
495
496         tmp = __client_get_item(uid);
497         if (NULL == tmp) {
498                 return STTD_ERROR_INVALID_PARAMETER;
499         }
500
501         hnd = tmp->data;
502         if (NULL != hnd->audio_id) {
503                 *audio_id = strdup(hnd->audio_id);
504         } else {
505                 *audio_id = NULL;
506         }
507
508         return 0;
509 }
510
511 int sttd_client_set_audio_id(unsigned int uid, const char* audio_id)
512 {
513         GSList *tmp = NULL;
514         client_info_s* hnd = NULL;
515
516         tmp = __client_get_item(uid);
517         if (NULL == tmp) {
518                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%u) is NOT valid", uid);
519                 return STTD_ERROR_INVALID_PARAMETER;
520         }
521
522         hnd = tmp->data;
523         if (NULL != hnd->audio_id) {
524                 free(hnd->audio_id);
525         }
526
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);
530         } else {
531                 hnd->audio_id = NULL;
532         }
533
534         return 0;
535 }
536
537 bool sttd_client_is_streaming(unsigned int uid)
538 {
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);
542                 return false;
543         }
544
545         client_info_s* hnd = tmp->data;
546         return hnd->is_streaming;
547 }
548
549
550 int sttd_client_set_streaming(unsigned int uid, bool is_streaming)
551 {
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;
556         }
557
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");
561
562         return STTD_ERROR_NONE;
563 }