Add APIs for adding/removing wake words 90/224190/12
authorJi-hoon Lee <dalton.lee@samsung.com>
Fri, 7 Feb 2020 04:35:42 +0000 (13:35 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Thu, 2 Apr 2020 00:21:02 +0000 (09:21 +0900)
Change-Id: Ia801deacb430d5430db811f9950e31e34be4f12c

client/ma.c
client/ma_dbus.c
client/ma_dbus.h
common/ma_defs.h
include/multi_assistant.h

index 194850b18492e08eada7911d13ab07824cab9d0d..cd4a949ea9b7d598ff3b9b90195cbcee5eaa0618 100644 (file)
@@ -2294,4 +2294,82 @@ int ma_unset_voice_key_status_changed_cb(void)
        return MA_ERROR_NONE;
 }
 
+int ma_add_wake_word(const char* wake_word, const char *language) {
+       MA_SLOGD("[Manager] Add wake word");
+
+       if (0 != __ma_get_feature_enabled()) {
+               MA_SLOGD("@@@ [Manager] not supported");
+               return MA_ERROR_NOT_SUPPORTED;
+       }
+
+       if (NULL == wake_word || NULL == language) {
+               MA_SLOGE("[ERROR] Invalid parameter"); //LCOV_EXCL_LINE
+               return MA_ERROR_INVALID_PARAMETER;
+       }
+
+       ma_state_e state;
+       if (0 != ma_client_get_client_state(g_ma, &state)) {
+               MA_SLOGE("[ERROR] A handle is not available");
+               MA_SLOGD("@@@");
+               return MA_ERROR_INVALID_STATE;
+       }
+
+       if (state != MA_STATE_READY) {
+               MA_SLOGE("[ERROR] Invalid State: Current state is not 'READY', state(%d)", state);
+               MA_SLOGD("@@@");
+               return MA_ERROR_INVALID_STATE;
+       }
+
+       MA_SLOGI("[Client DEBUG] Add wake word : %s %s", wake_word, language);
+
+       /* Add wake word */
+       int pid = getpid();
+       int ret = ma_dbus_add_wake_word(pid, wake_word, language);
+       if (0 != ret) {
+               MA_SLOGW("[WARNING] Failed to add wake word");
+       } else {
+               MA_SLOGD("[DEBUG] Success to add");
+       }
+       return ret;
+}
+
+int ma_remove_wake_word(const char* wake_word, const char *language) {
+       MA_SLOGD("[Manager] Remove wake word");
+
+       if (0 != __ma_get_feature_enabled()) {
+               MA_SLOGD("@@@ [Manager] not supported");
+               return MA_ERROR_NOT_SUPPORTED;
+       }
+
+       if (NULL == wake_word || NULL == language) {
+               MA_SLOGE("[ERROR] Invalid parameter"); //LCOV_EXCL_LINE
+               return MA_ERROR_INVALID_PARAMETER;
+       }
+
+       ma_state_e state;
+       if (0 != ma_client_get_client_state(g_ma, &state)) {
+               MA_SLOGE("[ERROR] A handle is not available");
+               MA_SLOGD("@@@");
+               return MA_ERROR_INVALID_STATE;
+       }
+
+       if (state != MA_STATE_READY) {
+               MA_SLOGE("[ERROR] Invalid State: Current state is not 'READY', state(%d)", state);
+               MA_SLOGD("@@@");
+               return MA_ERROR_INVALID_STATE;
+       }
+
+       MA_SLOGI("[Client DEBUG] Remove wake word : %s %s", wake_word, language);
+
+       /* Remove wake word */
+       int pid = getpid();
+       int ret = ma_dbus_remove_wake_word(pid, wake_word, language);
+       if (0 != ret) {
+               MA_SLOGW("[WARNING] Failed to remove wake word");
+       } else {
+               MA_SLOGD("[DEBUG] Success to remove");
+       }
+       return ret;
+}
+
 //LCOV_EXCL_STOP
index 0399c52042558f310624d872067a8d06066fdd59..3163676a48d9ed82e53273e2b382363bc6b4ccc1 100644 (file)
@@ -1642,3 +1642,163 @@ int ma_dbus_set_assistant_wakeup_language(int pid, const char* language)
 
        return 0;
 }
+
+int ma_dbus_add_wake_word(int pid, const char* wake_word, const char* language)
+{
+       if (0 != __dbus_check()) {
+               return MA_ERROR_OPERATION_FAILED;
+       }
+
+       DBusMessage* msg;
+
+       msg = dbus_message_new_method_call(
+                         MA_SERVER_SERVICE_NAME,
+                         MA_SERVER_SERVICE_OBJECT_PATH,
+                         MA_SERVER_SERVICE_INTERFACE,
+                         MA_METHOD_ADD_WAKE_WORD);
+
+       if (NULL == msg) {
+               MA_SLOGE("@@ Request multi-assistant add wake word : Fail to make message"); //LCOV_EXCL_LINE
+               return MA_ERROR_OPERATION_FAILED;
+       } else {
+               MA_SLOGD("[DEBUG] multi-assistant add wake word");
+       }
+
+       char* tmp_wake_word = NULL;
+       if (NULL != wake_word) {
+               tmp_wake_word = strdup(wake_word);
+       } else {
+               tmp_wake_word = strdup("#NULL");
+       }
+
+       char* tmp_language = NULL;
+       if (NULL != language) {
+               tmp_language = strdup(language);
+       } else {
+               tmp_language = strdup("#NULL");
+       }
+
+       dbus_message_append_args(msg,
+               DBUS_TYPE_INT32, &pid,
+               DBUS_TYPE_STRING, &tmp_wake_word,
+               DBUS_TYPE_STRING, &tmp_language,
+               DBUS_TYPE_INVALID);
+
+       dbus_message_set_no_reply(msg, TRUE);
+
+       DBusError error;
+       dbus_error_init (&error);
+       if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
+               if (dbus_error_is_set (&error)) {
+                       MA_SLOGE("[Dbus ERROR] Fail to Send : %s", error.message); //LCOV_EXCL_LINE
+                       dbus_error_free (&error);
+               } else {
+                       MA_SLOGE("[Dbus ERROR] Fail to Send"); //LCOV_EXCL_LINE
+               }
+               if (NULL != tmp_wake_word) {
+                       free(tmp_wake_word);
+                       tmp_wake_word = NULL;
+               }
+               if (NULL != tmp_language) {
+                       free(tmp_language);
+                       tmp_language = NULL;
+               }
+               return MA_ERROR_OPERATION_FAILED;
+       } else {
+               MA_SLOGD("[Dbus DEBUG] Success to Send");
+               dbus_connection_flush(g_conn_sender);
+       }
+
+       if (NULL != tmp_wake_word) {
+               free(tmp_wake_word);
+               tmp_wake_word = NULL;
+       }
+       if (NULL != tmp_language) {
+               free(tmp_language);
+               tmp_language = NULL;
+       }
+
+       dbus_message_unref(msg);
+
+       return 0;
+}
+
+int ma_dbus_remove_wake_word(int pid, const char* wake_word, const char* language)
+{
+       if (0 != __dbus_check()) {
+               return MA_ERROR_OPERATION_FAILED;
+       }
+
+       DBusMessage* msg;
+
+       msg = dbus_message_new_method_call(
+                         MA_SERVER_SERVICE_NAME,
+                         MA_SERVER_SERVICE_OBJECT_PATH,
+                         MA_SERVER_SERVICE_INTERFACE,
+                         MA_METHOD_REMOVE_WAKE_WORD);
+
+       if (NULL == msg) {
+               MA_SLOGE("@@ Request multi-assistant remove wake word : Fail to make message"); //LCOV_EXCL_LINE
+               return MA_ERROR_OPERATION_FAILED;
+       } else {
+               MA_SLOGD("[DEBUG] multi-assistant remove wake word");
+       }
+
+       char* tmp_wake_word = NULL;
+       if (NULL != wake_word) {
+               tmp_wake_word = strdup(wake_word);
+       } else {
+               tmp_wake_word = strdup("#NULL");
+       }
+
+       char* tmp_language = NULL;
+       if (NULL != language) {
+               tmp_language = strdup(language);
+       } else {
+               tmp_language = strdup("#NULL");
+       }
+
+       dbus_message_append_args(msg,
+               DBUS_TYPE_INT32, &pid,
+               DBUS_TYPE_STRING, &tmp_wake_word,
+               DBUS_TYPE_STRING, &tmp_language,
+               DBUS_TYPE_INVALID);
+
+       dbus_message_set_no_reply(msg, TRUE);
+
+       DBusError error;
+       dbus_error_init (&error);
+       if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
+               if (dbus_error_is_set (&error)) {
+                       MA_SLOGE("[Dbus ERROR] Fail to Send : %s", error.message); //LCOV_EXCL_LINE
+                       dbus_error_free (&error);
+               } else {
+                       MA_SLOGE("[Dbus ERROR] Fail to Send"); //LCOV_EXCL_LINE
+               }
+               if (NULL != tmp_wake_word) {
+                       free(tmp_wake_word);
+                       tmp_wake_word = NULL;
+               }
+               if (NULL != tmp_language) {
+                       free(tmp_language);
+                       tmp_language = NULL;
+               }
+               return MA_ERROR_OPERATION_FAILED;
+       } else {
+               MA_SLOGD("[Dbus DEBUG] Success to Send");
+               dbus_connection_flush(g_conn_sender);
+       }
+
+       if (NULL != tmp_wake_word) {
+               free(tmp_wake_word);
+               tmp_wake_word = NULL;
+       }
+       if (NULL != tmp_language) {
+               free(tmp_language);
+               tmp_language = NULL;
+       }
+
+       dbus_message_unref(msg);
+
+       return 0;
+}
\ No newline at end of file
index f74a1b5489f76ec4035cb250cfa0ecf59c75b847..c40c4f014f8ed834a54446bdb9d399c004c92531 100644 (file)
@@ -65,6 +65,10 @@ int ma_dbus_set_wake_word_audio_require_flag(int pid, bool require);
 
 int ma_dbus_set_assistant_wakeup_language(int pid, const char* language);
 
+int ma_dbus_add_wake_word(int pid, const char* wake_word, const char* language);
+
+int ma_dbus_remove_wake_word(int pid, const char* wake_word, const char* language);
+
 #ifdef __cplusplus
 }
 #endif
index c646763c88a32a9244959b3945950112f3350dd4..41b586d920cd450105f6d7799a907ee1c48b11cd 100644 (file)
@@ -82,6 +82,8 @@ extern "C"
 #define MA_METHOD_SEND_PREPROCESSING_RESULT                    "ma_method_send_preprocessing_result"
 #define MA_METHOD_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG     "ma_method_set_wake_word_audio_require_flag"
 #define MA_METHOD_SET_ASSISTANT_WAKEUP_LANGUAGE                        "ma_method_set_assistant_wakeup_language"
+#define MA_METHOD_ADD_WAKE_WORD                                                "ma_method_add_wake_word"
+#define MA_METHOD_REMOVE_WAKE_WORD                                     "ma_method_remove_wake_word"
 #define MA_METHOD_ERROR                                                                "ma_method_error"
 
 #define MA_UI_METHOD_INITIALIZE                                                "ma_ui_method_initialize"
index 5bf251f6b4574b939552c078b2a187de993f53fe..38a76469eb8e83fdd507e14dd027baeb72e71473 100644 (file)
@@ -783,6 +783,56 @@ int ma_set_voice_key_status_changed_cb(ma_voice_key_status_changed_cb callback,
  */
 int ma_unset_voice_key_status_changed_cb(void);
 
+/**
+ * @brief Adds a wake word used for activating assistant.
+ * @details Different <word, language> pairs identify a different word.
+ *          For example, <"Hi Tizen", "en_US"> and <"Hi Tizen", "en_GB">
+ *          are different words. If <"Hi Tizen", "en_US"> is added, then
+ *          "Hi Tizen" will not wake the Tizen voice assistant if the
+ *          wakeup language is set to "en_GB". <"Hi Tizen", "en_GB"> will
+ *          have to be added for that.\n
+ *          Adding the same word twice does not cause any error to be returned.
+ * @since_tizen 6.0
+ *
+ * @param[in] wake_word The wake word to be added to the list of wake words
+ * @param[in] language The language code for which the word will be added.
+ *                     The language is identified by its code (e.g. "en_US").
+ *
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #MA_ERROR_NONE Successful
+ * @retval #MA_ERROR_NOT_SUPPORTED Not supported
+ * @retval #MA_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MA_ERROR_INVALID_STATE Invalid state
+ * @retval #MA_ERROR_OPERATION_FAILED Operation failed
+ *
+ * @pre The state should be #MA_STATE_READY.
+ */
+int ma_add_wake_word(const char* wake_word, const char *language);
+
+/**
+ * @brief Removes a wake word used for activating assistant.
+ * @details Different <word, language> pairs identify a different word.
+ *          For example, <"Hi Tizen", "en_US"> and <"Hi Tizen", "en_GB">
+ *          are different words.\n
+ *          Removing a word not present on the list does not cause any error
+ *          to be returned.
+ * @since_tizen 6.0
+ *
+ * @param[in] wake_word The wake word to be removed from the list of wake words
+ * @param[in] language The language code for which the word will be removed.
+ *                     The language is identified by its code (e.g. "en_US").
+ *
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #MA_ERROR_NONE Successful
+ * @retval #MA_ERROR_NOT_SUPPORTED Not supported
+ * @retval #MA_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MA_ERROR_INVALID_STATE Invalid state
+ * @retval #MA_ERROR_OPERATION_FAILED Operation failed
+ *
+ * @pre The state should be #MA_STATE_READY.
+ */
+int ma_remove_wake_word(const char* wake_word, const char *language);
+
 #ifdef __cplusplus
 }
 #endif