Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_API by
Revision 7:5e1f0d7f7c7d, committed 2013-12-16
- Comitter:
- ktownsend
- Date:
- Mon Dec 16 12:46:12 2013 +0000
- Parent:
- 6:425638944835
- Child:
- 8:780bfa26d0ee
- Commit message:
- More GAP cleanup (Scan Response, etc.)
Changed in this revision
--- a/GapAdvertisingData.cpp Fri Dec 13 00:41:11 2013 +0000
+++ b/GapAdvertisingData.cpp Mon Dec 16 12:46:12 2013 +0000
@@ -179,7 +179,21 @@
/**************************************************************************/
/*!
+ @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)
--- a/GapAdvertisingData.h Fri Dec 13 00:41:11 2013 +0000
+++ b/GapAdvertisingData.h Mon Dec 16 12:46:12 2013 +0000
@@ -152,6 +152,7 @@
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:
--- a/GapAdvertisingParams.cpp Fri Dec 13 00:41:11 2013 +0000
+++ b/GapAdvertisingParams.cpp Mon Dec 16 12:46:12 2013 +0000
@@ -101,3 +101,13 @@
GapAdvertisingParams::~GapAdvertisingParams(void)
{
}
+
+/**************************************************************************/
+/*!
+ @brief returns the current Advertising Type value
+*/
+/**************************************************************************/
+GapAdvertisingParams::AdvertisingType GapAdvertisingParams::getAdvertisingType(void)
+{
+ return _advType;
+}
--- a/GapAdvertisingParams.h Fri Dec 13 00:41:11 2013 +0000
+++ b/GapAdvertisingParams.h Mon Dec 16 12:46:12 2013 +0000
@@ -24,6 +24,8 @@
uint16_t interval = GAP_ADV_PARAMS_INTERVAL_MIN,
uint16_t timeout = 0);
virtual ~GapAdvertisingParams(void);
+
+ virtual AdvertisingType getAdvertisingType(void);
private:
AdvertisingType _advType;
--- a/blecommon.h Fri Dec 13 00:41:11 2013 +0000
+++ b/blecommon.h Mon Dec 16 12:46:12 2013 +0000
@@ -10,7 +10,8 @@
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 */
--- a/hw/bleradio.h Fri Dec 13 00:41:11 2013 +0000
+++ b/hw/bleradio.h Mon Dec 16 12:46:12 2013 +0000
@@ -25,7 +25,7 @@
/* 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;
--- a/hw/nrf51822.cpp Fri Dec 13 00:41:11 2013 +0000
+++ b/hw/nrf51822.cpp Mon Dec 16 12:46:12 2013 +0000
@@ -19,8 +19,6 @@
@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 @@
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 @@
/**************************************************************************/
/*!
+ @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 @@
(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 @@
(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 @@
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 @@
/**************************************************************************/
/*!
@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 @@
/*!
@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)
--- a/hw/nrf51822.h Fri Dec 13 00:41:11 2013 +0000
+++ b/hw/nrf51822.h Mon Dec 16 12:46:12 2013 +0000
@@ -13,7 +13,7 @@
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);
--- a/main.cpp Fri Dec 13 00:41:11 2013 +0000
+++ b/main.cpp Mon Dec 16 12:46:12 2013 +0000
@@ -34,6 +34,7 @@
/* 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 @@
wait(2);
radio.reset();
- radio.setAdvertising(advParams, advData);
+ radio.setAdvertising(advParams, advData, scanResponse);
radio.start();
}
