diff --git a/components/protocomm/include/transports/protocomm_ble.h b/components/protocomm/include/transports/protocomm_ble.h index 085767625eae..51aec06e99f9 100644 --- a/components/protocomm/include/transports/protocomm_ble.h +++ b/components/protocomm/include/transports/protocomm_ble.h @@ -59,6 +59,35 @@ typedef struct name_uuid { uint16_t uuid; } protocomm_ble_name_uuid_t; +/** + * @brief Structure for BLE events in Protocomm. + */ +typedef struct { + /** + * This field indicates the type of BLE event that occurred. + */ + uint16_t evt_type; + /** + * The handle of the relevant connection. + */ + uint16_t conn_handle; + + union { + /** + * The status of the connection attempt; + * o 0: the connection was successfully established. + * o BLE host error code: the connection attempt failed for + * the specified reason. + */ + uint16_t conn_status; + + /** + * Return code indicating the reason for the disconnect. + */ + uint16_t disconnect_reason; + }; +} protocomm_ble_event_t; + /** * @brief Config parameters for protocomm BLE service */ diff --git a/components/protocomm/src/transports/protocomm_ble.c b/components/protocomm/src/transports/protocomm_ble.c index 6c070e4001d8..322c6086411e 100644 --- a/components/protocomm/src/transports/protocomm_ble.c +++ b/components/protocomm/src/transports/protocomm_ble.c @@ -345,7 +345,13 @@ static void transport_simple_ble_disconnect(esp_gatts_cb_event_t event, esp_gatt if (ret != ESP_OK) { ESP_LOGE(TAG, "error closing the session after disconnect"); } else { - if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_DISCONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + protocomm_ble_event_t ble_event = {}; + /* Assign the event type */ + ble_event.evt_type = PROTOCOMM_TRANSPORT_BLE_DISCONNECTED; + /* Set the Disconnection handle */ + ble_event.conn_handle = param->disconnect.conn_id; + + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_DISCONNECTED, &ble_event, sizeof(protocomm_ble_event_t), portMAX_DELAY) != ESP_OK) { ESP_LOGE(TAG, "Failed to post transport disconnection event"); } } @@ -371,7 +377,13 @@ static void transport_simple_ble_connect(esp_gatts_cb_event_t event, esp_gatt_if if (ret != ESP_OK) { ESP_LOGE(TAG, "error creating the session"); } else { - if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_CONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + protocomm_ble_event_t ble_event = {}; + /* Assign the event type */ + ble_event.evt_type = PROTOCOMM_TRANSPORT_BLE_CONNECTED; + /* Set the Connection handle */ + ble_event.conn_handle = param->connect.conn_id; + + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_CONNECTED, &ble_event, sizeof(protocomm_ble_event_t), portMAX_DELAY) != ESP_OK) { ESP_LOGE(TAG, "Failed to post transport pairing event"); } } diff --git a/components/protocomm/src/transports/protocomm_nimble.c b/components/protocomm/src/transports/protocomm_nimble.c index 4a4b0357700b..3b2b2bfc6993 100644 --- a/components/protocomm/src/transports/protocomm_nimble.c +++ b/components/protocomm/src/transports/protocomm_nimble.c @@ -569,7 +569,14 @@ static void transport_simple_ble_disconnect(struct ble_gap_event *event, void *a if (ret != ESP_OK) { ESP_LOGE(TAG, "error closing the session after disconnect"); } else { - if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_DISCONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + protocomm_ble_event_t ble_event = {}; + /* Assign the event type */ + ble_event.evt_type = PROTOCOMM_TRANSPORT_BLE_DISCONNECTED; + /* Set the Disconnection handle */ + ble_event.conn_handle = event->disconnect.conn.conn_handle; + ble_event.disconnect_reason = event->disconnect.reason; + + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_DISCONNECTED, &ble_event, sizeof(protocomm_ble_event_t), portMAX_DELAY) != ESP_OK) { ESP_LOGE(TAG, "Failed to post transport disconnection event"); } } @@ -595,7 +602,14 @@ static void transport_simple_ble_connect(struct ble_gap_event *event, void *arg) if (ret != ESP_OK) { ESP_LOGE(TAG, "error creating the session"); } else { - if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_CONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + protocomm_ble_event_t ble_event = {}; + /* Assign the event type */ + ble_event.evt_type = PROTOCOMM_TRANSPORT_BLE_CONNECTED; + /* Set the Connection handle */ + ble_event.conn_handle = event->connect.conn_handle; + ble_event.conn_status = event->connect.status; + + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_CONNECTED, &ble_event, sizeof(protocomm_ble_event_t), portMAX_DELAY) != ESP_OK) { ESP_LOGE(TAG, "Failed to post transport pairing event"); } }