Skip to content

extmod/bluetooth: Add python IRQ for GATTS_CONN_UPDATE and GATTS_ENC_UPDATE #6439

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
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
69 changes: 56 additions & 13 deletions extmod/btstack/modbluetooth_btstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
// print connection parameters (without using float operations)
uint16_t con_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 latency = hci_subevent_le_connection_update_complete_get_conn_latency(packet);
uint16_t 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\n",
con_handle, conn_interval * 125 / 100, 25 * (conn_interval & 3), latency);
mp_bluetooth_gatts_on_conn_update(con_handle, conn_interval, latency, timeout);
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);
Expand Down Expand Up @@ -345,6 +359,35 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
DEBUG_printf(" --> btstack # conns changed\n");
} else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) {
DEBUG_printf(" --> hci vendor specific\n");
} else if (event_type == SM_EVENT_AUTHORIZATION_RESULT ||
event_type == SM_EVENT_PAIRING_COMPLETE ||
// event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle
event_type == HCI_EVENT_ENCRYPTION_CHANGE) {
DEBUG_printf(" --> enc/auth/pair/bond change\n", );

hci_con_handle_t con_handle;
switch (event_type) {
case SM_EVENT_AUTHORIZATION_RESULT:
con_handle = sm_event_authorization_result_get_handle(packet);
break;
case SM_EVENT_PAIRING_COMPLETE:
con_handle = sm_event_pairing_complete_get_handle(packet);
break;
case HCI_EVENT_ENCRYPTION_CHANGE:
con_handle = hci_event_encryption_change_get_connection_handle(packet);
break;
default:
return;
}

hci_connection_t * hci_con = hci_connection_for_handle(con_handle);
sm_connection_t *desc = &hci_con->sm_connection;
mp_bluetooth_gatts_on_enc_update(con_handle,
desc->sm_connection_encrypted,
desc->sm_connection_authenticated,
desc->sm_le_db_index != -1,
desc->sm_actual_encryption_key_size);

} else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
DEBUG_printf(" --> hci disconnect complete\n");
uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet);
Expand Down
41 changes: 41 additions & 0 deletions extmod/modbluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,12 @@ 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_GATTS_CONN_UPDATE) {
// conn_handle, interval, latency, timeout, enc, auth, bonded, keysize
ringbuf_extract(&o->ringbuf, data_tuple, 4, 0, NULL, 0, NULL, NULL);
} else if (event == MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE) {
// conn_handle, interval, latency, timeout, enc, auth, bonded, keysize
ringbuf_extract(&o->ringbuf, data_tuple, 1, 4, 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 @@ -1040,6 +1046,16 @@ void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t valu
invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE, args, 2, &status, 1, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0);
}

void mp_bluetooth_gatts_on_conn_update(uint16_t conn_handle, uint16_t conn_itvl, uint16_t conn_latency, uint16_t supervision_timeout) {
uint16_t args[] = {conn_handle, conn_itvl, conn_latency, supervision_timeout};
invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_CONN_UPDATE, args, 4, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0);
}

void mp_bluetooth_gatts_on_enc_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) {
uint8_t args[] = {encrypted, authenticated, bonded, key_size};
invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE, &conn_handle, 1, args, 4, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0);
}

bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) {
uint16_t args[] = {conn_handle, value_handle};
mp_obj_t result = invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST, args, 2, NULL, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0);
Expand Down Expand Up @@ -1203,6 +1219,31 @@ void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t valu
schedule_ringbuf(atomic_state);
}

void mp_bluetooth_gatts_on_conn_update(uint16_t conn_handle, uint16_t conn_itvl, uint16_t conn_latency, uint16_t supervision_timeout) {
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_GATTS_CONN_UPDATE)) {
ringbuf_put16(&o->ringbuf, conn_handle);
ringbuf_put16(&o->ringbuf, conn_itvl);
ringbuf_put16(&o->ringbuf, conn_latency);
ringbuf_put16(&o->ringbuf, supervision_timeout);
}
schedule_ringbuf(atomic_state);
}

void mp_bluetooth_gatts_on_enc_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) {
MICROPY_PY_BLUETOOTH_ENTER
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
if (enqueue_irq(o, 2 + 1 + 1 + 1 + 1, MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE)) {
ringbuf_put16(&o->ringbuf, conn_handle);
ringbuf_put(&o->ringbuf, encrypted);
ringbuf_put(&o->ringbuf, authenticated);
ringbuf_put(&o->ringbuf, bonded);
ringbuf_put(&o->ringbuf, key_size);
}
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
10 changes: 10 additions & 0 deletions extmod/modbluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
#define MP_BLUETOOTH_IRQ_GATTC_INDICATE (19)
#define MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE (20)
#define MP_BLUETOOTH_IRQ_MTU_EXCHANGED (21)
#define MP_BLUETOOTH_IRQ_GATTS_CONN_UPDATE (22)
#define MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE (23)

#define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0)
#define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1)
Expand Down Expand Up @@ -136,6 +138,8 @@ _IRQ_GATTC_NOTIFY = const(18)
_IRQ_GATTC_INDICATE = const(19)
_IRQ_GATTS_INDICATE_DONE = const(20)
_IRQ_MTU_EXCHANGED = const(21)
_IRQ_GATTS_CONN_UPDATE = const(22)
_IRQ_GATTS_ENC_UPDATE = const(23)
*/

// bluetooth.UUID type.
Expand Down Expand Up @@ -264,6 +268,12 @@ void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle);
// Call this when an acknowledgment is received for an indication.
void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t value_handle, uint8_t status);

// Call this when any connection parameters have been changed.
void mp_bluetooth_gatts_on_conn_update(uint16_t conn_handle, uint16_t conn_itvl, uint16_t conn_latency, uint16_t supervision_timeout);

// Call this when any connection encryption has been changed.
void mp_bluetooth_gatts_on_enc_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size);

// Call this when a characteristic is read from. Return false to deny the read.
bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle);

Expand Down
49 changes: 43 additions & 6 deletions extmod/nimble/modbluetooth_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,31 @@ 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_gatts_on_conn_update(event->conn_update.conn_handle,
desc.conn_itvl, desc.conn_latency, desc.supervision_timeout);
}
break;
}

case BLE_GAP_EVENT_ENC_CHANGE: {
DEBUG_printf("gap_event_cb: enc change: status=%d\n", event->enc_change.status);
struct ble_gap_conn_desc desc;
if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) {
mp_bluetooth_gatts_on_enc_update(event->conn_update.conn_handle,
desc.sec_state.encrypted, desc.sec_state.authenticated,
desc.sec_state.bonded, desc.sec_state.key_size);
}
break;
}
}
return 0;
}
Expand Down Expand Up @@ -927,13 +952,15 @@ 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("periph_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_gatts_on_conn_update(event->conn_update.conn_handle,
desc.conn_itvl, desc.conn_latency, desc.supervision_timeout);
}
break;
}

case BLE_GAP_EVENT_MTU: {
if (event->mtu.channel_id == BLE_L2CAP_CID_ATT) {
Expand All @@ -943,6 +970,16 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) {
break;
}

case BLE_GAP_EVENT_ENC_CHANGE: {
DEBUG_printf("periph_gap_event_cb: enc change: status=%d\n", event->enc_change.status);
struct ble_gap_conn_desc desc;
if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) {
mp_bluetooth_gatts_on_enc_update(event->conn_update.conn_handle,
desc.sec_state.encrypted, desc.sec_state.authenticated,
desc.sec_state.bonded, desc.sec_state.key_size);
}
break;
}
default:
break;
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy