nrf52-dk

Committer:
mamont090671
Date:
Fri Jun 05 10:49:10 2020 +0000
Revision:
0:70ce6da7a141
nrf52840

Who changed what in which revision?

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