From c6cb6ae810d8ec540f330c37447f53a1fbd58dac Mon Sep 17 00:00:00 2001 From: tonyfreeman <24860049+tonyfreeman@users.noreply.github.com> Date: Wed, 8 Dec 2021 16:59:40 +0300 Subject: [PATCH] BLE: Increase mtu (#837) * Incease mtu & buffer size * bt: process mtu size in RPC callbacks Co-authored-by: Tony Freeman Co-authored-by: gornekich --- applications/bt/bt_service/bt.c | 6 ++-- firmware/targets/f6/ble-glue/app_conf.h | 2 +- firmware/targets/f6/ble-glue/serial_service.c | 28 ++++++++++++++----- firmware/targets/f6/ble-glue/serial_service.h | 4 +-- firmware/targets/f7/ble-glue/app_conf.h | 2 +- firmware/targets/f7/ble-glue/serial_service.c | 28 ++++++++++++++----- firmware/targets/f7/ble-glue/serial_service.h | 5 ++-- 7 files changed, 52 insertions(+), 23 deletions(-) diff --git a/applications/bt/bt_service/bt.c b/applications/bt/bt_service/bt.c index 074ab980..97cc156b 100755 --- a/applications/bt/bt_service/bt.c +++ b/applications/bt/bt_service/bt.c @@ -132,9 +132,9 @@ static void bt_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t byt size_t bytes_sent = 0; while(bytes_sent < bytes_len) { size_t bytes_remain = bytes_len - bytes_sent; - if(bytes_remain > FURI_HAL_BT_SERIAL_PACKET_SIZE_MAX) { - furi_hal_bt_serial_tx(&bytes[bytes_sent], FURI_HAL_BT_SERIAL_PACKET_SIZE_MAX); - bytes_sent += FURI_HAL_BT_SERIAL_PACKET_SIZE_MAX; + if(bytes_remain > bt->max_packet_size) { + furi_hal_bt_serial_tx(&bytes[bytes_sent], bt->max_packet_size); + bytes_sent += bt->max_packet_size; } else { furi_hal_bt_serial_tx(&bytes[bytes_sent], bytes_remain); bytes_sent += bytes_remain; diff --git a/firmware/targets/f6/ble-glue/app_conf.h b/firmware/targets/f6/ble-glue/app_conf.h index eebdbbdb..059e8e4f 100644 --- a/firmware/targets/f6/ble-glue/app_conf.h +++ b/firmware/targets/f6/ble-glue/app_conf.h @@ -139,7 +139,7 @@ /** * Maximum supported ATT_MTU size */ -#define CFG_BLE_MAX_ATT_MTU (251) +#define CFG_BLE_MAX_ATT_MTU (489) /** * Size of the storage area for Attribute values diff --git a/firmware/targets/f6/ble-glue/serial_service.c b/firmware/targets/f6/ble-glue/serial_service.c index 4c70ccb0..ab201546 100644 --- a/firmware/targets/f6/ble-glue/serial_service.c +++ b/firmware/targets/f6/ble-glue/serial_service.c @@ -184,17 +184,31 @@ bool serial_svc_is_started() { return serial_svc != NULL; } -bool serial_svc_update_tx(uint8_t* data, uint8_t data_len) { +bool serial_svc_update_tx(uint8_t* data, uint16_t data_len) { if(data_len > SERIAL_SVC_DATA_LEN_MAX) { return false; } - tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle, - serial_svc->tx_char_handle, + + for(uint16_t remained = data_len; remained > 0;) { + uint8_t value_len = MIN(SERIAL_SVC_CHAR_VALUE_LEN_MAX, remained); + uint16_t value_offset = data_len - remained; + remained -= value_len; + + tBleStatus result = aci_gatt_update_char_value_ext( 0, + serial_svc->svc_handle, + serial_svc->tx_char_handle, + remained ? 0x00 : 0x02, data_len, - data); - if(result) { - FURI_LOG_E(TAG, "Failed updating TX characteristic: %d", result); + value_offset, + value_len, + data + value_offset); + + if(result) { + FURI_LOG_E(TAG, "Failed updating TX characteristic: %d", result); + return false; + } } - return result != BLE_STATUS_SUCCESS; + + return true; } diff --git a/firmware/targets/f6/ble-glue/serial_service.h b/firmware/targets/f6/ble-glue/serial_service.h index 3ea548af..1c365c0e 100644 --- a/firmware/targets/f6/ble-glue/serial_service.h +++ b/firmware/targets/f6/ble-glue/serial_service.h @@ -3,7 +3,7 @@ #include #include -#define SERIAL_SVC_DATA_LEN_MAX (248) +#define SERIAL_SVC_DATA_LEN_MAX (486) #ifdef __cplusplus extern "C" { @@ -36,7 +36,7 @@ void serial_svc_stop(); bool serial_svc_is_started(); -bool serial_svc_update_tx(uint8_t* data, uint8_t data_len); +bool serial_svc_update_tx(uint8_t* data, uint16_t data_len); #ifdef __cplusplus } diff --git a/firmware/targets/f7/ble-glue/app_conf.h b/firmware/targets/f7/ble-glue/app_conf.h index eebdbbdb..059e8e4f 100644 --- a/firmware/targets/f7/ble-glue/app_conf.h +++ b/firmware/targets/f7/ble-glue/app_conf.h @@ -139,7 +139,7 @@ /** * Maximum supported ATT_MTU size */ -#define CFG_BLE_MAX_ATT_MTU (251) +#define CFG_BLE_MAX_ATT_MTU (489) /** * Size of the storage area for Attribute values diff --git a/firmware/targets/f7/ble-glue/serial_service.c b/firmware/targets/f7/ble-glue/serial_service.c index 4c70ccb0..ab201546 100644 --- a/firmware/targets/f7/ble-glue/serial_service.c +++ b/firmware/targets/f7/ble-glue/serial_service.c @@ -184,17 +184,31 @@ bool serial_svc_is_started() { return serial_svc != NULL; } -bool serial_svc_update_tx(uint8_t* data, uint8_t data_len) { +bool serial_svc_update_tx(uint8_t* data, uint16_t data_len) { if(data_len > SERIAL_SVC_DATA_LEN_MAX) { return false; } - tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle, - serial_svc->tx_char_handle, + + for(uint16_t remained = data_len; remained > 0;) { + uint8_t value_len = MIN(SERIAL_SVC_CHAR_VALUE_LEN_MAX, remained); + uint16_t value_offset = data_len - remained; + remained -= value_len; + + tBleStatus result = aci_gatt_update_char_value_ext( 0, + serial_svc->svc_handle, + serial_svc->tx_char_handle, + remained ? 0x00 : 0x02, data_len, - data); - if(result) { - FURI_LOG_E(TAG, "Failed updating TX characteristic: %d", result); + value_offset, + value_len, + data + value_offset); + + if(result) { + FURI_LOG_E(TAG, "Failed updating TX characteristic: %d", result); + return false; + } } - return result != BLE_STATUS_SUCCESS; + + return true; } diff --git a/firmware/targets/f7/ble-glue/serial_service.h b/firmware/targets/f7/ble-glue/serial_service.h index 3ea548af..6e19f86d 100644 --- a/firmware/targets/f7/ble-glue/serial_service.h +++ b/firmware/targets/f7/ble-glue/serial_service.h @@ -3,7 +3,8 @@ #include #include -#define SERIAL_SVC_DATA_LEN_MAX (248) +#define SERIAL_SVC_DATA_LEN_MAX (486) +#define SERIAL_SVC_CHAR_VALUE_LEN_MAX (243) #ifdef __cplusplus extern "C" { @@ -36,7 +37,7 @@ void serial_svc_stop(); bool serial_svc_is_started(); -bool serial_svc_update_tx(uint8_t* data, uint8_t data_len); +bool serial_svc_update_tx(uint8_t* data, uint16_t data_len); #ifdef __cplusplus }