diff --git a/extmod/btstack/modbluetooth_btstack.c b/extmod/btstack/modbluetooth_btstack.c index 97dd2cbb53..9faae26e46 100644 --- a/extmod/btstack/modbluetooth_btstack.c +++ b/extmod/btstack/modbluetooth_btstack.c @@ -295,20 +295,34 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t if (event_type == HCI_EVENT_LE_META) { DEBUG_printf(" --> hci le meta\n"); - if (hci_event_le_meta_get_subevent_code(packet) == HCI_SUBEVENT_LE_CONNECTION_COMPLETE) { - uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); - uint8_t addr_type = hci_subevent_le_connection_complete_get_peer_address_type(packet); - bd_addr_t addr; - hci_subevent_le_connection_complete_get_peer_address(packet, addr); - uint16_t irq_event; - if (hci_subevent_le_connection_complete_get_role(packet) == 0) { - // Master role. - irq_event = MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT; - } else { - // Slave role. - irq_event = MP_BLUETOOTH_IRQ_CENTRAL_CONNECT; + switch (hci_event_le_meta_get_subevent_code(packet)) { + case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: { + uint16_t conn_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); + uint8_t addr_type = hci_subevent_le_connection_complete_get_peer_address_type(packet); + bd_addr_t addr; + hci_subevent_le_connection_complete_get_peer_address(packet, addr); + uint16_t irq_event; + if (hci_subevent_le_connection_complete_get_role(packet) == 0) { + // Master role. + irq_event = MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT; + } else { + // Slave role. + irq_event = MP_BLUETOOTH_IRQ_CENTRAL_CONNECT; + } + mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, addr_type, addr); + break; + } + case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: { + uint8_t status = hci_subevent_le_connection_update_complete_get_status(packet); + uint16_t conn_handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); + uint16_t conn_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); + uint16_t conn_latency = hci_subevent_le_connection_update_complete_get_conn_latency(packet); + uint16_t supervision_timeout = hci_subevent_le_connection_update_complete_get_supervision_timeout(packet); + DEBUG_printf("- LE Connection %04x: connection update - connection interval %u.%02u ms, latency %u, timeout %u\n", + conn_handle, conn_interval * 125 / 100, 25 * (conn_interval & 3), conn_latency, supervision_timeout); + mp_bluetooth_gap_on_connection_update(conn_handle, conn_interval, conn_latency, supervision_timeout, status); + break; } - mp_bluetooth_gap_on_connected_disconnected(irq_event, conn_handle, addr_type, addr); } } else if (event_type == BTSTACK_EVENT_STATE) { uint8_t state = btstack_event_state_get_state(packet); diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index 2cff386f12..dc1084acc1 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -979,6 +979,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) { if (event == MP_BLUETOOTH_IRQ_CENTRAL_CONNECT || event == MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT || event == MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT || event == MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT) { // conn_handle, addr_type, addr ringbuf_extract(&o->ringbuf, data_tuple, 1, 1, &o->irq_data_addr, 0, NULL, NULL); + } else if (event == MP_BLUETOOTH_IRQ_CONNECTION_UPDATE) { + // conn_handle, conn_interval, conn_latency, supervision_timeout, status + ringbuf_extract(&o->ringbuf, data_tuple, 5, 0, NULL, 0, NULL, NULL); } else if (event == MP_BLUETOOTH_IRQ_GATTS_WRITE) { // conn_handle, value_handle ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL); @@ -1093,6 +1096,11 @@ void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_han invoke_irq_handler(event, &conn_handle, 1, &addr_type, 1, addr, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); } +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) { + uint16_t args[] = {conn_handle, conn_interval, conn_latency, supervision_timeout, status}; + invoke_irq_handler(MP_BLUETOOTH_IRQ_CONNECTION_UPDATE, args, 5, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); +} + void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { uint16_t args[] = {conn_handle, value_handle}; invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_WRITE, args, 2, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0); @@ -1276,6 +1284,19 @@ void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_han schedule_ringbuf(atomic_state); } +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) { + 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 + 2, MP_BLUETOOTH_IRQ_CONNECTION_UPDATE)) { + ringbuf_put16(&o->ringbuf, conn_handle); + ringbuf_put16(&o->ringbuf, conn_interval); + ringbuf_put16(&o->ringbuf, conn_latency); + ringbuf_put16(&o->ringbuf, supervision_timeout); + ringbuf_put16(&o->ringbuf, status); + } + schedule_ringbuf(atomic_state); +} + void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) { MICROPY_PY_BLUETOOTH_ENTER mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth)); diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 9caddb0f3f..2619e71894 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -114,6 +114,7 @@ #define MP_BLUETOOTH_IRQ_L2CAP_DISCONNECT (24) #define MP_BLUETOOTH_IRQ_L2CAP_RECV (25) #define MP_BLUETOOTH_IRQ_L2CAP_SEND_READY (26) +#define MP_BLUETOOTH_IRQ_CONNECTION_UPDATE (27) #define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0) #define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1) @@ -151,6 +152,7 @@ _IRQ_L2CAP_CONNECT = const(23) _IRQ_L2CAP_DISCONNECT = const(24) _IRQ_L2CAP_RECV = const(25) _IRQ_L2CAP_SEND_READY = const(26) +_IRQ_GATTS_CONN_UPDATE = const(27) */ // bluetooth.UUID type. @@ -281,6 +283,9 @@ int mp_bluetooth_l2cap_recvinto(uint16_t conn_handle, uint16_t cid, uint8_t *buf // Notify modbluetooth that a connection/disconnection event has occurred. void mp_bluetooth_gap_on_connected_disconnected(uint8_t event, uint16_t conn_handle, uint8_t addr_type, const uint8_t *addr); +// 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 a characteristic is written to. void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index e4a048e3d3..3983d18690 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -303,6 +303,19 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) { } break; } + + case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE: + DEBUG_printf("gap_event_cb: phy update: %d\n", event->phy_updated.tx_phy); + break; + + case BLE_GAP_EVENT_CONN_UPDATE: { + DEBUG_printf("gap_event_cb: connection update: status=%d\n", event->conn_update.status); + struct ble_gap_conn_desc desc; + if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) { + mp_bluetooth_gap_on_connection_update(event->conn_update.conn_handle, desc.conn_itvl, desc.conn_latency, desc.supervision_timeout, event->conn_update.status == 0 ? 0 : 1); + } + break; + } } return 0; } @@ -938,13 +951,13 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) { break; } - case BLE_GAP_EVENT_CONN_UPDATE: - // TODO - break; - - case BLE_GAP_EVENT_CONN_UPDATE_REQ: - // TODO + case BLE_GAP_EVENT_CONN_UPDATE: { + DEBUG_printf("peripheral_gap_event_cb: connection update: status=%d\n", event->conn_update.status); + if (ble_gap_conn_find(event->conn_update.conn_handle, &desc) == 0) { + mp_bluetooth_gap_on_connection_update(event->conn_update.conn_handle, desc.conn_itvl, desc.conn_latency, desc.supervision_timeout, event->conn_update.status == 0 ? 0 : 1); + } break; + } case BLE_GAP_EVENT_MTU: { if (event->mtu.channel_id == BLE_L2CAP_CID_ATT) {