added secured callback

This commit is contained in:
hathach committed 2020-03-18 14:22:36 +07:00
1 parent f0440b55ea
commit f35ca052f5
4 files changed
+46 -33

No files matched your search

+14 -8
View File
@@ -55,7 +55,8 @@ BLEConnection::BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const* e
_hvn_sem = xSemaphoreCreateCounting(hvn_qsize, hvn_qsize);
_wrcmd_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize);
_secured = false;
_sec_mode.sm = _sec_mode.lv = 1; // default to open
_bonded = false;
_hvc_sem = NULL;
_hvc_received = false;
@@ -84,7 +85,7 @@ bool BLEConnection::connected(void)
bool BLEConnection::paired (void)
{
return _secured;
return secured();
}
bool BLEConnection::bonded(void)
@@ -94,7 +95,7 @@ bool BLEConnection::bonded(void)
bool BLEConnection::secured(void)
{
return _secured;
return !(_sec_mode.sm == 1 && _sec_mode.lv == 1);
}
uint8_t BLEConnection::getRole (void)
@@ -132,6 +133,11 @@ uint16_t BLEConnection::getPeerName(char* buf, uint16_t bufsize)
return Bluefruit.Gatt.readCharByUuid(_conn_hdl, BLEUuid(BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME), buf, bufsize);
}
ble_gap_conn_sec_mode_t BLEConnection::getSecureMode(void)
{
return _sec_mode;
}
static inline bool is_tx_power_valid(int8_t power)
{
#if defined(NRF52832_XXAA)
@@ -267,7 +273,7 @@ bool BLEConnection::removeBondKey(void)
bool BLEConnection::requestPairing(void)
{
// skip if already paired
if ( _secured ) return true;
if ( secured() ) return true;
return Bluefruit.Pairing._authenticate(_conn_hdl);
}
@@ -299,11 +305,11 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
{
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
// Connection is secured (paired) if encryption level > 1
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
{
_secured = true;
_sec_mode = conn_sec->sec_mode;
// Connection is secured (paired) if encryption level > 1
if ( this->secured() )
{
// Try to restore CCCD with bonded peer, if it doesn't exist (newly bonded), initialize it
if ( !loadCccd() ) sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
}
+4 -2
View File
@@ -54,10 +54,11 @@ class BLEConnection
uint16_t _ediv;
bool _connected;
bool _secured;
bool _bonded; // have LTK stored in InternalFS
bool _hvc_received;
ble_gap_conn_sec_mode_t _sec_mode;
ble_gap_addr_t _peer_addr; // resolvable connect address
ble_gap_addr_t _bond_id_addr; // address stored as bonded
@@ -66,7 +67,6 @@ class BLEConnection
// On-demand semaphore/data that are created on the fly
SemaphoreHandle_t _hvc_sem;
SemaphoreHandle_t _pair_sem;
public:
BLEConnection(uint16_t conn_hdl, ble_gap_evt_connected_t const * evt_connected, uint8_t hvn_qsize, uint8_t wrcmd_qsize);
@@ -87,6 +87,8 @@ class BLEConnection
ble_gap_addr_t getPeerAddr(void);
uint16_t getPeerName(char* buf, uint16_t bufsize);
ble_gap_conn_sec_mode_t getSecureMode(void);
bool disconnect(void);
bool setTxPower(int8_t power); // set power for this connection
+27 -22
View File
@@ -33,6 +33,21 @@
#define LESC_SUPPORTED 0
#endif
// default is Just Work
static const ble_gap_sec_params_t _sec_param_default =
{
.bond = 1,
.mitm = 0,
.lesc = LESC_SUPPORTED,
.keypress = 0,
.io_caps = BLE_GAP_IO_CAPS_NONE,
.oob = 0,
.min_key_size = 7,
.max_key_size = 16,
.kdist_own = { .enc = 1, .id = 1},
.kdist_peer = { .enc = 1, .id = 1}
};
//------------- IMPLEMENTATION -------------//
// convert N-byte Number from Big <-> Little Endian to use with BLE
@@ -50,20 +65,16 @@ static void swap_endian(uint8_t data[], uint32_t nbytes)
}
}
// default is Just Work
static const ble_gap_sec_params_t _sec_param_default =
static void _passkey_display_cabllack_dfr(BLEPairing::pair_passkey_cb_t func, uint16_t conn_hdl, uint8_t const passkey[6], bool match_request)
{
.bond = 1,
.mitm = 0,
.lesc = LESC_SUPPORTED,
.keypress = 0,
.io_caps = BLE_GAP_IO_CAPS_NONE,
.oob = 0,
.min_key_size = 7,
.max_key_size = 16,
.kdist_own = { .enc = 1, .id = 1},
.kdist_peer = { .enc = 1, .id = 1}
};
bool matched = func(conn_hdl, passkey, match_request);
if (match_request)
{
// Match request require to report the match (numberic comparison)
sd_ble_gap_auth_key_reply(conn_hdl, matched ? BLE_GAP_AUTH_KEY_TYPE_PASSKEY : BLE_GAP_AUTH_KEY_TYPE_NONE, NULL);
}
}
BLEPairing::BLEPairing(void)
{
@@ -294,13 +305,7 @@ void BLEPairing::_eventHandler(ble_evt_t* evt)
// Invoke display callback
if ( _passkey_cb )
{
bool matched = _passkey_cb(conn_hdl, passkey_display->passkey, passkey_display->match_request);
if (passkey_display->match_request)
{
// Match request require to report the match (numberic comparison)
sd_ble_gap_auth_key_reply(conn_hdl, matched ? BLE_GAP_AUTH_KEY_TYPE_PASSKEY : BLE_GAP_AUTH_KEY_TYPE_NONE, NULL);
}
ada_callback(passkey_display->passkey, 6, _passkey_display_cabllack_dfr, _passkey_cb, conn_hdl, passkey_display->passkey, passkey_display->match_request);
}
}
break;
@@ -392,9 +397,9 @@ void BLEPairing::_eventHandler(ble_evt_t* evt)
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
LOG_LV2("PAIR", "Security Mode = %d, Level = %d", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
if ( conn->secured() && _secured_cb )
if ( _secured_cb )
{
ada_callback(NULL, 0, _secured_cb, conn_hdl, conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
ada_callback(NULL, 0, _secured_cb, conn_hdl);
}
}
break;
+1 -1
View File
@@ -37,7 +37,7 @@ class BLEPairing
public:
typedef bool (*pair_passkey_cb_t ) (uint16_t conn_hdl, uint8_t const passkey[6], bool match_request);
typedef void (*pair_complete_cb_t) (uint16_t conn_hdl, uint8_t auth_status);
typedef void (*pair_secured_cb_t) (uint16_t conn_hdl, uint8_t sec_mode, uint8_t level);
typedef void (*pair_secured_cb_t) (uint16_t conn_hdl);
BLEPairing(void);