2 * Copyright (c) 2011-2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <cynara-client.h>
19 #include <cynara-error.h>
20 #include <cynara-session.h>
21 #include <system_info.h>
24 #include "vcd_server.h"
28 static int g_feature_enabled = -1;
29 static int g_privilege_allowed = -1;
30 static cynara *p_cynara = NULL;
32 static int __vce_get_feature_enabled()
34 if (0 == g_feature_enabled) {
35 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Voice control feature NOT supported");
36 return VCE_ERROR_NOT_SUPPORTED;
37 } else if (-1 == g_feature_enabled) {
38 bool vc_supported = false;
39 bool mic_supported = false;
40 if (0 == system_info_get_platform_bool(VC_FEATURE_PATH, &vc_supported)) {
41 if (0 == system_info_get_platform_bool(VC_MIC_FEATURE_PATH, &mic_supported)) {
42 if (false == vc_supported || false == mic_supported) {
43 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Voice control feature NOT supported");
44 g_feature_enabled = 0;
45 return VCE_ERROR_NOT_SUPPORTED;
48 g_feature_enabled = 1;
50 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get feature value");
51 return VCE_ERROR_NOT_SUPPORTED;
54 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get feature value");
55 return VCE_ERROR_NOT_SUPPORTED;
62 static int __check_privilege_initialize()
64 int ret = cynara_initialize(&p_cynara, NULL);
65 if (CYNARA_API_SUCCESS != ret)
66 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to initialize");
68 return ret == CYNARA_API_SUCCESS;
71 static int __check_privilege(const char* uid, const char * privilege)
74 char label_path[1024] = "/proc/self/attr/current";
75 char smack_label[1024] = {'\0',};
81 fp = fopen(label_path, "r");
83 if (0 >= fread(smack_label, 1, sizeof(smack_label), fp))
84 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] fail to fread");
90 char *session = cynara_session_from_pid(pid);
91 int ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
92 SLOG(LOG_DEBUG, TAG_VCD, "[Client]cynara_check returned %d(%s)", ret, (CYNARA_API_ACCESS_ALLOWED == ret) ? "Allowed" : "Denied");
96 if (ret != CYNARA_API_ACCESS_ALLOWED)
101 static void __check_privilege_deinitialize()
104 cynara_finish(p_cynara);
108 static int __vce_check_privilege()
112 if (0 == g_privilege_allowed) {
113 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Permission is denied");
114 return VCE_ERROR_PERMISSION_DENIED;
115 } else if (-1 == g_privilege_allowed) {
116 if (false == __check_privilege_initialize()) {
117 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] privilege initialize is failed");
118 return VCE_ERROR_PERMISSION_DENIED;
120 snprintf(uid, 16, "%d", getuid());
121 if (false == __check_privilege(uid, VC_PRIVILEGE)) {
122 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Permission is denied");
123 g_privilege_allowed = 0;
124 __check_privilege_deinitialize();
125 return VCE_ERROR_PERMISSION_DENIED;
127 __check_privilege_deinitialize();
130 g_privilege_allowed = 1;
131 return VCE_ERROR_NONE;
134 int vce_main(int argc, char** argv, vce_request_callback_s *callback)
136 if (0 != __vce_get_feature_enabled()) {
137 return VCE_ERROR_NOT_SUPPORTED;
139 if (0 != __vce_check_privilege()) {
140 return VCE_ERROR_PERMISSION_DENIED;
143 SLOG(LOG_DEBUG, TAG_VCD, " ");
144 SLOG(LOG_DEBUG, TAG_VCD, " ");
145 SLOG(LOG_DEBUG, TAG_VCD, "===== VC Engine Service Initialize");
147 int ret = VCE_ERROR_NONE;
150 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail ecore_init()");
151 return VCE_ERROR_OPERATION_FAILED;
154 if (0 != vcd_dbus_open_connection()) {
155 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to open connection");
157 return VCE_ERROR_OPERATION_FAILED;
160 ret = vcd_initialize(callback);
162 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to initialize");
163 vcd_dbus_close_connection();
168 SLOG(LOG_DEBUG, TAG_VCD, "[Main] VC Engine Service start...");
170 SLOG(LOG_DEBUG, TAG_VCD, "=====");
171 SLOG(LOG_DEBUG, TAG_VCD, " ");
172 SLOG(LOG_DEBUG, TAG_VCD, " ");
177 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, int* user_info, void *user_data)
179 if (0 != __vce_get_feature_enabled()) {
180 return VCE_ERROR_NOT_SUPPORTED;
183 int ret = VCE_ERROR_NONE;
185 if (event < VCE_RESULT_EVENT_SUCCESS || event > VCE_RESULT_EVENT_ERROR) {
186 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
187 return VCE_ERROR_INVALID_PARAMETER;
190 if (NULL == all_result || NULL == non_fixed_result || NULL == nlu_result) {
191 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Input parameter is NULL. (no result)");
194 ret = vcd_send_result(event, result_id, count, all_result, non_fixed_result, nlu_result, msg, user_info, user_data);
196 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send result");
198 SLOG(LOG_INFO, TAG_VCD, "[INFO] Success to send result");
205 int vce_send_asr_result(vce_asr_result_event_e event, const char* asr_result, void *user_data)
207 if (0 != __vce_get_feature_enabled()) {
208 return VCE_ERROR_NOT_SUPPORTED;
211 int ret = VCE_ERROR_NONE;
213 if (event < VCE_ASR_RESULT_EVENT_FINAL_RESULT || event > VCE_ASR_RESULT_EVENT_ERROR) {
214 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
215 return VCE_ERROR_INVALID_PARAMETER;
218 if (NULL == asr_result) {
219 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Input parameter is NULL. (no result)");
222 ret = vcd_send_asr_result(event, asr_result, user_data);
224 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send ASR result");
226 SLOG(LOG_INFO, TAG_VCD, "[INFO] Success to send ASR result");
232 int vce_send_specific_engine_result(const char* engine_app_id, const char* event, const char* result, void *user_info)
234 if (0 != __vce_get_feature_enabled()) {
235 return VCE_ERROR_NOT_SUPPORTED;
238 int ret = VCE_ERROR_NONE;
240 if (NULL == engine_app_id || NULL == event) {
241 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
242 return VCE_ERROR_INVALID_PARAMETER;
245 if (NULL == result) {
246 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Input parameter is NULL. (no result)");
249 ret = vcd_send_specific_engine_result(engine_app_id, event, result, user_info);
251 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send specific engine result, ret(%d)", ret);
253 SLOG(LOG_INFO, TAG_VCD, "[INFO] Success to send specific engine result, event(%s), result(%s)", event, result);
259 int vce_send_nlg_result(const char* nlg_result, void *user_data)
261 if (0 != __vce_get_feature_enabled()) {
262 return VCE_ERROR_NOT_SUPPORTED;
265 int ret = VCE_ERROR_NONE;
267 if (NULL == nlg_result) {
268 SLOG(LOG_ERROR, TAG_VCD, "[INFO] Input parameter is NULL. (no result)");
271 ret = vcd_send_nlg_result(nlg_result, user_data);
273 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send NLG result");
275 SLOG(LOG_INFO, TAG_VCD, "[INFO] Success to send NLG result");
281 int vce_send_error(vce_error_e error, const char* msg, void *user_data)
283 if (0 != __vce_get_feature_enabled()) {
284 return VCE_ERROR_NOT_SUPPORTED;
287 int ret = VCE_ERROR_NONE;
290 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Input parameter is NULL. (no error message)");
293 ret = vcd_send_error(error, msg, user_data);
296 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send error");
304 int vce_get_foreach_command(vce_cmd_h vce_command, vce_command_cb callback, void* user_data)
306 if (0 != __vce_get_feature_enabled()) {
307 return VCE_ERROR_NOT_SUPPORTED;
310 int ret = VCE_ERROR_NONE;
312 ret = vcd_get_foreach_command(vce_command, callback, user_data);
314 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get foreach command");
320 int vce_get_command_count(vce_cmd_h vce_command, int* count)
322 if (0 != __vce_get_feature_enabled()) {
323 return VCE_ERROR_NOT_SUPPORTED;
326 int ret = VCE_ERROR_NONE;
329 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
330 return VCE_ERROR_INVALID_PARAMETER;
332 ret = vcd_get_command_count(vce_command, count);
334 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get command count");
340 int vce_get_audio_type(char** audio_type)
342 if (0 != __vce_get_feature_enabled()) {
343 return VCE_ERROR_NOT_SUPPORTED;
345 if (0 != __vce_check_privilege()) {
346 return VCE_ERROR_PERMISSION_DENIED;
349 int ret = VCE_ERROR_NONE;
351 ret = vcd_get_audio_type(audio_type);
353 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get audio type");
359 int vce_set_private_data(const char* key, const char* data)
361 if (0 != __vce_get_feature_enabled()) {
362 return VCE_ERROR_NOT_SUPPORTED;
364 if (0 != __vce_check_privilege()) {
365 return VCE_ERROR_PERMISSION_DENIED;
368 int ret = VCE_ERROR_NONE;
370 if (NULL == key || NULL == data) {
371 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
372 return VCE_ERROR_INVALID_PARAMETER;
375 if (!strncmp(key, "UpdateEventStart", strlen(key)))
376 ret = vce_send_update_status(VCE_UPDATE_EVENT_START, NULL);
377 else if (!strncmp(data, "UpdateEventComplete", strlen(key)))
378 ret = vce_send_update_status(VCE_UPDATE_EVENT_FINISH, NULL);
379 else if (!strncmp(data, "UpdateEventFail", strlen(key)))
380 ret = vce_send_update_status(VCE_UPDATE_EVENT_FAIL, data);
382 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send update status, event(%s), msg(%s): ret(%d)", key, data, ret);
386 ret = vcd_set_private_data(key, data);
388 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set private data to vc manager");
394 int vce_get_private_data(const char* key, char** data)
396 if (0 != __vce_get_feature_enabled()) {
397 return VCE_ERROR_NOT_SUPPORTED;
399 if (0 != __vce_check_privilege()) {
400 return VCE_ERROR_PERMISSION_DENIED;
403 int ret = VCE_ERROR_NONE;
405 if (NULL == key || NULL == data) {
406 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
407 return VCE_ERROR_INVALID_PARAMETER;
410 ret = vcd_get_private_data(key, data);
412 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to get private data from vc manager");
418 int vce_start_recording()
420 if (0 != __vce_get_feature_enabled()) {
421 return VCE_ERROR_NOT_SUPPORTED;
423 if (0 != __vce_check_privilege()) {
424 return VCE_ERROR_PERMISSION_DENIED;
427 int ret = VCE_ERROR_NONE;
429 ret = vcd_start_recording();
431 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to start recording");
437 int vce_stop_recording()
439 if (0 != __vce_get_feature_enabled()) {
440 return VCE_ERROR_NOT_SUPPORTED;
442 if (0 != __vce_check_privilege()) {
443 return VCE_ERROR_PERMISSION_DENIED;
446 int ret = VCE_ERROR_NONE;
448 ret = vcd_stop_recording();
450 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to stop recording");
456 int vce_send_update_status(vce_update_event_e update_event, const char* msg)
458 if (0 != __vce_get_feature_enabled()) {
459 return VCE_ERROR_NOT_SUPPORTED;
461 if (0 != __vce_check_privilege()) {
462 return VCE_ERROR_PERMISSION_DENIED;
465 int ret = vcd_send_update_status(update_event, msg);
467 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send update status");
473 int vce_set_private_data_set_cb(vce_private_data_set_cb callback_func)
475 if (0 != __vce_get_feature_enabled()) {
476 return VCE_ERROR_NOT_SUPPORTED;
478 if (0 != __vce_check_privilege()) {
479 return VCE_ERROR_PERMISSION_DENIED;
482 if (NULL == callback_func) {
483 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
484 return VCE_ERROR_INVALID_PARAMETER;
487 int ret = vcd_set_private_data_set_cb(callback_func);
489 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set private data set cb");
495 int vce_set_private_data_requested_cb(vce_private_data_requested_cb callback_func)
497 if (0 != __vce_get_feature_enabled()) {
498 return VCE_ERROR_NOT_SUPPORTED;
500 if (0 != __vce_check_privilege()) {
501 return VCE_ERROR_PERMISSION_DENIED;
504 if (NULL == callback_func) {
505 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
506 return VCE_ERROR_INVALID_PARAMETER;
509 int ret = vcd_set_private_data_requested_cb(callback_func);
511 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set private data requested cb");
517 int vce_set_nlu_base_info_requested_cb(vce_nlu_base_info_requested_cb callback_func)
519 if (0 != __vce_get_feature_enabled()) {
520 return VCE_ERROR_NOT_SUPPORTED;
523 if (NULL == callback_func) {
524 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
525 return VCE_ERROR_INVALID_PARAMETER;
528 int ret = vcd_set_nlu_base_info_requested_cb(callback_func);
530 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set nlu-base info requested cb");
536 int vce_set_specific_engine_request_cb(vce_specific_engine_request_cb callback_func)
538 if (0 != __vce_get_feature_enabled()) {
539 return VCE_ERROR_NOT_SUPPORTED;
542 if (NULL == callback_func) {
543 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
544 return VCE_ERROR_INVALID_PARAMETER;
547 int ret = vcd_set_specific_engine_request_cb(callback_func);
549 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set specific engine request cb");
555 int vce_unset_specific_engine_request_cb(void)
557 if (0 != __vce_get_feature_enabled()) {
558 return VCE_ERROR_NOT_SUPPORTED;
561 int ret = vcd_set_specific_engine_request_cb(NULL);
563 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to unset specific engine request cb");
569 /* for TTS feedback */
570 int vce_send_feedback_audio_format(int rate, vce_audio_channel_e channel, vce_audio_type_e audio_type)
572 if (0 != __vce_get_feature_enabled()) {
573 return VCE_ERROR_NOT_SUPPORTED;
576 int ret = VCE_ERROR_NONE;
578 if (channel < VCE_AUDIO_CHANNEL_MONO || channel > VCE_AUDIO_CHANNEL_STEREO) {
579 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
580 return VCE_ERROR_INVALID_PARAMETER;
583 if (audio_type < VCE_AUDIO_TYPE_PCM_S16_LE || audio_type > VCE_AUDIO_TYPE_PCM_U8) {
584 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
585 return VCE_ERROR_INVALID_PARAMETER;
588 ret = vcd_send_feedback_audio_format(rate, channel, audio_type);
590 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send feedback audio format");
596 int vce_send_feedback_streaming(vce_feedback_event_e event, char* buffer, int len)
598 if (0 != __vce_get_feature_enabled()) {
599 return VCE_ERROR_NOT_SUPPORTED;
602 int ret = VCE_ERROR_NONE;
604 if (NULL == buffer) {
605 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Input parameter is NULL");
606 return VCE_ERROR_INVALID_PARAMETER;
609 ret = vcd_send_feedback_streaming(event, buffer, len);
611 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to send feedback streaming");
617 int vce_set_request_tts_cb(vce_request_tts_cb callback_func, void* user_data)
619 if (NULL == callback_func) {
620 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
621 return VCE_ERROR_INVALID_PARAMETER;
624 int ret = vcd_set_request_tts_cb(callback_func, user_data);
626 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set request tts cb");
632 int vce_unset_request_tts_cb(void)
634 int ret = vcd_set_request_tts_cb(NULL, NULL);
636 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to unset request tts cb");
642 int vce_set_cancel_tts_cb(vce_cancel_tts_cb callback_func, void* user_data)
644 if (NULL == callback_func) {
645 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
646 return VCE_ERROR_INVALID_PARAMETER;
649 int ret = vcd_set_cancel_tts_cb(callback_func, user_data);
651 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set cancel tts cb");
657 int vce_unset_cancel_tts_cb(void)
659 int ret = vcd_set_cancel_tts_cb(NULL, NULL);
661 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to unset cancel tts cb");
667 int vce_set_tts_audio_format_request_cb(vce_tts_audio_format_request_cb callback_func, void* user_data)
669 if (NULL == callback_func) {
670 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Invalid parameter");
671 return VCE_ERROR_INVALID_PARAMETER;
674 int ret = vcd_set_tts_audio_format_request_cb(callback_func, user_data);
676 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to set get tts audio format cb");
682 int vce_unset_get_tts_audio_format_cb(void)
684 int ret = vcd_set_tts_audio_format_request_cb(NULL, NULL);
686 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] Fail to unset request tts cb");