From 67a372aae49d4aa183a0a442178286dc3a4e471f Mon Sep 17 00:00:00 2001
From: Jeff Belgum <belgum@google.com>
Date: Fri, 26 Apr 2019 01:48:37 +0000
Subject: [PATCH] [bt][host] Hardcode att bearer preferred mtu

BT-841 #done
TEST=1. manually check that Bluetooth cast setup works
     2. fx run-test bluetooth-tests -t bt-host-unittests

Change-Id: Ic5889243cbad7577598af2b16d7977576c45aa1e
---
 src/connectivity/bluetooth/core/bt-host/att/BUILD.gn     | 2 ++
 src/connectivity/bluetooth/core/bt-host/att/att.h        | 4 ++++
 src/connectivity/bluetooth/core/bt-host/att/bearer.cc    | 4 ++--
 .../bluetooth/core/bt-host/att/bearer_unittest.cc        | 4 ++++
 .../bluetooth/core/bt-host/gatt/server_unittest.cc       | 9 +++++----
 .../bluetooth/core/bt-host/hci/hci_constants.h           | 4 ++++
 6 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/connectivity/bluetooth/core/bt-host/att/BUILD.gn b/src/connectivity/bluetooth/core/bt-host/att/BUILD.gn
index 3715f76cb09..0f5100bd803 100644
--- a/src/connectivity/bluetooth/core/bt-host/att/BUILD.gn
+++ b/src/connectivity/bluetooth/core/bt-host/att/BUILD.gn
@@ -13,6 +13,8 @@ source_set("definitions") {
 
   public_deps = [
     "//src/connectivity/bluetooth/core/bt-host/common",
+    "//src/connectivity/bluetooth/core/bt-host/hci:definitions",
+    "//src/connectivity/bluetooth/core/bt-host/l2cap:definitions",
   ]
 }
 
diff --git a/src/connectivity/bluetooth/core/bt-host/att/att.h b/src/connectivity/bluetooth/core/bt-host/att/att.h
index ec9fbfe4867..46cd5e5d233 100644
--- a/src/connectivity/bluetooth/core/bt-host/att/att.h
+++ b/src/connectivity/bluetooth/core/bt-host/att/att.h
@@ -14,6 +14,8 @@
 
 #include "lib/zx/time.h"
 #include "src/connectivity/bluetooth/core/bt-host/common/uint128.h"
+#include "src/connectivity/bluetooth/core/bt-host/hci/hci_constants.h"
+#include "src/connectivity/bluetooth/core/bt-host/l2cap/l2cap.h"
 
 namespace bt {
 namespace att {
@@ -24,6 +26,8 @@ constexpr uint16_t kLEMinMTU = 23;
 // v5.0, Vol 3, Part G, 5.1.1
 constexpr uint16_t kBREDRMinMTU = 48;
 
+constexpr uint16_t kLEMaxMTU = hci::kMaxLEExtendedDataLength - sizeof(l2cap::BasicHeader);
+
 // The maximum length of an attribute value (v5.0, Vol 3, Part F, 3.2.9).
 constexpr size_t kMaxAttributeValueLength = 512;
 
diff --git a/src/connectivity/bluetooth/core/bt-host/att/bearer.cc b/src/connectivity/bluetooth/core/bt-host/att/bearer.cc
index 8f63e7e35d1..2ad921c23a0 100644
--- a/src/connectivity/bluetooth/core/bt-host/att/bearer.cc
+++ b/src/connectivity/bluetooth/core/bt-host/att/bearer.cc
@@ -277,8 +277,8 @@ Bearer::Bearer(fbl::RefPtr<l2cap::Channel> chan)
   }
 
   mtu_ = min_mtu();
-  preferred_mtu_ =
-      std::max(min_mtu(), std::min(chan_->tx_mtu(), chan_->rx_mtu()));
+  // TODO (BT-854): Dynamically configure preferred MTU value.
+  preferred_mtu_ = kLEMaxMTU;
 }
 
 Bearer::~Bearer() {
diff --git a/src/connectivity/bluetooth/core/bt-host/att/bearer_unittest.cc b/src/connectivity/bluetooth/core/bt-host/att/bearer_unittest.cc
index efc0011e689..05308930fac 100644
--- a/src/connectivity/bluetooth/core/bt-host/att/bearer_unittest.cc
+++ b/src/connectivity/bluetooth/core/bt-host/att/bearer_unittest.cc
@@ -66,6 +66,10 @@ TEST_F(ATT_BearerTest, CreateFailsToActivate) {
   EXPECT_FALSE(Bearer::Create(std::move(fake_chan)));
 }
 
+TEST_F(ATT_BearerTest, CreateUsesLEMaxMTUAsPreferredMTU) {
+  EXPECT_EQ(kLEMaxMTU, bearer()->preferred_mtu());
+}
+
 TEST_F(ATT_BearerTest, ShutDown) {
   ASSERT_TRUE(bearer()->is_open());
   ASSERT_FALSE(fake_att_chan()->link_error());
diff --git a/src/connectivity/bluetooth/core/bt-host/gatt/server_unittest.cc b/src/connectivity/bluetooth/core/bt-host/gatt/server_unittest.cc
index eee49b0fd14..142319b2343 100644
--- a/src/connectivity/bluetooth/core/bt-host/gatt/server_unittest.cc
+++ b/src/connectivity/bluetooth/core/bt-host/gatt/server_unittest.cc
@@ -7,6 +7,7 @@
 #include <fbl/macros.h>
 #include <lib/async/cpp/task.h>
 
+#include "src/connectivity/bluetooth/core/bt-host/att/att.h"
 #include "src/connectivity/bluetooth/core/bt-host/att/database.h"
 #include "src/connectivity/bluetooth/core/bt-host/common/test_helpers.h"
 #include "src/connectivity/bluetooth/core/bt-host/gatt/gatt_defs.h"
@@ -87,7 +88,7 @@ TEST_F(GATT_ServerTest, ExchangeMTURequestInvalidPDU) {
 }
 
 TEST_F(GATT_ServerTest, ExchangeMTURequestValueTooSmall) {
-  const uint16_t kServerMTU = l2cap::kDefaultMTU;
+  const uint16_t kServerMTU = att::kLEMaxMTU;
   constexpr uint16_t kClientMTU = 1;
 
   // clang-format off
@@ -98,7 +99,7 @@ TEST_F(GATT_ServerTest, ExchangeMTURequestValueTooSmall) {
 
   const auto kExpected = common::CreateStaticByteBuffer(
     0x03,       // opcode: exchange MTU response
-    0xA0, 0x02  // server rx mtu: |kServerMTU|
+    0xF7, 0x00  // server rx mtu: |kServerMTU|
   );
   // clang-format on
 
@@ -111,7 +112,7 @@ TEST_F(GATT_ServerTest, ExchangeMTURequestValueTooSmall) {
 }
 
 TEST_F(GATT_ServerTest, ExchangeMTURequest) {
-  constexpr uint16_t kServerMTU = l2cap::kDefaultMTU;
+  constexpr uint16_t kServerMTU = att::kLEMaxMTU;
   constexpr uint16_t kClientMTU = 0x64;
 
   // clang-format off
@@ -122,7 +123,7 @@ TEST_F(GATT_ServerTest, ExchangeMTURequest) {
 
   const auto kExpected = common::CreateStaticByteBuffer(
     0x03,       // opcode: exchange MTU response
-    0xA0, 0x02  // server rx mtu: |kServerMTU|
+    0xF7, 0x00  // server rx mtu: |kServerMTU|
   );
   // clang-format on
 
diff --git a/src/connectivity/bluetooth/core/bt-host/hci/hci_constants.h b/src/connectivity/bluetooth/core/bt-host/hci/hci_constants.h
index 041d65e985f..e2727b98b73 100644
--- a/src/connectivity/bluetooth/core/bt-host/hci/hci_constants.h
+++ b/src/connectivity/bluetooth/core/bt-host/hci/hci_constants.h
@@ -1147,6 +1147,10 @@ constexpr size_t kMaxLEAdvertisingDataLength = 0x1F;  // (31)
 // command packets.
 constexpr size_t kMaxLEExtendedAdvertisingDataLength = 251;
 
+// The maximum length of LE data packets when the LE Data Packet Length Extension
+// feature is supported. See v5.0, Vol 6, Part B, 4.5.10, Table 4.3.
+constexpr size_t kMaxLEExtendedDataLength = 251;
+
 // Maximum value of the Advertising SID subfield in the ADI field of the PDU
 constexpr uint8_t kLEAdvertsingSIDMax = 0xEF;
 
-- 
GitLab