Fix 1803 audio API.

Change-Id: Ie59683f3754541322e2a2f566637a3e4223e0eb6
diff --git a/mbtk/mbtk_lib/src/mbtk_audio.c b/mbtk/mbtk_lib/src/mbtk_audio.c
index f22120b..382bef3 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio.c
@@ -340,7 +340,6 @@
     else
     {
         printf("%s: ubus_invoke_async success\n", __FUNCTION__);
-        mbtk_audio_ubus_db->work_state++;
     }
 }
 
@@ -367,7 +366,6 @@
     else
     {
         printf("%s: ubus_invoke_async success\n", __FUNCTION__);
-        mbtk_audio_ubus_db->work_state++;
     }
 }
 
@@ -387,25 +385,36 @@
 void mbtk_audio_switch_pcm(int mode)
 {
     int rc = 0;
+    struct ubus_request *req = NULL;
 
     if(NULL == mbtk_audio_ubus_db)
     {
         printf("mbtk_dtmf_ubus not init!\n");
         return;
     }
+    req = (struct ubus_request *)malloc(sizeof(struct ubus_request));
+    if (req == NULL)
+    {
+        printf("leave %s: lack of memory\n", __FUNCTION__);
+        return;
+    }
+    memset(req, 0, sizeof(struct ubus_request));
     blob_buf_init(&audio_cm_b, 0);
     blobmsg_add_u32(&audio_cm_b, "param0", mode);
-    if ((rc = ubus_invoke(mbtk_audio_ubus_db->ctx,
+    if ((rc = ubus_invoke_async(mbtk_audio_ubus_db->ctx,
                                 mbtk_audio_ubus_db->audioif_request_id,
                                 AUDIO_UBUS_SWITCH_PCM,
-                                audio_cm_b.head, NULL, NULL, 0)) != UBUS_STATUS_OK)
+                                audio_cm_b.head, req)) != UBUS_STATUS_OK)
     {
+        free(req);
         printf("%s, ubus_invoke_async %s failed %s\n", __FUNCTION__, AUDIO_UBUS_MODE_SET, ubus_strerror(rc));
     }
     else
     {
         printf("%s: ubus_invoke_async success\n", __FUNCTION__);
         mbtk_audio_ubus_db->work_state++;
+        req->complete_cb = mbtk_ubus_complete_cb;
+        ubus_complete_request_async(mbtk_audio_ubus_db->ctx, req);
     }
 }
 
diff --git a/mbtk/test/asr1803/aiti_audio_test.c b/mbtk/test/asr1803/aiti_audio_test.c
index 4e24440..0472c9a 100755
--- a/mbtk/test/asr1803/aiti_audio_test.c
+++ b/mbtk/test/asr1803/aiti_audio_test.c
@@ -7,7 +7,6 @@
 // #include "ql_at.h"
 #include "ql/ql_audio.h"
 // #include "mopen_tts.h"
-#ifdef MBTK_PLATFORM_ASR1803
 
 #define MBTK_AUD_DEMO_WAV "/data/demo.wav"
 
@@ -535,13 +534,5 @@
 
     return 0;
 }
-#else
-int main(int argc, char *argv[])
-{
-
-    return 0;
-}
-
-#endif
 
 
diff --git a/mbtk/test/asr1803/audio_play_test.c b/mbtk/test/asr1803/audio_play_test.c
new file mode 100755
index 0000000..24a08dc
--- /dev/null
+++ b/mbtk/test/asr1803/audio_play_test.c
@@ -0,0 +1,211 @@
+#include "mbtk_type.h"
+#include <fcntl.h>
+#include <stdint.h>
+#include <limits.h>
+#include <termios.h>
+#include <stdarg.h>
+#include <pthread.h>
+#include "ql/ql_audio.h"
+
+static pthread_t play_thread_play;
+int play_hdl = 0;
+int handler = 0;
+int fd = -1;
+
+int Ql_cb_playback(int hdl, int result)
+{
+    // printf("%s: hdl=%d, result=%d\n\r", __func__, hdl, result);
+    if (result == AUD_PLAYER_FINISHED || result == AUD_PLAYER_NODATA)
+    {
+        printf("%s: play finished\n\r", __func__);
+    }
+    return 0;
+}
+
+
+void dtmf_cb1(char dtmf)
+{
+    printf("%s:%c\n", __FUNCTION__, dtmf);
+}
+
+int MBTK_wav_pcm16Le_check(int fd)
+{
+    struct wav_header hdr;
+
+    if (fd <= 0)
+        return -1;
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
+    {
+        printf("\n%s: cannot read header\n", __FUNCTION__);
+        return -1;
+    }
+
+    if ((hdr.riff_id != ID_RIFF)
+            || (hdr.riff_fmt != ID_WAVE)
+            || (hdr.fmt_id != ID_FMT))
+    {
+        printf("\n%s: is not a riff/wave file\n", __FUNCTION__);
+        return -1;
+    }
+
+    if ((hdr.audio_format != FORMAT_PCM) || (hdr.fmt_sz != 16)) {
+        printf("\n%s: is not pcm format\n", __FUNCTION__);
+        return -1;
+    }
+
+    if (hdr.bits_per_sample != 16) {
+        printf("\n%s: is not 16bit per sample\n", __FUNCTION__);
+        return -1;
+    }
+
+    return 0;
+}
+
+int MBTK_wav_pcm16Le_set(int fd)
+{
+    struct wav_header hdr;
+
+    if (fd <= 0)
+        return -1;
+
+    memset(&hdr, 0, sizeof(struct wav_header));
+
+    hdr.riff_id = ID_RIFF;
+    hdr.riff_fmt = ID_WAVE;
+    hdr.fmt_id = ID_FMT;
+    hdr.fmt_sz = 16;
+    hdr.audio_format = FORMAT_PCM;
+    hdr.num_channels = 1;
+    hdr.sample_rate = 8000;
+    hdr.bits_per_sample = 16;
+    hdr.byte_rate = (8000 * 1 * hdr.bits_per_sample) / 8;
+    hdr.block_align = (hdr.bits_per_sample * 1) / 8;
+    hdr.data_id = ID_DATA;
+    hdr.data_sz = 0;
+
+    hdr.riff_sz = hdr.data_sz + 44 - 8;
+    if (write(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
+        return -1;
+    }
+
+    return 0;
+}
+
+static void audio_play_thread(void *arg)
+{
+    Ql_AudPlayer_PlayFrmFile(play_hdl, fd, 0);
+}
+
+
+int main(int argc, char* argv[])
+{
+    if(argc != 2) {
+        printf("audio_play_test <wav_file>\n");
+        return -1;
+    }
+
+    struct stat st;
+    const char *path = (const char *)argv[1];
+    // mbtk_log_init("radio", "MBTK_AUDIO");
+
+    /* Check and open source file */
+    if (access(path, F_OK) || stat(path, &st)) {
+        printf("%s: error reading from file %s\n", __FUNCTION__, path);
+        return -1;
+    }
+
+    if (!st.st_size) {
+        printf("%s: empty file %s\n", __FUNCTION__, path);
+        return -1;
+    }
+
+    printf("1\n");
+    mbtk_audio_ubus_client_init(&handler, dtmf_cb1);
+    printf("2\n");
+    mbtk_audio_switch_pcm(1);
+    printf("3\n");
+    mbtk_audio_mode_set(2);
+    printf("4\n");
+    play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback);
+    printf("5\n");
+    if(0 == play_hdl) {
+        printf("Ql_AudPlayer_Open fail\n");
+        return -1;
+    }
+
+    char cmd[100];
+    bool running = TRUE;
+    printf("6\n");
+    while(running)
+    {
+        memset(cmd, 0, 100);
+        int err;
+        printf("1 : Play    Other : Exit\n");
+        if(fgets(cmd, 100, stdin))
+        {
+            if(cmd[0] == '\r' || cmd[0] == '\n')
+                continue;
+            int state = atoi(cmd);
+            switch(state){
+                case 1:
+                {
+                    if(0 == play_hdl)
+                        break;
+                    fd = open(argv[1], O_RDONLY);
+                    if (fd <= 0) {
+                        printf("Open file fail.\n");
+                        goto exit;
+                    }
+
+                    mbtk_audio_path_enable(1);
+                    if(0 == MBTK_wav_pcm16Le_check(fd))
+                    {
+                        pthread_attr_t thread_attr;
+                        pthread_attr_init(&thread_attr);
+                        if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+                        {
+                            printf("pthread_attr_setdetachstate() fail.\n");
+                            goto exit;
+                        }
+
+                        if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, NULL) < 0) {
+                            printf("%s: error creating thread_play!\n", __FUNCTION__);
+                            goto exit;
+                        }
+
+                        printf("Start play audio...\n");
+                    }
+                    else
+                    {
+                        printf("aplay file type error\n");
+                        goto exit;
+                    }
+                    break;
+                }
+                default:
+                {
+                    if(0 == play_hdl)
+                        break;
+                    Ql_AudPlayer_Close(play_hdl);
+                    running = FALSE;
+                    break;
+                }
+            }
+        }
+    }
+
+exit:
+    if(fd > 0) {
+        close(fd);
+    }
+    mbtk_audio_path_enable(0);
+
+    mbtk_audio_mode_set(0);
+    mbtk_audio_switch_pcm(0);
+
+
+    printf("Success exit.\n");
+    return 0;
+}
+
diff --git a/mbtk/test/asr1803/audio_test.c b/mbtk/test/asr1803/audio_test.c
index b811307..d8121ee 100755
--- a/mbtk/test/asr1803/audio_test.c
+++ b/mbtk/test/asr1803/audio_test.c
@@ -8,8 +8,6 @@
 #include "ql/ql_audio.h"
 // #include "mopen_tts.h"
 
-#ifdef MBTK_PLATFORM_ASR1803
-
 #define MBTK_AUD_DEMO_WAV "/data/demo.wav"
 
 static int record_fd = 0;
@@ -342,11 +340,3 @@
     return 0;
 }
 
-#else
-int main(int argc, char *argv[])
-{
-
-    return 0;
-}
-
-#endif
diff --git a/mbtk/test/asr1803/audio_test_yx.c b/mbtk/test/asr1803/audio_test_yx.c
new file mode 100755
index 0000000..53a7c01
--- /dev/null
+++ b/mbtk/test/asr1803/audio_test_yx.c
@@ -0,0 +1,246 @@
+#include "mbtk_type.h"
+#include <fcntl.h>
+#include <stdint.h>
+#include <limits.h>
+#include <termios.h>
+#include <stdarg.h>
+#include <fcntl.h>
+#include <sys/time.h>
+#include <pthread.h>
+#include <signal.h>
+
+#include "ql/ql_audio.h"
+
+int handler = 0;
+int s_play_hdl = -1;
+int s_play_fd = -1;
+static pthread_t play_thread_play;
+
+#define PROMPT_START_RECORD "/bin/start_record_test.wav"
+#define RECORD_FILEPATH "/usrdata/test.wav"
+#define PROMPT_START_PLAY "/bin/start_play_test.wav"
+#define PROMPT_TEST_FINISH "/bin/test_end_test.wav"
+
+#if 1
+void dtmf_cb1(char dtmf)
+{
+    printf("%s:%c\n", __FUNCTION__, dtmf);
+}
+#endif
+
+int Ql_cb_playback(int hdl, int result)
+{
+    printf("%s: hdl=%d, result=%d\n\r", __func__, hdl, result);
+    if (result == AUD_PLAYER_FINISHED || result == AUD_PLAYER_NODATA)
+    {
+        printf("%s: play finished\n\r", __func__);
+    }
+    return 0;
+}
+
+int MBTK_wav_pcm16Le_check(int fd)
+{
+    struct wav_header hdr;
+
+    if (fd <= 0)
+        return -1;
+
+    if (read(fd, &hdr, sizeof(hdr)) != sizeof(hdr))
+    {
+        printf("\n%s: cannot read header\n", __FUNCTION__);
+        return -1;
+    }
+
+    if ((hdr.riff_id != ID_RIFF) || (hdr.riff_fmt != ID_WAVE) || (hdr.fmt_id != ID_FMT))
+    {
+        printf("\n%s: is not a riff/wave file\n", __FUNCTION__);
+        return -1;
+    }
+
+    if ((hdr.audio_format != FORMAT_PCM) || (hdr.fmt_sz != 16))
+    {
+        printf("\n%s: is not pcm format\n", __FUNCTION__);
+        return -1;
+    }
+
+    if (hdr.bits_per_sample != 16)
+    {
+        printf("\n%s: is not 16bit per sample\n", __FUNCTION__);
+        return -1;
+    }
+
+    return 0;
+}
+
+int PLT_Audio_StartPlay(char *filePath, int repeat_value)
+{
+    s_play_hdl = Ql_AudPlayer_Open(NULL, Ql_cb_playback);
+	if (0 == s_play_hdl) {
+		printf("Ql_AudPlayer_Open Failed...\n");
+		return -2;
+	}
+
+	s_play_fd = open(filePath, O_RDWR);
+	if (s_play_fd <= 0){
+		printf(" Open wavFilePath Failed\n");
+	    return -3 ;
+	}
+
+	mbtk_audio_path_enable(1);
+	if (0 == MBTK_wav_pcm16Le_check(s_play_fd)) {
+		Ql_AudPlayer_PlayFrmFile(s_play_hdl, s_play_fd, 0);
+	} else {
+		printf("aplay FileType error...\n");
+		close(s_play_fd);
+		return -4;
+	}
+	close(s_play_fd);
+	mbtk_audio_path_enable(0);
+	Ql_AudPlayer_Close(s_play_hdl);
+
+	printf("aplay End...\n");
+    return 0;
+}
+
+static void audio_play_thread(void *arg)
+{
+    PLT_Audio_StartPlay((char*)arg, 1);
+}
+
+static void sig_handler(int sig)
+{
+    if(s_play_hdl > 0) {
+        Ql_AudPlayer_Stop(s_play_hdl);
+        if (pthread_join(play_thread_play, NULL)) {
+            printf("error join play_recorder!\n");
+        }
+
+        mbtk_audio_mode_set(-2);
+        mbtk_audio_switch_pcm(0);
+        mbtk_audio_ubus_client_deinit(handler);
+    }
+
+    printf("Success exit by signal...\n");
+    exit(0);
+}
+
+
+int main(int argc, char* argv[])
+{
+    if(argc != 2) {
+        printf("audio_play_test <wav_file>\n");
+        return -1;
+    }
+
+    struct stat st;
+    const char *path = (const char *)argv[1];
+    // mbtk_log_init("radio", "MBTK_AUDIO");
+
+    /* Check and open source file */
+    if (access(path, F_OK) || stat(path, &st)) {
+        printf("%s: error reading from file %s\n", __FUNCTION__, path);
+        return -1;
+    }
+
+    if (!st.st_size) {
+        printf("%s: empty file %s\n", __FUNCTION__, path);
+        return -1;
+    }
+
+    signal(SIGINT, sig_handler);
+    signal(SIGTERM, sig_handler);
+
+    mbtk_audio_ubus_client_init(&handler, dtmf_cb1);
+    mbtk_audio_switch_pcm(1);
+    mbtk_audio_mode_set(2);
+
+    char cmd[100];
+    bool running = TRUE;
+    while(running)
+    {
+        memset(cmd, 0, 100);
+        int err;
+        printf("1 : Play    2 : Pause    3 : Resume    4 : Stop    Other : Exit\n");
+        if(fgets(cmd, 100, stdin))
+        {
+            if(cmd[0] == '\r' || cmd[0] == '\n')
+                continue;
+            int state = atoi(cmd);
+            switch(state){
+                case 1:
+                {
+                    if(s_play_hdl > 0) {
+                        break;
+                    }
+                    pthread_attr_t thread_attr;
+                    pthread_attr_init(&thread_attr);
+                    if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+                    {
+                        printf("pthread_attr_setdetachstate() fail.\n");
+                        goto exit;
+                    }
+
+                    if (pthread_create(&play_thread_play, NULL, (void *)&audio_play_thread, path) < 0) {
+                        printf("%s: error creating thread_play!\n", __FUNCTION__);
+                        goto exit;
+                    }
+
+                    printf("Start play audio...\n");
+                    break;
+                }
+                case 2:
+                {
+                    if(s_play_hdl <= 0) {
+                        break;
+                    }
+                    Ql_AudPlayer_Pause(s_play_hdl);
+                    printf("Audio pause.\n");
+                    break;
+                }
+                case 3:
+                {
+                    if(s_play_hdl <= 0) {
+                        break;
+                    }
+                    Ql_AudPlayer_Resume(s_play_hdl);
+                    printf("Audio resume.\n");
+                    break;
+                }
+                case 4:
+                {
+                    if(s_play_hdl <= 0) {
+                        break;
+                    }
+                    Ql_AudPlayer_Stop(s_play_hdl);
+                    if (pthread_join(play_thread_play, NULL)) {
+                        printf("error join play_thread!\n");
+                    }
+                    s_play_hdl = -1;
+                    printf("Audio stop.\n");
+                    break;
+                }
+                default:
+                {
+                    running = FALSE;
+                    if(s_play_hdl <= 0) {
+                        break;
+                    }
+                    Ql_AudPlayer_Stop(s_play_hdl);
+                    if (pthread_join(play_thread_play, NULL)) {
+                        printf("error join play_thread!\n");
+                    }
+                }
+            }
+        }
+    }
+
+exit:
+
+    mbtk_audio_mode_set(-2);
+    mbtk_audio_switch_pcm(0);
+    mbtk_audio_ubus_client_deinit(handler);
+
+    printf("Success exit.\n");
+    return 0;
+}
+
diff --git a/mbtk/test/asr1803/mbtk_dtmf_test.c b/mbtk/test/asr1803/mbtk_dtmf_test.c
index 472e820..7dd055f 100755
--- a/mbtk/test/asr1803/mbtk_dtmf_test.c
+++ b/mbtk/test/asr1803/mbtk_dtmf_test.c
@@ -1,5 +1,3 @@
-#ifdef MBTK_PLATFORM_ASR1803
-
 /**
  *   \file dtmf_test.c
  *   \brief A Documented file.
@@ -95,13 +93,4 @@
 
     return 0;
 }
-#else
-#include <stdio.h>
 
-int main(int argc, char *argv[])
-{
-
-    return 0;
-}
-
-#endif
diff --git a/mbtk/test/asr1803/yx_audio_test.c b/mbtk/test/asr1803/yx_audio_test.c
index e72d68a..e01e9d9 100755
--- a/mbtk/test/asr1803/yx_audio_test.c
+++ b/mbtk/test/asr1803/yx_audio_test.c
@@ -8,8 +8,6 @@
 #include "ql/ql_audio.h"
 // #include "mopen_tts.h"
 
-#ifdef MBTK_PLATFORM_ASR1803
-
 #define MBTK_AUD_DEMO_WAV "/data/demo.wav"
 
 static int record_fd = 0;
@@ -365,12 +363,3 @@
 
     return 0;
 }
-#else
-
-int main(int argc, char *argv[])
-{
-
-    return 0;
-}
-
-#endif