Changed the device name.

Dependents:   BLE_Health_Thermometer_HeartRateMonitor

Fork of BLE_API_Native_IRC by Yoshihiro TSUBOI

Committer:
ktownsend
Date:
Fri Feb 14 10:50:45 2014 +0000
Revision:
8:b346eddb9346
Parent:
0:4c3097c65247
Added experimental notify/indicate support to updateValue

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:4c3097c65247 1 /* mbed Microcontroller Library
ktownsend 0:4c3097c65247 2 * Copyright (c) 2006-2013 ARM Limited
ktownsend 0:4c3097c65247 3 *
ktownsend 0:4c3097c65247 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:4c3097c65247 5 * you may not use this file except in compliance with the License.
ktownsend 0:4c3097c65247 6 * You may obtain a copy of the License at
ktownsend 0:4c3097c65247 7 *
ktownsend 0:4c3097c65247 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:4c3097c65247 9 *
ktownsend 0:4c3097c65247 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:4c3097c65247 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:4c3097c65247 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:4c3097c65247 13 * See the License for the specific language governing permissions and
ktownsend 0:4c3097c65247 14 * limitations under the License.
ktownsend 0:4c3097c65247 15 */
ktownsend 0:4c3097c65247 16
ktownsend 0:4c3097c65247 17 #include "nRF51GattServer.h"
ktownsend 0:4c3097c65247 18 #include "mbed.h"
ktownsend 0:4c3097c65247 19
ktownsend 0:4c3097c65247 20 #include "common/common.h"
ktownsend 0:4c3097c65247 21 #include "btle/custom/custom_helper.h"
ktownsend 0:4c3097c65247 22
ktownsend 0:4c3097c65247 23 /**************************************************************************/
ktownsend 0:4c3097c65247 24 /*!
ktownsend 0:4c3097c65247 25 @brief Adds a new service to the GATT table on the peripheral
ktownsend 0:4c3097c65247 26
ktownsend 0:4c3097c65247 27 @returns ble_error_t
ktownsend 0:4c3097c65247 28
ktownsend 0:4c3097c65247 29 @retval BLE_ERROR_NONE
ktownsend 0:4c3097c65247 30 Everything executed properly
ktownsend 0:4c3097c65247 31
ktownsend 0:4c3097c65247 32 @section EXAMPLE
ktownsend 0:4c3097c65247 33
ktownsend 0:4c3097c65247 34 @code
ktownsend 0:4c3097c65247 35
ktownsend 0:4c3097c65247 36 @endcode
ktownsend 0:4c3097c65247 37 */
ktownsend 0:4c3097c65247 38 /**************************************************************************/
ktownsend 0:4c3097c65247 39 ble_error_t nRF51GattServer::addService(GattService & service)
ktownsend 0:4c3097c65247 40 {
ktownsend 0:4c3097c65247 41 /* ToDo: Make sure we don't overflow the array, etc. */
ktownsend 0:4c3097c65247 42 /* ToDo: Make sure this service UUID doesn't already exist (?) */
ktownsend 0:4c3097c65247 43 /* ToDo: Basic validation */
ktownsend 0:4c3097c65247 44
ktownsend 0:4c3097c65247 45 /* Add the service to the nRF51 */
ktownsend 0:4c3097c65247 46 ble_uuid_t uuid;
ktownsend 0:4c3097c65247 47
ktownsend 0:4c3097c65247 48 if (service.primaryServiceID.type == UUID::UUID_TYPE_SHORT)
ktownsend 0:4c3097c65247 49 {
ktownsend 0:4c3097c65247 50 /* 16-bit BLE UUID */
ktownsend 0:4c3097c65247 51 uuid.type = BLE_UUID_TYPE_BLE;
ktownsend 0:4c3097c65247 52 }
ktownsend 0:4c3097c65247 53 else
ktownsend 0:4c3097c65247 54 {
ktownsend 0:4c3097c65247 55 /* 128-bit Custom UUID */
ktownsend 0:4c3097c65247 56 uuid.type = custom_add_uuid_base( service.primaryServiceID.base );
ktownsend 0:4c3097c65247 57 }
ktownsend 0:4c3097c65247 58
ktownsend 0:4c3097c65247 59 uuid.uuid = service.primaryServiceID.value;
ktownsend 0:4c3097c65247 60
ktownsend 0:4c3097c65247 61 ASSERT( ERROR_NONE == sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &uuid, &service.handle), BLE_ERROR_PARAM_OUT_OF_RANGE );
ktownsend 0:4c3097c65247 62
ktownsend 0:4c3097c65247 63 /* Add characteristics to the service */
ktownsend 0:4c3097c65247 64 for (uint8_t i = 0; i < service.characteristicCount; i++)
ktownsend 0:4c3097c65247 65 {
ktownsend 0:4c3097c65247 66 GattCharacteristic * p_char = service.characteristics[i];
ktownsend 0:4c3097c65247 67
ktownsend 0:4c3097c65247 68 uuid.uuid = p_char->uuid;
ktownsend 0:4c3097c65247 69 ASSERT ( ERROR_NONE == custom_add_in_characteristic(service.handle, &uuid, p_char->properties,
ktownsend 0:4c3097c65247 70 NULL, p_char->lenMin, p_char->lenMax, &nrfCharacteristicHandles[characteristicCount]), BLE_ERROR_PARAM_OUT_OF_RANGE );
ktownsend 0:4c3097c65247 71
ktownsend 0:4c3097c65247 72 /* Update the characteristic handle */
ktownsend 0:4c3097c65247 73 p_char->handle = characteristicCount;
ktownsend 8:b346eddb9346 74 p_characteristics[characteristicCount++] = p_char;
ktownsend 0:4c3097c65247 75 }
ktownsend 0:4c3097c65247 76
ktownsend 0:4c3097c65247 77 serviceCount++;
ktownsend 0:4c3097c65247 78
ktownsend 0:4c3097c65247 79 return BLE_ERROR_NONE;
ktownsend 0:4c3097c65247 80 }
ktownsend 0:4c3097c65247 81
ktownsend 0:4c3097c65247 82 /**************************************************************************/
ktownsend 0:4c3097c65247 83 /*!
ktownsend 0:4c3097c65247 84 @brief Reads the value of a characteristic, based on the service
ktownsend 0:4c3097c65247 85 and characteristic index fields
ktownsend 0:4c3097c65247 86
ktownsend 0:4c3097c65247 87 @param[in] charHandle
ktownsend 0:4c3097c65247 88 The handle of the GattCharacteristic to read from
ktownsend 0:4c3097c65247 89 @param[in] buffer
ktownsend 0:4c3097c65247 90 Buffer to hold the the characteristic's value
ktownsend 0:4c3097c65247 91 (raw byte array in LSB format)
ktownsend 0:4c3097c65247 92 @param[in] len
ktownsend 0:4c3097c65247 93 The number of bytes read into the buffer
ktownsend 0:4c3097c65247 94
ktownsend 0:4c3097c65247 95 @returns ble_error_t
ktownsend 0:4c3097c65247 96
ktownsend 0:4c3097c65247 97 @retval BLE_ERROR_NONE
ktownsend 0:4c3097c65247 98 Everything executed properly
ktownsend 0:4c3097c65247 99
ktownsend 0:4c3097c65247 100 @section EXAMPLE
ktownsend 0:4c3097c65247 101
ktownsend 0:4c3097c65247 102 @code
ktownsend 0:4c3097c65247 103
ktownsend 0:4c3097c65247 104 @endcode
ktownsend 0:4c3097c65247 105 */
ktownsend 0:4c3097c65247 106 /**************************************************************************/
ktownsend 0:4c3097c65247 107 ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t len)
ktownsend 0:4c3097c65247 108 {
ktownsend 0:4c3097c65247 109 ASSERT( ERROR_NONE == sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE);
ktownsend 0:4c3097c65247 110
ktownsend 0:4c3097c65247 111 return BLE_ERROR_NONE;
ktownsend 0:4c3097c65247 112 }
ktownsend 0:4c3097c65247 113
ktownsend 0:4c3097c65247 114 /**************************************************************************/
ktownsend 0:4c3097c65247 115 /*!
ktownsend 0:4c3097c65247 116 @brief Updates the value of a characteristic, based on the service
ktownsend 0:4c3097c65247 117 and characteristic index fields
ktownsend 0:4c3097c65247 118
ktownsend 0:4c3097c65247 119 @param[in] charHandle
ktownsend 0:4c3097c65247 120 The handle of the GattCharacteristic to write to
ktownsend 0:4c3097c65247 121 @param[in] buffer
ktownsend 0:4c3097c65247 122 Data to use when updating the characteristic's value
ktownsend 0:4c3097c65247 123 (raw byte array in LSB format)
ktownsend 0:4c3097c65247 124 @param[in] len
ktownsend 0:4c3097c65247 125 The number of bytes in buffer
ktownsend 0:4c3097c65247 126
ktownsend 0:4c3097c65247 127 @returns ble_error_t
ktownsend 0:4c3097c65247 128
ktownsend 0:4c3097c65247 129 @retval BLE_ERROR_NONE
ktownsend 0:4c3097c65247 130 Everything executed properly
ktownsend 0:4c3097c65247 131
ktownsend 0:4c3097c65247 132 @section EXAMPLE
ktownsend 0:4c3097c65247 133
ktownsend 0:4c3097c65247 134 @code
ktownsend 0:4c3097c65247 135
ktownsend 0:4c3097c65247 136 @endcode
ktownsend 0:4c3097c65247 137 */
ktownsend 0:4c3097c65247 138 /**************************************************************************/
ktownsend 0:4c3097c65247 139 ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len)
ktownsend 0:4c3097c65247 140 {
ktownsend 8:b346eddb9346 141 if ((p_characteristics[charHandle]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) &&
ktownsend 8:b346eddb9346 142 (m_connectionHandle != BLE_CONN_HANDLE_INVALID) )
ktownsend 8:b346eddb9346 143 {
ktownsend 8:b346eddb9346 144 /* HVX update for the characteristic value */
ktownsend 8:b346eddb9346 145 ble_gatts_hvx_params_t hvx_params;
ktownsend 8:b346eddb9346 146
ktownsend 8:b346eddb9346 147 hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle;
ktownsend 8:b346eddb9346 148 hvx_params.type = (p_characteristics[charHandle]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION;
ktownsend 8:b346eddb9346 149 hvx_params.offset = 0;
ktownsend 8:b346eddb9346 150 hvx_params.p_data = buffer;
ktownsend 8:b346eddb9346 151 hvx_params.p_len = &len;
ktownsend 8:b346eddb9346 152
ktownsend 8:b346eddb9346 153 error_t error = (error_t) sd_ble_gatts_hvx(m_connectionHandle, &hvx_params);
ktownsend 8:b346eddb9346 154
ktownsend 8:b346eddb9346 155 /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and ERROR_NO_TX_BUFFERS the ATT table has been updated. */
ktownsend 8:b346eddb9346 156 if ( (error != ERROR_NONE ) && (error != ERROR_INVALID_STATE) &&
ktownsend 8:b346eddb9346 157 (error != ERROR_BLE_NO_TX_BUFFERS ) && (error != ERROR_BUSY ) &&
ktownsend 8:b346eddb9346 158 (error != ERROR_BLEGATTS_SYS_ATTR_MISSING ) )
ktownsend 8:b346eddb9346 159 {
ktownsend 8:b346eddb9346 160 ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE );
ktownsend 8:b346eddb9346 161 }
ktownsend 8:b346eddb9346 162 } else
ktownsend 8:b346eddb9346 163 {
ktownsend 8:b346eddb9346 164 ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE );
ktownsend 8:b346eddb9346 165 }
ktownsend 0:4c3097c65247 166
ktownsend 0:4c3097c65247 167 return BLE_ERROR_NONE;
ktownsend 0:4c3097c65247 168 }