move discovery to its own class BLEDiscovery
This commit is contained in:
1 parent
ec9ea1ad6a
commit
6d79f0106e
12 files changed
+321
-209
No files matched your search
@@ -95,7 +95,7 @@ void loop()
|
||||
{
|
||||
Serial.print("Discovering BLE Uart Service ... ");
|
||||
|
||||
if ( bleCentralUart.discover() )
|
||||
if ( bleCentralUart.discover( Bluefruit.Central.connHandle() ) )
|
||||
{
|
||||
Serial.println("Found it");
|
||||
|
||||
|
||||
@@ -45,10 +45,6 @@ BLECentral::BLECentral(void)
|
||||
{
|
||||
_conn_hdl = BLE_CONN_HANDLE_INVALID;
|
||||
|
||||
_evt_sem = NULL;
|
||||
_evt_buf = NULL;
|
||||
_evt_bufsize = 0;
|
||||
|
||||
_txpacket_sem = NULL;
|
||||
|
||||
_svc_count = 0;
|
||||
@@ -67,8 +63,6 @@ BLECentral::BLECentral(void)
|
||||
.timeout = 0, // no timeout
|
||||
};
|
||||
|
||||
_disc_hdl_range.start_handle = 1;
|
||||
_disc_hdl_range.end_handle = 0xffff;
|
||||
|
||||
_connect_cb = NULL;
|
||||
_disconnect_cb = NULL;
|
||||
@@ -76,7 +70,7 @@ BLECentral::BLECentral(void)
|
||||
|
||||
void BLECentral::begin(void)
|
||||
{
|
||||
_evt_sem = xSemaphoreCreateBinary();
|
||||
|
||||
}
|
||||
|
||||
bool BLECentral::_registerService(BLECentralService* svc)
|
||||
@@ -237,107 +231,6 @@ void BLECentral::setDisconnectCallback( disconnect_callback_t fp)
|
||||
_disconnect_cb = fp;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* DISCOVERY
|
||||
*------------------------------------------------------------------*/
|
||||
bool BLECentral::discoverService(BLECentralService& svc, uint16_t start_handle)
|
||||
{
|
||||
ble_gattc_evt_prim_srvc_disc_rsp_t disc_svc;
|
||||
|
||||
_evt_buf = &disc_svc;
|
||||
_evt_bufsize = sizeof(disc_svc);
|
||||
|
||||
VERIFY_STATUS( sd_ble_gattc_primary_services_discover(_conn_hdl, start_handle, &svc.uuid._uuid), false );
|
||||
|
||||
// wait for discovery event: timeout or has no data
|
||||
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return false;
|
||||
|
||||
// Check the discovered UUID with input one
|
||||
if ( (disc_svc.count == 1) && (svc.uuid == disc_svc.services[0].uuid) )
|
||||
{
|
||||
_disc_hdl_range = disc_svc.services[0].handle_range;
|
||||
LOG_LV1(Discover, "[SVC] Found 0x%04X, Handle start = %d, end = %d", disc_svc.services[0].uuid.uuid, _disc_hdl_range.start_handle, _disc_hdl_range.end_handle);
|
||||
|
||||
_disc_hdl_range.start_handle++; // increase for characteristic discovery
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t BLECentral::discoverCharacteristic(BLECentralCharacteristic* chr[], uint8_t count)
|
||||
{
|
||||
uint8_t found = 0;
|
||||
|
||||
while( found < count )
|
||||
{
|
||||
ble_gattc_evt_char_disc_rsp_t disc_chr;
|
||||
|
||||
_evt_buf = &disc_chr;
|
||||
_evt_bufsize = sizeof(disc_chr);
|
||||
|
||||
LOG_LV1(Discover, "[CHR] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle);
|
||||
|
||||
VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &_disc_hdl_range), found );
|
||||
|
||||
// wait for discovery event: timeout or has no data
|
||||
// Assume only 1 characteristic discovered each
|
||||
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) || (disc_chr.count == 0) ) break;
|
||||
|
||||
// increase handle range for next discovery
|
||||
_disc_hdl_range.start_handle = disc_chr.chars[0].handle_value + 1;
|
||||
|
||||
// Look for matched uuid
|
||||
for (uint8_t i=0; i<count; i++)
|
||||
{
|
||||
if ( chr[i]->uuid == disc_chr.chars[0].uuid )
|
||||
{
|
||||
LOG_LV1(Discover, "[CHR] Found 0x%04X, handle = %d", disc_chr.chars[0].uuid.uuid, disc_chr.chars[0].handle_value);
|
||||
|
||||
chr[i]->_chr = disc_chr.chars[0];
|
||||
|
||||
// Discovery All descriptors as well
|
||||
chr[i]->discoverDescriptor();
|
||||
|
||||
found++;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
uint16_t BLECentral::_discoverDescriptor(ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count)
|
||||
{
|
||||
_evt_buf = disc_desc;
|
||||
_evt_bufsize = sizeof(ble_gattc_evt_desc_disc_rsp_t) + (max_count-1)*sizeof(ble_gattc_desc_t);
|
||||
|
||||
LOG_LV1(Discover, "[DESC] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle);
|
||||
|
||||
uint16_t result = 0;
|
||||
VERIFY_STATUS( sd_ble_gattc_descriptors_discover(_conn_hdl, &_disc_hdl_range), 0 );
|
||||
|
||||
// wait for discovery event: timeout or has no data
|
||||
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return 0;
|
||||
|
||||
result = min16(disc_desc->count, max_count);
|
||||
if (result)
|
||||
{
|
||||
for(uint16_t i=0; i<result; i++)
|
||||
{
|
||||
LOG_LV1(Discover, "[DESC] Descriptor %d: uuid = 0x%04X, handle = %d", i, disc_desc->descs[i].uuid.uuid, disc_desc->descs[i].handle);
|
||||
}
|
||||
|
||||
// increase handle range for next discovery
|
||||
// should be +1 more, but that will cause missing on the next Characteristic !!!!!
|
||||
// Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 (Char declaration) in the result
|
||||
_disc_hdl_range.start_handle = disc_desc->descs[result-1].handle;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event is forwarded from Bluefruit Poll() method
|
||||
@@ -426,84 +319,6 @@ void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
}
|
||||
break;
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* DISCOVERY
|
||||
*------------------------------------------------------------------*/
|
||||
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
|
||||
{
|
||||
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
|
||||
ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp;
|
||||
|
||||
if ( _conn_hdl == gattc->conn_handle )
|
||||
{
|
||||
LOG_LV1(Discover, "[SVC] Service Count: %d", svc_rsp->count);
|
||||
|
||||
if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS && svc_rsp->count && _evt_buf)
|
||||
{
|
||||
// Only support 1 service
|
||||
|
||||
_evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t));
|
||||
memcpy(_evt_buf, svc_rsp, _evt_bufsize);
|
||||
}else
|
||||
{
|
||||
_evt_bufsize = 0; // no data
|
||||
}
|
||||
|
||||
xSemaphoreGive(_evt_sem);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GATTC_EVT_CHAR_DISC_RSP:
|
||||
{
|
||||
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
|
||||
ble_gattc_evt_char_disc_rsp_t* chr_rsp = &gattc->params.char_disc_rsp;
|
||||
|
||||
if ( _conn_hdl == gattc->conn_handle )
|
||||
{
|
||||
LOG_LV1(Discover, "[CHR] Characteristic Count: %d", chr_rsp->count);
|
||||
if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && chr_rsp->count && _evt_buf )
|
||||
{
|
||||
// TODO support only 1 discovered char now
|
||||
_evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_char_disc_rsp_t));
|
||||
|
||||
memcpy(_evt_buf, chr_rsp, _evt_bufsize);
|
||||
}else
|
||||
{
|
||||
_evt_bufsize = 0; // no data
|
||||
}
|
||||
}
|
||||
|
||||
xSemaphoreGive(_evt_sem);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GATTC_EVT_DESC_DISC_RSP:
|
||||
{
|
||||
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
|
||||
ble_gattc_evt_desc_disc_rsp_t* desc_rsp = &gattc->params.desc_disc_rsp;
|
||||
|
||||
if ( _conn_hdl == gattc->conn_handle )
|
||||
{
|
||||
LOG_LV1(Discover, "[DESC] Descriptor Count: %d", desc_rsp->count);
|
||||
|
||||
if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && desc_rsp->count && _evt_buf )
|
||||
{
|
||||
// Copy up to bufsize
|
||||
uint16_t len = sizeof(ble_gattc_evt_desc_disc_rsp_t) + (desc_rsp->count-1)*sizeof(ble_gattc_desc_t);
|
||||
_evt_bufsize = min16(_evt_bufsize, len);
|
||||
|
||||
memcpy(_evt_buf, desc_rsp, _evt_bufsize);
|
||||
}else
|
||||
{
|
||||
_evt_bufsize = 0; // no data
|
||||
}
|
||||
}
|
||||
|
||||
xSemaphoreGive(_evt_sem);
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,8 +91,7 @@ class BLECentral
|
||||
/*------------------------------------------------------------------*/
|
||||
/* GATTC Discovery
|
||||
*------------------------------------------------------------------*/
|
||||
bool discoverService(BLECentralService& svc, uint16_t start_handle = 1);
|
||||
uint8_t discoverCharacteristic(BLECentralCharacteristic* chr[], uint8_t count);
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
@@ -104,24 +103,17 @@ class BLECentral
|
||||
void setConnectCallback ( connect_callback_t fp);
|
||||
void setDisconnectCallback( disconnect_callback_t fp);
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
* code. User should not call these directly
|
||||
*------------------------------------------------------------------*/
|
||||
uint16_t _discoverDescriptor(ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count);
|
||||
bool _registerService(BLECentralService* svc);
|
||||
bool _registerCharacteristic(BLECentralCharacteristic* chr);
|
||||
|
||||
private:
|
||||
uint16_t _conn_hdl;
|
||||
|
||||
SemaphoreHandle_t _evt_sem;
|
||||
void* _evt_buf;
|
||||
uint16_t _evt_bufsize;
|
||||
|
||||
// Transmission Buffer Count for HVX notification, max is seen at 7
|
||||
SemaphoreHandle_t _txpacket_sem;
|
||||
|
||||
@@ -134,14 +126,12 @@ class BLECentral
|
||||
ble_gap_scan_params_t _scan_param;
|
||||
scan_callback_t _scan_cb;
|
||||
|
||||
ble_gattc_handle_range_t _disc_hdl_range;
|
||||
|
||||
connect_callback_t _connect_cb;
|
||||
disconnect_callback_t _disconnect_cb;
|
||||
|
||||
bool _checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid[], uint8_t uuid_len);
|
||||
|
||||
void _event_handler(ble_evt_t* event);
|
||||
void _event_handler(ble_evt_t* evt);
|
||||
|
||||
friend class AdafruitBluefruit;
|
||||
};
|
||||
|
||||
@@ -60,6 +60,11 @@ BLECentralCharacteristic::BLECentralCharacteristic(BLEUuid bleuuid)
|
||||
_init();
|
||||
}
|
||||
|
||||
void BLECentralCharacteristic::assign(ble_gattc_char_t* gattc_chr)
|
||||
{
|
||||
_chr = *gattc_chr;
|
||||
}
|
||||
|
||||
uint16_t BLECentralCharacteristic::valueHandle()
|
||||
{
|
||||
return _chr.handle_value;
|
||||
@@ -70,14 +75,14 @@ BLECentralService& BLECentralCharacteristic::parentService (void)
|
||||
return *_service;
|
||||
}
|
||||
|
||||
bool BLECentralCharacteristic::discoverDescriptor(void)
|
||||
bool BLECentralCharacteristic::discoverDescriptor(uint16_t conn_handle)
|
||||
{
|
||||
struct {
|
||||
uint16_t count;
|
||||
ble_gattc_desc_t descs[MAX_DESCIRPTORS];
|
||||
}disc_rsp;
|
||||
|
||||
uint16_t count = Bluefruit.Central._discoverDescriptor((ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, MAX_DESCIRPTORS);
|
||||
uint16_t count = Bluefruit.Discovery._discoverDescriptor(conn_handle, (ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, MAX_DESCIRPTORS);
|
||||
|
||||
// only care CCCD for now
|
||||
for(uint16_t i=0; i<count; i++)
|
||||
|
||||
@@ -51,7 +51,7 @@ class BLECentralCharacteristic
|
||||
BLECentralCharacteristic(void);
|
||||
BLECentralCharacteristic(BLEUuid bleuuid);
|
||||
|
||||
bool discoverDescriptor(void);
|
||||
bool discoverDescriptor(uint16_t conn_handle);
|
||||
void begin(void);
|
||||
|
||||
uint16_t valueHandle();
|
||||
@@ -74,6 +74,8 @@ class BLECentralCharacteristic
|
||||
|
||||
void setNotifyCallback(notify_cb_t fp);
|
||||
|
||||
void assign(ble_gattc_char_t* gattc_chr);
|
||||
|
||||
private:
|
||||
ble_gattc_char_t _chr;
|
||||
uint16_t _cccd_handle;
|
||||
|
||||
@@ -68,9 +68,9 @@ bool BLECentralService::begin(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLECentralService::discover(void)
|
||||
bool BLECentralService::discover(uint16_t conn_handle)
|
||||
{
|
||||
VERIFY( Bluefruit.Central.discoverService(*this) );
|
||||
VERIFY( Bluefruit.Discovery.discoverService(conn_handle, *this) );
|
||||
_discovered = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ class BLECentralService
|
||||
|
||||
virtual bool begin(void);
|
||||
|
||||
virtual bool discover (void);
|
||||
virtual bool discover (uint16_t conn_handle);
|
||||
bool discovered(void);
|
||||
|
||||
friend class BLECentral;
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEDiscovery.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"
|
||||
|
||||
#define BLE_CENTRAL_TIMEOUT 3000
|
||||
|
||||
BLEDiscovery::BLEDiscovery(void)
|
||||
{
|
||||
_disc_hdl_range.start_handle = 1;
|
||||
_disc_hdl_range.end_handle = 0xffff;
|
||||
|
||||
_evt_sem = NULL;
|
||||
_evt_buf = NULL;
|
||||
_evt_bufsize = 0;
|
||||
}
|
||||
|
||||
void BLEDiscovery::begin(void)
|
||||
{
|
||||
_evt_sem = xSemaphoreCreateBinary();
|
||||
}
|
||||
|
||||
bool BLEDiscovery::discoverService(uint16_t conn_handle, BLECentralService& svc, uint16_t start_handle)
|
||||
{
|
||||
ble_gattc_evt_prim_srvc_disc_rsp_t disc_svc;
|
||||
|
||||
_evt_buf = &disc_svc;
|
||||
_evt_bufsize = sizeof(disc_svc);
|
||||
|
||||
VERIFY_STATUS( sd_ble_gattc_primary_services_discover(conn_handle, start_handle, &svc.uuid._uuid), false );
|
||||
|
||||
// wait for discovery event: timeout or has no data
|
||||
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return false;
|
||||
|
||||
// Check the discovered UUID with input one
|
||||
if ( (disc_svc.count == 1) && (svc.uuid == disc_svc.services[0].uuid) )
|
||||
{
|
||||
_disc_hdl_range = disc_svc.services[0].handle_range;
|
||||
LOG_LV1(Discover, "[SVC] Found 0x%04X, Handle start = %d, end = %d", disc_svc.services[0].uuid.uuid, _disc_hdl_range.start_handle, _disc_hdl_range.end_handle);
|
||||
|
||||
_disc_hdl_range.start_handle++; // increase for characteristic discovery
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLECentralCharacteristic* chr[], uint8_t count)
|
||||
{
|
||||
uint8_t found = 0;
|
||||
|
||||
while( found < count )
|
||||
{
|
||||
ble_gattc_evt_char_disc_rsp_t disc_chr;
|
||||
|
||||
_evt_buf = &disc_chr;
|
||||
_evt_bufsize = sizeof(disc_chr);
|
||||
|
||||
LOG_LV1(Discover, "[CHR] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle);
|
||||
|
||||
VERIFY_STATUS( sd_ble_gattc_characteristics_discover(conn_handle, &_disc_hdl_range), found );
|
||||
|
||||
// wait for discovery event: timeout or has no data
|
||||
// Assume only 1 characteristic discovered each
|
||||
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) || (disc_chr.count == 0) ) break;
|
||||
|
||||
// increase handle range for next discovery
|
||||
_disc_hdl_range.start_handle = disc_chr.chars[0].handle_value + 1;
|
||||
|
||||
// Look for matched uuid
|
||||
for (uint8_t i=0; i<count; i++)
|
||||
{
|
||||
if ( chr[i]->uuid == disc_chr.chars[0].uuid )
|
||||
{
|
||||
LOG_LV1(Discover, "[CHR] Found 0x%04X, handle = %d", disc_chr.chars[0].uuid.uuid, disc_chr.chars[0].handle_value);
|
||||
|
||||
// characteristic assign overload
|
||||
chr[i]->assign(&disc_chr.chars[0]);
|
||||
|
||||
// Discovery All descriptors as well
|
||||
chr[i]->discoverDescriptor(conn_handle);
|
||||
|
||||
found++;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
uint16_t BLEDiscovery::_discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count)
|
||||
{
|
||||
_evt_buf = disc_desc;
|
||||
_evt_bufsize = sizeof(ble_gattc_evt_desc_disc_rsp_t) + (max_count-1)*sizeof(ble_gattc_desc_t);
|
||||
|
||||
LOG_LV1(Discover, "[DESC] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle);
|
||||
|
||||
uint16_t result = 0;
|
||||
VERIFY_STATUS( sd_ble_gattc_descriptors_discover(conn_handle, &_disc_hdl_range), 0 );
|
||||
|
||||
// wait for discovery event: timeout or has no data
|
||||
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return 0;
|
||||
|
||||
result = min16(disc_desc->count, max_count);
|
||||
if (result)
|
||||
{
|
||||
for(uint16_t i=0; i<result; i++)
|
||||
{
|
||||
LOG_LV1(Discover, "[DESC] Descriptor %d: uuid = 0x%04X, handle = %d", i, disc_desc->descs[i].uuid.uuid, disc_desc->descs[i].handle);
|
||||
}
|
||||
|
||||
// increase handle range for next discovery
|
||||
// should be +1 more, but that will cause missing on the next Characteristic !!!!!
|
||||
// Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 (Char declaration) in the result
|
||||
_disc_hdl_range.start_handle = disc_desc->descs[result-1].handle;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void BLEDiscovery::_event_handler(ble_evt_t* evt)
|
||||
{
|
||||
switch ( evt->header.evt_id )
|
||||
{
|
||||
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
|
||||
{
|
||||
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
|
||||
ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp;
|
||||
|
||||
LOG_LV1(Discover, "[SVC] Service Count: %d", svc_rsp->count);
|
||||
|
||||
if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS && svc_rsp->count && _evt_buf)
|
||||
{
|
||||
// Only support 1 service
|
||||
|
||||
_evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t));
|
||||
memcpy(_evt_buf, svc_rsp, _evt_bufsize);
|
||||
}else
|
||||
{
|
||||
_evt_bufsize = 0; // no data
|
||||
}
|
||||
|
||||
xSemaphoreGive(_evt_sem);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GATTC_EVT_CHAR_DISC_RSP:
|
||||
{
|
||||
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
|
||||
ble_gattc_evt_char_disc_rsp_t* chr_rsp = &gattc->params.char_disc_rsp;
|
||||
|
||||
LOG_LV1(Discover, "[CHR] Characteristic Count: %d", chr_rsp->count);
|
||||
if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && chr_rsp->count && _evt_buf )
|
||||
{
|
||||
// TODO support only 1 discovered char now
|
||||
_evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_char_disc_rsp_t));
|
||||
|
||||
memcpy(_evt_buf, chr_rsp, _evt_bufsize);
|
||||
}else
|
||||
{
|
||||
_evt_bufsize = 0; // no data
|
||||
}
|
||||
|
||||
xSemaphoreGive(_evt_sem);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GATTC_EVT_DESC_DISC_RSP:
|
||||
{
|
||||
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
|
||||
ble_gattc_evt_desc_disc_rsp_t* desc_rsp = &gattc->params.desc_disc_rsp;
|
||||
|
||||
LOG_LV1(Discover, "[DESC] Descriptor Count: %d", desc_rsp->count);
|
||||
|
||||
if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && desc_rsp->count && _evt_buf )
|
||||
{
|
||||
// Copy up to bufsize
|
||||
uint16_t len = sizeof(ble_gattc_evt_desc_disc_rsp_t) + (desc_rsp->count-1)*sizeof(ble_gattc_desc_t);
|
||||
_evt_bufsize = min16(_evt_bufsize, len);
|
||||
|
||||
memcpy(_evt_buf, desc_rsp, _evt_bufsize);
|
||||
}else
|
||||
{
|
||||
_evt_bufsize = 0; // no data
|
||||
}
|
||||
|
||||
xSemaphoreGive(_evt_sem);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEDiscovery.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 BLEDISCOVERY_H_
|
||||
#define BLEDISCOVERY_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "bluefruit_common.h"
|
||||
|
||||
#include "BLEUuid.h"
|
||||
#include "BLECentralCharacteristic.h"
|
||||
#include "BLECentralService.h"
|
||||
|
||||
class BLEDiscovery
|
||||
{
|
||||
private:
|
||||
ble_gattc_handle_range_t _disc_hdl_range;
|
||||
void* _evt_buf;
|
||||
uint16_t _evt_bufsize;
|
||||
|
||||
SemaphoreHandle_t _evt_sem;
|
||||
|
||||
void _event_handler(ble_evt_t* evt);
|
||||
|
||||
public:
|
||||
BLEDiscovery(void);
|
||||
|
||||
void begin(void);
|
||||
bool discoverService(uint16_t conn_handle, BLECentralService& svc, uint16_t start_handle = 1);
|
||||
uint8_t discoverCharacteristic(uint16_t conn_handle, BLECentralCharacteristic* chr[], uint8_t count);
|
||||
uint16_t _discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count);
|
||||
|
||||
friend class AdafruitBluefruit;
|
||||
friend class BLECentral;
|
||||
};
|
||||
|
||||
#endif /* BLEDISCOVERY_H_ */
|
||||
@@ -45,6 +45,7 @@
|
||||
#include "BLECentral.h"
|
||||
#include "BLECentralCharacteristic.h"
|
||||
#include "BLECentralService.h"
|
||||
#include "BLEDiscovery.h"
|
||||
|
||||
// Services
|
||||
#include "services/BLEDis.h"
|
||||
@@ -81,6 +82,7 @@ class AdafruitBluefruit
|
||||
BLEAdvertising Advertising;
|
||||
BLEAdvertising ScanResponse;
|
||||
BLECentral Central;
|
||||
BLEDiscovery Discovery;
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* General Purpose Functions
|
||||
|
||||
@@ -69,15 +69,15 @@ void BLECentralUart::setRxCallback( rx_callback_t fp)
|
||||
_rx_cb = fp;
|
||||
}
|
||||
|
||||
bool BLECentralUart::discover(void)
|
||||
bool BLECentralUart::discover(uint16_t conn_handle)
|
||||
{
|
||||
// Call BLECentralService discover
|
||||
VERIFY( BLECentralService::discover() );
|
||||
VERIFY( BLECentralService::discover(conn_handle) );
|
||||
_discovered = false;
|
||||
|
||||
// Discover TXD, RXD characteristics
|
||||
BLECentralCharacteristic* chr_arr[] = { &_rxd, &_txd };
|
||||
VERIFY( 2 == Bluefruit.Central.discoverCharacteristic(chr_arr, 2) );
|
||||
VERIFY( 2 == Bluefruit.Discovery.discoverCharacteristic(conn_handle, chr_arr, 2) );
|
||||
|
||||
_discovered = true;
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ class BLECentralUart : public BLECentralService, public Stream
|
||||
BLECentralUart(uint16_t fifo_depth = BLE_UART_DEFAULT_FIFO_DEPTH);
|
||||
|
||||
virtual bool begin(void);
|
||||
virtual bool discover(void);
|
||||
virtual bool discover(uint16_t conn_handle);
|
||||
|
||||
bool enableNotify(void);
|
||||
void setRxCallback( rx_callback_t fp);
|
||||
|
||||
Reference in new issue
Block a user