rename BLEGapConnection to BLEConnection, move to seperate files

This commit is contained in:
hathach committed 2019-01-18 12:31:40 +07:00
1 parent 6578b3a37f
commit 4e5f419474
11 files changed
+187 -98

No files matched your search

@@ -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)
{
BLEGapConnection* conn = Bluefruit.Gap.getConnection( conn_hdl );
BLEConnection* conn = Bluefruit.Gap.getConnection( 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);
BLEGapConnection* conn = Bluefruit.Gap.getConnection( conn_hdl );
BLEConnection* conn = Bluefruit.Gap.getConnection( 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)
{
BLEGapConnection* conn = Bluefruit.Gap.getConnection( conn_hdl );
BLEConnection* conn = Bluefruit.Gap.getConnection( 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);
@@ -534,7 +534,7 @@ bool BLECharacteristic::notify(const void* data, uint16_t len)
if ( notifyEnabled() )
{
// TODO multiple connection support
BLEGapConnection* conn = Bluefruit.Gap.getConnection( Bluefruit.connHandle() );
BLEConnection* conn = Bluefruit.Gap.getConnection( Bluefruit.connHandle() );
VERIFY(conn);
uint16_t const max_payload = conn->getMTU() - 3;
@@ -616,7 +616,7 @@ bool BLECharacteristic::indicate(const void* data, uint16_t len)
if ( indicateEnabled() )
{
uint16_t conn_hdl = Bluefruit.connHandle();
BLEGapConnection* conn = Bluefruit.Gap.getConnection( conn_hdl );
BLEConnection* conn = Bluefruit.Gap.getConnection( conn_hdl );
uint16_t const max_payload = conn->getMTU() - 3;
const uint8_t* u8data = (const uint8_t*) data;
@@ -168,7 +168,7 @@ uint16_t BLEClientCharacteristic::read(void* buffer, uint16_t bufsize)
{
VERIFY( _chr.char_props.read, 0 );
BLEGapConnection* conn = Bluefruit.Gap.getConnection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Gap.getConnection( _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 );
BLEGapConnection* conn = Bluefruit.Gap.getConnection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Gap.getConnection( _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 );
BLEGapConnection* conn = Bluefruit.Gap.getConnection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Gap.getConnection( _service->connHandle() );
VERIFY(conn, 0);
uint16_t const max_payload = conn->getMTU() - 3;
@@ -363,7 +363,7 @@ bool BLEClientCharacteristic::writeCCCD(uint16_t value)
};
// TODO only Write without response consume a TX buffer
BLEGapConnection* conn = Bluefruit.Gap.getConnection(conn_handle);
BLEConnection* conn = Bluefruit.Gap.getConnection(conn_handle);
VERIFY( conn->getWriteCmdPacket() );
VERIFY_STATUS( sd_ble_gattc_write(conn_handle, &param), false );
@@ -474,7 +474,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
}
else if ( wr_rsp->write_op == BLE_GATT_OP_PREP_WRITE_REQ)
{
BLEGapConnection* conn = Bluefruit.Gap.getConnection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Gap.getConnection( _service->connHandle() );
uint16_t const max_payload = conn->getMTU() - 3;
uint16_t packet_len = min16(_adamsg.remaining, max_payload-2);
@@ -524,7 +524,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
case BLE_GATTC_EVT_READ_RSP:
{
BLEGapConnection* conn = Bluefruit.Gap.getConnection( _service->connHandle() );
BLEConnection* conn = Bluefruit.Gap.getConnection( _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;
@@ -0,0 +1,42 @@
/**************************************************************************/
/*!
@file BLEConnection.cpp
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2019, 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.
*/
/**************************************************************************/
//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+
//------------- IMPLEMENTATION -------------//
@@ -0,0 +1,104 @@
/**************************************************************************/
/*!
@file BLEConnection.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2019, 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 BLECONNECTION_H_
#define BLECONNECTION_H_
#include <Arduino.h>
#include "bluefruit_common.h"
#include "utility/bonding.h"
class BLEConnection
{
private:
uint16_t _conn_hdl;
public:
ble_gap_addr_t _addr;
uint16_t att_mtu;
bool _paired;
uint8_t role;
SemaphoreHandle_t hvn_sem;
SemaphoreHandle_t wrcmd_sem;
// On-demand semaphore that are created on the fly
SemaphoreHandle_t hvc_sem;
bool hvc_received;
SemaphoreHandle_t pair_sem;
uint16_t ediv;
bond_keys_t* bond_keys; // Shared keys with bonded device, size ~ 80 bytes
BLEConnection(uint16_t conn_hdl) { _conn_hdl = conn_hdl; }
uint16_t handle(void) { return _conn_hdl; }
bool connected(void);
bool paired(void) { return _paired; }
uint8_t getRole(void);
uint16_t getMTU (void)
{
return att_mtu;
}
ble_gap_addr_t getPeerAddr(void)
{
return _addr;
}
uint8_t getPeerAddr(uint8_t addr[6])
{
memcpy(addr, _addr.addr, BLE_GAP_ADDR_LEN);
return _addr.addr_type;
}
bool getHvnPacket (void)
{
VERIFY(hvn_sem != NULL);
return xSemaphoreTake(hvn_sem, ms2tick(BLE_GENERIC_TIMEOUT));
}
bool getWriteCmdPacket(void)
{
VERIFY(wrcmd_sem != NULL);
return xSemaphoreTake(wrcmd_sem, ms2tick(BLE_GENERIC_TIMEOUT));
}
};
#endif /* BLECONNECTION_H_ */
+13 -13
View File
@@ -150,7 +150,7 @@ bool BLEGap::connected(uint16_t conn_hdl)
bool BLEGap::requestPairing(uint16_t conn_hdl)
{
gap_peer_t* peer = &_peers[conn_hdl];
BLEGapConnection* conn = _connection[conn_hdl];
BLEConnection* conn = _connection[conn_hdl];
VERIFY(conn);
// skip if already paired
@@ -221,7 +221,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
const uint16_t conn_hdl = evt->evt.common_evt.conn_handle;
gap_peer_t* peer = (conn_hdl == BLE_CONN_HANDLE_INVALID) ? NULL : &_peers[conn_hdl];
BLEGapConnection* conn = (conn_hdl == BLE_CONN_HANDLE_INVALID) ? NULL : _connection[conn_hdl];
BLEConnection* conn = (conn_hdl == BLE_CONN_HANDLE_INVALID) ? NULL : _connection[conn_hdl];
switch(evt->header.evt_id)
{
@@ -240,7 +240,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
_connection[conn_hdl] = NULL;
}
_connection[conn_hdl] = new BLEGapConnection(conn_hdl);
_connection[conn_hdl] = new BLEConnection(conn_hdl);
conn = _connection[conn_hdl];
varclr(conn);
@@ -253,10 +253,10 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
// Transmission buffer sem pool
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);
conn->hvn_sem = xSemaphoreCreateCounting(hvn_qsize, hvn_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);
conn->wrcmd_sem = xSemaphoreCreateCounting(wrcmd_qsize, wrcmd_qsize);
LOG_LV2("GAP", "Conn Interval= %f", para->conn_params.min_conn_interval*1.25f);
@@ -277,11 +277,11 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
LOG_LV2("GAP", "Disconnect Reason 0x%02X", evt->evt.gap_evt.params.disconnected.reason);
// mark as disconnected, but keep the role for sub sequence event handler
vSemaphoreDelete( conn->hvn_tx_sem );
conn->hvn_tx_sem = NULL;
vSemaphoreDelete( conn->hvn_sem );
conn->hvn_sem = NULL;
vSemaphoreDelete( conn->wrcmd_tx_sem );
conn->wrcmd_tx_sem = NULL;
vSemaphoreDelete( conn->wrcmd_sem );
conn->wrcmd_sem = NULL;
if ( conn->role == BLE_GAP_ROLE_PERIPH )
{
@@ -443,16 +443,16 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
break;
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
if ( conn->hvn_tx_sem )
if ( conn->hvn_sem )
{
for(uint8_t i=0; i<evt->evt.gatts_evt.params.hvn_tx_complete.count; i++) xSemaphoreGive(conn->hvn_tx_sem);
for(uint8_t i=0; i<evt->evt.gatts_evt.params.hvn_tx_complete.count; i++) xSemaphoreGive(conn->hvn_sem);
}
break;
case BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE:
if ( conn->wrcmd_tx_sem )
if ( conn->wrcmd_sem )
{
for(uint8_t i=0; i<evt->evt.gattc_evt.params.write_cmd_tx_complete.count; i++) xSemaphoreGive(conn->wrcmd_tx_sem);
for(uint8_t i=0; i<evt->evt.gattc_evt.params.write_cmd_tx_complete.count; i++) xSemaphoreGive(conn->wrcmd_sem);
}
break;
+10 -67
View File
@@ -38,6 +38,7 @@
#include <Arduino.h>
#include "bluefruit_common.h"
#include "BLEConnection.h"
#include "BLEUuid.h"
#include "utility/bonding.h"
@@ -47,64 +48,6 @@ enum
CONN_CFG_CENTRAL = 2,
};
class BLEGapConnection
{
private:
uint16_t _conn_hdl;
public:
ble_gap_addr_t _addr;
uint16_t att_mtu;
bool _paired;
uint8_t role;
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;
SemaphoreHandle_t pair_sem;
uint16_t ediv;
bond_keys_t* bond_keys; // Shared keys with bonded device, size ~ 80 bytes
BLEGapConnection(uint16_t conn_hdl) { _conn_hdl = conn_hdl; }
uint16_t handle(void) { return _conn_hdl; }
bool connected(void);
bool paired(void) { return _paired; }
uint8_t getRole(void);
uint16_t getMTU (void)
{
return att_mtu;
}
ble_gap_addr_t getPeerAddr(void)
{
return _addr;
}
uint8_t getPeerAddr(uint8_t addr[6])
{
memcpy(addr, _addr.addr, BLE_GAP_ADDR_LEN);
return _addr.addr_type;
}
bool getHvnPacket (void)
{
VERIFY(hvn_tx_sem != NULL);
return xSemaphoreTake(hvn_tx_sem, ms2tick(BLE_GENERIC_TIMEOUT));
}
bool getWriteCmdPacket(void)
{
VERIFY(wrcmd_tx_sem != NULL);
return xSemaphoreTake(wrcmd_tx_sem, ms2tick(BLE_GENERIC_TIMEOUT));
}
};
class BLEGap
{
@@ -118,21 +61,21 @@ class BLEGap
bool setAddr (uint8_t mac[6], uint8_t type);
// bool setPrivacy (); sd_ble_gap_privacy_set()
BLEGapConnection* getConnection(uint16_t conn_hdl)
BLEConnection* getConnection(uint16_t conn_hdl)
{
return ( conn_hdl != BLE_CONN_HANDLE_INVALID ) ?_connection[conn_hdl] : NULL;
}
bool connected (uint16_t conn_hdl);
bool requestPairing (uint16_t conn_hdl);
bool connected (uint16_t conn_hdl);
bool requestPairing (uint16_t conn_hdl);
uint8_t getRole (uint16_t conn_hdl);
uint16_t getPeerName (uint16_t conn_hdl, char* buf, uint16_t bufsize);
uint8_t getRole (uint16_t conn_hdl);
uint16_t getPeerName (uint16_t conn_hdl, char* buf, uint16_t bufsize);
uint16_t getMaxMtuByConnCfg (uint8_t conn_cfg);
uint16_t getMaxMtuByConnCfg (uint8_t conn_cfg);
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);
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 startRssi(uint16_t conn_hdl, uint8_t );
// bool stopRssi(uint16_t conn_hdl);
@@ -173,7 +116,7 @@ class BLEGap
ble_gap_sec_params_t _sec_param;
BLEGapConnection* _connection[BLE_MAX_CONN];
BLEConnection* _connection[BLE_MAX_CONN];
friend class AdafruitBluefruit;
};
+2 -2
View File
@@ -77,7 +77,7 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe
bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl)
{
BLEGapConnection* conn = Bluefruit.Gap.getConnection(conn_hdl);
BLEConnection* conn = Bluefruit.Gap.getConnection(conn_hdl);
// hvi confirm semaphore is created on the fly
conn->hvc_sem = xSemaphoreCreateBinary();
@@ -96,7 +96,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;
BLEGapConnection* conn = (evt_conn_hdl == BLE_CONN_HANDLE_INVALID) ? NULL : Bluefruit.Gap.getConnection(evt_conn_hdl);
BLEConnection* conn = (evt_conn_hdl == BLE_CONN_HANDLE_INVALID) ? NULL : Bluefruit.Gap.getConnection(evt_conn_hdl);
/*------------- Server service -------------*/
// TODO multiple peripherals
+2 -2
View File
@@ -613,13 +613,13 @@ uint16_t AdafruitBluefruit::connInterval(void)
ble_gap_addr_t AdafruitBluefruit::getPeerAddr(void)
{
BLEGapConnection* conn = Gap.getConnection(_conn_hdl);
BLEConnection* conn = Gap.getConnection(_conn_hdl);
return conn ? conn->getPeerAddr() : ((ble_gap_addr_t) {0});
}
uint8_t AdafruitBluefruit::getPeerAddr(uint8_t addr[6])
{
BLEGapConnection* conn = Gap.getConnection(_conn_hdl);
BLEConnection* conn = Gap.getConnection(_conn_hdl);
if ( conn )
{
return conn->getPeerAddr(addr);
@@ -94,7 +94,7 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt
{
uint16_t conn_hdl = Bluefruit.connHandle();
BLEGapConnection* conn = Bluefruit.Gap.getConnection(conn_hdl);
BLEConnection* conn = Bluefruit.Gap.getConnection(conn_hdl);
ble_gatts_rw_authorize_reply_params_t reply = { .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE };
@@ -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
BLEGapConnection* conn = Bluefruit.Gap.getConnection( Bluefruit.connHandle() );
BLEConnection* conn = Bluefruit.Gap.getConnection( Bluefruit.connHandle() );
// Not up to GATT MTU, notify will be sent later by TXD timer handler
if ( _tx_fifo->count() < (conn->getMTU() - 3) )
@@ -305,7 +305,7 @@ void BLEUart::flush (void)
bool BLEUart::flush_tx_buffered(void)
{
// TODO multiple prph connections
BLEGapConnection* conn = Bluefruit.Gap.getConnection( Bluefruit.connHandle() );
BLEConnection* conn = Bluefruit.Gap.getConnection( Bluefruit.connHandle() );
uint16_t const gatt_mtu = conn->getMTU() - 3;
uint8_t* ff_data = (uint8_t*) rtos_malloc( gatt_mtu );