This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).

Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn

The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/

Committer:
edamame22
Date:
Thu Apr 13 04:48:11 2017 +0000
Revision:
0:29983394c6b6
Initial commit

Who changed what in which revision?

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