ver:init

Committer:
iv123
Date:
Sun Jun 18 16:10:28 2017 +0000
Revision:
0:88b85febcb45
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iv123 0:88b85febcb45 1 /* mbed Microcontroller Library
iv123 0:88b85febcb45 2 * Copyright (c) 2006-2013 ARM Limited
iv123 0:88b85febcb45 3 *
iv123 0:88b85febcb45 4 * Licensed under the Apache License, Version 2.0 (the "License");
iv123 0:88b85febcb45 5 * you may not use this file except in compliance with the License.
iv123 0:88b85febcb45 6 * You may obtain a copy of the License at
iv123 0:88b85febcb45 7 *
iv123 0:88b85febcb45 8 * http://www.apache.org/licenses/LICENSE-2.0
iv123 0:88b85febcb45 9 *
iv123 0:88b85febcb45 10 * Unless required by applicable law or agreed to in writing, software
iv123 0:88b85febcb45 11 * distributed under the License is distributed on an "AS IS" BASIS,
iv123 0:88b85febcb45 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
iv123 0:88b85febcb45 13 * See the License for the specific language governing permissions and
iv123 0:88b85febcb45 14 * limitations under the License.
iv123 0:88b85febcb45 15 */
iv123 0:88b85febcb45 16 /**
iv123 0:88b85febcb45 17 ******************************************************************************
iv123 0:88b85febcb45 18 * @file BlueNRGGattServer.cpp
iv123 0:88b85febcb45 19 * @author STMicroelectronics
iv123 0:88b85febcb45 20 * @brief Implementation of BlueNRG BLE_API GattServer Class
iv123 0:88b85febcb45 21 ******************************************************************************
iv123 0:88b85febcb45 22 * @copy
iv123 0:88b85febcb45 23 *
iv123 0:88b85febcb45 24 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
iv123 0:88b85febcb45 25 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
iv123 0:88b85febcb45 26 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
iv123 0:88b85febcb45 27 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
iv123 0:88b85febcb45 28 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
iv123 0:88b85febcb45 29 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
iv123 0:88b85febcb45 30 *
iv123 0:88b85febcb45 31 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
iv123 0:88b85febcb45 32 */
iv123 0:88b85febcb45 33
iv123 0:88b85febcb45 34 /** @defgroup BlueNRGGATTSERVER
iv123 0:88b85febcb45 35 * @brief BlueNRG BLE_API GattServer Adaptation
iv123 0:88b85febcb45 36 * @{
iv123 0:88b85febcb45 37 */
iv123 0:88b85febcb45 38
iv123 0:88b85febcb45 39 #include "BlueNRGGattServer.h"
iv123 0:88b85febcb45 40 #ifdef YOTTA_CFG_MBED_OS
iv123 0:88b85febcb45 41 #include "mbed-drivers/mbed.h"
iv123 0:88b85febcb45 42 #else
iv123 0:88b85febcb45 43 #include "mbed.h"
iv123 0:88b85febcb45 44 #endif
iv123 0:88b85febcb45 45 #include "BlueNRGGap.h"
iv123 0:88b85febcb45 46 #include "ble_utils.h"
iv123 0:88b85febcb45 47 #include "ble_debug.h"
iv123 0:88b85febcb45 48
iv123 0:88b85febcb45 49 /**************************************************************************/
iv123 0:88b85febcb45 50 /*!
iv123 0:88b85febcb45 51 @brief Adds a new service to the GATT table on the peripheral
iv123 0:88b85febcb45 52
iv123 0:88b85febcb45 53 @params[in] service
iv123 0:88b85febcb45 54 Pointer to instance of the Gatt Server to add
iv123 0:88b85febcb45 55
iv123 0:88b85febcb45 56 @returns ble_error_t
iv123 0:88b85febcb45 57
iv123 0:88b85febcb45 58 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 59 Everything executed properly
iv123 0:88b85febcb45 60
iv123 0:88b85febcb45 61 @section EXAMPLE
iv123 0:88b85febcb45 62
iv123 0:88b85febcb45 63 @code
iv123 0:88b85febcb45 64
iv123 0:88b85febcb45 65 @endcode
iv123 0:88b85febcb45 66 */
iv123 0:88b85febcb45 67 /**************************************************************************/
iv123 0:88b85febcb45 68 ble_error_t BlueNRGGattServer::addService(GattService &service)
iv123 0:88b85febcb45 69 {
iv123 0:88b85febcb45 70 /* ToDo: Make sure we don't overflow the array, etc. */
iv123 0:88b85febcb45 71 /* ToDo: Make sure this service UUID doesn't already exist (?) */
iv123 0:88b85febcb45 72 /* ToDo: Basic validation */
iv123 0:88b85febcb45 73
iv123 0:88b85febcb45 74 tBleStatus ret;
iv123 0:88b85febcb45 75 uint8_t type;
iv123 0:88b85febcb45 76 uint16_t short_uuid;
iv123 0:88b85febcb45 77 uint8_t primary_short_uuid[2];
iv123 0:88b85febcb45 78 uint8_t primary_base_uuid[16];
iv123 0:88b85febcb45 79 uint8_t char_base_uuid[16];
iv123 0:88b85febcb45 80 const uint8_t *base_uuid;
iv123 0:88b85febcb45 81 const uint8_t *base_char_uuid;
iv123 0:88b85febcb45 82
iv123 0:88b85febcb45 83 uint8_t charsCount = service.getCharacteristicCount();
iv123 0:88b85febcb45 84 const uint8_t available_characteristics = BLE_TOTAL_CHARACTERISTICS - characteristicCount;
iv123 0:88b85febcb45 85
iv123 0:88b85febcb45 86 // check that there is enough characteristics left in the
iv123 0:88b85febcb45 87 // characteristic array.
iv123 0:88b85febcb45 88 if (charsCount > available_characteristics) {
iv123 0:88b85febcb45 89 PRINTF("charCount = %u and characteristicCount = %u\r\n", charsCount, available_characteristics);
iv123 0:88b85febcb45 90 return BLE_ERROR_NO_MEM;
iv123 0:88b85febcb45 91 }
iv123 0:88b85febcb45 92
iv123 0:88b85febcb45 93 const uint16_t maxAttrRecords = computeAttributesRecord(service);
iv123 0:88b85febcb45 94
iv123 0:88b85febcb45 95 type = (service.getUUID()).shortOrLong();
iv123 0:88b85febcb45 96 PRINTF("AddService(): Type:%d\n\r", type);
iv123 0:88b85febcb45 97
iv123 0:88b85febcb45 98 /* Add the service to the BlueNRG */
iv123 0:88b85febcb45 99 short_uuid = (service.getUUID()).getShortUUID();
iv123 0:88b85febcb45 100 STORE_LE_16(primary_short_uuid, short_uuid);
iv123 0:88b85febcb45 101
iv123 0:88b85febcb45 102 if(type==UUID::UUID_TYPE_LONG) {
iv123 0:88b85febcb45 103 base_uuid = (service.getUUID()).getBaseUUID();
iv123 0:88b85febcb45 104
iv123 0:88b85febcb45 105 COPY_UUID_128(primary_base_uuid, base_uuid[15],base_uuid[14],primary_short_uuid[1],primary_short_uuid[0],base_uuid[11],base_uuid[10],base_uuid[9],base_uuid[8],base_uuid[7],base_uuid[6],base_uuid[5],base_uuid[4],base_uuid[3],base_uuid[2],base_uuid[1],base_uuid[0]);
iv123 0:88b85febcb45 106 }
iv123 0:88b85febcb45 107
iv123 0:88b85febcb45 108 ret = BLE_STATUS_SUCCESS;
iv123 0:88b85febcb45 109
iv123 0:88b85febcb45 110 if(type==UUID::UUID_TYPE_SHORT) {
iv123 0:88b85febcb45 111 ret = aci_gatt_add_serv(UUID_TYPE_16,
iv123 0:88b85febcb45 112 primary_short_uuid,
iv123 0:88b85febcb45 113 PRIMARY_SERVICE,
iv123 0:88b85febcb45 114 maxAttrRecords/*7*/,
iv123 0:88b85febcb45 115 &servHandle);
iv123 0:88b85febcb45 116 PRINTF("aci_gatt_add_serv UUID_TYPE_SHORT ret=%d\n\r", ret);
iv123 0:88b85febcb45 117
iv123 0:88b85febcb45 118 } else if(type==UUID::UUID_TYPE_LONG) {
iv123 0:88b85febcb45 119 ret = aci_gatt_add_serv(UUID_TYPE_128,
iv123 0:88b85febcb45 120 primary_base_uuid,
iv123 0:88b85febcb45 121 PRIMARY_SERVICE,
iv123 0:88b85febcb45 122 maxAttrRecords/*7*/,
iv123 0:88b85febcb45 123 &servHandle);
iv123 0:88b85febcb45 124 PRINTF("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret);
iv123 0:88b85febcb45 125 }
iv123 0:88b85febcb45 126
iv123 0:88b85febcb45 127 switch (ret) {
iv123 0:88b85febcb45 128 case BLE_STATUS_SUCCESS:
iv123 0:88b85febcb45 129 break;
iv123 0:88b85febcb45 130
iv123 0:88b85febcb45 131 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 132 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 133
iv123 0:88b85febcb45 134 case BLE_STATUS_OUT_OF_HANDLE:
iv123 0:88b85febcb45 135 case BLE_STATUS_INSUFFICIENT_RESOURCES:
iv123 0:88b85febcb45 136 case ERR_UNSPECIFIED_ERROR:
iv123 0:88b85febcb45 137 return BLE_ERROR_NO_MEM;
iv123 0:88b85febcb45 138
iv123 0:88b85febcb45 139 case BLE_STATUS_ERROR:
iv123 0:88b85febcb45 140 default:
iv123 0:88b85febcb45 141 return BLE_ERROR_INTERNAL_STACK_FAILURE;
iv123 0:88b85febcb45 142 }
iv123 0:88b85febcb45 143
iv123 0:88b85febcb45 144
iv123 0:88b85febcb45 145
iv123 0:88b85febcb45 146 service.setHandle(servHandle);
iv123 0:88b85febcb45 147 //serviceHandleVector.push_back(servHandle);
iv123 0:88b85febcb45 148 PRINTF("added servHandle handle =%u\n\r", servHandle);
iv123 0:88b85febcb45 149 uint16_t bleCharacteristic;
iv123 0:88b85febcb45 150
iv123 0:88b85febcb45 151 //iterate to include all characteristics
iv123 0:88b85febcb45 152 for (uint8_t i = 0; i < charsCount; i++) {
iv123 0:88b85febcb45 153 GattCharacteristic *p_char = service.getCharacteristic(i);
iv123 0:88b85febcb45 154 uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID();
iv123 0:88b85febcb45 155
iv123 0:88b85febcb45 156 uint8_t int_8_uuid[2];
iv123 0:88b85febcb45 157 STORE_LE_16(int_8_uuid, char_uuid);
iv123 0:88b85febcb45 158
iv123 0:88b85febcb45 159 type = (p_char->getValueAttribute().getUUID()).shortOrLong();
iv123 0:88b85febcb45 160
iv123 0:88b85febcb45 161 if(type==UUID::UUID_TYPE_LONG) {
iv123 0:88b85febcb45 162 base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID();
iv123 0:88b85febcb45 163 #ifdef DEBUG
iv123 0:88b85febcb45 164 for(uint8_t j=0; j<16; j++) {
iv123 0:88b85febcb45 165 PRINTF("base_char_uuid[%d] 0x%02x ", j, base_char_uuid[j]);
iv123 0:88b85febcb45 166 }
iv123 0:88b85febcb45 167 PRINTF("\n\r");
iv123 0:88b85febcb45 168 #endif
iv123 0:88b85febcb45 169 COPY_UUID_128(char_base_uuid,base_char_uuid[15],base_char_uuid[14],int_8_uuid[1],int_8_uuid[0],base_char_uuid[11],base_char_uuid[10],base_char_uuid[9],base_char_uuid[8],base_char_uuid[7],base_char_uuid[6],base_char_uuid[5],base_char_uuid[4],base_char_uuid[3],base_char_uuid[2],base_char_uuid[1],base_char_uuid[0]);
iv123 0:88b85febcb45 170 }
iv123 0:88b85febcb45 171
iv123 0:88b85febcb45 172 PRINTF("Char Properties 0x%x\n\r", p_char->getProperties());
iv123 0:88b85febcb45 173 /*
iv123 0:88b85febcb45 174 * Gatt_Evt_Mask -> HardCoded (0)
iv123 0:88b85febcb45 175 * Encryption_Key_Size -> Hardcoded (16)
iv123 0:88b85febcb45 176 * isVariable (variable length value field) -> Hardcoded (1)
iv123 0:88b85febcb45 177 */
iv123 0:88b85febcb45 178 uint8_t Gatt_Evt_Mask = 0x0;
iv123 0:88b85febcb45 179
iv123 0:88b85febcb45 180 if((p_char->getProperties() &
iv123 0:88b85febcb45 181 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
iv123 0:88b85febcb45 182 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) {
iv123 0:88b85febcb45 183 PRINTF("Setting up Gatt GATT_NOTIFY_ATTRIBUTE_WRITE Mask\n\r");
iv123 0:88b85febcb45 184 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_ATTRIBUTE_WRITE | GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP;
iv123 0:88b85febcb45 185 }
iv123 0:88b85febcb45 186 if((p_char->getProperties() &
iv123 0:88b85febcb45 187 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ|
iv123 0:88b85febcb45 188 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) {
iv123 0:88b85febcb45 189 PRINTF("Setting up Gatt GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP Mask\n\r");
iv123 0:88b85febcb45 190 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP;
iv123 0:88b85febcb45 191 } //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check.
iv123 0:88b85febcb45 192
iv123 0:88b85febcb45 193 if(type==UUID::UUID_TYPE_SHORT) {
iv123 0:88b85febcb45 194 ret = aci_gatt_add_char(service.getHandle(),
iv123 0:88b85febcb45 195 UUID_TYPE_16,
iv123 0:88b85febcb45 196 int_8_uuid,
iv123 0:88b85febcb45 197 p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
iv123 0:88b85febcb45 198 p_char->getProperties(),
iv123 0:88b85febcb45 199 ATTR_PERMISSION_NONE,
iv123 0:88b85febcb45 200 Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
iv123 0:88b85febcb45 201 16 /*Encryption_Key_Size*/,
iv123 0:88b85febcb45 202 1 /*isVariable*/,
iv123 0:88b85febcb45 203 &bleCharacteristic);
iv123 0:88b85febcb45 204
iv123 0:88b85febcb45 205 PRINTF("aci_gatt_add_char UUID_TYPE_16 props=%d MaxLength=%d ret=%d\n\r",
iv123 0:88b85febcb45 206 p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);
iv123 0:88b85febcb45 207
iv123 0:88b85febcb45 208 } else if(type==UUID::UUID_TYPE_LONG) {
iv123 0:88b85febcb45 209 ret = aci_gatt_add_char(service.getHandle(),
iv123 0:88b85febcb45 210 UUID_TYPE_128,
iv123 0:88b85febcb45 211 char_base_uuid,
iv123 0:88b85febcb45 212 p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
iv123 0:88b85febcb45 213 p_char->getProperties(),
iv123 0:88b85febcb45 214 ATTR_PERMISSION_NONE,
iv123 0:88b85febcb45 215 Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
iv123 0:88b85febcb45 216 16 /*Encryption_Key_Size*/,
iv123 0:88b85febcb45 217 1 /*isVariable*/,
iv123 0:88b85febcb45 218 &bleCharacteristic);
iv123 0:88b85febcb45 219
iv123 0:88b85febcb45 220 PRINTF("aci_gatt_add_char UUID_TYPE_128 props=%d MaxLength=%d ret=%d\n\r",
iv123 0:88b85febcb45 221 p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);
iv123 0:88b85febcb45 222 }
iv123 0:88b85febcb45 223
iv123 0:88b85febcb45 224 switch (ret) {
iv123 0:88b85febcb45 225 case BLE_STATUS_SUCCESS:
iv123 0:88b85febcb45 226 break;
iv123 0:88b85febcb45 227
iv123 0:88b85febcb45 228 case ERR_UNSPECIFIED_ERROR:
iv123 0:88b85febcb45 229 case BLE_STATUS_INSUFFICIENT_RESOURCES:
iv123 0:88b85febcb45 230 case BLE_STATUS_OUT_OF_HANDLE:
iv123 0:88b85febcb45 231 // TODO remove characteristics and the service previously added.
iv123 0:88b85febcb45 232 // remove service in the stack by using: Aci_Gatt_Del_Service
iv123 0:88b85febcb45 233 // remove characteristics in the stack by using: Aci_Gatt_Del_Char
iv123 0:88b85febcb45 234 // update service counter
iv123 0:88b85febcb45 235 // destroy registered characteristic and updat echaracteristic counter
iv123 0:88b85febcb45 236 return BLE_ERROR_NO_MEM;
iv123 0:88b85febcb45 237
iv123 0:88b85febcb45 238 case BLE_STATUS_INVALID_HANDLE:
iv123 0:88b85febcb45 239 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 240 case BLE_STATUS_CHARAC_ALREADY_EXISTS:
iv123 0:88b85febcb45 241 // TODO remove characteristics and the service previously added.
iv123 0:88b85febcb45 242 // remove service in the stack by using: Aci_Gatt_Del_Service
iv123 0:88b85febcb45 243 // remove characteristics in the stack by using: Aci_Gatt_Del_Char
iv123 0:88b85febcb45 244 // update service counter
iv123 0:88b85febcb45 245 // destroy registered characteristic and updat echaracteristic counter
iv123 0:88b85febcb45 246 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 247
iv123 0:88b85febcb45 248 case BLE_STATUS_ERROR:
iv123 0:88b85febcb45 249 default:
iv123 0:88b85febcb45 250 // TODO remove characteristics and the service previously added.
iv123 0:88b85febcb45 251 // remove service in the stack by using: Aci_Gatt_Del_Service
iv123 0:88b85febcb45 252 // remove characteristics in the stack by using: Aci_Gatt_Del_Char
iv123 0:88b85febcb45 253 // update service counter
iv123 0:88b85febcb45 254 // destroy registered characteristic and updat echaracteristic counter
iv123 0:88b85febcb45 255 return BLE_ERROR_INTERNAL_STACK_FAILURE;
iv123 0:88b85febcb45 256 }
iv123 0:88b85febcb45 257
iv123 0:88b85febcb45 258 bleCharHandleMap.insert(std::pair<uint16_t, uint16_t>(bleCharacteristic, servHandle));
iv123 0:88b85febcb45 259
iv123 0:88b85febcb45 260 p_characteristics[characteristicCount++] = p_char;
iv123 0:88b85febcb45 261 /* Set the characteristic value handle */
iv123 0:88b85febcb45 262 p_char->getValueAttribute().setHandle(bleCharacteristic+BlueNRGGattServer::CHAR_VALUE_HANDLE);
iv123 0:88b85febcb45 263 PRINTF("added bleCharacteristic (value handle =%u)\n\r", p_char->getValueAttribute().getHandle());
iv123 0:88b85febcb45 264
iv123 0:88b85febcb45 265 if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getLength() > 0)) {
iv123 0:88b85febcb45 266 ble_error_t err = write(p_char->getValueAttribute().getHandle(),
iv123 0:88b85febcb45 267 p_char->getValueAttribute().getValuePtr(),
iv123 0:88b85febcb45 268 p_char->getValueAttribute().getLength(), false /* localOnly */);
iv123 0:88b85febcb45 269 if (err) {
iv123 0:88b85febcb45 270 PRINTF("ERROR HERE !!!!\r\n");
iv123 0:88b85febcb45 271 return err;
iv123 0:88b85febcb45 272 }
iv123 0:88b85febcb45 273 }
iv123 0:88b85febcb45 274
iv123 0:88b85febcb45 275 // add descriptors now
iv123 0:88b85febcb45 276 uint16_t descHandle = 0;
iv123 0:88b85febcb45 277 PRINTF("p_char->getDescriptorCount()=%d\n\r", p_char->getDescriptorCount());
iv123 0:88b85febcb45 278
iv123 0:88b85febcb45 279 for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) {
iv123 0:88b85febcb45 280 GattAttribute *descriptor = p_char->getDescriptor(descIndex);
iv123 0:88b85febcb45 281 uint8_t desc_uuid[16] = { 0 };
iv123 0:88b85febcb45 282
iv123 0:88b85febcb45 283
iv123 0:88b85febcb45 284 uint8_t desc_uuid_type = CHAR_DESC_TYPE_16_BIT;
iv123 0:88b85febcb45 285 STORE_LE_16(desc_uuid, descriptor->getUUID().getShortUUID());
iv123 0:88b85febcb45 286
iv123 0:88b85febcb45 287 if((descriptor->getUUID()).shortOrLong() == UUID::UUID_TYPE_LONG) {
iv123 0:88b85febcb45 288 desc_uuid_type = CHAR_DESC_TYPE_128_BIT;
iv123 0:88b85febcb45 289 const uint8_t* base_desc_uuid = descriptor->getUUID().getBaseUUID();
iv123 0:88b85febcb45 290
iv123 0:88b85febcb45 291 COPY_UUID_128(desc_uuid, base_desc_uuid[15], base_desc_uuid[14],base_desc_uuid[13],base_desc_uuid[12], base_desc_uuid[11], base_desc_uuid[10], base_desc_uuid[9], base_desc_uuid[8], base_desc_uuid[7], base_desc_uuid[6], base_desc_uuid[5], base_desc_uuid[4], base_desc_uuid[3], base_desc_uuid[2], base_desc_uuid[1], base_desc_uuid[0]);
iv123 0:88b85febcb45 292 }
iv123 0:88b85febcb45 293
iv123 0:88b85febcb45 294 ret = aci_gatt_add_char_desc(service.getHandle(),
iv123 0:88b85febcb45 295 bleCharacteristic,
iv123 0:88b85febcb45 296 desc_uuid_type,
iv123 0:88b85febcb45 297 desc_uuid,
iv123 0:88b85febcb45 298 descriptor->getMaxLength(),
iv123 0:88b85febcb45 299 descriptor->getLength(),
iv123 0:88b85febcb45 300 descriptor->getValuePtr(),
iv123 0:88b85febcb45 301 CHAR_DESC_SECURITY_PERMISSION,
iv123 0:88b85febcb45 302 CHAR_DESC_ACCESS_PERMISSION,
iv123 0:88b85febcb45 303 GATT_NOTIFY_ATTRIBUTE_WRITE,
iv123 0:88b85febcb45 304 MIN_ENCRY_KEY_SIZE,
iv123 0:88b85febcb45 305 CHAR_ATTRIBUTE_LEN_IS_FIXED,
iv123 0:88b85febcb45 306 &descHandle);
iv123 0:88b85febcb45 307 PRINTF("Adding Descriptor descriptor handle=%d ret=%d\n\r", descHandle, ret);
iv123 0:88b85febcb45 308
iv123 0:88b85febcb45 309 switch (ret) {
iv123 0:88b85febcb45 310 case BLE_STATUS_SUCCESS:
iv123 0:88b85febcb45 311 PRINTF("Descriptor added successfully, descriptor handle=%d\n\r", descHandle);
iv123 0:88b85febcb45 312 descriptor->setHandle(descHandle);
iv123 0:88b85febcb45 313 break;
iv123 0:88b85febcb45 314
iv123 0:88b85febcb45 315 case ERR_UNSPECIFIED_ERROR:
iv123 0:88b85febcb45 316 case BLE_STATUS_INSUFFICIENT_RESOURCES:
iv123 0:88b85febcb45 317 case BLE_STATUS_OUT_OF_HANDLE:
iv123 0:88b85febcb45 318 // TODO remove characteristics and the service previously added.
iv123 0:88b85febcb45 319 // remove service in the stack by using: Aci_Gatt_Del_Service
iv123 0:88b85febcb45 320 // remove characteristics in the stack by using: Aci_Gatt_Del_Char
iv123 0:88b85febcb45 321 // update service counter
iv123 0:88b85febcb45 322 // destroy registered characteristic and updat echaracteristic counter
iv123 0:88b85febcb45 323 return BLE_ERROR_NO_MEM;
iv123 0:88b85febcb45 324
iv123 0:88b85febcb45 325 case BLE_STATUS_INVALID_HANDLE:
iv123 0:88b85febcb45 326 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 327 // TODO remove characteristics and the service previously added.
iv123 0:88b85febcb45 328 // remove service in the stack by using: Aci_Gatt_Del_Service
iv123 0:88b85febcb45 329 // remove characteristics in the stack by using: Aci_Gatt_Del_Char
iv123 0:88b85febcb45 330 // update service counter
iv123 0:88b85febcb45 331 // destroy registered characteristic and updat echaracteristic counter
iv123 0:88b85febcb45 332 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 333
iv123 0:88b85febcb45 334 case BLE_STATUS_INVALID_OPERATION:
iv123 0:88b85febcb45 335 return BLE_ERROR_OPERATION_NOT_PERMITTED;
iv123 0:88b85febcb45 336
iv123 0:88b85febcb45 337 case BLE_STATUS_ERROR:
iv123 0:88b85febcb45 338 default:
iv123 0:88b85febcb45 339 // TODO remove characteristics and the service previously added.
iv123 0:88b85febcb45 340 // remove service in the stack by using: Aci_Gatt_Del_Service
iv123 0:88b85febcb45 341 // remove characteristics in the stack by using: Aci_Gatt_Del_Char
iv123 0:88b85febcb45 342 // update service counter
iv123 0:88b85febcb45 343 // destroy registered characteristic and updat echaracteristic counter
iv123 0:88b85febcb45 344 return BLE_ERROR_INTERNAL_STACK_FAILURE;
iv123 0:88b85febcb45 345 }
iv123 0:88b85febcb45 346 }
iv123 0:88b85febcb45 347 }
iv123 0:88b85febcb45 348
iv123 0:88b85febcb45 349 serviceCount++;
iv123 0:88b85febcb45 350
iv123 0:88b85febcb45 351 //FIXME: There is no GattService pointer array in GattServer.
iv123 0:88b85febcb45 352 // There should be one? (Only the user is aware of GattServices!) Report to forum.
iv123 0:88b85febcb45 353
iv123 0:88b85febcb45 354 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 355 }
iv123 0:88b85febcb45 356
iv123 0:88b85febcb45 357 /**************************************************************************/
iv123 0:88b85febcb45 358 /*!
iv123 0:88b85febcb45 359 @brief Reads the value of a characteristic, based on char handle
iv123 0:88b85febcb45 360
iv123 0:88b85febcb45 361 @param[in] attributeHandle
iv123 0:88b85febcb45 362 The handle of the GattCharacteristic to read from
iv123 0:88b85febcb45 363 @param[in] buffer
iv123 0:88b85febcb45 364 Buffer to hold the the characteristic's value
iv123 0:88b85febcb45 365 (raw byte array in LSB format)
iv123 0:88b85febcb45 366 @param[in] lengthP
iv123 0:88b85febcb45 367 The number of bytes read into the buffer
iv123 0:88b85febcb45 368
iv123 0:88b85febcb45 369 @returns ble_error_t
iv123 0:88b85febcb45 370
iv123 0:88b85febcb45 371 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 372 Everything executed properly
iv123 0:88b85febcb45 373
iv123 0:88b85febcb45 374 @section EXAMPLE
iv123 0:88b85febcb45 375
iv123 0:88b85febcb45 376 @code
iv123 0:88b85febcb45 377
iv123 0:88b85febcb45 378 @endcode
iv123 0:88b85febcb45 379 */
iv123 0:88b85febcb45 380 /**************************************************************************/
iv123 0:88b85febcb45 381 ble_error_t BlueNRGGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
iv123 0:88b85febcb45 382 {
iv123 0:88b85febcb45 383 tBleStatus ret;
iv123 0:88b85febcb45 384 uint16_t charHandle = attributeHandle;
iv123 0:88b85febcb45 385
iv123 0:88b85febcb45 386 ret = aci_gatt_read_handle_value(charHandle, *lengthP, lengthP, buffer);
iv123 0:88b85febcb45 387
iv123 0:88b85febcb45 388 if(ret == BLE_STATUS_SUCCESS) {
iv123 0:88b85febcb45 389 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 390 }
iv123 0:88b85febcb45 391 switch (ret) {
iv123 0:88b85febcb45 392 case ERR_INVALID_HCI_CMD_PARAMS:
iv123 0:88b85febcb45 393 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 394 default:
iv123 0:88b85febcb45 395 return BLE_ERROR_UNSPECIFIED;
iv123 0:88b85febcb45 396 }
iv123 0:88b85febcb45 397 }
iv123 0:88b85febcb45 398
iv123 0:88b85febcb45 399 /**************************************************************************/
iv123 0:88b85febcb45 400 /*!
iv123 0:88b85febcb45 401 @brief Reads the value of a characteristic, based on the connection
iv123 0:88b85febcb45 402 and char handle
iv123 0:88b85febcb45 403
iv123 0:88b85febcb45 404 @param[in] connectionHandle
iv123 0:88b85febcb45 405 The handle of the connection
iv123 0:88b85febcb45 406 @param[in] attributeHandle
iv123 0:88b85febcb45 407 The handle of the GattCharacteristic to write to
iv123 0:88b85febcb45 408 @param[in] buffer
iv123 0:88b85febcb45 409 Data to use when updating the characteristic's value
iv123 0:88b85febcb45 410 (raw byte array in LSB format)
iv123 0:88b85febcb45 411 @param[in] lengthP
iv123 0:88b85febcb45 412 The number of bytes in buffer
iv123 0:88b85febcb45 413
iv123 0:88b85febcb45 414 @returns ble_error_t
iv123 0:88b85febcb45 415
iv123 0:88b85febcb45 416 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 417 Everything executed properly
iv123 0:88b85febcb45 418
iv123 0:88b85febcb45 419 @section EXAMPLE
iv123 0:88b85febcb45 420
iv123 0:88b85febcb45 421 @code
iv123 0:88b85febcb45 422
iv123 0:88b85febcb45 423 @endcode
iv123 0:88b85febcb45 424 */
iv123 0:88b85febcb45 425 /**************************************************************************/
iv123 0:88b85febcb45 426 ble_error_t BlueNRGGattServer::read(Gap::Handle_t connectionHandle,
iv123 0:88b85febcb45 427 GattAttribute::Handle_t attributeHandle,
iv123 0:88b85febcb45 428 uint8_t buffer[],
iv123 0:88b85febcb45 429 uint16_t *lengthP) {
iv123 0:88b85febcb45 430
iv123 0:88b85febcb45 431 /* avoid compiler warnings about unused variables */
iv123 0:88b85febcb45 432 (void)connectionHandle;
iv123 0:88b85febcb45 433 (void)attributeHandle;
iv123 0:88b85febcb45 434 (void)buffer;
iv123 0:88b85febcb45 435 (void)lengthP;
iv123 0:88b85febcb45 436
iv123 0:88b85febcb45 437 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 438 }
iv123 0:88b85febcb45 439
iv123 0:88b85febcb45 440 ble_error_t BlueNRGGattServer::write(Gap::Handle_t connectionHandle,
iv123 0:88b85febcb45 441 GattAttribute::Handle_t,
iv123 0:88b85febcb45 442 const uint8_t[],
iv123 0:88b85febcb45 443 uint16_t, bool localOnly) {
iv123 0:88b85febcb45 444 /* avoid compiler warnings about unused variables */
iv123 0:88b85febcb45 445 (void)connectionHandle;
iv123 0:88b85febcb45 446 (void)localOnly;
iv123 0:88b85febcb45 447
iv123 0:88b85febcb45 448 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 449 }
iv123 0:88b85febcb45 450
iv123 0:88b85febcb45 451 ble_error_t BlueNRGGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
iv123 0:88b85febcb45 452 {
iv123 0:88b85febcb45 453 /* avoid compiler warnings about unused variables */
iv123 0:88b85febcb45 454 (void)localOnly;
iv123 0:88b85febcb45 455
iv123 0:88b85febcb45 456 // check that the len of the data to write are compatible with the characteristic
iv123 0:88b85febcb45 457 GattCharacteristic* characteristic = getCharacteristicFromHandle(attributeHandle);
iv123 0:88b85febcb45 458 if (!characteristic) {
iv123 0:88b85febcb45 459 PRINTF("characteristic not found\r\n");
iv123 0:88b85febcb45 460 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 461 }
iv123 0:88b85febcb45 462
iv123 0:88b85febcb45 463 // if the attribute handle is the attribute handle of the characteristic value then
iv123 0:88b85febcb45 464 // write the value
iv123 0:88b85febcb45 465 if (attributeHandle == characteristic->getValueHandle()) {
iv123 0:88b85febcb45 466 // assert the len in input is correct for this characteristic
iv123 0:88b85febcb45 467 const GattAttribute& value_attribute = characteristic->getValueAttribute();
iv123 0:88b85febcb45 468
iv123 0:88b85febcb45 469 // reject write if the lenght exceed the maximum lenght of this attribute
iv123 0:88b85febcb45 470 if (value_attribute.getMaxLength() < len) {
iv123 0:88b85febcb45 471 PRINTF("invalid variable length: %u, max length is: %u\r\n", len, value_attribute.getMaxLength());
iv123 0:88b85febcb45 472 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 473 }
iv123 0:88b85febcb45 474
iv123 0:88b85febcb45 475 // reject write if the attribute size is fixed and the lenght in input is different than the
iv123 0:88b85febcb45 476 // length of the attribute.
iv123 0:88b85febcb45 477 if (value_attribute.hasVariableLength() == false && value_attribute.getMaxLength() != len) {
iv123 0:88b85febcb45 478 PRINTF("invalid fixed length: %u, len should be %u\r\n", len, value_attribute.getMaxLength());
iv123 0:88b85febcb45 479 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 480 }
iv123 0:88b85febcb45 481
iv123 0:88b85febcb45 482 tBleStatus ret;
iv123 0:88b85febcb45 483
iv123 0:88b85febcb45 484 uint16_t charHandle = characteristic->getValueHandle() - BlueNRGGattServer::CHAR_VALUE_HANDLE;
iv123 0:88b85febcb45 485
iv123 0:88b85febcb45 486 PRINTF("updating bleCharacteristic valueHandle=%u,\
iv123 0:88b85febcb45 487 corresponding serviceHandle=%u len=%d\n\r",
iv123 0:88b85febcb45 488 attributeHandle, bleCharHandleMap.find(charHandle)->second, len);
iv123 0:88b85febcb45 489
iv123 0:88b85febcb45 490 /*
iv123 0:88b85febcb45 491 * If notifications (or indications) are enabled on that characteristic, a notification (or indication)
iv123 0:88b85febcb45 492 * will be sent to the client after sending this command to the BlueNRG.
iv123 0:88b85febcb45 493 */
iv123 0:88b85febcb45 494 ret = aci_gatt_update_char_value(bleCharHandleMap.find(charHandle)->second, charHandle, 0, len, buffer);
iv123 0:88b85febcb45 495
iv123 0:88b85febcb45 496 if (ret != BLE_STATUS_SUCCESS){
iv123 0:88b85febcb45 497 PRINTF("Error while updating characteristic (ret=0x%x).\n\r", ret);
iv123 0:88b85febcb45 498 switch (ret) {
iv123 0:88b85febcb45 499 case BLE_STATUS_INVALID_HANDLE:
iv123 0:88b85febcb45 500 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 501 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 502 default:
iv123 0:88b85febcb45 503 return BLE_STACK_BUSY;
iv123 0:88b85febcb45 504 }
iv123 0:88b85febcb45 505 }
iv123 0:88b85febcb45 506
iv123 0:88b85febcb45 507 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 508 } else {
iv123 0:88b85febcb45 509 // write this handle has a descriptor handle
iv123 0:88b85febcb45 510 uint16_t charHandle = characteristic->getValueHandle() - BlueNRGGattServer::CHAR_VALUE_HANDLE;
iv123 0:88b85febcb45 511 uint16_t service_handle = bleCharHandleMap.find(charHandle)->second;
iv123 0:88b85febcb45 512
iv123 0:88b85febcb45 513 tBleStatus ret = aci_gatt_set_desc_value(
iv123 0:88b85febcb45 514 service_handle,
iv123 0:88b85febcb45 515 charHandle,
iv123 0:88b85febcb45 516 attributeHandle,
iv123 0:88b85febcb45 517 0,
iv123 0:88b85febcb45 518 len,
iv123 0:88b85febcb45 519 buffer
iv123 0:88b85febcb45 520 );
iv123 0:88b85febcb45 521
iv123 0:88b85febcb45 522 if (ret != BLE_STATUS_SUCCESS){
iv123 0:88b85febcb45 523 PRINTF("Error while updating characteristic descriptor (ret=0x%x).\n\r", ret);
iv123 0:88b85febcb45 524 switch (ret) {
iv123 0:88b85febcb45 525 case BLE_STATUS_INVALID_HANDLE:
iv123 0:88b85febcb45 526 case BLE_STATUS_INVALID_PARAMETER:
iv123 0:88b85febcb45 527 return BLE_ERROR_INVALID_PARAM;
iv123 0:88b85febcb45 528 default:
iv123 0:88b85febcb45 529 return BLE_STACK_BUSY;
iv123 0:88b85febcb45 530 }
iv123 0:88b85febcb45 531 }
iv123 0:88b85febcb45 532
iv123 0:88b85febcb45 533 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 534 }
iv123 0:88b85febcb45 535 }
iv123 0:88b85febcb45 536
iv123 0:88b85febcb45 537 /**************************************************************************/
iv123 0:88b85febcb45 538 /*!
iv123 0:88b85febcb45 539 @brief Reads a value according to the handle provided
iv123 0:88b85febcb45 540
iv123 0:88b85febcb45 541 @param[in] attributeHandle
iv123 0:88b85febcb45 542 The handle of the attribute to read from
iv123 0:88b85febcb45 543
iv123 0:88b85febcb45 544 @returns ble_error_t
iv123 0:88b85febcb45 545
iv123 0:88b85febcb45 546 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 547 Everything executed properly
iv123 0:88b85febcb45 548
iv123 0:88b85febcb45 549 @section EXAMPLE
iv123 0:88b85febcb45 550
iv123 0:88b85febcb45 551 @code
iv123 0:88b85febcb45 552
iv123 0:88b85febcb45 553 @endcode
iv123 0:88b85febcb45 554 */
iv123 0:88b85febcb45 555 /**************************************************************************/
iv123 0:88b85febcb45 556 ble_error_t BlueNRGGattServer::Read_Request_CB(uint16_t attributeHandle)
iv123 0:88b85febcb45 557 {
iv123 0:88b85febcb45 558 uint16_t gapConnectionHandle = BlueNRGGap::getInstance().getConnectionHandle();
iv123 0:88b85febcb45 559
iv123 0:88b85febcb45 560 GattReadCallbackParams readParams;
iv123 0:88b85febcb45 561 readParams.handle = attributeHandle;
iv123 0:88b85febcb45 562
iv123 0:88b85febcb45 563 //PRINTF("readParams.handle = %d\n\r", readParams.handle);
iv123 0:88b85febcb45 564 HCIDataReadEvent(&readParams);
iv123 0:88b85febcb45 565
iv123 0:88b85febcb45 566 //EXIT:
iv123 0:88b85febcb45 567 if(gapConnectionHandle != 0){
iv123 0:88b85febcb45 568 //PRINTF("Calling aci_gatt_allow_read\n\r");
iv123 0:88b85febcb45 569 aci_gatt_allow_read(gapConnectionHandle);
iv123 0:88b85febcb45 570 }
iv123 0:88b85febcb45 571
iv123 0:88b85febcb45 572 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 573 }
iv123 0:88b85febcb45 574
iv123 0:88b85febcb45 575 // ask if the write request should be accepted of rejected
iv123 0:88b85febcb45 576 // return 0 in case of success or an ATT error response in
iv123 0:88b85febcb45 577 // case of faillure
iv123 0:88b85febcb45 578 uint8_t BlueNRGGattServer::Write_Request_CB(
iv123 0:88b85febcb45 579 uint16_t connection_handle, uint16_t attr_handle, uint8_t data_length,
iv123 0:88b85febcb45 580 const uint8_t* data) {
iv123 0:88b85febcb45 581
iv123 0:88b85febcb45 582 GattCharacteristic* characteristic = getCharacteristicFromHandle(attr_handle);
iv123 0:88b85febcb45 583 if(!characteristic) {
iv123 0:88b85febcb45 584 return AUTH_CALLBACK_REPLY_ATTERR_INVALID_HANDLE & 0xFF;
iv123 0:88b85febcb45 585 }
iv123 0:88b85febcb45 586
iv123 0:88b85febcb45 587 // check if the data length is in range
iv123 0:88b85febcb45 588 if (characteristic->getValueAttribute().getMaxLength() < data_length) {
iv123 0:88b85febcb45 589 return AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH & 0xFF;
iv123 0:88b85febcb45 590 }
iv123 0:88b85febcb45 591
iv123 0:88b85febcb45 592 // if the length of the characteristic value is fixed
iv123 0:88b85febcb45 593 // then the data in input should be of that length
iv123 0:88b85febcb45 594 if (characteristic->getValueAttribute().hasVariableLength() == false &&
iv123 0:88b85febcb45 595 characteristic->getValueAttribute().getMaxLength() != data_length) {
iv123 0:88b85febcb45 596 return AUTH_CALLBACK_REPLY_ATTERR_INVALID_ATT_VAL_LENGTH & 0xFF;
iv123 0:88b85febcb45 597 }
iv123 0:88b85febcb45 598
iv123 0:88b85febcb45 599 GattWriteAuthCallbackParams params = {
iv123 0:88b85febcb45 600 connection_handle,
iv123 0:88b85febcb45 601 attr_handle,
iv123 0:88b85febcb45 602 /* offset */ 0,
iv123 0:88b85febcb45 603 data_length,
iv123 0:88b85febcb45 604 data,
iv123 0:88b85febcb45 605 /* authorizationReply */ AUTH_CALLBACK_REPLY_ATTERR_WRITE_NOT_PERMITTED
iv123 0:88b85febcb45 606 };
iv123 0:88b85febcb45 607
iv123 0:88b85febcb45 608 return characteristic->authorizeWrite(&params) & 0xFF;
iv123 0:88b85febcb45 609 }
iv123 0:88b85febcb45 610
iv123 0:88b85febcb45 611 /**************************************************************************/
iv123 0:88b85febcb45 612 /*!
iv123 0:88b85febcb45 613 @brief Returns the GattCharacteristic according to the handle provided
iv123 0:88b85febcb45 614
iv123 0:88b85febcb45 615 @param[in] attrHandle
iv123 0:88b85febcb45 616 The handle of the attribute
iv123 0:88b85febcb45 617
iv123 0:88b85febcb45 618 @returns ble_error_t
iv123 0:88b85febcb45 619
iv123 0:88b85febcb45 620 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 621 Everything executed properly
iv123 0:88b85febcb45 622
iv123 0:88b85febcb45 623 @section EXAMPLE
iv123 0:88b85febcb45 624
iv123 0:88b85febcb45 625 @code
iv123 0:88b85febcb45 626
iv123 0:88b85febcb45 627 @endcode
iv123 0:88b85febcb45 628 */
iv123 0:88b85febcb45 629 /**************************************************************************/
iv123 0:88b85febcb45 630 GattCharacteristic* BlueNRGGattServer::getCharacteristicFromHandle(uint16_t attrHandle)
iv123 0:88b85febcb45 631 {
iv123 0:88b85febcb45 632 GattCharacteristic *p_char = NULL;
iv123 0:88b85febcb45 633 int i;
iv123 0:88b85febcb45 634 uint16_t handle, handle_1;
iv123 0:88b85febcb45 635
iv123 0:88b85febcb45 636 PRINTF("BlueNRGGattServer::getCharacteristicFromHandle()>>Attr Handle received %d\n\r",attrHandle);
iv123 0:88b85febcb45 637 for(i=0; i<characteristicCount; i++)
iv123 0:88b85febcb45 638 {
iv123 0:88b85febcb45 639 handle = p_characteristics[i]->getValueAttribute().getHandle()-BlueNRGGattServer::CHAR_VALUE_HANDLE;
iv123 0:88b85febcb45 640 PRINTF("handle(%d)=%d\n\r", i, handle);
iv123 0:88b85febcb45 641 if(i==characteristicCount-1)//Last Characteristic check
iv123 0:88b85febcb45 642 {
iv123 0:88b85febcb45 643 if(attrHandle>=handle)
iv123 0:88b85febcb45 644 {
iv123 0:88b85febcb45 645 p_char = p_characteristics[i];
iv123 0:88b85febcb45 646 PRINTF("Found Characteristic Properties 0x%x (handle=%d)\n\r",p_char->getProperties(), handle);
iv123 0:88b85febcb45 647 break;
iv123 0:88b85febcb45 648 }
iv123 0:88b85febcb45 649 }
iv123 0:88b85febcb45 650 else {
iv123 0:88b85febcb45 651 handle_1 = p_characteristics[i+1]->getValueAttribute().getHandle()-BlueNRGGattServer::CHAR_VALUE_HANDLE;
iv123 0:88b85febcb45 652 //Testing if attribute handle is between two Characteristic Handles
iv123 0:88b85febcb45 653 if(attrHandle>=handle && attrHandle<handle_1)
iv123 0:88b85febcb45 654 {
iv123 0:88b85febcb45 655 p_char = p_characteristics[i];
iv123 0:88b85febcb45 656 PRINTF("Found Characteristic Properties 0x%x (handle=%d handle_1=%d)\n\r",p_char->getProperties(), handle, handle_1);
iv123 0:88b85febcb45 657 break;
iv123 0:88b85febcb45 658 } else continue;
iv123 0:88b85febcb45 659 }
iv123 0:88b85febcb45 660 }
iv123 0:88b85febcb45 661
iv123 0:88b85febcb45 662 return p_char;
iv123 0:88b85febcb45 663 }
iv123 0:88b85febcb45 664
iv123 0:88b85febcb45 665 void BlueNRGGattServer::HCIDataWrittenEvent(const GattWriteCallbackParams *params) {
iv123 0:88b85febcb45 666 this->handleDataWrittenEvent(params);
iv123 0:88b85febcb45 667 }
iv123 0:88b85febcb45 668
iv123 0:88b85febcb45 669 void BlueNRGGattServer::HCIDataReadEvent(const GattReadCallbackParams *params) {
iv123 0:88b85febcb45 670 PRINTF("Called HCIDataReadEvent\n\r");
iv123 0:88b85febcb45 671 this->handleDataReadEvent(params);
iv123 0:88b85febcb45 672 }
iv123 0:88b85febcb45 673
iv123 0:88b85febcb45 674 void BlueNRGGattServer::HCIEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) {
iv123 0:88b85febcb45 675 this->handleEvent(type, charHandle);
iv123 0:88b85febcb45 676 }
iv123 0:88b85febcb45 677
iv123 0:88b85febcb45 678 void BlueNRGGattServer::HCIDataSentEvent(unsigned count) {
iv123 0:88b85febcb45 679 this->handleDataSentEvent(count);
iv123 0:88b85febcb45 680 }
iv123 0:88b85febcb45 681
iv123 0:88b85febcb45 682
iv123 0:88b85febcb45 683 ble_error_t BlueNRGGattServer::initializeGATTDatabase(void) {
iv123 0:88b85febcb45 684 // <TODO>
iv123 0:88b85febcb45 685 return (ble_error_t)0;
iv123 0:88b85febcb45 686 }
iv123 0:88b85febcb45 687
iv123 0:88b85febcb45 688 /**************************************************************************/
iv123 0:88b85febcb45 689 /*!
iv123 0:88b85febcb45 690 @brief Clear BlueNRGGattServer's state.
iv123 0:88b85febcb45 691
iv123 0:88b85febcb45 692 @returns ble_error_t
iv123 0:88b85febcb45 693
iv123 0:88b85febcb45 694 @retval BLE_ERROR_NONE
iv123 0:88b85febcb45 695 Everything executed properly
iv123 0:88b85febcb45 696 */
iv123 0:88b85febcb45 697 /**************************************************************************/
iv123 0:88b85febcb45 698 ble_error_t BlueNRGGattServer::reset(void)
iv123 0:88b85febcb45 699 {
iv123 0:88b85febcb45 700 /* Clear all state that is from the parent, including private members */
iv123 0:88b85febcb45 701 if (GattServer::reset() != BLE_ERROR_NONE) {
iv123 0:88b85febcb45 702 return BLE_ERROR_INVALID_STATE;
iv123 0:88b85febcb45 703 }
iv123 0:88b85febcb45 704
iv123 0:88b85febcb45 705 /* Clear class members */
iv123 0:88b85febcb45 706 memset(p_characteristics, 0, sizeof(p_characteristics));
iv123 0:88b85febcb45 707 memset(bleCharacteristicHandles, 0, sizeof(bleCharacteristicHandles));
iv123 0:88b85febcb45 708 serviceCount = 0;
iv123 0:88b85febcb45 709 characteristicCount = 0;
iv123 0:88b85febcb45 710
iv123 0:88b85febcb45 711 return BLE_ERROR_NONE;
iv123 0:88b85febcb45 712 }
iv123 0:88b85febcb45 713
iv123 0:88b85febcb45 714
iv123 0:88b85febcb45 715 /// compute the number of attributes needed by this service.
iv123 0:88b85febcb45 716 uint16_t BlueNRGGattServer::computeAttributesRecord(GattService& service) {
iv123 0:88b85febcb45 717 uint16_t attribute_records = 1;
iv123 0:88b85febcb45 718
iv123 0:88b85febcb45 719 for (uint8_t characteristic_index = 0; characteristic_index < service.getCharacteristicCount(); ++characteristic_index) {
iv123 0:88b85febcb45 720 // add two attributes, one for the characteristic declaration
iv123 0:88b85febcb45 721 // and the other for the characteristic value.
iv123 0:88b85febcb45 722 attribute_records += 2;
iv123 0:88b85febcb45 723
iv123 0:88b85febcb45 724 const GattCharacteristic* characteristic = service.getCharacteristic(characteristic_index);
iv123 0:88b85febcb45 725 const uint8_t properties = characteristic->getProperties();
iv123 0:88b85febcb45 726 // if notify or indicate are present, two attributes are
iv123 0:88b85febcb45 727 // needed
iv123 0:88b85febcb45 728 if ((properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ||
iv123 0:88b85febcb45 729 (properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) {
iv123 0:88b85febcb45 730 attribute_records += 2;
iv123 0:88b85febcb45 731 }
iv123 0:88b85febcb45 732
iv123 0:88b85febcb45 733 // if broadcast is set, two attributes are needed
iv123 0:88b85febcb45 734 if (properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_BROADCAST) {
iv123 0:88b85febcb45 735 attribute_records += 2;
iv123 0:88b85febcb45 736 }
iv123 0:88b85febcb45 737
iv123 0:88b85febcb45 738 // if extended properties flag is set, two attributes are needed
iv123 0:88b85febcb45 739 if (properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES) {
iv123 0:88b85febcb45 740 attribute_records += 2;
iv123 0:88b85febcb45 741 }
iv123 0:88b85febcb45 742
iv123 0:88b85febcb45 743 attribute_records += characteristic->getDescriptorCount();
iv123 0:88b85febcb45 744 }
iv123 0:88b85febcb45 745
iv123 0:88b85febcb45 746 // for some reason, if there is just a service, this value should
iv123 0:88b85febcb45 747 // be equal to 5
iv123 0:88b85febcb45 748 if (attribute_records == 1) {
iv123 0:88b85febcb45 749 attribute_records = 5;
iv123 0:88b85febcb45 750 }
iv123 0:88b85febcb45 751
iv123 0:88b85febcb45 752 return attribute_records;
iv123 0:88b85febcb45 753 }