Skip to content

Commit f55b6d9

Browse files
committed
extmod/bluetooth: Add python IRQ for GATTS_ENC_UPDATE
Expose: conn_handle, enc, auth, bonded, keysize
1 parent 2980119 commit f55b6d9

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

extmod/btstack/modbluetooth_btstack.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,35 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
358358
DEBUG_printf(" --> btstack # conns changed\n");
359359
} else if (event_type == HCI_EVENT_VENDOR_SPECIFIC) {
360360
DEBUG_printf(" --> hci vendor specific\n");
361+
} else if (event_type == SM_EVENT_AUTHORIZATION_RESULT ||
362+
event_type == SM_EVENT_PAIRING_COMPLETE ||
363+
// event_type == GAP_EVENT_DEDICATED_BONDING_COMPLETED || // No conn_handle
364+
event_type == HCI_EVENT_ENCRYPTION_CHANGE) {
365+
DEBUG_printf(" --> enc/auth/pair/bond change\n", );
366+
367+
hci_con_handle_t con_handle;
368+
switch (event_type) {
369+
case SM_EVENT_AUTHORIZATION_RESULT:
370+
con_handle = sm_event_authorization_result_get_handle(packet);
371+
break;
372+
case SM_EVENT_PAIRING_COMPLETE:
373+
con_handle = sm_event_pairing_complete_get_handle(packet);
374+
break;
375+
case HCI_EVENT_ENCRYPTION_CHANGE:
376+
con_handle = hci_event_encryption_change_get_connection_handle(packet);
377+
break;
378+
default:
379+
return;
380+
}
381+
382+
hci_connection_t * hci_con = hci_connection_for_handle(con_handle);
383+
sm_connection_t *desc = &hci_con->sm_connection;
384+
mp_bluetooth_gatts_on_enc_update(con_handle,
385+
desc->sm_connection_encrypted,
386+
desc->sm_connection_authenticated,
387+
desc->sm_le_db_index != -1,
388+
desc->sm_actual_encryption_key_size);
389+
361390
} else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
362391
DEBUG_printf(" --> hci disconnect complete\n");
363392
uint16_t conn_handle = hci_event_disconnection_complete_get_connection_handle(packet);

extmod/modbluetooth.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
926926
} else if (event == MP_BLUETOOTH_IRQ_GATTS_CONN_UPDATE) {
927927
// conn_handle, interval, latency, timeout, enc, auth, bonded, keysize
928928
ringbuf_extract(&o->ringbuf, data_tuple, 4, 0, NULL, 0, NULL, NULL);
929+
} else if (event == MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE) {
930+
// conn_handle, interval, latency, timeout, enc, auth, bonded, keysize
931+
ringbuf_extract(&o->ringbuf, data_tuple, 1, 4, NULL, 0, NULL, NULL);
929932
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
930933
} else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) {
931934
// addr_type, addr, adv_type, rssi, adv_data
@@ -1061,6 +1064,19 @@ void mp_bluetooth_gatts_on_conn_update(uint16_t conn_handle, uint16_t conn_itvl,
10611064
schedule_ringbuf(atomic_state);
10621065
}
10631066

1067+
void mp_bluetooth_gatts_on_enc_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) {
1068+
MICROPY_PY_BLUETOOTH_ENTER
1069+
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
1070+
if (enqueue_irq(o, 2 + 1 + 1 + 1 + 1, MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE)) {
1071+
ringbuf_put16(&o->ringbuf, conn_handle);
1072+
ringbuf_put(&o->ringbuf, encrypted);
1073+
ringbuf_put(&o->ringbuf, authenticated);
1074+
ringbuf_put(&o->ringbuf, bonded);
1075+
ringbuf_put(&o->ringbuf, key_size);
1076+
}
1077+
schedule_ringbuf(atomic_state);
1078+
}
1079+
10641080
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
10651081
void mp_bluetooth_gap_on_scan_complete(void) {
10661082
MICROPY_PY_BLUETOOTH_ENTER

extmod/modbluetooth.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
#define MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE (20)
103103
#define MP_BLUETOOTH_IRQ_MTU_EXCHANGED (21)
104104
#define MP_BLUETOOTH_IRQ_GATTS_CONN_UPDATE (22)
105+
#define MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE (23)
105106

106107
#define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0)
107108
#define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1)
@@ -135,6 +136,7 @@ _IRQ_GATTC_INDICATE = const(19)
135136
_IRQ_GATTS_INDICATE_DONE = const(20)
136137
_IRQ_MTU_EXCHANGED = const(21)
137138
_IRQ_GATTS_CONN_UPDATE = const(22)
139+
_IRQ_GATTS_ENC_UPDATE = const(23)
138140
*/
139141

140142
// Common UUID type.
@@ -264,6 +266,9 @@ void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t valu
264266
// Call this when any connection parameters have been changed.
265267
void mp_bluetooth_gatts_on_conn_update(uint16_t conn_handle, uint16_t conn_itvl, uint16_t conn_latency, uint16_t supervision_timeout);
266268

269+
// Call this when any connection encryption has been changed.
270+
void mp_bluetooth_gatts_on_enc_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size);
271+
267272
#if MICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK
268273
// Call this when a characteristic is read from. Return false to deny the read.
269274
bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle);

extmod/nimble/modbluetooth_nimble.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) {
312312
}
313313
break;
314314
}
315+
316+
case BLE_GAP_EVENT_ENC_CHANGE: {
317+
DEBUG_printf("gap_event_cb: enc change: status=%d\n", event->enc_change.status);
318+
struct ble_gap_conn_desc desc;
319+
if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) {
320+
mp_bluetooth_gatts_on_enc_update(event->conn_update.conn_handle,
321+
desc.sec_state.encrypted, desc.sec_state.authenticated,
322+
desc.sec_state.bonded, desc.sec_state.key_size);
323+
}
324+
break;
325+
}
315326
}
316327
return 0;
317328
}
@@ -933,6 +944,16 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) {
933944
break;
934945
}
935946

947+
case BLE_GAP_EVENT_ENC_CHANGE: {
948+
DEBUG_printf("periph_gap_event_cb: enc change: status=%d\n", event->enc_change.status);
949+
struct ble_gap_conn_desc desc;
950+
if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) {
951+
mp_bluetooth_gatts_on_enc_update(event->conn_update.conn_handle,
952+
desc.sec_state.encrypted, desc.sec_state.authenticated,
953+
desc.sec_state.bonded, desc.sec_state.key_size);
954+
}
955+
break;
956+
}
936957
default:
937958
break;
938959
}

0 commit comments

Comments
 (0)
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