From 3c0f3e84f123c03852776518600049c6c02ee803 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 30 Mar 2017 16:36:59 +0700 Subject: [PATCH] introduce BLEGatt class --- libraries/Bluefruit52Lib/README.md | 5 +- .../Bluefruit52Lib/src/BLECharacteristic.cpp | 2 +- .../Bluefruit52Lib/src/BLECharacteristic.h | 3 +- libraries/Bluefruit52Lib/src/BLEGatt.cpp | 62 ++++++++++++++++++ libraries/Bluefruit52Lib/src/BLEGatt.h | 64 +++++++++++++++++++ libraries/Bluefruit52Lib/src/bluefruit.cpp | 16 +---- libraries/Bluefruit52Lib/src/bluefruit.h | 4 +- 7 files changed, 135 insertions(+), 21 deletions(-) create mode 100644 libraries/Bluefruit52Lib/src/BLEGatt.cpp create mode 100644 libraries/Bluefruit52Lib/src/BLEGatt.h diff --git a/libraries/Bluefruit52Lib/README.md b/libraries/Bluefruit52Lib/README.md index af90e05c..20a783cc 100644 --- a/libraries/Bluefruit52Lib/README.md +++ b/libraries/Bluefruit52Lib/README.md @@ -1,2 +1,3 @@ -# Adafruit_nRF52_Arduino_Helpers -Helper libraries and classes for the Adafruit_nRF52_Arduino repository +# Adafruit Feather nRF52 Bluefruit Libraries + +Peripherals & Central library \ No newline at end of file diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index b778d01b..1db37c74 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -278,7 +278,7 @@ err_t BLECharacteristic::begin(void) if ( !_is_temp && (_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb) ) { - (void) Bluefruit._registerCharacteristic(this); + (void) Bluefruit.Gatt._registerCharacteristic(this); } return ERROR_NONE; diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index eb104a00..01d1317c 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -163,8 +163,7 @@ class BLECharacteristic err_t notify(uint16_t num); err_t notify(uint8_t num); - - friend class AdafruitBluefruit; + friend class BLEGatt; }; diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp new file mode 100644 index 00000000..a07f4537 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -0,0 +1,62 @@ +/**************************************************************************/ +/*! + @file BLEGatt.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" + + +BLEGatt::BLEGatt(void) +{ + _chars_count = 0; + for(uint8_t i=0; i_eventHandler(evt); + + +} + +bool BLEGatt::_registerCharacteristic(BLECharacteristic* chars) +{ + if ( _chars_count == BLE_MAX_CHARS ) return false; + _chars_list[ _chars_count++ ] = chars; + + return true; +} diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.h b/libraries/Bluefruit52Lib/src/BLEGatt.h new file mode 100644 index 00000000..81490948 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLEGatt.h @@ -0,0 +1,64 @@ +/**************************************************************************/ +/*! + @file BLEGatt.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 BLEGATT_H_ +#define BLEGATT_H_ + +#include +#include "bluefruit_common.h" + +#include "BLEUuid.h" +#include "BLECharacteristic.h" +#include "BLEService.h" + +#include "BLECentralCharacteristic.h" +#include "BLECentralService.h" + +#define BLE_MAX_CHARS 40 + +class BLEGatt +{ + public: + BLEGatt(void); + + bool _registerCharacteristic(BLECharacteristic* chars); + void _eventHandler(ble_evt_t* evt); + + private: + BLECharacteristic* _chars_list[BLE_MAX_CHARS]; + uint8_t _chars_count; +}; + +#endif /* BLEGATT_H_ */ diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index ce2df582..901592a1 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -116,9 +116,6 @@ COMMENT_OUT( varclr(_pin); ) - _chars_count = 0; - for(uint8_t i=0; i_eventHandler(evt); - } + Gatt._eventHandler(evt); } /*------------------------------------------------------------------*/ /* Bonds diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 677d16a7..4c6c7a62 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -46,6 +46,7 @@ #include "BLECentralCharacteristic.h" #include "BLECentralService.h" #include "BLEDiscovery.h" +#include "BLEGatt.h" // Services #include "services/BLEDis.h" @@ -60,7 +61,6 @@ #include "central_services/BLECentralUart.h" -#define BLE_MAX_CHARS 50 #define BLE_MAX_DATA_PER_MTU (GATT_MTU_SIZE_DEFAULT - 3) extern "C" @@ -84,6 +84,8 @@ class AdafruitBluefruit BLECentral Central; BLEDiscovery Discovery; + BLEGatt Gatt; + /*------------------------------------------------------------------*/ /* General Purpose Functions *------------------------------------------------------------------*/