diff --git a/cores/nRF5/common_func.h b/cores/nRF5/common_func.h index 91071b8f..661e31a0 100644 --- a/cores/nRF5/common_func.h +++ b/cores/nRF5/common_func.h @@ -93,9 +93,17 @@ int cprintf(const char * format, ...); #define PRINT_LOCATION() cprintf("%s: %d:\n", __PRETTY_FUNCTION__, __LINE__) #define PRINT_MESS(x) cprintf("%s: %d: " x " \n" , __PRETTY_FUNCTION__, __LINE__) #define PRTNT_HEAP() if (CFG_DEBUG == 3) cprintf("\n%s: %d: Heap free: %d\n", __PRETTY_FUNCTION__, __LINE__, util_heap_get_free_size()) -#define PRINT_INT(x) cprintf("%s: %d: " #x " = %ld\n" , __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) ) -#define PRINT_HEX(x) cprintf("%s: %d: " #x " = %08lx\n", __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) ) #define PRINT_STR(x) cprintf("%s: %d: " #x " = %s\n" , __PRETTY_FUNCTION__, __LINE__, (char*)(x) ) +#define PRINT_INT(x) cprintf("%s: %d: " #x " = %ld\n" , __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) ) + +#define PRINT_HEX(x) \ + do {\ + cprintf("%s: %d: " #x " = ", __PRETTY_FUNCTION__, __LINE__);\ + char fmt[] = "%00X\n";\ + fmt[2] += 2*sizeof(x); /* Hex with correct size */\ + cprintf(fmt, (x) );\ + }while(0) + #define PRINT_BUFFER(buf, n) \ do {\ uint8_t const* p8 = (uint8_t const*) (buf);\ diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index f9224bb6..2bf913dd 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -57,7 +57,6 @@ #define CFG_DEFAULT_NAME "Bluefruit52" #define CFG_ADV_BLINKY_INTERVAL 500 - // Converts an integer of 1.25ms units to msecs #define MS100TO125(ms100) (((ms100)*4)/5) @@ -128,6 +127,17 @@ 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; } @@ -237,6 +247,15 @@ void AdafruitBluefruit::autoConnLed(bool enabled) _led_conn = enabled; } +void AdafruitBluefruit::setConnLedInterval(uint32_t ms) +{ + BaseType_t active = xTimerIsTimerActive(_led_blink_th); + xTimerChangePeriod(_led_blink_th, ms2tick(ms), 0); + + // Change period of inactive timer will also start it !! + if ( !active ) xTimerStop(_led_blink_th, 0); +} + bool AdafruitBluefruit::connected(void) { return ( _conn_hdl != BLE_CONN_HANDLE_INVALID ); @@ -516,6 +535,30 @@ err_t AdafruitBluefruit::saveAllCCCD(void) return sd_ble_gatts_sys_attr_get(_conn_hdl, _sys_attr, &_sys_attr_len, SVC_CONTEXT_FLAG); } +/*------------------------------------------------------------------*/ +/* Central API + *------------------------------------------------------------------*/ +void AdafruitBluefruit::setScanCallback(scan_callback_t fp) +{ + _scan_cb = fp; +} + +err_t AdafruitBluefruit::startScanning(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(); +} + +/*------------------------------------------------------------------*/ +/* Thread & SoftDevice Event handler + *------------------------------------------------------------------*/ void adafruit_bluefruit_task(void* arg) { (void) arg; @@ -755,7 +798,17 @@ void AdafruitBluefruit::_poll(void) break; - default: break; + 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); + } + break; + + + default: + //PRINT_HEX(evt->header.evt_id); + break; } // GATTs characteristics event handler diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index dcd9ce12..dce43141 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -58,34 +58,35 @@ class AdafruitBluefruit { public: - // Constructor - AdafruitBluefruit(void); + AdafruitBluefruit(void); // Constructor err_t begin(uint8_t prph_conn = 1, uint8_t central_conn = 0); void autoConnLed(bool enabled); - + void setConnLedInterval(uint32_t ms); void setName(const char* str); // bool setTxPower(int8_t power); - /*------------- Advertising -------------*/ + /*------------------------------------------------------------------*/ + /* Advertising & Scan Resp (active scan) + *------------------------------------------------------------------*/ uint8_t getAdvLen(void); uint8_t getAdvData(uint8_t* buffer); bool setAdvData(const uint8_t* data, uint8_t count); void clearAdvData(void); - bool addAdvData(uint8_t type, const void* data, uint8_t len); + bool addAdvData(uint8_t type, const void* data, uint8_t len); - bool addAdvFlags(uint8_t flags); - bool addAdvTxPower(void); - bool addAdvName(void); - bool addAdvApperance(uint16_t appearance); + bool addAdvFlags(uint8_t flags); + bool addAdvTxPower(void); + bool addAdvName(void); + bool addAdvApperance(uint16_t appearance); - bool addAdvUuid(uint16_t uuid16); - bool addAdvUuid(uint8_t const uuid128[]); - bool addAdvService(BLEService& service); + bool addAdvUuid(uint16_t uuid16); + bool addAdvUuid(uint8_t const uuid128[]); + bool addAdvService(BLEService& service); - bool setAdvBeacon(BLEBeacon& beacon); + bool setAdvBeacon(BLEBeacon& beacon); // Scan Response Data (less helper than Adv packet) uint8_t getScanRespLen(void); @@ -93,14 +94,17 @@ class AdafruitBluefruit bool setScanRespData(const uint8_t* data, uint8_t count); void clearScanData(void); - bool addScanRespData(uint8_t type, const void* data, uint8_t len); - bool addScanRespName(void); - bool addScanRespUuid(uint16_t uuid16); - bool addScanRespUuid(uint8_t const uuid128[]); + bool addScanRespData(uint8_t type, const void* data, uint8_t len); + bool addScanRespName(void); + bool addScanRespUuid(uint16_t uuid16); + bool addScanRespUuid(uint8_t const uuid128[]); err_t startAdvertising(void); void stopAdvertising(void); + /*------------------------------------------------------------------*/ + /* + *------------------------------------------------------------------*/ // Add service without using BLEService instance err_t addService(uint16_t uuid16); err_t addService(uint8_t const uuid128[]); @@ -120,9 +124,14 @@ class AdafruitBluefruit /*------------------------------------------------------------------*/ /* Central *------------------------------------------------------------------*/ -// void + 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); - /*------------- Callback -------------*/ + /*------------------------------------------------------------------*/ + /* Callback + *------------------------------------------------------------------*/ void setConnectCallback ( void (*fp) (uint32_t hostID) ); void setDisconnectCallback( void (*fp) (uint32_t hostID, uint8_t reason) ); @@ -168,6 +177,11 @@ 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);