Skip to content

Commit d08a2a5

Browse files
committed
bluetooth: gatt: add err param to discover cb
Add an error parameter to the discovery callback so that users can differentiate between an error occurring in the discovery process and the attribute simply not existing. Currently both situations are provided to the user as `attr == NULL`. Signed-off-by: Jordan Yates <[email protected]>
1 parent b4abd83 commit d08a2a5

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

include/zephyr/bluetooth/gatt.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,7 @@ struct bt_gatt_discover_params;
15581558
* @param conn Connection object.
15591559
* @param attr Attribute found, or NULL if not found.
15601560
* @param params Discovery parameters given.
1561+
* @param err 0 on success, -ECONNRESET on connection lost, -EPROTO on invalid data
15611562
*
15621563
* If discovery procedure has completed this callback will be called with
15631564
* attr set to NULL. This will not happen if procedure was stopped by returning
@@ -1598,7 +1599,8 @@ struct bt_gatt_discover_params;
15981599
*/
15991600
typedef uint8_t (*bt_gatt_discover_func_t)(struct bt_conn *conn,
16001601
const struct bt_gatt_attr *attr,
1601-
struct bt_gatt_discover_params *params);
1602+
struct bt_gatt_discover_params *params,
1603+
int err);
16021604

16031605
/** GATT Discover types */
16041606
enum {

subsys/bluetooth/host/gatt.c

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,7 +3843,7 @@ static void gatt_discover_next(struct bt_conn *conn, uint16_t last_handle,
38433843
}
38443844

38453845
done:
3846-
params->func(conn, NULL, params);
3846+
params->func(conn, NULL, params, 0);
38473847
}
38483848

38493849
static void gatt_find_type_rsp(struct bt_conn *conn, int err,
@@ -3857,7 +3857,10 @@ static void gatt_find_type_rsp(struct bt_conn *conn, int err,
38573857

38583858
LOG_DBG("err %d", err);
38593859

3860-
if (err || (length % sizeof(struct bt_att_handle_group) != 0)) {
3860+
if (length % sizeof(struct bt_att_handle_group) != 0) {
3861+
err = -EPROTO;
3862+
}
3863+
if (err) {
38613864
goto done;
38623865
}
38633866

@@ -3890,7 +3893,7 @@ static void gatt_find_type_rsp(struct bt_conn *conn, int err,
38903893
.handle = start_handle,
38913894
};
38923895

3893-
if (params->func(conn, &attr, params) == BT_GATT_ITER_STOP) {
3896+
if (params->func(conn, &attr, params, 0) == BT_GATT_ITER_STOP) {
38943897
return;
38953898
}
38963899
}
@@ -3899,7 +3902,7 @@ static void gatt_find_type_rsp(struct bt_conn *conn, int err,
38993902

39003903
return;
39013904
done:
3902-
params->func(conn, NULL, params);
3905+
params->func(conn, NULL, params, err);
39033906
}
39043907

39053908
static int gatt_find_type_encode(struct net_buf *buf, size_t len,
@@ -3975,7 +3978,7 @@ static void read_included_uuid_cb(struct bt_conn *conn, int err,
39753978

39763979
if (length != 16U) {
39773980
LOG_ERR("Invalid data len %u", length);
3978-
params->func(conn, NULL, params);
3981+
params->func(conn, NULL, params, -EINVAL);
39793982
return;
39803983
}
39813984

@@ -4001,7 +4004,7 @@ static void read_included_uuid_cb(struct bt_conn *conn, int err,
40014004
.handle = handle,
40024005
};
40034006

4004-
if (params->func(conn, &attr, params) == BT_GATT_ITER_STOP) {
4007+
if (params->func(conn, &attr, params, 0) == BT_GATT_ITER_STOP) {
40054008
return;
40064009
}
40074010
next:
@@ -4044,6 +4047,7 @@ static uint16_t parse_include(struct bt_conn *conn, const void *pdu,
40444047
struct bt_uuid_16 u16;
40454048
struct bt_uuid_128 u128;
40464049
} u;
4050+
int err = -EPROTO;
40474051

40484052
if (length < sizeof(*rsp)) {
40494053
LOG_WRN("Parse err");
@@ -4117,7 +4121,7 @@ static uint16_t parse_include(struct bt_conn *conn, const void *pdu,
41174121
.handle = handle,
41184122
};
41194123

4120-
if (params->func(conn, &attr, params) == BT_GATT_ITER_STOP) {
4124+
if (params->func(conn, &attr, params, 0) == BT_GATT_ITER_STOP) {
41214125
return 0;
41224126
}
41234127
}
@@ -4127,8 +4131,9 @@ static uint16_t parse_include(struct bt_conn *conn, const void *pdu,
41274131
return handle;
41284132
}
41294133

4134+
err = 0;
41304135
done:
4131-
params->func(conn, NULL, params);
4136+
params->func(conn, NULL, params, err);
41324137
return 0;
41334138
}
41344139

@@ -4143,6 +4148,7 @@ static uint16_t parse_characteristic(struct bt_conn *conn, const void *pdu,
41434148
struct bt_uuid_16 u16;
41444149
struct bt_uuid_128 u128;
41454150
} u;
4151+
int err = -EPROTO;
41464152

41474153
if (length < sizeof(*rsp)) {
41484154
LOG_WRN("Parse err");
@@ -4205,7 +4211,7 @@ static uint16_t parse_characteristic(struct bt_conn *conn, const void *pdu,
42054211
.handle = handle,
42064212
};
42074213

4208-
if (params->func(conn, &attr, params) == BT_GATT_ITER_STOP) {
4214+
if (params->func(conn, &attr, params, 0) == BT_GATT_ITER_STOP) {
42094215
return 0;
42104216
}
42114217
}
@@ -4215,8 +4221,9 @@ static uint16_t parse_characteristic(struct bt_conn *conn, const void *pdu,
42154221
return handle;
42164222
}
42174223

4224+
err = 0;
42184225
done:
4219-
params->func(conn, NULL, params);
4226+
params->func(conn, NULL, params, err);
42204227
return 0;
42214228
}
42224229

@@ -4227,6 +4234,7 @@ static uint16_t parse_read_std_char_desc(struct bt_conn *conn, const void *pdu,
42274234
const struct bt_att_read_type_rsp *rsp;
42284235
uint16_t handle = 0U;
42294236
uint16_t uuid_val;
4237+
int err = -EPROTO;
42304238

42314239
if (params->uuid->type != BT_UUID_TYPE_16) {
42324240
goto done;
@@ -4319,7 +4327,7 @@ static uint16_t parse_read_std_char_desc(struct bt_conn *conn, const void *pdu,
43194327
.handle = handle,
43204328
};
43214329

4322-
if (params->func(conn, &attr, params) == BT_GATT_ITER_STOP) {
4330+
if (params->func(conn, &attr, params, 0) == BT_GATT_ITER_STOP) {
43234331
return 0;
43244332
}
43254333
}
@@ -4329,8 +4337,9 @@ static uint16_t parse_read_std_char_desc(struct bt_conn *conn, const void *pdu,
43294337
return handle;
43304338
}
43314339

4340+
err = 0;
43324341
done:
4333-
params->func(conn, NULL, params);
4342+
params->func(conn, NULL, params, err);
43344343
return 0;
43354344
}
43364345

@@ -4344,7 +4353,7 @@ static void gatt_read_type_rsp(struct bt_conn *conn, int err,
43444353
LOG_DBG("err %d", err);
43454354

43464355
if (err) {
4347-
params->func(conn, NULL, params);
4356+
params->func(conn, NULL, params, err == BT_ATT_ERR_ATTRIBUTE_NOT_FOUND ? 0 : err);
43484357
return;
43494358
}
43504359

@@ -4410,6 +4419,7 @@ static uint16_t parse_service(struct bt_conn *conn, const void *pdu,
44104419
struct bt_uuid_16 u16;
44114420
struct bt_uuid_128 u128;
44124421
} u;
4422+
int err = -EPROTO;
44134423

44144424
if (length < sizeof(*rsp)) {
44154425
LOG_WRN("Parse err");
@@ -4476,7 +4486,7 @@ static uint16_t parse_service(struct bt_conn *conn, const void *pdu,
44764486
attr.handle = start_handle;
44774487
attr.user_data = &value;
44784488

4479-
if (params->func(conn, &attr, params) == BT_GATT_ITER_STOP) {
4489+
if (params->func(conn, &attr, params, 0) == BT_GATT_ITER_STOP) {
44804490
return 0;
44814491
}
44824492
}
@@ -4486,8 +4496,9 @@ static uint16_t parse_service(struct bt_conn *conn, const void *pdu,
44864496
return end_handle;
44874497
}
44884498

4499+
err = 0;
44894500
done:
4490-
params->func(conn, NULL, params);
4501+
params->func(conn, NULL, params, err);
44914502
return 0;
44924503
}
44934504

@@ -4501,7 +4512,7 @@ static void gatt_read_group_rsp(struct bt_conn *conn, int err,
45014512
LOG_DBG("err %d", err);
45024513

45034514
if (err) {
4504-
params->func(conn, NULL, params);
4515+
params->func(conn, NULL, params, err == BT_ATT_ERR_ATTRIBUTE_NOT_FOUND ? 0 : err);
45054516
return;
45064517
}
45074518

@@ -4569,6 +4580,7 @@ static void gatt_find_info_rsp(struct bt_conn *conn, int err,
45694580
if (err) {
45704581
goto done;
45714582
}
4583+
err = -EPROTO;
45724584

45734585
if (length < sizeof(*rsp)) {
45744586
LOG_WRN("Parse err");
@@ -4653,7 +4665,7 @@ static void gatt_find_info_rsp(struct bt_conn *conn, int err,
46534665
.handle = handle,
46544666
};
46554667

4656-
if (params->func(conn, &attr, params) == BT_GATT_ITER_STOP) {
4668+
if (params->func(conn, &attr, params, 0) == BT_GATT_ITER_STOP) {
46574669
return;
46584670
}
46594671
}
@@ -4663,7 +4675,7 @@ static void gatt_find_info_rsp(struct bt_conn *conn, int err,
46634675
return;
46644676

46654677
done:
4666-
params->func(conn, NULL, params);
4678+
params->func(conn, NULL, params, err);
46674679
}
46684680

46694681
static int gatt_find_info_encode(struct net_buf *buf, size_t len,
@@ -5405,7 +5417,8 @@ static int gatt_write_ccc(struct bt_conn *conn,
54055417
#if defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC)
54065418
static uint8_t gatt_ccc_discover_cb(struct bt_conn *conn,
54075419
const struct bt_gatt_attr *attr,
5408-
struct bt_gatt_discover_params *params)
5420+
struct bt_gatt_discover_params *params,
5421+
int err)
54095422
{
54105423
struct bt_gatt_subscribe_params *sub_params = params->sub_params;
54115424

0 commit comments

Comments
 (0)