Skip to content

Commit 4a66a4b

Browse files
lib: bluetooth: ble_gq: align event handler
Align event handler for ble_gq with other libraries. Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent 551eabf commit 4a66a4b

File tree

7 files changed

+60
-27
lines changed

7 files changed

+60
-27
lines changed

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ Libraries
115115

116116
* Added the error event to align event handling with other services.
117117

118+
* BLE Gatt Queue library:
119+
120+
* Updated the event handling to align with other libraries.
121+
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.evt_handler_ctx` context.
122+
118123
Samples
119124
=======
120125

include/bm/bluetooth/ble_gq.h

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,40 @@ enum ble_gq_req_type {
129129
};
130130

131131
/**
132-
* @brief Error handler type.
132+
* @brief Advertising event types.
133133
*/
134-
typedef void (*ble_gq_req_error_cb_t)(uint16_t conn_handle, uint32_t nrf_error, void *context);
135-
136-
/**
137-
* @brief Structure used to handle SoftDevice error.
138-
*/
139-
struct ble_gq_req_error_handler {
140-
/**
141-
* @brief Error handler to be called in case of an error from SoftDevice.
142-
*/
143-
ble_gq_req_error_cb_t cb;
134+
enum ble_gq_evt_type {
144135
/**
145-
* @brief Parameter passed to the error handler;
136+
* @brief Error.
146137
*/
147-
void *ctx;
138+
BLE_GQ_EVT_ERROR,
139+
};
140+
141+
/** @brief Gatt Queue event. */
142+
struct ble_gq_evt {
143+
/** @brief Advertising event type. */
144+
enum ble_gq_evt_type evt_type;
145+
union {
146+
/** @ref BLE_GQ_EVT_ERROR event data. */
147+
struct {
148+
/** Connection handle. */
149+
uint16_t conn_handle;
150+
/** Event result code. */
151+
uint32_t reason;
152+
/** User provided context. */
153+
void *ctx;
154+
} error;
155+
};
148156
};
149157

158+
/* Forward declaration */
159+
struct ble_gq_req;
160+
161+
/**
162+
* @brief Event handler type.
163+
*/
164+
typedef void (*ble_gq_evt_handler_t)(const struct ble_gq_req *req, struct ble_gq_evt *evt);
165+
150166
/**
151167
* @brief Structure to hold a BLE GATT request.
152168
*/
@@ -168,7 +184,11 @@ struct ble_gq_req {
168184
/**
169185
* @brief Error handler structure.
170186
*/
171-
struct ble_gq_req_error_handler error_handler;
187+
ble_gq_evt_handler_t evt_handler;
188+
/**
189+
* @brief Context passed to the event handler.
190+
*/
191+
void *evt_handler_ctx;
172192
/**
173193
* @brief Request type specific parameters.
174194
*/

include/bm/bluetooth/services/ble_cgms.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ struct ble_cgms {
464464
/** Pointer to BLE GATT Queue instance. */
465465
const struct ble_gq *gatt_queue;
466466
/** Error handler to be called in case of an error from SoftDevice. */
467-
ble_gq_req_error_cb_t gatt_err_handler;
467+
ble_gq_evt_handler_t ble_gq_evt_handler;
468468
/** Handle of the CGM Service (as provided by the BLE stack). */
469469
uint16_t service_handle;
470470
/** GATTS characteristic handles for the different characteristics in the service. */

lib/bluetooth/ble_gq/gatt_queue.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,21 @@ static const req_data_store_t req_data_store[BLE_GQ_REQ_NUM] = {
106106
static void request_error_handle(const struct ble_gq_req *req, uint16_t conn_handle,
107107
uint32_t nrf_err)
108108
{
109+
struct ble_gq_evt evt = {
110+
.evt_type = BLE_GQ_EVT_ERROR,
111+
.error.conn_handle = conn_handle,
112+
.error.ctx = req->evt_handler_ctx,
113+
.error.reason = nrf_err,
114+
};
115+
109116
if (nrf_err == NRF_SUCCESS) {
110117
LOG_DBG("SD GATT procedure (%d) succeeded on connection handle: %d.", req->type,
111118
conn_handle);
112119
} else {
113120
LOG_DBG("SD GATT procedure (%d) failed on connection handle %d with nrf_error %#x",
114121
req->type, conn_handle, nrf_err);
115-
if (req->error_handler.cb != NULL) {
116-
req->error_handler.cb(conn_handle, nrf_err, req->error_handler.ctx);
122+
if (req->evt_handler != NULL) {
123+
req->evt_handler(req, &evt);
117124
}
118125
}
119126
}

subsys/bluetooth/services/ble_cgms/cgms.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <nrf_error.h>
88
#include <stdint.h>
99

10+
#include <bm/bluetooth/ble_gq.h>
1011
#include <bm/bluetooth/ble_racp.h>
1112
#include <bm/bluetooth/services/ble_date_time.h>
1213
#include <bm/bluetooth/services/ble_cgms.h>
@@ -23,16 +24,16 @@
2324
LOG_MODULE_REGISTER(ble_cgms, CONFIG_BLE_CGMS_LOG_LEVEL);
2425

2526
/* GATT errors and nrf_ble_gq errors event handler. */
26-
static void gatt_error_handler(uint16_t conn_handle, uint32_t nrf_error, void *ctx)
27+
static void ble_gq_evt_handler(const struct ble_gq_req *req, struct ble_gq_evt *gq_evt)
2728
{
2829
struct ble_cgms_evt evt = {
2930
.evt_type = BLE_CGMS_EVT_ERROR,
30-
.error.reason = nrf_error,
31+
.error.reason = gq_evt->error.reason,
3132
};
32-
struct ble_cgms *cgms = (struct ble_cgms *)ctx;
33+
struct ble_cgms *cgms = (struct ble_cgms *)gq_evt->error.ctx;
3334

34-
if (nrf_error != NRF_ERROR_INVALID_STATE) {
35-
LOG_ERR("GATT error, nrf_error %d", nrf_err);
35+
if (gq_evt->error.reason != NRF_ERROR_INVALID_STATE) {
36+
LOG_ERR("GATT error, nrf_error %d", gq_evt->error.reason);
3637
if (cgms->evt_handler) {
3738
cgms->evt_handler(cgms, &evt);
3839
}
@@ -205,7 +206,7 @@ uint32_t ble_cgms_init(struct ble_cgms *cgms, const struct ble_cgms_config *cgms
205206
cgms->is_session_started = false;
206207
cgms->nb_run_session = 0;
207208
cgms->conn_handle = BLE_CONN_HANDLE_INVALID;
208-
cgms->gatt_err_handler = gatt_error_handler;
209+
cgms->ble_gq_evt_handler = ble_gq_evt_handler;
209210

210211
memcpy(cgms->calibration_val[0].value, init_calib_val, BLE_CGMS_MAX_CALIB_LEN);
211212

subsys/bluetooth/services/ble_cgms/cgms_racp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ static void racp_send(struct ble_cgms *cgms, struct ble_racp_value *racp_val)
8282

8383
struct ble_gq_req cgms_req = {
8484
.type = BLE_GQ_REQ_GATTS_HVX,
85-
.error_handler.cb = cgms->gatt_err_handler,
86-
.error_handler.ctx = cgms,
85+
.evt_handler = cgms->ble_gq_evt_handler,
86+
.evt_handler_ctx = cgms,
8787
.gatts_hvx.type = BLE_GATT_HVX_INDICATION,
8888
.gatts_hvx.handle = cgms->char_handles.racp.value_handle,
8989
.gatts_hvx.offset = 0,

subsys/bluetooth/services/ble_cgms/cgms_socp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ static void socp_send(struct ble_cgms *cgms)
356356

357357
struct ble_gq_req cgms_req = {
358358
.type = BLE_GQ_REQ_GATTS_HVX,
359-
.error_handler.cb = cgms->gatt_err_handler,
360-
.error_handler.ctx = cgms,
359+
.evt_handler = cgms->ble_gq_evt_handler,
360+
.evt_handler_ctx = cgms,
361361
.gatts_hvx.type = BLE_GATT_HVX_INDICATION,
362362
.gatts_hvx.handle = cgms->char_handles.socp.value_handle,
363363
.gatts_hvx.offset = 0,

0 commit comments

Comments
 (0)