From ff8ef5216b0dc6b32d21f94e41cb1a0b98d008f1 Mon Sep 17 00:00:00 2001
From: David Duarte <licorne@google.com>
Date: Fri, 8 Dec 2023 19:42:56 +0000
Subject: [PATCH] hearing_aid: Add a flag to augment l2cap flush threshold

Bug: 308684052
Bug: 315500864
Test: m com.android.btservices
Change-Id: I02ad13b4804399652f2a105a3dc34f7e3e86d4d6
---
 flags/Android.bp                      |  1 +
 flags/asha.aconfig                    |  8 ++++++++
 system/bta/hearing_aid/hearing_aid.cc | 10 ++++++++--
 3 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 flags/asha.aconfig

diff --git a/flags/Android.bp b/flags/Android.bp
index 5f65b563117..ecc849939f6 100644
--- a/flags/Android.bp
+++ b/flags/Android.bp
@@ -8,6 +8,7 @@ aconfig_declarations {
     package: "com.android.bluetooth.flags",
     srcs: [
         "a2dp.aconfig",
+        "asha.aconfig",
         "audio_routing.aconfig",
         "bta_dm.aconfig",
         "framework.aconfig",
diff --git a/flags/asha.aconfig b/flags/asha.aconfig
new file mode 100644
index 00000000000..083d4b41df2
--- /dev/null
+++ b/flags/asha.aconfig
@@ -0,0 +1,8 @@
+package: "com.android.bluetooth.flags"
+
+flag {
+    name: "higher_l2cap_flush_threshold"
+    namespace: "bluetooth"
+    description: "Use a higher threshold for flushing L2CAP packets"
+    bug: "315500864"
+}
diff --git a/system/bta/hearing_aid/hearing_aid.cc b/system/bta/hearing_aid/hearing_aid.cc
index b43799699fb..f6ca2980c9f 100644
--- a/system/bta/hearing_aid/hearing_aid.cc
+++ b/system/bta/hearing_aid/hearing_aid.cc
@@ -20,6 +20,7 @@
 
 #define LOG_TAG "bluetooth"
 
+#include <android_bluetooth_flags.h>
 #include <base/functional/bind.h>
 #include <base/functional/callback.h>
 #include <base/logging.h>
@@ -1410,6 +1411,11 @@ class HearingAidImpl : public HearingAid {
       }
     }
 
+    uint16_t l2cap_flush_threshold = 0;
+    if (IS_FLAG_ENABLED(higher_l2cap_flush_threshold)) {
+      l2cap_flush_threshold = 1;
+    }
+
     // TODO: monural, binarual check
 
     // divide encoded data into packets, add header, send.
@@ -1430,7 +1436,7 @@ class HearingAidImpl : public HearingAid {
 
       uint16_t cid = GAP_ConnGetL2CAPCid(left->gap_handle);
       uint16_t packets_in_chans = L2CA_FlushChannel(cid, L2CAP_FLUSH_CHANS_GET);
-      if (packets_in_chans) {
+      if (packets_in_chans > l2cap_flush_threshold) {
         // Compare the two sides LE CoC credit value to confirm need to drop or
         // skip audio packet.
         if (NeedToDropPacket(left, right) && IsBelowDropFrequency(time_point)) {
@@ -1464,7 +1470,7 @@ class HearingAidImpl : public HearingAid {
 
       uint16_t cid = GAP_ConnGetL2CAPCid(right->gap_handle);
       uint16_t packets_in_chans = L2CA_FlushChannel(cid, L2CAP_FLUSH_CHANS_GET);
-      if (packets_in_chans) {
+      if (packets_in_chans > l2cap_flush_threshold) {
         // Compare the two sides LE CoC credit value to confirm need to drop or
         // skip audio packet.
         if (NeedToDropPacket(right, left) && IsBelowDropFrequency(time_point)) {
-- 
GitLab