BLE test

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Andrea Palmieri
Date:
Mon May 23 18:32:06 2016 +0200
Revision:
233:488e0e8b9b43
Parent:
229:9981f62cdb1a
Child:
242:058b2e731adc
Minor change

Signed-off-by: Andrea Palmieri <andrea.palmieri@st.com>

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 ******************************************************************************
Wolfgang Betz 130:770ce14d3d15 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 */
Wolfgang Betz 130:770ce14d3d15 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 */
Wolfgang Betz 130:770ce14d3d15 38
Wolfgang Betz 130:770ce14d3d15 39 #include "BlueNRGGattServer.h"
Wolfgang Betz 144:bdf5e8432131 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
Wolfgang Betz 130:770ce14d3d15 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 */
Wolfgang Betz 130:770ce14d3d15 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;
Wolfgang Betz 130:770ce14d3d15 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);
Wolfgang Betz 130:770ce14d3d15 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);
Wolfgang Betz 130:770ce14d3d15 88
Wolfgang Betz 130:770ce14d3d15 89 if(type==UUID::UUID_TYPE_LONG) {
Wolfgang Betz 130:770ce14d3d15 90 base_uuid = (service.getUUID()).getBaseUUID();
Wolfgang Betz 130:770ce14d3d15 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 }
Wolfgang Betz 130:770ce14d3d15 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;
Wolfgang Betz 130:770ce14d3d15 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 }
Wolfgang Betz 130:770ce14d3d15 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");
Wolfgang Betz 130:770ce14d3d15 154 Gatt_Evt_Mask = Gatt_Evt_Mask | 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");
Wolfgang Betz 130:770ce14d3d15 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);
Wolfgang Betz 130:770ce14d3d15 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);
Wolfgang Betz 130:770ce14d3d15 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 }
Wolfgang Betz 130:770ce14d3d15 193
Andrea Palmieri 229:9981f62cdb1a 194 bleCharHandleMap.insert(std::pair<uint16_t, uint16_t>(bleCharacteristic, servHandle));
Wolfgang Betz 130:770ce14d3d15 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());
Wolfgang Betz 130:770ce14d3d15 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
Wolfgang Betz 130:770ce14d3d15 235 }
Wolfgang Betz 130:770ce14d3d15 236
Wolfgang Betz 130:770ce14d3d15 237 serviceCount++;
Wolfgang Betz 130:770ce14d3d15 238
Wolfgang Betz 130:770ce14d3d15 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.
Wolfgang Betz 130:770ce14d3d15 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;
Andrea Palmieri 229:9981f62cdb1a 272 uint16_t charHandle = attributeHandle-BlueNRGGattServer::CHAR_VALUE_HANDLE;
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 }
Wolfgang Betz 130:770ce14d3d15 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
Andrea Palmieri 142:adf1567d3900 344 tBleStatus ret;
Andrea Palmieri 229:9981f62cdb1a 345
Andrea Palmieri 229:9981f62cdb1a 346 uint16_t charHandle = attributeHandle-BlueNRGGattServer::CHAR_VALUE_HANDLE;
Wolfgang Betz 130:770ce14d3d15 347
Andrea Palmieri 229:9981f62cdb1a 348 PRINTF("updating bleCharacteristic valueHandle=%u,\
Andrea Palmieri 229:9981f62cdb1a 349 corresponding serviceHandle=%u len=%d\n\r",
Andrea Palmieri 229:9981f62cdb1a 350 attributeHandle, bleCharHandleMap.find(charHandle)->second, len);
Andrea Palmieri 229:9981f62cdb1a 351
Andrea Palmieri 233:488e0e8b9b43 352 /*
Andrea Palmieri 233:488e0e8b9b43 353 * If notifications (or indications) are enabled on that characteristic, a notification (or indication)
Andrea Palmieri 233:488e0e8b9b43 354 * will be sent to the client after sending this command to the BlueNRG.
Andrea Palmieri 233:488e0e8b9b43 355 */
Andrea Palmieri 229:9981f62cdb1a 356 ret = aci_gatt_update_char_value(bleCharHandleMap.find(charHandle)->second, charHandle, 0, len, buffer);
Wolfgang Betz 130:770ce14d3d15 357
Wolfgang Betz 130:770ce14d3d15 358 if (ret != BLE_STATUS_SUCCESS){
Andrea Palmieri 207:b9df918d6d5a 359 PRINTF("Error while updating characteristic (ret=0x%x).\n\r", ret);
Andrea Palmieri 207:b9df918d6d5a 360 switch (ret) {
Andrea Palmieri 207:b9df918d6d5a 361 case BLE_STATUS_INVALID_HANDLE:
Andrea Palmieri 207:b9df918d6d5a 362 case BLE_STATUS_INVALID_PARAMETER:
Andrea Palmieri 207:b9df918d6d5a 363 return BLE_ERROR_INVALID_PARAM;
Andrea Palmieri 207:b9df918d6d5a 364 default:
Andrea Palmieri 207:b9df918d6d5a 365 return BLE_STACK_BUSY;
Andrea Palmieri 207:b9df918d6d5a 366 }
Wolfgang Betz 130:770ce14d3d15 367 }
Wolfgang Betz 130:770ce14d3d15 368
Wolfgang Betz 130:770ce14d3d15 369 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 370 }
Wolfgang Betz 130:770ce14d3d15 371
Wolfgang Betz 130:770ce14d3d15 372 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 373 /*!
Wolfgang Betz 130:770ce14d3d15 374 @brief Reads a value according to the handle provided
Wolfgang Betz 130:770ce14d3d15 375
Andrea Palmieri 229:9981f62cdb1a 376 @param[in] attributeHandle
Andrea Palmieri 229:9981f62cdb1a 377 The handle of the attribute to read from
Wolfgang Betz 130:770ce14d3d15 378
Wolfgang Betz 130:770ce14d3d15 379 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 380
Wolfgang Betz 130:770ce14d3d15 381 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 382 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 383
Wolfgang Betz 130:770ce14d3d15 384 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 385
Wolfgang Betz 130:770ce14d3d15 386 @code
Wolfgang Betz 130:770ce14d3d15 387
Wolfgang Betz 130:770ce14d3d15 388 @endcode
Wolfgang Betz 130:770ce14d3d15 389 */
Wolfgang Betz 130:770ce14d3d15 390 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 391 ble_error_t BlueNRGGattServer::Read_Request_CB(uint16_t attributeHandle)
Wolfgang Betz 130:770ce14d3d15 392 {
Wolfgang Betz 130:770ce14d3d15 393 uint16_t gapConnectionHandle = BlueNRGGap::getInstance().getConnectionHandle();
Wolfgang Betz 130:770ce14d3d15 394
Wolfgang Betz 130:770ce14d3d15 395 GattReadCallbackParams readParams;
Andrea Palmieri 229:9981f62cdb1a 396 readParams.handle = attributeHandle;
Wolfgang Betz 130:770ce14d3d15 397
Andrea Palmieri 229:9981f62cdb1a 398 //PRINTF("readParams.handle = %d\n\r", readParams.handle);
Wolfgang Betz 130:770ce14d3d15 399 HCIDataReadEvent(&readParams);
Wolfgang Betz 130:770ce14d3d15 400
Wolfgang Betz 130:770ce14d3d15 401 //EXIT:
Wolfgang Betz 130:770ce14d3d15 402 if(gapConnectionHandle != 0){
Wolfgang Betz 132:51056160fa4a 403 //PRINTF("Calling aci_gatt_allow_read\n\r");
Wolfgang Betz 130:770ce14d3d15 404 aci_gatt_allow_read(gapConnectionHandle);
Wolfgang Betz 130:770ce14d3d15 405 }
Wolfgang Betz 130:770ce14d3d15 406
Wolfgang Betz 130:770ce14d3d15 407 return BLE_ERROR_NONE;
Wolfgang Betz 130:770ce14d3d15 408 }
Wolfgang Betz 130:770ce14d3d15 409
Wolfgang Betz 130:770ce14d3d15 410 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 411 /*!
Wolfgang Betz 130:770ce14d3d15 412 @brief Returns the GattCharacteristic according to the handle provided
Wolfgang Betz 130:770ce14d3d15 413
Andrea Palmieri 229:9981f62cdb1a 414 @param[in] attrHandle
Andrea Palmieri 229:9981f62cdb1a 415 The handle of the attribute
Wolfgang Betz 130:770ce14d3d15 416
Wolfgang Betz 130:770ce14d3d15 417 @returns ble_error_t
Wolfgang Betz 130:770ce14d3d15 418
Wolfgang Betz 130:770ce14d3d15 419 @retval BLE_ERROR_NONE
Wolfgang Betz 130:770ce14d3d15 420 Everything executed properly
Wolfgang Betz 130:770ce14d3d15 421
Wolfgang Betz 130:770ce14d3d15 422 @section EXAMPLE
Wolfgang Betz 130:770ce14d3d15 423
Wolfgang Betz 130:770ce14d3d15 424 @code
Wolfgang Betz 130:770ce14d3d15 425
Wolfgang Betz 130:770ce14d3d15 426 @endcode
Wolfgang Betz 130:770ce14d3d15 427 */
Wolfgang Betz 130:770ce14d3d15 428 /**************************************************************************/
Wolfgang Betz 130:770ce14d3d15 429 GattCharacteristic* BlueNRGGattServer::getCharacteristicFromHandle(uint16_t attrHandle)
Wolfgang Betz 130:770ce14d3d15 430 {
Wolfgang Betz 130:770ce14d3d15 431 GattCharacteristic *p_char = NULL;
Wolfgang Betz 130:770ce14d3d15 432 int i;
Wolfgang Betz 130:770ce14d3d15 433 uint16_t handle, handle_1;
Wolfgang Betz 130:770ce14d3d15 434
Andrea Palmieri 229:9981f62cdb1a 435 PRINTF("BlueNRGGattServer::getCharacteristicFromHandle()>>Attr Handle received %d\n\r",attrHandle);
Wolfgang Betz 130:770ce14d3d15 436 for(i=0; i<characteristicCount; i++)
Wolfgang Betz 130:770ce14d3d15 437 {
Andrea Palmieri 229:9981f62cdb1a 438 handle = p_characteristics[i]->getValueAttribute().getHandle()-BlueNRGGattServer::CHAR_VALUE_HANDLE;
Wolfgang Betz 132:51056160fa4a 439 PRINTF("handle(%d)=%d\n\r", i, handle);
Wolfgang Betz 130:770ce14d3d15 440 if(i==characteristicCount-1)//Last Characteristic check
Wolfgang Betz 130:770ce14d3d15 441 {
Wolfgang Betz 130:770ce14d3d15 442 if(attrHandle>=handle)
Wolfgang Betz 130:770ce14d3d15 443 {
Wolfgang Betz 130:770ce14d3d15 444 p_char = p_characteristics[i];
Wolfgang Betz 132:51056160fa4a 445 PRINTF("Found Characteristic Properties 0x%x (handle=%d)\n\r",p_char->getProperties(), handle);
Wolfgang Betz 130:770ce14d3d15 446 break;
Wolfgang Betz 130:770ce14d3d15 447 }
Wolfgang Betz 130:770ce14d3d15 448 }
Wolfgang Betz 130:770ce14d3d15 449 else {
Andrea Palmieri 229:9981f62cdb1a 450 handle_1 = p_characteristics[i+1]->getValueAttribute().getHandle()-BlueNRGGattServer::CHAR_VALUE_HANDLE;
Wolfgang Betz 130:770ce14d3d15 451 //Testing if attribute handle is between two Characteristic Handles
Wolfgang Betz 130:770ce14d3d15 452 if(attrHandle>=handle && attrHandle<handle_1)
Wolfgang Betz 130:770ce14d3d15 453 {
Wolfgang Betz 130:770ce14d3d15 454 p_char = p_characteristics[i];
Wolfgang Betz 132:51056160fa4a 455 PRINTF("Found Characteristic Properties 0x%x (handle=%d handle_1=%d)\n\r",p_char->getProperties(), handle, handle_1);
Wolfgang Betz 130:770ce14d3d15 456 break;
Wolfgang Betz 130:770ce14d3d15 457 } else continue;
Wolfgang Betz 130:770ce14d3d15 458 }
Wolfgang Betz 130:770ce14d3d15 459 }
Wolfgang Betz 130:770ce14d3d15 460
Wolfgang Betz 130:770ce14d3d15 461 return p_char;
Wolfgang Betz 130:770ce14d3d15 462 }
Wolfgang Betz 130:770ce14d3d15 463
Wolfgang Betz 130:770ce14d3d15 464 void BlueNRGGattServer::HCIDataWrittenEvent(const GattWriteCallbackParams *params) {
Wolfgang Betz 130:770ce14d3d15 465 this->handleDataWrittenEvent(params);
Wolfgang Betz 130:770ce14d3d15 466 }
Wolfgang Betz 130:770ce14d3d15 467
Wolfgang Betz 130:770ce14d3d15 468 void BlueNRGGattServer::HCIDataReadEvent(const GattReadCallbackParams *params) {
Wolfgang Betz 132:51056160fa4a 469 PRINTF("Called HCIDataReadEvent\n\r");
Wolfgang Betz 130:770ce14d3d15 470 this->handleDataReadEvent(params);
Wolfgang Betz 130:770ce14d3d15 471 }
Wolfgang Betz 130:770ce14d3d15 472
Wolfgang Betz 130:770ce14d3d15 473 void BlueNRGGattServer::HCIEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) {
Wolfgang Betz 130:770ce14d3d15 474 this->handleEvent(type, charHandle);
Wolfgang Betz 130:770ce14d3d15 475 }
Wolfgang Betz 130:770ce14d3d15 476
Wolfgang Betz 130:770ce14d3d15 477 void BlueNRGGattServer::HCIDataSentEvent(unsigned count) {
Wolfgang Betz 130:770ce14d3d15 478 this->handleDataSentEvent(count);
Wolfgang Betz 130:770ce14d3d15 479 }
Wolfgang Betz 130:770ce14d3d15 480
Wolfgang Betz 130:770ce14d3d15 481
Wolfgang Betz 130:770ce14d3d15 482 ble_error_t BlueNRGGattServer::initializeGATTDatabase(void) {
Wolfgang Betz 130:770ce14d3d15 483 // <TODO>
Wolfgang Betz 130:770ce14d3d15 484 return (ble_error_t)0;
Wolfgang Betz 130:770ce14d3d15 485 }
Andrea Palmieri 229:9981f62cdb1a 486
Andrea Palmieri 229:9981f62cdb1a 487 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 488 /*!
Andrea Palmieri 229:9981f62cdb1a 489 @brief Clear BlueNRGGattServer's state.
Andrea Palmieri 229:9981f62cdb1a 490
Andrea Palmieri 229:9981f62cdb1a 491 @returns ble_error_t
Andrea Palmieri 229:9981f62cdb1a 492
Andrea Palmieri 229:9981f62cdb1a 493 @retval BLE_ERROR_NONE
Andrea Palmieri 229:9981f62cdb1a 494 Everything executed properly
Andrea Palmieri 229:9981f62cdb1a 495 */
Andrea Palmieri 229:9981f62cdb1a 496 /**************************************************************************/
Andrea Palmieri 229:9981f62cdb1a 497 ble_error_t BlueNRGGattServer::reset(void)
Andrea Palmieri 229:9981f62cdb1a 498 {
Andrea Palmieri 229:9981f62cdb1a 499 /* Clear all state that is from the parent, including private members */
Andrea Palmieri 229:9981f62cdb1a 500 if (GattServer::reset() != BLE_ERROR_NONE) {
Andrea Palmieri 229:9981f62cdb1a 501 return BLE_ERROR_INVALID_STATE;
Andrea Palmieri 229:9981f62cdb1a 502 }
Andrea Palmieri 229:9981f62cdb1a 503
Andrea Palmieri 229:9981f62cdb1a 504 /* Clear class members */
Andrea Palmieri 229:9981f62cdb1a 505 memset(p_characteristics, 0, sizeof(p_characteristics));
Andrea Palmieri 229:9981f62cdb1a 506 memset(bleCharacteristicHandles, 0, sizeof(bleCharacteristicHandles));
Andrea Palmieri 229:9981f62cdb1a 507 serviceCount = 0;
Andrea Palmieri 229:9981f62cdb1a 508 characteristicCount = 0;
Andrea Palmieri 229:9981f62cdb1a 509
Andrea Palmieri 229:9981f62cdb1a 510 return BLE_ERROR_NONE;
Andrea Palmieri 229:9981f62cdb1a 511 }