Remove BLEGap, API function is taken by Bluefruit, BLEPeriph, BLECentral

This commit is contained in:
hathach committed 2019-02-01 16:16:20 +07:00
1 parent e40181e3e5
commit b7323d1819
19 files changed
+317 -479

No files matched your search

+2 -1
View File
@@ -16,6 +16,7 @@
- Gap.getPeerName() is replaced by Bluefruit.getPeerName()
- Enhance Gap, add BLEConnection class to mange peer connection
- Remove Gap.connected(), Gap.getRole() in prefer to BLEConnection API
- Remove BLEGap, API function is taken by Bluefruit, BLEPeriph, BLECentral
- Added setRssiCallback(), monitorRssi(), getRssi(), stopRssi() for tracking rssi of a connection
- rssi_poll and rssi_callback example sketches are added
- Change BLECharacteristic callback signature BLECharacteristic& to BLECharacteristic*
@@ -27,7 +28,7 @@
- Fixed #173 bleuart return incorrect value when failed to send (PR #178 thanks Nenik)
- Added Client Battery support BLEClientBas
- Added BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST event support for Central
- Added Jlink as programmer to upload sketch #133
- Added Jlink as programmer to upload sketch #133. Though at least one serial DFU upload is needed to disable firmware crc checking
- Fixed issue with high speed uart baud ~ 1 Mbps (PR #158 thanks Ureloc)
- Add HardwardPWM removePin(), refactor hwpwm.ino sketch
- Fixed print float issue with precision > 10
@@ -195,7 +195,7 @@ uint16_t HAPCharacteristic::writeHapValue(uint32_t num)
void HAPCharacteristic::createHapResponse(uint16_t conn_hdl, uint8_t status, TLV8_t tlv_para[], uint8_t count)
{
BLEConnection* conn = Bluefruit.Gap.Connection( conn_hdl );
BLEConnection* conn = Bluefruit.Connection( conn_hdl );
// Determine body len (not including 2 byte length itself)
uint16_t body_len = tlv8_encode_calculate_len(tlv_para, count);
@@ -358,7 +358,7 @@ void HAPCharacteristic::processGattWrite(uint16_t conn_hdl, ble_gatts_evt_write_
LOG_LV2("GATTS", "Write Op = %d, len = %d, offset = %d, uuid = 0x%04X", gatt_req->op, gatt_req->len, gatt_req->offset, gatt_req->uuid.uuid);
LOG_LV2_BUFFER(NULL, gatt_req->data, gatt_req->len);
BLEConnection* conn = Bluefruit.Gap.Connection( conn_hdl );
BLEConnection* conn = Bluefruit.Connection( conn_hdl );
uint16_t const gatt_mtu = conn->getMtu() - 3;
ble_gatts_rw_authorize_reply_params_t reply =
@@ -462,7 +462,7 @@ void HAPCharacteristic::processGattWrite(uint16_t conn_hdl, ble_gatts_evt_write_
void HAPCharacteristic::processGattRead(uint16_t conn_hdl, ble_gatts_evt_read_t* gatt_req)
{
BLEConnection* conn = Bluefruit.Gap.Connection( conn_hdl );
BLEConnection* conn = Bluefruit.Connection( conn_hdl );
uint16_t const gatt_mtu = conn->getMtu() - 3;
LOG_LV2("GATTS", "Read uuid = 0x%04X, offset = %d", gatt_req->uuid.uuid, gatt_req->offset);
@@ -44,7 +44,7 @@ void setup()
bleuart.begin();
// Set up Rssi changed callback
Bluefruit.Gap.setRssiCallback(rssi_changed_callback);
Bluefruit.setRssiCallback(rssi_changed_callback);
// Set up and start advertising
startAdv();
@@ -90,7 +90,7 @@ void connect_callback(uint16_t conn_handle)
Serial.println("Connected");
// Get the reference to current connection
BLEConnection* conn = Bluefruit.Gap.Connection(conn_handle);
BLEConnection* conn = Bluefruit.Connection(conn_handle);
// Start monitoring rssi of this connection
// This function should be called in connect callback
@@ -85,7 +85,7 @@ void loop()
uint16_t conn_hdl = Bluefruit.connHandle();
// Get the reference to current connected connection
BLEConnection* conn = Bluefruit.Gap.Connection(conn_hdl);
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
// get the RSSI value of this connection
// monitorRssi() must be called previously (in callback)
@@ -104,7 +104,7 @@ void connect_callback(uint16_t conn_handle)
Serial.println("Connected");
// Get the reference to current connection
BLEConnection* conn = Bluefruit.Gap.Connection(conn_handle);
BLEConnection* conn = Bluefruit.Connection(conn_handle);
// Start monitoring rssi of this connection
// This function should be called in connect callback
+8 -5
View File
@@ -42,6 +42,9 @@
*/
BLECentral::BLECentral(void)
{
_connect_cb = NULL;
_disconnect_cb = NULL;
_conn_param.min_conn_interval = _conn_param.max_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT;
_conn_param.slave_latency = BLE_GAP_CONN_SLAVE_LATENCY;
_conn_param.conn_sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS/10;
@@ -89,7 +92,7 @@ bool BLECentral::connect(const ble_gap_evt_adv_report_t* adv_report)
*/
bool BLECentral::connected(uint16_t conn_hdl)
{
BLEConnection* conn = Bluefruit.Gap.Connection(conn_hdl);
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
return conn && conn->connected() && (conn->getRole() == BLE_GAP_ROLE_CENTRAL);
}
@@ -107,14 +110,14 @@ uint8_t BLECentral::connected(void)
return count;
}
void BLECentral::setConnectCallback( BLEGap::connect_callback_t fp)
void BLECentral::setConnectCallback( ble_connect_callback_t fp )
{
Bluefruit.Gap._central_setConnectCallback(fp);
_connect_cb = fp;
}
void BLECentral::setDisconnectCallback( BLEGap::disconnect_callback_t fp)
void BLECentral::setDisconnectCallback( ble_disconnect_callback_t fp )
{
Bluefruit.Gap._central_setDisconnectCallback(fp);
_disconnect_cb = fp;
}
void BLECentral::clearBonds(void)
+10 -14
View File
@@ -39,23 +39,12 @@
#include <Arduino.h>
#include "bluefruit_common.h"
#include "BLEGap.h"
#include "BLEGatt.h"
#include "BLEUuid.h"
#include "BLECharacteristic.h"
#include "BLEClientCharacteristic.h"
#include "BLEService.h"
#include "BLEClientService.h"
class AdafruitBluefruit;
class BLECentral
{
public:
BLECentral(void); // Constructor
void begin(void);
/*------------------------------------------------------------------*/
@@ -73,13 +62,20 @@ class BLECentral
void clearBonds (void);
/*------------- Callbacks -------------*/
void setConnectCallback ( BLEGap::connect_callback_t fp);
void setDisconnectCallback( BLEGap::disconnect_callback_t fp);
void setConnectCallback ( ble_connect_callback_t fp);
void setDisconnectCallback( ble_disconnect_callback_t fp);
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
* Although declare as public, it is meant to be invoked by internal code.
*------------------------------------------------------------------*/
void _eventHandler(ble_evt_t* evt);
private:
ble_gap_conn_params_t _conn_param;
void _eventHandler(ble_evt_t* evt);
ble_connect_callback_t _connect_cb;
ble_disconnect_callback_t _disconnect_cb;
friend class AdafruitBluefruit;
};
@@ -574,7 +574,7 @@ bool BLECharacteristic::notify(const void* data, uint16_t len)
if ( notifyEnabled() )
{
// TODO multiple connection support
BLEConnection* conn = Bluefruit.Gap.Connection( Bluefruit.connHandle() );
BLEConnection* conn = Bluefruit.Connection( Bluefruit.connHandle() );
VERIFY(conn);
uint16_t const max_payload = conn->getMtu() - 3;
@@ -656,7 +656,7 @@ bool BLECharacteristic::indicate(const void* data, uint16_t len)
if ( indicateEnabled() )
{
uint16_t conn_hdl = Bluefruit.connHandle();
BLEConnection* conn = Bluefruit.Gap.Connection( conn_hdl );
BLEConnection* conn = Bluefruit.Connection( conn_hdl );
VERIFY(conn);
uint16_t const max_payload = conn->getMtu() - 3;
@@ -168,7 +168,7 @@ uint16_t BLEClientCharacteristic::read(void* buffer, uint16_t bufsize)
{
VERIFY( _chr.char_props.read, 0 );
BLEConnection* conn = Bluefruit.Gap.Connection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Connection( _service->connHandle() );
VERIFY(conn, 0);
uint16_t const max_payload = conn->getMtu() - 3;
@@ -205,7 +205,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
{
VERIFY( _chr.char_props.write, 0 );
BLEConnection* conn = Bluefruit.Gap.Connection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Connection( _service->connHandle() );
VERIFY(conn, 0);
uint16_t const max_payload = conn->getMtu() - 3;
@@ -281,7 +281,7 @@ uint16_t BLEClientCharacteristic::write(const void* data, uint16_t len)
{
// VERIFY( _chr.char_props.write_wo_resp, 0 );
BLEConnection* conn = Bluefruit.Gap.Connection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Connection( _service->connHandle() );
VERIFY(conn, 0);
uint16_t const max_payload = conn->getMtu() - 3;
@@ -362,7 +362,7 @@ bool BLEClientCharacteristic::writeCCCD(uint16_t value)
};
// TODO only Write without response consume a TX buffer
BLEConnection* conn = Bluefruit.Gap.Connection(conn_handle);
BLEConnection* conn = Bluefruit.Connection(conn_handle);
VERIFY( conn && conn->getWriteCmdPacket() );
VERIFY_STATUS( sd_ble_gattc_write(conn_handle, &param), false );
@@ -473,7 +473,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
}
else if ( wr_rsp->write_op == BLE_GATT_OP_PREP_WRITE_REQ)
{
BLEConnection* conn = Bluefruit.Gap.Connection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Connection( _service->connHandle() );
uint16_t const max_payload = conn->getMtu() - 3;
uint16_t packet_len = min16(_adamsg.remaining, max_payload-2);
@@ -523,7 +523,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
case BLE_GATTC_EVT_READ_RSP:
{
BLEConnection* conn = Bluefruit.Gap.Connection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Connection( _service->connHandle() );
uint16_t const max_payload = conn->getMtu() - 3;
ble_gattc_evt_read_rsp_t* rd_rsp = (ble_gattc_evt_read_rsp_t*) &evt->evt.gattc_evt.params.read_rsp;
@@ -180,7 +180,7 @@ bool BLEConnection::requestPairing(void)
// skip if already paired
if ( _paired ) return true;
ble_gap_sec_params_t sec_param = Bluefruit.Gap.getSecureParam();
ble_gap_sec_params_t sec_param = Bluefruit.getSecureParam();
// on-the-fly semaphore
_pair_sem = xSemaphoreCreateBinary();
@@ -306,7 +306,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
}
};
ble_gap_sec_params_t sec_param = Bluefruit.Gap.getSecureParam();
ble_gap_sec_params_t sec_param = Bluefruit.getSecureParam();
VERIFY_STATUS(sd_ble_gap_sec_params_reply(_conn_hdl,
BLE_GAP_SEC_STATUS_SUCCESS,
_role == BLE_GAP_ROLE_PERIPH ? &sec_param : NULL,
@@ -404,7 +404,7 @@ void BLEConnection::_eventHandler(ble_evt_t* evt)
case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
{
uint16_t const max_mtu = Bluefruit.Gap.getMaxMtu(_role);
uint16_t const max_mtu = Bluefruit.getMaxMtu(_role);
_mtu = minof(evt->evt.gatts_evt.params.exchange_mtu_request.client_rx_mtu, max_mtu);
VERIFY_STATUS( sd_ble_gatts_exchange_mtu_reply(_conn_hdl, _mtu), );
-269
View File
@@ -1,269 +0,0 @@
/**************************************************************************/
/*!
@file BLEGap.cpp
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include "bluefruit.h"
BLEGap::BLEGap(void)
{
_prph.mtu_max = BLE_GATT_ATT_MTU_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;
_prph.connect_cb = NULL;
_prph.disconnect_cb = NULL;
_central.mtu_max = BLE_GATT_ATT_MTU_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;
_central.connect_cb = NULL;
_central.disconnect_cb = NULL;
memclr(_connection, sizeof(_connection));
_rssi_cb = NULL;
_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 BLEGap::configPrphConn(uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t 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)
{
_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)
{
_prph.connect_cb = fp;
}
void BLEGap::_prph_setDisconnectCallback( disconnect_callback_t fp)
{
_prph.disconnect_cb = fp;
}
void BLEGap::_central_setConnectCallback( connect_callback_t fp)
{
_central.connect_cb = fp;
}
void BLEGap::_central_setDisconnectCallback( disconnect_callback_t fp)
{
_central.disconnect_cb = fp;
}
uint16_t BLEGap::getMaxMtu(uint8_t role)
{
return (role == BLE_GAP_ROLE_PERIPH) ? _prph.mtu_max : _central.mtu_max;
}
BLEConnection* BLEGap::Connection(uint16_t conn_hdl)
{
return (conn_hdl < BLE_MAX_CONNECTION) ?_connection[conn_hdl] : NULL;
}
void BLEGap::setRssiCallback(rssi_callback_t fp)
{
_rssi_cb = fp;
}
/**
* Event handler
*/
void BLEGap::_eventHandler(ble_evt_t* evt)
{
// conn handle has fixed offset regardless of event type
const uint16_t conn_hdl = evt->evt.common_evt.conn_handle;
BLEConnection* conn = this->Connection(conn_hdl);
// Connection handler
if ( conn ) conn->_eventHandler(evt);
switch(evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
{
// Turn on Conn LED
Bluefruit._stopConnLed();
Bluefruit._setConnLed(true);
ble_gap_evt_connected_t const * para = &evt->evt.gap_evt.params.connected;
if ( _connection[conn_hdl] )
{
LOG_LV1("GAP", "Connection is already in used, something wrong !!");
delete _connection[conn_hdl];
_connection[conn_hdl] = NULL;
}
// Transmission buffer pool
uint8_t const hvn_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _prph.hvn_qsize : _central.hvn_qsize;
uint8_t const wrcmd_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _prph.wrcmd_qsize : _central.wrcmd_qsize;
_connection[conn_hdl] = new BLEConnection(conn_hdl, para, hvn_qsize, wrcmd_qsize);
conn = _connection[conn_hdl];
// Invoke connect callback
if ( conn->getRole() == BLE_GAP_ROLE_PERIPH )
{
if ( _prph.connect_cb ) ada_callback(NULL, _prph.connect_cb, conn_hdl);
}else
{
if ( _central.connect_cb ) ada_callback(NULL, _central.connect_cb, conn_hdl);
}
}
break;
case BLE_GAP_EVT_DISCONNECTED:
{
ble_gap_evt_disconnected_t const* para = &evt->evt.gap_evt.params.disconnected;
LOG_LV2("GAP", "Disconnect Reason 0x%02X", evt->evt.gap_evt.params.disconnected.reason);
delete _connection[conn_hdl];
_connection[conn_hdl] = NULL;
// Turn off Conn LED If not connected at all
bool still_connected = false;
for (uint8_t i=0; i<BLE_MAX_CONNECTION; i++)
{
if ( _connection[i] && _connection[i]->connected() )
{
still_connected = true;
break;
}
}
if ( !still_connected ) Bluefruit._setConnLed(false);
// Invoke disconnect callback
if ( conn->getRole() == BLE_GAP_ROLE_PERIPH )
{
if ( _prph.disconnect_cb ) ada_callback(NULL, _prph.disconnect_cb, conn_hdl, para->reason);
}else
{
if ( _central.disconnect_cb ) ada_callback(NULL, _central.disconnect_cb, conn_hdl, para->reason);
}
}
break;
case BLE_GAP_EVT_RSSI_CHANGED:
{
ble_gap_evt_rssi_changed_t const * rssi_changed = &evt->evt.gap_evt.params.rssi_changed;
if ( _rssi_cb )
{
ada_callback(NULL, _rssi_cb, conn_hdl, rssi_changed->rssi);
}
}
break;
case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
{
ble_gap_data_length_params_t* param = &evt->evt.gap_evt.params.data_length_update_request.peer_params;
LOG_LV2("GAP", "Data Length Req is (tx, rx) octets = (%d, %d), (tx, rx) time = (%d, %d) us",
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_hdl, NULL, NULL), );
}
break;
case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
{
ble_gap_data_length_params_t* datalen = &evt->evt.gap_evt.params.data_length_update.effective_params;
LOG_LV2("GAP", "Data Length is (tx, rx) octets = (%d, %d), (tx, rx) time = (%d, %d) us",
datalen->max_tx_octets, datalen->max_rx_octets, datalen->max_tx_time_us, datalen->max_rx_time_us);
}
break;
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
{
ble_gap_phys_t* req_phy = &evt->evt.gap_evt.params.phy_update_request.peer_preferred_phys;
#if CFG_DEBUG >= 1
char const *phy_str[] = { "Auto", "1 Mbps", "2 Mbps", "Coded" };
LOG_LV1("GAP", "PHY request tx: %s, rx: %s", phy_str[req_phy->tx_phys], phy_str[req_phy->rx_phys]);
#endif
// 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_hdl, &phy);
}
break;
case BLE_GAP_EVT_PHY_UPDATE:
{
ble_gap_evt_phy_update_t* active_phy = &evt->evt.gap_evt.params.phy_update;
#if CFG_DEBUG >= 1
if ( active_phy->status != BLE_HCI_STATUS_CODE_SUCCESS )
{
LOG_LV1("GAP", "Failed HCI status = 0x%02X", active_phy->status);
}else
{
char const *phy_str[] = { "Auto", "1 Mbps", "2 Mbps", "Coded" };
LOG_LV1("GAP", "PHY active tx: %s, rx: %s", phy_str[active_phy->tx_phy], phy_str[active_phy->rx_phy]);
}
#endif
}
break;
default: break;
}
}
-108
View File
@@ -1,108 +0,0 @@
/**************************************************************************/
/*!
@file BLEGap.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#ifndef BLEGAP_H_
#define BLEGAP_H_
#include <Arduino.h>
#include "bluefruit_common.h"
#include "BLEConnection.h"
#include "BLEUuid.h"
#include "utility/bonding.h"
enum
{
CONN_CFG_PERIPHERAL = 1,
CONN_CFG_CENTRAL = 2,
};
class BLEGap
{
public:
typedef void (*connect_callback_t ) (uint16_t conn_hdl);
typedef void (*disconnect_callback_t ) (uint16_t conn_hdl, uint8_t reason);
typedef void (*rssi_callback_t ) (uint16_t conn_hdl, int8_t rssi);
BLEGap(void);
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 setPrivacy (); sd_ble_gap_privacy_set()
ble_gap_sec_params_t getSecureParam(void)
{
return _sec_param;
}
BLEConnection* Connection(uint16_t conn_hdl);
uint16_t getMaxMtu(uint8_t role);
void setRssiCallback(rssi_callback_t fp);
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
* Although declare as public, it is meant to be invoked by internal code.
*------------------------------------------------------------------*/
void _eventHandler(ble_evt_t* evt);
void _prph_setConnectCallback ( connect_callback_t fp);
void _prph_setDisconnectCallback( disconnect_callback_t fp);
void _central_setConnectCallback ( connect_callback_t fp);
void _central_setDisconnectCallback( disconnect_callback_t fp);
private:
struct {
// 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;
ble_gap_sec_params_t _sec_param;
BLEConnection* _connection[BLE_MAX_CONNECTION];
rssi_callback_t _rssi_cb;
friend class AdafruitBluefruit;
};
#endif /* BLEGAP_H_ */
+2 -2
View File
@@ -50,7 +50,7 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe
int32_t count = 0;
ble_gattc_handle_range_t hdl_range = { .start_handle = start_hdl, .end_handle = end_hdl };
BLEConnection* conn = Bluefruit.Gap.Connection(conn_hdl);
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
while( _adamsg.isWaiting() )
{
@@ -82,7 +82,7 @@ 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;
BLEConnection* conn = Bluefruit.Gap.Connection(evt_conn_hdl);
BLEConnection* conn = Bluefruit.Connection(evt_conn_hdl);
/*------------- Server service -------------*/
// TODO multiple peripherals
+22 -19
View File
@@ -39,6 +39,8 @@
// Constructor
BLEPeriph::BLEPeriph(void)
{
_connect_cb = NULL;
_disconnect_cb = NULL;
_ppcp = ((ble_gap_conn_params_t) {
.min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT,
@@ -61,7 +63,7 @@ bool BLEPeriph::begin(void)
bool BLEPeriph::connected (uint16_t conn_hdl)
{
BLEConnection* conn = Bluefruit.Gap.Connection(conn_hdl);
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
return conn && conn->connected() && (conn->getRole() == BLE_GAP_ROLE_PERIPH);
}
@@ -76,20 +78,6 @@ uint8_t BLEPeriph::connected (void)
return count;
}
void BLEPeriph::printInfo(void)
{
char const * title_fmt = "%-16s: ";
Serial.printf(title_fmt, "Conn Intervals");
Serial.printf("min = %.2f ms, ", _ppcp.min_conn_interval*1.25f);
Serial.printf("max = %.2f ms", _ppcp.max_conn_interval*1.25f);
Serial.println();
Serial.printf(title_fmt, "Conn Timeout");
Serial.printf("%.2f ms", _ppcp.conn_sup_timeout*10.0f);
Serial.println();
}
bool BLEPeriph::setConnInterval (uint16_t min, uint16_t max)
{
_ppcp.min_conn_interval = min;
@@ -119,14 +107,14 @@ bool BLEPeriph::setConnSupervisionTimeoutMS(uint16_t timeout_ms)
return setConnSupervisionTimeout(timeout_ms / 10); // 10ms unit
}
void BLEPeriph::setConnectCallback( BLEGap::connect_callback_t fp )
void BLEPeriph::setConnectCallback( ble_connect_callback_t fp )
{
Bluefruit.Gap._prph_setConnectCallback(fp);
_connect_cb = fp;
}
void BLEPeriph::setDisconnectCallback( BLEGap::disconnect_callback_t fp )
void BLEPeriph::setDisconnectCallback( ble_disconnect_callback_t fp )
{
Bluefruit.Gap._prph_setDisconnectCallback(fp);
_disconnect_cb = fp;
}
@@ -159,3 +147,18 @@ void BLEPeriph::_eventHandler(ble_evt_t* evt)
default: break;
}
}
void BLEPeriph::printInfo(void)
{
char const * title_fmt = "%-16s: ";
Serial.printf(title_fmt, "Conn Intervals");
Serial.printf("min = %.2f ms, ", _ppcp.min_conn_interval*1.25f);
Serial.printf("max = %.2f ms", _ppcp.max_conn_interval*1.25f);
Serial.println();
Serial.printf(title_fmt, "Conn Timeout");
Serial.printf("%.2f ms", _ppcp.conn_sup_timeout*10.0f);
Serial.println();
}
+9 -5
View File
@@ -40,8 +40,6 @@
#include <Arduino.h>
#include "bluefruit_common.h"
#include "BLEGap.h"
class BLEPeriph
{
public:
@@ -57,19 +55,25 @@ class BLEPeriph
bool setConnSupervisionTimeout(uint16_t timeout);
bool setConnSupervisionTimeoutMS(uint16_t timeout_ms);
void setConnectCallback ( BLEGap::connect_callback_t fp);
void setDisconnectCallback( BLEGap::disconnect_callback_t fp);
//------------- Callbacks -------------//
void setConnectCallback ( ble_connect_callback_t fp);
void setDisconnectCallback( ble_disconnect_callback_t fp);
void printInfo(void);
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
* Although declare as public, it is meant to be invoked by internal code.
*------------------------------------------------------------------*/
void _eventHandler(ble_evt_t* evt);
void printInfo(void);
private:
ble_gap_conn_params_t _ppcp;
ble_connect_callback_t _connect_cb;
ble_disconnect_callback_t _disconnect_cb;
friend class AdafruitBluefruit;
};
#endif /* BLEPERIPH_H_ */
+210 -27
View File
@@ -99,7 +99,6 @@ static void nrf_error_cb(uint32_t id, uint32_t pc, uint32_t info)
* Constructor
*/
AdafruitBluefruit::AdafruitBluefruit(void)
: Central(), Gap()
{
/*------------------------------------------------------------------*/
/* SoftDevice Default Configuration
@@ -117,9 +116,21 @@ AdafruitBluefruit::AdafruitBluefruit(void)
_sd_cfg.uuid128_max = BLE_UUID_VS_COUNT_DEFAULT;
_sd_cfg.service_changed = 1;
_sd_cfg.prph.mtu_max = BLE_GATT_ATT_MTU_DEFAULT;
_sd_cfg.prph.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
_sd_cfg.prph.hvn_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
_sd_cfg.prph.wrcmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
_sd_cfg.central.mtu_max = BLE_GATT_ATT_MTU_DEFAULT;
_sd_cfg.central.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
_sd_cfg.central.hvn_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
_sd_cfg.central.wrcmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
_prph_count = 0;
_central_count = 0;
memclr(_connection, sizeof(_connection));
_ble_event_sem = NULL;
_soc_event_sem = NULL;
@@ -131,6 +142,21 @@ AdafruitBluefruit::AdafruitBluefruit(void)
_conn_hdl = BLE_CONN_HANDLE_INVALID;
_event_cb = NULL;
_rssi_cb = NULL;
_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},
};
COMMENT_OUT(
_auth_type = BLE_GAP_AUTH_KEY_TYPE_NONE;
@@ -155,12 +181,19 @@ void AdafruitBluefruit::configAttrTableSize(uint32_t attr_table_size)
void AdafruitBluefruit::configPrphConn(uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize)
{
Gap.configPrphConn(mtu_max, event_len, hvn_qsize, wrcmd_qsize);
_sd_cfg.prph.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);;
_sd_cfg.prph.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
_sd_cfg.prph.hvn_qsize = hvn_qsize;
_sd_cfg.prph.wrcmd_qsize = wrcmd_qsize;
}
void AdafruitBluefruit::configCentralConn(uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize)
{
Gap.configCentralConn(mtu_max, event_len, hvn_qsize, wrcmd_qsize);
_sd_cfg.central.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);;
_sd_cfg.central.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
_sd_cfg.central.hvn_qsize = hvn_qsize;
_sd_cfg.central.wrcmd_qsize = wrcmd_qsize;
}
void AdafruitBluefruit::configPrphBandwidth(uint8_t bw)
@@ -332,26 +365,26 @@ bool 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._prph.mtu_max;
blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = _sd_cfg.prph.mtu_max;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATT, &blecfg, ram_start), false );
// 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._prph.event_len;
blecfg.conn_cfg.params.gap_conn_cfg.event_length = _sd_cfg.prph.event_len;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GAP, &blecfg, ram_start), false );
// 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._prph.hvn_qsize;
blecfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = _sd_cfg.prph.hvn_qsize;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &blecfg, ram_start), false );
// 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._prph.wrcmd_qsize;
blecfg.conn_cfg.params.gattc_conn_cfg.write_cmd_tx_queue_size = _sd_cfg.prph.wrcmd_qsize;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTC, &blecfg, ram_start), false );
}
@@ -360,26 +393,26 @@ bool 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._central.mtu_max;
blecfg.conn_cfg.params.gatt_conn_cfg.att_mtu = _sd_cfg.central.mtu_max;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATT, &blecfg, ram_start), false );
// 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._central.event_len;
blecfg.conn_cfg.params.gap_conn_cfg.event_length =_sd_cfg.central.event_len;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GAP, &blecfg, ram_start), false );
// 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._central.hvn_qsize;
blecfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = _sd_cfg.central.hvn_qsize;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &blecfg, ram_start), false );
// 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._central.wrcmd_qsize;
blecfg.conn_cfg.params.gattc_conn_cfg.write_cmd_tx_queue_size = _sd_cfg.central.wrcmd_qsize;
VERIFY_STATUS ( sd_ble_cfg_set(BLE_CONN_CFG_GATTC, &blecfg, ram_start), false );
}
@@ -564,13 +597,13 @@ uint8_t AdafruitBluefruit::connected(void)
bool AdafruitBluefruit::connected(uint16_t conn_hdl)
{
BLEConnection* conn = Gap.Connection(conn_hdl);
BLEConnection* conn = this->Connection(conn_hdl);
return conn && conn->connected();
}
bool AdafruitBluefruit::disconnect(uint16_t conn_hdl)
{
BLEConnection* conn = Gap.Connection(conn_hdl);
BLEConnection* conn = this->Connection(conn_hdl);
// disconnect if connected
if ( conn && conn->connected() )
@@ -598,16 +631,32 @@ uint16_t AdafruitBluefruit::connHandle(void)
bool AdafruitBluefruit::connPaired(void)
{
BLEConnection* conn = Bluefruit.Gap.Connection(_conn_hdl);
BLEConnection* conn = Bluefruit.Connection(_conn_hdl);
return conn && conn->paired();
}
ble_gap_addr_t AdafruitBluefruit::getPeerAddr(uint16_t conn_hdl)
{
BLEConnection* conn = Gap.Connection(conn_hdl);
BLEConnection* conn = this->Connection(conn_hdl);
return conn ? conn->getPeerAddr() : ((ble_gap_addr_t) {0});
}
uint16_t AdafruitBluefruit::getMaxMtu(uint8_t role)
{
return (role == BLE_GAP_ROLE_PERIPH) ? _sd_cfg.prph.mtu_max : _sd_cfg.central.mtu_max;
}
BLEConnection* AdafruitBluefruit::Connection(uint16_t conn_hdl)
{
return (conn_hdl < BLE_MAX_CONNECTION) ? _connection[conn_hdl] : NULL;
}
void AdafruitBluefruit::setRssiCallback(rssi_callback_t fp)
{
_rssi_cb = fp;
}
COMMENT_OUT (
bool AdafruitBluefruit::setPIN(const char* pin)
{
@@ -728,16 +777,148 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
{
// conn handle has fixed offset for all events
uint16_t const conn_hdl = evt->evt.common_evt.conn_handle;
BLEConnection* conn = this->Connection(conn_hdl);
LOG_LV1("BLE", "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), conn_hdl);
// GAP handler
Gap._eventHandler(evt);
if ( conn ) conn->_eventHandler(evt);
switch(evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
{
// Turn on Conn LED
_stopConnLed();
_setConnLed(true);
ble_gap_evt_connected_t const * para = &evt->evt.gap_evt.params.connected;
if ( _connection[conn_hdl] )
{
LOG_LV1("GAP", "Connection is already in used, something wrong !!");
delete _connection[conn_hdl];
_connection[conn_hdl] = NULL;
}
// Transmission buffer pool
uint8_t const hvn_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _sd_cfg.prph.hvn_qsize : _sd_cfg.central.hvn_qsize;
uint8_t const wrcmd_qsize = (para->role == BLE_GAP_ROLE_PERIPH) ? _sd_cfg.prph.wrcmd_qsize : _sd_cfg.central.wrcmd_qsize;
_connection[conn_hdl] = new BLEConnection(conn_hdl, para, hvn_qsize, wrcmd_qsize);
conn = _connection[conn_hdl];
// Invoke connect callback
if ( conn->getRole() == BLE_GAP_ROLE_PERIPH )
{
if ( Periph._connect_cb ) ada_callback(NULL, Periph._connect_cb, conn_hdl);
}else
{
if ( Central._connect_cb ) ada_callback(NULL, Central._connect_cb, conn_hdl);
}
}
break;
case BLE_GAP_EVT_DISCONNECTED:
{
ble_gap_evt_disconnected_t const* para = &evt->evt.gap_evt.params.disconnected;
LOG_LV2("GAP", "Disconnect Reason 0x%02X", evt->evt.gap_evt.params.disconnected.reason);
// Turn off Conn LED If not connected at all
bool still_connected = false;
for (uint8_t i=0; i<BLE_MAX_CONNECTION; i++)
{
if ( _connection[i] && _connection[i]->connected() )
{
still_connected = true;
break;
}
}
if ( !still_connected ) _setConnLed(false);
// Invoke disconnect callback
if ( conn->getRole() == BLE_GAP_ROLE_PERIPH )
{
if ( Periph._disconnect_cb ) ada_callback(NULL, Periph._disconnect_cb, conn_hdl, para->reason);
}else
{
if ( Central._disconnect_cb ) ada_callback(NULL, Central._disconnect_cb, conn_hdl, para->reason);
}
delete _connection[conn_hdl];
_connection[conn_hdl] = NULL;
}
break;
case BLE_GAP_EVT_RSSI_CHANGED:
{
ble_gap_evt_rssi_changed_t const * rssi_changed = &evt->evt.gap_evt.params.rssi_changed;
if ( _rssi_cb )
{
ada_callback(NULL, _rssi_cb, conn_hdl, rssi_changed->rssi);
}
}
break;
case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
{
ble_gap_data_length_params_t* param = &evt->evt.gap_evt.params.data_length_update_request.peer_params;
LOG_LV2("GAP", "Data Length Req is (tx, rx) octets = (%d, %d), (tx, rx) time = (%d, %d) us",
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_hdl, NULL, NULL), );
}
break;
case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
{
ble_gap_data_length_params_t* datalen = &evt->evt.gap_evt.params.data_length_update.effective_params;
LOG_LV2("GAP", "Data Length is (tx, rx) octets = (%d, %d), (tx, rx) time = (%d, %d) us",
datalen->max_tx_octets, datalen->max_rx_octets, datalen->max_tx_time_us, datalen->max_rx_time_us);
}
break;
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
{
ble_gap_phys_t* req_phy = &evt->evt.gap_evt.params.phy_update_request.peer_preferred_phys;
#if CFG_DEBUG >= 1
char const *phy_str[] = { "Auto", "1 Mbps", "2 Mbps", "Coded" };
LOG_LV1("GAP", "PHY request tx: %s, rx: %s", phy_str[req_phy->tx_phys], phy_str[req_phy->rx_phys]);
#endif
// 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_hdl, &phy);
}
break;
case BLE_GAP_EVT_PHY_UPDATE:
{
ble_gap_evt_phy_update_t* active_phy = &evt->evt.gap_evt.params.phy_update;
#if CFG_DEBUG >= 1
if ( active_phy->status != BLE_HCI_STATUS_CODE_SUCCESS )
{
LOG_LV1("GAP", "Failed HCI status = 0x%02X", active_phy->status);
}else
{
char const *phy_str[] = { "Auto", "1 Mbps", "2 Mbps", "Coded" };
LOG_LV1("GAP", "PHY active tx: %s, rx: %s", phy_str[active_phy->tx_phy], phy_str[active_phy->rx_phy]);
}
#endif
}
break;
default: break;
}
Advertising._eventHandler(evt);
Scanner._eventHandler(evt);
Periph._eventHandler(evt);
/*------------- BLE Peripheral Events -------------*/
/* Only handle Peripheral events with matched connection handle
* or a few special one
@@ -785,6 +966,8 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
}
}
Periph._eventHandler(evt);
// Discovery Event Handler
if ( Discovery.begun() ) Discovery._eventHandler(evt);
@@ -821,7 +1004,7 @@ void AdafruitBluefruit::_setConnLed (bool on_off)
*------------------------------------------------------------------*/
bool AdafruitBluefruit::requestPairing(uint16_t conn_hdl)
{
BLEConnection* conn = Gap.Connection(conn_hdl);
BLEConnection* conn = this->Connection(conn_hdl);
VERIFY(conn);
return conn->requestPairing();
@@ -872,19 +1055,19 @@ void AdafruitBluefruit::printInfo(void)
Serial.print(" - ");
Serial.printf(title_fmt, "Max MTU");
Serial.println(Gap._prph.mtu_max);
Serial.println(_sd_cfg.prph.mtu_max);
Serial.print(" - ");
Serial.printf(title_fmt, "Event Length");
Serial.println(Gap._prph.event_len);
Serial.println(_sd_cfg.prph.event_len);
Serial.print(" - ");
Serial.printf(title_fmt, "HVN Queue Size");
Serial.println(Gap._prph.hvn_qsize);
Serial.println(_sd_cfg.prph.hvn_qsize);
Serial.print(" - ");
Serial.printf(title_fmt, "WrCmd Queue Size");
Serial.println(Gap._prph.wrcmd_qsize);
Serial.println(_sd_cfg.prph.wrcmd_qsize);
}
if ( _central_count )
@@ -893,19 +1076,19 @@ void AdafruitBluefruit::printInfo(void)
Serial.print(" - ");
Serial.printf(title_fmt, "Max MTU");
Serial.println(Gap._central.mtu_max);
Serial.println(_sd_cfg.central.mtu_max);
Serial.print(" - ");
Serial.printf(title_fmt, "Event Length");
Serial.println(Gap._central.event_len);
Serial.println(_sd_cfg.central.event_len);
Serial.print(" - ");
Serial.printf(title_fmt, "HVN Queue Size");
Serial.println(Gap._central.hvn_qsize);
Serial.println(_sd_cfg.central.hvn_qsize);
Serial.print(" - ");
Serial.printf(title_fmt, "WrCmd Queue Size");
Serial.println(Gap._central.wrcmd_qsize);
Serial.println(_sd_cfg.central.wrcmd_qsize);
}
/*------------- Settings -------------*/
+27 -6
View File
@@ -59,7 +59,6 @@
#include "BLEClientService.h"
#include "BLEDiscovery.h"
#include "BLEConnection.h"
#include "BLEGap.h"
#include "BLEGatt.h"
// Services
@@ -92,6 +91,12 @@ enum
BANDWIDTH_MAX,
};
enum
{
CONN_CFG_PERIPHERAL = 1,
CONN_CFG_CENTRAL = 2,
};
extern "C"
{
void SD_EVT_IRQHandler(void);
@@ -100,12 +105,13 @@ extern "C"
class AdafruitBluefruit
{
public:
typedef void (*rssi_callback_t ) (uint16_t conn_hdl, int8_t rssi);
AdafruitBluefruit(void); // Constructor
/*------------------------------------------------------------------*/
/* Lower Level Classes (Bluefruit.Advertising.*, etc.)
*------------------------------------------------------------------*/
BLEGap Gap;
BLEGatt Gatt;
BLEAdvertising Advertising;
@@ -173,12 +179,14 @@ class AdafruitBluefruit
uint16_t getPeerName (uint16_t conn_hdl, char* buf, uint16_t bufsize);
uint16_t getMaxMtu(uint8_t role);
void printInfo(void);
BLEConnection* Connection(uint16_t conn_hdl);
/*------------------------------------------------------------------*/
/* Callbacks
*------------------------------------------------------------------*/
void setRssiCallback(rssi_callback_t fp);
void setEventCallback ( void (*fp) (ble_evt_t*) );
COMMENT_OUT ( bool setPIN(const char* pin); )
@@ -188,9 +196,11 @@ class AdafruitBluefruit
* Although declare as public, it is meant to be invoked by internal
* code. User should not call these directly
*------------------------------------------------------------------*/
void _startConnLed (void);
void _stopConnLed (void);
void _setConnLed (bool on_off);
void _startConnLed (void);
void _stopConnLed (void);
void _setConnLed (bool on_off);
void printInfo(void);
private:
/*------------- SoftDevice Configuration -------------*/
@@ -198,6 +208,14 @@ class AdafruitBluefruit
uint32_t attr_table_size;
uint8_t service_changed;
uint8_t uuid128_max;
// Bandwidth configuration
struct {
uint16_t mtu_max;
uint8_t event_len;
uint8_t hvn_qsize;
uint8_t wrcmd_qsize;
}prph, central;
}_sd_cfg;
uint8_t _prph_count;
@@ -217,6 +235,9 @@ class AdafruitBluefruit
uint16_t _conn_hdl;
BLEConnection* _connection[BLE_MAX_CONNECTION];
rssi_callback_t _rssi_cb;
void (*_event_cb) (ble_evt_t*);
COMMENT_OUT(
@@ -64,4 +64,8 @@
// Converts an integer of 625ms units to msecs
#define MS625TO1000(u625) ( ((u625)*5) / 8 )
typedef void (*ble_connect_callback_t ) (uint16_t conn_hdl);
typedef void (*ble_disconnect_callback_t ) (uint16_t conn_hdl, uint8_t reason);
#endif /* BLUEFRUIT_COMMON_H_ */
@@ -93,7 +93,7 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic* chr, ble_gatts_evt
(request->op != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL))
{
uint16_t conn_hdl = Bluefruit.connHandle();
BLEConnection* conn = Bluefruit.Gap.Connection(conn_hdl);
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
ble_gatts_rw_authorize_reply_params_t reply = { .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE };
@@ -160,7 +160,7 @@ void BLEUart::bufferTXD(uint8_t enable)
if ( _tx_fifo == NULL )
{
_tx_fifo = new Adafruit_FIFO(1);
_tx_fifo->begin( Bluefruit.Gap.getMaxMtu(BLE_GAP_ROLE_PERIPH) );
_tx_fifo->begin( Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH) );
}
}else
{
@@ -178,7 +178,7 @@ err_t BLEUart::begin(void)
// Invoke base class begin()
VERIFY_STATUS( BLEService::begin() );
uint16_t max_mtu = Bluefruit.Gap.getMaxMtu(BLE_GAP_ROLE_PERIPH);
uint16_t max_mtu = Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH);
// Add TXD Characteristic
_txd.setProperties(CHR_PROPS_NOTIFY);
@@ -263,7 +263,7 @@ size_t BLEUart::write (const uint8_t *content, size_t len)
uint16_t written = _tx_fifo->write(content, len);
// TODO multiple prph connections
BLEConnection* conn = Bluefruit.Gap.Connection( Bluefruit.connHandle() );
BLEConnection* conn = Bluefruit.Connection( Bluefruit.connHandle() );
VERIFY(conn, 0);
// Not up to GATT MTU, notify will be sent later by TXD timer handler
@@ -306,7 +306,7 @@ void BLEUart::flush (void)
bool BLEUart::flush_tx_buffered(void)
{
// TODO multiple prph connections
BLEConnection* conn = Bluefruit.Gap.Connection( Bluefruit.connHandle() );
BLEConnection* conn = Bluefruit.Connection( Bluefruit.connHandle() );
uint16_t const gatt_mtu = conn->getMtu() - 3;
uint8_t* ff_data = (uint8_t*) rtos_malloc( gatt_mtu );