Skip to content

Commit 9e11b7a

Browse files
committed
Fix Jira 903 BLE peripheral.characteristic() sometimes returns NULL unexpected
Root cause: 1. The stack doesn't release the resource and makes peripheral crashed. Then central blocked. Solution: 1. Fix memory leak issue and release the resource. Changed File: gatt.c - Modify the code to make sure the resource can be released.
1 parent 19b230e commit 9e11b7a

File tree

1 file changed

+8
-6
lines changed
  • system/libarc32_arduino101/framework/src/services/ble

1 file changed

+8
-6
lines changed

Diff for: system/libarc32_arduino101/framework/src/services/ble/gatt.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,16 @@ static ssize_t on_prep_write(const struct nble_gatts_write_evt *evt,
805805
const uint8_t *buf, uint8_t buflen)
806806
{
807807
const struct bt_gatt_attr *attr = evt->attr;
808-
struct bt_conn *conn = bt_conn_lookup_handle(evt->conn_handle);
808+
struct bt_conn *conn = NULL;
809809
ssize_t status;
810810

811811
struct prep_data *data;
812+
813+
// Check permision first
814+
if (!(attr->perm & BT_GATT_PERM_PREPARE_WRITE)) {
815+
return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED);
816+
}
817+
812818
data = nble_curie_alloc_hook(sizeof (struct prep_data));
813819

814820
/* Assert if memory allocation failed */
@@ -824,11 +830,7 @@ static ssize_t on_prep_write(const struct nble_gatts_write_evt *evt,
824830
BT_ASSERT(data->buf);
825831

826832
memcpy(data->buf, buf, buflen);
827-
828-
if (!(attr->perm & BT_GATT_PERM_PREPARE_WRITE)) {
829-
return BT_GATT_ERR(BT_ATT_ERR_WRITE_NOT_PERMITTED);
830-
}
831-
833+
conn = bt_conn_lookup_handle(evt->conn_handle);
832834
/* Write attribute value to check if device is authorized */
833835
status = attr->write(conn, attr, buf, buflen, evt->offset,
834836
BT_GATT_WRITE_FLAG_PREPARE);

0 commit comments

Comments
 (0)