From ec263bbe8d095357e2d44036d925932d3eb8ea4a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 Oct 2017 15:16:30 +0700 Subject: [PATCH] implement configurable ATT MTU #81 --- cores/nRF5/linker/bluefruit52_s132_v500.ld | 16 ++++++++-- .../Bluefruit52Lib/src/BLECharacteristic.cpp | 8 ++--- .../src/BLEClientCharacteristic.cpp | 32 +++++++++++-------- .../src/BLEClientCharacteristic.h | 2 -- libraries/Bluefruit52Lib/src/BLEGap.cpp | 11 +++++-- libraries/Bluefruit52Lib/src/BLEGap.h | 9 ++---- libraries/Bluefruit52Lib/src/bluefruit.cpp | 17 ++++++---- libraries/Bluefruit52Lib/src/bluefruit.h | 31 ++++++++++++------ .../Bluefruit52Lib/src/services/BLEUart.cpp | 2 +- 9 files changed, 77 insertions(+), 51 deletions(-) diff --git a/cores/nRF5/linker/bluefruit52_s132_v500.ld b/cores/nRF5/linker/bluefruit52_s132_v500.ld index 157cc747..5dd28f6d 100755 --- a/cores/nRF5/linker/bluefruit52_s132_v500.ld +++ b/cores/nRF5/linker/bluefruit52_s132_v500.ld @@ -6,14 +6,26 @@ GROUP(-lgcc -lc -lnosys) MEMORY { FLASH (rx) : ORIGIN = 0x23000, LENGTH = 0x25000 /* Max 148 KB for dual banks */ - RAM (rwx) : ORIGIN = 0x20002000 + 0x800, LENGTH = 0xE000 - 0x800 /* SRAM required by S132 depend on * - Attribute Table Size 0x800 * - Vendor UUID count 20 * - Role concurrent connection central + peripherl + secure links - * - + * - Max ATT MTU is 247 + * - Event Len = 3, HVN queue = 3, Write CMD queue =3 */ + RAM (rwx) : ORIGIN = 0x20003000, LENGTH = 0xC000 + + /* Reference configuration + - MTU = 247, hvn = 7, wrcmd = 3 , evt len = 3 + ram_start = 0x20003310 + + - MTU = 247, hvn = 3, wrcmd = 3, evt len = 3 + ram_start = 0x20002F10 + + - MTU = 23, hvn = 7, wrcmd = 3, evt len = 3 + ram_start = 0x200025A0 + */ } SECTIONS diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index 6eb18dc2..695198ce 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -477,11 +477,7 @@ bool BLECharacteristic::notify(const void* data, uint16_t len) if ( notifyEnabled() ) { - // Break into multiple MTU-3 packet - // TODO Currently SD132 v2.0 MTU is fixed with max payload = 20 - // SD132 v3.0 could negotiate MTU to higher number - const uint16_t MTU_MPS = 20; - + uint16_t const max_payload = Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3; const uint8_t* u8data = (const uint8_t*) data; while ( remaining ) @@ -489,7 +485,7 @@ bool BLECharacteristic::notify(const void* data, uint16_t len) // TODO multiple connection support if ( !Bluefruit.Gap.getHvnPacket( Bluefruit.connHandle() ) ) return NRF_ERROR_RESOURCES; //BLE_ERROR_NO_TX_PACKETS; - uint16_t packet_len = min16(MTU_MPS, remaining); + uint16_t packet_len = min16(max_payload, remaining); ble_gatts_hvx_params_t hvx_params = { diff --git a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp index 14aff437..a69b9978 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp @@ -150,9 +150,11 @@ uint16_t BLEClientCharacteristic::read(void* buffer, uint16_t bufsize) { VERIFY( _chr.char_props.read, 0 ); + uint16_t const max_payload = Bluefruit.Gap.getMTU( _service->connHandle() ) - 3; + _adamsg.prepare(buffer, bufsize); VERIFY_STATUS( sd_ble_gattc_read(_service->connHandle(), _chr.handle_value, 0), 0); - int32_t rxlen = _adamsg.waitUntilComplete( (bufsize/(MTU_MPS-2)+1) * BLE_GENERIC_TIMEOUT ); + int32_t rxlen = _adamsg.waitUntilComplete( (bufsize/(max_payload-2) + 1) * BLE_GENERIC_TIMEOUT ); return (rxlen < 0) ? 0 : rxlen; } @@ -164,10 +166,9 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len) { VERIFY( _chr.char_props.write, 0 ); - // TODO Currently SD132 v2.0 MTU is fixed with max payload = 20 - // SD132 v3.0 could negotiate MTU to higher number - // For BLE_GATT_OP_PREP_WRITE_REQ, max data is 18 ( 2 byte is used for offset) - const bool long_write = (len > MTU_MPS); + uint16_t const max_payload = Bluefruit.Gap.getMTU( _service->connHandle() ) - 3; + + const bool long_write = (len > max_payload); int32_t count = 0; // CMD WRITE_REQUEST for single transaction @@ -192,19 +193,21 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len) else { /*------------- Long Write Sequence -------------*/ + // For BLE_GATT_OP_PREP_WRITE_REQ, 2 bytes are used for offset + ble_gattc_write_params_t param = { .write_op = BLE_GATT_OP_PREP_WRITE_REQ, .flags = 0, .handle = _chr.handle_value, .offset = 0, - .len = min16(len, MTU_MPS-2), + .len = min16(len, max_payload-2), .p_value = (uint8_t*) data }; _adamsg.prepare( (void*) data, len); VERIFY_STATUS ( sd_ble_gattc_write(_service->connHandle(), ¶m) ); - count = _adamsg.waitUntilComplete( (len/(MTU_MPS-2)+1) * BLE_GENERIC_TIMEOUT ); + count = _adamsg.waitUntilComplete( (len/(max_payload-2) + 1) * BLE_GENERIC_TIMEOUT ); // delay to swallow last WRITE RESPONSE // delay(20); @@ -217,18 +220,17 @@ uint16_t BLEClientCharacteristic::write(const void* data, uint16_t len) { // VERIFY( _chr.char_props.write_wo_resp, 0 ); - // Break into multiple MTU-3 packet - // TODO Currently SD132 v2.0 MTU is fixed with max payload = 20 - // SD132 v3.0 could negotiate MTU to higher number + uint16_t const max_payload = Bluefruit.Gap.getMTU( _service->connHandle() ) - 3; const uint8_t* u8data = (const uint8_t*) data; + // Break into multiple packet if needed uint16_t remaining = len; while( remaining ) { // TODO only Write without response consume a TX buffer if ( !Bluefruit.Gap.getWriteCmdPacket(_service->connHandle()) ) return NRF_ERROR_RESOURCES; //BLE_ERROR_NO_TX_PACKETS; - uint16_t packet_len = min16(MTU_MPS, remaining); + uint16_t packet_len = min16(max_payload, remaining); ble_gattc_write_params_t param = { @@ -353,7 +355,8 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt) _adamsg.complete(); }else if ( wr_rsp->write_op == BLE_GATT_OP_PREP_WRITE_REQ) { - uint16_t packet_len = min16(_adamsg.remaining, MTU_MPS-2); + uint16_t const max_payload = Bluefruit.Gap.getMTU( _service->connHandle() ) - 3; + uint16_t packet_len = min16(_adamsg.remaining, max_payload-2); // still has data, continue to prepare if ( packet_len ) @@ -400,6 +403,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt) case BLE_GATTC_EVT_READ_RSP: { + uint16_t const max_payload = Bluefruit.Gap.getMTU( _service->connHandle() ) - 3; ble_gattc_evt_read_rsp_t* rd_rsp = (ble_gattc_evt_read_rsp_t*) &evt->evt.gattc_evt.params.read_rsp; // Give up if failed (BLE_GATT_STATUS_ATTERR_INVALID_OFFSET usually) @@ -413,11 +417,11 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt) /* Complete condition is one of follows * - Running out of buffer - * - Receive data less than MPS - 1 (19) + * - Receive data less than MPS - 1 * - Couldn't perform GATTC Read */ if (( _adamsg.remaining == 0) || - (rd_rsp->len < (MTU_MPS-1) ) || + (rd_rsp->len < (max_payload-1) ) || (ERROR_NONE != sd_ble_gattc_read(_service->connHandle(), _chr.handle_value, _adamsg.xferlen)) ) { _adamsg.complete(); diff --git a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h index 19a6b131..603ff27b 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.h @@ -87,8 +87,6 @@ class BLEClientCharacteristic void useAdaCallback(bool enabled); private: - enum { MTU_MPS = 20 } ; - ble_gattc_char_t _chr; uint16_t _cccd_handle; diff --git a/libraries/Bluefruit52Lib/src/BLEGap.cpp b/libraries/Bluefruit52Lib/src/BLEGap.cpp index 53d97ec2..66e2a0ec 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGap.cpp @@ -124,7 +124,7 @@ bool BLEGap::getWriteCmdPacket(uint16_t conn_handle) uint16_t BLEGap::getMTU (uint16_t conn_handle) { - return BLE_GATT_ATT_MTU_DEFAULT; + return _peers[conn_handle].att_mtu; } uint16_t BLEGap::getPeerName(uint16_t conn_handle, char* buf, uint16_t bufsize) @@ -152,6 +152,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt) peer->connected = true; peer->role = para->role; peer->addr = para->peer_addr; + peer->att_mtu = BLE_GATT_ATT_MTU_DEFAULT; // Init transmission buffer for notification #if SD_VER < 500 @@ -214,8 +215,12 @@ void BLEGap::_eventHandler(ble_evt_t* evt) break; case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: - // TODO MTU exchange - VERIFY_STATUS( sd_ble_gatts_exchange_mtu_reply(conn_handle, BLE_GATT_ATT_MTU_DEFAULT), ); + { + peer->att_mtu = minof(evt->evt.gatts_evt.params.exchange_mtu_request.client_rx_mtu, BLEGATT_ATT_MTU_MAX); + VERIFY_STATUS( sd_ble_gatts_exchange_mtu_reply(conn_handle, peer->att_mtu), ); + + LOG_LV1(BLE, "ATT MTU is changed to %d", peer->att_mtu); + } break; #endif diff --git a/libraries/Bluefruit52Lib/src/BLEGap.h b/libraries/Bluefruit52Lib/src/BLEGap.h index 9af66169..4c763eb4 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.h +++ b/libraries/Bluefruit52Lib/src/BLEGap.h @@ -40,10 +40,6 @@ #include "bluefruit_common.h" #include "BLEUuid.h" -#define BLEGAP_EVENT_LENGTH BLE_GAP_EVENT_LENGTH_DEFAULT // BLE_GAP_EVENT_LENGTH_DEFAULT (3) -#define BLEGAP_HVN_TX_QUEUE_SIZE 7 // BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT (1) -#define BLEGAP_WRITECMD_TX_QUEUE_SIZE 3 // BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT (1) - class BLEGap { public: @@ -80,8 +76,9 @@ class BLEGap // Array of TX Packet semaphore, indexed by connection handle // Peer info where conn_handle serves as index typedef struct { - bool connected; - uint8_t role; + bool connected; + uint8_t role; + uint16_t att_mtu; ble_gap_addr_t addr; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 0ce7d928..3720377a 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -217,7 +217,7 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable) // TODO ATT MTU varclr(&blecfg); blecfg.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_HIGH_BANDWIDTH; - blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = BLE_GATT_ATT_MTU_DEFAULT; + blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = BLEGATT_ATT_MTU_MAX; VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATT, &blecfg, ram_start) ); // Event Length + HVN queue + WRITE CMD queue setting affecting bandwidth @@ -240,14 +240,17 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable) VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTC, &blecfg, ram_start) ); // Enable BLE stack - // TODO update linker SRAM usage // The memory requirement for a specific configuration will not increase // between SoftDevices with the same major version number - PRINT_HEX(ram_start); - uint32_t err = sd_ble_enable(&ram_start); - PRINT_HEX(ram_start); + if ( err ) + { + LOG_LV1(CFG, "SoftDevice config require more SRAM than provided by linker.\n" + "App Ram Start must be at least 0x%08X (provided 0x%08X)\n" + "Please update linker file or re-config SoftDevice", ram_start, (uint32_t) __data_start__); + } + PRINT_HEX(ram_start); VERIFY_STATUS(err); #endif @@ -597,7 +600,7 @@ void adafruit_ble_task(void* arg) #if SD_VER < 500 __ALIGN(4) uint8_t ev_buf[ sizeof(ble_evt_t) + (BLE_GATT_ATT_MTU_DEFAULT) ]; #else - __ALIGN(4) uint8_t ev_buf[ BLE_EVT_LEN_MAX(BLE_GATT_ATT_MTU_DEFAULT) ]; + __ALIGN(4) uint8_t ev_buf[ BLE_EVT_LEN_MAX(BLEGATT_ATT_MTU_MAX) ]; #endif uint16_t ev_len = sizeof(ev_buf); @@ -624,7 +627,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) // conn handle has fixed offset regardless of event type const uint16_t evt_conn_hdl = evt->evt.common_evt.conn_handle; - LOG_LV2(BLE, "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), evt_conn_hdl); + LOG_LV1(BLE, "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), evt_conn_hdl); // GAP handler Gap._eventHandler(evt); diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 3e163c71..18245ec7 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -42,15 +42,28 @@ #define CFG_ADV_BLINKY_INTERVAL 500 #define CFG_MAX_DEVNAME_LEN 32 -// Note changing these parameters will affect APP_RAM_BASE -// --> need to update RAM in linker file -#define BLE_GATTS_ATTR_TABLE_SIZE 0x800 //0x0B00 default is 0x580 -#define BLE_VENDOR_UUID_MAX 20 -#define BLE_PRPH_MAX_CONN 1 -#define BLE_CENTRAL_MAX_CONN 4 -#define BLE_CENTRAL_MAX_SECURE_CONN 1 // should be enough +/* Note changing these parameters will affect APP_RAM_BASE + * --> need to update RAM region in linker file + * + * default value are + * - BLE_GATTS_ATTR_TABLE_SIZE 0x580 + * - BLE_GAP_EVENT_LENGTH_DEFAULT = 3 + * - BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT = 1 + * - BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT = 1 + * - BLEGATT_ATT_MTU_MAX from 23 (default) to 247 + */ +#define BLE_GATTS_ATTR_TABLE_SIZE 0x800 //0x0B00 default is 0x580 +#define BLE_VENDOR_UUID_MAX 20 +#define BLEGAP_EVENT_LENGTH BLE_GAP_EVENT_LENGTH_DEFAULT +#define BLEGAP_HVN_TX_QUEUE_SIZE 3 +#define BLEGAP_WRITECMD_TX_QUEUE_SIZE 3 +#define BLEGATT_ATT_MTU_MAX 247 -#define BLE_MAX_CONN (BLE_CENTRAL_MAX_CONN+BLE_PRPH_MAX_CONN) +#define BLE_PRPH_MAX_CONN 1 +#define BLE_CENTRAL_MAX_CONN 4 +#define BLE_CENTRAL_MAX_SECURE_CONN 1 // should be enough + +#define BLE_MAX_CONN (BLE_CENTRAL_MAX_CONN+BLE_PRPH_MAX_CONN) #include "BLEUuid.h" #include "BLEAdvertising.h" @@ -83,8 +96,6 @@ #include "utility/AdaCallback.h" -#define BLE_MAX_DATA_PER_MTU (BLE_GATT_ATT_MTU_DEFAULT - 3) - enum { BLE_CONN_CFG_HIGH_BANDWIDTH = 1 diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp index 5397ada3..30aa8ac6 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp @@ -37,6 +37,7 @@ #include "bluefruit.h" #include "utility/TimeoutTimer.h" +// TODO larger ATT MTU #define TXD_FIFO_SIZE (BLE_GATT_ATT_MTU_DEFAULT-3) /* UART Serivce: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E @@ -181,7 +182,6 @@ err_t BLEUart::begin(void) // Add TXD Characteristic _txd.setProperties(CHR_PROPS_NOTIFY); -// _txd.setMaxLen(BLE_MAX_DATA_PER_MTU); // TODO enable encryption when bonding is enabled _txd.setPermission(SECMODE_OPEN, SECMODE_NO_ACCESS); _txd.setStringDescriptor("TXD");