BLE lib
Fork of Nucleo_BLE_BlueNRG by
BlueNRGGattServer.cpp@5:beb2c7544ce4, 2015-02-08 (annotated)
- Committer:
- vijaynvr
- Date:
- Sun Feb 08 14:25:58 2015 +0000
- Revision:
- 5:beb2c7544ce4
- Parent:
- 4:c8ed8022e996
working;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:a948f5f3904c | 1 | /* mbed Microcontroller Library |
sjallouli | 0:a948f5f3904c | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:a948f5f3904c | 3 | * |
sjallouli | 0:a948f5f3904c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:a948f5f3904c | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:a948f5f3904c | 6 | * You may obtain a copy of the License at |
sjallouli | 0:a948f5f3904c | 7 | * |
sjallouli | 0:a948f5f3904c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:a948f5f3904c | 9 | * |
sjallouli | 0:a948f5f3904c | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:a948f5f3904c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:a948f5f3904c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:a948f5f3904c | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:a948f5f3904c | 14 | * limitations under the License. |
sjallouli | 0:a948f5f3904c | 15 | */ |
sjallouli | 0:a948f5f3904c | 16 | |
sjallouli | 0:a948f5f3904c | 17 | #include "BlueNRGGattServer.h" |
sjallouli | 0:a948f5f3904c | 18 | #include "mbed.h" |
sjallouli | 0:a948f5f3904c | 19 | #include "BlueNRGGap.h" |
sjallouli | 0:a948f5f3904c | 20 | #include "Utils.h" |
sjallouli | 0:a948f5f3904c | 21 | |
sjallouli | 0:a948f5f3904c | 22 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 23 | /*! |
sjallouli | 0:a948f5f3904c | 24 | @brief Adds a new service to the GATT table on the peripheral |
sjallouli | 0:a948f5f3904c | 25 | |
sjallouli | 0:a948f5f3904c | 26 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 27 | |
sjallouli | 0:a948f5f3904c | 28 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 29 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 30 | |
sjallouli | 0:a948f5f3904c | 31 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 32 | |
sjallouli | 0:a948f5f3904c | 33 | @code |
sjallouli | 0:a948f5f3904c | 34 | |
sjallouli | 0:a948f5f3904c | 35 | @endcode |
sjallouli | 0:a948f5f3904c | 36 | */ |
sjallouli | 0:a948f5f3904c | 37 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 38 | ble_error_t BlueNRGGattServer::addService(GattService &service) |
sjallouli | 0:a948f5f3904c | 39 | { |
sjallouli | 0:a948f5f3904c | 40 | /* ToDo: Make sure we don't overflow the array, etc. */ |
sjallouli | 0:a948f5f3904c | 41 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ |
sjallouli | 0:a948f5f3904c | 42 | /* ToDo: Basic validation */ |
fbd38 | 3:89230b3e003b | 43 | |
sjallouli | 0:a948f5f3904c | 44 | tBleStatus ret; |
sjallouli | 0:a948f5f3904c | 45 | uint8_t type; |
sjallouli | 0:a948f5f3904c | 46 | uint16_t short_uuid; |
sjallouli | 0:a948f5f3904c | 47 | uint8_t primary_short_uuid[2]; |
sjallouli | 0:a948f5f3904c | 48 | uint8_t primary_base_uuid[16]; |
sjallouli | 0:a948f5f3904c | 49 | uint8_t char_base_uuid[16]; |
sjallouli | 0:a948f5f3904c | 50 | const uint8_t *base_uuid; |
sjallouli | 0:a948f5f3904c | 51 | const uint8_t *base_char_uuid; |
fbd38 | 3:89230b3e003b | 52 | |
sjallouli | 0:a948f5f3904c | 53 | type = (service.getUUID()).shortOrLong(); |
fbd38 | 4:c8ed8022e996 | 54 | DEBUG(">>> AddService(): Type:%d ", type); |
fbd38 | 3:89230b3e003b | 55 | |
sjallouli | 0:a948f5f3904c | 56 | /* Add the service to the BlueNRG */ |
sjallouli | 0:a948f5f3904c | 57 | short_uuid = (service.getUUID()).getShortUUID(); |
sjallouli | 0:a948f5f3904c | 58 | STORE_LE_16(primary_short_uuid, short_uuid); |
fbd38 | 3:89230b3e003b | 59 | |
sjallouli | 0:a948f5f3904c | 60 | if(type==UUID::UUID_TYPE_LONG) { |
fbd38 | 3:89230b3e003b | 61 | base_uuid = (service.getUUID()).getBaseUUID(); |
fbd38 | 3:89230b3e003b | 62 | |
sjallouli | 0:a948f5f3904c | 63 | 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], |
fbd38 | 3:89230b3e003b | 64 | 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]); |
sjallouli | 0:a948f5f3904c | 65 | } |
fbd38 | 3:89230b3e003b | 66 | |
sjallouli | 0:a948f5f3904c | 67 | if(type==UUID::UUID_TYPE_SHORT) { |
fbd38 | 3:89230b3e003b | 68 | ret = aci_gatt_add_serv(UUID_TYPE_16, primary_short_uuid, PRIMARY_SERVICE, 7, |
fbd38 | 3:89230b3e003b | 69 | &servHandle); |
fbd38 | 3:89230b3e003b | 70 | } else if(type==UUID::UUID_TYPE_LONG) { |
fbd38 | 3:89230b3e003b | 71 | ret = aci_gatt_add_serv(UUID_TYPE_128, primary_base_uuid, PRIMARY_SERVICE, 7, |
fbd38 | 3:89230b3e003b | 72 | &servHandle); |
sjallouli | 0:a948f5f3904c | 73 | } |
fbd38 | 3:89230b3e003b | 74 | |
sjallouli | 0:a948f5f3904c | 75 | service.setHandle(servHandle); |
sjallouli | 0:a948f5f3904c | 76 | //serviceHandleVector.push_back(servHandle); |
sjallouli | 0:a948f5f3904c | 77 | DEBUG("added servHandle handle =%u\n\r", servHandle); |
sjallouli | 0:a948f5f3904c | 78 | tHalUint16 bleCharacteristic; |
fbd38 | 3:89230b3e003b | 79 | |
sjallouli | 0:a948f5f3904c | 80 | //iterate to include all characteristics |
sjallouli | 0:a948f5f3904c | 81 | for (uint8_t i = 0; i < service.getCharacteristicCount(); i++) { |
sjallouli | 0:a948f5f3904c | 82 | GattCharacteristic *p_char = service.getCharacteristic(i); |
fbd38 | 3:89230b3e003b | 83 | uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID(); |
fbd38 | 3:89230b3e003b | 84 | |
sjallouli | 0:a948f5f3904c | 85 | uint8_t int_8_uuid[2]; |
sjallouli | 0:a948f5f3904c | 86 | STORE_LE_16(int_8_uuid, char_uuid); |
fbd38 | 3:89230b3e003b | 87 | |
sjallouli | 0:a948f5f3904c | 88 | if(type==UUID::UUID_TYPE_LONG) { |
sjallouli | 0:a948f5f3904c | 89 | base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID(); |
fbd38 | 3:89230b3e003b | 90 | |
sjallouli | 0:a948f5f3904c | 91 | 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], |
fbd38 | 3:89230b3e003b | 92 | 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]); |
sjallouli | 0:a948f5f3904c | 93 | } |
fbd38 | 3:89230b3e003b | 94 | |
fbd38 | 4:c8ed8022e996 | 95 | DEBUG(" Char Properties 0x%x ", p_char->getProperties()); |
sjallouli | 0:a948f5f3904c | 96 | /* |
sjallouli | 0:a948f5f3904c | 97 | * Gatt_Evt_Mask -> HardCoded (0) |
sjallouli | 0:a948f5f3904c | 98 | * Encryption_Key_Size -> Hardcoded (16) |
sjallouli | 0:a948f5f3904c | 99 | * isVariable (variable length value field) -> Hardcoded (1) |
sjallouli | 0:a948f5f3904c | 100 | */ |
sjallouli | 0:a948f5f3904c | 101 | tGattServerEvent Gatt_Evt_Mask = 0x0; |
fbd38 | 3:89230b3e003b | 102 | |
sjallouli | 0:a948f5f3904c | 103 | if((p_char->getProperties() & |
fbd38 | 3:89230b3e003b | 104 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE| |
fbd38 | 3:89230b3e003b | 105 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) { |
sjallouli | 0:a948f5f3904c | 106 | DEBUG("Setting up Gatt GATT_SERVER_ATTR_WRITE Mask\n\r"); |
sjallouli | 0:a948f5f3904c | 107 | Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_SERVER_ATTR_WRITE; |
sjallouli | 0:a948f5f3904c | 108 | } |
sjallouli | 0:a948f5f3904c | 109 | if((p_char->getProperties() & |
fbd38 | 3:89230b3e003b | 110 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ| |
fbd38 | 3:89230b3e003b | 111 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) { |
sjallouli | 0:a948f5f3904c | 112 | DEBUG("Setting up Gatt GATT_INTIMATE_APPL_WHEN_READ_N_WAIT Mask\n\r"); |
fbd38 | 3:89230b3e003b | 113 | Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_INTIMATE_APPL_WHEN_READ_N_WAIT; |
sjallouli | 0:a948f5f3904c | 114 | } //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check. |
fbd38 | 3:89230b3e003b | 115 | |
sjallouli | 0:a948f5f3904c | 116 | if(type==UUID::UUID_TYPE_SHORT) { |
sjallouli | 0:a948f5f3904c | 117 | ret = aci_gatt_add_char(service.getHandle(), UUID_TYPE_16, int_8_uuid, p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/, |
fbd38 | 3:89230b3e003b | 118 | p_char->getProperties(), ATTR_PERMISSION_NONE, Gatt_Evt_Mask /*Gatt_Evt_Mask*/, |
fbd38 | 3:89230b3e003b | 119 | 16 /*Encryption_Key_Size*/, 1 /*isVariable*/, &bleCharacteristic); |
fbd38 | 3:89230b3e003b | 120 | } else if(type==UUID::UUID_TYPE_LONG) { |
sjallouli | 0:a948f5f3904c | 121 | ret = aci_gatt_add_char(service.getHandle(), UUID_TYPE_128, char_base_uuid, p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/, |
fbd38 | 3:89230b3e003b | 122 | p_char->getProperties(), ATTR_PERMISSION_NONE, Gatt_Evt_Mask /*Gatt_Evt_Mask*/, |
fbd38 | 3:89230b3e003b | 123 | 16 /*Encryption_Key_Size*/, 1 /*isVariable*/, &bleCharacteristic); |
sjallouli | 0:a948f5f3904c | 124 | } |
sjallouli | 0:a948f5f3904c | 125 | /* Update the characteristic handle */ |
fbd38 | 3:89230b3e003b | 126 | uint16_t charHandle = characteristicCount; |
fbd38 | 3:89230b3e003b | 127 | |
fbd38 | 3:89230b3e003b | 128 | bleCharHanldeMap.insert(std::pair<tHalUint16, tHalUint16>(bleCharacteristic, servHandle)); |
fbd38 | 3:89230b3e003b | 129 | |
sjallouli | 0:a948f5f3904c | 130 | p_characteristics[characteristicCount++] = p_char; |
sjallouli | 0:a948f5f3904c | 131 | p_char->getValueAttribute().setHandle(bleCharacteristic); //Set the characteristic count as the corresponding char handle |
sjallouli | 0:a948f5f3904c | 132 | DEBUG("added bleCharacteristic handle =%u\n\r", bleCharacteristic); |
fbd38 | 3:89230b3e003b | 133 | |
sjallouli | 0:a948f5f3904c | 134 | if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) { |
sjallouli | 0:a948f5f3904c | 135 | updateValue(p_char->getValueAttribute().getHandle(), p_char->getValueAttribute().getValuePtr(), p_char->getValueAttribute().getInitialLength(), false /* localOnly */); |
sjallouli | 0:a948f5f3904c | 136 | } |
fbd38 | 3:89230b3e003b | 137 | |
sjallouli | 0:a948f5f3904c | 138 | // add descriptors now |
sjallouli | 0:a948f5f3904c | 139 | uint16_t descHandle = 0; |
sjallouli | 0:a948f5f3904c | 140 | for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) { |
sjallouli | 0:a948f5f3904c | 141 | GattAttribute *descriptor = p_char->getDescriptor(descIndex); |
sjallouli | 0:a948f5f3904c | 142 | uint16_t shortUUID = descriptor->getUUID().getShortUUID(); |
sjallouli | 0:a948f5f3904c | 143 | const tHalUint8 uuidArray[] = {(shortUUID>>8)&0xFF, (shortUUID&0xFF)}; |
fbd38 | 3:89230b3e003b | 144 | ret = aci_gatt_add_char_desc(service.getHandle(), p_char->getValueAttribute().getHandle(), |
fbd38 | 3:89230b3e003b | 145 | CHAR_DESC_TYPE_16_BIT, uuidArray, descriptor->getMaxLength(), descriptor->getInitialLength(), |
fbd38 | 3:89230b3e003b | 146 | descriptor->getValuePtr(), CHAR_DESC_SECURITY_PERMISSION, CHAR_DESC_ACCESS_PERMISSION, GATT_SERVER_ATTR_READ_WRITE, |
fbd38 | 3:89230b3e003b | 147 | MIN_ENCRY_KEY_SIZE, CHAR_ATTRIBUTE_LEN_IS_FIXED, &descHandle); |
sjallouli | 0:a948f5f3904c | 148 | if(ret==(tBleStatus)0) { |
sjallouli | 0:a948f5f3904c | 149 | DEBUG("Descriptor added successfully, descriptor handle=%d\n\r", descHandle); |
sjallouli | 0:a948f5f3904c | 150 | descriptor->setHandle(descHandle); |
sjallouli | 0:a948f5f3904c | 151 | } |
sjallouli | 0:a948f5f3904c | 152 | } |
fbd38 | 3:89230b3e003b | 153 | } |
fbd38 | 3:89230b3e003b | 154 | |
sjallouli | 0:a948f5f3904c | 155 | serviceCount++; |
fbd38 | 3:89230b3e003b | 156 | |
fbd38 | 3:89230b3e003b | 157 | //FIXME: There is no GattService pointer array in GattServer. |
sjallouli | 0:a948f5f3904c | 158 | // There should be one? (Only the user is aware of GattServices!) Report to forum. |
fbd38 | 3:89230b3e003b | 159 | |
sjallouli | 0:a948f5f3904c | 160 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 161 | } |
sjallouli | 0:a948f5f3904c | 162 | |
sjallouli | 0:a948f5f3904c | 163 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 164 | /*! |
sjallouli | 0:a948f5f3904c | 165 | @brief Reads the value of a characteristic, based on the service |
sjallouli | 0:a948f5f3904c | 166 | and characteristic index fields |
sjallouli | 0:a948f5f3904c | 167 | |
sjallouli | 0:a948f5f3904c | 168 | @param[in] charHandle |
sjallouli | 0:a948f5f3904c | 169 | The handle of the GattCharacteristic to read from |
sjallouli | 0:a948f5f3904c | 170 | @param[in] buffer |
sjallouli | 0:a948f5f3904c | 171 | Buffer to hold the the characteristic's value |
sjallouli | 0:a948f5f3904c | 172 | (raw byte array in LSB format) |
sjallouli | 0:a948f5f3904c | 173 | @param[in] len |
sjallouli | 0:a948f5f3904c | 174 | The number of bytes read into the buffer |
sjallouli | 0:a948f5f3904c | 175 | |
sjallouli | 0:a948f5f3904c | 176 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 177 | |
sjallouli | 0:a948f5f3904c | 178 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 179 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 180 | |
sjallouli | 0:a948f5f3904c | 181 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 182 | |
sjallouli | 0:a948f5f3904c | 183 | @code |
sjallouli | 0:a948f5f3904c | 184 | |
sjallouli | 0:a948f5f3904c | 185 | @endcode |
sjallouli | 0:a948f5f3904c | 186 | */ |
sjallouli | 0:a948f5f3904c | 187 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 188 | ble_error_t BlueNRGGattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP) |
sjallouli | 0:a948f5f3904c | 189 | { |
fbd38 | 3:89230b3e003b | 190 | uint8_t *ptr; |
fbd38 | 3:89230b3e003b | 191 | GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(charHandle); |
fbd38 | 3:89230b3e003b | 192 | *lengthP=p_char->getValueAttribute().getInitialLength(); |
fbd38 | 3:89230b3e003b | 193 | ptr = p_char->getValueAttribute().getValuePtr(); |
fbd38 | 3:89230b3e003b | 194 | for(int i=0; i<*lengthP; i++) { |
fbd38 | 3:89230b3e003b | 195 | buffer[i]=ptr[i]; |
fbd38 | 4:c8ed8022e996 | 196 | DEBUG(" BLE_readValue D=%d N=%d Bytes\r\n", buffer[0], *lengthP); |
fbd38 | 3:89230b3e003b | 197 | } |
sjallouli | 0:a948f5f3904c | 198 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 199 | } |
sjallouli | 0:a948f5f3904c | 200 | |
sjallouli | 0:a948f5f3904c | 201 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 202 | /*! |
sjallouli | 0:a948f5f3904c | 203 | @brief Updates the value of a characteristic, based on the service |
sjallouli | 0:a948f5f3904c | 204 | and characteristic index fields |
sjallouli | 0:a948f5f3904c | 205 | |
sjallouli | 0:a948f5f3904c | 206 | @param[in] charHandle |
sjallouli | 0:a948f5f3904c | 207 | The handle of the GattCharacteristic to write to |
sjallouli | 0:a948f5f3904c | 208 | @param[in] buffer |
sjallouli | 0:a948f5f3904c | 209 | Data to use when updating the characteristic's value |
sjallouli | 0:a948f5f3904c | 210 | (raw byte array in LSB format) |
sjallouli | 0:a948f5f3904c | 211 | @param[in] len |
sjallouli | 0:a948f5f3904c | 212 | The number of bytes in buffer |
sjallouli | 0:a948f5f3904c | 213 | |
sjallouli | 0:a948f5f3904c | 214 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 215 | |
sjallouli | 0:a948f5f3904c | 216 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 217 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 218 | |
sjallouli | 0:a948f5f3904c | 219 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 220 | |
sjallouli | 0:a948f5f3904c | 221 | @code |
sjallouli | 0:a948f5f3904c | 222 | |
sjallouli | 0:a948f5f3904c | 223 | @endcode |
sjallouli | 0:a948f5f3904c | 224 | */ |
sjallouli | 0:a948f5f3904c | 225 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 226 | ble_error_t BlueNRGGattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly) |
sjallouli | 0:a948f5f3904c | 227 | { |
fbd38 | 3:89230b3e003b | 228 | tBleStatus ret; |
sjallouli | 0:a948f5f3904c | 229 | tHalUint8 buff[2]; |
sjallouli | 0:a948f5f3904c | 230 | |
fbd38 | 3:89230b3e003b | 231 | // DEBUG("updating bleCharacteristic charHandle =%u, corresponding serviceHanle= %u\n\r", charHandle, bleCharHanldeMap.find(charHandle)->second); |
fbd38 | 3:89230b3e003b | 232 | |
sjallouli | 0:a948f5f3904c | 233 | ret = aci_gatt_update_char_value(bleCharHanldeMap.find(charHandle)->second, charHandle, 0, len, buffer); |
sjallouli | 0:a948f5f3904c | 234 | |
fbd38 | 3:89230b3e003b | 235 | if (ret != BLE_STATUS_SUCCESS) { |
fbd38 | 4:c8ed8022e996 | 236 | DEBUG(" !!! Error while updating characteristic.\n\r") ; |
fbd38 | 3:89230b3e003b | 237 | return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value |
sjallouli | 0:a948f5f3904c | 238 | //FIXME: Define Error values equivalent to BlueNRG Error Codes. |
sjallouli | 0:a948f5f3904c | 239 | } |
sjallouli | 0:a948f5f3904c | 240 | |
sjallouli | 0:a948f5f3904c | 241 | //Generate Data Sent Event Here? (GattServerEvents::GATT_EVENT_DATA_SENT) //FIXME: Is this correct? |
sjallouli | 0:a948f5f3904c | 242 | //Check if characteristic property is NOTIFY|INDICATE, if yes generate event |
sjallouli | 0:a948f5f3904c | 243 | GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(charHandle); |
sjallouli | 0:a948f5f3904c | 244 | if(p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
fbd38 | 3:89230b3e003b | 245 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) { |
sjallouli | 0:a948f5f3904c | 246 | BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT, charHandle); |
sjallouli | 0:a948f5f3904c | 247 | } |
sjallouli | 0:a948f5f3904c | 248 | |
sjallouli | 0:a948f5f3904c | 249 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 250 | } |
sjallouli | 0:a948f5f3904c | 251 | |
sjallouli | 0:a948f5f3904c | 252 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 253 | /*! |
sjallouli | 0:a948f5f3904c | 254 | @brief Reads a value according to the handle provided |
sjallouli | 0:a948f5f3904c | 255 | |
sjallouli | 0:a948f5f3904c | 256 | @param[in] charHandle |
sjallouli | 0:a948f5f3904c | 257 | The handle of the GattCharacteristic to read from |
sjallouli | 0:a948f5f3904c | 258 | |
sjallouli | 0:a948f5f3904c | 259 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 260 | |
sjallouli | 0:a948f5f3904c | 261 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 262 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 263 | |
sjallouli | 0:a948f5f3904c | 264 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 265 | |
sjallouli | 0:a948f5f3904c | 266 | @code |
sjallouli | 0:a948f5f3904c | 267 | |
sjallouli | 0:a948f5f3904c | 268 | @endcode |
sjallouli | 0:a948f5f3904c | 269 | */ |
sjallouli | 0:a948f5f3904c | 270 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 271 | ble_error_t BlueNRGGattServer::Read_Request_CB(tHalUint16 handle) |
sjallouli | 0:a948f5f3904c | 272 | { |
sjallouli | 0:a948f5f3904c | 273 | //signed short refvalue; |
sjallouli | 0:a948f5f3904c | 274 | uint16_t gapConnectionHandle = BlueNRGGap::getInstance().getConnectionHandle(); |
fbd38 | 3:89230b3e003b | 275 | |
sjallouli | 0:a948f5f3904c | 276 | //EXIT: |
sjallouli | 0:a948f5f3904c | 277 | if(gapConnectionHandle != 0) |
fbd38 | 3:89230b3e003b | 278 | aci_gatt_allow_read(gapConnectionHandle); |
sjallouli | 0:a948f5f3904c | 279 | } |
sjallouli | 0:a948f5f3904c | 280 | |
sjallouli | 0:a948f5f3904c | 281 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 282 | /*! |
sjallouli | 0:a948f5f3904c | 283 | @brief Returns the GattCharacteristic according to the handle provided |
sjallouli | 0:a948f5f3904c | 284 | |
sjallouli | 0:a948f5f3904c | 285 | @param[in] charHandle |
sjallouli | 0:a948f5f3904c | 286 | The handle of the GattCharacteristic |
sjallouli | 0:a948f5f3904c | 287 | |
sjallouli | 0:a948f5f3904c | 288 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 289 | |
sjallouli | 0:a948f5f3904c | 290 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 291 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 292 | |
sjallouli | 0:a948f5f3904c | 293 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 294 | |
sjallouli | 0:a948f5f3904c | 295 | @code |
sjallouli | 0:a948f5f3904c | 296 | |
sjallouli | 0:a948f5f3904c | 297 | @endcode |
sjallouli | 0:a948f5f3904c | 298 | */ |
sjallouli | 0:a948f5f3904c | 299 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 300 | GattCharacteristic* BlueNRGGattServer::getCharacteristicFromHandle(tHalUint16 attrHandle) |
sjallouli | 0:a948f5f3904c | 301 | { |
sjallouli | 0:a948f5f3904c | 302 | GattCharacteristic *p_char = NULL; |
sjallouli | 0:a948f5f3904c | 303 | int i; |
sjallouli | 0:a948f5f3904c | 304 | uint16_t handle; |
sjallouli | 0:a948f5f3904c | 305 | |
sjallouli | 0:a948f5f3904c | 306 | //DEBUG("BlueNRGGattServer::getCharacteristicFromHandle()>>Attribute Handle received 0x%x\n\r",attrHandle); |
fbd38 | 3:89230b3e003b | 307 | for(i=0; i<characteristicCount; i++) { |
sjallouli | 0:a948f5f3904c | 308 | handle = p_characteristics[i]->getValueAttribute().getHandle(); |
fbd38 | 3:89230b3e003b | 309 | |
fbd38 | 3:89230b3e003b | 310 | if(i==characteristicCount-1) { //Last Characteristic check |
fbd38 | 3:89230b3e003b | 311 | if(attrHandle>=bleCharacteristicHandles[handle]) { |
sjallouli | 0:a948f5f3904c | 312 | p_char = p_characteristics[i]; |
sjallouli | 0:a948f5f3904c | 313 | DEBUG("Found Characteristic Properties 0x%x\n\r",p_char->getProperties()); |
sjallouli | 0:a948f5f3904c | 314 | break; |
fbd38 | 3:89230b3e003b | 315 | } |
fbd38 | 3:89230b3e003b | 316 | } else { |
sjallouli | 0:a948f5f3904c | 317 | //Testing if attribute handle is between two Characteristic Handles |
fbd38 | 3:89230b3e003b | 318 | if(attrHandle>=bleCharacteristicHandles[handle] && attrHandle<bleCharacteristicHandles[handle+1]) { |
sjallouli | 0:a948f5f3904c | 319 | p_char = p_characteristics[i]; |
sjallouli | 0:a948f5f3904c | 320 | //DEBUG("Found Characteristic Properties 0x%x\n\r",p_char->getProperties()); |
sjallouli | 0:a948f5f3904c | 321 | break; |
sjallouli | 0:a948f5f3904c | 322 | } else continue; |
sjallouli | 0:a948f5f3904c | 323 | } |
sjallouli | 0:a948f5f3904c | 324 | } |
sjallouli | 0:a948f5f3904c | 325 | |
sjallouli | 0:a948f5f3904c | 326 | return p_char; |
sjallouli | 0:a948f5f3904c | 327 | } |
sjallouli | 0:a948f5f3904c | 328 | |
sjallouli | 0:a948f5f3904c | 329 |