move pairing event handle code to BLEGap
- add bond_init() - move thing around
This commit is contained in:
1 parent
86ea7e7ae5
commit
c4e25295e2
7 files changed
+349
-220
No files matched your search
@@ -53,7 +53,19 @@ BLEGap::BLEGap(void)
|
||||
_cfg_central.wr_cmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
|
||||
#endif
|
||||
|
||||
|
||||
_sec_param = (ble_gap_sec_params_t)
|
||||
{
|
||||
.bond = 1,
|
||||
.mitm = 0,
|
||||
.lesc = 0,
|
||||
.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},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -83,19 +95,19 @@ uint16_t BLEGap::getMaxMtuByConnCfg(uint8_t conn_cfg)
|
||||
return (conn_cfg == CONN_CFG_PERIPHERAL) ? _cfg_prph.mtu_max : _cfg_central.mtu_max;
|
||||
}
|
||||
|
||||
uint16_t BLEGap::getMaxMtu (uint8_t conn_handle)
|
||||
uint16_t BLEGap::getMaxMtu (uint8_t conn_hdl)
|
||||
{
|
||||
return (getRole(conn_handle) == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.mtu_max : _cfg_central.mtu_max;
|
||||
return (getRole(conn_hdl) == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.mtu_max : _cfg_central.mtu_max;
|
||||
}
|
||||
|
||||
uint8_t BLEGap::getHvnQueueSize (uint8_t conn_handle)
|
||||
uint8_t BLEGap::getHvnQueueSize (uint8_t conn_hdl)
|
||||
{
|
||||
return (getRole(conn_handle) == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.hvn_tx_qsize : _cfg_central.hvn_tx_qsize;
|
||||
return (getRole(conn_hdl) == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.hvn_tx_qsize : _cfg_central.hvn_tx_qsize;
|
||||
}
|
||||
|
||||
uint8_t BLEGap::getWriteCmdQueueSize (uint8_t conn_handle)
|
||||
uint8_t BLEGap::getWriteCmdQueueSize (uint8_t conn_hdl)
|
||||
{
|
||||
return (getRole(conn_handle) == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.wr_cmd_qsize : _cfg_central.wr_cmd_qsize;
|
||||
return (getRole(conn_hdl) == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.wr_cmd_qsize : _cfg_central.wr_cmd_qsize;
|
||||
}
|
||||
|
||||
|
||||
@@ -142,52 +154,76 @@ bool BLEGap::setAddr(uint8_t mac[6], uint8_t type)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLEGap::connected(uint16_t conn_handle)
|
||||
bool BLEGap::connected(uint16_t conn_hdl)
|
||||
{
|
||||
return _peers[conn_handle].connected;
|
||||
return _peers[conn_hdl].connected;
|
||||
}
|
||||
|
||||
uint8_t BLEGap::getRole(uint16_t conn_handle)
|
||||
bool BLEGap::bonded(uint16_t conn_hdl)
|
||||
{
|
||||
return _peers[conn_handle].role;
|
||||
return _peers[conn_hdl].bonded;
|
||||
}
|
||||
|
||||
uint8_t BLEGap::getPeerAddr(uint16_t conn_handle, uint8_t addr[6])
|
||||
bool BLEGap::requestPairing(uint16_t conn_hdl)
|
||||
{
|
||||
memcpy(addr, _peers[conn_handle].addr.addr, BLE_GAP_ADDR_LEN);
|
||||
return _peers[conn_handle].addr.addr_type;
|
||||
gap_peer_t* peer = &_peers[conn_hdl];
|
||||
|
||||
// skip if already bonded
|
||||
if ( peer->bonded ) return true;
|
||||
|
||||
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
|
||||
uint32_t start = millis();
|
||||
|
||||
// timeout in 30 seconds
|
||||
while ( ! ((volatile bool) peer->bonded) && (start + 30000 > millis()) )
|
||||
{
|
||||
yield();
|
||||
}
|
||||
|
||||
return peer->bonded;
|
||||
}
|
||||
|
||||
ble_gap_addr_t BLEGap::getPeerAddr(uint16_t conn_handle)
|
||||
uint8_t BLEGap::getRole(uint16_t conn_hdl)
|
||||
{
|
||||
return _peers[conn_handle].addr;
|
||||
return _peers[conn_hdl].role;
|
||||
}
|
||||
|
||||
bool BLEGap::getHvnPacket(uint16_t conn_handle)
|
||||
uint8_t BLEGap::getPeerAddr(uint16_t conn_hdl, uint8_t addr[6])
|
||||
{
|
||||
VERIFY( (conn_handle < BLE_MAX_CONN) && (_peers[conn_handle].hvn_tx_sem != NULL) );
|
||||
|
||||
return xSemaphoreTake(_peers[conn_handle].hvn_tx_sem, ms2tick(BLE_GENERIC_TIMEOUT));
|
||||
memcpy(addr, _peers[conn_hdl].addr.addr, BLE_GAP_ADDR_LEN);
|
||||
return _peers[conn_hdl].addr.addr_type;
|
||||
}
|
||||
|
||||
bool BLEGap::getWriteCmdPacket(uint16_t conn_handle)
|
||||
ble_gap_addr_t BLEGap::getPeerAddr(uint16_t conn_hdl)
|
||||
{
|
||||
return _peers[conn_hdl].addr;
|
||||
}
|
||||
|
||||
bool BLEGap::getHvnPacket(uint16_t conn_hdl)
|
||||
{
|
||||
VERIFY( (conn_hdl < BLE_MAX_CONN) && (_peers[conn_hdl].hvn_tx_sem != NULL) );
|
||||
|
||||
return xSemaphoreTake(_peers[conn_hdl].hvn_tx_sem, ms2tick(BLE_GENERIC_TIMEOUT));
|
||||
}
|
||||
|
||||
bool BLEGap::getWriteCmdPacket(uint16_t conn_hdl)
|
||||
{
|
||||
#if SD_VER < 500
|
||||
return getHvnPacket(conn_handle);
|
||||
return getHvnPacket(conn_hdl);
|
||||
#else
|
||||
VERIFY( (conn_handle < BLE_MAX_CONN) && (_peers[conn_handle].wrcmd_tx_sem != NULL) );
|
||||
return xSemaphoreTake(_peers[conn_handle].wrcmd_tx_sem, ms2tick(BLE_GENERIC_TIMEOUT));
|
||||
VERIFY( (conn_hdl < BLE_MAX_CONN) && (_peers[conn_hdl].wrcmd_tx_sem != NULL) );
|
||||
return xSemaphoreTake(_peers[conn_hdl].wrcmd_tx_sem, ms2tick(BLE_GENERIC_TIMEOUT));
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t BLEGap::getMTU (uint16_t conn_handle)
|
||||
uint16_t BLEGap::getMTU (uint16_t conn_hdl)
|
||||
{
|
||||
return _peers[conn_handle].att_mtu;
|
||||
return _peers[conn_hdl].att_mtu;
|
||||
}
|
||||
|
||||
uint16_t BLEGap::getPeerName(uint16_t conn_handle, char* buf, uint16_t bufsize)
|
||||
uint16_t BLEGap::getPeerName(uint16_t conn_hdl, char* buf, uint16_t bufsize)
|
||||
{
|
||||
return Bluefruit.Gatt.readCharByUuid(conn_handle, BLEUuid(BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME), buf, bufsize);
|
||||
return Bluefruit.Gatt.readCharByUuid(conn_hdl, BLEUuid(BLE_UUID_GAP_CHARACTERISTIC_DEVICE_NAME), buf, bufsize);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,9 +233,9 @@ uint16_t BLEGap::getPeerName(uint16_t conn_handle, char* buf, uint16_t bufsize)
|
||||
void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
{
|
||||
// conn handle has fixed offset regardless of event type
|
||||
const uint16_t conn_handle = evt->evt.common_evt.conn_handle;
|
||||
const uint16_t conn_hdl = evt->evt.common_evt.conn_handle;
|
||||
|
||||
gap_peer_t* peer = &_peers[conn_handle];
|
||||
gap_peer_t* peer = (conn_hdl == BLE_CONN_HANDLE_INVALID) ? NULL : &_peers[conn_hdl];
|
||||
|
||||
switch(evt->header.evt_id)
|
||||
{
|
||||
@@ -215,13 +251,13 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
// Init transmission buffer for notification
|
||||
#if SD_VER < 500
|
||||
uint8_t txbuf_max;
|
||||
(void) sd_ble_tx_packet_count_get(conn_handle, &txbuf_max);
|
||||
(void) sd_ble_tx_packet_count_get(conn_hdl, &txbuf_max);
|
||||
peer->hvn_tx_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
|
||||
#else
|
||||
peer->hvn_tx_sem = xSemaphoreCreateCounting(getHvnQueueSize(conn_handle), getHvnQueueSize(conn_handle));
|
||||
peer->hvn_tx_sem = xSemaphoreCreateCounting(getHvnQueueSize(conn_hdl), getHvnQueueSize(conn_hdl));
|
||||
#endif
|
||||
|
||||
peer->wrcmd_tx_sem = xSemaphoreCreateCounting(getWriteCmdQueueSize(conn_handle), getWriteCmdQueueSize(conn_handle));
|
||||
peer->wrcmd_tx_sem = xSemaphoreCreateCounting(getWriteCmdQueueSize(conn_hdl), getWriteCmdQueueSize(conn_hdl));
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -230,7 +266,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
ble_gap_evt_disconnected_t const* para = &evt->evt.gap_evt.params.disconnected;
|
||||
|
||||
// mark as disconnected, but keep the role for sub sequence event handler
|
||||
peer->connected = false;
|
||||
peer->connected = peer->bonded = false;
|
||||
|
||||
vSemaphoreDelete( peer->hvn_tx_sem );
|
||||
peer->hvn_tx_sem = NULL;
|
||||
@@ -241,7 +277,115 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
{
|
||||
// Pairing in progress, Peer asking for our info
|
||||
peer->bond_data = (bond_data_t*) rtos_malloc( sizeof(bond_data_t));
|
||||
VERIFY(peer->bond_data, );
|
||||
|
||||
bond_data_t* bdata = peer->bond_data;
|
||||
memclr(bdata, sizeof(bond_data_t));
|
||||
|
||||
peer->ediv = 0xFFFF; // invalid value for ediv
|
||||
|
||||
/* Step 1: Pairing/Bonding
|
||||
* - Central supplies its parameters
|
||||
* - We replies with our security parameters
|
||||
*/
|
||||
// ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
|
||||
COMMENT_OUT(
|
||||
// Change security parameter according to authentication type
|
||||
if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
|
||||
{
|
||||
sec_para.mitm = 1;
|
||||
sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
|
||||
}
|
||||
)
|
||||
|
||||
ble_gap_sec_keyset_t keyset =
|
||||
{
|
||||
.keys_own = {
|
||||
.p_enc_key = &bdata->own_enc,
|
||||
.p_id_key = NULL,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
},
|
||||
|
||||
.keys_peer = {
|
||||
.p_enc_key = &bdata->peer_enc,
|
||||
.p_id_key = &bdata->peer_id,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
}
|
||||
};
|
||||
|
||||
VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &_sec_param, &keyset), RETURN_VOID);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_AUTH_STATUS:
|
||||
{
|
||||
// Bonding process completed
|
||||
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
|
||||
|
||||
// Pairing/Bonding succeeded --> save encryption keys
|
||||
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
|
||||
{
|
||||
peer->bonded = true;
|
||||
peer->ediv = peer->bond_data->own_enc.master_id.ediv;
|
||||
|
||||
bond_save_keys(conn_hdl, peer->bond_data);
|
||||
}else
|
||||
{
|
||||
PRINT_HEX(status->auth_status);
|
||||
}
|
||||
|
||||
rtos_free(peer->bond_data);
|
||||
peer->bond_data = NULL;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_INFO_REQUEST:
|
||||
{
|
||||
// Reconnection. If bonded previously, Central will ask for stored keys.
|
||||
// return security information. Otherwise NULL
|
||||
ble_gap_evt_sec_info_request_t* sec_req = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
|
||||
|
||||
bond_data_t bdata;
|
||||
varclr(&bdata);
|
||||
|
||||
if ( bond_load_keys(sec_req->master_id.ediv, &bdata) )
|
||||
{
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &bdata.own_enc.enc_info, &bdata.peer_id.id_info, NULL);
|
||||
|
||||
peer->ediv = bdata.own_enc.master_id.ediv;
|
||||
} else
|
||||
{
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_CONN_SEC_UPDATE:
|
||||
// Connection is secured aka Paired
|
||||
|
||||
// Previously bonded --> secure by re-connection process
|
||||
// --> Load & Set Sys Attr (Apply Service Context)
|
||||
// Else Init Sys Attr
|
||||
bond_load_cccd(conn_hdl, peer->ediv);
|
||||
|
||||
// Paired is Bonded (as we always save keys)
|
||||
peer->bonded = true;
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_PASSKEY_DISPLAY:
|
||||
{
|
||||
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
|
||||
//
|
||||
// PRINT_INT(passkey_display->match_request);
|
||||
// PRINT_BUFFER(passkey_display->passkey, 6);
|
||||
|
||||
// sd_ble_gap_auth_key_reply
|
||||
}
|
||||
break;
|
||||
|
||||
#if SD_VER < 500
|
||||
@@ -283,7 +427,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
param->max_tx_octets, param->max_rx_octets, param->max_tx_time_us, param->max_rx_time_us);
|
||||
|
||||
// Let Softdevice decide the data length
|
||||
VERIFY_STATUS( sd_ble_gap_data_length_update(conn_handle, NULL, NULL), );
|
||||
VERIFY_STATUS( sd_ble_gap_data_length_update(conn_hdl, NULL, NULL), );
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -306,7 +450,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
// Tell SoftDevice to choose PHY automatically
|
||||
ble_gap_phys_t phy = { BLE_GAP_PHY_AUTO, BLE_GAP_PHY_AUTO };
|
||||
(void) sd_ble_gap_phy_update(conn_handle, &phy);
|
||||
(void) sd_ble_gap_phy_update(conn_hdl, &phy);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -329,8 +473,8 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
|
||||
{
|
||||
peer->att_mtu = minof(evt->evt.gatts_evt.params.exchange_mtu_request.client_rx_mtu, getMaxMtu(conn_handle));
|
||||
VERIFY_STATUS( sd_ble_gatts_exchange_mtu_reply(conn_handle, peer->att_mtu), );
|
||||
peer->att_mtu = minof(evt->evt.gatts_evt.params.exchange_mtu_request.client_rx_mtu, getMaxMtu(conn_hdl));
|
||||
VERIFY_STATUS( sd_ble_gatts_exchange_mtu_reply(conn_hdl, peer->att_mtu), );
|
||||
|
||||
LOG_LV1("GAP", "ATT MTU is changed to %d", peer->att_mtu);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include <Arduino.h>
|
||||
#include "bluefruit_common.h"
|
||||
#include "BLEUuid.h"
|
||||
#include "utility/bonding.h"
|
||||
|
||||
enum
|
||||
{
|
||||
@@ -49,8 +50,8 @@ enum
|
||||
class BLEGap
|
||||
{
|
||||
public:
|
||||
typedef void (*connect_callback_t ) (uint16_t conn_handle);
|
||||
typedef void (*disconnect_callback_t ) (uint16_t conn_handle, uint8_t reason);
|
||||
typedef void (*connect_callback_t ) (uint16_t conn_hdl);
|
||||
typedef void (*disconnect_callback_t ) (uint16_t conn_hdl, uint8_t reason);
|
||||
|
||||
BLEGap(void);
|
||||
|
||||
@@ -58,29 +59,29 @@ class BLEGap
|
||||
bool setAddr (uint8_t mac[6], uint8_t type);
|
||||
// bool setPrivacy (); sd_ble_gap_privacy_set()
|
||||
|
||||
bool connected (uint16_t conn_handle);
|
||||
bool connected (uint16_t conn_hdl);
|
||||
bool bonded (uint16_t conn_hdl);
|
||||
bool requestPairing (uint16_t conn_hdl);
|
||||
|
||||
uint8_t getRole (uint16_t conn_handle);
|
||||
uint8_t getRole (uint16_t conn_hdl);
|
||||
|
||||
uint8_t getPeerAddr (uint16_t conn_handle, uint8_t addr[6]);
|
||||
ble_gap_addr_t getPeerAddr (uint16_t conn_handle);
|
||||
uint16_t getPeerName (uint16_t conn_handle, char* buf, uint16_t bufsize);
|
||||
uint8_t getPeerAddr (uint16_t conn_hdl, uint8_t addr[6]);
|
||||
ble_gap_addr_t getPeerAddr (uint16_t conn_hdl);
|
||||
uint16_t getPeerName (uint16_t conn_hdl, char* buf, uint16_t bufsize);
|
||||
|
||||
uint16_t getMTU (uint16_t conn_handle);
|
||||
uint16_t getMTU (uint16_t conn_hdl);
|
||||
uint16_t getMaxMtuByConnCfg (uint8_t conn_cfg);
|
||||
uint16_t getMaxMtu (uint8_t conn_handle);
|
||||
uint16_t getMaxMtu (uint8_t conn_hdl);
|
||||
|
||||
uint8_t getHvnQueueSize (uint8_t conn_handle);
|
||||
uint8_t getWriteCmdQueueSize (uint8_t conn_handle);
|
||||
uint8_t getHvnQueueSize (uint8_t conn_hdl);
|
||||
uint8_t getWriteCmdQueueSize (uint8_t conn_hdl);
|
||||
|
||||
bool getHvnPacket (uint16_t conn_handle);
|
||||
bool getWriteCmdPacket (uint16_t conn_handle);
|
||||
bool getHvnPacket (uint16_t conn_hdl);
|
||||
bool getWriteCmdPacket (uint16_t conn_hdl);
|
||||
|
||||
void configPrphConn (uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize);
|
||||
void configCentralConn (uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize);
|
||||
|
||||
// bool requestPairing (uint16_t conn_handle);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
@@ -88,6 +89,28 @@ class BLEGap
|
||||
*------------------------------------------------------------------*/
|
||||
void _eventHandler(ble_evt_t* evt);
|
||||
|
||||
// Array of TX Packet semaphore, indexed by connection handle
|
||||
// Peer info where conn_hdl serves as index
|
||||
typedef struct {
|
||||
bool connected;
|
||||
bool bonded;
|
||||
uint8_t role;
|
||||
uint16_t att_mtu;
|
||||
|
||||
ble_gap_addr_t addr;
|
||||
|
||||
uint16_t ediv;
|
||||
bond_data_t* bond_data;
|
||||
|
||||
SemaphoreHandle_t hvn_tx_sem;
|
||||
SemaphoreHandle_t wrcmd_tx_sem;
|
||||
|
||||
SemaphoreHandle_t indicate_confirm_sem;
|
||||
bool hvc_received;
|
||||
} gap_peer_t;
|
||||
|
||||
gap_peer_t* _get_peer(uint16_t conn_hdl) { return &_peers[conn_hdl]; }
|
||||
|
||||
private:
|
||||
struct {
|
||||
uint16_t mtu_max;
|
||||
@@ -96,25 +119,9 @@ class BLEGap
|
||||
uint8_t wr_cmd_qsize;
|
||||
} _cfg_prph, _cfg_central;
|
||||
|
||||
// Array of TX Packet semaphore, indexed by connection handle
|
||||
// Peer info where conn_handle serves as index
|
||||
typedef struct {
|
||||
bool connected;
|
||||
uint8_t role;
|
||||
uint16_t att_mtu;
|
||||
|
||||
ble_gap_addr_t addr;
|
||||
|
||||
SemaphoreHandle_t hvn_tx_sem;
|
||||
SemaphoreHandle_t wrcmd_tx_sem;
|
||||
|
||||
SemaphoreHandle_t indicate_confirm_sem;
|
||||
bool hvc_received;
|
||||
} gap_peer_t;
|
||||
|
||||
gap_peer_t _peers[BLE_MAX_CONN];
|
||||
|
||||
gap_peer_t* _get_peer(uint16_t conn_hdl) { return &_peers[conn_hdl]; }
|
||||
ble_gap_sec_params_t _sec_param;
|
||||
|
||||
friend class AdafruitBluefruit;
|
||||
friend class BLEGatt;
|
||||
|
||||
@@ -109,7 +109,6 @@ AdafruitBluefruit::AdafruitBluefruit(void)
|
||||
_tx_power = 0;
|
||||
|
||||
_conn_hdl = BLE_CONN_HANDLE_INVALID;
|
||||
_bonded = false;
|
||||
|
||||
_ppcp_min_conn = BLE_GAP_CONN_MIN_INTERVAL_DFLT;
|
||||
_ppcp_max_conn = BLE_GAP_CONN_MAX_INTERVAL_DFLT;
|
||||
@@ -125,20 +124,6 @@ COMMENT_OUT(
|
||||
|
||||
varclr(&_bond_data);
|
||||
_bond_data.own_enc.master_id.ediv = 0xFFFF; // invalid value for ediv
|
||||
|
||||
_sec_param = (ble_gap_sec_params_t)
|
||||
{
|
||||
.bond = 1,
|
||||
.mitm = 0,
|
||||
.lesc = 0,
|
||||
.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},
|
||||
};
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::configServiceChanged(bool changed)
|
||||
@@ -464,9 +449,8 @@ err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
|
||||
// Create Timer for led advertising blinky
|
||||
_led_blink_th = xTimerCreate(NULL, ms2tick(CFG_ADV_BLINKY_INTERVAL/2), true, NULL, bluefruit_blinky_cb);
|
||||
|
||||
// Initialize nffs for bonding (it is safe to call nffs_pkg_init() multiple time)
|
||||
Nffs.begin();
|
||||
(void) Nffs.mkdir_p(CFG_BOND_NFFS_DIR);
|
||||
// Initialize bonding
|
||||
bond_init();
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
@@ -597,7 +581,7 @@ uint16_t AdafruitBluefruit::connHandle(void)
|
||||
|
||||
bool AdafruitBluefruit::connPaired(void)
|
||||
{
|
||||
return _bonded;
|
||||
return Gap.bonded(_conn_hdl);
|
||||
}
|
||||
|
||||
uint16_t AdafruitBluefruit::connInterval(void)
|
||||
@@ -944,109 +928,94 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
LOG_LV2("GAP", "Disconnect Reason 0x%02X", evt->evt.gap_evt.params.disconnected.reason);
|
||||
|
||||
_conn_hdl = BLE_CONN_HANDLE_INVALID;
|
||||
_bonded = false;
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
{
|
||||
// Pairing in progress
|
||||
varclr(&_bond_data);
|
||||
_bond_data.own_enc.master_id.ediv = 0xFFFF; // invalid value for ediv
|
||||
// case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
// // Pairing in progress
|
||||
// varclr(&_bond_data);
|
||||
// _bond_data.own_enc.master_id.ediv = 0xFFFF; // invalid value for ediv
|
||||
//
|
||||
// /* Step 1: Pairing/Bonding
|
||||
// * - Central supplies its parameters
|
||||
// * - We replies with our security parameters
|
||||
// */
|
||||
// // ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
|
||||
// COMMENT_OUT(
|
||||
// // Change security parameter according to authentication type
|
||||
// if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
|
||||
// {
|
||||
// sec_para.mitm = 1;
|
||||
// sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
|
||||
// }
|
||||
// )
|
||||
//
|
||||
// ble_gap_sec_keyset_t keyset =
|
||||
// {
|
||||
// .keys_own = {
|
||||
// .p_enc_key = &_bond_data.own_enc,
|
||||
// .p_id_key = NULL,
|
||||
// .p_sign_key = NULL,
|
||||
// .p_pk = NULL
|
||||
// },
|
||||
//
|
||||
// .keys_peer = {
|
||||
// .p_enc_key = &_bond_data.peer_enc,
|
||||
// .p_id_key = &_bond_data.peer_id,
|
||||
// .p_sign_key = NULL,
|
||||
// .p_pk = NULL
|
||||
// }
|
||||
// };
|
||||
//
|
||||
// VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &_sec_param, &keyset), RETURN_VOID);
|
||||
// break;
|
||||
|
||||
/* Step 1: Pairing/Bonding
|
||||
* - Central supplies its parameters
|
||||
* - We replies with our security parameters
|
||||
*/
|
||||
// ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
|
||||
COMMENT_OUT(
|
||||
// Change security parameter according to authentication type
|
||||
if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
|
||||
{
|
||||
sec_para.mitm = 1;
|
||||
sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
|
||||
}
|
||||
)
|
||||
// case BLE_GAP_EVT_SEC_INFO_REQUEST:
|
||||
// {
|
||||
// // Reconnection. If bonded previously, Central will ask for stored keys.
|
||||
// // return security information. Otherwise NULL
|
||||
// ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
|
||||
//
|
||||
// if ( bond_load_keys(sec_request->master_id.ediv, &_bond_data) )
|
||||
// {
|
||||
// sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond_data.own_enc.enc_info, &_bond_data.peer_id.id_info, NULL);
|
||||
// } else
|
||||
// {
|
||||
// sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
|
||||
ble_gap_sec_keyset_t keyset =
|
||||
{
|
||||
.keys_own = {
|
||||
.p_enc_key = &_bond_data.own_enc,
|
||||
.p_id_key = NULL,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
},
|
||||
|
||||
.keys_peer = {
|
||||
.p_enc_key = &_bond_data.peer_enc,
|
||||
.p_id_key = &_bond_data.peer_id,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
}
|
||||
};
|
||||
|
||||
VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &_sec_param, &keyset), RETURN_VOID);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_INFO_REQUEST:
|
||||
{
|
||||
// Reconnection. If bonded previously, Central will ask for stored keys.
|
||||
// return security information. Otherwise NULL
|
||||
ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
|
||||
|
||||
if ( bond_load_keys(sec_request->master_id.ediv, &_bond_data) )
|
||||
{
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond_data.own_enc.enc_info, &_bond_data.peer_id.id_info, NULL);
|
||||
} else
|
||||
{
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case BLE_GAP_EVT_PASSKEY_DISPLAY:
|
||||
{
|
||||
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
|
||||
//
|
||||
// PRINT_INT(passkey_display->match_request);
|
||||
// PRINT_BUFFER(passkey_display->passkey, 6);
|
||||
|
||||
// sd_ble_gap_auth_key_reply
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_CONN_SEC_UPDATE:
|
||||
{
|
||||
// Connection is secured aka Paired
|
||||
COMMENT_OUT( ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &evt->evt.gap_evt.params.conn_sec_update.conn_sec; )
|
||||
|
||||
// Previously bonded --> secure by re-connection process
|
||||
// --> Load & Set Sys Attr (Apply Service Context)
|
||||
// Else Init Sys Attr
|
||||
bond_load_cccd(evt_conn_hdl, _bond_data.own_enc.master_id.ediv);
|
||||
|
||||
// Consider Paired as Bonded
|
||||
_bonded = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_AUTH_STATUS:
|
||||
{
|
||||
// Bonding process completed
|
||||
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
|
||||
|
||||
// Pairing/Bonding succeeded --> save encryption keys
|
||||
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
|
||||
{
|
||||
_bonded = true;
|
||||
bond_save_keys(evt_conn_hdl, &_bond_data);
|
||||
}else
|
||||
{
|
||||
PRINT_HEX(status->auth_status);
|
||||
}
|
||||
}
|
||||
break;
|
||||
// case BLE_GAP_EVT_CONN_SEC_UPDATE:
|
||||
// {
|
||||
// // Connection is secured aka Paired
|
||||
// COMMENT_OUT( ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &evt->evt.gap_evt.params.conn_sec_update.conn_sec; )
|
||||
//
|
||||
// // Previously bonded --> secure by re-connection process
|
||||
// // --> Load & Set Sys Attr (Apply Service Context)
|
||||
// // Else Init Sys Attr
|
||||
// bond_load_cccd(evt_conn_hdl, _bond_data.own_enc.master_id.ediv);
|
||||
//
|
||||
// // Consider Paired as Bonded
|
||||
// _bonded = true;
|
||||
// }
|
||||
// break;
|
||||
//
|
||||
// case BLE_GAP_EVT_AUTH_STATUS:
|
||||
// {
|
||||
// // Bonding process completed
|
||||
// ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
|
||||
//
|
||||
// // Pairing/Bonding succeeded --> save encryption keys
|
||||
// if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
|
||||
// {
|
||||
// _bonded = true;
|
||||
// bond_save_keys(evt_conn_hdl, &_bond_data);
|
||||
// }else
|
||||
// {
|
||||
// PRINT_HEX(status->auth_status);
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
|
||||
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
|
||||
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
|
||||
@@ -1100,19 +1069,7 @@ void AdafruitBluefruit::_setConnLed (bool on_off)
|
||||
*------------------------------------------------------------------*/
|
||||
bool AdafruitBluefruit::requestPairing(void)
|
||||
{
|
||||
// skip if already bonded
|
||||
if (_bonded) return true;
|
||||
|
||||
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &_sec_param ), false);
|
||||
uint32_t start = millis();
|
||||
|
||||
// timeout in 30 seconds
|
||||
while ( !_bonded && (start + 30000 > millis()) )
|
||||
{
|
||||
yield();
|
||||
}
|
||||
|
||||
return _bonded;
|
||||
return Gap.requestPairing(_conn_hdl);
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::clearBonds(void)
|
||||
@@ -1122,14 +1079,18 @@ void AdafruitBluefruit::clearBonds(void)
|
||||
|
||||
void AdafruitBluefruit::_bledfu_get_bond_data(ble_gap_addr_t* addr, ble_gap_irk_t* irk, ble_gap_enc_key_t* enc_key)
|
||||
{
|
||||
if (!_bonded)
|
||||
{
|
||||
(*addr) = getPeerAddr();
|
||||
}else
|
||||
{
|
||||
(*addr) = _bond_data.peer_id.id_addr_info;
|
||||
(*irk) = _bond_data.peer_id.id_info;
|
||||
(*addr) = getPeerAddr();
|
||||
|
||||
(*enc_key) = _bond_data.own_enc;
|
||||
if ( connPaired() )
|
||||
{
|
||||
bond_data_t bdata;
|
||||
|
||||
if ( bond_load_keys( Gap._get_peer(_conn_hdl)->ediv, &bdata ) )
|
||||
{
|
||||
(*addr) = _bond_data.peer_id.id_addr_info;
|
||||
(*irk) = _bond_data.peer_id.id_info;
|
||||
|
||||
(*enc_key) = _bond_data.own_enc;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,8 +89,6 @@
|
||||
#include "clients/BLEClientHidAdafruit.h"
|
||||
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
|
||||
#include "utility/bonding.h"
|
||||
|
||||
enum
|
||||
@@ -228,13 +226,10 @@ class AdafruitBluefruit
|
||||
BLEDfu _dfu_svc;
|
||||
|
||||
uint16_t _conn_hdl;
|
||||
bool _bonded;
|
||||
|
||||
BLEGap::connect_callback_t _connect_cb;
|
||||
BLEGap::disconnect_callback_t _disconnect_cb;
|
||||
|
||||
ble_gap_sec_params_t _sec_param;
|
||||
|
||||
COMMENT_OUT(
|
||||
uint8_t _auth_type;
|
||||
char _pin[BLE_GAP_PASSKEY_LEN];
|
||||
|
||||
@@ -92,17 +92,19 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt
|
||||
(request->op != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) &&
|
||||
(request->op != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL))
|
||||
{
|
||||
uint16_t conn_hdl = Bluefruit.connHandle();
|
||||
|
||||
ble_gatts_rw_authorize_reply_params_t reply = { .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE };
|
||||
|
||||
if ( !chr.notifyEnabled() )
|
||||
{
|
||||
reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR;
|
||||
sd_ble_gatts_rw_authorize_reply(Bluefruit.connHandle(), &reply);
|
||||
sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
|
||||
return;
|
||||
}
|
||||
|
||||
reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
|
||||
sd_ble_gatts_rw_authorize_reply(Bluefruit.connHandle(), &reply);
|
||||
sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
|
||||
|
||||
enum { START_DFU = 1 };
|
||||
if ( request->data[0] == START_DFU )
|
||||
@@ -128,10 +130,22 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt
|
||||
|
||||
// Get CCCD
|
||||
uint16_t sysattr_len = sizeof(peer_data->sys_attr);
|
||||
sd_ble_gatts_sys_attr_get(Bluefruit.connHandle(), peer_data->sys_attr, &sysattr_len, BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
|
||||
sd_ble_gatts_sys_attr_get(conn_hdl, peer_data->sys_attr, &sysattr_len, BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
|
||||
|
||||
// Get Bond Data
|
||||
Bluefruit._bledfu_get_bond_data(&peer_data->addr, &peer_data->irk, &peer_data->enc_key);
|
||||
// Get Bond Data or using Address if not bonded
|
||||
peer_data->addr = Bluefruit.Gap.getPeerAddr(conn_hdl);
|
||||
|
||||
if ( Bluefruit.Gap.bonded(conn_hdl) )
|
||||
{
|
||||
bond_data_t bdata;
|
||||
|
||||
if ( bond_load_keys( Bluefruit.Gap._get_peer(conn_hdl)->ediv, &bdata ) )
|
||||
{
|
||||
peer_data->addr = bdata.peer_id.id_addr_info;
|
||||
peer_data->irk = bdata.peer_id.id_info;
|
||||
peer_data->enc_key = bdata.own_enc;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate crc
|
||||
peer_data->crc16 = crc16((uint8_t*) peer_data, offsetof(peer_data_t, crc16));
|
||||
|
||||
@@ -54,6 +54,13 @@
|
||||
* - CCCD variable
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
void bond_init(void)
|
||||
{
|
||||
// Initialize nffs for bonding (it is safe to call nffs_pkg_init() multiple time)
|
||||
Nffs.begin();
|
||||
(void) Nffs.mkdir_p(CFG_BOND_NFFS_DIR);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Keys
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
@@ -56,6 +56,8 @@ enum
|
||||
BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN
|
||||
};
|
||||
|
||||
void bond_init(void);
|
||||
void bond_clear(void);
|
||||
|
||||
void bond_save_keys(uint16_t conn_hdl, bond_data_t* bdata);
|
||||
bool bond_load_keys(uint16_t ediv, bond_data_t* bdata);
|
||||
@@ -63,7 +65,6 @@ bool bond_load_keys(uint16_t ediv, bond_data_t* bdata);
|
||||
void bond_save_cccd(uint16_t cond_hdl, uint16_t ediv);
|
||||
bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv);
|
||||
|
||||
void bond_clear(void);
|
||||
|
||||
|
||||
#endif /* BONDING_H_ */
|
||||
Reference in new issue
Block a user