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