From 7a722101572ceab99e1c0a8b9b9db877d359cc5b Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 23 Sep 2022 14:41:20 +0200 Subject: [PATCH] Blufi: Add function for blufi management inside custom gap event handler --- .../btc/profile/esp/blufi/include/esp_blufi.h | 15 +++++ .../profile/esp/blufi/nimble_host/esp_blufi.c | 66 +++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h b/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h index ee9069059cdc..a368325dfecd 100644 --- a/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h +++ b/components/bt/common/btc/profile/esp/blufi/include/esp_blufi.h @@ -12,6 +12,7 @@ #ifdef CONFIG_BT_NIMBLE_ENABLED #include "nimble/ble.h" +#include "host/ble_gap.h" #include "modlog/modlog.h" #endif @@ -81,4 +82,18 @@ void esp_blufi_adv_start(void); void esp_blufi_send_encap(void *arg); +#ifdef CONFIG_BT_NIMBLE_ENABLED +/** + * @brief Handle gap event for BluFi. + * This function can be called inside custom use gap event handler. + * It provide minimal event management for BluFi purpose. + * + * @param[in] event The type of event being signalled. + * @param[in] arg Application-specified argument. Currently unused + * @return int 0 in case of success. + * Other in case of failure. + */ +int esp_blufi_handle_gap_events(struct ble_gap_event *event, void *arg); +#endif + #endif/* _ESP_BLUFI_ */ diff --git a/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c b/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c index d86bb607f089..ca5bb0b4b5e3 100644 --- a/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c +++ b/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c @@ -470,4 +470,70 @@ void esp_blufi_btc_deinit(void) { btc_deinit(); } + +int esp_blufi_handle_gap_events(struct ble_gap_event *event, void *arg) +{ + struct ble_gap_conn_desc desc; + int rc; + + if (event != NULL) { + switch (event->type) { + case BLE_GAP_EVENT_CONNECT: + if (event->connect.status == 0) { + btc_msg_t msg; + esp_blufi_cb_param_t param; + + blufi_env.is_connected = true; + blufi_env.recv_seq = blufi_env.send_seq = 0; + blufi_env.conn_id = event->connect.conn_handle; + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_BLE_CONNECT; + + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + memcpy(param.connect.remote_bda, desc.peer_id_addr.val, ESP_BLUFI_BD_ADDR_LEN); + + param.connect.conn_id = event->connect.conn_handle; + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + } + return 0; + case BLE_GAP_EVENT_DISCONNECT: { + btc_msg_t msg; + esp_blufi_cb_param_t param; + + blufi_env.is_connected = false; + blufi_env.recv_seq = blufi_env.send_seq = 0; + blufi_env.sec_mode = 0x0; + blufi_env.offset = 0; + + memcpy(blufi_env.remote_bda, + event->disconnect.conn.peer_id_addr.val, + ESP_BLUFI_BD_ADDR_LEN); + + if (blufi_env.aggr_buf != NULL) { + osi_free(blufi_env.aggr_buf); + blufi_env.aggr_buf = NULL; + } + + msg.sig = BTC_SIG_API_CB; + msg.pid = BTC_PID_BLUFI; + msg.act = ESP_BLUFI_EVENT_BLE_DISCONNECT; + memcpy(param.disconnect.remote_bda, + event->disconnect.conn.peer_id_addr.val, + ESP_BLUFI_BD_ADDR_LEN); + btc_transfer_context(&msg, ¶m, sizeof(esp_blufi_cb_param_t), NULL); + return 0; + } + + case BLE_GAP_EVENT_MTU: + blufi_env.frag_size = (event->mtu.value < BLUFI_MAX_DATA_LEN ? event->mtu.value : + BLUFI_MAX_DATA_LEN) - BLUFI_MTU_RESERVED_SIZE; + return 0; + } + } + + return 0; +} #endif