check null before use when dbus close connection
[platform/core/uifw/voice-control.git] / server / vce.c
1 /*
2 * Copyright (c) 2011-2017 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #include "vcd_dbus.h"
19 #include "vcd_main.h"
20 #include "vcd_server.h"
21
22 #include "vce.h"
23
24
25 int vce_main(int argc, char** argv, vce_request_callback_s *callback)
26 {
27         SLOG(LOG_DEBUG, TAG_VCD, "  ");
28         SLOG(LOG_DEBUG, TAG_VCD, "  ");
29         SLOG(LOG_DEBUG, TAG_VCD, "===== VC Engine Service Initialize");
30
31         int ret = VCE_ERROR_NONE;
32
33         if (!ecore_init()) {
34                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail ecore_init()");
35                 return VCE_ERROR_OPERATION_FAILED;
36         }
37
38         if (0 != vcd_dbus_open_connection()) {
39                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open connection");
40                 ecore_shutdown();
41                 return VCE_ERROR_OPERATION_FAILED;
42         }
43
44         ret = vcd_initialize(callback);
45         if (0 != ret) {
46                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to initialize");
47                 vcd_dbus_close_connection();
48                 ecore_shutdown();
49                 return ret;
50         }
51
52         SLOG(LOG_DEBUG, TAG_VCD, "[Main] VC Engine Service start...");
53
54         SLOG(LOG_DEBUG, TAG_VCD, "=====");
55         SLOG(LOG_DEBUG, TAG_VCD, "  ");
56         SLOG(LOG_DEBUG, TAG_VCD, "  ");
57
58         return 0;
59 }
60
61 int vce_send_result(vce_result_event_e event, int* result_id, int count, const char* all_result, const char* non_fixed_result, const char* nlu_result, const char* msg, void *user_data)
62 {
63         int ret = VCE_ERROR_NONE;
64
65         if (NULL == all_result || NULL == non_fixed_result || NULL == nlu_result) {
66                 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Input parameter is NULL. (no result)");
67         }
68
69         ret = vcd_send_result(event, result_id, count, all_result, non_fixed_result, nlu_result, msg, user_data);
70         if (0 != ret) {
71                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send result");
72         } else {
73                 SLOG(LOG_INFO, TAG_VCD, "[INFO] Success to send result");
74         }
75
76         return ret;
77 }
78
79
80 int vce_send_asr_result(vce_asr_result_event_e event, const char* asr_result, void *user_data)
81 {
82         int ret = VCE_ERROR_NONE;
83
84         if (NULL == asr_result) {
85                 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Input parameter is NULL. (no result)");
86         }
87
88         ret = vcd_send_asr_result(event, asr_result, user_data);
89         if (0 != ret) {
90                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send ASR result");
91         } else {
92                 SLOG(LOG_INFO, TAG_VCD, "[INFO] Success to send ASR result");
93         }
94
95         return ret;
96 }
97
98 int vce_send_nlg_result(const char* nlg_result, void *user_data)
99 {
100         int ret = VCE_ERROR_NONE;
101
102         if (NULL == nlg_result) {
103                 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Input parameter is NULL. (no result)");
104         }
105
106         ret = vcd_send_nlg_result(nlg_result, user_data);
107         if (0 != ret) {
108                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send NLG result");
109         } else {
110                 SLOG(LOG_INFO, TAG_VCD, "[INFO] Success to send NLG result");
111         }
112
113         return ret;
114 }
115
116 int vce_send_error(vce_error_e error, const char* msg, void *user_data)
117 {
118         int ret = VCE_ERROR_NONE;
119
120         if (NULL == msg) {
121                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Input parameter is NULL. (no error message)");
122         }
123
124         ret = vcd_send_error(error, msg, user_data);
125
126         if (0 != ret) {
127                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send error");
128         }
129
130         return ret;
131 }
132
133
134 /* Daemon API */
135 int vce_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
136 {
137         int ret = VCE_ERROR_NONE;
138
139         ret = vcd_get_foreach_command(vce_command, callback, user_data);
140         if (0 != ret) {
141                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get foreach command");
142         }
143
144         return ret;
145 }
146
147 int vce_get_command_count(vce_cmd_h vce_command)
148 {
149         int ret = VCE_ERROR_NONE;
150
151         ret = vcd_get_command_count(vce_command);
152         if (0 != ret) {
153                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get command count");
154         }
155
156         return ret;
157 }
158
159 int vce_get_audio_type(char** audio_type)
160 {
161         int ret = VCE_ERROR_NONE;
162
163         ret = vcd_get_audio_type(audio_type);
164         if (0 != ret) {
165                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get audio type");
166         }
167
168         return ret;
169 }
170
171 int vce_set_private_data(const char* key, const char* data)
172 {
173         int ret = VCE_ERROR_NONE;
174
175         ret = vcd_set_private_data(key, data);
176         if (0 != ret) {
177                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set private data to vc manager");
178         }
179
180         return ret;
181 }
182
183 int vce_get_private_data(const char* key, char** data)
184 {
185         int ret = VCE_ERROR_NONE;
186
187         ret = vcd_get_private_data(key, data);
188         if (0 != ret) {
189                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get private data from vc manager");
190         }
191
192         return ret;
193 }
194
195 int vce_start_recording()
196 {
197         int ret = VCE_ERROR_NONE;
198
199         ret = vcd_start_recording();
200         if (0 != ret) {
201                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to start recording");
202         }
203
204         return ret;
205 }
206
207 int vce_stop_recording()
208 {
209         int ret = VCE_ERROR_NONE;
210
211         ret = vcd_stop_recording();
212         if (0 != ret) {
213                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to stop recording");
214         }
215
216         return ret;
217 }
218
219 int vce_set_private_data_set_cb(vce_private_data_set_cb callback_func)
220 {
221         if (NULL == callback_func) {
222                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
223                 return VCE_ERROR_INVALID_PARAMETER;
224         }
225
226         int ret = vcd_set_private_data_set_cb(callback_func);
227         if (0 != ret) {
228                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set private data set cb");
229         }
230
231         return ret;
232 }
233
234 int vce_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
235 {
236         if (NULL == callback_func) {
237                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
238                 return VCE_ERROR_INVALID_PARAMETER;
239         }
240
241         int ret = vcd_set_private_data_requested_cb(callback_func);
242         if (0 != ret) {
243                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set private data requested cb");
244         }
245
246         return ret;
247 }
248
249 int vce_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
250 {
251         if (NULL == callback_func) {
252                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
253                 return VCE_ERROR_INVALID_PARAMETER;
254         }
255
256         int ret = vcd_set_nlu_base_info_requested_cb(callback_func);
257         if (0 != ret) {
258                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set nlu-base info requested cb");
259         }
260
261         return ret;
262 }