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