Make a class for mas_config and add tests for it 21/224421/10
authorJi-hoon Lee <dalton.lee@samsung.com>
Mon, 10 Feb 2020 11:00:48 +0000 (20:00 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Fri, 3 Apr 2020 07:46:33 +0000 (16:46 +0900)
Change-Id: I1fc6eef26828353fde70a9bda74d09814887af4a

inc/multi_assistant_config.h
inc/multi_assistant_service_plugin.h
src/multi_assistant_config.cpp
src/multi_assistant_service.cpp
tests/utc/service/main.cpp

index c0737921d3109a530b21c9d039b71249684adf87..cec7c127916c69094bc3a1252fc1393c0653de21 100644 (file)
@@ -86,13 +86,32 @@ typedef struct {
 
 typedef int (*mas_config_assistant_info_cb)(ma_assistant_info_s* info, void* user_data);
 
-int mas_config_get_assistant_info(mas_config_assistant_info_cb callback, void* user_data);
-int mas_config_load_custom_wake_words(const char* app_id,
-       char wakeup_word[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
-       char wakeup_language[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN]);
-int mas_config_save_custom_wake_words(const char* app_id,
-       char wakeup_word[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
-       char wakeup_language[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN]);
+class CConfig {
+public:
+       CConfig() {};
+       virtual ~CConfig() {};
+
+       int mas_config_get_assistant_info(mas_config_assistant_info_cb callback, void* user_data);
+
+       int mas_config_add_custom_wake_word(const char* wake_word, const char* language,
+               char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+               char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN]);
+       int mas_config_remove_custom_wake_word(const char* wake_word, const char* language,
+               char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+               char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN]);
+       int mas_config_load_custom_wake_words(const char* app_id,
+               char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+               char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN]);
+       int mas_config_save_custom_wake_words(const char* app_id,
+               char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+               char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN]);
+       int mas_config_get_custom_wake_word_num(
+               char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+               char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN]);
+private:
+       int mas_config_parse_assistant_info(mas_config_assistant_info_cb callback,
+               const char *path, void* user_data);
+};
 
 #ifdef __cplusplus
 }
index 1639e458c6853b5e59ee76f4259dfbb078a60977..a8efc61324deaf3d792968a8c472107044b70ae4 100644 (file)
@@ -90,6 +90,8 @@ int multi_assistant_service_plugin_set_wake_word_audio_require_flag(const char*
 
 int multi_assistant_service_plugin_set_assistant_language(const char* appid, const char* language);
 
+int multi_assistant_service_plugin_set_voice_key_support_mode(int mode);
+
 int multi_assistant_service_plugin_set_voice_key_tap_duration(float duration);
 
 int multi_assistant_service_plugin_unset_voice_key_tap_duration(void);
index ba72431125cfb28a0b070328ef6d5fce5cdef13f..243740f199c4838fd7ee697e3726329a5fbcf71b 100644 (file)
@@ -24,7 +24,8 @@
 #include "multi_assistant_config.h"
 #include "multi_assistant_main.h"
 
-int mas_config_parse_assistant_info(mas_config_assistant_info_cb callback, const char *path, void* user_data)
+int CConfig::mas_config_parse_assistant_info(mas_config_assistant_info_cb callback,
+       const char *path, void* user_data)
 {
        xmlDocPtr doc = NULL;
        xmlNodePtr cur = NULL;
@@ -216,7 +217,7 @@ int mas_config_parse_assistant_info(mas_config_assistant_info_cb callback, const
        return 0;
 }
 
-int mas_config_get_assistant_info(mas_config_assistant_info_cb callback, void* user_data)
+int CConfig::mas_config_get_assistant_info(mas_config_assistant_info_cb callback, void* user_data)
 {
        const char *suffix = ".xml";
 
@@ -241,9 +242,68 @@ int mas_config_get_assistant_info(mas_config_assistant_info_cb callback, void* u
        return 0;
 }
 
-int mas_config_load_custom_wake_words(const char* app_id,
-       char wakeup_word[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
-       char wakeup_language[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN])
+int CConfig::mas_config_add_custom_wake_word(const char* wake_word, const char* language,
+       char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+       char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN])
+{
+       if (nullptr == wake_word || nullptr == language) return -1;
+
+       bool found = false;
+       for (int loop = 0;false == found && loop < MAX_WAKEUP_WORDS_NUM;loop++) {
+               if (0 == strncmp(wakeup_word_storage[loop], wake_word, MAX_WAKEUP_WORD_LEN) &&
+                       0 == strncmp(wakeup_language_storage[loop], language, MAX_SUPPORTED_LANGUAGE_LEN)) {
+                       LOGE("The wakeup word already exists!");
+                       return -1; /* Already exists */
+               }
+               if (0 == strlen(wakeup_word_storage[loop])) {
+                       strncpy(wakeup_word_storage[loop], wake_word, MAX_WAKEUP_WORD_LEN);
+                       wakeup_word_storage[loop][MAX_WAKEUP_WORD_LEN - 1] = '\0';
+                       strncpy(wakeup_language_storage[loop], language, MAX_SUPPORTED_LANGUAGE_LEN);
+                       wakeup_language_storage[loop][MAX_SUPPORTED_LANGUAGE_LEN - 1] = '\0';
+                       found = true;
+               }
+       }
+       if (!found) {
+               LOGE("No empty slot found while trying to add new wake word!");
+               return -1;
+       }
+       return 0;
+}
+
+int CConfig::mas_config_remove_custom_wake_word(const char* wake_word, const char* language,
+       char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+       char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN])
+{
+       if (nullptr == wake_word || nullptr == language) return -1;
+
+       bool found = false;
+       for (int loop = 0;loop < MAX_WAKEUP_WORDS_NUM;loop++) {
+               if (0 == strncmp(wakeup_word_storage[loop], wake_word, MAX_WAKEUP_WORD_LEN) &&
+                       0 == strncmp(wakeup_language_storage[loop], language, MAX_SUPPORTED_LANGUAGE_LEN)) {
+                       for (int shift = loop;shift < MAX_WAKEUP_WORDS_NUM - 1;shift++) {
+                               strncpy(wakeup_word_storage[shift],
+                                       wakeup_word_storage[shift + 1], MAX_WAKEUP_WORD_LEN);
+                               wakeup_word_storage[shift][MAX_WAKEUP_WORD_LEN - 1] = '\0';
+                               strncpy(wakeup_language_storage[shift],
+                                       wakeup_language_storage[shift + 1], MAX_SUPPORTED_LANGUAGE_LEN);
+                               wakeup_word_storage[shift][MAX_SUPPORTED_LANGUAGE_LEN - 1] = '\0';
+                       }
+                       memset(wakeup_word_storage[MAX_WAKEUP_WORDS_NUM - 1],
+                               0x00, sizeof(char) * MAX_WAKEUP_WORD_LEN);
+                       memset(wakeup_language_storage[MAX_WAKEUP_WORDS_NUM - 1],
+                               0x00, sizeof(char) * MAX_SUPPORTED_LANGUAGE_LEN);
+
+                       loop--; /* Just in case there are duplicated items */
+                       found = true;
+               }
+       }
+       if (!found) return -1;
+       return 0;
+}
+
+int CConfig::mas_config_load_custom_wake_words(const char* app_id,
+       char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+       char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN])
 {
        /* Add 1 for additional pipe character */
        char wakeup_words[MAX_WAKEUP_WORDS_NUM * (MAX_WAKEUP_WORD_LEN + 1)];
@@ -284,20 +344,21 @@ int mas_config_load_custom_wake_words(const char* app_id,
                *word_end = '\0';
                *language_end = '\0';
                if (0 == strlen(word_start)) break;
-               strncpy(wakeup_word[index], word_start, MAX_WAKEUP_WORD_LEN - 1);
-               strncpy(wakeup_language[index], language_start, MAX_WAKEUP_WORD_LEN - 1);
+               strncpy(wakeup_word_storage[index], word_start, MAX_WAKEUP_WORD_LEN - 1);
+               strncpy(wakeup_language_storage[index], language_start, MAX_WAKEUP_WORD_LEN - 1);
                word_start = word_end + 1;
                language_start = language_end + 1;
-               LOGD("Added custom wakeup word : (%s) (%s)", wakeup_word[index], wakeup_language[index]);
+               LOGD("Added custom wakeup word : (%s) (%s)",
+                       wakeup_word_storage[index], wakeup_language_storage[index]);
                index++;
        }
 
        return 0;
 }
 
-int mas_config_save_custom_wake_words(const char* app_id,
-       char wakeup_word[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
-       char wakeup_language[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN])
+int CConfig::mas_config_save_custom_wake_words(const char* app_id,
+       char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+       char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN])
 {
        /* Add 1 for additional pipe character */
        char wakeup_words[MAX_WAKEUP_WORDS_NUM * (MAX_WAKEUP_WORD_LEN + 1)];
@@ -305,12 +366,14 @@ int mas_config_save_custom_wake_words(const char* app_id,
        memset(wakeup_words, 0x00, sizeof(wakeup_words));
        memset(wakeup_languages, 0x00, sizeof(wakeup_languages));
        for (int loop = 0;loop < MAX_WAKEUP_WORDS_NUM;loop++) {
-               if (strlen(wakeup_word[loop]) > 0) {
+               if (strlen(wakeup_word_storage[loop]) > 0) {
                        int wakeup_words_len = strlen(wakeup_words);
-                       strncat(wakeup_words, wakeup_word[loop], sizeof(wakeup_words) - wakeup_words_len - 1);
+                       strncat(wakeup_words, wakeup_word_storage[loop],
+                               sizeof(wakeup_words) - wakeup_words_len - 1);
                        strcat(wakeup_words, "|");
                        int wakeup_languages_len = strlen(wakeup_languages);
-                       strncat(wakeup_languages, wakeup_language[loop], sizeof(wakeup_languages) - wakeup_languages_len - 1);
+                       strncat(wakeup_languages, wakeup_language_storage[loop],
+                               sizeof(wakeup_languages) - wakeup_languages_len - 1);
                        strcat(wakeup_languages, "|");
                }
        }
@@ -319,4 +382,17 @@ int mas_config_save_custom_wake_words(const char* app_id,
        preference_set_string("custom_wakeup_words", wakeup_words);
        preference_set_string("custom_wakeup_languages", wakeup_languages);
        return 0;
-}
\ No newline at end of file
+}
+
+int CConfig::mas_config_get_custom_wake_word_num(
+       char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN],
+       char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN])
+{
+       int num = 0;
+       for (int loop = 0;loop < MAX_WAKEUP_WORDS_NUM;loop++) {
+               if (strlen(wakeup_word_storage[loop]) > 0) {
+                       num++;
+               }
+       }
+       return num;
+}
index 34df15f2059a5dd88d7d77b05564e8d5a3b99025..56f5ed315833ec3a80633199debfb9dd55ec4a34 100644 (file)
@@ -79,6 +79,7 @@ static ma_voice_key_status_e g_last_voice_key_status = MA_VOICE_KEY_STATUS_RELEA
 
 /* client list */
 static GSList* g_client_list = NULL;
+static CConfig g_config;
 
 int ma_client_create(ma_client_s *info)
 {
@@ -583,32 +584,15 @@ int mas_client_add_wake_word(int pid, const char* wake_word, const char* languag
        for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
                if (g_maclient_info[loop].used &&
                        0 == strncmp(buf, g_maclient_info[loop].appid, MAX_APPID_LEN)) {
-                       bool found = false;
-                       bool duplicated = false;
-                       for (int innerLoop = 0;false == found && false == duplicated &&
-                               innerLoop < MAX_WAKEUP_WORDS_NUM;innerLoop++) {
-                               if (0 == strncmp(g_maclient_info[loop].wakeup_word[innerLoop], wake_word, MAX_WAKEUP_WORD_LEN) &&
-                                       0 == strncmp(g_maclient_info[loop].wakeup_language[innerLoop], language, MAX_SUPPORTED_LANGUAGE_LEN)) {
-                                       duplicated = true;
-                               }
-                               if (0 == strlen(g_maclient_info[loop].wakeup_word[innerLoop])) {
-                                       strncpy(g_maclient_info[loop].wakeup_word[innerLoop], wake_word, MAX_WAKEUP_WORD_LEN);
-                                       g_maclient_info[loop].wakeup_word[innerLoop][MAX_WAKEUP_WORD_LEN - 1] = '\0';
-                                       strncpy(g_maclient_info[loop].wakeup_language[innerLoop], language, MAX_SUPPORTED_LANGUAGE_LEN);
-                                       g_maclient_info[loop].wakeup_word[innerLoop][MAX_SUPPORTED_LANGUAGE_LEN - 1] = '\0';
-                                       found = true;
-                               }
-                       }
-                       if (found && !duplicated) {
-                               mas_config_save_custom_wake_words(pid_appid,
+                       ret = g_config.mas_config_add_custom_wake_word(wake_word, language,
+                               g_maclient_info[loop].wakeup_word,
+                               g_maclient_info[loop].wakeup_language);
+                       if (0 == ret) {
+                               g_config.mas_config_save_custom_wake_words(pid_appid,
                                        g_maclient_info[loop].wakeup_word,
                                        g_maclient_info[loop].wakeup_language);
                        } else {
-                               if (duplicated) {
-                                       LOGE("The wakeup word already exists!");
-                               } else {
-                                       LOGE("No empty slot found while trying to add new wake word!");
-                               }
+                               LOGE("add new wake word failed!");
                                return -1;
                        }
                }
@@ -631,28 +615,14 @@ int mas_client_remove_wake_word(int pid, const char* wake_word, const char* lang
        for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
                if (g_maclient_info[loop].used &&
                        0 == strncmp(buf, g_maclient_info[loop].appid, MAX_APPID_LEN)) {
-                       for (int innerLoop = 0;innerLoop < MAX_WAKEUP_WORDS_NUM;innerLoop++) {
-                               if (0 == strncmp(g_maclient_info[loop].wakeup_word[innerLoop], wake_word, MAX_WAKEUP_WORD_LEN) &&
-                                       0 == strncmp(g_maclient_info[loop].wakeup_language[innerLoop], language, MAX_SUPPORTED_LANGUAGE_LEN)) {
-                                       for (int shift = innerLoop;shift < MAX_WAKEUP_WORDS_NUM - 1;shift++) {
-                                               strncpy(g_maclient_info[loop].wakeup_word[shift],
-                                                       g_maclient_info[loop].wakeup_word[shift + 1], MAX_WAKEUP_WORD_LEN);
-                                               g_maclient_info[loop].wakeup_word[shift][MAX_WAKEUP_WORD_LEN - 1] = '\0';
-                                               strncpy(g_maclient_info[loop].wakeup_language[shift],
-                                                       g_maclient_info[loop].wakeup_language[shift + 1], MAX_SUPPORTED_LANGUAGE_LEN);
-                                               g_maclient_info[loop].wakeup_word[shift][MAX_SUPPORTED_LANGUAGE_LEN - 1] = '\0';
-                                       }
-                                       memset(g_maclient_info[loop].wakeup_word[MAX_WAKEUP_WORDS_NUM - 1],
-                                               0x00, sizeof(g_maclient_info[loop].wakeup_word));
-                                       memset(g_maclient_info[loop].wakeup_language[MAX_WAKEUP_WORDS_NUM - 1],
-                                               0x00, sizeof(g_maclient_info[loop].wakeup_language));
-
-                                       innerLoop--; /* Just in case there are duplicated items */
-                               }
-                       }
-                       mas_config_save_custom_wake_words(pid_appid,
+                       ret = g_config.mas_config_remove_custom_wake_word(wake_word, language,
+                               g_maclient_info[loop].wakeup_word,
+                               g_maclient_info[loop].wakeup_language);
+                       if (0 == ret) {
+                               g_config.mas_config_save_custom_wake_words(pid_appid,
                                        g_maclient_info[loop].wakeup_word,
                                        g_maclient_info[loop].wakeup_language);
+                       }
                }
        }
 
@@ -829,12 +799,12 @@ static int init_plugin(void)
                return -1;
        }
 
-       if (0 == mas_config_get_assistant_info(__mas_assistant_info_cb, NULL)) {
+       if (0 == g_config.mas_config_get_assistant_info(__mas_assistant_info_cb, NULL)) {
                for (int loop = 0; loop < MAX_MACLIENT_INFO_NUM; loop++) {
                        int inner_loop;
                        if (g_maclient_info[loop].used &&
                                0 < strlen(g_maclient_info[loop].appid)) {
-                               mas_config_load_custom_wake_words(g_maclient_info[loop].appid,
+                               g_config.mas_config_load_custom_wake_words(g_maclient_info[loop].appid,
                                        g_maclient_info[loop].wakeup_word, g_maclient_info[loop].wakeup_language);
                                if (0 < strlen(g_maclient_info[loop].wakeup_engine)) {
                                        multi_assistant_service_plugin_set_assistant_wakeup_engine(
index 24271c9a9fe164a538e8d4d2ddc19c0961890df8..1be2dc0239b6824cd7b688e67e3760d5e02c4748 100644 (file)
 
 #include <gtest/gtest.h>
 
+#include "multi_assistant_config.h"
+
+#include <string>
+
 class ServiceWithEmptyWakeWord : public testing::Test {
 public:
        ServiceWithEmptyWakeWord() {
        }
        virtual ~ServiceWithEmptyWakeWord() {
        }
+       void SetUp() override {
+               memset(wakeup_word_storage, 0x0, sizeof(wakeup_word_storage));
+               memset(wakeup_language_storage, 0x0, sizeof(wakeup_language_storage));
+       }
+       void TearDown() override {
+       }
+       CConfig config;
+       char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN];
+       char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN];
 };
 
 class ServiceWithAnArbitraryWakeWord : public testing::Test {
@@ -30,12 +43,48 @@ public:
        }
        virtual ~ServiceWithAnArbitraryWakeWord() {
        }
+       void SetUp() override {
+               memset(wakeup_word_storage, 0x0, sizeof(wakeup_word_storage));
+               memset(wakeup_language_storage, 0x0, sizeof(wakeup_language_storage));
+
+               strncpy(wakeup_word_storage[0], preloaded_wake_word.c_str(), sizeof(wakeup_word_storage[0]));
+               wakeup_word_storage[0][sizeof(wakeup_word_storage[0]) - 1] = '\0';
+       }
+       void TearDown() override {
+       }
+       const std::string preloaded_wake_word{"Hi Preloaded"};
+       const std::string preloaded_language{"pr_LD"};
+       CConfig config;
+       char wakeup_word_storage[MAX_WAKEUP_WORDS_NUM][MAX_WAKEUP_WORD_LEN];
+       char wakeup_language_storage[MAX_WAKEUP_WORDS_NUM][MAX_SUPPORTED_LANGUAGE_LEN];
 };
 
 TEST_F(ServiceWithEmptyWakeWord, HasOneWakeWordAfterAdd) {
+       const std::string arbitrary_wake_word{"Hi Tizen"};
+       const std::string arbitrary_language{"ar_LA"};
+
+       config.mas_config_add_custom_wake_word(
+               arbitrary_wake_word.c_str(), arbitrary_language.c_str(),
+               wakeup_word_storage,
+               wakeup_language_storage);
+
+       int wake_word_num = config.mas_config_get_custom_wake_word_num(
+               wakeup_word_storage, wakeup_language_storage);
+       ASSERT_EQ(wake_word_num, 1);
 }
 
 TEST_F(ServiceWithAnArbitraryWakeWord, HasTwoWakeWordsAfterAdd) {
+       const std::string arbitrary_wake_word{"Hi Tizen"};
+       const std::string arbitrary_language{"ar_LA"};
+
+       config.mas_config_add_custom_wake_word(
+               arbitrary_wake_word.c_str(), arbitrary_language.c_str(),
+               wakeup_word_storage,
+               wakeup_language_storage);
+
+       int wake_word_num = config.mas_config_get_custom_wake_word_num(
+               wakeup_word_storage, wakeup_language_storage);
+       ASSERT_EQ(wake_word_num, 2);
 }
 
 int main(int argc, char** argv) {