BLE lib

Fork of Nucleo_BLE_BlueNRG by STM32 eKairn

Committer:
sjallouli
Date:
Fri Dec 19 18:56:07 2014 +0000
Revision:
0:a948f5f3904c
Child:
2:d76864b8a341
BLE_BlueNRG for Nucleo board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sjallouli 0:a948f5f3904c 1 /* mbed Microcontroller Library
sjallouli 0:a948f5f3904c 2 * Copyright (c) 2006-2013 ARM Limited
sjallouli 0:a948f5f3904c 3 *
sjallouli 0:a948f5f3904c 4 * Licensed under the Apache License, Version 2.0 (the "License");
sjallouli 0:a948f5f3904c 5 * you may not use this file except in compliance with the License.
sjallouli 0:a948f5f3904c 6 * You may obtain a copy of the License at
sjallouli 0:a948f5f3904c 7 *
sjallouli 0:a948f5f3904c 8 * http://www.apache.org/licenses/LICENSE-2.0
sjallouli 0:a948f5f3904c 9 *
sjallouli 0:a948f5f3904c 10 * Unless required by applicable law or agreed to in writing, software
sjallouli 0:a948f5f3904c 11 * distributed under the License is distributed on an "AS IS" BASIS,
sjallouli 0:a948f5f3904c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sjallouli 0:a948f5f3904c 13 * See the License for the specific language governing permissions and
sjallouli 0:a948f5f3904c 14 * limitations under the License.
sjallouli 0:a948f5f3904c 15 */
sjallouli 0:a948f5f3904c 16
sjallouli 0:a948f5f3904c 17 #include "BlueNRGGattServer.h"
sjallouli 0:a948f5f3904c 18 #include "mbed.h"
sjallouli 0:a948f5f3904c 19 #include "BlueNRGGap.h"
sjallouli 0:a948f5f3904c 20 #include "Utils.h"
sjallouli 0:a948f5f3904c 21
sjallouli 0:a948f5f3904c 22 /**************************************************************************/
sjallouli 0:a948f5f3904c 23 /*!
sjallouli 0:a948f5f3904c 24 @brief Adds a new service to the GATT table on the peripheral
sjallouli 0:a948f5f3904c 25
sjallouli 0:a948f5f3904c 26 @returns ble_error_t
sjallouli 0:a948f5f3904c 27
sjallouli 0:a948f5f3904c 28 @retval BLE_ERROR_NONE
sjallouli 0:a948f5f3904c 29 Everything executed properly
sjallouli 0:a948f5f3904c 30
sjallouli 0:a948f5f3904c 31 @section EXAMPLE
sjallouli 0:a948f5f3904c 32
sjallouli 0:a948f5f3904c 33 @code
sjallouli 0:a948f5f3904c 34
sjallouli 0:a948f5f3904c 35 @endcode
sjallouli 0:a948f5f3904c 36 */
sjallouli 0:a948f5f3904c 37 /**************************************************************************/
sjallouli 0:a948f5f3904c 38 ble_error_t BlueNRGGattServer::addService(GattService &service)
sjallouli 0:a948f5f3904c 39 {
sjallouli 0:a948f5f3904c 40 /* ToDo: Make sure we don't overflow the array, etc. */
sjallouli 0:a948f5f3904c 41 /* ToDo: Make sure this service UUID doesn't already exist (?) */
sjallouli 0:a948f5f3904c 42 /* ToDo: Basic validation */
sjallouli 0:a948f5f3904c 43
sjallouli 0:a948f5f3904c 44 tBleStatus ret;
sjallouli 0:a948f5f3904c 45 uint8_t type;
sjallouli 0:a948f5f3904c 46 uint16_t short_uuid;
sjallouli 0:a948f5f3904c 47 uint8_t primary_short_uuid[2];
sjallouli 0:a948f5f3904c 48 uint8_t primary_base_uuid[16];
sjallouli 0:a948f5f3904c 49 uint8_t char_base_uuid[16];
sjallouli 0:a948f5f3904c 50 const uint8_t *base_uuid;
sjallouli 0:a948f5f3904c 51 const uint8_t *base_char_uuid;
sjallouli 0:a948f5f3904c 52
sjallouli 0:a948f5f3904c 53 type = (service.getUUID()).shortOrLong();
sjallouli 0:a948f5f3904c 54 DEBUG("AddService(): Type:%d\n\r", type);
sjallouli 0:a948f5f3904c 55
sjallouli 0:a948f5f3904c 56 /* Add the service to the BlueNRG */
sjallouli 0:a948f5f3904c 57 short_uuid = (service.getUUID()).getShortUUID();
sjallouli 0:a948f5f3904c 58 STORE_LE_16(primary_short_uuid, short_uuid);
sjallouli 0:a948f5f3904c 59
sjallouli 0:a948f5f3904c 60 if(type==UUID::UUID_TYPE_LONG) {
sjallouli 0:a948f5f3904c 61 base_uuid = (service.getUUID()).getBaseUUID();
sjallouli 0:a948f5f3904c 62
sjallouli 0:a948f5f3904c 63 COPY_UUID_128(primary_base_uuid, base_uuid[15],base_uuid[14],base_uuid[13],base_uuid[12],base_uuid[11],base_uuid[10],base_uuid[9],
sjallouli 0:a948f5f3904c 64 base_uuid[8],base_uuid[7],base_uuid[6],base_uuid[5],base_uuid[4],primary_short_uuid[1],primary_short_uuid[0],base_uuid[1],base_uuid[0]);
sjallouli 0:a948f5f3904c 65 }
sjallouli 0:a948f5f3904c 66
sjallouli 0:a948f5f3904c 67 if(type==UUID::UUID_TYPE_SHORT) {
sjallouli 0:a948f5f3904c 68 ret = aci_gatt_add_serv(UUID_TYPE_16, primary_short_uuid, PRIMARY_SERVICE, 7,
sjallouli 0:a948f5f3904c 69 &servHandle);
sjallouli 0:a948f5f3904c 70 }
sjallouli 0:a948f5f3904c 71 else if(type==UUID::UUID_TYPE_LONG) {
sjallouli 0:a948f5f3904c 72 ret = aci_gatt_add_serv(UUID_TYPE_128, primary_base_uuid, PRIMARY_SERVICE, 7,
sjallouli 0:a948f5f3904c 73 &servHandle);
sjallouli 0:a948f5f3904c 74 }
sjallouli 0:a948f5f3904c 75
sjallouli 0:a948f5f3904c 76 service.setHandle(servHandle);
sjallouli 0:a948f5f3904c 77 //serviceHandleVector.push_back(servHandle);
sjallouli 0:a948f5f3904c 78 DEBUG("added servHandle handle =%u\n\r", servHandle);
sjallouli 0:a948f5f3904c 79 tHalUint16 bleCharacteristic;
sjallouli 0:a948f5f3904c 80
sjallouli 0:a948f5f3904c 81 //iterate to include all characteristics
sjallouli 0:a948f5f3904c 82 for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) {
sjallouli 0:a948f5f3904c 83 GattCharacteristic *p_char = service.getCharacteristic(i);
sjallouli 0:a948f5f3904c 84 uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID();
sjallouli 0:a948f5f3904c 85
sjallouli 0:a948f5f3904c 86 uint8_t int_8_uuid[2];
sjallouli 0:a948f5f3904c 87 STORE_LE_16(int_8_uuid, char_uuid);
sjallouli 0:a948f5f3904c 88
sjallouli 0:a948f5f3904c 89 if(type==UUID::UUID_TYPE_LONG) {
sjallouli 0:a948f5f3904c 90 base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID();
sjallouli 0:a948f5f3904c 91
sjallouli 0:a948f5f3904c 92 COPY_UUID_128(char_base_uuid, base_char_uuid[15],base_char_uuid[14],base_char_uuid[13],base_char_uuid[12],base_char_uuid[11],base_char_uuid[10],base_char_uuid[9],
sjallouli 0:a948f5f3904c 93 base_char_uuid[8],base_char_uuid[7],base_char_uuid[6],base_char_uuid[5],base_char_uuid[4],int_8_uuid[1],int_8_uuid[0],base_char_uuid[1],base_char_uuid[0]);
sjallouli 0:a948f5f3904c 94 }
sjallouli 0:a948f5f3904c 95
sjallouli 0:a948f5f3904c 96 DEBUG("Char Properties 0x%x\n\r", p_char->getProperties());
sjallouli 0:a948f5f3904c 97 /*
sjallouli 0:a948f5f3904c 98 * Gatt_Evt_Mask -> HardCoded (0)
sjallouli 0:a948f5f3904c 99 * Encryption_Key_Size -> Hardcoded (16)
sjallouli 0:a948f5f3904c 100 * isVariable (variable length value field) -> Hardcoded (1)
sjallouli 0:a948f5f3904c 101 */
sjallouli 0:a948f5f3904c 102 tGattServerEvent Gatt_Evt_Mask = 0x0;
sjallouli 0:a948f5f3904c 103
sjallouli 0:a948f5f3904c 104 if((p_char->getProperties() &
sjallouli 0:a948f5f3904c 105 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
sjallouli 0:a948f5f3904c 106 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) {
sjallouli 0:a948f5f3904c 107 DEBUG("Setting up Gatt GATT_SERVER_ATTR_WRITE Mask\n\r");
sjallouli 0:a948f5f3904c 108 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_SERVER_ATTR_WRITE;
sjallouli 0:a948f5f3904c 109 }
sjallouli 0:a948f5f3904c 110 if((p_char->getProperties() &
sjallouli 0:a948f5f3904c 111 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ|
sjallouli 0:a948f5f3904c 112 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) {
sjallouli 0:a948f5f3904c 113 DEBUG("Setting up Gatt GATT_INTIMATE_APPL_WHEN_READ_N_WAIT Mask\n\r");
sjallouli 0:a948f5f3904c 114 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_INTIMATE_APPL_WHEN_READ_N_WAIT;
sjallouli 0:a948f5f3904c 115 } //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check.
sjallouli 0:a948f5f3904c 116
sjallouli 0:a948f5f3904c 117 if(type==UUID::UUID_TYPE_SHORT) {
sjallouli 0:a948f5f3904c 118 ret = aci_gatt_add_char(service.getHandle(), UUID_TYPE_16, int_8_uuid, p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
sjallouli 0:a948f5f3904c 119 p_char->getProperties(), ATTR_PERMISSION_NONE, Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
sjallouli 0:a948f5f3904c 120 16 /*Encryption_Key_Size*/, 1 /*isVariable*/, &bleCharacteristic);
sjallouli 0:a948f5f3904c 121 }
sjallouli 0:a948f5f3904c 122 else if(type==UUID::UUID_TYPE_LONG) {
sjallouli 0:a948f5f3904c 123 ret = aci_gatt_add_char(service.getHandle(), UUID_TYPE_128, char_base_uuid, p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
sjallouli 0:a948f5f3904c 124 p_char->getProperties(), ATTR_PERMISSION_NONE, Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
sjallouli 0:a948f5f3904c 125 16 /*Encryption_Key_Size*/, 1 /*isVariable*/, &bleCharacteristic);
sjallouli 0:a948f5f3904c 126 }
sjallouli 0:a948f5f3904c 127 /* Update the characteristic handle */
sjallouli 0:a948f5f3904c 128 uint16_t charHandle = characteristicCount;
sjallouli 0:a948f5f3904c 129
sjallouli 0:a948f5f3904c 130 bleCharHanldeMap.insert(std::pair<tHalUint16, tHalUint16>(bleCharacteristic, servHandle));
sjallouli 0:a948f5f3904c 131
sjallouli 0:a948f5f3904c 132 p_characteristics[characteristicCount++] = p_char;
sjallouli 0:a948f5f3904c 133 p_char->getValueAttribute().setHandle(bleCharacteristic); //Set the characteristic count as the corresponding char handle
sjallouli 0:a948f5f3904c 134 DEBUG("added bleCharacteristic handle =%u\n\r", bleCharacteristic);
sjallouli 0:a948f5f3904c 135
sjallouli 0:a948f5f3904c 136 if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) {
sjallouli 0:a948f5f3904c 137 updateValue(p_char->getValueAttribute().getHandle(), p_char->getValueAttribute().getValuePtr(), p_char->getValueAttribute().getInitialLength(), false /* localOnly */);
sjallouli 0:a948f5f3904c 138 }
sjallouli 0:a948f5f3904c 139
sjallouli 0:a948f5f3904c 140 // add descriptors now
sjallouli 0:a948f5f3904c 141 uint16_t descHandle = 0;
sjallouli 0:a948f5f3904c 142 for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) {
sjallouli 0:a948f5f3904c 143 GattAttribute *descriptor = p_char->getDescriptor(descIndex);
sjallouli 0:a948f5f3904c 144 uint16_t shortUUID = descriptor->getUUID().getShortUUID();
sjallouli 0:a948f5f3904c 145 const tHalUint8 uuidArray[] = {(shortUUID>>8)&0xFF, (shortUUID&0xFF)};
sjallouli 0:a948f5f3904c 146 ret = aci_gatt_add_char_desc(service.getHandle(), p_char->getValueAttribute().getHandle(),
sjallouli 0:a948f5f3904c 147 CHAR_DESC_TYPE_16_BIT, uuidArray, descriptor->getMaxLength(), descriptor->getInitialLength(),
sjallouli 0:a948f5f3904c 148 descriptor->getValuePtr(), CHAR_DESC_SECURITY_PERMISSION, CHAR_DESC_ACCESS_PERMISSION, GATT_SERVER_ATTR_READ_WRITE,
sjallouli 0:a948f5f3904c 149 MIN_ENCRY_KEY_SIZE, CHAR_ATTRIBUTE_LEN_IS_FIXED, &descHandle);
sjallouli 0:a948f5f3904c 150 if(ret==(tBleStatus)0) {
sjallouli 0:a948f5f3904c 151 DEBUG("Descriptor added successfully, descriptor handle=%d\n\r", descHandle);
sjallouli 0:a948f5f3904c 152 descriptor->setHandle(descHandle);
sjallouli 0:a948f5f3904c 153 }
sjallouli 0:a948f5f3904c 154 }
sjallouli 0:a948f5f3904c 155 }
sjallouli 0:a948f5f3904c 156
sjallouli 0:a948f5f3904c 157 serviceCount++;
sjallouli 0:a948f5f3904c 158
sjallouli 0:a948f5f3904c 159 //FIXME: There is no GattService pointer array in GattServer.
sjallouli 0:a948f5f3904c 160 // There should be one? (Only the user is aware of GattServices!) Report to forum.
sjallouli 0:a948f5f3904c 161
sjallouli 0:a948f5f3904c 162 return BLE_ERROR_NONE;
sjallouli 0:a948f5f3904c 163 }
sjallouli 0:a948f5f3904c 164
sjallouli 0:a948f5f3904c 165 /**************************************************************************/
sjallouli 0:a948f5f3904c 166 /*!
sjallouli 0:a948f5f3904c 167 @brief Reads the value of a characteristic, based on the service
sjallouli 0:a948f5f3904c 168 and characteristic index fields
sjallouli 0:a948f5f3904c 169
sjallouli 0:a948f5f3904c 170 @param[in] charHandle
sjallouli 0:a948f5f3904c 171 The handle of the GattCharacteristic to read from
sjallouli 0:a948f5f3904c 172 @param[in] buffer
sjallouli 0:a948f5f3904c 173 Buffer to hold the the characteristic's value
sjallouli 0:a948f5f3904c 174 (raw byte array in LSB format)
sjallouli 0:a948f5f3904c 175 @param[in] len
sjallouli 0:a948f5f3904c 176 The number of bytes read into the buffer
sjallouli 0:a948f5f3904c 177
sjallouli 0:a948f5f3904c 178 @returns ble_error_t
sjallouli 0:a948f5f3904c 179
sjallouli 0:a948f5f3904c 180 @retval BLE_ERROR_NONE
sjallouli 0:a948f5f3904c 181 Everything executed properly
sjallouli 0:a948f5f3904c 182
sjallouli 0:a948f5f3904c 183 @section EXAMPLE
sjallouli 0:a948f5f3904c 184
sjallouli 0:a948f5f3904c 185 @code
sjallouli 0:a948f5f3904c 186
sjallouli 0:a948f5f3904c 187 @endcode
sjallouli 0:a948f5f3904c 188 */
sjallouli 0:a948f5f3904c 189 /**************************************************************************/
sjallouli 0:a948f5f3904c 190 ble_error_t BlueNRGGattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP)
sjallouli 0:a948f5f3904c 191 {
sjallouli 0:a948f5f3904c 192 DEBUG("ReadValue() Not Supported\n\r");
sjallouli 0:a948f5f3904c 193 return BLE_ERROR_NONE;
sjallouli 0:a948f5f3904c 194 }
sjallouli 0:a948f5f3904c 195
sjallouli 0:a948f5f3904c 196 /**************************************************************************/
sjallouli 0:a948f5f3904c 197 /*!
sjallouli 0:a948f5f3904c 198 @brief Updates the value of a characteristic, based on the service
sjallouli 0:a948f5f3904c 199 and characteristic index fields
sjallouli 0:a948f5f3904c 200
sjallouli 0:a948f5f3904c 201 @param[in] charHandle
sjallouli 0:a948f5f3904c 202 The handle of the GattCharacteristic to write to
sjallouli 0:a948f5f3904c 203 @param[in] buffer
sjallouli 0:a948f5f3904c 204 Data to use when updating the characteristic's value
sjallouli 0:a948f5f3904c 205 (raw byte array in LSB format)
sjallouli 0:a948f5f3904c 206 @param[in] len
sjallouli 0:a948f5f3904c 207 The number of bytes in buffer
sjallouli 0:a948f5f3904c 208
sjallouli 0:a948f5f3904c 209 @returns ble_error_t
sjallouli 0:a948f5f3904c 210
sjallouli 0:a948f5f3904c 211 @retval BLE_ERROR_NONE
sjallouli 0:a948f5f3904c 212 Everything executed properly
sjallouli 0:a948f5f3904c 213
sjallouli 0:a948f5f3904c 214 @section EXAMPLE
sjallouli 0:a948f5f3904c 215
sjallouli 0:a948f5f3904c 216 @code
sjallouli 0:a948f5f3904c 217
sjallouli 0:a948f5f3904c 218 @endcode
sjallouli 0:a948f5f3904c 219 */
sjallouli 0:a948f5f3904c 220 /**************************************************************************/
sjallouli 0:a948f5f3904c 221 ble_error_t BlueNRGGattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly)
sjallouli 0:a948f5f3904c 222 {
sjallouli 0:a948f5f3904c 223 tBleStatus ret;
sjallouli 0:a948f5f3904c 224 tHalUint8 buff[2];
sjallouli 0:a948f5f3904c 225
sjallouli 0:a948f5f3904c 226 DEBUG("updating bleCharacteristic charHandle =%u, corresponding serviceHanle= %u\n\r", charHandle, bleCharHanldeMap.find(charHandle)->second);
sjallouli 0:a948f5f3904c 227
sjallouli 0:a948f5f3904c 228 ret = aci_gatt_update_char_value(bleCharHanldeMap.find(charHandle)->second, charHandle, 0, len, buffer);
sjallouli 0:a948f5f3904c 229
sjallouli 0:a948f5f3904c 230 if (ret != BLE_STATUS_SUCCESS){
sjallouli 0:a948f5f3904c 231 DEBUG("Error while updating characteristic.\n\r") ;
sjallouli 0:a948f5f3904c 232 return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value
sjallouli 0:a948f5f3904c 233 //FIXME: Define Error values equivalent to BlueNRG Error Codes.
sjallouli 0:a948f5f3904c 234 }
sjallouli 0:a948f5f3904c 235
sjallouli 0:a948f5f3904c 236 //Generate Data Sent Event Here? (GattServerEvents::GATT_EVENT_DATA_SENT) //FIXME: Is this correct?
sjallouli 0:a948f5f3904c 237 //Check if characteristic property is NOTIFY|INDICATE, if yes generate event
sjallouli 0:a948f5f3904c 238 GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(charHandle);
sjallouli 0:a948f5f3904c 239 if(p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
sjallouli 0:a948f5f3904c 240 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) {
sjallouli 0:a948f5f3904c 241 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT, charHandle);
sjallouli 0:a948f5f3904c 242 }
sjallouli 0:a948f5f3904c 243
sjallouli 0:a948f5f3904c 244 return BLE_ERROR_NONE;
sjallouli 0:a948f5f3904c 245 }
sjallouli 0:a948f5f3904c 246
sjallouli 0:a948f5f3904c 247 /**************************************************************************/
sjallouli 0:a948f5f3904c 248 /*!
sjallouli 0:a948f5f3904c 249 @brief Reads a value according to the handle provided
sjallouli 0:a948f5f3904c 250
sjallouli 0:a948f5f3904c 251 @param[in] charHandle
sjallouli 0:a948f5f3904c 252 The handle of the GattCharacteristic to read from
sjallouli 0:a948f5f3904c 253
sjallouli 0:a948f5f3904c 254 @returns ble_error_t
sjallouli 0:a948f5f3904c 255
sjallouli 0:a948f5f3904c 256 @retval BLE_ERROR_NONE
sjallouli 0:a948f5f3904c 257 Everything executed properly
sjallouli 0:a948f5f3904c 258
sjallouli 0:a948f5f3904c 259 @section EXAMPLE
sjallouli 0:a948f5f3904c 260
sjallouli 0:a948f5f3904c 261 @code
sjallouli 0:a948f5f3904c 262
sjallouli 0:a948f5f3904c 263 @endcode
sjallouli 0:a948f5f3904c 264 */
sjallouli 0:a948f5f3904c 265 /**************************************************************************/
sjallouli 0:a948f5f3904c 266 ble_error_t BlueNRGGattServer::Read_Request_CB(tHalUint16 handle)
sjallouli 0:a948f5f3904c 267 {
sjallouli 0:a948f5f3904c 268 //signed short refvalue;
sjallouli 0:a948f5f3904c 269 uint16_t gapConnectionHandle = BlueNRGGap::getInstance().getConnectionHandle();
sjallouli 0:a948f5f3904c 270
sjallouli 0:a948f5f3904c 271 //EXIT:
sjallouli 0:a948f5f3904c 272 if(gapConnectionHandle != 0)
sjallouli 0:a948f5f3904c 273 aci_gatt_allow_read(gapConnectionHandle);
sjallouli 0:a948f5f3904c 274 }
sjallouli 0:a948f5f3904c 275
sjallouli 0:a948f5f3904c 276 /**************************************************************************/
sjallouli 0:a948f5f3904c 277 /*!
sjallouli 0:a948f5f3904c 278 @brief Returns the GattCharacteristic according to the handle provided
sjallouli 0:a948f5f3904c 279
sjallouli 0:a948f5f3904c 280 @param[in] charHandle
sjallouli 0:a948f5f3904c 281 The handle of the GattCharacteristic
sjallouli 0:a948f5f3904c 282
sjallouli 0:a948f5f3904c 283 @returns ble_error_t
sjallouli 0:a948f5f3904c 284
sjallouli 0:a948f5f3904c 285 @retval BLE_ERROR_NONE
sjallouli 0:a948f5f3904c 286 Everything executed properly
sjallouli 0:a948f5f3904c 287
sjallouli 0:a948f5f3904c 288 @section EXAMPLE
sjallouli 0:a948f5f3904c 289
sjallouli 0:a948f5f3904c 290 @code
sjallouli 0:a948f5f3904c 291
sjallouli 0:a948f5f3904c 292 @endcode
sjallouli 0:a948f5f3904c 293 */
sjallouli 0:a948f5f3904c 294 /**************************************************************************/
sjallouli 0:a948f5f3904c 295 GattCharacteristic* BlueNRGGattServer::getCharacteristicFromHandle(tHalUint16 attrHandle)
sjallouli 0:a948f5f3904c 296 {
sjallouli 0:a948f5f3904c 297 GattCharacteristic *p_char = NULL;
sjallouli 0:a948f5f3904c 298 int i;
sjallouli 0:a948f5f3904c 299 uint16_t handle;
sjallouli 0:a948f5f3904c 300
sjallouli 0:a948f5f3904c 301 //DEBUG("BlueNRGGattServer::getCharacteristicFromHandle()>>Attribute Handle received 0x%x\n\r",attrHandle);
sjallouli 0:a948f5f3904c 302 for(i=0; i<characteristicCount; i++)
sjallouli 0:a948f5f3904c 303 {
sjallouli 0:a948f5f3904c 304 handle = p_characteristics[i]->getValueAttribute().getHandle();
sjallouli 0:a948f5f3904c 305
sjallouli 0:a948f5f3904c 306 if(i==characteristicCount-1)//Last Characteristic check
sjallouli 0:a948f5f3904c 307 {
sjallouli 0:a948f5f3904c 308 if(attrHandle>=bleCharacteristicHandles[handle])
sjallouli 0:a948f5f3904c 309 {
sjallouli 0:a948f5f3904c 310 p_char = p_characteristics[i];
sjallouli 0:a948f5f3904c 311 DEBUG("Found Characteristic Properties 0x%x\n\r",p_char->getProperties());
sjallouli 0:a948f5f3904c 312 break;
sjallouli 0:a948f5f3904c 313 }
sjallouli 0:a948f5f3904c 314 }
sjallouli 0:a948f5f3904c 315 else {
sjallouli 0:a948f5f3904c 316 //Testing if attribute handle is between two Characteristic Handles
sjallouli 0:a948f5f3904c 317 if(attrHandle>=bleCharacteristicHandles[handle] && attrHandle<bleCharacteristicHandles[handle+1])
sjallouli 0:a948f5f3904c 318 {
sjallouli 0:a948f5f3904c 319 p_char = p_characteristics[i];
sjallouli 0:a948f5f3904c 320 //DEBUG("Found Characteristic Properties 0x%x\n\r",p_char->getProperties());
sjallouli 0:a948f5f3904c 321 break;
sjallouli 0:a948f5f3904c 322 } else continue;
sjallouli 0:a948f5f3904c 323 }
sjallouli 0:a948f5f3904c 324 }
sjallouli 0:a948f5f3904c 325
sjallouli 0:a948f5f3904c 326 return p_char;
sjallouli 0:a948f5f3904c 327 }
sjallouli 0:a948f5f3904c 328
sjallouli 0:a948f5f3904c 329