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