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 257:6be2b4b0cd71, committed 2015-01-12
- Comitter:
- schilit
- Date:
- Mon Jan 12 14:49:53 2015 -0800
- Parent:
- 229:e14bc27b224f
- Parent:
- 256:fb2a891a0d98
- Commit message:
- Merged the BLE_API from upstream
Changed in this revision
| services/URIBeaconConfigService.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/README.md Tue Dec 02 02:51:52 2014 +0000 +++ b/README.md Mon Jan 12 14:49:53 2015 -0800 @@ -1,5 +1,5 @@ # mbed Bluetooth Low Energy Stack -This is the github repo for the BLE_API used by developer.mbed.org . The BLE stack is under development and constantly evolving. For up to date documentation please see [the mbed BLE Documentation page](http://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/docs/tip/). +This is the github repo for the BLE_API used by developer.mbed.org. Please see [mbed BLE Homepage](developer.mbed.org/teams/Bluetooth-Low-Energy/) for all documentation, code examples and general help. # Supported Services Supported GATT services and constantly being added and can be found in the /services folder. @@ -10,9 +10,12 @@ * Health Thermometer * Heart Rate * UART +* UriBeacon +* iBeacon # Getting Started The mbed BLE API is meant to be used in projects on developer.mbed.org. Please see examples and sample project files there. A good starting point are these pages: -* [mbed BLE API](developer.mbed.org/teams/Bluetooth-Low-Energy/) -* [mbed BLE Getting Started Guide](http://developer.mbed.org/forum/team-63-Bluetooth-Low-Energy-community/topic/5262/) \ No newline at end of file +* [mbed BLE Homepage](developer.mbed.org/teams/Bluetooth-Low-Energy/) for all things BLE +* [mbed BLE Getting Started Guide](http://developer.mbed.org/forum/team-63-Bluetooth-Low-Energy-community/topic/5262/) a wonderful primer on using BLE with mbed +* [mbed BLE API page](http://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/docs/tip/) for the API in generated by doxygen \ No newline at end of file
--- a/common/BLEDeviceInstanceBase.h Tue Dec 02 02:51:52 2014 +0000
+++ b/common/BLEDeviceInstanceBase.h Mon Jan 12 14:49:53 2015 -0800
@@ -28,6 +28,7 @@
virtual Gap& getGap() = 0;
virtual GattServer& getGattServer() = 0;
virtual ble_error_t init(void) = 0;
+ virtual ble_error_t shutdown(void) = 0;
virtual ble_error_t reset(void) = 0;
virtual ble_error_t setTxPower(int8_t txPower) = 0;
virtual void waitForEvent(void) = 0;
@@ -43,4 +44,4 @@
*/
extern BLEDeviceInstanceBase *createBLEDeviceInstance(void);
-#endif // ifndef __BLE_DEVICE_INSTANCE_BASE__
+#endif // ifndef __BLE_DEVICE_INSTANCE_BASE__
\ No newline at end of file
--- a/public/BLEDevice.h Tue Dec 02 02:51:52 2014 +0000
+++ b/public/BLEDevice.h Mon Jan 12 14:49:53 2015 -0800
@@ -36,6 +36,11 @@
ble_error_t init();
ble_error_t reset(void);
+ /**
+ * Purge the BLE stack of GATT and GAP state. init() must be called afterwards to re-instate services and GAP state.
+ */
+ ble_error_t shutdown(void);
+
/* GAP specific APIs */
public:
/**
@@ -211,6 +216,14 @@
void onDisconnection(Gap::DisconnectionEventCallback_t disconnectionCallback);
/**
+ * Append to a chain of callbacks to be invoked upon disconnection; these
+ * callbacks receive no context and are therefore different from the
+ * onDisconnection callback.
+ */
+ template<typename T>
+ void addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void));
+
+ /**
* Setup a callback for the GATT event DATA_SENT.
*/
void onDataSent(GattServer::ServerEventCallbackWithCount_t callback);
@@ -365,6 +378,13 @@
}
inline ble_error_t
+BLEDevice::shutdown(void)
+{
+ clearAdvertisingPayload();
+ return transport->shutdown();
+}
+
+inline ble_error_t
BLEDevice::setAddress(Gap::addr_type_t type, const Gap::address_t address)
{
return transport->getGap().setAddress(type, address);
@@ -498,6 +518,12 @@
transport->getGap().setOnDisconnection(disconnectionCallback);
}
+template<typename T>
+inline void
+BLEDevice::addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {
+ transport->getGap().addToDisconnectionCallChain(tptr, mptr);
+}
+
inline void
BLEDevice::onDataSent(GattServer::ServerEventCallbackWithCount_t callback)
{
--- a/public/Gap.h Tue Dec 02 02:51:52 2014 +0000
+++ b/public/Gap.h Mon Jan 12 14:49:53 2015 -0800
@@ -20,6 +20,9 @@
#include "GapAdvertisingData.h"
#include "GapAdvertisingParams.h"
#include "GapEvents.h"
+#include "CallChain.h"
+
+using namespace mbed;
class Gap {
public:
@@ -96,14 +99,36 @@
/* Event callback handlers */
void setOnTimeout(EventCallback_t callback) {onTimeout = callback;}
void setOnConnection(ConnectionEventCallback_t callback) {onConnection = callback;}
+
+ /**
+ * Set the application callback for disconnection events.
+ * @param callback
+ * Pointer to the unique callback.
+ */
void setOnDisconnection(DisconnectionEventCallback_t callback) {onDisconnection = callback;}
+ /**
+ * Append to a chain of callbacks to be invoked upon disconnection; these
+ * callbacks receive no context and are therefore different from the
+ * onDisconnection callback.
+ * @param callback
+ * function pointer to be invoked upon disconnection; receives no context.
+ *
+ * @note the disconnection CallChain should have been merged with
+ * onDisconnctionCallback; but this was not possible because
+ * FunctionPointer (which is a building block for CallChain) doesn't
+ * accept variadic templates.
+ */
+ template<typename T>
+ void addToDisconnectionCallChain(T *tptr, void (T::*mptr)(void)) {disconnectionCallChain.add(tptr, mptr);}
+
GapState_t getState(void) const {
return state;
}
protected:
- Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL) {
+ /* Default constructor. */
+ Gap() : state(), onTimeout(NULL), onConnection(NULL), onDisconnection(NULL), disconnectionCallChain() {
/* empty */
}
@@ -120,6 +145,7 @@
if (onDisconnection) {
onDisconnection(handle, reason);
}
+ disconnectionCallChain.call();
}
void processEvent(GapEvents::gapEvent_e type) {
@@ -140,6 +166,7 @@
EventCallback_t onTimeout;
ConnectionEventCallback_t onConnection;
DisconnectionEventCallback_t onDisconnection;
+ CallChain disconnectionCallChain;
private:
/* disallow copy and assignment */
--- a/public/GattCharacteristic.h Tue Dec 02 02:51:52 2014 +0000
+++ b/public/GattCharacteristic.h Mon Jan 12 14:49:53 2015 -0800
@@ -18,6 +18,8 @@
#define __GATT_CHARACTERISTIC_H__
#include "GattAttribute.h"
+#include "GattCharacteristicWriteCBParams.h"
+#include "FunctionPointerWithContext.h"
class GattCharacteristic {
public:
@@ -289,44 +291,120 @@
} presentation_format_t;
/**
- * @brief Creates a new GattCharacteristic using the specified 16-bit
- * UUID, value length, and properties
- *
- * @note The UUID value must be unique in the service and is normally >1
- *
- * @param[in] uuid
- * The UUID to use for this characteristic
- * @param[in] valuePtr
- * The memory holding the initial value. The value is copied
- * into the stack when the enclosing service is added; and
- * thereafter maintained internally by the stack.
- * @param[in] initialLen
- * The min length in bytes of this characteristic's value
- * @param[in] maxLen
- * The max length in bytes of this characteristic's value
- * @param[in] props
- * The 8-bit bit field containing the characteristic's properties
- * @param[in] descriptors
- * A pointer to an array of descriptors to be included within this characteristic
- * @param[in] numDescriptors
- * The number of descriptors
- *
- * @NOTE: If valuePtr == NULL, initialLength == 0, and properties == READ
- * for the value attribute of a characteristic, then that particular
- * characteristic may be considered optional and dropped while
- * instantiating the service with the underlying BLE stack.
- */
- /**************************************************************************/
- GattCharacteristic(const UUID &uuid, uint8_t *valuePtr = NULL, uint16_t initialLen = 0, uint16_t maxLen = 0,
- uint8_t props = BLE_GATT_CHAR_PROPERTIES_NONE,
- GattAttribute *descriptors[] = NULL, unsigned numDescriptors = 0) :
- _valueAttribute(uuid, valuePtr, initialLen, maxLen), _properties(props), _descriptors(descriptors), _descriptorCount(numDescriptors) {
+ * @brief Creates a new GattCharacteristic using the specified 16-bit
+ * UUID, value length, and properties
+ *
+ * @note The UUID value must be unique in the service and is normally >1
+ *
+ * @param[in] uuid
+ * The UUID to use for this characteristic
+ * @param[in] valuePtr
+ * The memory holding the initial value. The value is copied
+ * into the stack when the enclosing service is added; and
+ * thereafter maintained internally by the stack.
+ * @param[in] initialLen
+ * The min length in bytes of this characteristic's value
+ * @param[in] maxLen
+ * The max length in bytes of this characteristic's value
+ * @param[in] props
+ * The 8-bit bit field containing the characteristic's properties
+ * @param[in] descriptors
+ * A pointer to an array of descriptors to be included within this characteristic
+ * @param[in] numDescriptors
+ * The number of descriptors
+ * @param[in] writeAuthorizationIn
+ * Do the attribute(s) of this characteristic have write
+ * authorization enabled? if so, Write Authorization will be
+ * requested from the application on every write request
+ * operation (but not write command).
+ *
+ * @NOTE: If valuePtr == NULL, initialLength == 0, and properties == READ
+ * for the value attribute of a characteristic, then that particular
+ * characteristic may be considered optional and dropped while
+ * instantiating the service with the underlying BLE stack.
+ */
+ GattCharacteristic(const UUID &uuid,
+ uint8_t *valuePtr = NULL,
+ uint16_t initialLen = 0,
+ uint16_t maxLen = 0,
+ uint8_t props = BLE_GATT_CHAR_PROPERTIES_NONE,
+ GattAttribute *descriptors[] = NULL,
+ unsigned numDescriptors = 0) :
+ _valueAttribute(uuid, valuePtr, initialLen, maxLen),
+ _properties(props),
+ _descriptors(descriptors),
+ _descriptorCount(numDescriptors),
+ enabledReadAuthorization(false),
+ enabledWriteAuthorization(false),
+ readAuthorizationCallback(),
+ writeAuthorizationCallback() {
+ /* empty */
}
+ /**
+ * Authorization.
+ */
public:
- GattAttribute& getValueAttribute() {return _valueAttribute; }
- uint8_t getProperties(void) const {return _properties; }
- uint8_t getDescriptorCount(void) const {return _descriptorCount;}
+ void setWriteAuthorizationCallback(void (*callback)(GattCharacteristicWriteAuthCBParams *)) {
+ writeAuthorizationCallback.attach(callback);
+ enabledWriteAuthorization = true;
+ }
+ template <typename T>
+ void setWriteAuthorizationCallback(T *object, void (T::*member)(GattCharacteristicWriteAuthCBParams *)) {
+ writeAuthorizationCallback.attach(object, member);
+ enabledWriteAuthorization = true;
+ }
+ void setReadAuthorizationCallback(void (*callback)(GattCharacteristicReadAuthCBParams *)) {
+ readAuthorizationCallback.attach(callback);
+ enabledReadAuthorization = true;
+ }
+ template <typename T>
+ void setReadAuthorizationCallback(T *object, void (T::*member)(GattCharacteristicReadAuthCBParams *)) {
+ readAuthorizationCallback.attach(object, member);
+ enabledReadAuthorization = true;
+ }
+
+ /**
+ * Helper function meant to be called from the guts of the BLE stack to
+ * determine the authorization reply for a write request.
+ * @param params to capture the context of the write-auth request; and also contains an out-parameter for reply.
+ * @return true if the write is authorized to proceed.
+ */
+ bool authorizeWrite(GattCharacteristicWriteAuthCBParams *params) {
+ if (!isWriteAuthorizationEnabled()) {
+ return true;
+ }
+
+ params->authorizationReply = true; /* initialized to true by default */
+ writeAuthorizationCallback.call(params);
+ return params->authorizationReply;
+ }
+
+ /**
+ * Helper function meant to be called from the guts of the BLE stack to
+ * determine the authorization reply for a read request.
+ * @param params to capture the context of the read-auth request; and also contains an out-parameter for reply.
+ * @return true if the read is authorized to proceed.
+ */
+ bool authorizeRead(GattCharacteristicReadAuthCBParams *params) {
+ if (!isReadAuthorizationEnabled()) {
+ return true;
+ }
+
+ params->authorizationReply = true; /* initialized to true by default */
+ readAuthorizationCallback.call(params);
+ return params->authorizationReply;
+ }
+
+ /* accessors */
+public:
+ GattAttribute& getValueAttribute() {return _valueAttribute; }
+ const GattAttribute& getValueAttribute() const {return _valueAttribute; }
+ GattAttribute::Handle_t getValueHandle(void) const {return getValueAttribute().getHandle();}
+ uint8_t getProperties(void) const {return _properties; }
+ uint8_t getDescriptorCount(void) const {return _descriptorCount; }
+ bool isReadAuthorizationEnabled() const {return enabledReadAuthorization; }
+ bool isWriteAuthorizationEnabled() const {return enabledWriteAuthorization; }
GattAttribute *getDescriptor(uint8_t index) {
if (index >= _descriptorCount) {
@@ -342,6 +420,11 @@
GattAttribute **_descriptors;
uint8_t _descriptorCount;
+ bool enabledReadAuthorization;
+ bool enabledWriteAuthorization;
+ FunctionPointerWithContext<GattCharacteristicReadAuthCBParams *> readAuthorizationCallback;
+ FunctionPointerWithContext<GattCharacteristicWriteAuthCBParams *> writeAuthorizationCallback;
+
private:
/* disallow copy and assignment */
GattCharacteristic(const GattCharacteristic &);
--- a/public/GattCharacteristicWriteCBParams.h Tue Dec 02 02:51:52 2014 +0000
+++ b/public/GattCharacteristicWriteCBParams.h Mon Jan 12 14:49:53 2015 -0800
@@ -33,4 +33,20 @@
const uint8_t *data; /**< Incoming data, variable length. */
};
-#endif /*__GATT_CHARACTERISTIC_WRITE_CB_PARAMS_H__*/
+struct GattCharacteristicWriteAuthCBParams {
+ GattAttribute::Handle_t charHandle;
+ uint16_t offset; /**< Offset for the write operation. */
+ uint16_t len; /**< Length of the incoming data. */
+ const uint8_t *data; /**< Incoming data, variable length. */
+ bool authorizationReply; /* This is the out parameter which needs to be set to true by the callback if the
+ * request is to proceed; false otherwise. */
+};
+
+struct GattCharacteristicReadAuthCBParams {
+ GattAttribute::Handle_t charHandle;
+ uint16_t offset; /**< Offset for the write operation. */
+ bool authorizationReply; /* This is the out parameter which needs to be set to true by the callback if the
+ * request is to proceed; false otherwise. */
+};
+
+#endif /*__GATT_CHARACTERISTIC_WRITE_CB_PARAMS_H__*/
\ No newline at end of file
--- a/public/GattServerEvents.h Tue Dec 02 02:51:52 2014 +0000
+++ b/public/GattServerEvents.h Mon Jan 12 14:49:53 2015 -0800
@@ -17,31 +17,22 @@
#ifndef __GATT_SERVER_EVENTS_H__
#define __GATT_SERVER_EVENTS_H__
-#include "blecommon.h"
-
-/**************************************************************************/
/*!
\brief
The base class used to abstract away the callback events that can be
triggered with the GATT Server.
*/
-/**************************************************************************/
class GattServerEvents
{
public:
- /******************************************************************/
- /*!
- \brief
- Identifies GATT events generated by the radio HW when an event
- callback occurs
- */
- /******************************************************************/
typedef enum gattEvent_e {
- GATT_EVENT_DATA_SENT = 1, /**< Fired when a msg was successfully sent out (notify only?) */
- GATT_EVENT_DATA_WRITTEN = 2, /**< Client wrote data to Server (separate into char and descriptor writes?) */
- GATT_EVENT_UPDATES_ENABLED = 3, /**< Notify/Indicate Enabled in CCCD */
- GATT_EVENT_UPDATES_DISABLED = 4, /**< Notify/Indicate Disabled in CCCD */
- GATT_EVENT_CONFIRMATION_RECEIVED = 5 /**< Response received from Indicate message */
+ GATT_EVENT_DATA_SENT = 1, /**< Fired when a msg was successfully sent out (notify only?) */
+ GATT_EVENT_DATA_WRITTEN = 2, /**< Client wrote data to Server (separate into char and descriptor writes?) */
+ GATT_EVENT_UPDATES_ENABLED = 3, /**< Notify/Indicate Enabled in CCCD */
+ GATT_EVENT_UPDATES_DISABLED = 4, /**< Notify/Indicate Disabled in CCCD */
+ GATT_EVENT_CONFIRMATION_RECEIVED = 5, /**< Response received from Indicate message */
+ GATT_EVENT_READ_AUTHORIZATION_REQ = 6, /**< Request application to authorize read */
+ GATT_EVENT_WRITE_AUTHORIZATION_REQ = 7, /**< Request application to authorize write */
} gattEvent_t;
};
--- a/services/BatteryService.h Tue Dec 02 02:51:52 2014 +0000
+++ b/services/BatteryService.h Mon Jan 12 14:49:53 2015 -0800
@@ -19,11 +19,20 @@
#include "BLEDevice.h"
-/* Battery Service */
-/* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml */
-/* Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml */
+/**
+* @class BatteryService
+* @brief BLE Battery Service. This service displays the battery level from 0%->100% represented as a 8bit number.<br>
+* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml <br>
+* Battery Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.battery_level.xml
+*/
class BatteryService {
public:
+ /**
+ * @param[ref] _ble
+ * BLEDevice object for the underlying controller.
+ * @param[in] level
+ * 8bit batterly level. Usually used to represent percentage of batterly charge remaining.
+ */
BatteryService(BLEDevice &_ble, uint8_t level = 100) :
ble(_ble),
batteryLevel(level),
@@ -43,9 +52,11 @@
}
/**
- * Update the battery level with a new value. Valid values range from
+ * @brief Update the battery level with a new value. Valid values range from
* 0..100. Anything outside this range will be ignored.
- * @param newLevel New level.
+ *
+ * @param newLevel
+ * update to battery level.
*/
void updateBatteryLevel(uint8_t newLevel) {
batteryLevel = newLevel;
@@ -53,9 +64,9 @@
}
private:
- BLEDevice &ble;
- uint8_t batteryLevel;
- GattCharacteristic batteryLevelCharacteristic;
+ BLEDevice &ble;
+ uint8_t batteryLevel;
+ GattCharacteristic batteryLevelCharacteristic;
};
-#endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/
+#endif /* #ifndef __BLE_BATTERY_SERVICE_H__*/
\ No newline at end of file
--- a/services/DFUService.cpp Tue Dec 02 02:51:52 2014 +0000
+++ b/services/DFUService.cpp Mon Jan 12 14:49:53 2015 -0800
@@ -16,25 +16,25 @@
#include "DFUService.h"
-const uint8_t DFUServiceBaseUUID[] = {
+const uint8_t DFUServiceBaseUUID[] = {
0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0xEF, 0xDE,
0x15, 0x23, 0x78, 0x5F, 0xEA, 0xBC, 0xD1, 0x23,
};
-const uint16_t DFUServiceShortUUID = 0x1530;
-const uint16_t DFUServiceControlCharacteristicShortUUID = 0x1531;
-const uint16_t DFUServicePacketCharacteristicShortUUID = 0x1532;
+const uint16_t DFUServiceShortUUID = 0x1530;
+const uint16_t DFUServiceControlCharacteristicShortUUID = 0x1531;
+const uint16_t DFUServicePacketCharacteristicShortUUID = 0x1532;
-const uint8_t DFUServiceUUID[] = {
+const uint8_t DFUServiceUUID[] = {
0x00, 0x00, (uint8_t)(DFUServiceShortUUID >> 8), (uint8_t)(DFUServiceShortUUID & 0xFF), 0x12, 0x12, 0xEF, 0xDE,
0x15, 0x23, 0x78, 0x5F, 0xEA, 0xBC, 0xD1, 0x23,
};
-const uint8_t DFUServiceControlCharacteristicUUID[] = {
+const uint8_t DFUServiceControlCharacteristicUUID[] = {
0x00, 0x00, (uint8_t)(DFUServiceControlCharacteristicShortUUID >> 8), (uint8_t)(DFUServiceControlCharacteristicShortUUID & 0xFF), 0x12, 0x12, 0xEF, 0xDE,
0x15, 0x23, 0x78, 0x5F, 0xEA, 0xBC, 0xD1, 0x23,
};
-const uint8_t DFUServicePacketCharacteristicUUID[] = {
+const uint8_t DFUServicePacketCharacteristicUUID[] = {
0x00, 0x00, (uint8_t)(DFUServicePacketCharacteristicShortUUID >> 8), (uint8_t)(DFUServicePacketCharacteristicShortUUID & 0xFF), 0x12, 0x12, 0xEF, 0xDE,
0x15, 0x23, 0x78, 0x5F, 0xEA, 0xBC, 0xD1, 0x23,
};
-DFUService::ResetPrepare_t DFUService::handoverCallback = NULL;
+DFUService::ResetPrepare_t DFUService::handoverCallback = NULL;
\ No newline at end of file
--- a/services/DFUService.h Tue Dec 02 02:51:52 2014 +0000
+++ b/services/DFUService.h Mon Jan 12 14:49:53 2015 -0800
@@ -30,16 +30,28 @@
extern const uint8_t DFUServiceControlCharacteristicUUID[];
extern const uint8_t DFUServicePacketCharacteristicUUID[];
+/**
+* @class DFUService
+* @brief Device Firmware Update Service.
+*/
class DFUService {
public:
/**
- * Signature for the handover callback. The application may provide such a
+ * @brief Signature for the handover callback. The application may provide such a
* callback when setting up the DFU service, in which case it will be
* invoked before handing control over to the bootloader.
*/
typedef void (*ResetPrepare_t)(void);
public:
+ /**
+ * @brief Adds Device Firmware Update service to an existing ble object.
+ *
+ * @param[ref] _ble
+ * BLEDevice object for the underlying controller.
+ * @param[in] _handoverCallback
+ * Application specific handover callback.
+ */
DFUService(BLEDevice &_ble, ResetPrepare_t _handoverCallback = NULL) :
ble(_ble),
controlBytes(),
@@ -63,22 +75,28 @@
ble.addService(dfuService);
handoverCallback = _handoverCallback;
- serviceAdded = true;
+ serviceAdded = true;
ble.onDataWritten(this, &DFUService::onDataWritten);
}
- uint16_t getControlHandle(void) {
- return controlPoint.getValueAttribute().getHandle();
+ /**
+ * @brief get the handle for the value attribute of the control characteristic.
+ */
+ uint16_t getControlHandle(void) const {
+ return controlPoint.getValueHandle();
}
/**
- * This callback allows the DFU service to receive the initial trigger to
+ * @brief This callback allows the DFU service to receive the initial trigger to
* handover control to the bootloader; but first the application is given a
* chance to clean up.
+ *
+ * @param[in] params
+ * Information about the characterisitc being updated.
*/
virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
- if (params->charHandle == controlPoint.getValueAttribute().getHandle()) {
+ if (params->charHandle == controlPoint.getValueHandle()) {
/* At present, writing anything will do the trick--this needs to be improved. */
if (handoverCallback) {
handoverCallback();
@@ -112,4 +130,4 @@
GattCharacteristic packet;
};
-#endif /* #ifndef __BLE_DFU_SERVICE_H__*/
+#endif /* #ifndef __BLE_DFU_SERVICE_H__*/
\ No newline at end of file
--- a/services/DeviceInformationService.h Tue Dec 02 02:51:52 2014 +0000
+++ b/services/DeviceInformationService.h Mon Jan 12 14:49:53 2015 -0800
@@ -19,16 +19,19 @@
#include "BLEDevice.h"
-/* Device Information Service */
-/* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml */
-/* Manufacturer Name String Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.manufacturer_name_string.xml */
+/**
+* @class DeviceInformationService
+* @brief BLE Device Information Service <br>
+* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml <br>
+* Manufacturer Name String Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.manufacturer_name_string.xml
+*/
class DeviceInformationService {
public:
/**
- * Constructor.
+ * @brief Device Information Service Constructor.
*
- * @param[in] _ble
- * Reference to the BLEDevice.
+ * @param[ref] _ble
+ * BLEDevice object for the underlying controller.
* @param[in] manufacturersName
* This characteristic represents the name of the
* manufacturer of the device. The name is copied into the
@@ -92,7 +95,7 @@
(softwareRevision != NULL) ? strlen(softwareRevision) : 0, /* minLength */
(softwareRevision != NULL) ? strlen(softwareRevision) : 0, /* maxLength */
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)
- {
+ {
static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
if (serviceAdded) {
return;
@@ -121,4 +124,4 @@
GattCharacteristic softwareRevisionStringCharacteristic;
};
-#endif /* #ifndef __BLE_DEVICE_INFORMATION_SERVICE_H__*/
+#endif /* #ifndef __BLE_DEVICE_INFORMATION_SERVICE_H__*/
\ No newline at end of file
--- a/services/HealthThermometerService.h Tue Dec 02 02:51:52 2014 +0000
+++ b/services/HealthThermometerService.h Mon Jan 12 14:49:53 2015 -0800
@@ -19,28 +19,35 @@
#include "BLEDevice.h"
-/* Health Thermometer Service */
-/* Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml */
-/* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
-/* Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml */
+/**
+* @class HealthThermometerService
+* @brief BLE Health Thermometer Service. This service provides the location of the thermometer and the temperature. <br>
+* Service: https://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.health_thermometer.xml <br>
+* Temperature Measurement: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml <br>
+* Temperature Type: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_type.xml
+*/
class HealthThermometerService {
public:
- enum {
- LOCATION_ARMPIT = 1,
- LOCATION_BODY,
- LOCATION_EAR,
- LOCATION_FINGER,
- LOCATION_GI_TRACT,
- LOCATION_MOUTH,
- LOCATION_RECTUM,
- LOCATION_TOE,
- LOCATION_EAR_DRUM,
+ /**
+ * @enum Sensor Location
+ * @brief Location of sensor on the body
+ */
+ enum SensorLocation_t {
+ LOCATION_ARMPIT = 1, /*!< armpit */
+ LOCATION_BODY, /*!< body */
+ LOCATION_EAR, /*!< ear */
+ LOCATION_FINGER, /*!< finger */
+ LOCATION_GI_TRACT, /*!< GI tract */
+ LOCATION_MOUTH, /*!< mouth */
+ LOCATION_RECTUM, /*!< rectum */
+ LOCATION_TOE, /*!< toe */
+ LOCATION_EAR_DRUM, /*!< ear drum */
};
public:
-
/**
- * @param[in] _ble reference to the BLE device
+ * @brief Add the Health Thermometer Service to an existing ble object, initialize with temperature and location.
+ * @param[ref] _ble reference to the BLE device
* @param[in] initialTemp initial value in celsius
* @param[in] _location
*/
@@ -59,6 +66,13 @@
ble.addService(hrmService);
}
+ /**
+ * @brief Update the temperature being broadcast
+ *
+ * @param[in] temperature
+ * Floating point value of the temperature
+ *
+ */
void updateTemperature(float temperature) {
if (ble.getGapState().connected) {
valueBytes.updateTemperature(temperature);
@@ -66,6 +80,15 @@
}
}
+ /**
+ * @brief Update the location.
+ * @param loc
+ * new location value.
+ */
+ void updateLocation(SensorLocation_t loc) {
+ ble.updateCharacteristicValue(tempLocation.getValueHandle(), reinterpret_cast<uint8_t *>(&loc), sizeof(uint8_t));
+ }
+
private:
/* Private internal representation for the bytes used to work with the vaulue of the heart-rate characteristic. */
struct TemperatureValueBytes {
@@ -77,8 +100,8 @@
static const unsigned TIMESTAMP_FLAG_POS = 1;
static const unsigned TEMPERATURE_TYPE_FLAG_POS = 2;
- static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0;
- static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1;
+ static const uint8_t TEMPERATURE_UNITS_CELSIUS = 0;
+ static const uint8_t TEMPERATURE_UNITS_FAHRENHEIT = 1;
TemperatureValueBytes(float initialTemperature) : bytes() {
/* assumption: temperature values are expressed in Celsius */
@@ -93,7 +116,7 @@
memcpy(&bytes[OFFSET_OF_VALUE], &temp_ieee11073, sizeof(float));
}
- uint8_t *getPointer(void) {
+ uint8_t *getPointer(void) {
return bytes;
}
@@ -101,7 +124,7 @@
return bytes;
}
- private:
+private:
/**
* @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
* @param temperature The temperature as a float.
@@ -114,8 +137,7 @@
return (((uint32_t)exponent) << 24) | mantissa;
}
-
- private:
+private:
/* First byte = 8-bit flags, Second field is a float holding the temperature value. */
/* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.temperature_measurement.xml */
uint8_t bytes[SIZEOF_VALUE_BYTES];
@@ -128,4 +150,4 @@
GattCharacteristic tempLocation;
};
-#endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/
+#endif /* #ifndef __BLE_HEALTH_THERMOMETER_SERVICE_H__*/
\ No newline at end of file
--- a/services/HeartRateService.h Tue Dec 02 02:51:52 2014 +0000
+++ b/services/HeartRateService.h Mon Jan 12 14:49:53 2015 -0800
@@ -19,31 +19,38 @@
#include "BLEDevice.h"
-/* Heart Rate Service */
-/* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml */
-/* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
-/* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml */
+/**
+* @class HeartRateService
+* @brief BLE Service for HeartRate. This BLE Service contains the location of the sensor, the heartrate in beats per minute. <br>
+* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml <br>
+* HRM Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml <br>
+* Location: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.body_sensor_location.xml
+*/
class HeartRateService {
public:
+ /**
+ * @enum SensorLocation
+ * @brief Location of HeartRate sensor on body.
+ */
enum {
- LOCATION_OTHER = 0,
- LOCATION_CHEST,
- LOCATION_WRIST,
- LOCATION_FINGER,
- LOCATION_HAND,
- LOCATION_EAR_LOBE,
- LOCATION_FOOT,
+ LOCATION_OTHER = 0, /*!< Other Location */
+ LOCATION_CHEST, /*!< Chest */
+ LOCATION_WRIST, /*!< Wrist */
+ LOCATION_FINGER, /*!< Finger */
+ LOCATION_HAND, /*!< Hand */
+ LOCATION_EAR_LOBE, /*!< Earlobe */
+ LOCATION_FOOT, /*!< Foot */
};
public:
/**
- * Constructor.
+ * @brief Constructor with 8bit HRM Counter value.
*
- * param[in] _ble
+ * @param[ref] _ble
* Reference to the underlying BLEDevice.
- * param[in] hrmCounter (8-bit)
+ * @param[in] hrmCounter (8-bit)
* initial value for the hrm counter.
- * param[in] location
+ * @param[in] location
* Sensor's location.
*/
HeartRateService(BLEDevice &_ble, uint8_t hrmCounter, uint8_t location) :
@@ -60,7 +67,14 @@
}
/**
- * Same constructor as above, but with a 16-bit HRM Counter value.
+ * @brief Constructor with a 16-bit HRM Counter value.
+ *
+ * @param[in] _ble
+ * Reference to the underlying BLEDevice.
+ * @param[in] hrmCounter (8-bit)
+ * initial value for the hrm counter.
+ * @param[in] location
+ * Sensor's location.
*/
HeartRateService(BLEDevice &_ble, uint16_t hrmCounter, uint8_t location) :
ble(_ble),
@@ -76,7 +90,10 @@
}
/**
- * Set a new 8-bit value for heart rate.
+ * @brief Set a new 8-bit value for heart rate.
+ *
+ * @param[in] hrmCounter
+ * HeartRate in bpm.
*/
void updateHeartRate(uint8_t hrmCounter) {
valueBytes.updateHeartRate(hrmCounter);
@@ -85,6 +102,9 @@
/**
* Set a new 16-bit value for heart rate.
+ *
+ * @param[in] hrmCounter
+ * HeartRate in bpm.
*/
void updateHeartRate(uint16_t hrmCounter) {
valueBytes.updateHeartRate(hrmCounter);
@@ -94,6 +114,9 @@
/**
* This callback allows the HeartRateService to receive updates to the
* controlPoint Characteristic.
+ *
+ * @param[in] params
+ * Information about the characterisitc being updated.
*/
virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
if (params->charHandle == controlPoint.getValueAttribute().getHandle()) {
@@ -140,17 +163,17 @@
}
void updateHeartRate(uint8_t hrmCounter) {
- valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG;
+ valueBytes[FLAGS_BYTE_INDEX] &= ~VALUE_FORMAT_FLAG;
valueBytes[FLAGS_BYTE_INDEX + 1] = hrmCounter;
}
void updateHeartRate(uint16_t hrmCounter) {
- valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
+ valueBytes[FLAGS_BYTE_INDEX] |= VALUE_FORMAT_FLAG;
valueBytes[FLAGS_BYTE_INDEX + 1] = (uint8_t)(hrmCounter & 0xFF);
valueBytes[FLAGS_BYTE_INDEX + 2] = (uint8_t)(hrmCounter >> 8);
}
- uint8_t *getPointer(void) {
+ uint8_t *getPointer(void) {
return valueBytes;
}
@@ -158,11 +181,11 @@
return valueBytes;
}
- unsigned getNumValueBytes(void) const {
+ unsigned getNumValueBytes(void) const {
return 1 + ((valueBytes[FLAGS_BYTE_INDEX] & VALUE_FORMAT_FLAG) ? sizeof(uint16_t) : sizeof(uint8_t));
}
- private:
+private:
/* First byte = 8-bit values, no extra info, Second byte = uint8_t HRM value */
/* See --> https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml */
uint8_t valueBytes[MAX_VALUE_BYTES];
@@ -177,4 +200,4 @@
GattCharacteristic controlPoint;
};
-#endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/
+#endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/services/LinkLossService.h Mon Jan 12 14:49:53 2015 -0800
@@ -0,0 +1,103 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __BLE_LINK_LOSS_SERVICE_H__
+#define __BLE_LINK_LOSS_SERVICE_H__
+
+#include "Gap.h"
+
+/**
+* @class LinkLossService
+* @brief This service defines behavior when a link is lost between two devices. <br>
+* Service: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.link_loss.xml <br>
+* Alertness Level Char: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.alert_level.xml <br>
+*/
+class LinkLossService {
+public:
+ enum AlertLevel_t {
+ NO_ALERT = 0,
+ MILD_ALERT = 1,
+ HIGH_ALERT = 2
+ };
+
+ typedef void (* callback_t)(AlertLevel_t level);
+
+ /**
+ * @param[ref] ble
+ * BLEDevice object for the underlying controller.
+ */
+ LinkLossService(BLEDevice &bleIn, callback_t callbackIn, AlertLevel_t levelIn = NO_ALERT) :
+ ble(bleIn),
+ alertLevel(levelIn),
+ callback(callbackIn),
+ alertLevelChar(GattCharacteristic::UUID_ALERT_LEVEL_CHAR, reinterpret_cast<uint8_t *>(&alertLevel), sizeof(uint8_t), sizeof(uint8_t),
+ GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE) {
+ static bool serviceAdded = false; /* We should only ever add one LinkLoss service. */
+ if (serviceAdded) {
+ return;
+ }
+
+ GattCharacteristic *charTable[] = {&alertLevelChar};
+ GattService linkLossService(GattService::UUID_LINK_LOSS_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+
+ ble.addService(linkLossService);
+ serviceAdded = true;
+
+ ble.addToDisconnectionCallChain(this, &LinkLossService::onDisconnectionFilter);
+ ble.onDataWritten(this, &LinkLossService::onDataWritten);
+ }
+
+ /**
+ * Update the callback.
+ */
+ void setCallback(callback_t newCallback) {
+ callback = newCallback;
+ }
+
+ /**
+ * Update Alertness Level.
+ */
+ void setAlertLevel(AlertLevel_t newLevel) {
+ alertLevel = newLevel;
+ }
+
+private:
+ /**
+ * This callback allows receiving updates to the AlertLevel Characteristic.
+ *
+ * @param[in] params
+ * Information about the characterisitc being updated.
+ */
+ virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
+ if (params->charHandle == alertLevelChar.getValueHandle()) {
+ alertLevel = *reinterpret_cast<const AlertLevel_t *>(params->data);
+ }
+ }
+
+ void onDisconnectionFilter(void) {
+ if (alertLevel != NO_ALERT) {
+ callback(alertLevel);
+ }
+ }
+
+private:
+ BLEDevice &ble;
+ AlertLevel_t alertLevel;
+ callback_t callback;
+ GattCharacteristic alertLevelChar;
+};
+
+#endif /* __BLE_LINK_LOSS_SERVICE_H__ */
\ No newline at end of file
--- a/services/UARTService.cpp Tue Dec 02 02:51:52 2014 +0000
+++ b/services/UARTService.cpp Mon Jan 12 14:49:53 2015 -0800
@@ -16,26 +16,26 @@
#include "UARTService.h"
-const uint8_t UARTServiceBaseUUID[LENGTH_OF_LONG_UUID] = {
+const uint8_t UARTServiceBaseUUID[LENGTH_OF_LONG_UUID] = {
0x6E, 0x40, 0x00, 0x00, 0xB5, 0xA3, 0xF3, 0x93,
0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
};
const uint16_t UARTServiceShortUUID = 0x0001;
const uint16_t UARTServiceTXCharacteristicShortUUID = 0x0002;
const uint16_t UARTServiceRXCharacteristicShortUUID = 0x0003;
-const uint8_t UARTServiceUUID[LENGTH_OF_LONG_UUID] = {
+const uint8_t UARTServiceUUID[LENGTH_OF_LONG_UUID] = {
0x6E, 0x40, (uint8_t)(UARTServiceShortUUID >> 8), (uint8_t)(UARTServiceShortUUID & 0xFF), 0xB5, 0xA3, 0xF3, 0x93,
0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
};
-const uint8_t UARTServiceUUID_reversed[LENGTH_OF_LONG_UUID] = {
+const uint8_t UARTServiceUUID_reversed[LENGTH_OF_LONG_UUID] = {
0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0,
0x93, 0xF3, 0xA3, 0xB5, (uint8_t)(UARTServiceShortUUID & 0xFF), (uint8_t)(UARTServiceShortUUID >> 8), 0x40, 0x6E
};
-const uint8_t UARTServiceTXCharacteristicUUID[LENGTH_OF_LONG_UUID] = {
+const uint8_t UARTServiceTXCharacteristicUUID[LENGTH_OF_LONG_UUID] = {
0x6E, 0x40, (uint8_t)(UARTServiceTXCharacteristicShortUUID >> 8), (uint8_t)(UARTServiceTXCharacteristicShortUUID & 0xFF), 0xB5, 0xA3, 0xF3, 0x93,
0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
};
-const uint8_t UARTServiceRXCharacteristicUUID[LENGTH_OF_LONG_UUID] = {
+const uint8_t UARTServiceRXCharacteristicUUID[LENGTH_OF_LONG_UUID] = {
0x6E, 0x40, (uint8_t)(UARTServiceRXCharacteristicShortUUID >> 8), (uint8_t)(UARTServiceRXCharacteristicShortUUID & 0xFF), 0xB5, 0xA3, 0xF3, 0x93,
0xE0, 0xA9, 0xE5, 0x0E, 0x24, 0xDC, 0xCA, 0x9E,
-};
+};
\ No newline at end of file
--- a/services/UARTService.h Tue Dec 02 02:51:52 2014 +0000
+++ b/services/UARTService.h Mon Jan 12 14:49:53 2015 -0800
@@ -23,17 +23,21 @@
#include "UUID.h"
#include "BLEDevice.h"
-extern const uint8_t UARTServiceBaseUUID[LENGTH_OF_LONG_UUID];
+extern const uint8_t UARTServiceBaseUUID[LENGTH_OF_LONG_UUID];
extern const uint16_t UARTServiceShortUUID;
extern const uint16_t UARTServiceTXCharacteristicShortUUID;
extern const uint16_t UARTServiceRXCharacteristicShortUUID;
-extern const uint8_t UARTServiceUUID[LENGTH_OF_LONG_UUID];
-extern const uint8_t UARTServiceUUID_reversed[LENGTH_OF_LONG_UUID];
+extern const uint8_t UARTServiceUUID[LENGTH_OF_LONG_UUID];
+extern const uint8_t UARTServiceUUID_reversed[LENGTH_OF_LONG_UUID];
-extern const uint8_t UARTServiceTXCharacteristicUUID[LENGTH_OF_LONG_UUID];
-extern const uint8_t UARTServiceRXCharacteristicUUID[LENGTH_OF_LONG_UUID];
+extern const uint8_t UARTServiceTXCharacteristicUUID[LENGTH_OF_LONG_UUID];
+extern const uint8_t UARTServiceRXCharacteristicUUID[LENGTH_OF_LONG_UUID];
+/**
+* @class UARTService
+* @brief BLE Service to enable UART over BLE
+*/
class UARTService {
public:
/**< Maximum length of data (in bytes) that can be transmitted by the UART service module to the peer. */
@@ -41,6 +45,11 @@
static const unsigned BLE_UART_SERVICE_MAX_DATA_LEN = (GATT_MTU_SIZE_DEFAULT - 3);
public:
+
+ /**
+ * @param[ref] ble
+ * BLEDevice object for the underlying controller.
+ */
UARTService(BLEDevice &_ble) :
ble(_ble),
receiveBuffer(),
@@ -73,8 +82,6 @@
}
/**
- * Override for Stream::write().
- *
* We attempt to collect bytes before pushing them to the UART RX
* characteristic--writing to the RX characteristic will then generate
* notifications for the client. Updates made in quick succession to a
@@ -91,15 +98,15 @@
* @param length Amount of characters to be appended.
* @return Amount of characters appended to the rxCharacteristic.
*/
- ssize_t write(const void* _buffer, size_t length) {
- size_t origLength = length;
- const uint8_t *buffer = static_cast<const uint8_t *>(_buffer);
+ ssize_t write(const void *_buffer, size_t length) {
+ size_t origLength = length;
+ const uint8_t *buffer = static_cast<const uint8_t *>(_buffer);
if (ble.getGapState().connected) {
unsigned bufferIndex = 0;
while (length) {
unsigned bytesRemainingInSendBuffer = BLE_UART_SERVICE_MAX_DATA_LEN - sendBufferIndex;
- unsigned bytesToCopy = (length < bytesRemainingInSendBuffer) ? length : bytesRemainingInSendBuffer;
+ unsigned bytesToCopy = (length < bytesRemainingInSendBuffer) ? length : bytesRemainingInSendBuffer;
/* copy bytes into sendBuffer */
memcpy(&sendBuffer[sendBufferIndex], &buffer[bufferIndex], bytesToCopy);
@@ -131,6 +138,11 @@
return (write(&c, 1) == 1) ? 1 : EOF;
}
+ /**
+ * Override for Stream::_getc()
+ * @return
+ * The character read.
+ */
int _getc() {
if (receiveBufferIndex == numBytesReceived) {
return EOF;
@@ -178,4 +190,4 @@
* application. */
};
-#endif /* #ifndef __BLE_UART_SERVICE_H__*/
+#endif /* #ifndef __BLE_UART_SERVICE_H__*/
\ No newline at end of file
--- a/services/URIBeaconConfigService.h Tue Dec 02 02:51:52 2014 +0000
+++ b/services/URIBeaconConfigService.h Mon Jan 12 14:49:53 2015 -0800
@@ -32,14 +32,22 @@
static const uint8_t beaconPeriodCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x88);
static const uint8_t resetCharUUID[] = URI_BEACON_CONFIG_UUID_INITIALIZER_LIST(0x20, 0x89);
+/**
+* @class URIBeaconConfigService
+* @brief UriBeacon Configuration Service. Can be used to set URL, adjust power levels, and set flags.
+*/
class URIBeaconConfigService {
public:
+ /**
+ * @enum TXPowerModes_t
+ * @brief Transmission Power Modes for UriBeacon
+ */
enum TXPowerModes_t {
- TX_POWER_MODE_LOWEST = 0,
- TX_POWER_MODE_LOW = 1,
- TX_POWER_MODE_MEDIUM = 2,
- TX_POWER_MODE_HIGH = 3,
- NUM_POWER_MODES
+ TX_POWER_MODE_LOWEST = 0, /*!< Lowest TX power mode */
+ TX_POWER_MODE_LOW = 1, /*!< Low TX power mode */
+ TX_POWER_MODE_MEDIUM = 2, /*!< Medium TX power mode */
+ TX_POWER_MODE_HIGH = 3, /*!< High TX power mode */
+ NUM_POWER_MODES /*!< Number of Power Modes defined */
};
/**
@@ -123,13 +131,13 @@
* Update flags of the URIBeacon dynamically.
*
* @param[in] flagsIn
- *
+ * @verbatim
* ### UriBeacon Flags
* Bit | Description
* :---- | :----------
* 0 | Invisible Hint
* 1..7 | Reserved for future use. Must be zero.
- *
+ * @endverbatim
* The `Invisible Hint` flag is a command for the user-agent that tells
* it not to access or display the UriBeacon. This is a guideline only,
* and is not a blocking method. User agents may, with user approval,
@@ -142,7 +150,10 @@
}
/**
- * Update the firmwarePowerLevels and defaultPowerLevels tables.
+ * @brief Update the txPowerLevels table.
+ *
+ * @param[in] powerLevelsIn
+ * Array of power levels
*/
void setDefaultTxPowerLevels(const int8_t firmwarePowerLevelsIn[NUM_POWER_MODES],
const int8_t defaultPowerLevelsIn[NUM_POWER_MODES]) {
@@ -154,7 +165,10 @@
}
/**
- * Set the effective power mode from one of the values in the powerLevels tables.
+ * @brief Set the effective power mode from one of the values in the powerLevels tables.
+ *
+ * @param[in] mode
+ * Set the TX Power Mode.
*/
void setTxPowerMode(TXPowerModes_t mode) {
txPowerMode = mode;
@@ -165,7 +179,10 @@
/**
* The period in milliseconds that a UriBeacon packet is transmitted.
*
- * @Note: A value of zero disables UriBeacon transmissions.
+ * @note A value of zero disables UriBeacon transmissions.
+ *
+ * @param beaconPeriodIn
+ * Beacon advertising period in milliseconds
*/
void setBeaconPeriod(uint16_t beaconPeriodIn) {
beaconPeriod = beaconPeriodIn;
@@ -174,7 +191,7 @@
}
private:
- /**
+ /*
* Setup the advertisement payload and GAP settings.
*/
void configureGAP(void) {
@@ -198,11 +215,15 @@
ble.setTxPower(firmwarePowerLevels[txPowerMode]);
}
+ /*
+ * Encode the URI Prefix to a single byte if possible.
+ */
size_t encodeURISchemePrefix(const char *&urldata, size_t &sizeofURLData) {
if (!sizeofURLData) {
return 0;
}
+ /* These are the URI Prefixes that can be abbreviated.*/
const char *prefixes[] = {
"http://www.",
"https://www.",
@@ -228,7 +249,11 @@
return encodedBytes;
}
+ /*
+ * Encode the URI Suffix to a single byte if possible.
+ */
size_t encodeURI(const char *urldata, size_t sizeofURLData) {
+ /* These are the URI suffixes that can be abbreviated. */
const char *suffixes[] = {
".com/",
".org/",
@@ -339,6 +364,9 @@
ble.setAdvertisingPayload();
}
+ /*
+ * Reset the default values.
+ */
void resetDefaults(void) {
lockedState = false;
uriDataLength = 0;
@@ -351,6 +379,10 @@
updateGATT();
}
+ /*
+ * Internal helper function used to update the GATT database following any
+ * change to the internal state of the service object.
+ */
void updateGATT(void) {
updateLockedStateCharacteristic();
updateURIDataCharacteristic();
@@ -381,12 +413,31 @@
}
void updateTxPowerLevelsCharacteristic(void) {
- ble.updateCharacteristicValue(getHandle(txPowerLevelsChar), reinterpret_cast<uint8_t *>(powerLevels), NUM_POWER_MODES * sizeof(int8_t));
+ ble.updateCharacteristicValue(txPowerLevelsChar.getValueHandle(), reinterpret_cast<uint8_t *>(powerLevels), NUM_POWER_MODES * sizeof(int8_t));
+ }
+
+private:
+ void uriDataWriteAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
+ if (lockedState || (params->offset != 0) || (params->len > MAX_SIZE_URI_DATA_CHAR_VALUE)) {
+ params->authorizationReply = false;
+ }
+ }
+
+ void falgsAuthorizationCallback(GattCharacteristicWriteAuthCBParams *params) {
+ if (lockedState || ((*(params->data) & 0xFE) != 0)) {
+ params->authorizationReply = false;
+ }
+ }
+
+ void denyGATTWritesIfLocked(GattCharacteristicWriteAuthCBParams *params) {
+ if (lockedState) {
+ params->authorizationReply = false;
+ }
}
private:
/**
- * For debugging only.
+ * For debugging only. Print Hex representation of ServiceDataPayload to the console.
*/
void dumpEncodedSeviceData() const {
printf("encoded: '");
@@ -397,7 +448,9 @@
}
private:
- static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 18; /* Uri Data must be between 0 and 18 bytes in length. */
+ static const size_t MAX_SIZEOF_SERVICE_DATA_PAYLOAD = 22; /* Uri Data must be between 0 and 18 bytes in length; and
+ * together with the 4-byte header, the service data must
+ * fit within 22 bytes. */
static const size_t MAX_SIZE_URI_DATA_CHAR_VALUE = 48; /* This is chosen arbitrarily. It should be large enough
* to hold any reasonable uncompressed URI. */
@@ -431,4 +484,4 @@
GattCharacteristic resetChar;
};
-#endif /* #ifndef __BLE_URI_BEACON_CONFIG_SERVICE_H__*/
\ No newline at end of file
+#endif /* #ifndef __BLE_URI_BEACON_CONFIG_SERVICE_H__*/
