Fix dbus delay when requesting hello
[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
107                 free(info);
108                 info = NULL;
109
110                 return STTD_ERROR_OPERATION_FAILED;
111         }
112
113 #ifdef CLIENT_DATA_DEBUG
114         client_show_list();
115 #endif
116         return 0;
117 }
118
119 int sttd_client_delete(int uid)
120 {
121         GSList *tmp = NULL;
122         client_info_s* hnd = NULL;
123
124         /*Get handle*/
125         tmp = __client_get_item(uid);
126         if (NULL == tmp) {
127                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid);
128                 return STTD_ERROR_INVALID_PARAMETER;
129         }
130
131         /*Free client structure*/
132         hnd = tmp->data;
133         if (NULL != hnd) {
134                 if (NULL != hnd->start_beep)    free(hnd->start_beep);
135                 if (NULL != hnd->stop_beep)     free(hnd->stop_beep);
136                 free(hnd);
137         }
138
139         /*Remove handle from list*/
140         g_client_list = g_slist_remove_link(g_client_list, tmp);
141
142 #ifdef CLIENT_DATA_DEBUG
143         client_show_list();
144 #endif
145
146         return 0;
147 }
148
149 int sttd_client_get_start_sound(int uid, char** filename)
150 {
151         if (NULL == filename) {
152                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Filename is NULL");
153                 return STTD_ERROR_INVALID_PARAMETER;
154         }
155
156         GSList *tmp = NULL;
157         client_info_s* hnd = NULL;
158
159         tmp = __client_get_item(uid);
160         if (NULL == tmp) {
161                 return STTD_ERROR_INVALID_PARAMETER;
162         }
163
164         hnd = tmp->data;
165         if (NULL != hnd->start_beep) {
166                 *filename = strdup(hnd->start_beep);
167         } else {
168                 *filename = NULL;
169         }
170
171         return 0;
172 }
173
174 int sttd_client_set_start_sound(int uid, const char* filename)
175 {
176         GSList *tmp = NULL;
177         client_info_s* hnd = NULL;
178
179         tmp = __client_get_item(uid);
180         if (NULL == tmp) {
181                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid);
182                 return STTD_ERROR_INVALID_PARAMETER;
183         }
184
185         hnd = tmp->data;
186         if (NULL != hnd->start_beep) {
187                 free(hnd->start_beep);
188         }
189
190         if (NULL != filename) {
191                 hnd->start_beep = strdup(filename);
192                 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Start sound file : %s", hnd->start_beep);
193         } else {
194                 hnd->start_beep = NULL;
195         }
196
197         return 0;
198 }
199
200 int sttd_client_get_stop_sound(int uid, char** filename)
201 {
202         if (NULL == filename) {
203                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Filename is NULL");
204                 return STTD_ERROR_INVALID_PARAMETER;
205         }
206
207         GSList *tmp = NULL;
208         client_info_s* hnd = NULL;
209
210         tmp = __client_get_item(uid);
211         if (NULL == tmp) {
212                 return STTD_ERROR_INVALID_PARAMETER;
213         }
214
215         hnd = tmp->data;
216         if (NULL != hnd->stop_beep) {
217                 *filename = strdup(hnd->stop_beep);
218         } else {
219                 *filename = NULL;
220         }
221
222         return 0;
223 }
224
225 int sttd_client_set_stop_sound(int uid, const char* filename)
226 {
227         GSList *tmp = NULL;
228         client_info_s* hnd = NULL;
229
230         tmp = __client_get_item(uid);
231         if (NULL == tmp) {
232                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid);
233                 return STTD_ERROR_INVALID_PARAMETER;
234         }
235
236         hnd = tmp->data;
237         if (NULL != hnd->stop_beep) {
238                 free(hnd->stop_beep);
239         }
240
241         if (NULL != filename) {
242                 hnd->stop_beep = strdup(filename);
243                 SLOG(LOG_DEBUG, TAG_STTD, "[Client Data] Stop sound file : %s", hnd->stop_beep);
244         } else {
245                 hnd->stop_beep = NULL;
246         }
247
248         return 0;
249 }
250
251 int sttd_client_get_state(int uid, app_state_e* state)
252 {
253         GSList *tmp = NULL;
254         client_info_s* hnd = NULL;
255
256         tmp = __client_get_item(uid);
257         if (NULL == tmp) {
258                 return STTD_ERROR_INVALID_PARAMETER;
259         }
260
261         hnd = tmp->data;
262         *state = hnd->state;
263
264         return 0;
265 }
266
267 int sttd_client_set_state(int uid, app_state_e state)
268 {
269         GSList *tmp = NULL;
270         client_info_s* hnd = NULL;
271
272         tmp = __client_get_item(uid);
273         if (NULL == tmp) {
274                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid);
275                 return STTD_ERROR_INVALID_PARAMETER;
276         }
277
278         hnd = tmp->data;
279         hnd->state = state ;
280
281         return 0;
282 }
283
284 int sttd_client_get_ref_count()
285 {
286         int count = g_slist_length(g_client_list);
287
288         return count;
289 }
290
291 int sttd_client_get_pid(int uid)
292 {
293         GSList *tmp = NULL;
294         client_info_s* hnd = NULL;
295
296         tmp = __client_get_item(uid);
297         if (NULL == tmp) {
298                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] sttd_client_get_pid : uid(%d) is not found", uid);
299                 return STTD_ERROR_INVALID_PARAMETER;
300         }
301
302         hnd = tmp->data;
303
304         return hnd->pid;
305 }
306
307 #if 0
308 int sttd_client_get_current_recording()
309 {
310         GSList *iter = NULL;
311         client_info_s *data = NULL;
312
313         if (0 < g_slist_length(g_client_list)) {
314                 iter = g_slist_nth(g_client_list, 0);
315
316                 while (NULL != iter) {
317                         /* Get handle data from list */
318                         data = iter->data;
319
320                         if (APP_STATE_RECORDING == data->state)
321                                 return data->uid;
322
323                         iter = g_slist_next(iter);
324                 }
325         }
326
327         return -1;
328 }
329
330 int sttd_client_get_current_thinking()
331 {
332         GSList *iter = NULL;
333         client_info_s *data = NULL;
334
335         if (0 < g_slist_length(g_client_list)) {
336                 iter = g_slist_nth(g_client_list, 0);
337
338                 while (NULL != iter) {
339                         /* Get handle data from list */
340                         data = iter->data;
341
342                         if (APP_STATE_PROCESSING == data->state)
343                                 return data->uid;
344
345                         iter = g_slist_next(iter);
346                 }
347         }
348
349         return -1;
350 }
351
352 int sttd_cliet_set_timer(int uid, Ecore_Timer* timer)
353 {
354         GSList *tmp = NULL;
355         client_info_s* hnd = NULL;
356
357         tmp = __client_get_item(uid);
358         if (NULL == tmp) {
359                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid);
360                 return STTD_ERROR_INVALID_PARAMETER;
361         }
362
363         hnd = tmp->data;
364         hnd->timer = timer;
365
366         return 0;
367 }
368
369 int sttd_cliet_get_timer(int uid, Ecore_Timer** timer)
370 {
371         GSList *tmp = NULL;
372         client_info_s* hnd = NULL;
373
374         tmp = __client_get_item(uid);
375         if (NULL == tmp) {
376                 SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid);
377                 return STTD_ERROR_INVALID_PARAMETER;
378         }
379
380         hnd = tmp->data;
381         *timer = hnd->timer;
382
383         return 0;
384 }
385 #endif
386
387 int sttd_client_get_list(int** uids, int* uid_count)
388 {
389         if (NULL == uids || NULL == uid_count)
390                 return -1;
391
392         int count = g_slist_length(g_client_list);
393
394         if (0 == count)
395                 return -1;
396
397         int *tmp;
398         tmp = (int*)calloc(count, sizeof(int));
399         if (NULL == tmp) {
400                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] Fail to allocate memory");
401                 return STTD_ERROR_OUT_OF_MEMORY;
402         }
403
404         GSList *iter = NULL;
405         client_info_s *data = NULL;
406         int i = 0;
407
408         iter = g_slist_nth(g_client_list, 0);
409         while (NULL != iter) {
410                 if (NULL == iter->data) {
411                         count--;
412                         iter = g_slist_next(iter);
413                         continue;
414                 }
415
416                 data = iter->data;
417                 tmp[i] = data->uid;
418                 iter = g_slist_next(iter);
419
420                 i++;
421         }
422
423         *uids = tmp;
424         *uid_count = count;
425
426         return 0;
427 }
428
429 int stt_client_set_current_recognition(int uid)
430 {
431         if (0 != g_cur_recog_uid) {
432                 return -1;
433         }
434
435         g_cur_recog_uid = uid;
436
437         return 0;
438 }
439
440 int stt_client_get_current_recognition()
441 {
442         return g_cur_recog_uid;
443 }
444
445 int stt_client_unset_current_recognition()
446 {
447         g_cur_recog_uid = 0;
448         return 0;
449 }
450
451 int stt_client_set_app_agreed(int uid)
452 {
453         GSList *tmp = NULL;
454         client_info_s* hnd = NULL;
455
456         tmp = __client_get_item(uid);
457         if (NULL == tmp) {
458                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid);
459                 return STTD_ERROR_INVALID_PARAMETER;
460         }
461
462         hnd = tmp->data;
463         hnd->app_agreed = true;
464
465         return 0;
466 }
467
468 bool stt_client_get_app_agreed(int uid)
469 {
470         GSList *tmp = NULL;
471         client_info_s* hnd = NULL;
472
473         tmp = __client_get_item(uid);
474         if (NULL == tmp) {
475                 SLOG(LOG_ERROR, TAG_STTD, "[Client Data ERROR] uid(%d) is NOT valid", uid);
476                 return STTD_ERROR_INVALID_PARAMETER;
477         }
478
479         hnd = tmp->data;
480         return hnd->app_agreed;
481 }