more ble gap works

This commit is contained in:
hathach committed 2019-01-17 18:55:23 +07:00
1 parent e312da361e
commit 56ca1fdc48
5 files changed
+80 -82

No files matched your search

+30 -32
View File
@@ -40,19 +40,17 @@ BLEGap::BLEGap(void)
{
memclr(_peers, sizeof(_peers));
memclr(_connection, sizeof(_connection));
varclr(&_prph);
varclr(&_central);
_cfg_prph.mtu_max = BLE_GATT_ATT_MTU_DEFAULT;
_cfg_central.mtu_max = BLE_GATT_ATT_MTU_DEFAULT;
_prph.mtu_max = BLE_GATT_ATT_MTU_DEFAULT;
_central.mtu_max = BLE_GATT_ATT_MTU_DEFAULT;
_cfg_prph.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
_cfg_prph.hvn_tx_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
_cfg_prph.wr_cmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
_prph.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
_prph.hvn_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
_prph.wrcmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
_cfg_central.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
_cfg_central.hvn_tx_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
_cfg_central.wr_cmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
_central.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
_central.hvn_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
_central.wrcmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
_sec_param = (ble_gap_sec_params_t)
{
@@ -72,18 +70,18 @@ BLEGap::BLEGap(void)
void BLEGap::configPrphConn(uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize)
{
_cfg_prph.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);
_cfg_prph.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
_cfg_prph.hvn_tx_qsize = hvn_qsize;
_cfg_prph.wr_cmd_qsize = wrcmd_qsize;
_prph.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);
_prph.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
_prph.hvn_qsize = hvn_qsize;
_prph.wrcmd_qsize = wrcmd_qsize;
}
void BLEGap::configCentralConn(uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize)
{
_cfg_central.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);
_cfg_central.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
_cfg_central.hvn_tx_qsize = hvn_qsize;
_cfg_central.wr_cmd_qsize = wrcmd_qsize;
_central.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);
_central.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
_central.hvn_qsize = hvn_qsize;
_central.wrcmd_qsize = wrcmd_qsize;
}
void BLEGap::_prph_setConnectCallback( connect_callback_t fp)
@@ -109,12 +107,12 @@ void BLEGap::_central_setDisconnectCallback( disconnect_callback_t fp)
uint16_t BLEGap::getMaxMtuByConnCfg(uint8_t conn_cfg)
{
return (conn_cfg == CONN_CFG_PERIPHERAL) ? _cfg_prph.mtu_max : _cfg_central.mtu_max;
return (conn_cfg == CONN_CFG_PERIPHERAL) ? _prph.mtu_max : _central.mtu_max;
}
uint16_t BLEGap::getMaxMtu (uint8_t conn_hdl)
{
return (getRole(conn_hdl) == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.mtu_max : _cfg_central.mtu_max;
return (getRole(conn_hdl) == BLE_GAP_ROLE_PERIPH) ? _prph.mtu_max : _central.mtu_max;
}
/**
@@ -309,10 +307,10 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
peer->role = para->role;
// Transmission buffer sem pool
uint16_t const hvn_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.hvn_tx_qsize : _cfg_central.hvn_tx_qsize;
uint8_t const hvn_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _prph.hvn_qsize : _central.hvn_qsize;
conn->hvn_tx_sem = xSemaphoreCreateCounting(hvn_qsize, hvn_qsize);
uint16_t const wrcmd_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _cfg_prph.wr_cmd_qsize : _cfg_central.wr_cmd_qsize;
uint8_t const wrcmd_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _prph.wrcmd_qsize : _central.wrcmd_qsize;
conn->wrcmd_tx_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize);
LOG_LV2("GAP", "Conn Interval= %f", para->conn_params.min_conn_interval*1.25f);
@@ -376,13 +374,13 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
{
// Pairing in progress, Peer asking for our info
peer->bond_keys = (bond_keys_t*) rtos_malloc( sizeof(bond_keys_t));
VERIFY(peer->bond_keys, );
conn->bond_keys = (bond_keys_t*) rtos_malloc( sizeof(bond_keys_t));
VERIFY(conn->bond_keys, );
bond_keys_t* bkeys = peer->bond_keys;
bond_keys_t* bkeys = conn->bond_keys;
memclr(bkeys, sizeof(bond_keys_t));
peer->ediv = 0xFFFF; // invalid value for ediv
conn->ediv = 0xFFFF; // invalid value for ediv
/* Step 1: Pairing/Bonding
* - Central supplies its parameters
@@ -432,16 +430,16 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
{
conn->paired = true;
peer->ediv = peer->bond_keys->own_enc.master_id.ediv;
conn->ediv = conn->bond_keys->own_enc.master_id.ediv;
bond_save_keys(conn->role, conn_hdl, peer->bond_keys);
bond_save_keys(conn->role, conn_hdl, conn->bond_keys);
}else
{
PRINT_HEX(status->auth_status);
}
rtos_free(peer->bond_keys);
peer->bond_keys = NULL;
rtos_free(conn->bond_keys);
conn->bond_keys = NULL;
}
break;
@@ -459,7 +457,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
{
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &bkeys.own_enc.enc_info, &bkeys.peer_id.id_info, NULL);
peer->ediv = bkeys.own_enc.master_id.ediv;
conn->ediv = bkeys.own_enc.master_id.ediv;
} else
{
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
@@ -477,7 +475,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
{
// Previously bonded --> secure by re-connection process --> Load & Set SysAttr (Apply Service Context)
// Else Init SysAttr (first bonded)
if ( !bond_load_cccd(conn->role, conn_hdl, peer->ediv) )
if ( !bond_load_cccd(conn->role, conn_hdl, conn->ediv) )
{
sd_ble_gatts_sys_attr_set(conn_hdl, NULL, 0, 0);
}
+20 -18
View File
@@ -72,6 +72,13 @@ class BLEGapConnection
SemaphoreHandle_t hvn_tx_sem;
SemaphoreHandle_t wrcmd_tx_sem;
// On-demand semaphore that are created on the fly
SemaphoreHandle_t hvc_sem;
bool hvc_received;
uint16_t ediv;
bond_keys_t* bond_keys; // Shared keys with bonded device, size ~ 80 bytes
};
class BLEGap
@@ -126,25 +133,25 @@ class BLEGap
// Array of TX Packet semaphore, indexed by connection handle
// Peer info where conn_hdl serves as index
typedef struct {
uint16_t ediv;
bond_keys_t* bond_keys; // Shared keys with bonded device, size ~ 80 bytes
bool hvc_received;
uint8_t role;
// On-demand semaphore that are created on the fly
SemaphoreHandle_t hvc_sem;
} gap_peer_t;
gap_peer_t* _get_peer(uint16_t conn_hdl) { return &_peers[conn_hdl]; }
BLEGapConnection* _get_connection(uint16_t conn_hdl)
{
return _connection[conn_hdl];
}
private:
struct {
uint16_t mtu_max;
uint8_t event_len;
uint8_t hvn_tx_qsize;
uint8_t wr_cmd_qsize;
} _cfg_prph, _cfg_central;
// Bandwidth configuration
uint16_t mtu_max;
uint8_t event_len;
uint8_t hvn_qsize;
uint8_t wrcmd_qsize;
connect_callback_t connect_cb;
disconnect_callback_t disconnect_cb;
} _prph, _central;
gap_peer_t _peers[BLE_MAX_CONN];
@@ -152,11 +159,6 @@ class BLEGap
BLEGapConnection* _connection[BLE_MAX_CONN];
struct {
connect_callback_t connect_cb;
disconnect_callback_t disconnect_cb;
}_prph, _central;
friend class AdafruitBluefruit;
};
+13 -15
View File
@@ -77,17 +77,17 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe
bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl)
{
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(conn_hdl);
BLEGapConnection* conn = Bluefruit.Gap._get_connection(conn_hdl);
// hvi confirm semaphore is created on the fly
peer->hvc_sem = xSemaphoreCreateBinary();
conn->hvc_sem = xSemaphoreCreateBinary();
xSemaphoreTake(peer->hvc_sem, portMAX_DELAY);
xSemaphoreTake(conn->hvc_sem, portMAX_DELAY);
vSemaphoreDelete(peer->hvc_sem);
peer->hvc_sem = NULL;
vSemaphoreDelete(conn->hvc_sem);
conn->hvc_sem = NULL;
return peer->hvc_received;
return conn->hvc_received;
}
void BLEGatt::_eventHandler(ble_evt_t* evt)
@@ -96,6 +96,8 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
const uint16_t evt_conn_hdl = evt->evt.common_evt.conn_handle;
const uint16_t evt_id = evt->header.evt_id;
BLEGapConnection* conn = (evt_conn_hdl == BLE_CONN_HANDLE_INVALID) ? NULL : Bluefruit.Gap._get_connection(evt_conn_hdl);
/*------------- Server service -------------*/
// TODO multiple peripherals
// if ( evt_conn_hdl == Bluefruit.connHandle() )
@@ -146,8 +148,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
// Save CCCD if paired
if ( Bluefruit.connPaired() && (evt_id == BLE_GATTS_EVT_WRITE) && (req_handle == chr->handles().cccd_handle) )
{
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
bond_save_cccd( Bluefruit.Gap.getRole(evt_conn_hdl), evt_conn_hdl, peer->ediv);
bond_save_cccd( Bluefruit.Gap.getRole(evt_conn_hdl), evt_conn_hdl, conn->ediv);
}
}
}
@@ -232,10 +233,9 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
case BLE_GATTS_EVT_HVC:
{
LOG_LV2("GATTS", "Confirm received handle = 0x%04X", evt->evt.gatts_evt.params.hvc.handle);
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
if ( peer->hvc_sem ) xSemaphoreGive(peer->hvc_sem);
peer->hvc_received = true;
if ( conn->hvc_sem ) xSemaphoreGive(conn->hvc_sem);
conn->hvc_received = true;
}
break;
@@ -243,10 +243,8 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
{
LOG_LV2("GATTS", "Timeout Source = %d", evt->evt.gatts_evt.params.timeout.src);
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
if ( peer->hvc_sem ) xSemaphoreGive(peer->hvc_sem);
peer->hvc_received = false;
if ( conn->hvc_sem ) xSemaphoreGive(conn->hvc_sem);
conn->hvc_received = false;
}
break;
+16 -16
View File
@@ -338,26 +338,26 @@ err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
// ATT MTU
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = CONN_CFG_PERIPHERAL;
blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = Gap._cfg_prph.mtu_max;
blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = Gap._prph.mtu_max;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATT, &blecfg, ram_start) );
// Event length and max connection for this config
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = CONN_CFG_PERIPHERAL;
blecfg.conn_cfg.params.gap_conn_cfg.conn_count = _prph_count;
blecfg.conn_cfg.params.gap_conn_cfg.event_length = Gap._cfg_prph.event_len;
blecfg.conn_cfg.params.gap_conn_cfg.event_length = Gap._prph.event_len;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GAP, &blecfg, ram_start) );
// HVN queue size
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = CONN_CFG_PERIPHERAL;
blecfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = Gap._cfg_prph.hvn_tx_qsize;
blecfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = Gap._prph.hvn_qsize;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &blecfg, ram_start) );
// WRITE COMMAND queue size
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = CONN_CFG_PERIPHERAL;
blecfg.conn_cfg.params.gattc_conn_cfg.write_cmd_tx_queue_size = Gap._cfg_prph.wr_cmd_qsize;
blecfg.conn_cfg.params.gattc_conn_cfg.write_cmd_tx_queue_size = Gap._prph.wrcmd_qsize;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTC, &blecfg, ram_start) );
}
@@ -366,26 +366,26 @@ err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
// ATT MTU
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = CONN_CFG_CENTRAL;
blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = Gap._cfg_central.mtu_max;
blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = Gap._central.mtu_max;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATT, &blecfg, ram_start) );
// Event length and max connection for this config
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = CONN_CFG_CENTRAL;
blecfg.conn_cfg.params.gap_conn_cfg.conn_count = _central_count;
blecfg.conn_cfg.params.gap_conn_cfg.event_length = Gap._cfg_central.event_len;
blecfg.conn_cfg.params.gap_conn_cfg.event_length = Gap._central.event_len;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GAP, &blecfg, ram_start) );
// HVN queue size
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = CONN_CFG_CENTRAL;
blecfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = Gap._cfg_central.hvn_tx_qsize;
blecfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = Gap._central.hvn_qsize;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &blecfg, ram_start) );
// WRITE COMMAND queue size
varclr(&blecfg);
blecfg.conn_cfg.conn_cfg_tag = CONN_CFG_CENTRAL;
blecfg.conn_cfg.params.gattc_conn_cfg.write_cmd_tx_queue_size = Gap._cfg_central.wr_cmd_qsize;
blecfg.conn_cfg.params.gattc_conn_cfg.write_cmd_tx_queue_size = Gap._central.wrcmd_qsize;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTC, &blecfg, ram_start) );
}
@@ -903,19 +903,19 @@ void AdafruitBluefruit::printInfo(void)
Serial.print(" - ");
Serial.printf(title_fmt, "Max MTU");
Serial.println(Gap._cfg_prph.mtu_max);
Serial.println(Gap._prph.mtu_max);
Serial.print(" - ");
Serial.printf(title_fmt, "Event Length");
Serial.println(Gap._cfg_prph.event_len);
Serial.println(Gap._prph.event_len);
Serial.print(" - ");
Serial.printf(title_fmt, "HVN Queue Size");
Serial.println(Gap._cfg_prph.hvn_tx_qsize);
Serial.println(Gap._prph.hvn_qsize);
Serial.print(" - ");
Serial.printf(title_fmt, "WrCmd Queue Size");
Serial.println(Gap._cfg_prph.wr_cmd_qsize);
Serial.println(Gap._prph.wrcmd_qsize);
}
if ( _central_count )
@@ -924,19 +924,19 @@ void AdafruitBluefruit::printInfo(void)
Serial.print(" - ");
Serial.printf(title_fmt, "Max MTU");
Serial.println(Gap._cfg_central.mtu_max);
Serial.println(Gap._central.mtu_max);
Serial.print(" - ");
Serial.printf(title_fmt, "Event Length");
Serial.println(Gap._cfg_central.event_len);
Serial.println(Gap._central.event_len);
Serial.print(" - ");
Serial.printf(title_fmt, "HVN Queue Size");
Serial.println(Gap._cfg_central.hvn_tx_qsize);
Serial.println(Gap._central.hvn_qsize);
Serial.print(" - ");
Serial.printf(title_fmt, "WrCmd Queue Size");
Serial.println(Gap._cfg_central.wr_cmd_qsize);
Serial.println(Gap._central.wrcmd_qsize);
}
/*------------- Settings -------------*/
@@ -141,7 +141,7 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt
{
bond_keys_t bkeys;
if ( bond_load_keys( BLE_GAP_ROLE_PERIPH, Bluefruit.Gap._get_peer(conn_hdl)->ediv, &bkeys ) )
if ( bond_load_keys( BLE_GAP_ROLE_PERIPH, Bluefruit.Gap._get_connection(conn_hdl)->ediv, &bkeys ) )
{
peer_data->addr = bkeys.peer_id.id_addr_info;
peer_data->irk = bkeys.peer_id.id_info;