Add support for scanning.

This commit is contained in:
Rohit Grover 2015-05-12 10:56:12 +01:00
commit 853f379e69
4 changed files with 62 additions and 8 deletions

View file

@ -163,6 +163,17 @@ static void btle_handler(ble_evt_t *p_ble_evt)
// BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
break;
case BLE_GAP_EVT_ADV_REPORT: {
const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
nRF51Gap::getInstance().processAdvertisementReport(advReport->peer_addr.addr,
advReport->rssi,
advReport->scan_rsp,
static_cast<Gap::AdvertisementType_t>(advReport->type),
advReport->dlen,
advReport->data);
break;
}
default:
break;
}

View file

@ -97,3 +97,19 @@ static void error_callback(uint32_t nrf_error)
ASSERT_STATUS_RET_VOID( nrf_error );
}
#endif // SDK_CONN_PARAMS_MODULE_ENABLE
void
btle_gapStartScanning(void)
{
ble_gap_scan_params_t scanParams = {
.active = 0, /**< If 1, perform active scanning (scan requests). */
.selective = 0, /**< If 1, ignore unknown devices (non whitelisted). */
.p_whitelist = NULL, /**< Pointer to whitelist, NULL if none is given. */
.interval = 1000, /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
.window = 500, /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
.timeout = 0, /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
};
sd_ble_gap_scan_start(&scanParams);
}

View file

@ -22,7 +22,8 @@
#include "ble.h"
#include "GapAdvertisingParams.h"
#include "GapAdvertisingData.h"
#include "public/Gap.h"
#include "Gap.h"
#include "GapScanningParams.h"
#include "nrf_soc.h"
#include "ble_radio_notification.h"
@ -47,9 +48,9 @@ public:
virtual ble_error_t getAddress(addr_type_t *typeP, address_t address);
virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
virtual uint16_t getMinAdvertisingInterval(void) const {return GAP_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);}
virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return GAP_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);}
virtual uint16_t getMaxAdvertisingInterval(void) const {return GAP_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);}
virtual uint16_t getMinAdvertisingInterval(void) const {return ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);}
virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);}
virtual uint16_t getMaxAdvertisingInterval(void) const {return ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);}
virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
virtual ble_error_t stopAdvertising(void);
@ -77,6 +78,34 @@ public:
ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, onRadioNotification);
}
virtual ble_error_t startScan(const GapScanningParams &scanningParams, AdvertisementReportCallback_t callback) {
if ((onAdvertisementReport = callback) != NULL) {
ble_gap_scan_params_t scanParams = {
.active = scanningParams.getActiveScanning(), /**< If 1, perform active scanning (scan requests). */
.selective = 0, /**< If 1, ignore unknown devices (non whitelisted). */
.p_whitelist = NULL, /**< Pointer to whitelist, NULL if none is given. */
.interval = scanningParams.getInterval(), /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
.window = scanningParams.getWindow(), /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
.timeout = scanningParams.getTimeout(), /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
};
if (sd_ble_gap_scan_start(&scanParams) != NRF_SUCCESS) {
return BLE_ERROR_PARAM_OUT_OF_RANGE;
}
}
return BLE_ERROR_NONE;
}
virtual ble_error_t stopScan(void) {
if (sd_ble_gap_scan_stop() == NRF_SUCCESS) {
return BLE_ERROR_NONE;
}
return BLE_STACK_BUSY;
}
private:
uint16_t m_connectionHandle;
nRF51Gap() {

View file

@ -271,12 +271,10 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt)
/* 1.) Handle CCCD changes */
handle_value = gattsEventP->params.write.handle;
for (uint8_t i = 0; i<characteristicCount; i++) {
for (uint8_t i = 0; i < characteristicCount; i++) {
if ((p_characteristics[i]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
(nrfCharacteristicHandles[i].cccd_handle == handle_value)) {
uint16_t cccd_value =
(gattsEventP->params.write.data[1] << 8) |
gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
uint16_t cccd_value = (gattsEventP->params.write.data[1] << 8) | gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
if (((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
((p_characteristics[i]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {