From 6950c9831907536d40ab6df0db581a320dd4eb11 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 20 Feb 2017 12:35:23 +0700 Subject: [PATCH] group central API into separate class for clarification --- .../central/central_scan/central_scan.ino | 4 +- libraries/Bluefruit52Lib/src/BLECentral.cpp | 71 +++++++++++++++++++ libraries/Bluefruit52Lib/src/BLECentral.h | 68 ++++++++++++++++++ libraries/Bluefruit52Lib/src/bluefruit.cpp | 38 +++------- libraries/Bluefruit52Lib/src/bluefruit.h | 13 ++-- 5 files changed, 156 insertions(+), 38 deletions(-) create mode 100644 libraries/Bluefruit52Lib/src/BLECentral.cpp create mode 100644 libraries/Bluefruit52Lib/src/BLECentral.h diff --git a/libraries/Bluefruit52Lib/examples/central/central_scan/central_scan.ino b/libraries/Bluefruit52Lib/examples/central/central_scan/central_scan.ino index ac113bdd..a7340f9d 100644 --- a/libraries/Bluefruit52Lib/examples/central/central_scan/central_scan.ino +++ b/libraries/Bluefruit52Lib/examples/central/central_scan/central_scan.ino @@ -26,8 +26,8 @@ void setup() // Start Central Scan Bluefruit.setConnLedInterval(250); - Bluefruit.setScanCallback(scan_callback); - Bluefruit.startScanning(); + Bluefruit.central.setScanCallback(scan_callback); + Bluefruit.central.startScanning(); } void scan_callback(ble_gap_evt_adv_report_t* report) diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp new file mode 100644 index 00000000..d4755793 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -0,0 +1,71 @@ +/**************************************************************************/ +/*! + @file BLECentral.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" + +/** + * Constructor + */ +BLECentral::BLECentral(void) +{ + _scan_cb = NULL; + _scan_param = (ble_gap_scan_params_t) { + .active = 1, + .selective = 0, + .p_whitelist = NULL, + .interval = 0x00A0, + .window = 0x0050, + .timeout = 0, // no timeout + }; +} + +void BLECentral::setScanCallback(scan_callback_t fp) +{ + _scan_cb = fp; +} + +err_t BLECentral::startScanning(void) +{ + VERIFY_STATUS( sd_ble_gap_scan_start(&_scan_param) ); + Bluefruit._startConnLed(); // start blinking + return ERROR_NONE; +} + +err_t BLECentral::stopScanning(void) +{ + Bluefruit._stopConnLed(); // stop blinking + return sd_ble_gap_scan_stop(); +} diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h new file mode 100644 index 00000000..c0a542f4 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -0,0 +1,68 @@ +/**************************************************************************/ +/*! + @file BLECentral.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 BLECENTRAL_H_ +#define BLECENTRAL_H_ + +#include +#include "bluefruit_common.h" + +#include "BLEUuid.h" +#include "BLECharacteristic.h" +#include "BLEService.h" + +class AdafruitBluefruit; + +class BLECentral +{ + public: + BLECentral(void); // Constructor + + typedef void (*scan_callback_t) (ble_gap_evt_adv_report_t*); + + void setScanCallback(scan_callback_t fp); + err_t startScanning(void); + err_t stopScanning(void); + + private: + ble_gap_scan_params_t _scan_param; + scan_callback_t _scan_cb; + + friend class AdafruitBluefruit; +}; + + + +#endif /* BLECENTRAL_H_ */ diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index e2157a22..bfc56e70 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -99,6 +99,7 @@ static void bluefruit_blinky_cb( TimerHandle_t xTimer ) * Constructor */ AdafruitBluefruit::AdafruitBluefruit(void) + : central() { _prph_enabled = true; _central_enabled = false; @@ -127,17 +128,6 @@ AdafruitBluefruit::AdafruitBluefruit(void) _sys_attr = NULL; _sys_attr_len = 0; - - _scan_cb = NULL; - _scan_param = (ble_gap_scan_params_t) { - .active = 1, - .selective = 0, - .p_whitelist = NULL, - .interval = 0x00A0, - .window = 0x0050, - .timeout = 0, // no timeout - }; - _connect_cb = NULL; _discconnect_cb = NULL; } @@ -465,7 +455,7 @@ err_t AdafruitBluefruit::startAdvertising(void) VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para) ); - if (_led_conn) xTimerStart(_led_blink_th, 0); // start blinking + _startConnLed(); // start blinking return ERROR_NONE; } @@ -474,7 +464,7 @@ void AdafruitBluefruit::stopAdvertising(void) { sd_ble_gap_adv_stop(); - xTimerStop(_led_blink_th, 0); // stop blinking + _stopConnLed(); // stop blinking } err_t AdafruitBluefruit::addService(uint16_t uuid16) @@ -536,24 +526,16 @@ err_t AdafruitBluefruit::saveAllCCCD(void) } /*------------------------------------------------------------------*/ -/* Central API +/* Private Methods *------------------------------------------------------------------*/ -void AdafruitBluefruit::setScanCallback(scan_callback_t fp) +void AdafruitBluefruit::_startConnLed(void) { - _scan_cb = fp; + if (_led_conn) xTimerStart(_led_blink_th, 0); } -err_t AdafruitBluefruit::startScanning(void) +void AdafruitBluefruit::_stopConnLed(void) { - VERIFY_STATUS( sd_ble_gap_scan_start(&_scan_param) ); - if (_led_conn) xTimerStart(_led_blink_th, 0); // start blinking - return ERROR_NONE; -} - -err_t AdafruitBluefruit::stopScanning(void) -{ - xTimerStop(_led_blink_th, 0); // stop blinking - return sd_ble_gap_scan_stop(); + xTimerStop(_led_blink_th, 0); } /*------------------------------------------------------------------*/ @@ -642,7 +624,7 @@ void AdafruitBluefruit::_poll(void) ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected; - xTimerStop(_led_blink_th, 0); + _stopConnLed(); if (_led_conn) ledOn(LED_CONN); _conn_hdl = evt->evt.gap_evt.conn_handle; @@ -801,7 +783,7 @@ void AdafruitBluefruit::_poll(void) case BLE_GAP_EVT_ADV_REPORT: { ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report; - if (_scan_cb) _scan_cb(adv_report); + if (central._scan_cb) central._scan_cb(adv_report); } break; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 161f02ca..02c83bb9 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -40,6 +40,7 @@ #include "BLEUuid.h" #include "BLECharacteristic.h" #include "BLEService.h" +#include "BLECentral.h" // Services #include "services/BLEDis.h" @@ -124,10 +125,7 @@ class AdafruitBluefruit /*------------------------------------------------------------------*/ /* Central *------------------------------------------------------------------*/ - typedef void (*scan_callback_t) (ble_gap_evt_adv_report_t*); - void setScanCallback(scan_callback_t fp); - err_t startScanning(void); - err_t stopScanning(void); + BLECentral central; /*------------------------------------------------------------------*/ /* Callback @@ -177,18 +175,17 @@ class AdafruitBluefruit // Transmission Buffer Count for HVX notification, max is seen at 7 SemaphoreHandle_t _txbuf_sem; - /*------------- Central -------------*/ - ble_gap_scan_params_t _scan_param; - scan_callback_t _scan_cb; - /*------------- Callbacks -------------*/ void (*_connect_cb) (uint32_t hostID); void (*_discconnect_cb) (uint32_t hostID, uint8_t reason); bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len); void _poll(void); + void _startConnLed(void); + void _stopConnLed(void); friend void adafruit_bluefruit_task(void* arg); + friend class BLECentral; }; extern AdafruitBluefruit Bluefruit;