tizen beta release
[platform/core/uifw/stt.git] / client / stt_client.c
1 /*
2 * Copyright (c) 2011 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*)g_malloc0 (sizeof(stt_client_s));
45
46         stt_h temp = (stt_h)g_malloc0(sizeof(stt_h));
47         temp->handle = __client_generate_uid(getpid()); 
48
49         /* initialize client data */
50         client->stt = temp;
51         client->pid = getpid(); 
52         client->uid = temp->handle;
53         
54         client->result_cb = NULL;
55         client->result_user_data = NULL;
56         client->partial_result_cb = NULL;
57         client->partial_result_user_data = NULL;
58         client->state_changed_cb = NULL;
59         client->state_changed_user_data = NULL;
60         client->error_cb = NULL;
61         client->error_user_data = NULL;
62
63         client->profanity = STT_OPTION_PROFANITY_AUTO;  
64         client->punctuation = STT_OPTION_PUNCTUATION_AUTO;
65         client->silence = STT_OPTION_SILENCE_DETECTION_AUTO;
66
67         client->current_state = STT_STATE_READY; 
68
69         client->cb_ref_count = 0;
70
71         g_client_list = g_list_append(g_client_list, client);
72
73         *stt = temp;
74
75         return 0;       
76 }
77
78 int stt_client_destroy(stt_h stt)
79 {
80         if (stt == NULL) {
81                 SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL");
82                 return 0;
83         }       
84
85         GList *iter = NULL;
86         stt_client_s *data = NULL;
87
88         /* if list have item */
89         if (g_list_length(g_client_list) > 0) {
90                 /* Get a first item */
91                 iter = g_list_first(g_client_list);
92
93                 while (NULL != iter) {
94                         data = iter->data;
95                         if (stt->handle == data->stt->handle) {
96                                 g_client_list = g_list_remove_link(g_client_list, iter);
97
98                                 while (0 != data->cb_ref_count)
99                                 {
100                                         /* wait for release callback function */
101                                 }
102                                 free(data);
103                                 free(stt);
104
105                                 return 0;
106                         }
107
108                         /* Next item */
109                         iter = g_list_next(iter);
110                 }
111         }
112
113         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] client Not founded");
114
115         return -1;
116 }
117
118
119 stt_client_s* stt_client_get(stt_h stt)
120 {
121         if (stt == NULL) {
122                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Input parameter is NULL");
123                 return NULL;
124         }
125
126         GList *iter = NULL;
127         stt_client_s *data = NULL;
128
129         if (g_list_length(g_client_list) > 0) {
130                 /* Get a first item */
131                 iter = g_list_first(g_client_list);
132
133                 while (NULL != iter) {
134                         data = iter->data;
135
136                         if (stt->handle == data->stt->handle) 
137                                 return data;
138
139                         /* Next item */
140                         iter = g_list_next(iter);
141                 }
142         }
143
144         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by stt");
145
146         return NULL;
147 }
148
149 stt_client_s* stt_client_get_by_uid(const int uid)
150 {
151         if (uid < 0) {
152                 SLOG(LOG_ERROR, TAG_STTC, "[ERROR] out of range : handle");
153                 return NULL;
154         }
155
156         GList *iter = NULL;
157         stt_client_s *data = NULL;
158
159         if (g_list_length(g_client_list) > 0) {
160                 /* Get a first item */
161                 iter = g_list_first(g_client_list);
162
163                 while (NULL != iter) {
164                         data = iter->data;
165                         if (uid == data->uid) {
166                                 return data;
167                         }
168
169                         /* Next item */
170                         iter = g_list_next(iter);
171                 }
172         }
173
174         SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get client by uid");
175
176         return NULL;
177 }
178
179 int stt_client_get_size()
180 {
181         return g_list_length(g_client_list);
182 }
183
184 int stt_client_use_callback(stt_client_s* client)
185 {
186         client->cb_ref_count++;
187         return 0;
188 }
189
190 int stt_client_not_use_callback(stt_client_s* client)
191 {
192         client->cb_ref_count--;
193         return 0;
194 }
195
196
197