Add audio gain set API.

Change-Id: I7dadc9310f1aa880a05dfe057621d753bdbb940c
diff --git a/mbtk/include/mbtk/mbtk_audio.h b/mbtk/include/mbtk/mbtk_audio.h
index 72baf0f..c2b9a28 100755
--- a/mbtk/include/mbtk/mbtk_audio.h
+++ b/mbtk/include/mbtk/mbtk_audio.h
@@ -202,6 +202,7 @@
 void mbtk_audio_ubus_volume_set(unsigned int volume);
 void mbtk_audio_ubus_volume_get(mbtk_volume_cb cb);
 int mbtk_audio_dsp_set(int type, int gain);
+void mbtk_audio_ubus_gain_set(uint8 *gain, int gain_num);
 
 /**
  * @brief      mbtk_audio_switch_pcm
diff --git a/mbtk/mbtk_lib/src/mbtk_audio.c b/mbtk/mbtk_lib/src/mbtk_audio.c
index 6e54fc9..ff58fe9 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio.c
@@ -42,6 +42,7 @@
 #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 DEBUG 1
 
@@ -890,6 +891,64 @@
     }
 }
 
+// TX : 0-10; RX : 11
+#define MBTK_AUDIO_GAIN_NUM 12
+void mbtk_audio_ubus_gain_set(uint8 *gain, int gain_num)
+{
+    int rc = 0;
+    struct ubus_request *req = NULL;
+
+    req = (struct ubus_request *)malloc(sizeof(struct ubus_request));
+    if (req == NULL)
+    {
+        printf("leave %s: lack of memory\n", __FUNCTION__);
+        return;
+    }
+    if(gain_num != MBTK_AUDIO_GAIN_NUM) {
+        printf("gain_num error.\n");
+        return;
+    }
+    memset(req, 0, sizeof(struct ubus_request));
+    blob_buf_init(&audio_cm_b, 0);
+
+    char name[20];
+    int i = 0;
+    // Set TX
+    for(; i < gain_num - 1; i++) {
+        memset(name, 0x0, 20);
+        sprintf(name, "volume_gain_%d", i);
+        blobmsg_add_u8(&audio_cm_b, name, gain[i]);
+    }
+
+    // Set RX
+    blobmsg_add_u8(&audio_cm_b, "volume_gain_rx", gain[i]);
+
+#if 1
+    if ((rc = ubus_invoke_async(mbtk_audio_ubus_db->ctx, mbtk_audio_ubus_db->audioif_request_id, AUDIO_UBUS_AUDIO_GAIN_SET, audio_cm_b.head, req)) != UBUS_STATUS_OK)
+    {
+        free(req);
+        printf("%s, ubus_invoke_async volume set failed: %s\n", __FUNCTION__,  ubus_strerror(rc));
+    }
+    else
+    {
+        printf("%s: ubus_invoke_async success\n", __FUNCTION__);
+        req->complete_cb = mbtk_ubus_complete_cb;
+        ubus_complete_request_async(mbtk_audio_ubus_db->ctx, req);
+    }
+#else
+    if ((rc = ubus_invoke(APP_ctx, APP_audio_request_id, AUDIO_UBUS_AUDIO_GAIN_SET, audio_cm_b.head, NULL, 0, 0)) != UBUS_STATUS_OK)
+    {
+        printf("%s, ubus_invoke_async volume set failed: %s\n", __FUNCTION__, ubus_strerror(rc));
+    }
+    else
+    {
+        printf("%s: ubus_invoke_async success\n", __FUNCTION__);
+    }
+    free(req);
+#endif
+}
+
+
 static void audio_volume_get_data_cb (struct ubus_request *req, int type, struct blob_attr *msg)
 {
     UNUSEDPARAM(req);
diff --git a/mbtk/test/Makefile b/mbtk/test/Makefile
index 3a10f89..c584f1d 100755
--- a/mbtk/test/Makefile
+++ b/mbtk/test/Makefile
@@ -3,13 +3,13 @@
 
 LOCAL_PATH=$(BUILD_ROOT)/test
 
-INC_DIR += 
-	
+INC_DIR +=
+
 LIB_DIR +=
 
-LIBS += -lmbtk_lib -lql_lib -llynq_lib -lmbtk_mqtt_lib -lpolarssl -laudio-apu -lcutils -ltinyalsa -lacm
+LIBS += -lmbtk_lib -lql_lib -llynq_lib -lmbtk_mqtt_lib -lpolarssl -laudio-apu -lcutils -ltinyalsa -lacm -lubus -lubox
 
-CFLAGS += 
+CFLAGS +=
 
 DEFINE +=
 
@@ -38,4 +38,3 @@
 	$(CC) $(CFLAGS) -o $(OUT_DIR)/bin/$@ $@.c $(DEFINE) $(INC_DIR) $(LIB_DIR) $(LIBS)
 
 clean:
-	
\ No newline at end of file
diff --git a/mbtk/test/ubus_cli_demo.c b/mbtk/test/ubus_cli_demo.c
new file mode 100755
index 0000000..0b689c9
--- /dev/null
+++ b/mbtk/test/ubus_cli_demo.c
@@ -0,0 +1,163 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libubox/blobmsg_json.h>
+#include "libubus.h"
+
+#include "audio_if_parameter.h"
+//#include <include/audio_if.h>
+//#include <include/audio_if_types.h>
+//#include <include/audio_if_ubus.h>
+
+#define LOG(fmt, args...) \
+    do{ \
+        printf("%s#%d: " fmt "\n", __FUNCTION__, __LINE__, ##args); \
+    } while(0)
+
+//User task
+static pthread_t        APP_MainLoopTask;
+
+/**********************************************************************\
+*   APP Global Variables for uBus
+\**********************************************************************/
+#define AUDIO_UBUS_ID            "audio_if"
+#define AUDIO_UBUS_AUDIO_GAIN_SET	"audio_gain_set"
+
+static struct ubus_context     *APP_ctx = NULL;
+static uint32_t  APP_audio_request_id;
+static struct blob_buf audio_cm_b;
+
+
+/**********************************************************************\
+*   Function:       APP_uBusInit
+*   Description:    init UBUS context
+*   Returns:        0 on success
+\**********************************************************************/
+static int APP_uBusInit(void)
+{
+    int rc = 0;
+
+    uloop_init();
+    APP_ctx = ubus_connect(NULL);
+    if (APP_ctx == NULL)
+    {
+        printf(" %s Failed to connect to ubus!\n", __FUNCTION__);
+        return -1;
+    }
+
+    ubus_add_uloop(APP_ctx);
+    // lookup audio_if until success
+    while(1)
+    {
+        rc = ubus_lookup_id(APP_ctx, AUDIO_UBUS_ID, & APP_audio_request_id);
+        if (0 != rc)
+        {
+            printf("%s, Failed to look up(%s), rc=%d\n", __FUNCTION__, AUDIO_UBUS_ID, rc);
+            usleep(100000); //100ms
+        }
+        else
+            break;
+    }
+    printf("%s, ubus_lookup_id(%s) OK\n", __FUNCTION__, AUDIO_UBUS_ID);
+    return 0;
+}
+
+static void  APP_Audio_GaniSet_cb(struct ubus_request *req, int rc)
+{
+    if (req)
+    {
+        free(req);
+        req = NULL;
+    }
+    printf("%s do nothing, rc=%d\n", __FUNCTION__, rc);
+}
+
+
+/**********************************************************************\
+*   Function:      APP_Audio_VolumeSet
+*   Description:   This function is called to send command into audio_if.
+*   unsigned int volume(0~100)
+*   Returns:        0 on success
+\**********************************************************************/
+static void  APP_Audio_GainSet(unsigned char *gain, int gain_num)
+{
+    int rc = 0;
+    struct ubus_request *req = NULL;
+
+    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);
+
+    char name[20];
+    int i = 0;
+    for(; i < gain_num; i++) {
+        memset(name, 0x0, 20);
+        sprintf(name, "volume_gain_%d", i);
+        blobmsg_add_u8(&audio_cm_b, name, gain[i]);
+    }
+
+#if 1
+    if ((rc = ubus_invoke_async(APP_ctx, APP_audio_request_id, AUDIO_UBUS_AUDIO_GAIN_SET, audio_cm_b.head, req)) != UBUS_STATUS_OK)
+    {
+        free(req);
+        printf("%s, ubus_invoke_async volume set failed: %s\n", __FUNCTION__,  ubus_strerror(rc));
+    }
+    else
+    {
+        printf("%s: ubus_invoke_async success\n", __FUNCTION__);
+        req->complete_cb = APP_Audio_GaniSet_cb;
+        ubus_complete_request_async(APP_ctx, req);
+    }
+#else
+    if ((rc = ubus_invoke(APP_ctx, APP_audio_request_id, AUDIO_UBUS_AUDIO_GAIN_SET, audio_cm_b.head, NULL, 0, 0)) != UBUS_STATUS_OK)
+    {
+        printf("%s, ubus_invoke_async volume set failed: %s\n", __FUNCTION__, ubus_strerror(rc));
+    }
+    else
+    {
+        printf("%s: ubus_invoke_async success\n", __FUNCTION__);
+    }
+    free(req);
+
+#endif
+}
+
+/**********************************************************************\
+*   Function:       main
+*   Description:    Main function of this APP.
+*   Returns:        0 on success
+\**********************************************************************/
+int main (int argc ,char *argv[])
+{
+    pthread_attr_t tattr;
+
+    /*Init ubus server*/
+    if(APP_uBusInit())
+        return -1;
+
+#if 0
+    /*Create thread to accept user choice*/
+    pthread_attr_init(&tattr);
+    pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
+    pthread_create(&APP_MainLoopTask, &tattr, (void *)APP_MainLoop, NULL);
+#else
+    unsigned char gain[] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0};
+    APP_Audio_GainSet(gain, sizeof(gain));
+
+#endif
+
+    uloop_run();
+
+    printf("Here, uloop stopped!!!\n ");
+
+    /*unregister uloop*/
+    /* thread will get here only if uloop stopped*/
+    ubus_free(APP_ctx);
+    uloop_done();
+    return 0;
+}
+