From a5052a03750abd9a94531e9bd8c3ebd3e708d7ec Mon Sep 17 00:00:00 2001 From: gornekich Date: Sun, 21 Nov 2021 17:47:54 +0300 Subject: [PATCH] [FL-2053] BLE MTU processing #830 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: あく --- applications/bt/bt_service/bt.c | 10 +++++++--- applications/bt/bt_service/bt_i.h | 1 + firmware/targets/f6/ble-glue/gap.c | 10 ++++++++++ firmware/targets/f6/ble-glue/gap.h | 2 ++ firmware/targets/f6/ble-glue/serial_service.h | 2 +- firmware/targets/f7/ble-glue/gap.c | 10 ++++++++++ firmware/targets/f7/ble-glue/gap.h | 2 ++ firmware/targets/f7/ble-glue/serial_service.h | 2 +- 8 files changed, 34 insertions(+), 5 deletions(-) diff --git a/applications/bt/bt_service/bt.c b/applications/bt/bt_service/bt.c index 296f5593..bcf37fa0 100755 --- a/applications/bt/bt_service/bt.c +++ b/applications/bt/bt_service/bt.c @@ -55,6 +55,8 @@ static void bt_battery_level_changed_callback(const void* _event, void* context) Bt* bt_alloc() { Bt* bt = furi_alloc(sizeof(Bt)); + // Init default maximum packet size + bt->max_packet_size = FURI_HAL_BT_PACKET_SIZE_MAX; // Load settings if(!bt_settings_load(&bt->bt_settings)) { bt_settings_save(&bt->bt_settings); @@ -113,9 +115,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_PACKET_SIZE_MAX) { - furi_hal_bt_tx(&bytes[bytes_sent], FURI_HAL_BT_PACKET_SIZE_MAX); - bytes_sent += FURI_HAL_BT_PACKET_SIZE_MAX; + if(bytes_remain > bt->max_packet_size) { + furi_hal_bt_tx(&bytes[bytes_sent], bt->max_packet_size); + bytes_sent += bt->max_packet_size; } else { furi_hal_bt_tx(&bytes[bytes_sent], bytes_remain); bytes_sent += bytes_remain; @@ -177,6 +179,8 @@ static void bt_on_gap_event_callback(BleEvent event, void* context) { BtMessage message = { .type = BtMessageTypePinCodeShow, .data.pin_code = event.data.pin_code}; furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK); + } else if(event.type == BleEventTypeUpdateMTU) { + bt->max_packet_size = event.data.max_packet_size; } } diff --git a/applications/bt/bt_service/bt_i.h b/applications/bt/bt_service/bt_i.h index 698dee1e..840d920e 100644 --- a/applications/bt/bt_service/bt_i.h +++ b/applications/bt/bt_service/bt_i.h @@ -41,6 +41,7 @@ typedef struct { struct Bt { uint8_t* bt_keys_addr_start; uint16_t bt_keys_size; + uint16_t max_packet_size; BtSettings bt_settings; BtStatus status; osMessageQueueId_t message_queue; diff --git a/firmware/targets/f6/ble-glue/gap.c b/firmware/targets/f6/ble-glue/gap.c index 2fd29f10..a2381653 100644 --- a/firmware/targets/f6/ble-glue/gap.c +++ b/firmware/targets/f6/ble-glue/gap.c @@ -155,6 +155,16 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt ) } break; + case EVT_BLUE_ATT_EXCHANGE_MTU_RESP: + { + aci_att_exchange_mtu_resp_event_rp0 *pr = (void*)blue_evt->data; + FURI_LOG_I(TAG, "Rx MTU size: %d", pr->Server_RX_MTU); + // Set maximum packet size given header size is 3 bytes + BleEvent event = {.type = BleEventTypeUpdateMTU, .data.max_packet_size = pr->Server_RX_MTU - 3}; + gap->on_event_cb(event, gap->context); + } + break; + case EVT_BLUE_GAP_AUTHORIZATION_REQUEST: FURI_LOG_I(TAG, "Authorization request event"); break; diff --git a/firmware/targets/f6/ble-glue/gap.h b/firmware/targets/f6/ble-glue/gap.h index 5b82cfec..dfeffd63 100644 --- a/firmware/targets/f6/ble-glue/gap.h +++ b/firmware/targets/f6/ble-glue/gap.h @@ -13,10 +13,12 @@ typedef enum { BleEventTypeStartAdvertising, BleEventTypeStopAdvertising, BleEventTypePinCodeShow, + BleEventTypeUpdateMTU, } BleEventType; typedef union { uint32_t pin_code; + uint16_t max_packet_size; } BleEventData; typedef struct { diff --git a/firmware/targets/f6/ble-glue/serial_service.h b/firmware/targets/f6/ble-glue/serial_service.h index 0aa4c79f..5be13f1b 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 (245) +#define SERIAL_SVC_DATA_LEN_MAX (248) #ifdef __cplusplus extern "C" { diff --git a/firmware/targets/f7/ble-glue/gap.c b/firmware/targets/f7/ble-glue/gap.c index 2fd29f10..a2381653 100644 --- a/firmware/targets/f7/ble-glue/gap.c +++ b/firmware/targets/f7/ble-glue/gap.c @@ -155,6 +155,16 @@ SVCCTL_UserEvtFlowStatus_t SVCCTL_App_Notification( void *pckt ) } break; + case EVT_BLUE_ATT_EXCHANGE_MTU_RESP: + { + aci_att_exchange_mtu_resp_event_rp0 *pr = (void*)blue_evt->data; + FURI_LOG_I(TAG, "Rx MTU size: %d", pr->Server_RX_MTU); + // Set maximum packet size given header size is 3 bytes + BleEvent event = {.type = BleEventTypeUpdateMTU, .data.max_packet_size = pr->Server_RX_MTU - 3}; + gap->on_event_cb(event, gap->context); + } + break; + case EVT_BLUE_GAP_AUTHORIZATION_REQUEST: FURI_LOG_I(TAG, "Authorization request event"); break; diff --git a/firmware/targets/f7/ble-glue/gap.h b/firmware/targets/f7/ble-glue/gap.h index 5b82cfec..dfeffd63 100644 --- a/firmware/targets/f7/ble-glue/gap.h +++ b/firmware/targets/f7/ble-glue/gap.h @@ -13,10 +13,12 @@ typedef enum { BleEventTypeStartAdvertising, BleEventTypeStopAdvertising, BleEventTypePinCodeShow, + BleEventTypeUpdateMTU, } BleEventType; typedef union { uint32_t pin_code; + uint16_t max_packet_size; } BleEventData; typedef struct { diff --git a/firmware/targets/f7/ble-glue/serial_service.h b/firmware/targets/f7/ble-glue/serial_service.h index 0aa4c79f..5be13f1b 100644 --- a/firmware/targets/f7/ble-glue/serial_service.h +++ b/firmware/targets/f7/ble-glue/serial_service.h @@ -3,7 +3,7 @@ #include #include -#define SERIAL_SVC_DATA_LEN_MAX (245) +#define SERIAL_SVC_DATA_LEN_MAX (248) #ifdef __cplusplus extern "C" {