Skip to content

Commit daa6bae

Browse files
author
Jamie C. Driver
committed
ble: test gatt characteristics by attribute handle rather than by uuid
1 parent 61e32f0 commit daa6bae

1 file changed

Lines changed: 26 additions & 26 deletions

File tree

main/ble/ble.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,23 @@ static const ble_uuid128_t service_uuid
3838
= BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e);
3939

4040
// 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
41-
static const ble_uuid128_t tx_service_uuid
41+
static const ble_uuid128_t tx_chr_uuid
4242
= BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e);
4343

4444
// 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
45-
static const ble_uuid128_t rx_service_uuid
45+
static const ble_uuid128_t rx_chr_uuid
4646
= BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e);
4747

4848
static bool ble_is_enabled = false;
4949
static bool ble_is_connected = false;
50+
static uint16_t tx_val_handle;
51+
static uint16_t rx_val_handle;
5052
static size_t ble_read = 0;
5153
static uint8_t own_addr_type = BLE_OWN_ADDR_RANDOM;
5254
static uint8_t* full_ble_data_in = NULL;
5355
static TickType_t last_processing_time = 0;
5456
static uint8_t* ble_data_out = NULL;
5557
static uint16_t peer_conn_handle = 0;
56-
static uint16_t peer_conn_attr_handle = 0;
5758
static const size_t ATT_OVERHEAD = 3;
5859
static const size_t MAX_BLE_ATTR_SIZE = 512;
5960
static size_t ble_max_write_size = 0;
@@ -74,22 +75,17 @@ static void set_ble_max_write_size_for_mtu(const uint16_t mtu)
7475
}
7576
}
7677

77-
static int gatt_chr_event(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg)
78+
static int gatt_chr_event(
79+
const uint16_t conn_handle, const uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg)
7880
{
79-
JADE_LOGI("Entering gatt_chr_event %d", ctxt->op);
80-
const ble_uuid_t* uuid;
81-
int rc;
82-
83-
uuid = ctxt->chr->uuid;
84-
85-
uint16_t ble_msg_len;
81+
JADE_LOGI("Entering gatt_chr_event op: %d for attr: %d", ctxt->op, attr_handle);
8682

87-
if (ble_uuid_cmp(uuid, &rx_service_uuid.u) == 0) {
83+
if (attr_handle == rx_val_handle) {
8884
switch (ctxt->op) {
8985
case BLE_GATT_ACCESS_OP_WRITE_CHR:
9086
JADE_LOGI("Reading from ble device");
9187

92-
ble_msg_len = OS_MBUF_PKTLEN(ctxt->om);
88+
const uint16_t ble_msg_len = OS_MBUF_PKTLEN(ctxt->om);
9389
JADE_LOGI("Reading %u bytes", ble_msg_len);
9490

9591
if (ble_msg_len == 0) {
@@ -107,7 +103,7 @@ static int gatt_chr_event(uint16_t conn_handle, uint16_t attr_handle, struct ble
107103

108104
uint16_t out_copy_len;
109105
uint8_t* const ble_data_in = full_ble_data_in + 1;
110-
rc = ble_hs_mbuf_to_flat(ctxt->om, ble_data_in + ble_read, ble_msg_len, &out_copy_len);
106+
const int rc = ble_hs_mbuf_to_flat(ctxt->om, ble_data_in + ble_read, ble_msg_len, &out_copy_len);
111107
JADE_ASSERT(rc == 0);
112108
JADE_ASSERT(out_copy_len == ble_msg_len);
113109

@@ -118,16 +114,16 @@ static int gatt_chr_event(uint16_t conn_handle, uint16_t attr_handle, struct ble
118114
return 0;
119115

120116
default:
121-
JADE_LOGW("Unexpected gatt access op: %u, ignoring", ctxt->op);
117+
JADE_LOGW("Unexpected gatt access op: %u for rx chr, ignoring", ctxt->op);
122118
return 0;
123119
}
124-
} else if (ble_uuid_cmp(uuid, &tx_service_uuid.u) == 0) {
125-
JADE_LOGW("Received op %u for tx uuid, ignoring", ctxt->op);
120+
} else if (attr_handle == tx_val_handle) {
121+
JADE_LOGW("Received op %u for tx chr, ignoring", ctxt->op);
126122
return 0;
127123
}
128124

129125
char buf[BLE_UUID_STR_LEN];
130-
JADE_LOGW("Unexpected uuid, ignoring: %s", ble_uuid_to_str(uuid, buf));
126+
JADE_LOGW("Unexpected uuid, ignoring: %s", ble_uuid_to_str(ctxt->chr->uuid, buf));
131127
return 0;
132128
}
133129

@@ -137,13 +133,15 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
137133
// that mandate an encrypted connection.
138134
.type = BLE_GATT_SVC_TYPE_PRIMARY,
139135
.uuid = &service_uuid.u,
140-
.characteristics = (struct ble_gatt_chr_def[]){ { .uuid = &tx_service_uuid.u,
136+
.characteristics = (struct ble_gatt_chr_def[]){ { .uuid = &tx_chr_uuid.u,
141137
.access_cb = gatt_chr_event,
142138
.flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_READ_ENC
143-
| BLE_GATT_CHR_F_READ_AUTHEN },
144-
{ .uuid = &rx_service_uuid.u,
139+
| BLE_GATT_CHR_F_READ_AUTHEN,
140+
.val_handle = &tx_val_handle },
141+
{ .uuid = &rx_chr_uuid.u,
145142
.access_cb = gatt_chr_event,
146-
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC | BLE_GATT_CHR_F_WRITE_AUTHEN },
143+
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC | BLE_GATT_CHR_F_WRITE_AUTHEN,
144+
.val_handle = &rx_val_handle },
147145
{
148146
0,
149147
} },
@@ -316,7 +314,7 @@ static bool write_ble(const uint8_t* msg, const size_t towrite, void* ignore)
316314
struct os_mbuf* data = ble_hs_mbuf_from_flat(msg + written, writenow);
317315
JADE_ASSERT(data);
318316

319-
rc = ble_gatts_indicate_custom(peer_conn_handle, peer_conn_attr_handle, data);
317+
rc = ble_gatts_indicate_custom(peer_conn_handle, tx_val_handle, data);
320318
if (rc != 0) {
321319
JADE_LOGW("ble_gattc_indicate_custom() returned error %d trying to write %u bytes, attempt %u", rc,
322320
writenow, try);
@@ -590,7 +588,6 @@ static int ble_gap_event(struct ble_gap_event* event, void* arg)
590588
ble_read = 0;
591589
ble_print_conn_desc(&event->disconnect.conn);
592590
peer_conn_handle = 0;
593-
peer_conn_attr_handle = 0;
594591
ble_is_connected = false;
595592

596593
// Restart advertising if ble enabled
@@ -673,8 +670,11 @@ static int ble_gap_event(struct ble_gap_event* event, void* arg)
673670
event->subscribe.conn_handle, event->subscribe.attr_handle, event->subscribe.reason,
674671
event->subscribe.prev_notify, event->subscribe.cur_notify, event->subscribe.prev_indicate,
675672
event->subscribe.cur_indicate);
676-
peer_conn_handle = event->subscribe.conn_handle;
677-
peer_conn_attr_handle = event->subscribe.attr_handle;
673+
674+
// Cache the last peer to subscribe to the tx val (so they can be notified)
675+
if (event->subscribe.attr_handle == tx_val_handle) {
676+
peer_conn_handle = event->subscribe.conn_handle;
677+
}
678678
ble_is_connected = true;
679679
return 0;
680680

0 commit comments

Comments
 (0)