High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
Revision 325:501ad8b8bbe5, committed 2015-03-23
- Comitter:
- rgrover1
- Date:
- Mon Mar 23 16:28:09 2015 +0000
- Parent:
- 324:13a128a1505d
- Child:
- 326:f79c1562312f
- Commit message:
- Synchronized with git rev c7a2b9bb
Author: Rohit Grover
Release 0.3.0
==============
Enhancements
~~~~~~~~~~~~
* BLEDevice::setAdvertisingInterval() uses milliseconds as the unit for its
argument. If advertising interval is set to 0, advertising is stopped. If
advertising interval is smaller than the minimum value supported, then the
minimum value is used instead.
* Add an enumeration called GattCharacteristicAuthCBReply_t to capture a
status code from read/write authorization callback. Previously it used to
return only a bool.
* Add APIs for getMinAdvertisingInterval() and variants.
Bugfixes
~~~~~~~~
* URIBeaconConfigService uses better write-authorization filters. It now
passes tests defined in Google's validator app.
Changed in this revision
--- a/public/BLEDevice.h Mon Mar 23 16:28:09 2015 +0000 +++ b/public/BLEDevice.h Mon Mar 23 16:28:09 2015 +0000 @@ -83,12 +83,10 @@ /** * @param[in] interval - * Advertising interval between 0x0020 and 0x4000 in 0.625ms - * units (20ms to 10.24s). If using non-connectable mode - * (ADV_NON_CONNECTABLE_UNDIRECTED) this min value is - * 0x00A0 (100ms). To reduce the likelihood of collisions, the - * link layer perturbs this interval by a pseudo-random delay - * with a range of 0 ms to 10 ms for each advertising event. + * Advertising interval in units of milliseconds. Advertising + * is disabled if interval is 0. If interval is smaller than + * the minimum supported value, then the minimum supported + * value is used instead. * * \par * Decreasing this value will allow central devices to detect @@ -106,6 +104,19 @@ void setAdvertisingInterval(uint16_t interval); /** + * @return Minimum Advertising interval in milliseconds. + */ + uint16_t getMinAdvertisingInterval(void) const; + /** + * @return Minimum Advertising interval in milliseconds for non connectible mode. + */ + uint16_t getMinNonConnectableAdvertisingInterval(void) const; + /** + * @return Maximum Advertising interval in milliseconds. + */ + uint16_t getMaxAdvertisingInterval(void) const; + + /** * @param[in] timeout * Advertising timeout between 0x1 and 0x3FFF (1 and 16383) * in seconds. Enter 0 to disable the advertising timeout. @@ -443,7 +454,27 @@ inline void BLEDevice::setAdvertisingInterval(uint16_t interval) { - advParams.setInterval(interval); + if (interval == 0) { + stopAdvertising(); + } else if (interval < getMinAdvertisingInterval()) { + interval = getMinAdvertisingInterval(); + } + advParams.setInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(interval)); +} + +inline uint16_t +BLEDevice::getMinAdvertisingInterval(void) const { + return transport->getGap().getMinAdvertisingInterval(); +} + +inline uint16_t +BLEDevice::getMinNonConnectableAdvertisingInterval(void) const { + return transport->getGap().getMinNonConnectableAdvertisingInterval(); +} + +inline uint16_t +BLEDevice::getMaxAdvertisingInterval(void) const { + return transport->getGap().getMaxAdvertisingInterval(); } inline void
--- a/public/Gap.h Mon Mar 23 16:28:09 2015 +0000 +++ b/public/Gap.h Mon Mar 23 16:28:09 2015 +0000 @@ -72,6 +72,9 @@ static uint16_t MSEC_TO_ADVERTISEMENT_DURATION_UNITS(uint32_t durationInMillis) { return (durationInMillis * 1000) / UNIT_0_625_MS; } + static uint16_t GAP_DURATION_UNITS_TO_MS(uint16_t gapUnits) { + return (gapUnits * UNIT_0_625_MS) / 1000; + } typedef void (*EventCallback_t)(void); typedef void (*ConnectionEventCallback_t)(Handle_t, addr_type_t peerAddrType, const address_t peerAddr, const ConnectionParams_t *); @@ -85,6 +88,9 @@ virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &) = 0; virtual ble_error_t startAdvertising(const GapAdvertisingParams &) = 0; virtual ble_error_t stopAdvertising(void) = 0; + virtual uint16_t getMinAdvertisingInterval(void) const = 0; + virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const = 0; + virtual uint16_t getMaxAdvertisingInterval(void) const = 0; virtual ble_error_t disconnect(DisconnectionReason_t reason) = 0; virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params) = 0; virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params) = 0;
--- a/public/GattCharacteristic.h Mon Mar 23 16:28:09 2015 +0000 +++ b/public/GattCharacteristic.h Mon Mar 23 16:28:09 2015 +0000 @@ -365,12 +365,12 @@ * @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) { + GattCharacteristicAuthCBReply_t authorizeWrite(GattCharacteristicWriteAuthCBParams *params) { if (!isWriteAuthorizationEnabled()) { - return true; + return AUTH_CALLBACK_REPLY_SUCCESS; } - params->authorizationReply = true; /* initialized to true by default */ + params->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; /* initialized to no-error by default */ writeAuthorizationCallback.call(params); return params->authorizationReply; } @@ -391,12 +391,12 @@ * * @return true if the read is authorized to proceed. */ - bool authorizeRead(GattCharacteristicReadAuthCBParams *params) { + GattCharacteristicAuthCBReply_t authorizeRead(GattCharacteristicReadAuthCBParams *params) { if (!isReadAuthorizationEnabled()) { - return true; + return AUTH_CALLBACK_REPLY_SUCCESS; } - params->authorizationReply = true; /* initialized to true by default */ + params->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; /* initialized to no-error by default */ readAuthorizationCallback.call(params); return params->authorizationReply; }
--- a/public/GattCharacteristicCallbackParams.h Mon Mar 23 16:28:09 2015 +0000 +++ b/public/GattCharacteristicCallbackParams.h Mon Mar 23 16:28:09 2015 +0000 @@ -44,13 +44,28 @@ uint8_t *data; /**< Outgoing data, variable length. */ }; +enum GattCharacteristicAuthCBReply_t { + AUTH_CALLBACK_REPLY_SUCCESS = 0x00, /**< Success. */ + AUTH_CALLBACK_REPLY_ATTERR_INVALID_HANDLE = 0x0101, /**< ATT Error: Invalid Attribute Handle. */ + AUTH_CALLBACK_REPLY_ATTERR_READ_NOT_PERMITTED = 0x0102, /**< ATT Error: Read not permitted. */ + AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED = 0x0103, /**< ATT Error: Write not permitted. */ + AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHENTICATION = 0x0105, /**< ATT Error: Authenticated link required. */ + AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET = 0x0107, /**< ATT Error: Offset specified was past the end of the attribute. */ + AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION = 0x0108, /**< ATT Error: Used in ATT as Insufficient Authorisation. */ + AUTH_CALLBACK_REPLY_ATTERR_PREPARE_QUEUE_FULL = 0x0109, /**< ATT Error: Used in ATT as Prepare Queue Full. */ + AUTH_CALLBACK_REPLY_ATTERR_ATTRIBUTE_NOT_FOUND = 0x010A, /**< ATT Error: Used in ATT as Attribute not found. */ + AUTH_CALLBACK_REPLY_ATTERR_ATTRIBUTE_NOT_LONG = 0x010B, /**< ATT Error: Attribute cannot be read or written using read/write blob requests. */ + AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH = 0x010D, /**< ATT Error: Invalid value size. */ + AUTH_CALLBACK_REPLY_ATTERR_INSUF_RESOURCES = 0x0111, /**< ATT Error: Encrypted link required. */ +}; + 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. */ + GattCharacteristicAuthCBReply_t 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 { @@ -58,8 +73,8 @@ uint16_t offset; /**< Offset for the read operation. */ uint16_t len; /**< Optional: new length of the outgoing data. */ uint8_t *data; /**< Optional: new outgoing data. Leave at NULL if data is unchanged. */ - 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. */ + GattCharacteristicAuthCBReply_t 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_CALLBACK_PARAMS_H__*/ \ No newline at end of file
--- a/services/URIBeaconConfigService.h Mon Mar 23 16:28:09 2015 +0000 +++ b/services/URIBeaconConfigService.h Mon Mar 23 16:28:09 2015 +0000 @@ -131,11 +131,11 @@ lockChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::lockAuthorizationCallback); unlockChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::unlockAuthorizationCallback); uriDataChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::uriDataWriteAuthorizationCallback); - flagsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::flagsAuthorizationCallback); - advPowerLevelsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); - txPowerModeChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); - beaconPeriodChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); - resetChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::denyGATTWritesIfLocked); + flagsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::basicAuthorizationCallback<uint8_t>); + advPowerLevelsChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::basicAuthorizationCallback<PowerLevels_t>); + txPowerModeChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::powerModeAuthorizationCallback); + beaconPeriodChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::basicAuthorizationCallback<uint16_t>); + resetChar.setWriteAuthorizationCallback(this, &URIBeaconConfigService::basicAuthorizationCallback<uint8_t>); static GattCharacteristic *charTable[] = { &lockedStateChar, &lockChar, &unlockChar, &uriDataChar, @@ -210,7 +210,7 @@ ble.clearAdvertisingPayload(); ble.setTxPower(params.advPowerLevels[params.txPowerMode]); ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); - ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(beaconPeriod)); + ble.setAdvertisingInterval(beaconPeriod); ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); @@ -297,33 +297,66 @@ private: void lockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { - authParams->authorizationReply = (authParams->len == sizeof(Lock_t)) && !lockedState; + if (lockedState) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; + } else if (authParams->len != sizeof(Lock_t)) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; + } else if (authParams->offset != 0) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; + } else { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; + } } void unlockAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { - if (!lockedState || (authParams->len == sizeof(Lock_t) && (memcmp(authParams->data, params.lock, sizeof(Lock_t)) == 0))) { - authParams->authorizationReply = true; + if (!lockedState) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; + } else if (authParams->len != sizeof(Lock_t)) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; + } else if (authParams->offset != 0) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; + } else if (memcmp(authParams->data, params.lock, sizeof(Lock_t)) != 0) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; } else { - authParams->authorizationReply = false; + authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; } } void uriDataWriteAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { - if (lockedState || (authParams->offset != 0) || (authParams->len > URI_DATA_MAX)) { - authParams->authorizationReply = false; + if (lockedState) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; + } else if (authParams->offset != 0) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; + } else { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; } } - void flagsAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { - if (lockedState || (authParams->len != 1)) { - authParams->authorizationReply = false; + void powerModeAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { + if (lockedState) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; + } else if (authParams->len != sizeof(uint8_t)) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; + } else if (authParams->offset != 0) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; + } else if (*((uint8_t *)authParams->data) >= NUM_POWER_MODES) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED; + } else { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; } } - void denyGATTWritesIfLocked(GattCharacteristicWriteAuthCBParams *authParams) { + template <typename T> + void basicAuthorizationCallback(GattCharacteristicWriteAuthCBParams *authParams) { if (lockedState) { - authParams->authorizationReply = false; + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INSUF_AUTHORIZATION; + } else if (authParams->len != sizeof(T)) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH; + } else if (authParams->offset != 0) { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_ATTERR_INVALID_OFFSET; + } else { + authParams->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; } }