Incremental stack ext changes for HAL 2.0

- Enhanced create codec logic for TWS codec.
CRs-Fixed: 2425222
Change-Id: Ifac5c65052366327a8cd692f04a499596bf2d2a5
diff --git a/system_bt_ext/btif/include/btif_bat.h b/system_bt_ext/btif/include/btif_bat.h
index 094dcc1..29166dd 100644
--- a/system_bt_ext/btif/include/btif_bat.h
+++ b/system_bt_ext/btif/include/btif_bat.h
@@ -122,6 +122,7 @@
 void btif_ba_bta_callback(uint16_t event, uint8_t result);
 void ba_send_message(uint8_t event, uint8_t size, char* ptr, bool is_btif_thread);
 uint16_t btif_get_ba_latency();
+bool btif_ba_is_active();
 /*
 uint8_t get_curr_vol_level();
 uint8_t get_max_vol_level();
diff --git a/system_bt_ext/btif/src/btif_ba.cc b/system_bt_ext/btif/src/btif_ba.cc
index 04b6a90..c96fa8b 100644
--- a/system_bt_ext/btif/src/btif_ba.cc
+++ b/system_bt_ext/btif/src/btif_ba.cc
@@ -262,6 +262,15 @@
     return &batInterface;
 }
 
+bool btif_ba_is_active()
+{
+    LOG_INFO(LOG_TAG,"%s:",__func__);
+    if (btif_ba_get_state() > BTIF_BA_STATE_IDLE_AUDIO_NS)
+        return true;
+    else
+        return false;
+}
+
 btif_ba_state_t btif_ba_get_state()
 {
     if (btif_ba_cb.sm_handle == NULL) {
diff --git a/system_bt_ext/stack/a2dp/a2dp_vendor_aptx_tws.cc b/system_bt_ext/stack/a2dp/a2dp_vendor_aptx_tws.cc
index 56240e1..98a7950 100644
--- a/system_bt_ext/stack/a2dp/a2dp_vendor_aptx_tws.cc
+++ b/system_bt_ext/stack/a2dp/a2dp_vendor_aptx_tws.cc
@@ -459,16 +459,12 @@
 const char* A2DP_VendorCodecIndexStrAptxTWS(void) { return "aptX-TWS"; }
 
 bool A2DP_VendorInitCodecConfigAptxTWS(tAVDT_CFG* p_cfg) {
-  if (A2DP_GetOffloadStatus()) {
-    if (!A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
-                                    BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
-      LOG_ERROR(LOG_TAG, "%s: APTX-TWS disabled in offload mode", __func__);
-      return false;
-    }
-  } else {
-    LOG_ERROR(LOG_TAG, "%s: APTX-TWS is not supported in Non-Split mode", __func__);
+  if (!A2DP_IsCodecEnabled((btav_a2dp_codec_index_t)
+                                  BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
+    LOG_ERROR(LOG_TAG, "%s: APTX-TWS disabled in both SW and HW mode", __func__);
     return false;
   }
+
   if (A2DP_BuildInfoAptxTWS(AVDT_MEDIA_TYPE_AUDIO, &a2dp_aptx_tws_caps,
                            p_cfg->codec_info) != A2DP_SUCCESS) {
     return false;
@@ -490,7 +486,8 @@
     : A2dpCodecConfig((btav_a2dp_codec_index_t)BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS,
                       "aptX-TWS", codec_priority) {
   // Compute the local capability
-    if (A2DP_GetOffloadStatus()) {
+    if (A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
+                                  BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
       a2dp_aptx_tws_caps = a2dp_aptx_tws_offload_caps;
       a2dp_aptx_tws_default_config = a2dp_aptx_tws_default_offload_config;
     } else {
@@ -516,17 +513,13 @@
 bool A2dpCodecConfigAptxTWS::init() {
   if (!isValid()) return false;
 
-  if (A2DP_GetOffloadStatus()) {
-    if (A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
-                                   BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
-      LOG_ERROR(LOG_TAG, "%s: APTX-TWS enabled in offload mode", __func__);
-      return true;
-    } else {
-      LOG_ERROR(LOG_TAG, "%s: APTX-TWS disabled in offload mode", __func__);
-      return false;
-    }
-  } else {
-    LOG_ERROR(LOG_TAG,"APTX-TWS not enabled in non-split mode");
+  if (A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
+                                 BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
+    LOG_ERROR(LOG_TAG, "%s: APTX-Tws enabled in HW mode", __func__);
+    return true;
+  } else if(!A2DP_IsCodecEnabledInSoftware((btav_a2dp_codec_index_t)
+                                 BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)){
+    LOG_ERROR(LOG_TAG, "%s: APTX-Tws disabled in both SW and HW mode", __func__);
     return false;
   }
   return true;
diff --git a/system_bt_ext/stack/a2dp/a2dp_vendor_aptx_tws_encoder.cc b/system_bt_ext/stack/a2dp/a2dp_vendor_aptx_tws_encoder.cc
index d538f45..486c3e5 100644
--- a/system_bt_ext/stack/a2dp/a2dp_vendor_aptx_tws_encoder.cc
+++ b/system_bt_ext/stack/a2dp/a2dp_vendor_aptx_tws_encoder.cc
@@ -136,7 +136,8 @@
     A2dpCodecConfig* a2dp_codec_config,
     a2dp_source_read_callback_t read_callback,
     a2dp_source_enqueue_callback_t enqueue_callback) {
-  if (A2DP_GetOffloadStatus()) {
+  if (A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
+                                  BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
     LOG_INFO(LOG_TAG,"aptX-TWS is running in offload mode");
     return;
   }
@@ -158,7 +159,8 @@
   LOG_ERROR(LOG_TAG,"a2dp_vendor_aptx_tws_feeding_reset: Not supported in legacy mode");
   return;
 
-  if (A2DP_GetOffloadStatus()) {
+  if (A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
+                                  BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
     LOG_INFO(LOG_TAG,"a2dp_vendor_aptx_tws_feeding_reset: aptx-TWS is in offload mode");
     return;
   } else {
@@ -168,7 +170,8 @@
 }
 
 void a2dp_vendor_aptx_tws_feeding_flush(void) {
-  if (A2DP_GetOffloadStatus()) {
+  if (A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
+                                  BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
     LOG_INFO(LOG_TAG,"a2dp_vendor_aptx_tws_feeding_flush: aptx-TWS is in offload mode");
     return;
   } else {
@@ -178,7 +181,8 @@
 }
 
 period_ms_t a2dp_vendor_aptx_tws_get_encoder_interval_ms(void) {
-  if (A2DP_GetOffloadStatus()) {
+  if (A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
+                                  BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
     LOG_INFO(LOG_TAG,"a2dp_vendor_aptx_tws_get_encoder_interval_ms:"
              "aptx-TWS is in offload mode");
     return 0;
@@ -187,7 +191,8 @@
 }
 
 void a2dp_vendor_aptx_tws_send_frames(uint64_t timestamp_us) {
-  if (A2DP_GetOffloadStatus()) {
+  if (A2DP_IsCodecEnabledInOffload((btav_a2dp_codec_index_t)
+                                  BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_TWS)) {
     LOG_INFO(LOG_TAG,"a2dp_vendor_aptx_hd_send_frames: aptx-HD is in offload mode");
     return;
   }