Fix error return value properly
[platform/core/uifw/stt.git] / client / stt_client.c
1 /*
2 *  Copyright (c) 2011-2014 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 "stt_client.h"
16
17 #define MUTEX_TIME 3
18
19 /* Max number of handle */
20 static const int g_max_handle = 999;
21 /* allocated handle */
22 static int g_allocated_handle = 0;
23 /* client list */
24 static GList *g_client_list = NULL;
25
26
27 /* private functions */
28 static int __client_generate_uid(int pid)
29 {
30         g_allocated_handle++;
31
32         if (g_allocated_handle > g_max_handle) {
33                 g_allocated_handle = 1;
34         }
35
36         /* generate uid, handle number should be smaller than 1000 */
37         return pid * 1000 + g_allocated_handle;
38 }
39
40 int stt_client_new(stt_h* stt)
41 {
42         stt_client_s *client = NULL;
43
44         client = (stt_client_s*)calloc(1, sizeof(stt_client_s));
45         if (NULL == client) {
46                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
47                 return STT_ERROR_OUT_OF_MEMORY;
48         }
49
50         stt_h temp = (stt_h)calloc(1, sizeof(struct stt_s));
51         if (NULL == temp) {
52                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to allocate memory");
53                 free(client);
54                 return STT_ERROR_OUT_OF_MEMORY;
55         }
56         temp->handle = __client_generate_uid(getpid());
57
58         /* initialize client data */
59         client->stt = temp;
60         client->pid = getpid();
61         client->uid = temp->handle;
62
63         client->recognition_result_cb = NULL;
64         client->recognition_result_user_data = NULL;
65         client->state_changed_cb = NULL;
66         client->state_changed_user_data = NULL;
67         client->error_cb = NULL;
68         client->error_user_data = NULL;
69         client->default_lang_changed_cb = NULL;
70         client->default_lang_changed_user_data = NULL;
71
72         client->current_engine_id = NULL;
73
74         client->silence_supported = false;
75         client->silence = STT_OPTION_SILENCE_DETECTION_AUTO;
76
77         client->event = 0;
78         client->data_list = NULL;
79         client->data_count = 0;
80         client->msg = NULL;
81
82         client->reason = 0;
83         client->err_msg = NULL;
84
85         client->before_state = STT_STATE_CREATED;
86         client->current_state = STT_STATE_CREATED;
87
88         client->internal_state = STT_INTERNAL_STATE_NONE;
89
90         client->cb_ref_count = 0;
91
92         g_client_list = g_list_append(g_client_list, client);
93
94         *stt = temp;
95
96         return 0;
97 }
98
99 int stt_client_destroy(stt_h stt)
100 {
101         if (stt == NULL) {
102                 SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL");
103                 return 0;
104         }
105
106         GList *iter = NULL;
107         stt_client_s *data = NULL;
108
109         /* if list have item */
110         if (g_list_length(g_client_list) > 0) {
111                 /* Get a first item */
112                 iter = g_list_first(g_client_list);
113
114                 while (NULL != iter) {
115                         data = iter->data;
116                         if (stt->handle == data->stt->handle) {
117                                 g_client_list = g_list_remove_link(g_client_list, iter);
118
119                                 while (0 != data->cb_ref_count) {
120                                         /* wait for release callback function */
121                                 }
122
123                                 if (NULL != data->current_engine_id) {
124                                         free(data->current_engine_id);
125                                 }
126
127                                 if (NULL != data->err_msg) {
128                                         free(data->err_msg);
129                                 }
130
131                                 free(data);
132                                 free(stt);
133
134                                 return 0;
135                         }
136
137                         /* Next item */
138                         iter = g_list_next(iter);
139                 }
140         }
141
142         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] client Not founded");
143
144         return -1;
145 }
146
147
148 stt_client_s* stt_client_get(stt_h stt)
149 {
150         if (NULL == stt) {
151                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
152                 return NULL;
153         }
154
155         GList *iter = NULL;
156         stt_client_s *data = NULL;
157
158         if (g_list_length(g_client_list) > 0) {
159                 /* Get a first item */
160                 iter = g_list_first(g_client_list);
161
162                 while (NULL != iter) {
163                         data = iter->data;
164                         if (NULL != data) {
165                                 if (stt->handle == data->stt->handle)
166                                         return data;
167                         }
168                         /* Next item */
169                         iter = g_list_next(iter);
170                 }
171         }
172
173         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by stt");
174
175         return NULL;
176 }
177
178 stt_client_s* stt_client_get_by_uid(const int uid)
179 {
180         if (uid < 0) {
181                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] out of range : handle");
182                 return NULL;
183         }
184
185         GList *iter = NULL;
186         stt_client_s *data = NULL;
187
188         if (g_list_length(g_client_list) > 0) {
189                 /* Get a first item */
190                 iter = g_list_first(g_client_list);
191
192                 while (NULL != iter) {
193                         data = iter->data;
194                         if (uid == data->uid) {
195                                 return data;
196                         }
197
198                         /* Next item */
199                         iter = g_list_next(iter);
200                 }
201         }
202
203         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by uid");
204
205         return NULL;
206 }
207
208 int stt_client_get_size()
209 {
210         return g_list_length(g_client_list);
211 }
212
213 int stt_client_use_callback(stt_client_s* client)
214 {
215         client->cb_ref_count++;
216         return 0;
217 }
218
219 int stt_client_not_use_callback(stt_client_s* client)
220 {
221         client->cb_ref_count--;
222         return 0;
223 }
224
225 int stt_client_get_use_callback(stt_client_s* client)
226 {
227         return client->cb_ref_count;
228 }
229
230 GList* stt_client_get_client_list()
231 {
232         return g_client_list;
233 }