Skip to content

Commit 478c3a8

Browse files
Marceau Fillonpi-anl
authored andcommitted
extmod/bluetooth: Expose function to compare_database_hashes.
Will compare current connection db_hash against stored hash in bond db, send service_changed indication if changed and return comparison result.
1 parent 68793e7 commit 478c3a8

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

extmod/modbluetooth.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,15 @@ static mp_obj_t bluetooth_ble_gap_passkey(size_t n_args, const mp_obj_t *args) {
724724
return bluetooth_handle_errno(mp_bluetooth_gap_passkey(conn_handle, action, passkey));
725725
}
726726
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_passkey_obj, 4, 4, bluetooth_ble_gap_passkey);
727+
728+
static mp_obj_t bluetooth_compare_database_hashes(mp_obj_t self_in, mp_obj_t conn_handle_in) {
729+
(void)self_in;
730+
int result = 0;
731+
uint16_t conn_handle = mp_obj_get_int(conn_handle_in);
732+
result = mp_bluetooth_compare_database_hashes(conn_handle);
733+
return MP_OBJ_NEW_SMALL_INT(result);
734+
}
735+
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_compare_database_hashes_obj, bluetooth_compare_database_hashes);
727736
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
728737

729738
// ----------------------------------------------------------------------------
@@ -946,6 +955,7 @@ static const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = {
946955
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
947956
{ MP_ROM_QSTR(MP_QSTR_gap_pair), MP_ROM_PTR(&bluetooth_ble_gap_pair_obj) },
948957
{ MP_ROM_QSTR(MP_QSTR_gap_passkey), MP_ROM_PTR(&bluetooth_ble_gap_passkey_obj) },
958+
{ MP_ROM_QSTR(MP_QSTR_gap_compare_database_hashes), MP_ROM_PTR(&bluetooth_compare_database_hashes_obj) },
949959
#endif
950960
// GATT Server
951961
{ MP_ROM_QSTR(MP_QSTR_gatts_register_services), MP_ROM_PTR(&bluetooth_ble_gatts_register_services_obj) },

extmod/modbluetooth.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ int mp_bluetooth_gap_pair(uint16_t conn_handle);
360360

361361
// Respond to a pairing request.
362362
int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey);
363+
364+
// Compare stored database hash to current one
365+
int mp_bluetooth_compare_database_hashes(uint16_t conn_handle);
363366
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
364367

365368
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE

extmod/nimble/modbluetooth_nimble.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,23 @@ int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t pass
11371137
DEBUG_printf("mp_bluetooth_gap_passkey: injecting IO: conn_handle=%d, action=%d, passkey=" UINT_FMT ", numcmp_accept=%d\n", conn_handle, io.action, (mp_uint_t)io.passkey, io.numcmp_accept);
11381138
return ble_hs_err_to_errno(ble_sm_inject_io(conn_handle, &io));
11391139
}
1140+
1141+
int mp_bluetooth_compare_database_hashes(uint16_t conn_handle) {
1142+
DEBUG_printf("mp_bluetooth_compare_database_hashes: conn_handle=%d\n", conn_handle);
1143+
struct ble_store_value_hash hash_value;
1144+
struct ble_store_key_hash hash_key;
1145+
struct ble_hs_conn *conn;
1146+
1147+
conn = ble_hs_conn_find(conn_handle);
1148+
BLE_HS_DBG_ASSERT(conn != NULL);
1149+
1150+
hash_key.peer_addr = conn->bhc_peer_addr;
1151+
hash_key.peer_addr.type =
1152+
ble_hs_misc_peer_addr_type_to_id(conn->bhc_peer_addr.type);
1153+
hash_key.idx = 0;
1154+
1155+
return ble_store_read_hash(conn_handle, &hash_key, &hash_value);
1156+
}
11401157
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
11411158

11421159
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE

lib/mynewt-nimble

0 commit comments

Comments
 (0)