2014-07-30 10:36:32 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2015-07-06 13:17:22 +00:00
|
|
|
#include "nRF5xGattServer.h"
|
2014-07-30 10:36:32 +00:00
|
|
|
#include "mbed.h"
|
|
|
|
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "btle/custom/custom_helper.h"
|
|
|
|
|
2015-07-06 13:17:22 +00:00
|
|
|
#include "nRF5xGap.h"
|
2014-07-30 10:36:32 +00:00
|
|
|
|
2015-07-06 13:17:22 +00:00
|
|
|
nRF5xGattServer &nRF5xGattServer::getInstance(void) {
|
Improve shutdown to clear BLE API and not just SD
Improve the shutdown functionality, such that a call to ble.shutdown() from
the user application clears the API and nRF5x state and NOT only the
SoftDevice. To achieve this the following changes are introduced:
* Add a protected member cleanup() to nRF5xGap, nRF5xGattClient,
nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery.
* Modify the shutdown() implementation in nRF5xn such that it also calls the
static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h,
GattClient.h and GattServer.h.
* Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager
classes so that they dynamically create their respective objects only if
needed. Previously the GattClient, GattServer and SecurityManager objects were
declared as static, which means that they were always present even though they
were not always needed. This increases memory consumption unnecessarily.
Furthermore, pointers to the object instances are stored in static members of
the classes as specified by the BLE API base classes. This ensures that
calls to shutdown do not require calls to getInstance() functions that would
otherwise result in undesired memory allocations.
* nRF5xGap object is always needed, so this remains allocated statically. But
the reference in Gap is pointed to this object.
The shutdown procedure is as follows:
1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown()
1. The SoftDevice is shutdown
1. The static members of Gap.h, SecurityManager.h, GattClient.h and
GattServer.h are called to clean up their own state.
If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is
returned.
2015-12-11 17:37:58 +00:00
|
|
|
if (gattServerInstance == NULL) {
|
|
|
|
gattServerInstance = new nRF5xGattServer();
|
|
|
|
}
|
|
|
|
return (nRF5xGattServer &) *gattServerInstance;
|
2015-05-28 14:18:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 10:36:32 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/*!
|
|
|
|
@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
|
|
|
|
*/
|
|
|
|
/**************************************************************************/
|
2015-07-06 13:17:22 +00:00
|
|
|
ble_error_t nRF5xGattServer::addService(GattService &service)
|
2014-07-30 10:36:32 +00:00
|
|
|
{
|
|
|
|
/* ToDo: Make sure this service UUID doesn't already exist (?) */
|
|
|
|
/* ToDo: Basic validation */
|
|
|
|
|
|
|
|
/* Add the service to the nRF51 */
|
|
|
|
ble_uuid_t nordicUUID;
|
|
|
|
nordicUUID = custom_convert_to_nordic_uuid(service.getUUID());
|
|
|
|
|
|
|
|
uint16_t serviceHandle;
|
|
|
|
ASSERT( ERROR_NONE ==
|
|
|
|
sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
|
|
|
|
&nordicUUID,
|
|
|
|
&serviceHandle),
|
|
|
|
BLE_ERROR_PARAM_OUT_OF_RANGE );
|
|
|
|
service.setHandle(serviceHandle);
|
|
|
|
|
|
|
|
/* Add characteristics to the service */
|
|
|
|
for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
|
2015-10-13 11:54:46 +00:00
|
|
|
if (characteristicCount >= BLE_TOTAL_CHARACTERISTICS) {
|
|
|
|
return BLE_ERROR_NO_MEM;
|
|
|
|
}
|
|
|
|
GattCharacteristic *p_char = service.getCharacteristic(i);
|
2014-07-30 10:36:32 +00:00
|
|
|
|
2014-09-22 10:15:05 +00:00
|
|
|
/* Skip any incompletely defined, read-only characteristics. */
|
2014-09-22 08:04:52 +00:00
|
|
|
if ((p_char->getValueAttribute().getValuePtr() == NULL) &&
|
2015-11-27 17:22:30 +00:00
|
|
|
(p_char->getValueAttribute().getLength() == 0) &&
|
2014-09-22 08:04:52 +00:00
|
|
|
(p_char->getProperties() == GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-02 12:35:55 +00:00
|
|
|
nordicUUID = custom_convert_to_nordic_uuid(p_char->getValueAttribute().getUUID());
|
2014-07-30 10:36:32 +00:00
|
|
|
|
2015-04-24 07:19:06 +00:00
|
|
|
/* The user-description descriptor is a special case which needs to be
|
|
|
|
* handled at the time of adding the characteristic. The following block
|
|
|
|
* is meant to discover its presence. */
|
|
|
|
const uint8_t *userDescriptionDescriptorValuePtr = NULL;
|
|
|
|
uint16_t userDescriptionDescriptorValueLen = 0;
|
|
|
|
for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
|
|
|
|
GattAttribute *p_desc = p_char->getDescriptor(j);
|
|
|
|
if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
|
|
|
|
userDescriptionDescriptorValuePtr = p_desc->getValuePtr();
|
|
|
|
userDescriptionDescriptorValueLen = p_desc->getLength();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 10:36:32 +00:00
|
|
|
ASSERT ( ERROR_NONE ==
|
2014-09-02 12:34:54 +00:00
|
|
|
custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID,
|
2014-07-30 10:36:32 +00:00
|
|
|
&nordicUUID,
|
|
|
|
p_char->getProperties(),
|
2015-04-22 10:58:53 +00:00
|
|
|
p_char->getRequiredSecurity(),
|
2014-09-02 12:35:55 +00:00
|
|
|
p_char->getValueAttribute().getValuePtr(),
|
2015-11-27 17:22:30 +00:00
|
|
|
p_char->getValueAttribute().getLength(),
|
2014-09-02 12:35:55 +00:00
|
|
|
p_char->getValueAttribute().getMaxLength(),
|
2015-11-30 01:47:06 +00:00
|
|
|
p_char->getValueAttribute().hasVariableLength(),
|
2015-04-24 07:19:06 +00:00
|
|
|
userDescriptionDescriptorValuePtr,
|
|
|
|
userDescriptionDescriptorValueLen,
|
2014-12-12 13:14:08 +00:00
|
|
|
p_char->isReadAuthorizationEnabled(),
|
2014-12-12 11:03:38 +00:00
|
|
|
p_char->isWriteAuthorizationEnabled(),
|
2014-07-30 10:36:32 +00:00
|
|
|
&nrfCharacteristicHandles[characteristicCount]),
|
|
|
|
BLE_ERROR_PARAM_OUT_OF_RANGE );
|
|
|
|
|
|
|
|
/* Update the characteristic handle */
|
2015-07-01 10:39:13 +00:00
|
|
|
p_characteristics[characteristicCount] = p_char;
|
|
|
|
p_char->getValueAttribute().setHandle(nrfCharacteristicHandles[characteristicCount].value_handle);
|
|
|
|
characteristicCount++;
|
2014-08-28 09:51:52 +00:00
|
|
|
|
2014-09-02 12:34:54 +00:00
|
|
|
/* Add optional descriptors if any */
|
|
|
|
for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) {
|
2015-10-13 11:54:46 +00:00
|
|
|
if (descriptorCount >= BLE_TOTAL_DESCRIPTORS) {
|
|
|
|
return BLE_ERROR_NO_MEM;
|
|
|
|
}
|
2015-09-29 14:04:59 +00:00
|
|
|
|
2015-10-13 11:54:46 +00:00
|
|
|
GattAttribute *p_desc = p_char->getDescriptor(j);
|
2015-04-24 07:19:06 +00:00
|
|
|
/* skip the user-description-descriptor here; this has already been handled when adding the characteristic (above). */
|
|
|
|
if (p_desc->getUUID() == BLE_UUID_DESCRIPTOR_CHAR_USER_DESC) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-24 07:10:54 +00:00
|
|
|
|
|
|
|
nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID());
|
|
|
|
|
|
|
|
ASSERT(ERROR_NONE ==
|
|
|
|
custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID,
|
|
|
|
&nordicUUID,
|
|
|
|
p_desc->getValuePtr(),
|
2015-11-27 17:22:30 +00:00
|
|
|
p_desc->getLength(),
|
2015-04-24 07:10:54 +00:00
|
|
|
p_desc->getMaxLength(),
|
2015-11-30 01:47:06 +00:00
|
|
|
p_desc->hasVariableLength(),
|
2015-04-24 07:10:54 +00:00
|
|
|
&nrfDescriptorHandles[descriptorCount]),
|
|
|
|
BLE_ERROR_PARAM_OUT_OF_RANGE);
|
2014-09-02 12:34:54 +00:00
|
|
|
|
2014-08-28 09:51:52 +00:00
|
|
|
p_descriptors[descriptorCount++] = p_desc;
|
2015-07-01 10:39:13 +00:00
|
|
|
p_desc->setHandle(nrfDescriptorHandles[descriptorCount]);
|
2014-07-30 10:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceCount++;
|
|
|
|
|
|
|
|
return BLE_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Reads the value of a characteristic, based on the service
|
|
|
|
and characteristic index fields
|
|
|
|
|
2015-03-30 14:24:28 +00:00
|
|
|
@param[in] attributeHandle
|
2014-07-30 10:36:32 +00:00
|
|
|
The handle of the GattCharacteristic to read from
|
|
|
|
@param[in] buffer
|
|
|
|
Buffer to hold the the characteristic's value
|
|
|
|
(raw byte array in LSB format)
|
2015-03-30 14:24:28 +00:00
|
|
|
@param[in/out] len
|
|
|
|
input: Length in bytes to be read.
|
|
|
|
output: Total length of attribute value upon successful return.
|
2014-07-30 10:36:32 +00:00
|
|
|
|
|
|
|
@returns ble_error_t
|
|
|
|
|
|
|
|
@retval BLE_ERROR_NONE
|
|
|
|
Everything executed properly
|
|
|
|
*/
|
|
|
|
/**************************************************************************/
|
2015-07-06 13:17:22 +00:00
|
|
|
ble_error_t nRF5xGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
|
2015-03-30 14:24:28 +00:00
|
|
|
{
|
2015-06-18 20:23:54 +00:00
|
|
|
return read(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, lengthP);
|
2015-03-30 14:24:28 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:17:22 +00:00
|
|
|
ble_error_t nRF5xGattServer::read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
|
2014-07-30 10:36:32 +00:00
|
|
|
{
|
2015-03-30 14:24:28 +00:00
|
|
|
ble_gatts_value_t value = {
|
|
|
|
.len = *lengthP,
|
|
|
|
.offset = 0,
|
|
|
|
.p_value = buffer,
|
|
|
|
};
|
|
|
|
|
2014-07-30 10:36:32 +00:00
|
|
|
ASSERT( ERROR_NONE ==
|
2015-07-01 10:39:13 +00:00
|
|
|
sd_ble_gatts_value_get(connectionHandle, attributeHandle, &value),
|
2014-07-30 10:36:32 +00:00
|
|
|
BLE_ERROR_PARAM_OUT_OF_RANGE);
|
2015-03-30 14:24:28 +00:00
|
|
|
*lengthP = value.len;
|
2014-07-30 10:36:32 +00:00
|
|
|
|
|
|
|
return BLE_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Updates the value of a characteristic, based on the service
|
|
|
|
and characteristic index fields
|
|
|
|
|
|
|
|
@param[in] charHandle
|
|
|
|
The handle of the GattCharacteristic to write to
|
|
|
|
@param[in] buffer
|
|
|
|
Data to use when updating the characteristic's value
|
|
|
|
(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
|
|
|
|
*/
|
|
|
|
/**************************************************************************/
|
2015-07-06 13:17:22 +00:00
|
|
|
ble_error_t nRF5xGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
|
2015-03-30 14:24:28 +00:00
|
|
|
{
|
2015-06-18 20:23:54 +00:00
|
|
|
return write(BLE_CONN_HANDLE_INVALID, attributeHandle, buffer, len, localOnly);
|
2015-03-30 14:24:28 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:17:22 +00:00
|
|
|
ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
|
2014-07-30 10:36:32 +00:00
|
|
|
{
|
2015-01-06 13:06:37 +00:00
|
|
|
ble_error_t returnValue = BLE_ERROR_NONE;
|
2014-07-30 10:36:32 +00:00
|
|
|
|
2015-03-30 14:24:28 +00:00
|
|
|
ble_gatts_value_t value = {
|
|
|
|
.len = len,
|
|
|
|
.offset = 0,
|
|
|
|
.p_value = const_cast<uint8_t *>(buffer),
|
|
|
|
};
|
|
|
|
|
2014-07-30 10:36:32 +00:00
|
|
|
if (localOnly) {
|
|
|
|
/* Only update locally regardless of notify/indicate */
|
|
|
|
ASSERT_INT( ERROR_NONE,
|
2015-07-01 10:39:13 +00:00
|
|
|
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
|
2014-07-30 10:36:32 +00:00
|
|
|
BLE_ERROR_PARAM_OUT_OF_RANGE );
|
2015-03-30 14:24:28 +00:00
|
|
|
return BLE_ERROR_NONE;
|
2014-07-30 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 10:39:13 +00:00
|
|
|
int characteristicIndex = resolveValueHandleToCharIndex(attributeHandle);
|
|
|
|
if ((characteristicIndex != -1) &&
|
2015-08-28 12:44:34 +00:00
|
|
|
(p_characteristics[characteristicIndex]->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) {
|
2014-07-30 10:36:32 +00:00
|
|
|
/* HVX update for the characteristic value */
|
|
|
|
ble_gatts_hvx_params_t hvx_params;
|
|
|
|
|
2015-07-01 10:39:13 +00:00
|
|
|
hvx_params.handle = attributeHandle;
|
2014-07-30 10:36:32 +00:00
|
|
|
hvx_params.type =
|
2015-07-01 10:39:13 +00:00
|
|
|
(p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
|
2014-07-30 10:36:32 +00:00
|
|
|
hvx_params.offset = 0;
|
2015-04-15 07:21:11 +00:00
|
|
|
hvx_params.p_data = const_cast<uint8_t *>(buffer);
|
2014-07-30 10:36:32 +00:00
|
|
|
hvx_params.p_len = &len;
|
2015-08-28 13:01:52 +00:00
|
|
|
|
2015-09-02 08:17:02 +00:00
|
|
|
if (connectionHandle == BLE_CONN_HANDLE_INVALID) { /* use the default connection handle if the caller hasn't specified a valid connectionHandle. */
|
|
|
|
connectionHandle = nRF5xGap::getInstance().getConnectionHandle();
|
|
|
|
}
|
|
|
|
error_t error = (error_t) sd_ble_gatts_hvx(connectionHandle, &hvx_params);
|
2015-01-09 10:44:09 +00:00
|
|
|
if (error != ERROR_NONE) {
|
2015-08-28 12:44:34 +00:00
|
|
|
switch (error) {
|
|
|
|
case ERROR_BLE_NO_TX_BUFFERS: /* Notifications consume application buffers. The return value can be used for resending notifications. */
|
|
|
|
case ERROR_BUSY:
|
|
|
|
returnValue = BLE_STACK_BUSY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERROR_INVALID_STATE:
|
|
|
|
case ERROR_BLEGATTS_SYS_ATTR_MISSING:
|
|
|
|
returnValue = BLE_ERROR_INVALID_STATE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
ASSERT_INT( ERROR_NONE,
|
|
|
|
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
|
|
|
|
BLE_ERROR_PARAM_OUT_OF_RANGE );
|
2015-09-25 13:19:49 +00:00
|
|
|
|
|
|
|
/* Notifications consume application buffers. The return value can
|
|
|
|
* be used for resending notifications. */
|
|
|
|
returnValue = BLE_STACK_BUSY;
|
2015-08-28 12:44:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-01-06 13:06:37 +00:00
|
|
|
}
|
2014-07-30 10:36:32 +00:00
|
|
|
} else {
|
2015-08-28 12:44:34 +00:00
|
|
|
returnValue = BLE_ERROR_INVALID_STATE; // if assert is not used
|
2014-07-30 10:36:32 +00:00
|
|
|
ASSERT_INT( ERROR_NONE,
|
2015-07-01 10:39:13 +00:00
|
|
|
sd_ble_gatts_value_set(connectionHandle, attributeHandle, &value),
|
2014-07-30 10:36:32 +00:00
|
|
|
BLE_ERROR_PARAM_OUT_OF_RANGE );
|
|
|
|
}
|
|
|
|
|
2015-01-06 13:06:37 +00:00
|
|
|
return returnValue;
|
2014-07-30 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:17:22 +00:00
|
|
|
ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP)
|
2015-07-02 06:57:51 +00:00
|
|
|
{
|
|
|
|
/* Forward the call with the default connection handle. */
|
2015-07-06 13:17:22 +00:00
|
|
|
return areUpdatesEnabled(nRF5xGap::getInstance().getConnectionHandle(), characteristic, enabledP);
|
2015-07-02 06:57:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 13:17:22 +00:00
|
|
|
ble_error_t nRF5xGattServer::areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP)
|
2015-07-02 06:57:51 +00:00
|
|
|
{
|
|
|
|
int characteristicIndex = resolveValueHandleToCharIndex(characteristic.getValueHandle());
|
|
|
|
if (characteristicIndex == -1) {
|
|
|
|
return BLE_ERROR_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the cccd value from the GATT server. */
|
|
|
|
GattAttribute::Handle_t cccdHandle = nrfCharacteristicHandles[characteristicIndex].cccd_handle;
|
|
|
|
uint16_t cccdValue;
|
|
|
|
uint16_t length = sizeof(cccdValue);
|
|
|
|
ble_error_t rc = read(connectionHandle, cccdHandle, reinterpret_cast<uint8_t *>(&cccdValue), &length);
|
|
|
|
if (rc != BLE_ERROR_NONE) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if (length != sizeof(cccdValue)) {
|
|
|
|
return BLE_ERROR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for NOTFICATION or INDICATION in CCCD. */
|
|
|
|
if ((cccdValue & BLE_GATT_HVX_NOTIFICATION) || (cccdValue & BLE_GATT_HVX_INDICATION)) {
|
|
|
|
*enabledP = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BLE_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
Improve shutdown to clear BLE API and not just SD
Improve the shutdown functionality, such that a call to ble.shutdown() from
the user application clears the API and nRF5x state and NOT only the
SoftDevice. To achieve this the following changes are introduced:
* Add a protected member cleanup() to nRF5xGap, nRF5xGattClient,
nRF5xGattServer, nRF5xSecurityManager and nRF5xServiceDiscovery.
* Modify the shutdown() implementation in nRF5xn such that it also calls the
static member shutdown() exposed by the BLE API in Gap.h, SecurityManager.h,
GattClient.h and GattServer.h.
* Modify nRF5xGattClient, nRF5xGattServer and nRF5xSecurityManager
classes so that they dynamically create their respective objects only if
needed. Previously the GattClient, GattServer and SecurityManager objects were
declared as static, which means that they were always present even though they
were not always needed. This increases memory consumption unnecessarily.
Furthermore, pointers to the object instances are stored in static members of
the classes as specified by the BLE API base classes. This ensures that
calls to shutdown do not require calls to getInstance() functions that would
otherwise result in undesired memory allocations.
* nRF5xGap object is always needed, so this remains allocated statically. But
the reference in Gap is pointed to this object.
The shutdown procedure is as follows:
1. The user calls ble.shutdown() which executes the code in nRF5xn::shutdown()
1. The SoftDevice is shutdown
1. The static members of Gap.h, SecurityManager.h, GattClient.h and
GattServer.h are called to clean up their own state.
If at any point an error occur during the last step, BLE_ERROR_INVALID_STATE is
returned.
2015-12-11 17:37:58 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Clear nRF5xGattServer's state.
|
|
|
|
|
|
|
|
@returns ble_error_t
|
|
|
|
|
|
|
|
@retval BLE_ERROR_NONE
|
|
|
|
Everything executed properly
|
|
|
|
*/
|
|
|
|
/**************************************************************************/
|
|
|
|
ble_error_t nRF5xGattServer::cleanup(void)
|
|
|
|
{
|
|
|
|
/* Clear all state that is from the parent, including private members */
|
|
|
|
if (GattServer::cleanup() != BLE_ERROR_NONE) {
|
|
|
|
return BLE_ERROR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear derived class members */
|
|
|
|
memset(p_characteristics, 0, sizeof(p_characteristics));
|
|
|
|
memset(p_descriptors, 0, sizeof(p_descriptors));
|
|
|
|
memset(nrfCharacteristicHandles, 0, sizeof(ble_gatts_char_handles_t));
|
|
|
|
memset(nrfDescriptorHandles, 0, sizeof(nrfDescriptorHandles));
|
|
|
|
descriptorCount = 0;
|
|
|
|
|
|
|
|
return BLE_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
2014-07-30 10:36:32 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/*!
|
|
|
|
@brief Callback handler for events getting pushed up from the SD
|
|
|
|
*/
|
|
|
|
/**************************************************************************/
|
2015-07-06 13:17:22 +00:00
|
|
|
void nRF5xGattServer::hwCallback(ble_evt_t *p_ble_evt)
|
2014-07-30 10:36:32 +00:00
|
|
|
{
|
2015-05-12 08:11:59 +00:00
|
|
|
GattAttribute::Handle_t handle_value;
|
2014-08-28 09:51:52 +00:00
|
|
|
GattServerEvents::gattEvent_t eventType;
|
|
|
|
const ble_gatts_evt_t *gattsEventP = &p_ble_evt->evt.gatts_evt;
|
2014-07-30 10:36:32 +00:00
|
|
|
|
|
|
|
switch (p_ble_evt->header.evt_id) {
|
2015-07-01 10:39:13 +00:00
|
|
|
case BLE_GATTS_EVT_WRITE: {
|
|
|
|
/* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */
|
|
|
|
|
|
|
|
/* 1.) Handle CCCD changes */
|
|
|
|
handle_value = gattsEventP->params.write.handle;
|
|
|
|
int characteristicIndex = resolveCCCDHandleToCharIndex(handle_value);
|
|
|
|
if ((characteristicIndex != -1) &&
|
|
|
|
(p_characteristics[characteristicIndex]->getProperties() &
|
|
|
|
(GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY))) {
|
|
|
|
|
2015-05-12 07:21:23 +00:00
|
|
|
uint16_t cccd_value = (gattsEventP->params.write.data[1] << 8) | gattsEventP->params.write.data[0]; /* Little Endian but M0 may be mis-aligned */
|
2014-07-30 10:36:32 +00:00
|
|
|
|
2015-07-01 10:39:13 +00:00
|
|
|
if (((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION)) ||
|
|
|
|
((p_characteristics[characteristicIndex]->getProperties() & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) {
|
2014-08-28 09:51:52 +00:00
|
|
|
eventType = GattServerEvents::GATT_EVENT_UPDATES_ENABLED;
|
2014-07-30 10:36:32 +00:00
|
|
|
} else {
|
2014-08-28 09:51:52 +00:00
|
|
|
eventType = GattServerEvents::GATT_EVENT_UPDATES_DISABLED;
|
2014-07-30 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 10:39:13 +00:00
|
|
|
handleEvent(eventType, p_characteristics[characteristicIndex]->getValueHandle());
|
2014-07-30 10:36:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-01 10:39:13 +00:00
|
|
|
/* 2.) Changes to the characteristic value will be handled with other events below */
|
|
|
|
eventType = GattServerEvents::GATT_EVENT_DATA_WRITTEN;
|
|
|
|
}
|
2014-07-30 10:36:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_GATTS_EVT_HVC:
|
|
|
|
/* Indication confirmation received */
|
2014-08-28 09:51:52 +00:00
|
|
|
eventType = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED;
|
|
|
|
handle_value = gattsEventP->params.hvc.handle;
|
2014-07-30 10:36:32 +00:00
|
|
|
break;
|
|
|
|
|
2014-08-29 08:38:33 +00:00
|
|
|
case BLE_EVT_TX_COMPLETE: {
|
2014-08-29 09:56:49 +00:00
|
|
|
handleDataSentEvent(p_ble_evt->evt.common_evt.params.tx_complete.count);
|
2014-08-28 09:51:52 +00:00
|
|
|
return;
|
2014-08-29 08:38:33 +00:00
|
|
|
}
|
2014-08-28 09:51:52 +00:00
|
|
|
|
2014-07-30 10:36:32 +00:00
|
|
|
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
|
2015-03-30 14:24:28 +00:00
|
|
|
sd_ble_gatts_sys_attr_set(gattsEventP->conn_handle, NULL, 0, 0);
|
2014-07-30 10:36:32 +00:00
|
|
|
return;
|
|
|
|
|
2014-12-12 11:03:38 +00:00
|
|
|
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
|
2014-12-12 13:14:08 +00:00
|
|
|
switch (gattsEventP->params.authorize_request.type) {
|
|
|
|
case BLE_GATTS_AUTHORIZE_TYPE_READ:
|
|
|
|
eventType = GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ;
|
|
|
|
handle_value = gattsEventP->params.authorize_request.request.read.handle;
|
|
|
|
break;
|
|
|
|
case BLE_GATTS_AUTHORIZE_TYPE_WRITE:
|
|
|
|
eventType = GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ;
|
|
|
|
handle_value = gattsEventP->params.authorize_request.request.write.handle;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2014-12-12 11:03:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-07-30 10:36:32 +00:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-01 10:39:13 +00:00
|
|
|
int characteristicIndex = resolveValueHandleToCharIndex(handle_value);
|
|
|
|
if (characteristicIndex == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-30 10:36:32 +00:00
|
|
|
/* Find index (charHandle) in the pool */
|
2015-07-01 10:39:13 +00:00
|
|
|
switch (eventType) {
|
|
|
|
case GattServerEvents::GATT_EVENT_DATA_WRITTEN: {
|
|
|
|
GattWriteCallbackParams cbParams = {
|
2015-07-15 09:58:21 +00:00
|
|
|
.connHandle = gattsEventP->conn_handle,
|
|
|
|
.handle = handle_value,
|
|
|
|
.writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.write.op),
|
|
|
|
.offset = gattsEventP->params.write.offset,
|
|
|
|
.len = gattsEventP->params.write.len,
|
|
|
|
.data = gattsEventP->params.write.data
|
2015-07-01 10:39:13 +00:00
|
|
|
};
|
|
|
|
handleDataWrittenEvent(&cbParams);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GattServerEvents::GATT_EVENT_WRITE_AUTHORIZATION_REQ: {
|
|
|
|
GattWriteAuthCallbackParams cbParams = {
|
2015-07-15 09:58:21 +00:00
|
|
|
.connHandle = gattsEventP->conn_handle,
|
|
|
|
.handle = handle_value,
|
|
|
|
.offset = gattsEventP->params.authorize_request.request.write.offset,
|
|
|
|
.len = gattsEventP->params.authorize_request.request.write.len,
|
|
|
|
.data = gattsEventP->params.authorize_request.request.write.data,
|
2015-08-14 08:04:18 +00:00
|
|
|
.authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
|
|
|
|
* set to AUTH_CALLBACK_REPLY_SUCCESS if the client
|
|
|
|
* request is to proceed. */
|
2015-07-01 10:39:13 +00:00
|
|
|
};
|
|
|
|
ble_gatts_rw_authorize_reply_params_t reply = {
|
|
|
|
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
|
|
|
|
.params = {
|
|
|
|
.write = {
|
|
|
|
.gatt_status = p_characteristics[characteristicIndex]->authorizeWrite(&cbParams)
|
2015-01-16 09:20:26 +00:00
|
|
|
}
|
2014-12-12 11:03:38 +00:00
|
|
|
}
|
2015-07-01 10:39:13 +00:00
|
|
|
};
|
|
|
|
sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If write-authorization is enabled for a characteristic,
|
|
|
|
* AUTHORIZATION_REQ event (if replied with true) is *not*
|
|
|
|
* followed by another DATA_WRITTEN event; so we still need
|
|
|
|
* to invoke handleDataWritten(), much the same as we would
|
|
|
|
* have done if write-authorization had not been enabled.
|
|
|
|
*/
|
|
|
|
if (reply.params.write.gatt_status == BLE_GATT_STATUS_SUCCESS) {
|
|
|
|
GattWriteCallbackParams cbParams = {
|
2015-07-15 09:58:21 +00:00
|
|
|
.connHandle = gattsEventP->conn_handle,
|
|
|
|
.handle = handle_value,
|
|
|
|
.writeOp = static_cast<GattWriteCallbackParams::WriteOp_t>(gattsEventP->params.authorize_request.request.write.op),
|
|
|
|
.offset = gattsEventP->params.authorize_request.request.write.offset,
|
|
|
|
.len = gattsEventP->params.authorize_request.request.write.len,
|
|
|
|
.data = gattsEventP->params.authorize_request.request.write.data,
|
2015-07-01 10:39:13 +00:00
|
|
|
};
|
|
|
|
handleDataWrittenEvent(&cbParams);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GattServerEvents::GATT_EVENT_READ_AUTHORIZATION_REQ: {
|
|
|
|
GattReadAuthCallbackParams cbParams = {
|
2015-08-14 08:04:18 +00:00
|
|
|
.connHandle = gattsEventP->conn_handle,
|
|
|
|
.handle = handle_value,
|
|
|
|
.offset = gattsEventP->params.authorize_request.request.read.offset,
|
|
|
|
.len = 0,
|
|
|
|
.data = NULL,
|
|
|
|
.authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS /* the callback handler must leave this member
|
|
|
|
* set to AUTH_CALLBACK_REPLY_SUCCESS if the client
|
|
|
|
* request is to proceed. */
|
2015-07-01 10:39:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ble_gatts_rw_authorize_reply_params_t reply = {
|
|
|
|
.type = BLE_GATTS_AUTHORIZE_TYPE_READ,
|
|
|
|
.params = {
|
|
|
|
.read = {
|
|
|
|
.gatt_status = p_characteristics[characteristicIndex]->authorizeRead(&cbParams)
|
2014-12-16 11:35:29 +00:00
|
|
|
}
|
2014-12-12 13:14:08 +00:00
|
|
|
}
|
2015-07-01 10:39:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (cbParams.authorizationReply == BLE_GATT_STATUS_SUCCESS) {
|
|
|
|
if (cbParams.data != NULL) {
|
|
|
|
reply.params.read.update = 1;
|
|
|
|
reply.params.read.offset = cbParams.offset;
|
|
|
|
reply.params.read.len = cbParams.len;
|
|
|
|
reply.params.read.p_data = cbParams.data;
|
|
|
|
}
|
2014-08-28 09:51:52 +00:00
|
|
|
}
|
2015-07-01 10:39:13 +00:00
|
|
|
|
|
|
|
sd_ble_gatts_rw_authorize_reply(gattsEventP->conn_handle, &reply);
|
|
|
|
break;
|
2014-07-30 10:36:32 +00:00
|
|
|
}
|
2015-07-01 10:39:13 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
handleEvent(eventType, handle_value);
|
|
|
|
break;
|
2014-07-30 10:36:32 +00:00
|
|
|
}
|
|
|
|
}
|