diff --git a/doc/nrf-bm/release_notes/release_notes_changelog.rst b/doc/nrf-bm/release_notes/release_notes_changelog.rst index ebffc06962..1dad1ecc44 100644 --- a/doc/nrf-bm/release_notes/release_notes_changelog.rst +++ b/doc/nrf-bm/release_notes/release_notes_changelog.rst @@ -112,7 +112,12 @@ Libraries * :ref:`lib_ble_conn_params` library: - * Added missing Kconfig dependencies. + * Added: + + * Missing Kconfig dependencies. + * Error event. + + * Updated the name of the ``id`` member in the :c:struct:`ble_conn_params_evt` structure to :c:member:`ble_conn_params_evt.evt_type`. * :ref:`lib_bm_zms` library: @@ -175,9 +180,31 @@ Libraries * To use errno instead of nrf_errors. * The :c:func:`bm_storage_init` function to expect an additional input parameter of type pointer to struct :c:struct:`bm_storage_config` for configuring the storage instance that is being initialized. +* :ref:`lib_ble_service_bas` service: + + * Added the error event to align event handling with other services. + The event is currently unused. + +* :ref:`lib_ble_service_hrs` service: + + * Added the error event to align event handling with other services. + The event is currently unused. + +* :ref:`lib_ble_service_lbs` service: + + * Added the error event to align event handling with other services. + The event is currently unused. + * :ref:`lib_ble_service_nus` service: * Fixed an issue where the client context was shared between all instances. + * Added the error event to align event handling with other services. + * Updated the name of the ``type`` member in the :c:struct:`ble_nus_evt` structure to :c:member:`ble_nus_evt.evt_type`. + +* BLE Gatt Queue library: + + * Updated the event handling to align with other libraries. + The :c:struct:`ble_gq_req` now takes an :c:type:`ble_gq_evt_handler_t` event handler and the :c:member:`ble_gq_req.ctx` context. Samples ======= diff --git a/include/bm/bluetooth/ble_adv.h b/include/bm/bluetooth/ble_adv.h index 54e3936535..9a21da65a4 100644 --- a/include/bm/bluetooth/ble_adv.h +++ b/include/bm/bluetooth/ble_adv.h @@ -73,10 +73,6 @@ enum ble_adv_mode { * @brief Advertising event types. */ enum ble_adv_evt_type { - /** - * @brief Error. - */ - BLE_ADV_EVT_ERROR, /** * @brief Idle; no connectable advertising is ongoing. */ @@ -120,7 +116,11 @@ enum ble_adv_evt_type { * for directed advertising by calling @ref ble_adv_peer_addr_reply. Otherwise, it can * ignore the event to let the device advertise in the next configured advertising mode. */ - BLE_ADV_EVT_PEER_ADDR_REQUEST + BLE_ADV_EVT_PEER_ADDR_REQUEST, + /** + * @brief Error. + */ + BLE_ADV_EVT_ERROR, }; /** @brief Advertising event. */ @@ -130,6 +130,7 @@ struct ble_adv_evt { union { /** @ref BLE_ADV_EVT_ERROR event data. */ struct { + /** Error reason. */ uint32_t reason; } error; }; diff --git a/include/bm/bluetooth/ble_conn_params.h b/include/bm/bluetooth/ble_conn_params.h index bb010a379f..422c4d65a1 100644 --- a/include/bm/bluetooth/ble_conn_params.h +++ b/include/bm/bluetooth/ble_conn_params.h @@ -22,9 +22,9 @@ extern "C" { #endif /** - * @brief BLE connection parameter event IDs. + * @brief BLE connection parameter event types. */ -enum ble_conn_params_evt_id { +enum ble_conn_params_evt_type { /** * @brief Connection parameters updated. */ @@ -45,6 +45,10 @@ enum ble_conn_params_evt_id { * @brief GAP radio phy mode update procedure completed. */ BLE_CONN_PARAMS_EVT_RADIO_PHY_MODE_UPDATED, + /** + * @brief Error. + */ + BLE_CONN_PARAMS_EVT_ERROR, }; /** @@ -66,9 +70,9 @@ struct ble_conn_params_data_length { */ struct ble_conn_params_evt { /** - * @brief Event ID. + * @brief Event type. */ - enum ble_conn_params_evt_id id; + enum ble_conn_params_evt_type evt_type; /** * @brief Connection handle. */ @@ -98,6 +102,13 @@ struct ble_conn_params_evt { * From @ref BLE_CONN_PARAMS_EVT_RADIO_PHY_MODE_UPDATED. */ ble_gap_evt_phy_update_t phy_update_evt; + /** @ref BLE_CONN_PARAMS_EVT_ERROR event data. */ + struct { + /** + * @brief Error reason. + */ + uint32_t reason; + } error; }; }; diff --git a/include/bm/bluetooth/ble_gq.h b/include/bm/bluetooth/ble_gq.h index 2007a1a5a1..94b7d105d7 100644 --- a/include/bm/bluetooth/ble_gq.h +++ b/include/bm/bluetooth/ble_gq.h @@ -129,24 +129,40 @@ enum ble_gq_req_type { }; /** - * @brief Error handler type. + * @brief Advertising event types. */ -typedef void (*ble_gq_req_error_cb_t)(uint16_t conn_handle, uint32_t nrf_error, void *context); - -/** - * @brief Structure used to handle SoftDevice error. - */ -struct ble_gq_req_error_handler { +enum ble_gq_evt_type { /** - * @brief Error handler to be called in case of an error from SoftDevice. + * @brief Error. */ - ble_gq_req_error_cb_t cb; + BLE_GQ_EVT_ERROR, +}; + +/** @brief Gatt Queue event. */ +struct ble_gq_evt { + /** @brief Advertising event type. */ + enum ble_gq_evt_type evt_type; /** - * @brief Parameter passed to the error handler; + * @brief Connection handle for which the event applies. */ - void *ctx; + uint16_t conn_handle; + union { + /** @ref BLE_GQ_EVT_ERROR event data. */ + struct { + /** Event result code. */ + uint32_t reason; + } error; + }; }; +/* Forward declaration */ +struct ble_gq_req; + +/** + * @brief Event handler type. + */ +typedef void (*ble_gq_evt_handler_t)(const struct ble_gq_req *req, struct ble_gq_evt *evt); + /** * @brief Structure to hold a BLE GATT request. */ @@ -168,7 +184,11 @@ struct ble_gq_req { /** * @brief Error handler structure. */ - struct ble_gq_req_error_handler error_handler; + ble_gq_evt_handler_t evt_handler; + /** + * @brief Context. + */ + void *ctx; /** * @brief Request type specific parameters. */ diff --git a/include/bm/bluetooth/ble_qwr.h b/include/bm/bluetooth/ble_qwr.h index cfead9bbe0..249c65e906 100644 --- a/include/bm/bluetooth/ble_qwr.h +++ b/include/bm/bluetooth/ble_qwr.h @@ -48,8 +48,6 @@ extern "C" { /** @brief Queued Writes module event types. */ enum ble_qwr_evt_type { - /** Error event */ - BLE_QWR_EVT_ERROR, /** Event that indicates that an execute write command was received for a registered handle * and that the received data was actually written and is now ready. */ @@ -58,6 +56,8 @@ enum ble_qwr_evt_type { * and that the write request must now be accepted or rejected. */ BLE_QWR_EVT_AUTH_REQUEST, + /** Error event */ + BLE_QWR_EVT_ERROR, }; /** @brief Queued Writes module events. */ @@ -65,10 +65,6 @@ struct ble_qwr_evt { /** Type of the event. */ enum ble_qwr_evt_type evt_type; union { - /** @ref BLE_QWR_EVT_ERROR event data. */ - struct { - uint32_t reason; - } error; /** @ref BLE_QWR_EVT_EXECUTE_WRITE event data. */ struct { /** Handle of the attribute to which the event relates. */ @@ -79,6 +75,11 @@ struct ble_qwr_evt { /** Handle of the attribute to which the event relates. */ uint16_t attr_handle; } auth_req; + /** @ref BLE_QWR_EVT_ERROR event data. */ + struct { + /** Error reason. */ + uint32_t reason; + } error; }; }; diff --git a/include/bm/bluetooth/services/ble_bas.h b/include/bm/bluetooth/services/ble_bas.h index 9ff23c86b9..bd9cc5afba 100644 --- a/include/bm/bluetooth/services/ble_bas.h +++ b/include/bm/bluetooth/services/ble_bas.h @@ -42,7 +42,11 @@ enum ble_bas_evt_type { /** * @brief Battery level notification disabled. */ - BLE_BAS_EVT_NOTIFICATION_DISABLED + BLE_BAS_EVT_NOTIFICATION_DISABLED, + /** + * @brief Error event. + */ + BLE_BAS_EVT_ERROR, }; /** @@ -57,6 +61,13 @@ struct ble_bas_evt { * @brief Connection handle for which the event applies. */ uint16_t conn_handle; + union { + /** @ref BLE_BAS_EVT_ERROR event data. */ + struct { + /** Error reason. */ + uint32_t reason; + } error; + }; }; /* Forward declaration */ diff --git a/include/bm/bluetooth/services/ble_cgms.h b/include/bm/bluetooth/services/ble_cgms.h index a7b017bd1d..bc6a800db4 100644 --- a/include/bm/bluetooth/services/ble_cgms.h +++ b/include/bm/bluetooth/services/ble_cgms.h @@ -247,18 +247,30 @@ extern "C" { /** @brief CGM Service events. */ enum ble_cgms_evt_type { - /** Error. */ - BLE_CGMS_EVT_ERROR, - /** Glucose value notification enabled. */ + /** + * @brief Glucose value notification enabled. + */ BLE_CGMS_EVT_NOTIFICATION_ENABLED, - /** Glucose value notification disabled. */ + /** + * @brief Glucose value notification disabled. + */ BLE_CGMS_EVT_NOTIFICATION_DISABLED, - /** Glucose value notification start session. */ + /** + * @brief Glucose value notification start session. + */ BLE_CGMS_EVT_START_SESSION, - /** Glucose value notification stop session. */ + /** + * @brief Glucose value notification stop session. + */ BLE_CGMS_EVT_STOP_SESSION, - /** Glucose value write communication interval. */ + /** + * @brief Glucose value write communication interval. + */ BLE_CGMS_EVT_WRITE_COMM_INTERVAL, + /** + * @brief Error. + */ + BLE_CGMS_EVT_ERROR, }; /** @} */ /* ble_cgms_enums */ @@ -272,10 +284,14 @@ enum ble_cgms_evt_type { struct ble_cgms_evt { /** Event type. */ enum ble_cgms_evt_type evt_type; + /** + * @brief Connection handle for which the event applies. + */ + uint16_t conn_handle; union { /** @ref BLE_CGMS_EVT_ERROR event data. */ struct { - /* Error reason */ + /** Error reason. */ uint32_t reason; } error; }; @@ -444,7 +460,7 @@ struct ble_cgms { /** Pointer to BLE GATT Queue instance. */ const struct ble_gq *gatt_queue; /** Error handler to be called in case of an error from SoftDevice. */ - ble_gq_req_error_cb_t gatt_err_handler; + ble_gq_evt_handler_t ble_gq_evt_handler; /** Handle of the CGM Service (as provided by the BLE stack). */ uint16_t service_handle; /** GATTS characteristic handles for the different characteristics in the service. */ diff --git a/include/bm/bluetooth/services/ble_hids.h b/include/bm/bluetooth/services/ble_hids.h index 70a978fd0d..ed6a548362 100644 --- a/include/bm/bluetooth/services/ble_hids.h +++ b/include/bm/bluetooth/services/ble_hids.h @@ -246,6 +246,10 @@ struct ble_hids_evt { * @brief Event type. */ enum ble_hids_evt_type evt_type; + /** + * @brief Connection handle for which the event applies. + */ + uint16_t conn_handle; /** * @brief BLE event. */ diff --git a/include/bm/bluetooth/services/ble_hrs.h b/include/bm/bluetooth/services/ble_hrs.h index 123bd2fa98..0fbc555243 100644 --- a/include/bm/bluetooth/services/ble_hrs.h +++ b/include/bm/bluetooth/services/ble_hrs.h @@ -56,7 +56,11 @@ enum ble_hrs_evt_type { /** * @brief Heart rate value notifcation disabled. */ - BLE_HRS_EVT_NOTIFICATION_DISABLED + BLE_HRS_EVT_NOTIFICATION_DISABLED, + /** + * @brief Error event. + */ + BLE_HRS_EVT_ERROR, }; /** @@ -67,6 +71,17 @@ struct ble_hrs_evt { * @brief Event type. */ enum ble_hrs_evt_type evt_type; + /** + * @brief Connection handle for which the event applies. + */ + uint16_t conn_handle; + union { + /** @ref BLE_HRS_EVT_ERROR event data. */ + struct { + /** Error reason. */ + uint32_t reason; + } error; + }; }; /* Forward declaration */ diff --git a/include/bm/bluetooth/services/ble_lbs.h b/include/bm/bluetooth/services/ble_lbs.h index 4decb8b7bb..057ca0a176 100644 --- a/include/bm/bluetooth/services/ble_lbs.h +++ b/include/bm/bluetooth/services/ble_lbs.h @@ -40,19 +40,33 @@ struct ble_lbs; NRF_SDH_BLE_OBSERVER(_name ## _obs, ble_lbs_on_ble_evt, &_name, HIGH) enum ble_lbs_evt_type { + /** + * @brief LED write event. + */ BLE_LBS_EVT_LED_WRITE, + /** + * @brief Error event. + */ + BLE_LBS_EVT_ERROR, }; struct ble_lbs_evt { enum ble_lbs_evt_type evt_type; + /** + * @brief Connection handle for which the event applies. + */ + uint16_t conn_handle; union { /** @ref BLE_LBS_EVT_LED_WRITE event data. */ struct { - /** Connection handle */ - uint16_t conn_handle; /** Value to write */ uint8_t value; } led_write; + /** @ref BLE_LBS_EVT_ERROR event data. */ + struct { + /** Error reason. */ + uint32_t reason; + } error; }; }; diff --git a/include/bm/bluetooth/services/ble_nus.h b/include/bm/bluetooth/services/ble_nus.h index 6d6b481927..1653ebff31 100644 --- a/include/bm/bluetooth/services/ble_nus.h +++ b/include/bm/bluetooth/services/ble_nus.h @@ -67,26 +67,26 @@ void ble_nus_on_ble_evt(ble_evt_t const *ble_evt, void *context); /** @brief Nordic UART Service event types. */ enum ble_nus_evt_type { - /** Data received. */ + /** + * @brief Data received. + */ BLE_NUS_EVT_RX_DATA, - /** Service is ready to accept new data to be transmitted. */ + /** + * @brief Service is ready to accept new data to be transmitted. + */ BLE_NUS_EVT_TX_RDY, - /** Notification has been enabled. */ + /** + * @brief Notification has been enabled. + */ BLE_NUS_EVT_COMM_STARTED, - /** Notification has been disabled. */ + /** + * @brief Notification has been disabled. + */ BLE_NUS_EVT_COMM_STOPPED, -}; - -/** - * @brief Nordic UART Service @ref BLE_NUS_EVT_RX_DATA event data. - * - * @details This structure is passed to an event when @ref BLE_NUS_EVT_RX_DATA occurs. - */ -struct ble_nus_evt_rx_data { - /** Pointer to the buffer with received data. */ - uint8_t const *data; - /** Length of received data. */ - uint16_t length; + /** + * @brief Error event. + */ + BLE_NUS_EVT_ERROR, }; /** @@ -106,21 +106,31 @@ struct ble_nus_client_context { */ struct ble_nus_evt { /** Event type. */ - enum ble_nus_evt_type type; - /** Pointer to the instance. */ - struct ble_nus *nus; + enum ble_nus_evt_type evt_type; /** Connection handle. */ uint16_t conn_handle; /** Pointer to the link context. */ struct ble_nus_client_context *link_ctx; union { /** @ref BLE_NUS_EVT_RX_DATA event data. */ - struct ble_nus_evt_rx_data rx_data; - } params; + struct { + /** Pointer to the buffer with received data. */ + uint8_t const *data; + /** Length of received data. */ + uint16_t length; + } rx_data; + /** @ref BLE_BAS_EVT_ERROR event data. */ + struct { + /** Error reason. */ + uint32_t reason; + } error; + }; }; +struct ble_nus; + /** @brief Nordic UART Service event handler type. */ -typedef void (*ble_nus_evt_handler_t) (const struct ble_nus_evt *evt); +typedef void (*ble_nus_evt_handler_t)(struct ble_nus *nus, const struct ble_nus_evt *evt); /* * @brief Nordic UART Service initialization structure. diff --git a/lib/bluetooth/ble_adv/ble_adv.c b/lib/bluetooth/ble_adv/ble_adv.c index 8c12154fb7..d8a4928080 100644 --- a/lib/bluetooth/ble_adv/ble_adv.c +++ b/lib/bluetooth/ble_adv/ble_adv.c @@ -81,6 +81,7 @@ static void on_disconnected(struct ble_adv *ble_adv, ble_evt_t const *ble_evt) if (ble_evt->evt.gap_evt.conn_handle == ble_adv->conn_handle) { nrf_err = ble_adv_start(ble_adv, BLE_ADV_MODE_DIRECTED_HIGH_DUTY); if (nrf_err) { + LOG_ERR("Failed to start advertising, nrf_error %#x", nrf_err); adv_evt.evt_type = BLE_ADV_EVT_ERROR; adv_evt.error.reason = nrf_err; ble_adv->evt_handler(ble_adv, &adv_evt); @@ -101,6 +102,7 @@ static void on_terminated(struct ble_adv *ble_adv, ble_evt_t const *ble_evt) LOG_DBG("Advertising timeout"); nrf_err = ble_adv_start(ble_adv, adv_mode_next(ble_adv->mode_current)); if (nrf_err) { + LOG_ERR("Failed to start advertising, nrf_error %#x", nrf_err); adv_evt.error.reason = nrf_err; adv_evt.evt_type = BLE_ADV_EVT_ERROR; ble_adv->evt_handler(ble_adv, &adv_evt); diff --git a/lib/bluetooth/ble_conn_params/att_mtu.c b/lib/bluetooth/ble_conn_params/att_mtu.c index 7aa93733c7..a9f66da4f0 100644 --- a/lib/bluetooth/ble_conn_params/att_mtu.c +++ b/lib/bluetooth/ble_conn_params/att_mtu.c @@ -29,6 +29,10 @@ static struct { static void mtu_exchange_request(uint16_t conn_handle, int idx) { uint32_t nrf_err; + struct ble_conn_params_evt app_evt = { + .evt_type = BLE_CONN_PARAMS_EVT_ERROR, + .conn_handle = conn_handle, + }; nrf_err = sd_ble_gattc_exchange_mtu_request(conn_handle, links[idx].att_mtu_desired); if (nrf_err == NRF_SUCCESS) { @@ -41,6 +45,9 @@ static void mtu_exchange_request(uint16_t conn_handle, int idx) links[idx].att_mtu_exchange_pending = true; } else if (nrf_err) { LOG_ERR("Failed to initiate ATT MTU exchange, nrf_error %#x", nrf_err); + + app_evt.error.reason = nrf_err; + ble_conn_params_event_send(&app_evt); } } @@ -48,6 +55,9 @@ static void on_exchange_mtu_req_evt(uint16_t conn_handle, int idx, const ble_gatts_evt_exchange_mtu_request_t *evt) { uint32_t nrf_err; + struct ble_conn_params_evt app_evt = { + .conn_handle = conn_handle, + }; /* Determine the lowest ATT MTU between our own desired ATT MTU and the peer's, * and at the same time ensure that we don't go lower than the actual MTU size. @@ -61,17 +71,19 @@ static void on_exchange_mtu_req_evt(uint16_t conn_handle, int idx, nrf_err = sd_ble_gatts_exchange_mtu_reply(conn_handle, links[idx].att_mtu); if (nrf_err) { LOG_ERR("Failed to reply to MTU exchange request, nrf_error %#x", nrf_err); + + app_evt.evt_type = BLE_CONN_PARAMS_EVT_ERROR; + app_evt.error.reason = nrf_err; + ble_conn_params_event_send(&app_evt); + return; } LOG_INF("ATT MTU set to %u bytes for peer %#x", links[idx].att_mtu, conn_handle); /* The ATT MTU exchange has finished, send an event to the application */ - const struct ble_conn_params_evt app_evt = { - .id = BLE_CONN_PARAMS_EVT_ATT_MTU_UPDATED, - .conn_handle = conn_handle, - .att_mtu = links[idx].att_mtu, - }; + app_evt.evt_type = BLE_CONN_PARAMS_EVT_ATT_MTU_UPDATED; + app_evt.att_mtu = links[idx].att_mtu; ble_conn_params_event_send(&app_evt); } @@ -90,7 +102,7 @@ static void on_exchange_mtu_rsp_evt(uint16_t conn_handle, int idx, /* The ATT MTU exchange has finished, send an event to the application. */ const struct ble_conn_params_evt app_evt = { - .id = BLE_CONN_PARAMS_EVT_ATT_MTU_UPDATED, + .evt_type = BLE_CONN_PARAMS_EVT_ATT_MTU_UPDATED, .conn_handle = conn_handle, .att_mtu = links[idx].att_mtu, }; diff --git a/lib/bluetooth/ble_conn_params/conn_param.c b/lib/bluetooth/ble_conn_params/conn_param.c index bbb1fd75bb..e898fd3551 100644 --- a/lib/bluetooth/ble_conn_params/conn_param.c +++ b/lib/bluetooth/ble_conn_params/conn_param.c @@ -115,7 +115,7 @@ static void on_conn_params_update(uint16_t conn_handle, int idx, if (conn_params_can_agree(&evt->conn_params)) { const struct ble_conn_params_evt app_evt = { - .id = BLE_CONN_PARAMS_EVT_UPDATED, + .evt_type = BLE_CONN_PARAMS_EVT_UPDATED, .conn_handle = conn_handle, .conn_params = evt->conn_params, }; @@ -132,7 +132,7 @@ static void on_conn_params_update(uint16_t conn_handle, int idx, LOG_WRN("Could not agree on peer %#x connection params", conn_handle); const struct ble_conn_params_evt app_evt = { - .id = BLE_CONN_PARAMS_EVT_REJECTED, + .evt_type = BLE_CONN_PARAMS_EVT_REJECTED, .conn_handle = conn_handle, }; @@ -174,6 +174,10 @@ NRF_SDH_BLE_OBSERVER(ble_observer, on_ble_evt, NULL, HIGH); static int on_state_evt(enum nrf_sdh_state_evt evt, void *ctx) { uint32_t nrf_err; + struct ble_conn_params_evt app_evt = { + .evt_type = BLE_CONN_PARAMS_EVT_ERROR, + .conn_handle = BLE_CONN_HANDLE_INVALID, + }; if (evt != NRF_SDH_STATE_EVT_BLE_ENABLED) { return 0; @@ -182,6 +186,10 @@ static int on_state_evt(enum nrf_sdh_state_evt evt, void *ctx) nrf_err = sd_ble_gap_ppcp_set(&ppcp); if (nrf_err) { LOG_ERR("Failed to set preferred conn params, nrf_error %#x", nrf_err); + + app_evt.error.reason = nrf_err; + ble_conn_params_event_send(&app_evt); + return 0; } diff --git a/lib/bluetooth/ble_conn_params/data_length.c b/lib/bluetooth/ble_conn_params/data_length.c index 16b216e629..937e86ab4d 100644 --- a/lib/bluetooth/ble_conn_params/data_length.c +++ b/lib/bluetooth/ble_conn_params/data_length.c @@ -40,6 +40,10 @@ static void data_length_update(uint16_t conn_handle, int idx) .max_rx_time_us = BLE_GAP_DATA_LENGTH_AUTO, }; ble_gap_data_length_limitation_t dll = {0}; + struct ble_conn_params_evt app_evt = { + .evt_type = BLE_CONN_PARAMS_EVT_ERROR, + .conn_handle = conn_handle, + }; do { retry = false; @@ -67,15 +71,20 @@ static void data_length_update(uint16_t conn_handle, int idx) dlp.max_rx_octets = links[idx].desired.rx, retry = true; - } - if (dll.tx_rx_time_limited_us != 0) { + } else if (dll.tx_rx_time_limited_us != 0) { LOG_ERR("The requested combination of TX and RX packet lengths " "is too long by %u microseconds.", dll.tx_rx_time_limited_us); + + app_evt.error.reason = nrf_err; + ble_conn_params_event_send(&app_evt); } } else if (nrf_err) { LOG_ERR("Failed to initiate or respond to Data Length Update procedure, " "nrf_error %#x", nrf_err); + + app_evt.error.reason = nrf_err; + ble_conn_params_event_send(&app_evt); } } while (retry); } @@ -111,7 +120,7 @@ static void on_data_length_update_evt(uint16_t conn_handle, int idx, /* The Data length update has finished, send an event to the application. */ const struct ble_conn_params_evt app_evt = { - .id = BLE_CONN_PARAMS_EVT_DATA_LENGTH_UPDATED, + .evt_type = BLE_CONN_PARAMS_EVT_DATA_LENGTH_UPDATED, .conn_handle = conn_handle, .data_length.tx = links[idx].data_length.tx, .data_length.rx = links[idx].data_length.rx, diff --git a/lib/bluetooth/ble_conn_params/phy_mode.c b/lib/bluetooth/ble_conn_params/phy_mode.c index b60ece7b66..92018b9365 100644 --- a/lib/bluetooth/ble_conn_params/phy_mode.c +++ b/lib/bluetooth/ble_conn_params/phy_mode.c @@ -30,6 +30,10 @@ static void radio_phy_mode_update(uint16_t conn_handle, int idx) { uint32_t nrf_err; ble_gap_phys_t phys = links[idx].phy_mode; + struct ble_conn_params_evt app_evt = { + .evt_type = BLE_CONN_PARAMS_EVT_ERROR, + .conn_handle = conn_handle, + }; if (phys.tx_phys != BLE_GAP_PHY_NOT_SET) { @@ -58,12 +62,18 @@ static void radio_phy_mode_update(uint16_t conn_handle, int idx) radio_phy_mode_update(conn_handle, idx); } else { LOG_ERR("Failed PHY update procedure, nrf_error %#x", nrf_err); + app_evt.error.reason = nrf_err; + ble_conn_params_event_send(&app_evt); } } static void on_radio_phy_mode_update_evt(uint16_t conn_handle, int idx, const ble_gap_evt_phy_update_t *evt) { + struct ble_conn_params_evt app_evt = { + .conn_handle = conn_handle, + }; + if (evt->status == BLE_HCI_STATUS_CODE_SUCCESS) { links[idx].phy_mode_update_pending = false; links[idx].phy_mode.tx_phys = evt->tx_phy; @@ -79,13 +89,16 @@ static void on_radio_phy_mode_update_evt(uint16_t conn_handle, int idx, links[idx].phy_mode_update_pending = false; LOG_ERR("PHY update failed with status %u for peer %#x", evt->status, conn_handle); + + /* Send error event to application and return */ + app_evt.evt_type = BLE_CONN_PARAMS_EVT_ERROR; + app_evt.error.reason = evt->status; + ble_conn_params_event_send(&app_evt); + return; } - const struct ble_conn_params_evt app_evt = { - .id = BLE_CONN_PARAMS_EVT_RADIO_PHY_MODE_UPDATED, - .conn_handle = conn_handle, - .phy_update_evt = *evt, - }; + app_evt.evt_type = BLE_CONN_PARAMS_EVT_RADIO_PHY_MODE_UPDATED; + app_evt.phy_update_evt = *evt; ble_conn_params_event_send(&app_evt); } diff --git a/lib/bluetooth/ble_gq/gatt_queue.c b/lib/bluetooth/ble_gq/gatt_queue.c index f33658b31e..5d0812d436 100644 --- a/lib/bluetooth/ble_gq/gatt_queue.c +++ b/lib/bluetooth/ble_gq/gatt_queue.c @@ -106,14 +106,20 @@ static const req_data_store_t req_data_store[BLE_GQ_REQ_NUM] = { static void request_error_handle(const struct ble_gq_req *req, uint16_t conn_handle, uint32_t nrf_err) { + struct ble_gq_evt evt = { + .evt_type = BLE_GQ_EVT_ERROR, + .conn_handle = conn_handle, + .error.reason = nrf_err, + }; + if (nrf_err == NRF_SUCCESS) { LOG_DBG("SD GATT procedure (%d) succeeded on connection handle: %d.", req->type, conn_handle); } else { LOG_DBG("SD GATT procedure (%d) failed on connection handle %d with nrf_error %#x", req->type, conn_handle, nrf_err); - if (req->error_handler.cb != NULL) { - req->error_handler.cb(conn_handle, nrf_err, req->error_handler.ctx); + if (req->evt_handler != NULL) { + req->evt_handler(req, &evt); } } } diff --git a/samples/bluetooth/ble_cgms/src/main.c b/samples/bluetooth/ble_cgms/src/main.c index 748e31f629..3ff8d4092d 100644 --- a/samples/bluetooth/ble_cgms/src/main.c +++ b/samples/bluetooth/ble_cgms/src/main.c @@ -418,7 +418,7 @@ void on_conn_params_evt(const struct ble_conn_params_evt *evt) { uint32_t nrf_err; - switch (evt->id) { + switch (evt->evt_type) { case BLE_CONN_PARAMS_EVT_REJECTED: nrf_err = sd_ble_gap_disconnect(conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); if (nrf_err) { diff --git a/samples/bluetooth/ble_hrs/src/main.c b/samples/bluetooth/ble_hrs/src/main.c index f30c85d0e1..da969488af 100644 --- a/samples/bluetooth/ble_hrs/src/main.c +++ b/samples/bluetooth/ble_hrs/src/main.c @@ -261,7 +261,7 @@ void on_conn_params_evt(const struct ble_conn_params_evt *evt) { uint32_t nrf_err; - switch (evt->id) { + switch (evt->evt_type) { case BLE_CONN_PARAMS_EVT_REJECTED: nrf_err = sd_ble_gap_disconnect(evt->conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); diff --git a/samples/bluetooth/ble_nus/src/main.c b/samples/bluetooth/ble_nus/src/main.c index 554480fdfd..5374e9cbb8 100644 --- a/samples/bluetooth/ble_nus/src/main.c +++ b/samples/bluetooth/ble_nus/src/main.c @@ -251,7 +251,7 @@ void on_conn_params_evt(const struct ble_conn_params_evt *evt) { uint32_t nrf_err; - switch (evt->id) { + switch (evt->evt_type) { case BLE_CONN_PARAMS_EVT_REJECTED: nrf_err = sd_ble_gap_disconnect(evt->conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); @@ -316,34 +316,38 @@ uint16_t ble_qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_evt *qwr_ * * @param[in] evt NUS event parameters. */ -static void ble_nus_evt_handler(const struct ble_nus_evt *evt) +static void ble_nus_evt_handler(struct ble_nus *nus, const struct ble_nus_evt *evt) { const char newline = '\n'; nrfx_err_t nrfx_err; - if (evt->type != BLE_NUS_EVT_RX_DATA) { + if (evt->evt_type == BLE_NUS_EVT_ERROR) { + LOG_ERR("NUS error event, error %d", evt->error.reason); + } + + if (evt->evt_type != BLE_NUS_EVT_RX_DATA) { return; } /* Handle incoming data */ LOG_DBG("Received data from BLE NUS: %.*s (%d)", - evt->params.rx_data.length, evt->params.rx_data.data, evt->params.rx_data.length); + evt->rx_data.length, evt->rx_data.data, evt->rx_data.length); #if defined(CONFIG_APP_NUS_LPUARTE) - nrfx_err = bm_lpuarte_tx(&lpu, evt->params.rx_data.data, evt->params.rx_data.length, 3000); + nrfx_err = bm_lpuarte_tx(&lpu, evt->rx_data.data, evt->rx_data.length, 3000); if (nrfx_err != NRFX_SUCCESS) { LOG_ERR("bm_lpuarte_tx failed, nrfx_err %#x", nrfx_err); } #else - nrfx_err = nrfx_uarte_tx(&nus_uarte_inst, evt->params.rx_data.data, - evt->params.rx_data.length, NRFX_UARTE_TX_BLOCKING); + nrfx_err = nrfx_uarte_tx(&nus_uarte_inst, evt->rx_data.data, + evt->rx_data.length, NRFX_UARTE_TX_BLOCKING); if (nrfx_err != NRFX_SUCCESS) { LOG_ERR("nrfx_uarte_tx failed, nrfx_err %#x", nrfx_err); } #endif - if (evt->params.rx_data.data[evt->params.rx_data.length - 1] == '\r') { + if (evt->rx_data.data[evt->rx_data.length - 1] == '\r') { #if defined(CONFIG_APP_NUS_LPUARTE) bm_lpuarte_tx(&lpu, &newline, 1, 3000); #else diff --git a/samples/bluetooth/ble_pwr_profiling/src/main.c b/samples/bluetooth/ble_pwr_profiling/src/main.c index 0d013710cb..d33294466a 100644 --- a/samples/bluetooth/ble_pwr_profiling/src/main.c +++ b/samples/bluetooth/ble_pwr_profiling/src/main.c @@ -365,7 +365,7 @@ static void on_conn_params_evt(const struct ble_conn_params_evt *evt) { uint32_t nrf_err; - switch (evt->id) { + switch (evt->evt_type) { case BLE_CONN_PARAMS_EVT_REJECTED: nrf_err = sd_ble_gap_disconnect(evt->conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); diff --git a/samples/bluetooth/ble_radio_notification/src/main.c b/samples/bluetooth/ble_radio_notification/src/main.c index 6ab62bb18a..1b224ac894 100644 --- a/samples/bluetooth/ble_radio_notification/src/main.c +++ b/samples/bluetooth/ble_radio_notification/src/main.c @@ -46,7 +46,7 @@ static void on_conn_params_evt(const struct ble_conn_params_evt *evt) { uint32_t nrf_err; - switch (evt->id) { + switch (evt->evt_type) { case BLE_CONN_PARAMS_EVT_REJECTED: nrf_err = sd_ble_gap_disconnect(evt->conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); diff --git a/subsys/bluetooth/services/ble_cgms/cgms.c b/subsys/bluetooth/services/ble_cgms/cgms.c index 1896a88dcc..1aba6e47eb 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms.c +++ b/subsys/bluetooth/services/ble_cgms/cgms.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -23,16 +24,20 @@ LOG_MODULE_REGISTER(ble_cgms, CONFIG_BLE_CGMS_LOG_LEVEL); /* GATT errors and nrf_ble_gq errors event handler. */ -static void gatt_error_handler(uint16_t conn_handle, uint32_t nrf_error, void *ctx) +static void ble_gq_evt_handler(const struct ble_gq_req *req, struct ble_gq_evt *gq_evt) { struct ble_cgms_evt evt = { .evt_type = BLE_CGMS_EVT_ERROR, - .error.reason = nrf_error, + .error.reason = gq_evt->error.reason, + .conn_handle = gq_evt->conn_handle, }; - struct ble_cgms *cgms = (struct ble_cgms *)ctx; + struct ble_cgms *cgms = (struct ble_cgms *)req->ctx; - if (cgms->evt_handler && (nrf_error != NRF_ERROR_INVALID_STATE)) { - cgms->evt_handler(cgms, &evt); + if (gq_evt->error.reason != NRF_ERROR_INVALID_STATE) { + LOG_ERR("GATT error, nrf_error %#x", gq_evt->error.reason); + if (cgms->evt_handler) { + cgms->evt_handler(cgms, &evt); + } } } @@ -202,7 +207,7 @@ uint32_t ble_cgms_init(struct ble_cgms *cgms, const struct ble_cgms_config *cgms cgms->is_session_started = false; cgms->nb_run_session = 0; cgms->conn_handle = BLE_CONN_HANDLE_INVALID; - cgms->gatt_err_handler = gatt_error_handler; + cgms->ble_gq_evt_handler = ble_gq_evt_handler; memcpy(cgms->calibration_val[0].value, init_calib_val, BLE_CGMS_MAX_CALIB_LEN); diff --git a/subsys/bluetooth/services/ble_cgms/cgms_meas.c b/subsys/bluetooth/services/ble_cgms/cgms_meas.c index 5daa86a71b..db33b7b430 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_meas.c +++ b/subsys/bluetooth/services/ble_cgms/cgms_meas.c @@ -160,7 +160,9 @@ uint32_t cgms_meas_send(struct ble_cgms *cgms, struct ble_cgms_rec *rec, uint16_ /* Glucose measurement CCCD write event handler */ static void on_meas_cccd_write(struct ble_cgms *cgms, const ble_gatts_evt_write_t *evt_write) { - struct ble_cgms_evt evt; + struct ble_cgms_evt evt = { + .conn_handle = cgms->conn_handle, + }; if (evt_write->len != 2) { return; diff --git a/subsys/bluetooth/services/ble_cgms/cgms_racp.c b/subsys/bluetooth/services/ble_cgms/cgms_racp.c index 29f9235055..ebacfc8f9a 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_racp.c +++ b/subsys/bluetooth/services/ble_cgms/cgms_racp.c @@ -75,6 +75,7 @@ static void racp_send(struct ble_cgms *cgms, struct ble_racp_value *racp_val) struct ble_cgms_evt evt = { .evt_type = BLE_CGMS_EVT_ERROR, + .conn_handle = cgms->conn_handle, }; /* Send indication */ @@ -82,8 +83,8 @@ static void racp_send(struct ble_cgms *cgms, struct ble_racp_value *racp_val) struct ble_gq_req cgms_req = { .type = BLE_GQ_REQ_GATTS_HVX, - .error_handler.cb = cgms->gatt_err_handler, - .error_handler.ctx = cgms, + .evt_handler = cgms->ble_gq_evt_handler, + .ctx = cgms, .gatts_hvx.type = BLE_GATT_HVX_INDICATION, .gatts_hvx.handle = cgms->char_handles.racp.value_handle, .gatts_hvx.offset = 0, @@ -93,13 +94,16 @@ static void racp_send(struct ble_cgms *cgms, struct ble_racp_value *racp_val) nrf_err = ble_gq_item_add(cgms->gatt_queue, &cgms_req, cgms->conn_handle); - /* Report error to application */ - if ((cgms->evt_handler != NULL) && - (nrf_err) && + if ((nrf_err != NRF_SUCCESS) && (nrf_err != NRF_ERROR_INVALID_STATE)) { - evt.error.reason = nrf_err; - cgms->evt_handler(cgms, &evt); + LOG_ERR("Failed to add item to gatt queue, nrf_error %#x", nrf_err); + /* Report error to application */ + if (cgms->evt_handler != NULL) { + evt.error.reason = nrf_err; + cgms->evt_handler(cgms, &evt); + } } + } static void racp_response_code_send(struct ble_cgms *cgms, uint8_t racp_opcode, uint8_t value) @@ -295,6 +299,7 @@ static void racp_report_records_procedure(struct ble_cgms *cgms) uint32_t nrf_err = NRF_SUCCESS; struct ble_cgms_evt evt = { .evt_type = BLE_CGMS_EVT_ERROR, + .conn_handle = cgms->conn_handle, }; while (cgms->racp_data.racp_processing_active) { @@ -316,6 +321,7 @@ static void racp_report_records_procedure(struct ble_cgms *cgms) nrf_err = racp_report_records_less_equal(cgms); break; default: + LOG_ERR("Unknown RACP operator %d", cgms->racp_data.racp_proc_operator); /* Report error to application */ if (cgms->evt_handler != NULL) { evt.error.reason = NRF_ERROR_INTERNAL; @@ -344,6 +350,7 @@ static void racp_report_records_procedure(struct ble_cgms *cgms) return; default: + LOG_ERR("Unhandled RACP error, nrf_error %#x", nrf_err); /* Report error to application. */ if (cgms->evt_handler != NULL) { evt.error.reason = NRF_ERROR_INTERNAL; @@ -592,6 +599,7 @@ void cgms_racp_on_rw_auth_req(struct ble_cgms *cgms, uint32_t nrf_err; struct ble_cgms_evt cgms_evt = { .evt_type = BLE_CGMS_EVT_ERROR, + .conn_handle = cgms->conn_handle, }; ble_gatts_rw_authorize_reply_params_t auth_reply = { .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE, @@ -617,6 +625,7 @@ void cgms_racp_on_rw_auth_req(struct ble_cgms *cgms, nrf_err = sd_ble_gatts_rw_authorize_reply(cgms->conn_handle, &auth_reply); if (nrf_err) { + LOG_ERR("sd_ble_gatts_rw_authorize_reply failed, nrf_error %#x", nrf_err); if (cgms->evt_handler != NULL) { cgms_evt.error.reason = nrf_err; cgms->evt_handler(cgms, &cgms_evt); diff --git a/subsys/bluetooth/services/ble_cgms/cgms_socp.c b/subsys/bluetooth/services/ble_cgms/cgms_socp.c index c6747bd173..61102d502f 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_socp.c +++ b/subsys/bluetooth/services/ble_cgms/cgms_socp.c @@ -349,6 +349,7 @@ static void socp_send(struct ble_cgms *cgms) uint16_t len; struct ble_cgms_evt cgms_evt = { .evt_type = BLE_CGMS_EVT_ERROR, + .conn_handle = cgms->conn_handle, }; /* Send indication. */ @@ -356,8 +357,8 @@ static void socp_send(struct ble_cgms *cgms) struct ble_gq_req cgms_req = { .type = BLE_GQ_REQ_GATTS_HVX, - .error_handler.cb = cgms->gatt_err_handler, - .error_handler.ctx = cgms, + .evt_handler = cgms->ble_gq_evt_handler, + .ctx = cgms, .gatts_hvx.type = BLE_GATT_HVX_INDICATION, .gatts_hvx.handle = cgms->char_handles.socp.value_handle, .gatts_hvx.offset = 0, @@ -366,14 +367,15 @@ static void socp_send(struct ble_cgms *cgms) }; nrf_err = ble_gq_item_add(cgms->gatt_queue, &cgms_req, cgms->conn_handle); - - /* Report error to application. */ - if ((cgms->evt_handler != NULL) && - (nrf_err) && - (nrf_err != NRF_ERROR_INVALID_STATE)) { - cgms_evt.error.reason = nrf_err; - cgms->evt_handler(cgms, &cgms_evt); + if ((nrf_err) && (nrf_err != NRF_ERROR_INVALID_STATE)) { + LOG_ERR("Failed to add item to gatt queue, nrf_error %#x", nrf_err); + /* Report error to application. */ + if (cgms->evt_handler != NULL) { + cgms_evt.error.reason = nrf_err; + cgms->evt_handler(cgms, &cgms_evt); + } } + } static bool is_feature_present(struct ble_cgms *cgms, uint32_t feature) @@ -386,7 +388,9 @@ static void on_socp_value_write(struct ble_cgms *cgms, const ble_gatts_evt_write { uint32_t nrf_err; struct ble_cgms_socp_value socp_request; - struct ble_cgms_evt evt; + struct ble_cgms_evt evt = { + .conn_handle = cgms->conn_handle, + }; struct ble_cgms_sst sst = {0}; /* Decode request. */ @@ -428,6 +432,7 @@ static void on_socp_value_write(struct ble_cgms *cgms, const ble_gatts_evt_write nrf_err = cgms_sst_set(cgms, &sst); if (nrf_err) { + LOG_ERR("Failed to set SST, nrf_error %#x", nrf_err); if (cgms->evt_handler != NULL) { evt.evt_type = BLE_CGMS_EVT_ERROR; evt.error.reason = nrf_err; @@ -442,6 +447,7 @@ static void on_socp_value_write(struct ble_cgms *cgms, const ble_gatts_evt_write cgms->sensor_status.status.status &= (~BLE_CGMS_STATUS_SESSION_STOPPED); nrf_err = ble_cgms_update_status(cgms, &cgms->sensor_status); if (nrf_err) { + LOG_ERR("Failed to update CGMS status, nrf_error %#x", nrf_err); if (cgms->evt_handler != NULL) { evt.evt_type = BLE_CGMS_EVT_ERROR; evt.error.reason = nrf_err; @@ -468,6 +474,7 @@ static void on_socp_value_write(struct ble_cgms *cgms, const ble_gatts_evt_write } nrf_err = ble_cgms_update_status(cgms, &status); if (nrf_err) { + LOG_ERR("Failed to update CGMS status, nrf_error %#x", nrf_err); if (cgms->evt_handler != NULL) { evt.evt_type = BLE_CGMS_EVT_ERROR; evt.error.reason = nrf_err; @@ -490,6 +497,7 @@ void cgms_socp_on_rw_auth_req(struct ble_cgms *cgms, uint32_t nrf_err; struct ble_cgms_evt cgms_evt = { .evt_type = BLE_CGMS_EVT_ERROR, + .conn_handle = cgms->conn_handle, }; ble_gatts_rw_authorize_reply_params_t auth_reply = { .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE, @@ -516,9 +524,9 @@ void cgms_socp_on_rw_auth_req(struct ble_cgms *cgms, nrf_err = sd_ble_gatts_rw_authorize_reply(cgms->conn_handle, &auth_reply); if (nrf_err) { + LOG_ERR("Failed to reply authorize rw request, nrf_error %#x", nrf_err); if (cgms->evt_handler != NULL) { cgms_evt.error.reason = nrf_err; - cgms_evt.evt_type = BLE_CGMS_EVT_ERROR; cgms->evt_handler(cgms, &cgms_evt); } return; diff --git a/subsys/bluetooth/services/ble_cgms/cgms_sst.c b/subsys/bluetooth/services/ble_cgms/cgms_sst.c index 6abb97057d..be12077543 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_sst.c +++ b/subsys/bluetooth/services/ble_cgms/cgms_sst.c @@ -122,6 +122,7 @@ static void on_sst_value_write(struct ble_cgms *cgms, const ble_gatts_evt_write_ }; struct ble_cgms_evt cgms_evt = { .evt_type = BLE_CGMS_EVT_ERROR, + .conn_handle = cgms->conn_handle, }; nrf_err = sd_ble_gatts_rw_authorize_reply(cgms->conn_handle, &auth_reply); @@ -134,6 +135,7 @@ static void on_sst_value_write(struct ble_cgms *cgms, const ble_gatts_evt_write_ nrf_err = cgm_update_sst(cgms, evt_write); if (nrf_err) { + LOG_ERR("Failed to update SST, nrf_error %#x", nrf_err); if (cgms->evt_handler != NULL) { cgms_evt.error.reason = nrf_err; cgms->evt_handler(cgms, &cgms_evt); diff --git a/subsys/bluetooth/services/ble_hids/hids.c b/subsys/bluetooth/services/ble_hids/hids.c index 4008abe4d4..a0e55f1e44 100644 --- a/subsys/bluetooth/services/ble_hids/hids.c +++ b/subsys/bluetooth/services/ble_hids/hids.c @@ -120,8 +120,10 @@ static void on_connect(struct ble_hids *hids, ble_evt_t const *ble_evt) struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gap_evt.conn_handle, }; + LOG_ERR("Failed to get link context, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } @@ -149,8 +151,10 @@ static void on_connect(struct ble_hids *hids, ble_evt_t const *ble_evt) struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gap_evt.conn_handle, }; + LOG_ERR("Failed to set GATTS value, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } } @@ -167,8 +171,10 @@ static void on_control_point_write(struct ble_hids *hids, ble_evt_t const *ble_e struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, }; + LOG_ERR("Failed to get link context, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } @@ -177,7 +183,9 @@ static void on_control_point_write(struct ble_hids *hids, ble_evt_t const *ble_e } if (evt_write->len == 1) { - struct ble_hids_evt evt; + struct ble_hids_evt evt = { + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, + }; switch (evt_write->data[0]) { case HIDS_CONTROL_POINT_SUSPEND: @@ -215,8 +223,10 @@ static void on_protocol_mode_write(struct ble_hids *hids, ble_evt_t const *ble_e struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, }; + LOG_ERR("Failed to get link context, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } @@ -225,7 +235,9 @@ static void on_protocol_mode_write(struct ble_hids *hids, ble_evt_t const *ble_e } if (evt_write->len == 1) { - struct ble_hids_evt evt; + struct ble_hids_evt evt = { + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, + }; switch (evt_write->data[0]) { case PROTOCOL_MODE_BOOT: @@ -265,8 +277,10 @@ void on_protocol_mode_read_auth(struct ble_hids *hids, ble_evt_t const *ble_evt) struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, }; + LOG_ERR("Failed to get link context, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } @@ -287,8 +301,10 @@ void on_protocol_mode_read_auth(struct ble_hids *hids, ble_evt_t const *ble_evt) struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, }; + LOG_ERR("Failed to authorize rw request, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } } @@ -302,7 +318,9 @@ static void on_report_cccd_write(struct ble_hids *hids, struct ble_hids_char_id if (evt_write->len == 2) { /* CCCD written, update notification state */ if (hids->evt_handler != NULL) { - struct ble_hids_evt evt; + struct ble_hids_evt evt = { + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, + }; if (is_notification_enabled(evt_write->data)) { evt.evt_type = BLE_HIDS_EVT_NOTIF_ENABLED; @@ -332,8 +350,10 @@ static void on_report_value_write(struct ble_hids *hids, ble_evt_t const *ble_ev struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, }; + LOG_ERR("Failed to get link context, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } @@ -347,7 +367,9 @@ static void on_report_value_write(struct ble_hids *hids, ble_evt_t const *ble_ev /* Notify the applicartion */ if (hids->evt_handler != NULL) { - struct ble_hids_evt evt; + struct ble_hids_evt evt = { + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, + }; evt.evt_type = BLE_HIDS_EVT_REP_CHAR_WRITE; evt.params.char_write.char_id = *char_id; @@ -375,8 +397,10 @@ static void on_report_value_read_auth(struct ble_hids *hids, struct ble_hids_cha struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gatts_evt.conn_handle, }; + LOG_ERR("Failed to get link context, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } @@ -400,8 +424,10 @@ static void on_report_value_read_auth(struct ble_hids *hids, struct ble_hids_cha struct ble_hids_evt evt = { .evt_type = BLE_HIDS_EVT_ERROR, .params.error.reason = nrf_err, + .conn_handle = ble_evt->evt.gap_evt.conn_handle, }; + LOG_ERR("Failed to authorize rw request, nrf_error %#x", nrf_err); hids->evt_handler(hids, &evt); } } else { @@ -409,7 +435,9 @@ static void on_report_value_read_auth(struct ble_hids *hids, struct ble_hids_cha } if (hids->evt_handler != NULL) { - struct ble_hids_evt evt; + struct ble_hids_evt evt = { + .conn_handle = ble_evt->evt.gap_evt.conn_handle, + }; evt.evt_type = BLE_HIDS_EVT_REPORT_READ; evt.params.char_auth_read.char_id = *char_id; diff --git a/subsys/bluetooth/services/ble_hrs/hrs.c b/subsys/bluetooth/services/ble_hrs/hrs.c index 43b74a2733..df4490efa1 100644 --- a/subsys/bluetooth/services/ble_hrs/hrs.c +++ b/subsys/bluetooth/services/ble_hrs/hrs.c @@ -164,7 +164,9 @@ static void on_disconnect(struct ble_hrs *hrs, const ble_gap_evt_t *gap_evt) static void on_write(struct ble_hrs *hrs, const ble_gatts_evt_t *gatts_evt) { - struct ble_hrs_evt hrs_evt; + struct ble_hrs_evt hrs_evt = { + .conn_handle = gatts_evt->conn_handle, + }; if (!hrs->evt_handler) { return; @@ -381,7 +383,7 @@ void ble_hrs_conn_params_evt(struct ble_hrs *hrs, const struct ble_conn_params_e __ASSERT(conn_params_evt, "GATT event is NULL"); if ((hrs->conn_handle == conn_params_evt->conn_handle) - && (conn_params_evt->id == BLE_CONN_PARAMS_EVT_ATT_MTU_UPDATED)) { + && (conn_params_evt->evt_type == BLE_CONN_PARAMS_EVT_ATT_MTU_UPDATED)) { hrs->max_hrm_len = MAX_HRM_LEN_CALC(conn_params_evt->att_mtu); } } diff --git a/subsys/bluetooth/services/ble_lbs/lbs.c b/subsys/bluetooth/services/ble_lbs/lbs.c index 6f7f14a177..efd5fd770b 100644 --- a/subsys/bluetooth/services/ble_lbs/lbs.c +++ b/subsys/bluetooth/services/ble_lbs/lbs.c @@ -21,7 +21,7 @@ static void on_write(struct ble_lbs *lbs, const ble_evt_t *ble_evt) ble_gatts_evt_t const *gatts_evt = &ble_evt->evt.gatts_evt; struct ble_lbs_evt lbs_evt = { .evt_type = BLE_LBS_EVT_LED_WRITE, - .led_write.conn_handle = gatts_evt->conn_handle, + .conn_handle = gatts_evt->conn_handle, }; if ((gatts_evt->params.write.handle != lbs->led_char_handles.value_handle) || diff --git a/subsys/bluetooth/services/ble_nus/nus.c b/subsys/bluetooth/services/ble_nus/nus.c index 7765916a6d..a2bf91290a 100644 --- a/subsys/bluetooth/services/ble_nus/nus.c +++ b/subsys/bluetooth/services/ble_nus/nus.c @@ -104,8 +104,7 @@ static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt) uint32_t nrf_err; const uint16_t conn_handle = ble_evt->evt.gap_evt.conn_handle; struct ble_nus_evt evt = { - .type = BLE_NUS_EVT_COMM_STARTED, - .nus = nus, + .evt_type = BLE_NUS_EVT_COMM_STARTED, .conn_handle = conn_handle, }; uint8_t cccd_value[2]; @@ -119,6 +118,11 @@ static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt) ctx = ble_nus_client_context_get(nus, conn_handle); if (ctx == NULL) { LOG_ERR("Could not fetch nus context for connection handle %#x", conn_handle); + evt.evt_type = BLE_NUS_EVT_ERROR; + evt.error.reason = NRF_ERROR_NOT_FOUND; + if (nus->evt_handler != NULL) { + nus->evt_handler(nus, &evt); + } } /* Check the hosts CCCD value to inform of readiness to send data using the @@ -132,7 +136,7 @@ static void on_connect(struct ble_nus *nus, ble_evt_t const *ble_evt) } evt.link_ctx = ctx; - nus->evt_handler(&evt); + nus->evt_handler(nus, &evt); } } @@ -147,7 +151,6 @@ static void on_write(struct ble_nus *nus, ble_evt_t const *ble_evt) const uint16_t conn_handle = ble_evt->evt.gatts_evt.conn_handle; const ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write; struct ble_nus_evt evt = { - .nus = nus, .conn_handle = conn_handle, }; struct ble_nus_client_context *ctx; @@ -155,6 +158,11 @@ static void on_write(struct ble_nus *nus, ble_evt_t const *ble_evt) ctx = ble_nus_client_context_get(nus, conn_handle); if (ctx == NULL) { LOG_ERR("Could not fetch nus context for connection handle %#x", conn_handle); + evt.evt_type = BLE_NUS_EVT_ERROR; + evt.error.reason = NRF_ERROR_NOT_FOUND; + if (nus->evt_handler != NULL) { + nus->evt_handler(nus, &evt); + } } LOG_DBG("Link ctx %p", ctx); @@ -164,23 +172,23 @@ static void on_write(struct ble_nus *nus, ble_evt_t const *ble_evt) if (ctx != NULL) { if (is_notification_enabled(evt_write->data)) { ctx->is_notification_enabled = true; - evt.type = BLE_NUS_EVT_COMM_STARTED; + evt.evt_type = BLE_NUS_EVT_COMM_STARTED; } else { ctx->is_notification_enabled = false; - evt.type = BLE_NUS_EVT_COMM_STOPPED; + evt.evt_type = BLE_NUS_EVT_COMM_STOPPED; } if (nus->evt_handler != NULL) { - nus->evt_handler(&evt); + nus->evt_handler(nus, &evt); } } } else if ((evt_write->handle == nus->rx_handles.value_handle) && (nus->evt_handler != NULL)) { - evt.type = BLE_NUS_EVT_RX_DATA; - evt.params.rx_data.data = evt_write->data; - evt.params.rx_data.length = evt_write->len; + evt.evt_type = BLE_NUS_EVT_RX_DATA; + evt.rx_data.data = evt_write->data; + evt.rx_data.length = evt_write->len; - nus->evt_handler(&evt); + nus->evt_handler(nus, &evt); } else { /* Do nothing. This event is not relevant for this service. */ } @@ -196,8 +204,7 @@ static void on_hvx_tx_complete(struct ble_nus *nus, ble_evt_t const *ble_evt) { const uint16_t conn_handle = ble_evt->evt.gatts_evt.conn_handle; struct ble_nus_evt evt = { - .type = BLE_NUS_EVT_TX_RDY, - .nus = nus, + .evt_type = BLE_NUS_EVT_TX_RDY, .conn_handle = conn_handle, }; struct ble_nus_client_context *ctx; @@ -210,7 +217,7 @@ static void on_hvx_tx_complete(struct ble_nus *nus, ble_evt_t const *ble_evt) if ((ctx->is_notification_enabled) && (nus->evt_handler != NULL)) { evt.link_ctx = ctx; - nus->evt_handler(&evt); + nus->evt_handler(nus, &evt); } } diff --git a/tests/subsys/bluetooth/services/ble_nus/src/unity_test.c b/tests/subsys/bluetooth/services/ble_nus/src/unity_test.c index a11f0b8dae..b2d4746dc0 100644 --- a/tests/subsys/bluetooth/services/ble_nus/src/unity_test.c +++ b/tests/subsys/bluetooth/services/ble_nus/src/unity_test.c @@ -85,47 +85,49 @@ static uint32_t stub_sd_ble_gatts_value_get_err(uint16_t conn_handle, uint16_t h } } -static void ble_nus_evt_handler_on_connect(const struct ble_nus_evt *evt) +static void ble_nus_evt_handler_on_connect(struct ble_nus *nus, const struct ble_nus_evt *evt) { last_link_ctx = evt->link_ctx; - TEST_ASSERT_EQUAL(BLE_NUS_EVT_COMM_STARTED, evt->type); + TEST_ASSERT_EQUAL(BLE_NUS_EVT_COMM_STARTED, evt->evt_type); TEST_ASSERT_TRUE(evt->link_ctx->is_notification_enabled); evt_handler_called = true; } -static void ble_nus_evt_handler_on_connect_null_ctx(const struct ble_nus_evt *evt) +static void ble_nus_evt_handler_on_connect_null_ctx(struct ble_nus *nus, + const struct ble_nus_evt *evt) { - TEST_ASSERT_EQUAL(BLE_NUS_EVT_COMM_STARTED, evt->type); + TEST_ASSERT_EQUAL(BLE_NUS_EVT_ERROR, evt->evt_type); TEST_ASSERT_NULL(evt->link_ctx); evt_handler_called = true; } -static void ble_nus_evt_handler_on_write_notif(const struct ble_nus_evt *evt) +static void ble_nus_evt_handler_on_write_notif(struct ble_nus *nus, const struct ble_nus_evt *evt) { - TEST_ASSERT_EQUAL(BLE_NUS_EVT_COMM_STARTED, evt->type); + TEST_ASSERT_EQUAL(BLE_NUS_EVT_COMM_STARTED, evt->evt_type); TEST_ASSERT_TRUE(evt->link_ctx->is_notification_enabled); evt_handler_called = true; } -static void ble_nus_evt_handler_on_write_indica(const struct ble_nus_evt *evt) +static void ble_nus_evt_handler_on_write_indica(struct ble_nus *nus, const struct ble_nus_evt *evt) { - TEST_ASSERT_EQUAL(BLE_NUS_EVT_COMM_STOPPED, evt->type); + TEST_ASSERT_EQUAL(BLE_NUS_EVT_COMM_STOPPED, evt->evt_type); TEST_ASSERT_FALSE(evt->link_ctx->is_notification_enabled); evt_handler_called = true; } -static void ble_nus_evt_handler_on_write_value(const struct ble_nus_evt *evt) +static void ble_nus_evt_handler_on_write_value(struct ble_nus *nus, const struct ble_nus_evt *evt) { - TEST_ASSERT_EQUAL(BLE_NUS_EVT_RX_DATA, evt->type); - TEST_ASSERT_EQUAL(0xAB, evt->params.rx_data.data[0]); - TEST_ASSERT_EQUAL(0xCD, evt->params.rx_data.data[1]); - TEST_ASSERT_EQUAL(2, evt->params.rx_data.length); + TEST_ASSERT_EQUAL(BLE_NUS_EVT_RX_DATA, evt->evt_type); + TEST_ASSERT_EQUAL(0xAB, evt->rx_data.data[0]); + TEST_ASSERT_EQUAL(0xCD, evt->rx_data.data[1]); + TEST_ASSERT_EQUAL(2, evt->rx_data.length); evt_handler_called = true; } -static void ble_nus_evt_handler_on_hvx_tx_complete(const struct ble_nus_evt *evt) +static void ble_nus_evt_handler_on_hvx_tx_complete(struct ble_nus *nus, + const struct ble_nus_evt *evt) { - TEST_ASSERT_EQUAL(BLE_NUS_EVT_TX_RDY, evt->type); + TEST_ASSERT_EQUAL(BLE_NUS_EVT_TX_RDY, evt->evt_type); TEST_ASSERT_EQUAL_PTR(last_link_ctx, evt->link_ctx); evt_handler_called = true; } @@ -323,11 +325,11 @@ void test_ble_nus_on_ble_evt_gap_evt_on_write(void) TEST_ASSERT_TRUE(evt_handler_called); evt_handler_called = false; - ble_nus.evt_handler = ble_nus_evt_handler_on_write_indica; + ble_nus.evt_handler = ble_nus_evt_handler_on_connect_null_ctx; __cmock_nrf_sdh_ble_idx_get_ExpectAndReturn(test_case_conn_handle, -1); ble_nus_on_ble_evt(&ble_evt, &ble_nus); - TEST_ASSERT_FALSE(evt_handler_called); + TEST_ASSERT_TRUE(evt_handler_called); uint8_t *const data_ptr = ble_evt.evt.gatts_evt.params.write.data;