adding BLECentralService, BLECentralCharacteristic, BLECentralUart serivce

This commit is contained in:
hathach committed 2017-03-22 17:17:11 +07:00
1 parent 9ca48429cf
commit 6354c8d186
12 files changed
+371 -46

No files matched your search

+57 -30
View File
@@ -59,11 +59,11 @@ BLECentral::BLECentral(void)
.timeout = 0, // no timeout
};
_hdl_range.start_handle = 1;
_hdl_range.end_handle = 0xffff;
_disc_hdl_range.start_handle = 1;
_disc_hdl_range.end_handle = 0xffff;
_connect_cb = NULL;
_discconnect_cb = NULL;
_disconnect_cb = NULL;
}
void BLECentral::begin(void)
@@ -127,10 +127,8 @@ bool BLECentral::checkUuidInScan(const ble_gap_evt_adv_report_t* report, BLEUuid
uint8_t type_arr[2];
PRINT_INT(uuid_len);
// Check both UUID16 more available and complete list
if ( uuid_len == 2)
if ( uuid_len == 16)
{
type_arr[0] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE;
type_arr[1] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE;
@@ -144,6 +142,8 @@ bool BLECentral::checkUuidInScan(const ble_gap_evt_adv_report_t* report, BLEUuid
uuid = ble_uuid._uuid128;
}
uuid_len /= 8; // convert uuid_len to number of bytes
for (int i=0; i<2; i++)
{
uint8_t len = 0;
@@ -199,20 +199,20 @@ void BLECentral::setConnectCallback( connect_callback_t fp)
void BLECentral::setDisconnectCallback( disconnect_callback_t fp)
{
_discconnect_cb = fp;
_disconnect_cb = fp;
}
/*------------------------------------------------------------------*/
/* DISCOVERY
*------------------------------------------------------------------*/
bool BLECentral::discoverService(BLEUuid uuid, uint16_t start_handle)
bool BLECentral::discoverService(BLEUuid uuid, ble_gattc_handle_range_t* hdl_range, uint16_t start_handle)
{
uuid.begin(); // add uuid128 if needed
ble_gattc_evt_prim_srvc_disc_rsp_t discover_svc;
ble_gattc_evt_prim_srvc_disc_rsp_t disc_svc;
_evt_buf = &discover_svc;
_evt_bufsize = sizeof(discover_svc);
_evt_buf = &disc_svc;
_evt_bufsize = sizeof(disc_svc);
VERIFY_STATUS( sd_ble_gattc_primary_services_discover(_conn_hdl, start_handle, &uuid._uuid), false );
@@ -220,27 +220,49 @@ bool BLECentral::discoverService(BLEUuid uuid, uint16_t start_handle)
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return false;
// Check the discovered UUID with input one
if ( (discover_svc.count == 1) && (uuid == discover_svc.services[0].uuid) )
if ( (disc_svc.count == 1) && (uuid == disc_svc.services[0].uuid) )
{
_hdl_range = discover_svc.services[0].handle_range;
PRINT_INT(_hdl_range.start_handle);
PRINT_INT(_hdl_range.end_handle);
_disc_hdl_range = disc_svc.services[0].handle_range;
(*hdl_range) = disc_svc.services[0].handle_range;;
PRINT_INT(_disc_hdl_range.start_handle);
PRINT_INT(_disc_hdl_range.end_handle);
return true;
}
return false;
}
COMMENT_OUT(
bool BLECentral::discoverCharacteristic()
bool BLECentral::discoverService(BLECentralService& svc, uint16_t start_handle)
{
// uuid.begin(); // add uuid128 if needed
return svc.discover(start_handle);
}
VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &_hdl_range), false );
bool BLECentral::discoverCharacteristic(BLECentralCharacteristic& chr)
{
ble_gattc_evt_char_disc_rsp_t disc_chr;
_evt_buf = &disc_chr;
_evt_bufsize = sizeof(disc_chr);
PRINT_INT(_disc_hdl_range.start_handle);
PRINT_INT(_disc_hdl_range.end_handle);
VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &_disc_hdl_range), false );
// wait for discovery event: timeout or has no data
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return false;
if ( disc_chr.count > 0 )
{
chr = BLECentralCharacteristic(&disc_chr.chars[0]);
// increase handle range for next characteristic
_disc_hdl_range.start_handle = disc_chr.chars[0].handle_value + 1;
}
return true;
}
)
/**
* Event is forwarded from Bluefruit Poll() method
@@ -280,7 +302,7 @@ void BLECentral::_event_handler(ble_evt_t* evt)
_conn_hdl = BLE_CONN_HANDLE_INVALID;
if ( _discconnect_cb ) _discconnect_cb(evt->evt.gap_evt.params.disconnected.reason);
if ( _disconnect_cb ) _disconnect_cb(evt->evt.gap_evt.params.disconnected.reason);
startScanning();
}
@@ -306,6 +328,8 @@ void BLECentral::_event_handler(ble_evt_t* evt)
{
ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp;
PRINT_INT(svc_rsp->count);
// len of the discovered services
uint16_t len = sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t) + (svc_rsp->count-1)*sizeof(ble_gattc_service_t);
_evt_bufsize = min16(_evt_bufsize, len);
@@ -313,7 +337,7 @@ void BLECentral::_event_handler(ble_evt_t* evt)
memcpy(_evt_buf, svc_rsp, _evt_bufsize);
}else
{
_evt_bufsize = 0;
_evt_bufsize = 0; // no data
}
}
@@ -325,21 +349,24 @@ void BLECentral::_event_handler(ble_evt_t* evt)
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 )
{
if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS)
PRINT_INT(chr_rsp->count);
if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && (chr_rsp->count > 0) )
{
ble_gattc_evt_char_disc_rsp_t* chr_rsp = &gattc->params.char_disc_rsp;
// TODO support only 1 discovered char now
_evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_char_disc_rsp_t));
PRINT_INT(chr_rsp->count);
for(uint8_t i=0; i<chr_rsp->count; i++)
{
PRINT_HEX(chr_rsp->chars[i].uuid.type);
PRINT_HEX(chr_rsp->chars[i].uuid.uuid);
}
memcpy(_evt_buf, chr_rsp, _evt_bufsize);
}else
{
_evt_bufsize = 0; // no data
}
}
xSemaphoreGive(_evt_sem);
}
break;
+8 -5
View File
@@ -41,9 +41,11 @@
#include "BLEUuid.h"
#include "BLECharacteristic.h"
#include "BLECentralCharacteristic.h"
#include "BLEService.h"
#include "BLECentralCharacteristic.h"
#include "BLECentralService.h"
class AdafruitBluefruit;
class BLECentral
@@ -83,8 +85,9 @@ class BLECentral
/*------------------------------------------------------------------*/
/* GATTC Discovery
*------------------------------------------------------------------*/
bool discoverService(BLEUuid uuid, uint16_t start_handle = 1);
// bool discoverCharacteristic();
bool discoverService(BLEUuid uuid, ble_gattc_handle_range_t* hdl_range, uint16_t start_handle = 1);
bool discoverService(BLECentralService& svc, uint16_t start_handle = 1);
bool discoverCharacteristic(BLECentralCharacteristic& chr);
/*------------------------------------------------------------------*/
/* CALLBACKS
@@ -105,10 +108,10 @@ class BLECentral
ble_gap_scan_params_t _scan_param;
scan_callback_t _scan_cb;
ble_gattc_handle_range_t _hdl_range;
ble_gattc_handle_range_t _disc_hdl_range;
connect_callback_t _connect_cb;
disconnect_callback_t _discconnect_cb;
disconnect_callback_t _disconnect_cb;
bool _checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid[], uint8_t uuid_len);
@@ -36,3 +36,14 @@
#include "bluefruit.h"
BLECentralCharacteristic::BLECentralCharacteristic(void)
: uuid()
{
varclr(&_chr);
}
BLECentralCharacteristic::BLECentralCharacteristic(ble_gattc_char_t* gattc_char)
: uuid(gattc_char->uuid)
{
_chr = (*gattc_char);
}
@@ -33,20 +33,26 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#ifndef BLECHARACTERISTICCENTRAL_H_
#define BLECHARACTERISTICCENTRAL_H_
#ifndef BLECENTRALCHARACTERISTIC_H_
#define BLECENTRALCHARACTERISTIC_H_
#include "bluefruit_common.h"
#include "BLEUuid.h"
#include "BLECharacteristic.h"
class BLECharacteristicCentral
class BLECentralCharacteristic
{
public:
BLEUuid uuid;
BLECentralCharacteristic(void);
BLECentralCharacteristic(ble_gattc_char_t* gattc_char);
private:
ble_gattc_char_t _chr;
};
#endif /* BLECHARACTERISTICCENTRAL_H_ */
#endif /* BLECENTRALCHARACTERISTIC_H_ */
@@ -0,0 +1,69 @@
/**************************************************************************/
/*!
@file BLECentralService.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"
void BLECentralService::_init(void)
{
_hdl_range.start_handle = 1;
_hdl_range.end_handle = 0xffff;
}
BLECentralService::BLECentralService(void)
: uuid()
{
_init();
}
BLECentralService::BLECentralService(BLEUuid bleuuid)
: uuid(bleuuid)
{
_init();
}
err_t BLECentralService::begin(void)
{
// Add UUID128 if needed
uuid.begin();
return ERROR_NONE;
}
bool BLECentralService::discover(uint16_t start_handle)
{
return Bluefruit.Central.discoverService(uuid, &_hdl_range, start_handle);
}
@@ -0,0 +1,62 @@
/**************************************************************************/
/*!
@file BLECentralService.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 BLECENTRALSERVICE_H_
#define BLECENTRALSERVICE_H_
#include "bluefruit_common.h"
#include "BLEUuid.h"
class BLECentralService
{
protected:
ble_gattc_handle_range_t _hdl_range;
void _init(void);
public:
BLEUuid uuid;
BLECentralService(void);
BLECentralService(BLEUuid bleuuid);
virtual err_t begin(void);
virtual bool discover(uint16_t start_handle = 1);
};
#endif /* BLECENTRALSERVICE_H_ */
+25 -3
View File
@@ -40,18 +40,40 @@ void BLEUuid::set(uint16_t uuid16)
{
_uuid.type = BLE_UUID_TYPE_BLE;
_uuid.uuid = uuid16;
_uuid128 = NULL;
}
void BLEUuid::set(uint8_t const uuid128[16])
{
_uuid.type = BLE_UUID_TYPE_UNKNOWN; _uuid.uuid = 0;
_uuid128 = uuid128;
_uuid.type = BLE_UUID_TYPE_UNKNOWN;
_uuid.uuid = 0;
_uuid128 = uuid128;
}
void BLEUuid::get(uint16_t& uuid16 )
bool BLEUuid::get(uint16_t* uuid16 ) const
{
*uuid16 = _uuid.uuid;
return true;
}
bool BLEUuid::get(uint8_t uuid128[16])
{
if (_uuid.type < BLE_UUID_TYPE_VENDOR_BEGIN ) return false;
if ( _uuid128 )
{
memcpy(uuid128, _uuid128, 16);
}else
{
uint8_t len = 16;
VERIFY_STATUS( sd_ble_uuid_encode(&_uuid, &len, uuid128), false);
VERIFY(len == 16);
}
return true;
}
/**
+5 -3
View File
@@ -46,13 +46,15 @@ class BLEUuid
// Constructors
BLEUuid(void ) { _uuid.type = BLE_UUID_TYPE_UNKNOWN; _uuid.uuid = 0; _uuid128 = NULL; }
BLEUuid(uint16_t uuid16 ) { set(uuid16); }
BLEUuid(uint8_t const uuid128[16] ) { set(uuid128); }
BLEUuid(uint16_t uuid16 ) { set(uuid16 ); }
BLEUuid(uint8_t const uuid128[16] ) { set(uuid128); }
BLEUuid(ble_uuid_t uuid ) { _uuid = uuid; _uuid128 = NULL; }
void set(uint16_t uuid16);
void set(uint8_t const uuid128[16]);
void get(uint16_t& uuid16);
bool get(uint16_t* uuid16) const;
bool get(uint8_t uuid128[16]);
size_t size (void) const;
+3
View File
@@ -44,6 +44,7 @@
#include "BLECentral.h"
#include "BLECentralCharacteristic.h"
#include "BLECentralService.h"
// Services
#include "services/BLEDis.h"
@@ -55,6 +56,8 @@
#include "services/BLEHidAdafruit.h"
#include "services/BLEMidi.h"
#include "central_services/BLECentralUart.h"
#define BLE_MAX_CHARS 50
#define BLE_MAX_DATA_PER_MTU (GATT_MTU_SIZE_DEFAULT - 3)
@@ -0,0 +1,59 @@
/**************************************************************************/
/*!
@file BLECentralUart.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"
BLECentralUart::BLECentralUart(uint16_t fifo_depth)
: BLECentralService(BLEUART_UUID_SERVICE), _txd(), _rxd(), _rxd_fifo(fifo_depth, 1)
{
}
err_t BLECentralUart::begin(void)
{
// Add UUID128 if needed
uuid.begin();
return ERROR_NONE;
}
bool BLECentralUart::discover(uint16_t start_handle)
{
// Call BLECentralService discover
VERIFY( BLECentralService::discover(start_handle) );
return true;
}
@@ -0,0 +1,62 @@
/**************************************************************************/
/*!
@file BLECentralUart.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 BLECENTRALUART_H_
#define BLECENTRALUART_H_
#include "bluefruit_common.h"
#include "utility/adafruit_fifo.h"
#include "BLECentralCharacteristic.h"
#include "BLECentralService.h"
#include "services/BLEUart.h"
class BLECentralUart : public BLECentralService
{
public:
BLECentralUart(uint16_t fifo_depth = BLE_UART_DEFAULT_FIFO_DEPTH);
virtual err_t begin(void);
virtual bool discover(uint16_t start_handle = 1);
private:
BLECentralCharacteristic _txd;
BLECentralCharacteristic _rxd;
Adafruit_FIFO _rxd_fifo;
};
#endif /* BLECENTRALUART_H_ */
@@ -110,7 +110,6 @@ err_t BLEUart::begin(void)
_rxd.setStringDescriptor("RXD");
VERIFY_STATUS(_rxd.begin());
return ERROR_NONE;
}