解决YX Audio 卡死的问题

Change-Id: I3b26fd9f8b7bf0200ba1fcf41e4dcf9a46145b6e
diff --git a/mbtk/mbtk_lib/src/mbtk_adc.c b/mbtk/mbtk_lib/src/mbtk_adc.c
index dface9f..581bcba 100755
--- a/mbtk/mbtk_lib/src/mbtk_adc.c
+++ b/mbtk/mbtk_lib/src/mbtk_adc.c
@@ -33,7 +33,7 @@
     //system("echo 3 > /sys/kernel/debug/adc");
     if(!access(ADC_DEVICE_803, R_OK))
     {
-        LOGI("DEV:%s", ADC_DEVICE_803);
+        //LOGI("DEV:%s", ADC_DEVICE_803);
         fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
     }
     else
@@ -104,11 +104,11 @@
     }
 #else
     if(!access(ADC_DEVICE_802, R_OK)) {
-        LOGI("DEV:%s", ADC_DEVICE_802);
+        //LOGI("DEV:%s", ADC_DEVICE_802);
         fd = open(ADC_DEVICE_802, O_RDWR|O_CREAT|O_TRUNC, 0644);
     } else {
         if(!access(ADC_DEVICE_803, R_OK)) {
-            LOGI("DEV:%s", ADC_DEVICE_803);
+            //LOGI("DEV:%s", ADC_DEVICE_803);
             fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
         } else {
             LOGE("No found ADC devices.");
@@ -133,7 +133,7 @@
         close(fd);
         return -2;
     }else{
-        LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf);
+        //LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf);
         adc_value = strstr(adc_buf, "channel");
     }
     close(fd);
diff --git a/mbtk/mbtk_lib/src/mbtk_audio.c b/mbtk/mbtk_lib/src/mbtk_audio.c
index 382bef3..1858fa1 100755
--- a/mbtk/mbtk_lib/src/mbtk_audio.c
+++ b/mbtk/mbtk_lib/src/mbtk_audio.c
@@ -322,7 +322,7 @@
 void mbtk_audio_mode_set(int mode)
 {
     int rc = 0;
-
+#ifndef MBTK_YX_SUPPORT
     if(NULL == mbtk_audio_ubus_db)
     {
         printf("mbtk_dtmf_ubus not init!\n");
@@ -333,7 +333,7 @@
     if ((rc = ubus_invoke(mbtk_audio_ubus_db->ctx,
                                 mbtk_audio_ubus_db->audioif_request_id,
                                 AUDIO_UBUS_MODE_SET,
-                                audio_cm_b.head, NULL, NULL, 0)) != UBUS_STATUS_OK)
+                                audio_cm_b.head, NULL, NULL, 2 * 1000)) != UBUS_STATUS_OK)
     {
         printf("%s, ubus_invoke_async %s failed %s\n", __FUNCTION__, AUDIO_UBUS_MODE_SET, ubus_strerror(rc));
     }
@@ -341,13 +341,38 @@
     {
         printf("%s: ubus_invoke_async success\n", __FUNCTION__);
     }
+#else
+    static struct ubus_context *ctx;
+    ctx = ubus_connect(NULL);
+	if (!ctx) {
+		printf("Failed to connect to ubus\n");
+		return;
+	}
+
+    static struct blob_buf b;
+    uint32_t id;
+	int ret;
+    ret = ubus_lookup_id(ctx, AUDIO_UBUS_REQUEST_NAME, &id);
+	if (ret) {
+        printf("ubus_lookup_id() fail.\n");
+		return ret;
+	}
+
+    blob_buf_init(&b, 0);
+    blobmsg_add_u32(&b, "param0", mode);
+	if((ret = ubus_invoke(ctx, id, AUDIO_UBUS_MODE_SET, b.head, NULL, NULL, 0)) != UBUS_STATUS_OK) {
+        printf("ubus_invoke fail:%d.\n", ret);
+	} else {
+        printf("ubus_invoke success.\n");
+	}
+#endif
 }
 
 
 void mbtk_audio_path_enable(int mode)
 {
     int rc = 0;
-
+#ifndef MBTK_YX_SUPPORT
     if(NULL == mbtk_audio_ubus_db)
     {
         printf("mbtk_dtmf_ubus not init!\n");
@@ -367,6 +392,32 @@
     {
         printf("%s: ubus_invoke_async success\n", __FUNCTION__);
     }
+#else
+    static struct ubus_context *ctx;
+    ctx = ubus_connect(NULL);
+	if (!ctx) {
+		printf("Failed to connect to ubus\n");
+		return;
+	}
+
+    static struct blob_buf b;
+    uint32_t id;
+	int ret;
+    ret = ubus_lookup_id(ctx, AUDIO_UBUS_REQUEST_NAME, &id);
+	if (ret) {
+        printf("ubus_lookup_id() fail.\n");
+		return ret;
+	}
+
+    blob_buf_init(&b, 0);
+    blobmsg_add_u32(&b, "param0", mode);
+	if((ret = ubus_invoke(ctx, id, AUDIO_UBUS_PATH_ENABLE, b.head, NULL, NULL, 0)) != UBUS_STATUS_OK) {
+        printf("ubus_invoke fail:%d.\n", ret);
+	} else {
+        printf("ubus_invoke success.\n");
+	}
+
+#endif
 }
 
 
@@ -385,37 +436,53 @@
 void mbtk_audio_switch_pcm(int mode)
 {
     int rc = 0;
-    struct ubus_request *req = NULL;
-
+#ifndef MBTK_YX_SUPPORT
     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_async(mbtk_audio_ubus_db->ctx,
+    if ((rc = ubus_invoke(mbtk_audio_ubus_db->ctx,
                                 mbtk_audio_ubus_db->audioif_request_id,
                                 AUDIO_UBUS_SWITCH_PCM,
-                                audio_cm_b.head, req)) != UBUS_STATUS_OK)
+                                audio_cm_b.head, NULL, NULL, 0/*2 * 1000*/)) != UBUS_STATUS_OK)
     {
-        free(req);
-        printf("%s, ubus_invoke_async %s failed %s\n", __FUNCTION__, AUDIO_UBUS_MODE_SET, ubus_strerror(rc));
+        printf("%s, ubus_invoke %s failed %s\n", __FUNCTION__, AUDIO_UBUS_SWITCH_PCM, 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);
+        printf("%s: ubus_invoke success\n", __FUNCTION__);
     }
+#else
+
+    static struct ubus_context *ctx;
+    ctx = ubus_connect(NULL);
+	if (!ctx) {
+		printf("Failed to connect to ubus\n");
+		return;
+	}
+
+    static struct blob_buf b;
+    uint32_t id;
+	int ret;
+    ret = ubus_lookup_id(ctx, AUDIO_UBUS_REQUEST_NAME, &id);
+	if (ret) {
+        printf("ubus_lookup_id() fail.\n");
+		return ret;
+	}
+
+    blob_buf_init(&b, 0);
+    blobmsg_add_u32(&b, "param0", mode);
+	if((ret = ubus_invoke(ctx, id, AUDIO_UBUS_SWITCH_PCM, b.head, NULL, NULL, 0)) != UBUS_STATUS_OK) {
+        printf("ubus_invoke fail:%d.\n", ret);
+	} else {
+        printf("ubus_invoke success.\n");
+	}
+
+#endif
 }
 
 int mbtk_audio_dsp_set(int type, int gain)
@@ -826,7 +893,7 @@
 int mbtk_audio_ubus_client_init(mbtk_audio_client_handle_type *ph_audio, mbtk_dtmf_cb cb)
 {
     int id;
-
+#ifndef MBTK_YX_SUPPORT
     // Set call handle.
     if(ph_audio == NULL || mbtk_audio_ubus_db != NULL)
     {
@@ -849,13 +916,16 @@
 
     *ph_audio = mbtk_audio_ubus_db;
     LOGI("%s %d:%x", __FUNCTION__, __LINE__, mbtk_audio_ubus_db);
-
+#else
+    // Do nothong for YX.
+#endif
     return 0;
 }
 
 int mbtk_audio_ubus_client_deinit(mbtk_audio_client_handle_type h_audio)
 {
     int ret;
+#ifndef MBTK_YX_SUPPORT
     struct mbtk_audio_ubus_db_t *audio_ubus = (struct mbtk_audio_ubus_db_t *)h_audio;
 
     mbtk_audio_log("%s \n", __FUNCTION__);
@@ -889,7 +959,9 @@
     LOGI("%s %d", __FUNCTION__, __LINE__);
     free(h_audio);
     mbtk_audio_ubus_db = NULL;
-
+#else
+    // Do nothong for YX.
+#endif
     return 0;
 }
 
diff --git a/mbtk/mbtk_lib/src/mbtk_gpio.c b/mbtk/mbtk_lib/src/mbtk_gpio.c
index 9e562a9..18d216d 100755
--- a/mbtk/mbtk_lib/src/mbtk_gpio.c
+++ b/mbtk/mbtk_lib/src/mbtk_gpio.c
@@ -1061,7 +1061,7 @@
     }
     gpio_register_set_direction(port, 1);           //设方向为输出
     ret = gpio_register_get_value(port);
-    printf("gpio default value is : %d.\n", ret);
+    //printf("gpio default value is : %d.\n", ret);
     
     
     //[High]
@@ -1069,7 +1069,7 @@
         gpio_register_set_value(port, 1);
         usleep(50);
         valueh = gpio_register_get_value(port);
-        printf("set high? %d\n",valueh);
+        //printf("set high? %d\n",valueh);
         if(1 == valueh){
             break;
         }
@@ -1082,7 +1082,7 @@
             break;
         }
     }
-	printf("******gpio should is high: %d.******\n", ret);
+	//printf("******gpio should is high: %d.******\n", ret);
     if(1 != ret){
         ret=-1;
         goto exit;
@@ -1096,7 +1096,7 @@
         gpio_register_set_value(port, 0);               //输出低
         usleep(50);
         valuel = gpio_register_get_value(port);
-        printf("set low? %d\n",valuel);
+        //printf("set low? %d\n",valuel);
         if(0 == valuel){
             break;
         }
@@ -1109,7 +1109,7 @@
             break;
         }
     }
-    printf("******gpio should is low: %d.******\n", ret);
+    //printf("******gpio should is low: %d.******\n", ret);
     if(0 != ret){
         ret=-1;
         goto exit;
@@ -1144,7 +1144,7 @@
         gpio_register_set_value(port, 1);
         usleep(50);
         valueh = gpio_register_get_value(port);
-        printf("set high? %d\n",valueh);
+        //printf("set high? %d\n",valueh);
         if(1 == valueh){
             break;
         }
@@ -1161,7 +1161,7 @@
         ret=-1;
         goto exit;
     }
-    printf("******gpio should is high: %d.******\n", ret);
+    //printf("******gpio should is high: %d.******\n", ret);
 
 
     //[Low]
@@ -1170,7 +1170,7 @@
         gpio_register_set_value(port, 0);               //输出低
         usleep(50);
         valuel = gpio_register_get_value(port);
-        printf("set low? %d\n",valuel);
+        //printf("set low? %d\n",valuel);
         if(0 == valuel){
             break;
         }
@@ -1183,7 +1183,7 @@
             break;
         }
     }
-    printf("******gpio should is low: %d.******\n", ret);
+    //printf("******gpio should is low: %d.******\n", ret);
     if(0 != ret){
         ret=-1;
         goto exit;
@@ -1385,7 +1385,7 @@
             fail_io[n] = test_gpio_0[i];
             n++;
         }else{
-            printf("############gpio [%d] test success############\n", test_gpio_0[i]);
+            //printf("############gpio [%d] test success############\n", test_gpio_0[i]);
         }
     }
 	
@@ -1397,7 +1397,7 @@
             fail_io[n] = test_gpio_1[i];
             n++;
         }else{
-            printf("############gpio [%d] test success############\n", test_gpio_1[i]);
+            //printf("############gpio [%d] test success############\n", test_gpio_1[i]);
         }
     }
 
@@ -1405,7 +1405,7 @@
     for(i = 0; i < total_2; i=i+2){
         ret_2 = gpio_register_test_out_2(test_gpio_2[i], test_gpio_2[i+1], 0);
         if(-1 == ret_2){
-            printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
+            //printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio_2[i+1]);
             fail_io[n] = test_gpio_2[i];
             n++;
         }else{