Skip to content

extmod/nimble: Provide subscription information on subscribtion update. #7555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions extmod/modbluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
} else if (event == MP_BLUETOOTH_IRQ_MTU_EXCHANGED) {
// conn_handle, mtu
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL);
} else if (event == MP_BLUETOOTH_IRQ_SUBSCRIPTION_UPDATE) {
// conn_handle, chr_value_handlle, cur_notify, cur_indicate
ringbuf_extract(&o->ringbuf, data_tuple, 4, 0, NULL, 0, NULL, NULL);
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
} else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) {
// addr_type, addr, adv_type, rssi, adv_data
Expand Down Expand Up @@ -1177,6 +1180,11 @@ void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_i
invoke_irq_handler(MP_BLUETOOTH_IRQ_CONNECTION_UPDATE, args, 5, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0);
}

void mp_bluetooth_gatts_on_subscription_update(uint16_t conn_handle, uint16_t chr_value_handle, uint16_t cur_notify, uint16_t cur_indicate) {
mp_int_t args[] = {conn_handle, chr_value_handle, cur_notify, cur_indicate};
invoke_irq_handler(MP_BLUETOOTH_IRQ_SUBSCRIPTION_UPDATE, args, 4, 0, NULL_ADDR, NULL_UUID, NULL_DATA, NULL_DATA_LEN, 0);
}

#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) {
mp_int_t args[] = {conn_handle, encrypted, authenticated, bonded, key_size};
Expand Down Expand Up @@ -1449,6 +1457,18 @@ void mp_bluetooth_gatts_on_mtu_exchanged(uint16_t conn_handle, uint16_t value) {
schedule_ringbuf(atomic_state);
}

void mp_bluetooth_gatts_on_subscription_update(uint16_t conn_handle, uint16_t chr_value_handle, uint16_t cur_notify, uint16_t cur_indicate) {
MICROPY_PY_BLUETOOTH_ENTER
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
if (enqueue_irq(o, 2 + 2 + 2 + 2, MP_BLUETOOTH_IRQ_SUBSCRIPTION_UPDATE)) {
ringbuf_put16(&o->ringbuf, conn_handle);
ringbuf_put16(&o->ringbuf, chr_value_handle);
ringbuf_put16(&o->ringbuf, cur_notify);
ringbuf_put16(&o->ringbuf, cur_indicate);
}
schedule_ringbuf(atomic_state);
}

#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
void mp_bluetooth_gap_on_scan_complete(void) {
MICROPY_PY_BLUETOOTH_ENTER
Expand Down
4 changes: 4 additions & 0 deletions extmod/modbluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
#define MP_BLUETOOTH_IRQ_GET_SECRET (29)
#define MP_BLUETOOTH_IRQ_SET_SECRET (30)
#define MP_BLUETOOTH_IRQ_PASSKEY_ACTION (31)
#define MP_BLUETOOTH_IRQ_SUBSCRIPTION_UPDATE (32)

#define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0)
#define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1)
Expand Down Expand Up @@ -414,6 +415,9 @@ void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_han
// Call this when any connection parameters have been changed.
void mp_bluetooth_gap_on_connection_update(uint16_t conn_handle, uint16_t conn_interval, uint16_t conn_latency, uint16_t supervision_timeout, uint16_t status);

// Call this when any connection subscribes or unsubscribes to a characteristic
void mp_bluetooth_gatts_on_subscription_update(uint16_t conn_handle, uint16_t chr_value_handle, uint16_t cur_notify, uint16_t cur_indicate);

#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
// Call this when any connection encryption has been changed (e.g. during pairing).
void mp_bluetooth_gatts_on_encryption_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size);
Expand Down
6 changes: 6 additions & 0 deletions extmod/nimble/modbluetooth_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,12 @@ STATIC int commmon_gap_event_cb(struct ble_gap_event *event, void *arg) {
return 0;
}

case BLE_GAP_EVENT_SUBSCRIBE: {
DEBUG_printf("commmon_gap_event_cb: subscribe: characteristic handle=%d, reason=%d cur_notify=%d cur_indicate=%d \n", event->attr_handle, event->subscribe.reason, event->subscribe.cur_notify, event->subscribe.cur_indicate);
mp_bluetooth_gatts_on_subscription_update(event->subscribe.conn_handle, event->subscribe.attr_handle, event->subscribe.cur_notify, event->subscribe.cur_indicate);
return 0;
}

default:
DEBUG_printf("commmon_gap_event_cb: unknown type %d\n", event->type);
return 0;
Expand Down