BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Vincent Coubard
Date:
Thu Sep 15 10:51:20 2016 +0100
Branch:
ee968623f0c7a55b1be23b1d5b6b84c0b190f500
Revision:
258:e5ef65120b06
Parent:
253:9665a6f8bbdb
Child:
259:323f588e5f57
Sync with ee968623f0c7a55b1be23b1d5b6b84c0b190f500

2016-07-07 17:46:57+01:00: Vincent Coubard
fix CharacteristicReadWrite_test_xxx

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 130:770ce14d3d15 1 /* mbed Microcontroller Library
Wolfgang Betz 130:770ce14d3d15 2 * Copyright (c) 2006-2013 ARM Limited
Wolfgang Betz 130:770ce14d3d15 3 *
Wolfgang Betz 130:770ce14d3d15 4 * Licensed under the Apache License, Version 2.0 (the "License");
Wolfgang Betz 130:770ce14d3d15 5 * you may not use this file except in compliance with the License.
Wolfgang Betz 130:770ce14d3d15 6 * You may obtain a copy of the License at
Wolfgang Betz 130:770ce14d3d15 7 *
Wolfgang Betz 130:770ce14d3d15 8 * http://www.apache.org/licenses/LICENSE-2.0
Wolfgang Betz 130:770ce14d3d15 9 *
Wolfgang Betz 130:770ce14d3d15 10 * Unless required by applicable law or agreed to in writing, software
Wolfgang Betz 130:770ce14d3d15 11 * distributed under the License is distributed on an "AS IS" BASIS,
Wolfgang Betz 130:770ce14d3d15 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Wolfgang Betz 130:770ce14d3d15 13 * See the License for the specific language governing permissions and
Wolfgang Betz 130:770ce14d3d15 14 * limitations under the License.
Wolfgang Betz 130:770ce14d3d15 15 */
Wolfgang Betz 130:770ce14d3d15 16 /**
Wolfgang Betz 130:770ce14d3d15 17 ******************************************************************************
Vincent Coubard 258:e5ef65120b06 18 * @file BlueNRGGattServer.cpp
Wolfgang Betz 130:770ce14d3d15 19 * @author STMicroelectronics
Wolfgang Betz 130:770ce14d3d15 20 * @brief Implementation of BlueNRG BLE_API GattServer Class
Wolfgang Betz 130:770ce14d3d15 21 ******************************************************************************
Wolfgang Betz 130:770ce14d3d15 22 * @copy
Wolfgang Betz 130:770ce14d3d15 23 *
Wolfgang Betz 130:770ce14d3d15 24 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
Wolfgang Betz 130:770ce14d3d15 25 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
Wolfgang Betz 130:770ce14d3d15 26 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
Wolfgang Betz 130:770ce14d3d15 27 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
Wolfgang Betz 130:770ce14d3d15 28 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
Wolfgang Betz 130:770ce14d3d15 29 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
Wolfgang Betz 130:770ce14d3d15 30 *
Wolfgang Betz 130:770ce14d3d15 31 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
Andrea Palmieri 229:9981f62cdb1a 32 */
Vincent Coubard 258:e5ef65120b06 33
Wolfgang Betz 130:770ce14d3d15 34 /** @defgroup BlueNRGGATTSERVER
Wolfgang Betz 130:770ce14d3d15 35 * @brief BlueNRG BLE_API GattServer Adaptation
Wolfgang Betz 130:770ce14d3d15 36 * @{
Wolfgang Betz 130:770ce14d3d15 37 */
Vincent Coubard 258:e5ef65120b06 38
Wolfgang Betz 130:770ce14d3d15 39 #include "BlueNRGGattServer.h"
Vincent Coubard 253:9665a6f8bbdb 40 #include "mbed-drivers/mbed.h"
Wolfgang Betz 130:770ce14d3d15 41 #include "BlueNRGGap.h"
Wolfgang Betz 130:770ce14d3d15 42 #include "Utils.h"
Wolfgang Betz 132:51056160fa4a 43 #include "debug.h"
Wolfgang Betz 130:770ce14d3d15 44
Wolfgang Betz 130:770ce14d3d15 45 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 46 /*!
Wolfgang Betz 130:770ce14d3d15 47 @brief Adds a new service to the GATT table on the peripheral
Wolfgang Betz 130:770ce14d3d15 48
Wolfgang Betz 130:770ce14d3d15 49 @params[in] service
Wolfgang Betz 130:770ce14d3d15 50 Pointer to instance of the Gatt Server to add
Vincent Coubard 258:e5ef65120b06 51
Wolfgang Betz 130:770ce14d3d15 52 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 53
Wolfgang Betz 130:770ce14d3d15 54 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 55 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 56
Wolfgang Betz 130:770ce14d3d15 57 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 58
Wolfgang Betz 130:770ce14d3d15 59 @code
Wolfgang Betz 130:770ce14d3d15 60
Wolfgang Betz 130:770ce14d3d15 61 @endcode
Wolfgang Betz 130:770ce14d3d15 62 */
Wolfgang Betz 130:770ce14d3d15 63 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 64 ble_error_t BlueNRGGattServer::addService(GattService &service)
Wolfgang Betz 130:770ce14d3d15 65 {
Wolfgang Betz 130:770ce14d3d15 66 /* ToDo: Make sure we don't overflow the array, etc. */
Wolfgang Betz 130:770ce14d3d15 67 /* ToDo: Make sure this service UUID doesn't already exist (?) */
Wolfgang Betz 130:770ce14d3d15 68 /* ToDo: Basic validation */
Vincent Coubard 258:e5ef65120b06 69
Wolfgang Betz 130:770ce14d3d15 70 tBleStatus ret;
Wolfgang Betz 130:770ce14d3d15 71 uint8_t type;
Wolfgang Betz 130:770ce14d3d15 72 uint16_t short_uuid;
Wolfgang Betz 130:770ce14d3d15 73 uint8_t primary_short_uuid[2];
Wolfgang Betz 130:770ce14d3d15 74 uint8_t primary_base_uuid[16];
Wolfgang Betz 130:770ce14d3d15 75 uint8_t char_base_uuid[16];
Wolfgang Betz 130:770ce14d3d15 76 const uint8_t *base_uuid;
Wolfgang Betz 130:770ce14d3d15 77 const uint8_t *base_char_uuid;
Vincent Coubard 258:e5ef65120b06 78
Wolfgang Betz 130:770ce14d3d15 79 uint8_t charsCount = 0;
Wolfgang Betz 130:770ce14d3d15 80 uint8_t maxAttrRecords = 0;
Wolfgang Betz 130:770ce14d3d15 81
Wolfgang Betz 130:770ce14d3d15 82 type = (service.getUUID()).shortOrLong();
Wolfgang Betz 132:51056160fa4a 83 PRINTF("AddService(): Type:%d\n\r", type);
Vincent Coubard 258:e5ef65120b06 84
Wolfgang Betz 130:770ce14d3d15 85 /* Add the service to the BlueNRG */
Wolfgang Betz 130:770ce14d3d15 86 short_uuid = (service.getUUID()).getShortUUID();
Wolfgang Betz 130:770ce14d3d15 87 STORE_LE_16(primary_short_uuid, short_uuid);
Vincent Coubard 258:e5ef65120b06 88
Wolfgang Betz 130:770ce14d3d15 89 if(type==UUID::UUID_TYPE_LONG) {
Vincent Coubard 258:e5ef65120b06 90 base_uuid = (service.getUUID()).getBaseUUID();
Vincent Coubard 258:e5ef65120b06 91
Andrea Palmieri 207:b9df918d6d5a 92 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]);
Wolfgang Betz 130:770ce14d3d15 93 }
Wolfgang Betz 130:770ce14d3d15 94
Wolfgang Betz 130:770ce14d3d15 95 charsCount = service.getCharacteristicCount();
Wolfgang Betz 130:770ce14d3d15 96 //1(service record)+2records*char+1record*char_desc
Wolfgang Betz 130:770ce14d3d15 97 maxAttrRecords = 1+3*charsCount;
Wolfgang Betz 130:770ce14d3d15 98
Wolfgang Betz 130:770ce14d3d15 99 if(type==UUID::UUID_TYPE_SHORT) {
Wolfgang Betz 130:770ce14d3d15 100 ret = aci_gatt_add_serv(UUID_TYPE_16,
Wolfgang Betz 130:770ce14d3d15 101 primary_short_uuid,
Wolfgang Betz 130:770ce14d3d15 102 PRIMARY_SERVICE,
Wolfgang Betz 130:770ce14d3d15 103 maxAttrRecords/*7*/,
Wolfgang Betz 130:770ce14d3d15 104 &servHandle);
Wolfgang Betz 132:51056160fa4a 105 PRINTF("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret);
Wolfgang Betz 130:770ce14d3d15 106 }
Wolfgang Betz 130:770ce14d3d15 107 else if(type==UUID::UUID_TYPE_LONG) {
Wolfgang Betz 130:770ce14d3d15 108 ret = aci_gatt_add_serv(UUID_TYPE_128,
Wolfgang Betz 130:770ce14d3d15 109 primary_base_uuid,
Wolfgang Betz 130:770ce14d3d15 110 PRIMARY_SERVICE,
Wolfgang Betz 130:770ce14d3d15 111 maxAttrRecords/*7*/,
Wolfgang Betz 130:770ce14d3d15 112 &servHandle);
Wolfgang Betz 132:51056160fa4a 113 PRINTF("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret);
Wolfgang Betz 130:770ce14d3d15 114 }
Vincent Coubard 258:e5ef65120b06 115
Wolfgang Betz 130:770ce14d3d15 116 service.setHandle(servHandle);
Wolfgang Betz 130:770ce14d3d15 117 //serviceHandleVector.push_back(servHandle);
Wolfgang Betz 132:51056160fa4a 118 PRINTF("added servHandle handle =%u\n\r", servHandle);
Wolfgang Betz 130:770ce14d3d15 119 uint16_t bleCharacteristic;
Vincent Coubard 258:e5ef65120b06 120
Wolfgang Betz 130:770ce14d3d15 121 //iterate to include all characteristics
Wolfgang Betz 130:770ce14d3d15 122 for (uint8_t i = 0; i < charsCount; i++) {
Wolfgang Betz 130:770ce14d3d15 123 GattCharacteristic *p_char = service.getCharacteristic(i);
Andrea Palmieri 229:9981f62cdb1a 124 uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID();
Andrea Palmieri 229:9981f62cdb1a 125
Wolfgang Betz 130:770ce14d3d15 126 uint8_t int_8_uuid[2];
Wolfgang Betz 130:770ce14d3d15 127 STORE_LE_16(int_8_uuid, char_uuid);
Andrea Palmieri 229:9981f62cdb1a 128
Andrea Palmieri 229:9981f62cdb1a 129 type = (p_char->getValueAttribute().getUUID()).shortOrLong();
Andrea Palmieri 229:9981f62cdb1a 130
Wolfgang Betz 130:770ce14d3d15 131 if(type==UUID::UUID_TYPE_LONG) {
Wolfgang Betz 130:770ce14d3d15 132 base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID();
Andrea Palmieri 229:9981f62cdb1a 133 #ifdef DEBUG
Andrea Palmieri 229:9981f62cdb1a 134 for(uint8_t j=0; j<16; j++) {
Andrea Palmieri 229:9981f62cdb1a 135 PRINTF("base_char_uuid[%d] 0x%02x ", j, base_char_uuid[j]);
Andrea Palmieri 229:9981f62cdb1a 136 }
Andrea Palmieri 229:9981f62cdb1a 137 PRINTF("\n\r");
Andrea Palmieri 229:9981f62cdb1a 138 #endif
Andrea Palmieri 207:b9df918d6d5a 139 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]);
Wolfgang Betz 130:770ce14d3d15 140 }
Vincent Coubard 258:e5ef65120b06 141
Wolfgang Betz 132:51056160fa4a 142 PRINTF("Char Properties 0x%x\n\r", p_char->getProperties());
Wolfgang Betz 130:770ce14d3d15 143 /*
Wolfgang Betz 130:770ce14d3d15 144 * Gatt_Evt_Mask -> HardCoded (0)
Wolfgang Betz 130:770ce14d3d15 145 * Encryption_Key_Size -> Hardcoded (16)
Wolfgang Betz 130:770ce14d3d15 146 * isVariable (variable length value field) -> Hardcoded (1)
Wolfgang Betz 130:770ce14d3d15 147 */
Wolfgang Betz 130:770ce14d3d15 148 uint8_t Gatt_Evt_Mask = 0x0;
Wolfgang Betz 130:770ce14d3d15 149
Wolfgang Betz 130:770ce14d3d15 150 if((p_char->getProperties() &
Wolfgang Betz 130:770ce14d3d15 151 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
Wolfgang Betz 130:770ce14d3d15 152 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) {
Wolfgang Betz 132:51056160fa4a 153 PRINTF("Setting up Gatt GATT_NOTIFY_ATTRIBUTE_WRITE Mask\n\r");
Vincent Coubard 258:e5ef65120b06 154 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP /* | GATT_NOTIFY_ATTRIBUTE_WRITE */;
Wolfgang Betz 130:770ce14d3d15 155 }
Wolfgang Betz 130:770ce14d3d15 156 if((p_char->getProperties() &
Wolfgang Betz 130:770ce14d3d15 157 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ|
Wolfgang Betz 130:770ce14d3d15 158 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) {
Wolfgang Betz 132:51056160fa4a 159 PRINTF("Setting up Gatt GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP Mask\n\r");
Vincent Coubard 258:e5ef65120b06 160 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP;
Wolfgang Betz 130:770ce14d3d15 161 } //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check.
Wolfgang Betz 130:770ce14d3d15 162
Wolfgang Betz 130:770ce14d3d15 163 if(type==UUID::UUID_TYPE_SHORT) {
Wolfgang Betz 130:770ce14d3d15 164 ret = aci_gatt_add_char(service.getHandle(),
Wolfgang Betz 130:770ce14d3d15 165 UUID_TYPE_16,
Wolfgang Betz 130:770ce14d3d15 166 int_8_uuid,
Wolfgang Betz 130:770ce14d3d15 167 p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
Wolfgang Betz 130:770ce14d3d15 168 p_char->getProperties(),
Wolfgang Betz 130:770ce14d3d15 169 ATTR_PERMISSION_NONE,
Wolfgang Betz 130:770ce14d3d15 170 Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
Wolfgang Betz 130:770ce14d3d15 171 16 /*Encryption_Key_Size*/,
Wolfgang Betz 130:770ce14d3d15 172 1 /*isVariable*/,
Wolfgang Betz 130:770ce14d3d15 173 &bleCharacteristic);
Vincent Coubard 258:e5ef65120b06 174
Andrea Palmieri 229:9981f62cdb1a 175 PRINTF("aci_gatt_add_char UUID_TYPE_16 props=%d MaxLength=%d ret=%d\n\r",
Andrea Palmieri 229:9981f62cdb1a 176 p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);
Wolfgang Betz 130:770ce14d3d15 177
Wolfgang Betz 130:770ce14d3d15 178 } else if(type==UUID::UUID_TYPE_LONG) {
Wolfgang Betz 130:770ce14d3d15 179 ret = aci_gatt_add_char(service.getHandle(),
Wolfgang Betz 130:770ce14d3d15 180 UUID_TYPE_128,
Wolfgang Betz 130:770ce14d3d15 181 char_base_uuid,
Wolfgang Betz 130:770ce14d3d15 182 p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
Wolfgang Betz 130:770ce14d3d15 183 p_char->getProperties(),
Wolfgang Betz 130:770ce14d3d15 184 ATTR_PERMISSION_NONE,
Wolfgang Betz 130:770ce14d3d15 185 Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
Wolfgang Betz 130:770ce14d3d15 186 16 /*Encryption_Key_Size*/,
Wolfgang Betz 130:770ce14d3d15 187 1 /*isVariable*/,
Wolfgang Betz 130:770ce14d3d15 188 &bleCharacteristic);
Vincent Coubard 258:e5ef65120b06 189
Andrea Palmieri 229:9981f62cdb1a 190 PRINTF("aci_gatt_add_char UUID_TYPE_128 props=%d MaxLength=%d ret=%d\n\r",
Andrea Palmieri 229:9981f62cdb1a 191 p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);
Wolfgang Betz 130:770ce14d3d15 192 }
Vincent Coubard 258:e5ef65120b06 193
Andrea Palmieri 229:9981f62cdb1a 194 bleCharHandleMap.insert(std::pair<uint16_t, uint16_t>(bleCharacteristic, servHandle));
Vincent Coubard 258:e5ef65120b06 195
Wolfgang Betz 130:770ce14d3d15 196 p_characteristics[characteristicCount++] = p_char;
Andrea Palmieri 229:9981f62cdb1a 197 /* Set the characteristic value handle */
Andrea Palmieri 229:9981f62cdb1a 198 p_char->getValueAttribute().setHandle(bleCharacteristic+BlueNRGGattServer::CHAR_VALUE_HANDLE);
Andrea Palmieri 229:9981f62cdb1a 199 PRINTF("added bleCharacteristic (value handle =%u)\n\r", p_char->getValueAttribute().getHandle());
Wolfgang Betz 130:770ce14d3d15 200
Andrea Palmieri 202:caf4864292c1 201 if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getLength() > 0)) {
Andrea Palmieri 229:9981f62cdb1a 202 write(p_char->getValueAttribute().getHandle(),
Andrea Palmieri 229:9981f62cdb1a 203 p_char->getValueAttribute().getValuePtr(),
Andrea Palmieri 229:9981f62cdb1a 204 p_char->getValueAttribute().getLength(), false /* localOnly */);
Wolfgang Betz 130:770ce14d3d15 205 }
Wolfgang Betz 130:770ce14d3d15 206
Wolfgang Betz 130:770ce14d3d15 207 // add descriptors now
Wolfgang Betz 130:770ce14d3d15 208 uint16_t descHandle = 0;
Wolfgang Betz 132:51056160fa4a 209 PRINTF("p_char->getDescriptorCount()=%d\n\r", p_char->getDescriptorCount());
Vincent Coubard 258:e5ef65120b06 210
Wolfgang Betz 130:770ce14d3d15 211 for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) {
Wolfgang Betz 130:770ce14d3d15 212 GattAttribute *descriptor = p_char->getDescriptor(descIndex);
Wolfgang Betz 130:770ce14d3d15 213 uint16_t shortUUID = descriptor->getUUID().getShortUUID();
Wolfgang Betz 130:770ce14d3d15 214 const uint8_t uuidArray[] = {(uint8_t)((shortUUID>>8)&0xFF), (uint8_t)((shortUUID&0xFF))};
Andrea Palmieri 229:9981f62cdb1a 215 ret = aci_gatt_add_char_desc(service.getHandle(),
Andrea Palmieri 229:9981f62cdb1a 216 bleCharacteristic,
Andrea Palmieri 229:9981f62cdb1a 217 CHAR_DESC_TYPE_16_BIT,
Andrea Palmieri 229:9981f62cdb1a 218 uuidArray,
Andrea Palmieri 229:9981f62cdb1a 219 descriptor->getMaxLength(),
Andrea Palmieri 229:9981f62cdb1a 220 descriptor->getLength(),
Andrea Palmieri 229:9981f62cdb1a 221 descriptor->getValuePtr(),
Andrea Palmieri 229:9981f62cdb1a 222 CHAR_DESC_SECURITY_PERMISSION,
Andrea Palmieri 229:9981f62cdb1a 223 CHAR_DESC_ACCESS_PERMISSION,
Andrea Palmieri 229:9981f62cdb1a 224 GATT_NOTIFY_ATTRIBUTE_WRITE,
Andrea Palmieri 229:9981f62cdb1a 225 MIN_ENCRY_KEY_SIZE,
Andrea Palmieri 229:9981f62cdb1a 226 CHAR_ATTRIBUTE_LEN_IS_FIXED,
Andrea Palmieri 229:9981f62cdb1a 227 &descHandle);
Wolfgang Betz 132:51056160fa4a 228 PRINTF("Adding Descriptor descriptor handle=%d ret=%d\n\r", descHandle, ret);
Wolfgang Betz 130:770ce14d3d15 229 if(ret==(tBleStatus)0) {
Wolfgang Betz 132:51056160fa4a 230 PRINTF("Descriptor added successfully, descriptor handle=%d\n\r", descHandle);
Wolfgang Betz 130:770ce14d3d15 231 descriptor->setHandle(descHandle);
Wolfgang Betz 130:770ce14d3d15 232 }
Wolfgang Betz 130:770ce14d3d15 233 }
Wolfgang Betz 130:770ce14d3d15 234
Vincent Coubard 258:e5ef65120b06 235 }
Vincent Coubard 258:e5ef65120b06 236
Wolfgang Betz 130:770ce14d3d15 237 serviceCount++;
Vincent Coubard 258:e5ef65120b06 238
Vincent Coubard 258:e5ef65120b06 239 //FIXME: There is no GattService pointer array in GattServer.
Wolfgang Betz 130:770ce14d3d15 240 // There should be one? (Only the user is aware of GattServices!) Report to forum.
Vincent Coubard 258:e5ef65120b06 241
Wolfgang Betz 130:770ce14d3d15 242 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 243 }
Wolfgang Betz 130:770ce14d3d15 244
Wolfgang Betz 130:770ce14d3d15 245 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 246 /*!
Andrea Palmieri 233:488e0e8b9b43 247 @brief Reads the value of a characteristic, based on char handle
Wolfgang Betz 130:770ce14d3d15 248
Andrea Palmieri 233:488e0e8b9b43 249 @param[in] attributeHandle
Wolfgang Betz 130:770ce14d3d15 250 The handle of the GattCharacteristic to read from
Wolfgang Betz 130:770ce14d3d15 251 @param[in] buffer
Wolfgang Betz 130:770ce14d3d15 252 Buffer to hold the the characteristic's value
Wolfgang Betz 130:770ce14d3d15 253 (raw byte array in LSB format)
Wolfgang Betz 130:770ce14d3d15 254 @param[in] lengthP
Wolfgang Betz 130:770ce14d3d15 255 The number of bytes read into the buffer
Wolfgang Betz 130:770ce14d3d15 256
Wolfgang Betz 130:770ce14d3d15 257 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 258
Wolfgang Betz 130:770ce14d3d15 259 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 260 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 261
Wolfgang Betz 130:770ce14d3d15 262 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 263
Wolfgang Betz 130:770ce14d3d15 264 @code
Wolfgang Betz 130:770ce14d3d15 265
Wolfgang Betz 130:770ce14d3d15 266 @endcode
Wolfgang Betz 130:770ce14d3d15 267 */
Wolfgang Betz 130:770ce14d3d15 268 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 269 ble_error_t BlueNRGGattServer::read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP)
Wolfgang Betz 130:770ce14d3d15 270 {
Andrea Palmieri 204:6a6d2f041905 271 tBleStatus ret;
Vincent Coubard 258:e5ef65120b06 272 uint16_t charHandle = attributeHandle;
Andrea Palmieri 204:6a6d2f041905 273
Andrea Palmieri 229:9981f62cdb1a 274 ret = aci_gatt_read_handle_value(charHandle, *lengthP, lengthP, buffer);
Andrea Palmieri 142:adf1567d3900 275
Andrea Palmieri 204:6a6d2f041905 276 if(ret == BLE_STATUS_SUCCESS) {
Andrea Palmieri 204:6a6d2f041905 277 return BLE_ERROR_NONE;
Andrea Palmieri 204:6a6d2f041905 278 }
Andrea Palmieri 204:6a6d2f041905 279 switch (ret) {
Andrea Palmieri 204:6a6d2f041905 280 case ERR_INVALID_HCI_CMD_PARAMS:
Andrea Palmieri 204:6a6d2f041905 281 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 204:6a6d2f041905 282 default:
Andrea Palmieri 204:6a6d2f041905 283 return BLE_ERROR_UNSPECIFIED;
Andrea Palmieri 204:6a6d2f041905 284 }
Wolfgang Betz 130:770ce14d3d15 285 }
Wolfgang Betz 130:770ce14d3d15 286
Wolfgang Betz 130:770ce14d3d15 287 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 288 /*!
Andrea Palmieri 233:488e0e8b9b43 289 @brief Reads the value of a characteristic, based on the connection
Andrea Palmieri 233:488e0e8b9b43 290 and char handle
Wolfgang Betz 130:770ce14d3d15 291
Andrea Palmieri 233:488e0e8b9b43 292 @param[in] connectionHandle
Andrea Palmieri 233:488e0e8b9b43 293 The handle of the connection
Andrea Palmieri 233:488e0e8b9b43 294 @param[in] attributeHandle
Wolfgang Betz 130:770ce14d3d15 295 The handle of the GattCharacteristic to write to
Wolfgang Betz 130:770ce14d3d15 296 @param[in] buffer
Wolfgang Betz 130:770ce14d3d15 297 Data to use when updating the characteristic's value
Wolfgang Betz 130:770ce14d3d15 298 (raw byte array in LSB format)
Andrea Palmieri 233:488e0e8b9b43 299 @param[in] lengthP
Wolfgang Betz 130:770ce14d3d15 300 The number of bytes in buffer
Wolfgang Betz 130:770ce14d3d15 301
Wolfgang Betz 130:770ce14d3d15 302 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 303
Wolfgang Betz 130:770ce14d3d15 304 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 305 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 306
Wolfgang Betz 130:770ce14d3d15 307 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 308
Wolfgang Betz 130:770ce14d3d15 309 @code
Wolfgang Betz 130:770ce14d3d15 310
Wolfgang Betz 130:770ce14d3d15 311 @endcode
Wolfgang Betz 130:770ce14d3d15 312 */
Wolfgang Betz 130:770ce14d3d15 313 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 314 ble_error_t BlueNRGGattServer::read(Gap::Handle_t connectionHandle,
Andrea Palmieri 229:9981f62cdb1a 315 GattAttribute::Handle_t attributeHandle,
Andrea Palmieri 229:9981f62cdb1a 316 uint8_t buffer[],
Andrea Palmieri 229:9981f62cdb1a 317 uint16_t *lengthP) {
Andrea Palmieri 142:adf1567d3900 318
Andrea Palmieri 142:adf1567d3900 319 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 320 (void)connectionHandle;
Andrea Palmieri 142:adf1567d3900 321 (void)attributeHandle;
Andrea Palmieri 142:adf1567d3900 322 (void)buffer;
Andrea Palmieri 142:adf1567d3900 323 (void)lengthP;
Andrea Palmieri 142:adf1567d3900 324
Andrea Palmieri 142:adf1567d3900 325 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 326 }
Wolfgang Betz 130:770ce14d3d15 327
Andrea Palmieri 229:9981f62cdb1a 328 ble_error_t BlueNRGGattServer::write(Gap::Handle_t connectionHandle,
Andrea Palmieri 229:9981f62cdb1a 329 GattAttribute::Handle_t,
Andrea Palmieri 229:9981f62cdb1a 330 const uint8_t[],
Andrea Palmieri 229:9981f62cdb1a 331 uint16_t, bool localOnly) {
Andrea Palmieri 142:adf1567d3900 332 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 333 (void)connectionHandle;
Andrea Palmieri 142:adf1567d3900 334 (void)localOnly;
Andrea Palmieri 142:adf1567d3900 335
Andrea Palmieri 142:adf1567d3900 336 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 337 }
Vincent Coubard 258:e5ef65120b06 338
Andrea Palmieri 229:9981f62cdb1a 339 ble_error_t BlueNRGGattServer::write(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
Wolfgang Betz 130:770ce14d3d15 340 {
Andrea Palmieri 142:adf1567d3900 341 /* avoid compiler warnings about unused variables */
Andrea Palmieri 142:adf1567d3900 342 (void)localOnly;
Andrea Palmieri 142:adf1567d3900 343
Vincent Coubard 258:e5ef65120b06 344 // check that the len of the data to write are compatible with the characteristic
Vincent Coubard 258:e5ef65120b06 345 GattCharacteristic* characteristic = getCharacteristicFromHandle(attributeHandle);
Vincent Coubard 258:e5ef65120b06 346 if (!characteristic) {
Vincent Coubard 258:e5ef65120b06 347 return BLE_ERROR_INVALID_PARAM;
Vincent Coubard 258:e5ef65120b06 348 }
Vincent Coubard 258:e5ef65120b06 349
Vincent Coubard 258:e5ef65120b06 350 // assert the len in input is correct for this characteristic
Vincent Coubard 258:e5ef65120b06 351 const GattAttribute& value_attribute = characteristic->getValueAttribute();
Vincent Coubard 258:e5ef65120b06 352
Vincent Coubard 258:e5ef65120b06 353 // reject write if the lenght exceed the maximum lenght of this attribute
Vincent Coubard 258:e5ef65120b06 354 if (value_attribute.getMaxLength() < len) {
Vincent Coubard 258:e5ef65120b06 355 return BLE_ERROR_INVALID_PARAM;
Vincent Coubard 258:e5ef65120b06 356 }
Vincent Coubard 258:e5ef65120b06 357
Vincent Coubard 258:e5ef65120b06 358 // reject write if the attribute size is fixed and the lenght in input is different than the
Vincent Coubard 258:e5ef65120b06 359 // length of the attribute.
Vincent Coubard 258:e5ef65120b06 360 if (value_attribute.hasVariableLength() == false && value_attribute.getLength() != len) {
Vincent Coubard 258:e5ef65120b06 361 return BLE_ERROR_INVALID_PARAM;
Vincent Coubard 258:e5ef65120b06 362 }
Vincent Coubard 258:e5ef65120b06 363
Andrea Palmieri 142:adf1567d3900 364 tBleStatus ret;
Andrea Palmieri 229:9981f62cdb1a 365
Andrea Palmieri 229:9981f62cdb1a 366 uint16_t charHandle = attributeHandle-BlueNRGGattServer::CHAR_VALUE_HANDLE;
Wolfgang Betz 130:770ce14d3d15 367
Andrea Palmieri 229:9981f62cdb1a 368 PRINTF("updating bleCharacteristic valueHandle=%u,\
Andrea Palmieri 229:9981f62cdb1a 369 corresponding serviceHandle=%u len=%d\n\r",
Andrea Palmieri 229:9981f62cdb1a 370 attributeHandle, bleCharHandleMap.find(charHandle)->second, len);
Andrea Palmieri 229:9981f62cdb1a 371
Andrea Palmieri 233:488e0e8b9b43 372 /*
Andrea Palmieri 233:488e0e8b9b43 373 * If notifications (or indications) are enabled on that characteristic, a notification (or indication)
Andrea Palmieri 233:488e0e8b9b43 374 * will be sent to the client after sending this command to the BlueNRG.
Andrea Palmieri 233:488e0e8b9b43 375 */
Andrea Palmieri 229:9981f62cdb1a 376 ret = aci_gatt_update_char_value(bleCharHandleMap.find(charHandle)->second, charHandle, 0, len, buffer);
Wolfgang Betz 130:770ce14d3d15 377
Wolfgang Betz 130:770ce14d3d15 378 if (ret != BLE_STATUS_SUCCESS){
Andrea Palmieri 207:b9df918d6d5a 379 PRINTF("Error while updating characteristic (ret=0x%x).\n\r", ret);
Andrea Palmieri 207:b9df918d6d5a 380 switch (ret) {
Andrea Palmieri 207:b9df918d6d5a 381 case BLE_STATUS_INVALID_HANDLE:
Andrea Palmieri 207:b9df918d6d5a 382 case BLE_STATUS_INVALID_PARAMETER:
Andrea Palmieri 207:b9df918d6d5a 383 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 207:b9df918d6d5a 384 default:
Andrea Palmieri 207:b9df918d6d5a 385 return BLE_STACK_BUSY;
Andrea Palmieri 207:b9df918d6d5a 386 }
Wolfgang Betz 130:770ce14d3d15 387 }
Wolfgang Betz 130:770ce14d3d15 388
Wolfgang Betz 130:770ce14d3d15 389 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 390 }
Wolfgang Betz 130:770ce14d3d15 391
Wolfgang Betz 130:770ce14d3d15 392 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 393 /*!
Wolfgang Betz 130:770ce14d3d15 394 @brief Reads a value according to the handle provided
Wolfgang Betz 130:770ce14d3d15 395
Andrea Palmieri 229:9981f62cdb1a 396 @param[in] attributeHandle
Andrea Palmieri 229:9981f62cdb1a 397 The handle of the attribute to read from
Wolfgang Betz 130:770ce14d3d15 398
Wolfgang Betz 130:770ce14d3d15 399 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 400
Wolfgang Betz 130:770ce14d3d15 401 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 402 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 403
Wolfgang Betz 130:770ce14d3d15 404 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 405
Wolfgang Betz 130:770ce14d3d15 406 @code
Wolfgang Betz 130:770ce14d3d15 407
Wolfgang Betz 130:770ce14d3d15 408 @endcode
Wolfgang Betz 130:770ce14d3d15 409 */
Wolfgang Betz 130:770ce14d3d15 410 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 411 ble_error_t BlueNRGGattServer::Read_Request_CB(uint16_t attributeHandle)
Wolfgang Betz 130:770ce14d3d15 412 {
Wolfgang Betz 130:770ce14d3d15 413 uint16_t gapConnectionHandle = BlueNRGGap::getInstance().getConnectionHandle();
Vincent Coubard 258:e5ef65120b06 414
Wolfgang Betz 130:770ce14d3d15 415 GattReadCallbackParams readParams;
Andrea Palmieri 229:9981f62cdb1a 416 readParams.handle = attributeHandle;
Wolfgang Betz 130:770ce14d3d15 417
Andrea Palmieri 229:9981f62cdb1a 418 //PRINTF("readParams.handle = %d\n\r", readParams.handle);
Wolfgang Betz 130:770ce14d3d15 419 HCIDataReadEvent(&readParams);
Vincent Coubard 258:e5ef65120b06 420
Wolfgang Betz 130:770ce14d3d15 421 //EXIT:
Wolfgang Betz 130:770ce14d3d15 422 if(gapConnectionHandle != 0){
Wolfgang Betz 132:51056160fa4a 423 //PRINTF("Calling aci_gatt_allow_read\n\r");
Wolfgang Betz 130:770ce14d3d15 424 aci_gatt_allow_read(gapConnectionHandle);
Wolfgang Betz 130:770ce14d3d15 425 }
Vincent Coubard 258:e5ef65120b06 426
Wolfgang Betz 130:770ce14d3d15 427 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 428 }
Wolfgang Betz 130:770ce14d3d15 429
Wolfgang Betz 130:770ce14d3d15 430 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 431 /*!
Wolfgang Betz 130:770ce14d3d15 432 @brief Returns the GattCharacteristic according to the handle provided
Wolfgang Betz 130:770ce14d3d15 433
Andrea Palmieri 229:9981f62cdb1a 434 @param[in] attrHandle
Andrea Palmieri 229:9981f62cdb1a 435 The handle of the attribute
Wolfgang Betz 130:770ce14d3d15 436
Wolfgang Betz 130:770ce14d3d15 437 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 438
Wolfgang Betz 130:770ce14d3d15 439 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 440 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 441
Wolfgang Betz 130:770ce14d3d15 442 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 443
Wolfgang Betz 130:770ce14d3d15 444 @code
Wolfgang Betz 130:770ce14d3d15 445
Wolfgang Betz 130:770ce14d3d15 446 @endcode
Wolfgang Betz 130:770ce14d3d15 447 */
Wolfgang Betz 130:770ce14d3d15 448 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 449 GattCharacteristic* BlueNRGGattServer::getCharacteristicFromHandle(uint16_t attrHandle)
Wolfgang Betz 130:770ce14d3d15 450 {
Wolfgang Betz 130:770ce14d3d15 451 GattCharacteristic *p_char = NULL;
Wolfgang Betz 130:770ce14d3d15 452 int i;
Wolfgang Betz 130:770ce14d3d15 453 uint16_t handle, handle_1;
Wolfgang Betz 130:770ce14d3d15 454
Andrea Palmieri 229:9981f62cdb1a 455 PRINTF("BlueNRGGattServer::getCharacteristicFromHandle()>>Attr Handle received %d\n\r",attrHandle);
Wolfgang Betz 130:770ce14d3d15 456 for(i=0; i<characteristicCount; i++)
Wolfgang Betz 130:770ce14d3d15 457 {
Andrea Palmieri 229:9981f62cdb1a 458 handle = p_characteristics[i]->getValueAttribute().getHandle()-BlueNRGGattServer::CHAR_VALUE_HANDLE;
Wolfgang Betz 132:51056160fa4a 459 PRINTF("handle(%d)=%d\n\r", i, handle);
Wolfgang Betz 130:770ce14d3d15 460 if(i==characteristicCount-1)//Last Characteristic check
Wolfgang Betz 130:770ce14d3d15 461 {
Wolfgang Betz 130:770ce14d3d15 462 if(attrHandle>=handle)
Wolfgang Betz 130:770ce14d3d15 463 {
Wolfgang Betz 130:770ce14d3d15 464 p_char = p_characteristics[i];
Wolfgang Betz 132:51056160fa4a 465 PRINTF("Found Characteristic Properties 0x%x (handle=%d)\n\r",p_char->getProperties(), handle);
Wolfgang Betz 130:770ce14d3d15 466 break;
Vincent Coubard 258:e5ef65120b06 467 }
Wolfgang Betz 130:770ce14d3d15 468 }
Wolfgang Betz 130:770ce14d3d15 469 else {
Andrea Palmieri 229:9981f62cdb1a 470 handle_1 = p_characteristics[i+1]->getValueAttribute().getHandle()-BlueNRGGattServer::CHAR_VALUE_HANDLE;
Wolfgang Betz 130:770ce14d3d15 471 //Testing if attribute handle is between two Characteristic Handles
Wolfgang Betz 130:770ce14d3d15 472 if(attrHandle>=handle && attrHandle<handle_1)
Wolfgang Betz 130:770ce14d3d15 473 {
Wolfgang Betz 130:770ce14d3d15 474 p_char = p_characteristics[i];
Wolfgang Betz 132:51056160fa4a 475 PRINTF("Found Characteristic Properties 0x%x (handle=%d handle_1=%d)\n\r",p_char->getProperties(), handle, handle_1);
Wolfgang Betz 130:770ce14d3d15 476 break;
Wolfgang Betz 130:770ce14d3d15 477 } else continue;
Wolfgang Betz 130:770ce14d3d15 478 }
Wolfgang Betz 130:770ce14d3d15 479 }
Wolfgang Betz 130:770ce14d3d15 480
Wolfgang Betz 130:770ce14d3d15 481 return p_char;
Wolfgang Betz 130:770ce14d3d15 482 }
Wolfgang Betz 130:770ce14d3d15 483
Wolfgang Betz 130:770ce14d3d15 484 void BlueNRGGattServer::HCIDataWrittenEvent(const GattWriteCallbackParams *params) {
Wolfgang Betz 130:770ce14d3d15 485 this->handleDataWrittenEvent(params);
Wolfgang Betz 130:770ce14d3d15 486 }
Vincent Coubard 258:e5ef65120b06 487
Wolfgang Betz 130:770ce14d3d15 488 void BlueNRGGattServer::HCIDataReadEvent(const GattReadCallbackParams *params) {
Wolfgang Betz 132:51056160fa4a 489 PRINTF("Called HCIDataReadEvent\n\r");
Wolfgang Betz 130:770ce14d3d15 490 this->handleDataReadEvent(params);
Wolfgang Betz 130:770ce14d3d15 491 }
Wolfgang Betz 130:770ce14d3d15 492
Wolfgang Betz 130:770ce14d3d15 493 void BlueNRGGattServer::HCIEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) {
Wolfgang Betz 130:770ce14d3d15 494 this->handleEvent(type, charHandle);
Wolfgang Betz 130:770ce14d3d15 495 }
Wolfgang Betz 130:770ce14d3d15 496
Wolfgang Betz 130:770ce14d3d15 497 void BlueNRGGattServer::HCIDataSentEvent(unsigned count) {
Wolfgang Betz 130:770ce14d3d15 498 this->handleDataSentEvent(count);
Wolfgang Betz 130:770ce14d3d15 499 }
Wolfgang Betz 130:770ce14d3d15 500
Vincent Coubard 258:e5ef65120b06 501
Wolfgang Betz 130:770ce14d3d15 502 ble_error_t BlueNRGGattServer::initializeGATTDatabase(void) {
Vincent Coubard 258:e5ef65120b06 503 // <TODO>
Vincent Coubard 258:e5ef65120b06 504 return (ble_error_t)0;
Wolfgang Betz 130:770ce14d3d15 505 }
Andrea Palmieri 229:9981f62cdb1a 506
Andrea Palmieri 229:9981f62cdb1a 507 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 508 /*!
Andrea Palmieri 229:9981f62cdb1a 509 @brief Clear BlueNRGGattServer's state.
Andrea Palmieri 229:9981f62cdb1a 510
Andrea Palmieri 229:9981f62cdb1a 511 @returns ble_error_t
Andrea Palmieri 229:9981f62cdb1a 512
Andrea Palmieri 229:9981f62cdb1a 513 @retval BLE_ERROR_NONE
Andrea Palmieri 229:9981f62cdb1a 514 Everything executed properly
Andrea Palmieri 229:9981f62cdb1a 515 */
Andrea Palmieri 229:9981f62cdb1a 516 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 517 ble_error_t BlueNRGGattServer::reset(void)
Andrea Palmieri 229:9981f62cdb1a 518 {
Andrea Palmieri 229:9981f62cdb1a 519 /* Clear all state that is from the parent, including private members */
Andrea Palmieri 229:9981f62cdb1a 520 if (GattServer::reset() != BLE_ERROR_NONE) {
Andrea Palmieri 229:9981f62cdb1a 521 return BLE_ERROR_INVALID_STATE;
Andrea Palmieri 229:9981f62cdb1a 522 }
Andrea Palmieri 229:9981f62cdb1a 523
Andrea Palmieri 229:9981f62cdb1a 524 /* Clear class members */
Andrea Palmieri 229:9981f62cdb1a 525 memset(p_characteristics, 0, sizeof(p_characteristics));
Andrea Palmieri 229:9981f62cdb1a 526 memset(bleCharacteristicHandles, 0, sizeof(bleCharacteristicHandles));
Andrea Palmieri 229:9981f62cdb1a 527 serviceCount = 0;
Andrea Palmieri 229:9981f62cdb1a 528 characteristicCount = 0;
Andrea Palmieri 229:9981f62cdb1a 529
Andrea Palmieri 229:9981f62cdb1a 530 return BLE_ERROR_NONE;
Vincent Coubard 258:e5ef65120b06 531 }