Merge "Fix the issue where resources may not be released correctly ,memory leak or other undefined behavio" into mbtk_source_v2
diff --git a/mbtk/include/mbtk/mbtk_audio2.h b/mbtk/include/mbtk/mbtk_audio2.h
index cadc475..d84d1ea 100755
--- a/mbtk/include/mbtk/mbtk_audio2.h
+++ b/mbtk/include/mbtk/mbtk_audio2.h
@@ -19,6 +19,8 @@
 
 typedef void (*mbtk_audio_volume_set_func)();
 
+typedef void (*mbtk_audio_service_error_cb_f)(int error);
+
 typedef enum {
     MBTK_AUDIO_DIRECTION_OUTPUT = 0,    // Play
     MBTK_AUDIO_DIRECTION_INPUT         // Recorder
@@ -111,7 +113,20 @@
 
 int mbtk_audio_close_new(void *dev_hdl);
 
+int mbtk_register_error_callback(mbtk_audio_service_error_cb_f cb);
+int mbtk_audio_playback_set_block_flag(mbtk_audio_handle handle, int flags);
 
+/**
+ *    device: UINT32
+ *         0: earpiece
+ *         1: speaker
+ *         2: headset
+ *    enable_state: UINT32
+ *         0: close
+ *         1: open
+ */
+int mbtk_audio_set_loopback_enable_state(int device, int enable_state);
+int mbtk_audio_get_loopback_enable_state(int *device, int *enable_state);
 
 
 #endif /* _MBTK_AUDIO2_H */
diff --git a/mbtk/include/mbtk/mbtk_fota.h b/mbtk/include/mbtk/mbtk_fota.h
index bf166d8..3fe224d 100755
--- a/mbtk/include/mbtk/mbtk_fota.h
+++ b/mbtk/include/mbtk/mbtk_fota.h
@@ -35,6 +35,12 @@
     QUEC_FOTA_FAILED        /*failed*/

 }QL_FOTA_STATUS;

 

+typedef enum mbtkabsystem
+{
+    mbtk_sys_A = 0,

+    mbtk_sys_B = 1

+} mbtk_absystem_t;

+

 /*callback function define, used to get upgrade state and rate of process*/

 typedef int(*fota_callback)(int state, int percent);

 

@@ -96,6 +102,10 @@
 int mbtk_fota_status(void);

 

 int mbtk_fota_get_asr_reboot_cnt_flag(void);

+int mbtk_fota_get_active_absys_type(void);

+int mbtk_fota_get_tmp_absys_type(void);

+int mbtk_fota_get_sync_absys_type(void);

+int mbtk_fota_get_mtd_check_type(void);

 

 #ifdef __cplusplus

 }

diff --git a/mbtk/libmbtk_lib/audio/mbtk_pcm_stream.c b/mbtk/libmbtk_lib/audio/mbtk_pcm_stream.c
index e178b56..213534b 100755
--- a/mbtk/libmbtk_lib/audio/mbtk_pcm_stream.c
+++ b/mbtk/libmbtk_lib/audio/mbtk_pcm_stream.c
@@ -755,6 +755,23 @@
     return NULL;
 }
 
+int mbtk_audio_playback_set_block_flag(mbtk_audio_handle handle, int flags)
+{
+    if (handle == NULL) {
+        LOGE("Invalid handle: NULL");
+        return -1;
+    }
+
+    if (flags < 0) {
+        LOGE("Invalid flags: %d", flags);
+        return -1;
+    }
+
+    LOGD("Setting block flag: %d", flags);
+
+    return 0;
+}
+
 int mbtk_audio_play_file_new(void *dev_hdl, int file_fd, int offset)
 {
     unsigned bufsize = 0;
diff --git a/mbtk/libmbtk_lib/audio/mbtk_wav.c b/mbtk/libmbtk_lib/audio/mbtk_wav.c
index f2e104c..ac4bed8 100755
--- a/mbtk/libmbtk_lib/audio/mbtk_wav.c
+++ b/mbtk/libmbtk_lib/audio/mbtk_wav.c
@@ -26,6 +26,10 @@
 
 static audio_buff_t audio_buff;
 
+static int current_loopback_state = 0;
+static int current_loopback_device = -1;
+static mbtk_audio_service_error_cb_f service_error_cb = NULL;
+
 static void audio_play_thread(void *arg)
 {
     int rc, len, frames = 0;
@@ -125,6 +129,17 @@
     }
 }
 
+int mbtk_register_error_callback(mbtk_audio_service_error_cb_f cb)
+{
+    if (cb == NULL) {
+        LOGE("Error: Callback function is NULL.");
+        return -1;
+    }
+    service_error_cb = cb;
+    LOGD("Callback function registered successfully.");
+    return 0;
+}
+
 int mbtk_audio_wav_init()
 {
     //mbtk_log_init("radio", "MBTK_AUDIO");
@@ -407,3 +422,49 @@
     return mbtk_audio_pcm_deinit();
 }
 
+int mbtk_audio_set_loopback_enable_state(int device, int enable_state)
+{
+    char command[128];
+
+    if (device < 0 || device > 2 || (enable_state != 0 && enable_state != 1)) {
+        LOGE("Invalid device or enable_state");
+        return -1;
+    }
+
+    snprintf(command, sizeof(command), "ubus call audio_if audio_mode_set '{\"param0\":0}'");
+    if (system(command) != 0) {
+        LOGE("Failed to set audio mode");
+        return -1;
+    }
+
+    if (enable_state == 1) {
+        snprintf(command, sizeof(command), "ubus call audio_if loopback_enable '{\"param0\":%d}'", device);
+        if (system(command) != 0) {
+            LOGE("Failed to enable loopback");
+            return -1;
+        }
+    } else {
+        if (system("ubus call audio_if loopback_disable") != 0) {
+            LOGE("Failed to disable loopback");
+            return -1;
+        }
+    }
+
+    current_loopback_device = device;
+    current_loopback_state = enable_state;
+
+    return 0;
+}
+
+int mbtk_audio_get_loopback_enable_state(int *device, int *enable_state)
+{
+    if (device == NULL || enable_state == NULL) {
+        LOGE("Null pointer provided for device or enable_state");
+        return -1;
+    }
+
+    *device = current_loopback_device;
+    *enable_state = current_loopback_state;
+
+    return 0;
+}
diff --git a/mbtk/libmbtk_lib/fota/mbtk_fota.c b/mbtk/libmbtk_lib/fota/mbtk_fota.c
index 68e2ba0..323ec9b 100755
--- a/mbtk/libmbtk_lib/fota/mbtk_fota.c
+++ b/mbtk/libmbtk_lib/fota/mbtk_fota.c
@@ -8,10 +8,20 @@
 #include <pthread.h>

 #include <libubox/blobmsg_json.h>

 #include "libubus.h"

-#include "mbtk_fota.h"

+

 #include <semaphore.h>

 #include <cutils/properties.h>

+#include <libubox/blob.h>

+#include <libubox/uloop.h>

+#include <libubox/usock.h>

+#include <libubox/list.h>

+#include <libubus.h>

+#include <uci.h>

 

+#include <sys/ioctl.h>

+#include <mtd/mtd-user.h>

+

+#include "mbtk_fota.h"

 #include "mbtk_log.h"

 

 

@@ -425,3 +435,73 @@
     MBTK_SOURCE_INFO_PRINT("mbtk_fota_lib");

 }

 

+int mbtk_fota_get_active_absys_type(void)

+{

+    int type = 0;

+    char tmp_type[] ={0};

+

+    property_get("persist.mbtk.absys_active", tmp_type, "0");

+    type = atoi(tmp_type);

+    if (type == 97)

+    {

+        type = mbtk_sys_A;

+    }

+    else if (type == 98)

+    {

+        type = mbtk_sys_B;

+    }

+    else

+    {

+        LOGE("get_active_absys_type %s fail",tmp_type);

+        type = -1;

+    }

+

+    return type;

+}

+

+int mbtk_fota_get_tmp_absys_type(void)

+{

+    int type = 0;

+    char tmp_type[] ={0};

+

+    property_get("persist.mbtk.absys_tmp", tmp_type, "0");

+    type = atoi(tmp_type);

+    if (type == 97)

+    {

+        type = mbtk_sys_A;

+    }

+    else if (type == 98)

+    {

+        type = mbtk_sys_B;

+    }

+    else

+    {

+        LOGE("get_tmp_absys_type %s fail",tmp_type);

+        type = -1;

+    }

+

+    return type;

+}

+

+int mbtk_fota_get_sync_absys_type(void)

+{

+    int type = 0;

+    char tmp_type[] ={0};

+

+    property_get("persist.mbtk.absys_sync", tmp_type, "0");

+    type = atoi(tmp_type);

+

+    return type;

+}

+

+int mbtk_fota_get_mtd_check_type(void)

+{

+    int type = 0;

+    char tmp_type[] ={0};

+

+    property_get("persist.mbtk.mtd_check", tmp_type, "0");

+    type = atoi(tmp_type);

+

+    return type;

+}

+