Previous version which works for my stm32f401 Nucleo board

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Wolfgang Betz
Date:
Tue Oct 06 12:12:37 2015 +0200
Revision:
128:46ae62a90136
Parent:
125:2e468f07a94f
Merge branch 'master' of hg::http://wobetz@developer.mbed.org/teams/ST/code/X_NUCLEO_IDB0XA1 into idb0xa1-split

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wolfgang Betz 90:26c0c9807ab4 1 /* mbed Microcontroller Library
Wolfgang Betz 90:26c0c9807ab4 2 * Copyright (c) 2006-2013 ARM Limited
Wolfgang Betz 90:26c0c9807ab4 3 *
Wolfgang Betz 90:26c0c9807ab4 4 * Licensed under the Apache License, Version 2.0 (the "License");
Wolfgang Betz 90:26c0c9807ab4 5 * you may not use this file except in compliance with the License.
Wolfgang Betz 90:26c0c9807ab4 6 * You may obtain a copy of the License at
Wolfgang Betz 90:26c0c9807ab4 7 *
Wolfgang Betz 90:26c0c9807ab4 8 * http://www.apache.org/licenses/LICENSE-2.0
Wolfgang Betz 90:26c0c9807ab4 9 *
Wolfgang Betz 90:26c0c9807ab4 10 * Unless required by applicable law or agreed to in writing, software
Wolfgang Betz 90:26c0c9807ab4 11 * distributed under the License is distributed on an "AS IS" BASIS,
Wolfgang Betz 90:26c0c9807ab4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Wolfgang Betz 90:26c0c9807ab4 13 * See the License for the specific language governing permissions and
Wolfgang Betz 90:26c0c9807ab4 14 * limitations under the License.
Wolfgang Betz 90:26c0c9807ab4 15 */
Wolfgang Betz 90:26c0c9807ab4 16 /**
Wolfgang Betz 90:26c0c9807ab4 17 ******************************************************************************
Wolfgang Betz 90:26c0c9807ab4 18 * @file BlueNRGGattServer.cpp
Wolfgang Betz 90:26c0c9807ab4 19 * @author STMicroelectronics
Wolfgang Betz 90:26c0c9807ab4 20 * @brief Implementation of BlueNRG BLE_API GattServer Class
Wolfgang Betz 90:26c0c9807ab4 21 ******************************************************************************
Wolfgang Betz 90:26c0c9807ab4 22 * @copy
Wolfgang Betz 90:26c0c9807ab4 23 *
Wolfgang Betz 90:26c0c9807ab4 24 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
Wolfgang Betz 90:26c0c9807ab4 25 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
Wolfgang Betz 90:26c0c9807ab4 26 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
Wolfgang Betz 90:26c0c9807ab4 27 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
Wolfgang Betz 90:26c0c9807ab4 28 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
Wolfgang Betz 90:26c0c9807ab4 29 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
Wolfgang Betz 90:26c0c9807ab4 30 *
Wolfgang Betz 90:26c0c9807ab4 31 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
Wolfgang Betz 90:26c0c9807ab4 32 */
Wolfgang Betz 90:26c0c9807ab4 33
Wolfgang Betz 90:26c0c9807ab4 34
Wolfgang Betz 90:26c0c9807ab4 35 // ANDREA: Changed some types (e.g., tHalUint8 --> uint8_t)
Wolfgang Betz 90:26c0c9807ab4 36
Wolfgang Betz 90:26c0c9807ab4 37 /** @defgroup BlueNRGGATTSERVER
Wolfgang Betz 90:26c0c9807ab4 38 * @brief BlueNRG BLE_API GattServer Adaptation
Wolfgang Betz 90:26c0c9807ab4 39 * @{
Wolfgang Betz 90:26c0c9807ab4 40 */
Wolfgang Betz 90:26c0c9807ab4 41
Wolfgang Betz 90:26c0c9807ab4 42 #include "BlueNRGGattServer.h"
Wolfgang Betz 90:26c0c9807ab4 43 #include "mbed.h"
Wolfgang Betz 90:26c0c9807ab4 44 #include "BlueNRGGap.h"
Wolfgang Betz 90:26c0c9807ab4 45 #include "Utils.h"
Wolfgang Betz 90:26c0c9807ab4 46
Wolfgang Betz 90:26c0c9807ab4 47 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 48 /*!
Wolfgang Betz 90:26c0c9807ab4 49 @brief Adds a new service to the GATT table on the peripheral
Wolfgang Betz 90:26c0c9807ab4 50
Wolfgang Betz 90:26c0c9807ab4 51 @params[in] service
Wolfgang Betz 90:26c0c9807ab4 52 Pointer to instance of the Gatt Server to add
Wolfgang Betz 90:26c0c9807ab4 53
Wolfgang Betz 90:26c0c9807ab4 54 @returns ble_error_t
Wolfgang Betz 90:26c0c9807ab4 55
Wolfgang Betz 90:26c0c9807ab4 56 @retval BLE_ERROR_NONE
Wolfgang Betz 90:26c0c9807ab4 57 Everything executed properly
Wolfgang Betz 90:26c0c9807ab4 58
Wolfgang Betz 90:26c0c9807ab4 59 @section EXAMPLE
Wolfgang Betz 90:26c0c9807ab4 60
Wolfgang Betz 90:26c0c9807ab4 61 @code
Wolfgang Betz 90:26c0c9807ab4 62
Wolfgang Betz 90:26c0c9807ab4 63 @endcode
Wolfgang Betz 90:26c0c9807ab4 64 */
Wolfgang Betz 90:26c0c9807ab4 65 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 66 ble_error_t BlueNRGGattServer::addService(GattService &service)
Wolfgang Betz 90:26c0c9807ab4 67 {
Wolfgang Betz 90:26c0c9807ab4 68 /* ToDo: Make sure we don't overflow the array, etc. */
Wolfgang Betz 90:26c0c9807ab4 69 /* ToDo: Make sure this service UUID doesn't already exist (?) */
Wolfgang Betz 90:26c0c9807ab4 70 /* ToDo: Basic validation */
Wolfgang Betz 90:26c0c9807ab4 71
Wolfgang Betz 90:26c0c9807ab4 72 tBleStatus ret;
Wolfgang Betz 90:26c0c9807ab4 73 uint8_t type;
Wolfgang Betz 90:26c0c9807ab4 74 uint16_t short_uuid;
Wolfgang Betz 90:26c0c9807ab4 75 uint8_t primary_short_uuid[2];
Wolfgang Betz 90:26c0c9807ab4 76 uint8_t primary_base_uuid[16];
Wolfgang Betz 90:26c0c9807ab4 77 uint8_t char_base_uuid[16];
Wolfgang Betz 90:26c0c9807ab4 78 const uint8_t *base_uuid;
Wolfgang Betz 90:26c0c9807ab4 79 const uint8_t *base_char_uuid;
Wolfgang Betz 90:26c0c9807ab4 80
Wolfgang Betz 90:26c0c9807ab4 81 uint8_t charsCount = 0;
Wolfgang Betz 90:26c0c9807ab4 82 uint8_t maxAttrRecords = 0;
Wolfgang Betz 90:26c0c9807ab4 83
Wolfgang Betz 90:26c0c9807ab4 84 type = (service.getUUID()).shortOrLong();
Wolfgang Betz 90:26c0c9807ab4 85 DEBUG("AddService(): Type:%d\n\r", type);
Wolfgang Betz 90:26c0c9807ab4 86
Wolfgang Betz 90:26c0c9807ab4 87 /* Add the service to the BlueNRG */
Wolfgang Betz 90:26c0c9807ab4 88 short_uuid = (service.getUUID()).getShortUUID();
Wolfgang Betz 90:26c0c9807ab4 89 STORE_LE_16(primary_short_uuid, short_uuid);
Wolfgang Betz 90:26c0c9807ab4 90
Wolfgang Betz 90:26c0c9807ab4 91 if(type==UUID::UUID_TYPE_LONG) {
Wolfgang Betz 90:26c0c9807ab4 92 base_uuid = (service.getUUID()).getBaseUUID();
Wolfgang Betz 90:26c0c9807ab4 93
Wolfgang Betz 90:26c0c9807ab4 94 COPY_UUID_128(primary_base_uuid, base_uuid[15],base_uuid[14],base_uuid[13],base_uuid[12],base_uuid[11],base_uuid[10],base_uuid[9],
Wolfgang Betz 90:26c0c9807ab4 95 base_uuid[8],base_uuid[7],base_uuid[6],base_uuid[5],base_uuid[4],primary_short_uuid[1],primary_short_uuid[0],base_uuid[1],base_uuid[0]);
Wolfgang Betz 90:26c0c9807ab4 96 }
Wolfgang Betz 90:26c0c9807ab4 97
Wolfgang Betz 90:26c0c9807ab4 98 charsCount = service.getCharacteristicCount();
Wolfgang Betz 90:26c0c9807ab4 99 //1(service record)+2records*char+1record*char_desc
Wolfgang Betz 90:26c0c9807ab4 100 maxAttrRecords = 1+3*charsCount;
Wolfgang Betz 90:26c0c9807ab4 101
Wolfgang Betz 90:26c0c9807ab4 102 if(type==UUID::UUID_TYPE_SHORT) {
Wolfgang Betz 90:26c0c9807ab4 103 ret = aci_gatt_add_serv(UUID_TYPE_16,
Wolfgang Betz 90:26c0c9807ab4 104 primary_short_uuid,
Wolfgang Betz 90:26c0c9807ab4 105 PRIMARY_SERVICE,
Wolfgang Betz 90:26c0c9807ab4 106 maxAttrRecords/*7*/,
Wolfgang Betz 90:26c0c9807ab4 107 &servHandle);
Wolfgang Betz 90:26c0c9807ab4 108 DEBUG("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret);
Wolfgang Betz 90:26c0c9807ab4 109 }
Wolfgang Betz 90:26c0c9807ab4 110 else if(type==UUID::UUID_TYPE_LONG) {
Wolfgang Betz 90:26c0c9807ab4 111 ret = aci_gatt_add_serv(UUID_TYPE_128,
Wolfgang Betz 90:26c0c9807ab4 112 primary_base_uuid,
Wolfgang Betz 90:26c0c9807ab4 113 PRIMARY_SERVICE,
Wolfgang Betz 90:26c0c9807ab4 114 maxAttrRecords/*7*/,
Wolfgang Betz 90:26c0c9807ab4 115 &servHandle);
Wolfgang Betz 90:26c0c9807ab4 116 DEBUG("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret);
Wolfgang Betz 90:26c0c9807ab4 117 }
Wolfgang Betz 90:26c0c9807ab4 118
Wolfgang Betz 90:26c0c9807ab4 119 service.setHandle(servHandle);
Wolfgang Betz 90:26c0c9807ab4 120 //serviceHandleVector.push_back(servHandle);
Wolfgang Betz 90:26c0c9807ab4 121 DEBUG("added servHandle handle =%u\n\r", servHandle);
Wolfgang Betz 90:26c0c9807ab4 122 uint16_t bleCharacteristic;
Wolfgang Betz 90:26c0c9807ab4 123
Wolfgang Betz 90:26c0c9807ab4 124 //iterate to include all characteristics
Wolfgang Betz 90:26c0c9807ab4 125 for (uint8_t i = 0; i < charsCount; i++) {
Wolfgang Betz 90:26c0c9807ab4 126 GattCharacteristic *p_char = service.getCharacteristic(i);
Wolfgang Betz 90:26c0c9807ab4 127 uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID();
Wolfgang Betz 90:26c0c9807ab4 128
Wolfgang Betz 90:26c0c9807ab4 129 uint8_t int_8_uuid[2];
Wolfgang Betz 90:26c0c9807ab4 130 STORE_LE_16(int_8_uuid, char_uuid);
Wolfgang Betz 90:26c0c9807ab4 131
Wolfgang Betz 90:26c0c9807ab4 132 if(type==UUID::UUID_TYPE_LONG) {
Wolfgang Betz 90:26c0c9807ab4 133 base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID();
Wolfgang Betz 90:26c0c9807ab4 134
Wolfgang Betz 90:26c0c9807ab4 135 COPY_UUID_128(char_base_uuid, base_char_uuid[15],base_char_uuid[14],base_char_uuid[13],base_char_uuid[12],base_char_uuid[11],base_char_uuid[10],base_char_uuid[9],
Wolfgang Betz 90:26c0c9807ab4 136 base_char_uuid[8],base_char_uuid[7],base_char_uuid[6],base_char_uuid[5],base_char_uuid[4],int_8_uuid[1],int_8_uuid[0],base_char_uuid[1],base_char_uuid[0]);
Wolfgang Betz 90:26c0c9807ab4 137 }
Wolfgang Betz 90:26c0c9807ab4 138
Wolfgang Betz 90:26c0c9807ab4 139 DEBUG("Char Properties 0x%x\n\r", p_char->getProperties());
Wolfgang Betz 90:26c0c9807ab4 140 /*
Wolfgang Betz 90:26c0c9807ab4 141 * Gatt_Evt_Mask -> HardCoded (0)
Wolfgang Betz 90:26c0c9807ab4 142 * Encryption_Key_Size -> Hardcoded (16)
Wolfgang Betz 90:26c0c9807ab4 143 * isVariable (variable length value field) -> Hardcoded (1)
Wolfgang Betz 90:26c0c9807ab4 144 */
Wolfgang Betz 90:26c0c9807ab4 145 uint8_t Gatt_Evt_Mask = 0x0;
Wolfgang Betz 90:26c0c9807ab4 146
Wolfgang Betz 90:26c0c9807ab4 147 if((p_char->getProperties() &
Wolfgang Betz 90:26c0c9807ab4 148 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
Wolfgang Betz 90:26c0c9807ab4 149 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) {
Wolfgang Betz 90:26c0c9807ab4 150 DEBUG("Setting up Gatt GATT_NOTIFY_ATTRIBUTE_WRITE Mask\n\r");
Wolfgang Betz 90:26c0c9807ab4 151 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_ATTRIBUTE_WRITE;
Wolfgang Betz 90:26c0c9807ab4 152 }
Wolfgang Betz 90:26c0c9807ab4 153 if((p_char->getProperties() &
Wolfgang Betz 90:26c0c9807ab4 154 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ|
Wolfgang Betz 90:26c0c9807ab4 155 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) {
Wolfgang Betz 90:26c0c9807ab4 156 DEBUG("Setting up Gatt GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP Mask\n\r");
Wolfgang Betz 90:26c0c9807ab4 157 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP;
Wolfgang Betz 90:26c0c9807ab4 158 } //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check.
Wolfgang Betz 90:26c0c9807ab4 159
Wolfgang Betz 90:26c0c9807ab4 160 if(type==UUID::UUID_TYPE_SHORT) {
Wolfgang Betz 90:26c0c9807ab4 161 ret = aci_gatt_add_char(service.getHandle(),
Wolfgang Betz 90:26c0c9807ab4 162 UUID_TYPE_16,
Wolfgang Betz 90:26c0c9807ab4 163 int_8_uuid,
Wolfgang Betz 90:26c0c9807ab4 164 p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
Wolfgang Betz 90:26c0c9807ab4 165 p_char->getProperties(),
Wolfgang Betz 90:26c0c9807ab4 166 ATTR_PERMISSION_NONE,
Wolfgang Betz 90:26c0c9807ab4 167 Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
Wolfgang Betz 90:26c0c9807ab4 168 16 /*Encryption_Key_Size*/,
Wolfgang Betz 90:26c0c9807ab4 169 1 /*isVariable*/,
Wolfgang Betz 90:26c0c9807ab4 170 &bleCharacteristic);
Wolfgang Betz 90:26c0c9807ab4 171
Wolfgang Betz 90:26c0c9807ab4 172 DEBUG("aci_gatt_add_char UUID_TYPE_16 props=%d MaxLength=%d ret=%d\n\r", p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);
Wolfgang Betz 90:26c0c9807ab4 173
Wolfgang Betz 90:26c0c9807ab4 174 } else if(type==UUID::UUID_TYPE_LONG) {
Wolfgang Betz 90:26c0c9807ab4 175 ret = aci_gatt_add_char(service.getHandle(),
Wolfgang Betz 90:26c0c9807ab4 176 UUID_TYPE_128,
Wolfgang Betz 90:26c0c9807ab4 177 char_base_uuid,
Wolfgang Betz 90:26c0c9807ab4 178 p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/,
Wolfgang Betz 90:26c0c9807ab4 179 p_char->getProperties(),
Wolfgang Betz 90:26c0c9807ab4 180 ATTR_PERMISSION_NONE,
Wolfgang Betz 90:26c0c9807ab4 181 Gatt_Evt_Mask /*Gatt_Evt_Mask*/,
Wolfgang Betz 90:26c0c9807ab4 182 16 /*Encryption_Key_Size*/,
Wolfgang Betz 90:26c0c9807ab4 183 1 /*isVariable*/,
Wolfgang Betz 90:26c0c9807ab4 184 &bleCharacteristic);
Wolfgang Betz 90:26c0c9807ab4 185
Wolfgang Betz 90:26c0c9807ab4 186 DEBUG("aci_gatt_add_char UUID_TYPE_128 props=%d MaxLength=%d ret=%d\n\r", p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret);
Wolfgang Betz 90:26c0c9807ab4 187 }
Wolfgang Betz 90:26c0c9807ab4 188
Wolfgang Betz 90:26c0c9807ab4 189 /* Update the characteristic handle */
Wolfgang Betz 90:26c0c9807ab4 190 //uint16_t charHandle = characteristicCount;
Wolfgang Betz 90:26c0c9807ab4 191
Wolfgang Betz 90:26c0c9807ab4 192 bleCharHanldeMap.insert(std::pair<uint16_t, uint16_t>(bleCharacteristic, servHandle));
Wolfgang Betz 90:26c0c9807ab4 193
Wolfgang Betz 90:26c0c9807ab4 194 p_characteristics[characteristicCount++] = p_char;
Wolfgang Betz 90:26c0c9807ab4 195 p_char->getValueAttribute().setHandle(bleCharacteristic); //Set the characteristic count as the corresponding char handle
Wolfgang Betz 90:26c0c9807ab4 196 DEBUG("added bleCharacteristic handle =%u\n\r", bleCharacteristic);
Wolfgang Betz 90:26c0c9807ab4 197
Wolfgang Betz 90:26c0c9807ab4 198 if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) {
Wolfgang Betz 90:26c0c9807ab4 199 write(p_char->getValueAttribute().getHandle(), p_char->getValueAttribute().getValuePtr(), p_char->getValueAttribute().getInitialLength(), false /* localOnly */);
Wolfgang Betz 90:26c0c9807ab4 200 }
Wolfgang Betz 90:26c0c9807ab4 201
Wolfgang Betz 90:26c0c9807ab4 202 // add descriptors now
Wolfgang Betz 90:26c0c9807ab4 203 uint16_t descHandle = 0;
Wolfgang Betz 90:26c0c9807ab4 204 DEBUG("p_char->getDescriptorCount()=%d\n\r", p_char->getDescriptorCount());
Wolfgang Betz 90:26c0c9807ab4 205
Wolfgang Betz 90:26c0c9807ab4 206 for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) {
Wolfgang Betz 90:26c0c9807ab4 207 GattAttribute *descriptor = p_char->getDescriptor(descIndex);
Wolfgang Betz 90:26c0c9807ab4 208 uint16_t shortUUID = descriptor->getUUID().getShortUUID();
Wolfgang Betz 113:31c73e9b0108 209 const uint8_t uuidArray[] = {(uint8_t)((shortUUID>>8)&0xFF), (uint8_t)((shortUUID&0xFF))};
Wolfgang Betz 90:26c0c9807ab4 210 ret = aci_gatt_add_char_desc(service.getHandle(), p_char->getValueAttribute().getHandle(),
Wolfgang Betz 90:26c0c9807ab4 211 CHAR_DESC_TYPE_16_BIT, uuidArray, descriptor->getMaxLength(), descriptor->getInitialLength(),
Wolfgang Betz 90:26c0c9807ab4 212 descriptor->getValuePtr(), CHAR_DESC_SECURITY_PERMISSION, CHAR_DESC_ACCESS_PERMISSION, GATT_NOTIFY_ATTRIBUTE_WRITE,
Wolfgang Betz 90:26c0c9807ab4 213 MIN_ENCRY_KEY_SIZE, CHAR_ATTRIBUTE_LEN_IS_FIXED, &descHandle);
Wolfgang Betz 90:26c0c9807ab4 214 DEBUG("Adding Descriptor descriptor handle=%d ret=%d\n\r", descHandle, ret);
Wolfgang Betz 90:26c0c9807ab4 215 if(ret==(tBleStatus)0) {
Wolfgang Betz 90:26c0c9807ab4 216 DEBUG("Descriptor added successfully, descriptor handle=%d\n\r", descHandle);
Wolfgang Betz 90:26c0c9807ab4 217 descriptor->setHandle(descHandle);
Wolfgang Betz 90:26c0c9807ab4 218 }
Wolfgang Betz 90:26c0c9807ab4 219 }
Wolfgang Betz 90:26c0c9807ab4 220
Wolfgang Betz 90:26c0c9807ab4 221 }
Wolfgang Betz 90:26c0c9807ab4 222
Wolfgang Betz 90:26c0c9807ab4 223 serviceCount++;
Wolfgang Betz 90:26c0c9807ab4 224
Wolfgang Betz 90:26c0c9807ab4 225 //FIXME: There is no GattService pointer array in GattServer.
Wolfgang Betz 90:26c0c9807ab4 226 // There should be one? (Only the user is aware of GattServices!) Report to forum.
Wolfgang Betz 90:26c0c9807ab4 227
Wolfgang Betz 90:26c0c9807ab4 228 return BLE_ERROR_NONE;
Wolfgang Betz 90:26c0c9807ab4 229 }
Wolfgang Betz 90:26c0c9807ab4 230
Wolfgang Betz 90:26c0c9807ab4 231 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 232 /*!
Wolfgang Betz 90:26c0c9807ab4 233 @brief Reads the value of a characteristic, based on the service
Wolfgang Betz 90:26c0c9807ab4 234 and characteristic index fields
Wolfgang Betz 90:26c0c9807ab4 235
Wolfgang Betz 90:26c0c9807ab4 236 @param[in] charHandle
Wolfgang Betz 90:26c0c9807ab4 237 The handle of the GattCharacteristic to read from
Wolfgang Betz 90:26c0c9807ab4 238 @param[in] buffer
Wolfgang Betz 90:26c0c9807ab4 239 Buffer to hold the the characteristic's value
Wolfgang Betz 90:26c0c9807ab4 240 (raw byte array in LSB format)
Wolfgang Betz 90:26c0c9807ab4 241 @param[in] lengthP
Wolfgang Betz 90:26c0c9807ab4 242 The number of bytes read into the buffer
Wolfgang Betz 90:26c0c9807ab4 243
Wolfgang Betz 90:26c0c9807ab4 244 @returns ble_error_t
Wolfgang Betz 90:26c0c9807ab4 245
Wolfgang Betz 90:26c0c9807ab4 246 @retval BLE_ERROR_NONE
Wolfgang Betz 90:26c0c9807ab4 247 Everything executed properly
Wolfgang Betz 90:26c0c9807ab4 248
Wolfgang Betz 90:26c0c9807ab4 249 @section EXAMPLE
Wolfgang Betz 90:26c0c9807ab4 250
Wolfgang Betz 90:26c0c9807ab4 251 @code
Wolfgang Betz 90:26c0c9807ab4 252
Wolfgang Betz 90:26c0c9807ab4 253 @endcode
Wolfgang Betz 90:26c0c9807ab4 254 */
Wolfgang Betz 90:26c0c9807ab4 255 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 256 ble_error_t BlueNRGGattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP)
Wolfgang Betz 90:26c0c9807ab4 257 {
Wolfgang Betz 90:26c0c9807ab4 258 DEBUG("ReadValue() Not Supported\n\r");
Wolfgang Betz 90:26c0c9807ab4 259 return BLE_ERROR_NONE;
Wolfgang Betz 90:26c0c9807ab4 260 }
Wolfgang Betz 90:26c0c9807ab4 261
Wolfgang Betz 90:26c0c9807ab4 262 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 263 /*!
Wolfgang Betz 90:26c0c9807ab4 264 @brief Updates the value of a characteristic, based on the service
Wolfgang Betz 90:26c0c9807ab4 265 and characteristic index fields
Wolfgang Betz 90:26c0c9807ab4 266
Wolfgang Betz 90:26c0c9807ab4 267 @param[in] charHandle
Wolfgang Betz 90:26c0c9807ab4 268 The handle of the GattCharacteristic to write to
Wolfgang Betz 90:26c0c9807ab4 269 @param[in] buffer
Wolfgang Betz 90:26c0c9807ab4 270 Data to use when updating the characteristic's value
Wolfgang Betz 90:26c0c9807ab4 271 (raw byte array in LSB format)
Wolfgang Betz 90:26c0c9807ab4 272 @param[in] len
Wolfgang Betz 90:26c0c9807ab4 273 The number of bytes in buffer
Wolfgang Betz 90:26c0c9807ab4 274
Wolfgang Betz 90:26c0c9807ab4 275 @returns ble_error_t
Wolfgang Betz 90:26c0c9807ab4 276
Wolfgang Betz 90:26c0c9807ab4 277 @retval BLE_ERROR_NONE
Wolfgang Betz 90:26c0c9807ab4 278 Everything executed properly
Wolfgang Betz 90:26c0c9807ab4 279
Wolfgang Betz 90:26c0c9807ab4 280 @section EXAMPLE
Wolfgang Betz 90:26c0c9807ab4 281
Wolfgang Betz 90:26c0c9807ab4 282 @code
Wolfgang Betz 90:26c0c9807ab4 283
Wolfgang Betz 90:26c0c9807ab4 284 @endcode
Wolfgang Betz 90:26c0c9807ab4 285 */
Wolfgang Betz 90:26c0c9807ab4 286 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 287 // <<<ANDREA>>>
Wolfgang Betz 90:26c0c9807ab4 288 ble_error_t BlueNRGGattServer::readValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) {
Wolfgang Betz 90:26c0c9807ab4 289 // Empty by now
Wolfgang Betz 90:26c0c9807ab4 290 return BLE_ERROR_NONE;
Wolfgang Betz 90:26c0c9807ab4 291 }
Wolfgang Betz 90:26c0c9807ab4 292
Wolfgang Betz 90:26c0c9807ab4 293 ble_error_t BlueNRGGattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly) {
Wolfgang Betz 90:26c0c9807ab4 294 // Empty by now
Wolfgang Betz 90:26c0c9807ab4 295 return BLE_ERROR_NONE;
Wolfgang Betz 90:26c0c9807ab4 296 }
Wolfgang Betz 90:26c0c9807ab4 297
Wolfgang Betz 90:26c0c9807ab4 298 ble_error_t BlueNRGGattServer::write(GattAttribute::Handle_t charHandle, const uint8_t buffer[], uint16_t len, bool localOnly)
Wolfgang Betz 90:26c0c9807ab4 299 {
Wolfgang Betz 90:26c0c9807ab4 300 tBleStatus ret;
Wolfgang Betz 90:26c0c9807ab4 301 //uint8_t buff[2];
Wolfgang Betz 90:26c0c9807ab4 302
Wolfgang Betz 90:26c0c9807ab4 303 DEBUG("updating bleCharacteristic charHandle =%u, corresponding serviceHanle= %u len=%d\n\r", charHandle, bleCharHanldeMap.find(charHandle)->second, len);
Wolfgang Betz 90:26c0c9807ab4 304 /*
apalmieri 125:2e468f07a94f 305 for(int i=0; i<len; i++) {
Wolfgang Betz 90:26c0c9807ab4 306 DEBUG("buffer[%d]=%d\n\r", i, buffer[i]);
Wolfgang Betz 90:26c0c9807ab4 307 }
Wolfgang Betz 90:26c0c9807ab4 308 */
Wolfgang Betz 90:26c0c9807ab4 309 ret = aci_gatt_update_char_value(bleCharHanldeMap.find(charHandle)->second, charHandle, 0, len, buffer);
Wolfgang Betz 90:26c0c9807ab4 310
Wolfgang Betz 90:26c0c9807ab4 311 if (ret != BLE_STATUS_SUCCESS){
Wolfgang Betz 104:dfde0e42edf1 312 DEBUG("Error while updating characteristic (ret=0x%x).\n\r", ret) ;
Wolfgang Betz 90:26c0c9807ab4 313 return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value
Wolfgang Betz 90:26c0c9807ab4 314 //FIXME: Define Error values equivalent to BlueNRG Error Codes.
Wolfgang Betz 90:26c0c9807ab4 315 }
Wolfgang Betz 90:26c0c9807ab4 316
Wolfgang Betz 90:26c0c9807ab4 317 //Generate Data Sent Event Here? (GattServerEvents::GATT_EVENT_DATA_SENT) //FIXME: Is this correct?
Wolfgang Betz 90:26c0c9807ab4 318 //Check if characteristic property is NOTIFY|INDICATE, if yes generate event
Wolfgang Betz 90:26c0c9807ab4 319 GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(charHandle);
Wolfgang Betz 90:26c0c9807ab4 320 if(p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
Wolfgang Betz 90:26c0c9807ab4 321 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) {
Wolfgang Betz 104:dfde0e42edf1 322 DEBUG("Generate event after updating\n\r");
Wolfgang Betz 90:26c0c9807ab4 323 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT, charHandle);
Wolfgang Betz 90:26c0c9807ab4 324 }
Wolfgang Betz 90:26c0c9807ab4 325
Wolfgang Betz 90:26c0c9807ab4 326 return BLE_ERROR_NONE;
Wolfgang Betz 90:26c0c9807ab4 327 }
Wolfgang Betz 90:26c0c9807ab4 328
Wolfgang Betz 90:26c0c9807ab4 329 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 330 /*!
Wolfgang Betz 90:26c0c9807ab4 331 @brief Reads a value according to the handle provided
Wolfgang Betz 90:26c0c9807ab4 332
Wolfgang Betz 90:26c0c9807ab4 333 @param[in] charHandle
Wolfgang Betz 90:26c0c9807ab4 334 The handle of the GattCharacteristic to read from
Wolfgang Betz 90:26c0c9807ab4 335
Wolfgang Betz 90:26c0c9807ab4 336 @returns ble_error_t
Wolfgang Betz 90:26c0c9807ab4 337
Wolfgang Betz 90:26c0c9807ab4 338 @retval BLE_ERROR_NONE
Wolfgang Betz 90:26c0c9807ab4 339 Everything executed properly
Wolfgang Betz 90:26c0c9807ab4 340
Wolfgang Betz 90:26c0c9807ab4 341 @section EXAMPLE
Wolfgang Betz 90:26c0c9807ab4 342
Wolfgang Betz 90:26c0c9807ab4 343 @code
Wolfgang Betz 90:26c0c9807ab4 344
Wolfgang Betz 90:26c0c9807ab4 345 @endcode
Wolfgang Betz 90:26c0c9807ab4 346 */
Wolfgang Betz 90:26c0c9807ab4 347 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 348 ble_error_t BlueNRGGattServer::Read_Request_CB(uint16_t handle)
Wolfgang Betz 90:26c0c9807ab4 349 {
Wolfgang Betz 90:26c0c9807ab4 350 //signed short refvalue;
Wolfgang Betz 90:26c0c9807ab4 351 uint16_t gapConnectionHandle = BlueNRGGap::getInstance().getConnectionHandle();
Wolfgang Betz 90:26c0c9807ab4 352
Wolfgang Betz 90:26c0c9807ab4 353 GattReadCallbackParams readParams;
Wolfgang Betz 90:26c0c9807ab4 354 readParams.handle = handle;
Wolfgang Betz 90:26c0c9807ab4 355
Wolfgang Betz 90:26c0c9807ab4 356 //DEBUG("readParams.charHandle = %d\n\r", readParams.charHandle);
Wolfgang Betz 90:26c0c9807ab4 357 HCIDataReadEvent(&readParams);
Wolfgang Betz 90:26c0c9807ab4 358
Wolfgang Betz 90:26c0c9807ab4 359 //EXIT:
Wolfgang Betz 90:26c0c9807ab4 360 if(gapConnectionHandle != 0){
Wolfgang Betz 90:26c0c9807ab4 361 //DEBUG("Calling aci_gatt_allow_read\n\r");
Wolfgang Betz 90:26c0c9807ab4 362 aci_gatt_allow_read(gapConnectionHandle);
Wolfgang Betz 90:26c0c9807ab4 363 }
Wolfgang Betz 90:26c0c9807ab4 364
Wolfgang Betz 90:26c0c9807ab4 365 return BLE_ERROR_NONE;
Wolfgang Betz 90:26c0c9807ab4 366 }
Wolfgang Betz 90:26c0c9807ab4 367
Wolfgang Betz 90:26c0c9807ab4 368 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 369 /*!
Wolfgang Betz 90:26c0c9807ab4 370 @brief Returns the GattCharacteristic according to the handle provided
Wolfgang Betz 90:26c0c9807ab4 371
Wolfgang Betz 90:26c0c9807ab4 372 @param[in] charHandle
Wolfgang Betz 90:26c0c9807ab4 373 The handle of the GattCharacteristic
Wolfgang Betz 90:26c0c9807ab4 374
Wolfgang Betz 90:26c0c9807ab4 375 @returns ble_error_t
Wolfgang Betz 90:26c0c9807ab4 376
Wolfgang Betz 90:26c0c9807ab4 377 @retval BLE_ERROR_NONE
Wolfgang Betz 90:26c0c9807ab4 378 Everything executed properly
Wolfgang Betz 90:26c0c9807ab4 379
Wolfgang Betz 90:26c0c9807ab4 380 @section EXAMPLE
Wolfgang Betz 90:26c0c9807ab4 381
Wolfgang Betz 90:26c0c9807ab4 382 @code
Wolfgang Betz 90:26c0c9807ab4 383
Wolfgang Betz 90:26c0c9807ab4 384 @endcode
Wolfgang Betz 90:26c0c9807ab4 385 */
Wolfgang Betz 90:26c0c9807ab4 386 /**************************************************************************/
Wolfgang Betz 90:26c0c9807ab4 387 GattCharacteristic* BlueNRGGattServer::getCharacteristicFromHandle(uint16_t attrHandle)
Wolfgang Betz 90:26c0c9807ab4 388 {
Wolfgang Betz 90:26c0c9807ab4 389 GattCharacteristic *p_char = NULL;
Wolfgang Betz 90:26c0c9807ab4 390 int i;
Wolfgang Betz 104:dfde0e42edf1 391 uint16_t handle, handle_1;
Wolfgang Betz 90:26c0c9807ab4 392
Wolfgang Betz 104:dfde0e42edf1 393 DEBUG("BlueNRGGattServer::getCharacteristicFromHandle()>>Attribute Handle received %d\n\r",attrHandle);
Wolfgang Betz 90:26c0c9807ab4 394 for(i=0; i<characteristicCount; i++)
Wolfgang Betz 90:26c0c9807ab4 395 {
Wolfgang Betz 90:26c0c9807ab4 396 handle = p_characteristics[i]->getValueAttribute().getHandle();
Wolfgang Betz 104:dfde0e42edf1 397 DEBUG("handle(%d)=%d\n\r", i, handle);
Wolfgang Betz 90:26c0c9807ab4 398 if(i==characteristicCount-1)//Last Characteristic check
Wolfgang Betz 90:26c0c9807ab4 399 {
Wolfgang Betz 104:dfde0e42edf1 400 if(attrHandle>=handle)
Wolfgang Betz 90:26c0c9807ab4 401 {
Wolfgang Betz 90:26c0c9807ab4 402 p_char = p_characteristics[i];
Wolfgang Betz 104:dfde0e42edf1 403 DEBUG("Found Characteristic Properties 0x%x (handle=%d)\n\r",p_char->getProperties(), handle);
Wolfgang Betz 90:26c0c9807ab4 404 break;
Wolfgang Betz 90:26c0c9807ab4 405 }
Wolfgang Betz 90:26c0c9807ab4 406 }
Wolfgang Betz 90:26c0c9807ab4 407 else {
Wolfgang Betz 104:dfde0e42edf1 408 handle_1 = p_characteristics[i+1]->getValueAttribute().getHandle();
Wolfgang Betz 90:26c0c9807ab4 409 //Testing if attribute handle is between two Characteristic Handles
Wolfgang Betz 104:dfde0e42edf1 410 if(attrHandle>=handle && attrHandle<handle_1)
Wolfgang Betz 90:26c0c9807ab4 411 {
Wolfgang Betz 90:26c0c9807ab4 412 p_char = p_characteristics[i];
Wolfgang Betz 104:dfde0e42edf1 413 DEBUG("Found Characteristic Properties 0x%x (handle=%d handle_1=%d)\n\r",p_char->getProperties(), handle, handle_1);
Wolfgang Betz 90:26c0c9807ab4 414 break;
Wolfgang Betz 90:26c0c9807ab4 415 } else continue;
Wolfgang Betz 90:26c0c9807ab4 416 }
Wolfgang Betz 90:26c0c9807ab4 417 }
Wolfgang Betz 90:26c0c9807ab4 418
Wolfgang Betz 90:26c0c9807ab4 419 return p_char;
Wolfgang Betz 90:26c0c9807ab4 420 }
Wolfgang Betz 90:26c0c9807ab4 421
Wolfgang Betz 90:26c0c9807ab4 422 void BlueNRGGattServer::HCIDataWrittenEvent(const GattWriteCallbackParams *params) {
Wolfgang Betz 90:26c0c9807ab4 423 this->handleDataWrittenEvent(params);
Wolfgang Betz 90:26c0c9807ab4 424 }
Wolfgang Betz 90:26c0c9807ab4 425
Wolfgang Betz 90:26c0c9807ab4 426 void BlueNRGGattServer::HCIDataReadEvent(const GattReadCallbackParams *params) {
Wolfgang Betz 90:26c0c9807ab4 427 DEBUG("Called HCIDataReadEvent\n\r");
Wolfgang Betz 90:26c0c9807ab4 428 this->handleDataReadEvent(params);
Wolfgang Betz 90:26c0c9807ab4 429 }
Wolfgang Betz 90:26c0c9807ab4 430
Wolfgang Betz 90:26c0c9807ab4 431 void BlueNRGGattServer::HCIEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) {
Wolfgang Betz 90:26c0c9807ab4 432 this->handleEvent(type, charHandle);
Wolfgang Betz 90:26c0c9807ab4 433 }
Wolfgang Betz 90:26c0c9807ab4 434
Wolfgang Betz 90:26c0c9807ab4 435 void BlueNRGGattServer::HCIDataSentEvent(unsigned count) {
Wolfgang Betz 90:26c0c9807ab4 436 this->handleDataSentEvent(count);
Wolfgang Betz 90:26c0c9807ab4 437 }
Wolfgang Betz 90:26c0c9807ab4 438
Wolfgang Betz 90:26c0c9807ab4 439
Wolfgang Betz 90:26c0c9807ab4 440 ble_error_t BlueNRGGattServer::initializeGATTDatabase(void) {
Wolfgang Betz 90:26c0c9807ab4 441 // <TODO>
Wolfgang Betz 90:26c0c9807ab4 442 return (ble_error_t)0;
Wolfgang Betz 90:26c0c9807ab4 443 }