More GAP cleanup (Scan Response, etc.)

This commit is contained in:
ktownsend 2013-12-16 12:46:12 +00:00
parent 3d4940b867
commit 92b46b339a
9 changed files with 171 additions and 8 deletions

View file

@ -177,9 +177,23 @@ void GapAdvertisingData::clear(void)
_payloadLen = 0;
}
/**************************************************************************/
/*!
@brief Returns a pointer to the the current payload
@returns A pointer to the payload
*/
/**************************************************************************/
uint8_t * GapAdvertisingData::getPayload(void)
{
return _payload;
}
/**************************************************************************/
/*!
@brief Returns the current payload length (0..31 bytes)
@returns The payload length in bytes
*/
/**************************************************************************/
uint8_t GapAdvertisingData::getPayloadLen(void)

View file

@ -152,6 +152,7 @@ class GapAdvertisingData
ble_error_t addFlags(Flags flag = LE_GENERAL_DISCOVERABLE);
ble_error_t addTxPower(int8_t txPower);
void clear(void);
uint8_t * getPayload(void);
uint8_t getPayloadLen(void);
private:

View file

@ -101,3 +101,13 @@ GapAdvertisingParams::GapAdvertisingParams(AdvertisingType advType, uint16_t int
GapAdvertisingParams::~GapAdvertisingParams(void)
{
}
/**************************************************************************/
/*!
@brief returns the current Advertising Type value
*/
/**************************************************************************/
GapAdvertisingParams::AdvertisingType GapAdvertisingParams::getAdvertisingType(void)
{
return _advType;
}

View file

@ -24,6 +24,8 @@ class GapAdvertisingParams
uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN,
uint16_t timeout = 0);
virtual ~GapAdvertisingParams(void);
virtual AdvertisingType getAdvertisingType(void);
private:
AdvertisingType _advType;

View file

@ -10,7 +10,8 @@ extern "C" {
typedef enum ble_error_e
{
BLE_ERROR_NONE = 0,
BLE_ERROR_BUFFER_OVERFLOW = 1
BLE_ERROR_BUFFER_OVERFLOW = 1,
BLE_ERROR_BUFFER_INVALID_PARAM = 2
} ble_error_t;
/* https://developer.bluetooth.org/gatt/units/Pages/default.aspx */

View file

@ -25,7 +25,7 @@ class BLERadio
/* ToDo: Force constructor with event handler callback */
/* These functions must be defined in the sub-class */
virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &) = 0;
virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &, GapAdvertisingData &) = 0;
virtual ble_error_t addService(GattService &) = 0;
virtual ble_error_t readCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t) = 0;
virtual ble_error_t writeCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t) = 0;

View file

@ -19,8 +19,6 @@ void nRF51822::uartCallback(void)
@brief Constructor
*/
/**************************************************************************/
//nRF51822::nRF51822() : uart(P0_4, P0_0) /* LPC812 */
nRF51822::nRF51822() : uart(p9, p10) /* LPC1768 using apps board */
{
/* Setup the nRF UART interface */
@ -30,7 +28,6 @@ nRF51822::nRF51822() : uart(p9, p10) /* LPC1768 using apps board */
uart.attach(this, &nRF51822::uartCallback);
/* Add flow control for UART (required by the nRF51822) */
//uart.set_flow_control(Serial::RTSCTS, P0_6, P0_8); /* LPC812 */
uart.set_flow_control(Serial::RTSCTS, p30, p29); /* LPC1768 */
/* Reset the service counter */
@ -66,17 +63,99 @@ void nRF51822::test(void)
/**************************************************************************/
/*!
@brief Sets the advertising parameters and payload for the device
@param[in] params
Basic advertising details, including the advertising
delay, timeout and how the device should be advertised
@params[in] advData
The primary advertising data payload
@params[in] scanResponse
The optional Scan Response payload if the advertising
type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED
in \ref GapAdveritinngParams
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@retval BLE_ERROR_BUFFER_OVERFLOW
The proposed action would cause a buffer overflow. All
advertising payloads must be <= 31 bytes.
@section EXAMPLE
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF51822::setAdvertising(GapAdvertisingParams &, GapAdvertisingData &)
ble_error_t nRF51822::setAdvertising(GapAdvertisingParams & params, GapAdvertisingData & advData, GapAdvertisingData & scanResponse)
{
uint8_t len = 0;
uint8_t *buffer;
/* ToDo: Send advertising params, Command ID = 0x000x */
/* Send advertising data, Command ID = 0x000A */
len = advData.getPayloadLen();
buffer = advData.getPayload();
if (len > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
{
return BLE_ERROR_BUFFER_OVERFLOW;
}
uart.printf("10 0A 00 %02X ", len);
for (uint16_t i = 0; i < len; i++)
{
uart.printf(" %02X", buffer[i]);
}
uart.printf("\r\n");
/* ToDo: Check response */
wait(0.1);
/* Send scan response data, Command ID = 0x000x */
if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED))
{
len = advData.getPayloadLen();
buffer = advData.getPayload();
if (len > GAP_ADVERTISING_DATA_MAX_PAYLOAD)
{
return BLE_ERROR_BUFFER_OVERFLOW;
}
uart.printf("10 0A 00 %02X ", len);
for (uint16_t i = 0; i < len; i++)
{
uart.printf(" %02X", buffer[i]);
}
uart.printf("\r\n");
/* ToDo: Check response */
wait(0.1);
}
return BLE_ERROR_NONE;
}
/**************************************************************************/
/*!
@brief Adds a new service to the GATT table on the peripheral
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@section EXAMPLE
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF51822::addService(GattService & service)
@ -156,6 +235,17 @@ ble_error_t nRF51822::addService(GattService & service)
(raw byte array in LSB format)
@param[in] len
The number of bytes read into the buffer
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@section EXAMPLE
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF51822::readCharacteristic(GattService &service, GattCharacteristic &characteristic, uint8_t buffer[], uint16_t len)
@ -179,6 +269,17 @@ ble_error_t nRF51822::readCharacteristic(GattService &service, GattCharacteristi
(raw byte array in LSB format)
@param[in] len
The number of bytes in buffer
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@section EXAMPLE
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF51822::writeCharacteristic(GattService &service, GattCharacteristic &characteristic, uint8_t buffer[], uint16_t len)
@ -203,6 +304,17 @@ ble_error_t nRF51822::writeCharacteristic(GattService &service, GattCharacterist
added before this function was called.
@note All services must be added before calling this function!
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@section EXAMPLE
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF51822::start(void)
@ -219,6 +331,17 @@ ble_error_t nRF51822::start(void)
/**************************************************************************/
/*!
@brief Stops the BLE HW and disconnects from any devices
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@section EXAMPLE
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF51822::stop(void)
@ -236,6 +359,17 @@ ble_error_t nRF51822::stop(void)
/*!
@brief Resets the BLE HW, removing any existing services and
characteristics
@returns ble_error_t
@retval BLE_ERROR_NONE
Everything executed properly
@section EXAMPLE
@code
@endcode
*/
/**************************************************************************/
ble_error_t nRF51822::reset(void)

View file

@ -13,7 +13,7 @@ class nRF51822 : public BLERadio
virtual ~nRF51822(void);
/* Functions that mus be implemented from NRFRadio */
virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &);
virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &, GapAdvertisingData &);
virtual ble_error_t addService(GattService &);
virtual ble_error_t readCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t);
virtual ble_error_t writeCharacteristic(GattService &, GattCharacteristic &, uint8_t[], uint16_t);

View file

@ -34,6 +34,7 @@ GattCharacteristic thermInterval ( 0x2A21, 2, 2, BLE_GATT_CHAR_PROPERTIES_READ )
/* GAP Advertising Example (iBeacon) */
GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED );
GapAdvertisingData advData;
GapAdvertisingData scanResponse;
uint8_t iBeaconPayload[25] = { 0x4C, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, 0x00, 0x00, 0x00, 0x00, 0xC8 };
@ -45,7 +46,7 @@ void startBeacon(void)
wait(2);
radio.reset();
radio.setAdvertising(advParams, advData);
radio.setAdvertising(advParams, advData, scanResponse);
radio.start();
}