introduce BLEGap to manage part of GAP events
This commit is contained in:
1 parent
a974333516
commit
f4b4ec0470
5 files changed
+173
-43
No files matched your search
@@ -472,7 +472,7 @@ err_t BLECharacteristic::notify(const void* data, int len)
|
||||
|
||||
while ( actual_len )
|
||||
{
|
||||
if ( !Bluefruit.getTxPacket(100) ) return BLE_ERROR_NO_TX_PACKETS;
|
||||
if ( !Bluefruit.Gap.getTxPacket() ) return BLE_ERROR_NO_TX_PACKETS;
|
||||
|
||||
uint16_t packet_len = min16(MTU_MPS, actual_len);
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEGap.cpp
|
||||
@author hathach
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, 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)
|
||||
{
|
||||
for(int i=0; i<BLE_GAP_MAX_CONN; i++) _txpacket_sem[i] = NULL;
|
||||
}
|
||||
|
||||
bool BLEGap::getTxPacket(void)
|
||||
{
|
||||
return getTxPacket( Bluefruit.connHandle() );
|
||||
}
|
||||
|
||||
bool BLEGap::getTxPacket(uint16_t conn_handle)
|
||||
{
|
||||
VERIFY( (conn_handle < BLE_GAP_MAX_CONN) && (_txpacket_sem[conn_handle] != NULL) );
|
||||
|
||||
return xSemaphoreTake(_txpacket_sem[conn_handle], ms2tick(BLE_GAP_TIMEOUT));
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
switch(evt->header.evt_id)
|
||||
{
|
||||
case BLE_GAP_EVT_CONNECTED:
|
||||
{
|
||||
// Init transmission buffer for notification
|
||||
uint8_t txbuf_max;
|
||||
(void) sd_ble_tx_packet_count_get(conn_handle, &txbuf_max);
|
||||
_txpacket_sem[conn_handle] = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
vSemaphoreDelete( _txpacket_sem[conn_handle] );
|
||||
_txpacket_sem[conn_handle] = NULL;
|
||||
break;
|
||||
|
||||
case BLE_EVT_TX_COMPLETE:
|
||||
if ( _txpacket_sem[conn_handle] )
|
||||
{
|
||||
for(uint8_t i=0; i<evt->evt.common_evt.params.tx_complete.count; i++)
|
||||
{
|
||||
xSemaphoreGive(_txpacket_sem[conn_handle]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEGap.h
|
||||
@author hathach
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, 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 "BLEUuid.h"
|
||||
|
||||
#define BLE_GAP_MAX_CONN (BLE_CENTRAL_MAX_CONN+BLE_PRPH_MAX_CONN)
|
||||
#define BLE_GAP_TIMEOUT 100
|
||||
|
||||
class BLEGap
|
||||
{
|
||||
public:
|
||||
BLEGap(void);
|
||||
|
||||
bool getTxPacket(void);
|
||||
bool getTxPacket(uint16_t conn_handle);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
* code. User should not call these directly
|
||||
*------------------------------------------------------------------*/
|
||||
void _eventHandler(ble_evt_t* evt);
|
||||
|
||||
private:
|
||||
// Array of TX Packet semaphore, indexed by connection handle
|
||||
SemaphoreHandle_t _txpacket_sem[BLE_GAP_MAX_CONN];
|
||||
};
|
||||
|
||||
#endif /* BLEGAP_H_ */
|
||||
@@ -37,14 +37,6 @@
|
||||
#include "bluefruit.h"
|
||||
#include <Nffs.h>
|
||||
|
||||
// Note chaning these parameters will affect APP_RAM_BASE
|
||||
// --> need to update RAM in feather52_s132.ld linker
|
||||
#define BLE_GATTS_ATTR_TABLE_SIZE 0x0B00
|
||||
#define BLE_VENDOR_UUID_MAX 10
|
||||
#define BLE_PRPH_MAX_CONN 1
|
||||
#define BLE_CENTRAL_MAX_CONN 4
|
||||
#define BLE_CENTRAL_MAX_SECURE_CONN 1 // currently 1 to save SRAM
|
||||
|
||||
#define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
|
||||
|
||||
#define CFG_BLE_TX_POWER_LEVEL 0
|
||||
@@ -102,7 +94,6 @@ AdafruitBluefruit::AdafruitBluefruit(void)
|
||||
_tx_power = 0;
|
||||
|
||||
strcpy(_name, CFG_DEFAULT_NAME);
|
||||
_txpacket_sem = NULL;
|
||||
|
||||
_conn_hdl = BLE_CONN_HANDLE_INVALID;
|
||||
_bonded = false;
|
||||
@@ -348,12 +339,6 @@ ble_gap_addr_t AdafruitBluefruit::peerAddr(void)
|
||||
return _peer_addr;
|
||||
}
|
||||
|
||||
bool AdafruitBluefruit::getTxPacket(uint32_t ms)
|
||||
{
|
||||
VERIFY(_txpacket_sem != NULL);
|
||||
return xSemaphoreTake(_txpacket_sem, ms2tick(ms));
|
||||
}
|
||||
|
||||
COMMENT_OUT (
|
||||
bool AdafruitBluefruit::setPIN(const char* pin)
|
||||
{
|
||||
@@ -474,6 +459,9 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
// conn handle has fixed offset regardless of event type
|
||||
const uint16_t evt_conn_hdl = evt->evt.common_evt.conn_handle;
|
||||
|
||||
// GAP handler
|
||||
Gap._eventHandler(evt);
|
||||
|
||||
/*------------- BLE Peripheral Events -------------*/
|
||||
/* Only handle Peripheral events with matched connection handle
|
||||
* or a few special one
|
||||
@@ -507,11 +495,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
VERIFY_STATUS( sd_ble_gap_conn_param_update(_conn_hdl, NULL), );
|
||||
}
|
||||
|
||||
// Init transmission buffer for notification
|
||||
uint8_t txbuf_max;
|
||||
(void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max);
|
||||
_txpacket_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
|
||||
|
||||
if ( _connect_cb ) _connect_cb();
|
||||
}
|
||||
}
|
||||
@@ -536,9 +519,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
_bonded = false;
|
||||
varclr(&_peer_addr);
|
||||
|
||||
vSemaphoreDelete(_txpacket_sem);
|
||||
_txpacket_sem = NULL;
|
||||
|
||||
if ( _discconnect_cb ) _discconnect_cb(evt->evt.gap_evt.params.disconnected.reason);
|
||||
|
||||
Advertising.start();
|
||||
@@ -553,16 +533,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_EVT_TX_COMPLETE:
|
||||
if ( _txpacket_sem )
|
||||
{
|
||||
for(uint8_t i=0; i<evt->evt.common_evt.params.tx_complete.count; i++)
|
||||
{
|
||||
xSemaphoreGive(_txpacket_sem);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
{
|
||||
// Pairing in progress
|
||||
@@ -687,6 +657,9 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
*------------------------------------------------------------------*/
|
||||
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();
|
||||
|
||||
|
||||
@@ -37,6 +37,14 @@
|
||||
#include <Arduino.h>
|
||||
#include "bluefruit_common.h"
|
||||
|
||||
// Note chaning these parameters will affect APP_RAM_BASE
|
||||
// --> need to update RAM in feather52_s132.ld linker
|
||||
#define BLE_GATTS_ATTR_TABLE_SIZE 0x0B00
|
||||
#define BLE_VENDOR_UUID_MAX 10
|
||||
#define BLE_PRPH_MAX_CONN 1
|
||||
#define BLE_CENTRAL_MAX_CONN 4
|
||||
#define BLE_CENTRAL_MAX_SECURE_CONN 1 // should be enough
|
||||
|
||||
#include "BLEUuid.h"
|
||||
#include "BLEAdvertising.h"
|
||||
#include "BLECharacteristic.h"
|
||||
@@ -46,6 +54,7 @@
|
||||
#include "BLECentralCharacteristic.h"
|
||||
#include "BLECentralService.h"
|
||||
#include "BLEDiscovery.h"
|
||||
#include "BLEGap.h"
|
||||
#include "BLEGatt.h"
|
||||
|
||||
// Services
|
||||
@@ -61,6 +70,7 @@
|
||||
|
||||
#include "central_services/BLECentralUart.h"
|
||||
|
||||
|
||||
#define BLE_MAX_DATA_PER_MTU (GATT_MTU_SIZE_DEFAULT - 3)
|
||||
|
||||
extern "C"
|
||||
@@ -84,6 +94,7 @@ class AdafruitBluefruit
|
||||
BLECentral Central;
|
||||
BLEDiscovery Discovery;
|
||||
|
||||
BLEGap Gap;
|
||||
BLEGatt Gatt;
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
@@ -129,9 +140,6 @@ class AdafruitBluefruit
|
||||
|
||||
COMMENT_OUT ( bool setPIN(const char* pin); )
|
||||
|
||||
// internal usage only
|
||||
bool _registerCharacteristic(BLECharacteristic* chars);
|
||||
|
||||
private:
|
||||
/*------------- BLE para -------------*/
|
||||
bool _prph_enabled;
|
||||
@@ -155,9 +163,6 @@ class AdafruitBluefruit
|
||||
|
||||
BLEDfu _dfu_svc;
|
||||
|
||||
BLECharacteristic* _chars_list[BLE_GATT_MAX_SERVER_CHARS];
|
||||
uint8_t _chars_count;
|
||||
|
||||
uint16_t _conn_hdl;
|
||||
bool _bonded;
|
||||
|
||||
@@ -180,9 +185,6 @@ COMMENT_OUT(
|
||||
char _pin[BLE_GAP_PASSKEY_LEN];
|
||||
)
|
||||
|
||||
// Transmission Buffer Count for HVX notification, max is seen at 7
|
||||
SemaphoreHandle_t _txpacket_sem;
|
||||
|
||||
/*------------- Callbacks -------------*/
|
||||
connect_callback_t _connect_cb;
|
||||
disconnect_callback_t _discconnect_cb;
|
||||
|
||||
Reference in new issue
Block a user