Fix audio api.

Change-Id: I98ae49566665268e69d15b9e761d42546ed3878e
diff --git a/mbtk/include/mbtk/mbtk_audio_ubus.h b/mbtk/include/mbtk/mbtk_audio_ubus.h
new file mode 100755
index 0000000..cca443d
--- /dev/null
+++ b/mbtk/include/mbtk/mbtk_audio_ubus.h
@@ -0,0 +1,41 @@
+/*
+* mbtk_audio_ubus.h
+*
+* MBTK audio ubus client API header.
+*
+* Author : lb
+* Date   : 2024/3/18 16:04:33
+*/
+#ifndef _MBTK_AUDIO_UBUS_H
+#define _MBTK_AUDIO_UBUS_H
+
+
+int mbtk_audio_ubus_init();
+
+int mbtk_audio_ubus_deinit();
+
+/**
+* mode:
+*     AUDIO_MODE_INVALID = -2,
+*     AUDIO_MODE_CURRENT = -1,
+*     AUDIO_MODE_NORMAL = 0,
+*     AUDIO_MODE_RINGTONE = 1,
+*     AUDIO_MODE_IN_CALL = 2,
+*     AUDIO_MODE_IN_COMMUNICATION=3,
+*     AUDIO_MODE_IN_VT_CALL= 4,
+*     AUDIO_MODE_CNT,
+*     AUDIO_MODE_MAX = AUDIO_MODE_CNT-1
+ */
+int mbtk_audio_mode_set(int mode);
+
+/**
+ *    device: UINT32
+ *         0: earpiece
+ *         1: speaker
+ *         2: headset
+ */
+int mbtk_audio_loopback_start(int device);
+
+int mbtk_audio_loopback_stop();
+
+#endif /* _MBTK_AUDIO_UBUS_H */
diff --git a/mbtk/libmbtk_audio/Makefile b/mbtk/libmbtk_audio/Makefile
index de3f3ef..b3ede3a 100755
--- a/mbtk/libmbtk_audio/Makefile
+++ b/mbtk/libmbtk_audio/Makefile
@@ -33,7 +33,8 @@
 LOCAL_SRC_FILES = src/mbtk_audio.c \
 	src/mbtk_audio_gain.c \
 	src/mbtk_pcm_stream.c \
-	src/mbtk_wav.c
+	src/mbtk_wav.c \
+	src/mbtk_audio_ubus.c
 
 OBJS = $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(LOCAL_SRC_FILES)))
 $(info OBJS = $(OBJS))
diff --git a/mbtk/libmbtk_audio/src/mbtk_audio_ubus.c b/mbtk/libmbtk_audio/src/mbtk_audio_ubus.c
new file mode 100755
index 0000000..f676f02
--- /dev/null
+++ b/mbtk/libmbtk_audio/src/mbtk_audio_ubus.c
@@ -0,0 +1,207 @@
+/*
+*    mbtk_audio_ubus.c
+*
+*    MBTK audio ubus client API.
+*
+*/
+/******************************************************************************
+
+                          EDIT HISTORY FOR FILE
+
+  WHEN        WHO       WHAT,WHERE,WHY
+--------    --------    -------------------------------------------------------
+2024/3/18     LiuBin      Initial version
+
+******************************************************************************/
+#include <fcntl.h>
+#include <stdint.h>
+#include <limits.h>
+#include <termios.h>
+#include <stdarg.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <sys/statfs.h>
+#include <sys/types.h>
+#include "mbtk_log.h"
+#include "mbtk_type.h"
+#include <libubox/blobmsg_json.h>
+#include "libubus.h"
+#include "mbtk_audio_ubus.h"
+
+#define AUDIO_UBUS_TIMEOUT 5000     // 3s
+
+#define AUDIO_UBUS_REQUEST_NAME         "audio_if"
+#define AUDIO_UBUS_VOLUME_SET           "volume_set"
+#define AUDIO_UBUS_VOLUME_GET           "volume_status"
+#define AUDIO_UBUS_MODE_SET             "audio_mode_set"
+#define AUDIO_UBUS_PATH_ENABLE          "audio_path_enable"
+#define AUDIO_UBUS_SWITCH_PCM           "switch_pcm"
+#define AUDIO_UBUS_DSP_SET              "config_dspgain"
+#define AUDIO_UBUS_LOOPBACK_EN          "loopback_enable"
+#define AUDIO_UBUS_LOOPBACK_DIS         "loopback_disable"
+#define AUDIO_UBUS_AUDIO_GAIN_SET       "audio_gain_set"
+#define AUDIO_UBUS_AUDIO_REG_SET        "audio_reg_set"
+
+static struct ubus_context *audio_ctx = NULL;
+static uint32_t ubus_id_audio_if = 0;
+
+static void ubus_complete_cb(struct ubus_request *req, int rc)
+{
+    if(audio_ctx == NULL) {
+        LOGE("MBTK audio ubus not inited.");
+        return;
+    }
+    if (req)
+    {
+        free(req);
+        req = NULL;
+    }
+
+    LOGD("ubus_complete_cb() , rc = %d", rc);
+}
+
+int mbtk_audio_ubus_init()
+{
+    if(audio_ctx) {
+        LOGE("MBTK audio ubus has inited.");
+        return -1;
+    }
+
+    audio_ctx = ubus_connect(NULL);
+	if (!audio_ctx) {
+		LOGE("Failed to connect to ubus.");
+		return -1;
+	}
+
+	int ret = ubus_lookup_id(audio_ctx, AUDIO_UBUS_REQUEST_NAME, &ubus_id_audio_if);
+	if (ret) {
+        LOGE("ubus_lookup_id() fail.");
+		return ret;
+	}
+    return 0;
+}
+
+int mbtk_audio_ubus_deinit()
+{
+    if(audio_ctx) {
+        ubus_free(audio_ctx);
+        audio_ctx = NULL;
+        ubus_id_audio_if = 0;
+    }
+
+    return 0;
+}
+
+/**
+ * @brief      mbtk_audio_mode_set
+ *
+ * @details    detailed description
+ *
+ * @param      param
+ *             "param0": UINT32
+ *             typedef enum {
+ *                 AUDIO_MODE_INVALID = -2,
+ *                 AUDIO_MODE_CURRENT = -1,
+ *                 AUDIO_MODE_NORMAL = 0,
+ *                 AUDIO_MODE_RINGTONE = 1,
+ *                 AUDIO_MODE_IN_CALL = 2,
+ *                 AUDIO_MODE_IN_COMMUNICATION=3,
+ *                 AUDIO_MODE_IN_VT_CALL= 4,
+ *                 AUDIO_MODE_CNT,
+ *                 AUDIO_MODE_MAX = AUDIO_MODE_CNT-1,
+ *             } audio_mode_
+ * @return     return type
+ */
+int mbtk_audio_mode_set(int mode)
+{
+    if(audio_ctx == NULL || ubus_id_audio_if == 0) {
+        LOGE("MBTK audio ubus not inited.");
+        return -1;
+    }
+    static struct blob_buf b;
+    int ret;
+    blob_buf_init(&b, 0);
+    blobmsg_add_u32(&b, "param0", mode);
+    if((ret = ubus_invoke(audio_ctx, ubus_id_audio_if, AUDIO_UBUS_MODE_SET, b.head, NULL, NULL, AUDIO_UBUS_TIMEOUT)) != UBUS_STATUS_OK) {
+        LOGE("ubus_invoke fail:%d.\n", ret);
+        return -1;
+    } else {
+        LOGD("ubus_invoke success.\n");
+        return 0;
+    }
+}
+
+/**
+ * @brief      mbtk_audio_loopback_start
+ *
+ * @details    detailed description
+ *
+ * @param      param
+ *             device: UINT32
+ *             0: earpiece
+ *             1: speaker
+ *             2: headset
+ * @return     return type
+ */
+int mbtk_audio_loopback_start(int device)
+{
+    int rc = 0;
+    struct ubus_request *req = NULL;
+
+    if(audio_ctx == NULL || ubus_id_audio_if == 0) {
+        LOGE("MBTK audio ubus not inited.");
+        return -1;
+    }
+
+    req = (struct ubus_request *)malloc(sizeof(struct ubus_request));
+    if (req == NULL)
+    {
+        LOGE("leave %s: lack of memory", __FUNCTION__);
+        return -1;
+    }
+    static struct blob_buf b;
+    memset(req, 0, sizeof(struct ubus_request));
+    blob_buf_init(&b, 0);
+    blobmsg_add_u32(&b, "param0", device);
+    if ((rc = ubus_invoke_async(audio_ctx,
+                                ubus_id_audio_if,
+                                AUDIO_UBUS_LOOPBACK_EN,
+                                b.head, req)) != UBUS_STATUS_OK) {
+        free(req);
+        LOGE("ubus_invoke_async %s failed %s", AUDIO_UBUS_LOOPBACK_EN, ubus_strerror(rc));
+        return -1;
+    }
+    else
+    {
+        LOGD("ubus_invoke_async %s success", AUDIO_UBUS_LOOPBACK_EN);
+        req->complete_cb = ubus_complete_cb;
+        ubus_complete_request_async(audio_ctx, req);
+        return 0;
+    }
+}
+
+int mbtk_audio_loopback_stop()
+{
+    int rc = 0;
+
+    if(audio_ctx == NULL || ubus_id_audio_if == 0) {
+        LOGE("MBTK audio ubus not inited.");
+        return -1;
+    }
+    static struct blob_buf b;
+    blob_buf_init(&b, 0);
+    if ((rc = ubus_invoke(audio_ctx,
+                          ubus_id_audio_if,
+                          AUDIO_UBUS_LOOPBACK_DIS,
+                          b.head, NULL, NULL, AUDIO_UBUS_TIMEOUT)) != UBUS_STATUS_OK)
+    {
+        LOGE("ubus_invoke %s failed %s", AUDIO_UBUS_LOOPBACK_DIS, ubus_strerror(rc));
+        return -1;
+    }
+    else
+    {
+        LOGD("ubus_invoke %s success", AUDIO_UBUS_LOOPBACK_DIS);
+        return 0;
+    }
+}
+
diff --git a/mbtk/libmbtk_factory/Makefile b/mbtk/libmbtk_factory/Makefile
index 0da7069..c40b45a 100755
--- a/mbtk/libmbtk_factory/Makefile
+++ b/mbtk/libmbtk_factory/Makefile
@@ -7,7 +7,7 @@
 
 LIB_DIR +=
 
-LIBS += -llog -lmbtk_lib
+LIBS += -llog -lmbtk_lib -lmbtk_audio
 
 CFLAGS += -shared -Wl,-shared,-Bsymbolic
 
diff --git a/mbtk/libmbtk_factory/mbtk_audio.c b/mbtk/libmbtk_factory/mbtk_audio.c
new file mode 100755
index 0000000..fd0f640
--- /dev/null
+++ b/mbtk/libmbtk_factory/mbtk_audio.c
@@ -0,0 +1,77 @@
+#include "mbtk_log.h"
+#include "mbtk_audio2.h"
+#include "mbtk_audio_ubus.h"
+
+#define AUD_DEMO_WAV "/user_data/demo.wav"
+
+int mbtk_at_loopback(int type)
+{
+    LOGD("mbtk_at_loopback() : type - %d", type);
+    if(0 == type) // Stop
+    {
+        if(mbtk_audio_loopback_stop()) {
+            return -1;
+        }
+
+        mbtk_audio_ubus_deinit();
+    }
+    else // Start
+    {
+        mbtk_audio_ubus_init();
+
+        mbtk_audio_mode_set(0);
+        mbtk_audio_loopback_start(2);
+    }
+
+    return 0;
+}
+
+int mbtk_at_play(const char *args)
+{
+    int ret = 0;
+    if(mbtk_audio_wav_init()) {
+        LOGE("mbtk_audio_wav_init() fail.");
+        return -1;
+    }
+
+    if(mbtk_audio_wav_play_start(AUD_DEMO_WAV)) {
+        LOGE("mbtk_audio_wav_play_start() fail.");
+        ret = -1;
+        goto exit;
+    }
+
+    sleep(5);
+
+exit:
+    if(mbtk_audio_wav_deinit()) {
+        LOGE("mbtk_audio_wav_deinit() fail.");
+        return -1;
+    }
+
+    return ret;
+}
+
+int mbtk_at_rec(const char *args)
+{
+    int ret = 0;
+    if(mbtk_audio_wav_init()) {
+        LOGE("mbtk_audio_wav_init() fail.");
+        return -1;
+    }
+
+    if(mbtk_audio_wav_recorder_start(AUD_DEMO_WAV, MBTK_AUDIO_SAMPLE_RATE_8000)) {
+        LOGE("mbtk_audio_wav_recorder_start() fail.");
+        ret = -1;
+        goto exit;
+    }
+
+    sleep(5);
+
+exit:
+    if(mbtk_audio_wav_deinit()) {
+        LOGE("mbtk_audio_wav_deinit() fail.");
+        return -1;
+    }
+
+    return 0;
+}