Skip to content

Commit 7b7c2d7

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

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

extmod/btstack/modbluetooth_btstack.c

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

extmod/modbluetooth.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,9 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
928928
} else if (event == MP_BLUETOOTH_IRQ_GATTS_CONN_UPDATE) {
929929
// conn_handle, interval, latency, timeout, enc, auth, bonded, keysize
930930
ringbuf_extract(&o->ringbuf, data_tuple, 4, 0, NULL, 0, NULL, NULL);
931+
} else if (event == MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE) {
932+
// conn_handle, interval, latency, timeout, enc, auth, bonded, keysize
933+
ringbuf_extract(&o->ringbuf, data_tuple, 1, 4, NULL, 0, NULL, NULL);
931934
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
932935
} else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) {
933936
// addr_type, addr, adv_type, rssi, adv_data
@@ -1048,6 +1051,11 @@ void mp_bluetooth_gatts_on_conn_update(uint16_t conn_handle, uint16_t conn_itvl,
10481051
invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_CONN_UPDATE, args, 4, NULL_U8, 0, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0);
10491052
}
10501053

1054+
void mp_bluetooth_gatts_on_enc_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) {
1055+
uint8_t args[] = {encrypted, authenticated, bonded, key_size};
1056+
invoke_irq_handler(MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE, &conn_handle, 1, args, 4, NULL_ADDR, NULL_I8, 0, NULL_UUID, NULL_DATA, 0);
1057+
}
1058+
10511059
bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle) {
10521060
uint16_t args[] = {conn_handle, value_handle};
10531061
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);
@@ -1223,6 +1231,19 @@ void mp_bluetooth_gatts_on_conn_update(uint16_t conn_handle, uint16_t conn_itvl,
12231231
schedule_ringbuf(atomic_state);
12241232
}
12251233

1234+
void mp_bluetooth_gatts_on_enc_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size) {
1235+
MICROPY_PY_BLUETOOTH_ENTER
1236+
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
1237+
if (enqueue_irq(o, 2 + 1 + 1 + 1 + 1, MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE)) {
1238+
ringbuf_put16(&o->ringbuf, conn_handle);
1239+
ringbuf_put(&o->ringbuf, encrypted);
1240+
ringbuf_put(&o->ringbuf, authenticated);
1241+
ringbuf_put(&o->ringbuf, bonded);
1242+
ringbuf_put(&o->ringbuf, key_size);
1243+
}
1244+
schedule_ringbuf(atomic_state);
1245+
}
1246+
12261247
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
12271248
void mp_bluetooth_gap_on_scan_complete(void) {
12281249
MICROPY_PY_BLUETOOTH_ENTER

extmod/modbluetooth.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#define MP_BLUETOOTH_IRQ_GATTS_INDICATE_DONE (20)
106106
#define MP_BLUETOOTH_IRQ_MTU_EXCHANGED (21)
107107
#define MP_BLUETOOTH_IRQ_GATTS_CONN_UPDATE (22)
108+
#define MP_BLUETOOTH_IRQ_GATTS_ENC_UPDATE (23)
108109

109110
#define MP_BLUETOOTH_ADDRESS_MODE_PUBLIC (0)
110111
#define MP_BLUETOOTH_ADDRESS_MODE_RANDOM (1)
@@ -138,6 +139,7 @@ _IRQ_GATTC_INDICATE = const(19)
138139
_IRQ_GATTS_INDICATE_DONE = const(20)
139140
_IRQ_MTU_EXCHANGED = const(21)
140141
_IRQ_GATTS_CONN_UPDATE = const(22)
142+
_IRQ_GATTS_ENC_UPDATE = const(23)
141143
*/
142144

143145
// bluetooth.UUID type.
@@ -269,6 +271,9 @@ void mp_bluetooth_gatts_on_indicate_complete(uint16_t conn_handle, uint16_t valu
269271
// Call this when any connection parameters have been changed.
270272
void mp_bluetooth_gatts_on_conn_update(uint16_t conn_handle, uint16_t conn_itvl, uint16_t conn_latency, uint16_t supervision_timeout);
271273

274+
// Call this when any connection encryption has been changed.
275+
void mp_bluetooth_gatts_on_enc_update(uint16_t conn_handle, bool encrypted, bool authenticated, bool bonded, uint8_t key_size);
276+
272277
// Call this when a characteristic is read from. Return false to deny the read.
273278
bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_handle);
274279

extmod/nimble/modbluetooth_nimble.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,17 @@ STATIC int gap_event_cb(struct ble_gap_event *event, void *arg) {
310310
}
311311
break;
312312
}
313+
314+
case BLE_GAP_EVENT_ENC_CHANGE: {
315+
DEBUG_printf("gap_event_cb: enc change: status=%d\n", event->enc_change.status);
316+
struct ble_gap_conn_desc desc;
317+
if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) {
318+
mp_bluetooth_gatts_on_enc_update(event->conn_update.conn_handle,
319+
desc.sec_state.encrypted, desc.sec_state.authenticated,
320+
desc.sec_state.bonded, desc.sec_state.key_size);
321+
}
322+
break;
323+
}
313324
}
314325
return 0;
315326
}
@@ -959,6 +970,16 @@ STATIC int peripheral_gap_event_cb(struct ble_gap_event *event, void *arg) {
959970
break;
960971
}
961972

973+
case BLE_GAP_EVENT_ENC_CHANGE: {
974+
DEBUG_printf("periph_gap_event_cb: enc change: status=%d\n", event->enc_change.status);
975+
struct ble_gap_conn_desc desc;
976+
if (ble_gap_conn_find(event->enc_change.conn_handle, &desc) == 0) {
977+
mp_bluetooth_gatts_on_enc_update(event->conn_update.conn_handle,
978+
desc.sec_state.encrypted, desc.sec_state.authenticated,
979+
desc.sec_state.bonded, desc.sec_state.key_size);
980+
}
981+
break;
982+
}
962983
default:
963984
break;
964985
}

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