Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Hello_BLE F446RE-BLE
Fork of X_NUCLEO_IDB0XA1 by
BlueNRGGattServer.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 /** 00017 ****************************************************************************** 00018 * @file BlueNRGGattServer.cpp 00019 * @author STMicroelectronics 00020 * @brief Implementation of BlueNRG BLE_API GattServer Class 00021 ****************************************************************************** 00022 * @copy 00023 * 00024 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 00025 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 00026 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 00027 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 00028 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 00029 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 00030 * 00031 * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2> 00032 */ 00033 00034 00035 // ANDREA: Changed some types (e.g., tHalUint8 --> uint8_t) 00036 00037 /** @defgroup BlueNRGGATTSERVER 00038 * @brief BlueNRG BLE_API GattServer Adaptation 00039 * @{ 00040 */ 00041 00042 #include "BlueNRGGattServer.h" 00043 #include "mbed.h" 00044 #include "BlueNRGGap.h" 00045 #include "Utils.h" 00046 00047 /**************************************************************************/ 00048 /*! 00049 @brief Adds a new service to the GATT table on the peripheral 00050 00051 @params[in] service 00052 Pointer to instance of the Gatt Server to add 00053 00054 @returns ble_error_t 00055 00056 @retval BLE_ERROR_NONE 00057 Everything executed properly 00058 00059 @section EXAMPLE 00060 00061 @code 00062 00063 @endcode 00064 */ 00065 /**************************************************************************/ 00066 ble_error_t BlueNRGGattServer::addService(GattService &service) 00067 { 00068 /* ToDo: Make sure we don't overflow the array, etc. */ 00069 /* ToDo: Make sure this service UUID doesn't already exist (?) */ 00070 /* ToDo: Basic validation */ 00071 00072 tBleStatus ret; 00073 uint8_t type; 00074 uint16_t short_uuid; 00075 uint8_t primary_short_uuid[2]; 00076 uint8_t primary_base_uuid[16]; 00077 uint8_t char_base_uuid[16]; 00078 const uint8_t *base_uuid; 00079 const uint8_t *base_char_uuid; 00080 00081 uint8_t charsCount = 0; 00082 uint8_t maxAttrRecords = 0; 00083 00084 type = (service.getUUID()).shortOrLong(); 00085 DEBUG("AddService(): Type:%d\n\r", type); 00086 00087 /* Add the service to the BlueNRG */ 00088 short_uuid = (service.getUUID()).getShortUUID(); 00089 STORE_LE_16(primary_short_uuid, short_uuid); 00090 00091 if(type==UUID::UUID_TYPE_LONG) { 00092 base_uuid = (service.getUUID()).getBaseUUID(); 00093 00094 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], 00095 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]); 00096 } 00097 00098 charsCount = service.getCharacteristicCount(); 00099 //1(service record)+2records*char+1record*char_desc 00100 maxAttrRecords = 1+3*charsCount; 00101 00102 if(type==UUID::UUID_TYPE_SHORT) { 00103 ret = aci_gatt_add_serv(UUID_TYPE_16, 00104 primary_short_uuid, 00105 PRIMARY_SERVICE, 00106 maxAttrRecords/*7*/, 00107 &servHandle); 00108 DEBUG("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret); 00109 } 00110 else if(type==UUID::UUID_TYPE_LONG) { 00111 ret = aci_gatt_add_serv(UUID_TYPE_128, 00112 primary_base_uuid, 00113 PRIMARY_SERVICE, 00114 maxAttrRecords/*7*/, 00115 &servHandle); 00116 DEBUG("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret); 00117 } 00118 00119 service.setHandle(servHandle); 00120 //serviceHandleVector.push_back(servHandle); 00121 DEBUG("added servHandle handle =%u\n\r", servHandle); 00122 uint16_t bleCharacteristic; 00123 00124 //iterate to include all characteristics 00125 for (uint8_t i = 0; i < charsCount; i++) { 00126 GattCharacteristic *p_char = service.getCharacteristic(i); 00127 uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID(); 00128 00129 uint8_t int_8_uuid[2]; 00130 STORE_LE_16(int_8_uuid, char_uuid); 00131 00132 if(type==UUID::UUID_TYPE_LONG) { 00133 base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID(); 00134 00135 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], 00136 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]); 00137 } 00138 00139 DEBUG("Char Properties 0x%x\n\r", p_char->getProperties()); 00140 /* 00141 * Gatt_Evt_Mask -> HardCoded (0) 00142 * Encryption_Key_Size -> Hardcoded (16) 00143 * isVariable (variable length value field) -> Hardcoded (1) 00144 */ 00145 uint8_t Gatt_Evt_Mask = 0x0; 00146 00147 if((p_char->getProperties() & 00148 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE| 00149 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) { 00150 DEBUG("Setting up Gatt GATT_NOTIFY_ATTRIBUTE_WRITE Mask\n\r"); 00151 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_ATTRIBUTE_WRITE; 00152 } 00153 if((p_char->getProperties() & 00154 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ| 00155 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) { 00156 DEBUG("Setting up Gatt GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP Mask\n\r"); 00157 Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP; 00158 } //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check. 00159 00160 if(type==UUID::UUID_TYPE_SHORT) { 00161 ret = aci_gatt_add_char(service.getHandle(), 00162 UUID_TYPE_16, 00163 int_8_uuid, 00164 p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/, 00165 p_char->getProperties(), 00166 ATTR_PERMISSION_NONE, 00167 Gatt_Evt_Mask /*Gatt_Evt_Mask*/, 00168 16 /*Encryption_Key_Size*/, 00169 1 /*isVariable*/, 00170 &bleCharacteristic); 00171 00172 DEBUG("aci_gatt_add_char UUID_TYPE_16 props=%d MaxLength=%d ret=%d\n\r", p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret); 00173 00174 } else if(type==UUID::UUID_TYPE_LONG) { 00175 ret = aci_gatt_add_char(service.getHandle(), 00176 UUID_TYPE_128, 00177 char_base_uuid, 00178 p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/, 00179 p_char->getProperties(), 00180 ATTR_PERMISSION_NONE, 00181 Gatt_Evt_Mask /*Gatt_Evt_Mask*/, 00182 16 /*Encryption_Key_Size*/, 00183 1 /*isVariable*/, 00184 &bleCharacteristic); 00185 00186 DEBUG("aci_gatt_add_char UUID_TYPE_128 props=%d MaxLength=%d ret=%d\n\r", p_char->getProperties(), p_char->getValueAttribute().getMaxLength(), ret); 00187 } 00188 00189 /* Update the characteristic handle */ 00190 //uint16_t charHandle = characteristicCount; 00191 00192 bleCharHanldeMap.insert(std::pair<uint16_t, uint16_t>(bleCharacteristic, servHandle)); 00193 00194 p_characteristics[characteristicCount++] = p_char; 00195 p_char->getValueAttribute().setHandle(bleCharacteristic); //Set the characteristic count as the corresponding char handle 00196 DEBUG("added bleCharacteristic handle =%u\n\r", bleCharacteristic); 00197 00198 if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) { 00199 write(p_char->getValueAttribute().getHandle(), p_char->getValueAttribute().getValuePtr(), p_char->getValueAttribute().getInitialLength(), false /* localOnly */); 00200 } 00201 00202 // add descriptors now 00203 uint16_t descHandle = 0; 00204 DEBUG("p_char->getDescriptorCount()=%d\n\r", p_char->getDescriptorCount()); 00205 00206 for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) { 00207 GattAttribute *descriptor = p_char->getDescriptor(descIndex); 00208 uint16_t shortUUID = descriptor->getUUID().getShortUUID(); 00209 const uint8_t uuidArray[] = {(uint8_t)((shortUUID>>8)&0xFF), (uint8_t)((shortUUID&0xFF))}; 00210 ret = aci_gatt_add_char_desc(service.getHandle(), p_char->getValueAttribute().getHandle(), 00211 CHAR_DESC_TYPE_16_BIT, uuidArray, descriptor->getMaxLength(), descriptor->getInitialLength(), 00212 descriptor->getValuePtr(), CHAR_DESC_SECURITY_PERMISSION, CHAR_DESC_ACCESS_PERMISSION, GATT_NOTIFY_ATTRIBUTE_WRITE, 00213 MIN_ENCRY_KEY_SIZE, CHAR_ATTRIBUTE_LEN_IS_FIXED, &descHandle); 00214 DEBUG("Adding Descriptor descriptor handle=%d ret=%d\n\r", descHandle, ret); 00215 if(ret==(tBleStatus)0) { 00216 DEBUG("Descriptor added successfully, descriptor handle=%d\n\r", descHandle); 00217 descriptor->setHandle(descHandle); 00218 } 00219 } 00220 00221 } 00222 00223 serviceCount++; 00224 00225 //FIXME: There is no GattService pointer array in GattServer. 00226 // There should be one? (Only the user is aware of GattServices!) Report to forum. 00227 00228 return BLE_ERROR_NONE; 00229 } 00230 00231 /**************************************************************************/ 00232 /*! 00233 @brief Reads the value of a characteristic, based on the service 00234 and characteristic index fields 00235 00236 @param[in] charHandle 00237 The handle of the GattCharacteristic to read from 00238 @param[in] buffer 00239 Buffer to hold the the characteristic's value 00240 (raw byte array in LSB format) 00241 @param[in] lengthP 00242 The number of bytes read into the buffer 00243 00244 @returns ble_error_t 00245 00246 @retval BLE_ERROR_NONE 00247 Everything executed properly 00248 00249 @section EXAMPLE 00250 00251 @code 00252 00253 @endcode 00254 */ 00255 /**************************************************************************/ 00256 ble_error_t BlueNRGGattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP) 00257 { 00258 DEBUG("ReadValue() Not Supported\n\r"); 00259 return BLE_ERROR_NONE; 00260 } 00261 00262 /**************************************************************************/ 00263 /*! 00264 @brief Updates the value of a characteristic, based on the service 00265 and characteristic index fields 00266 00267 @param[in] charHandle 00268 The handle of the GattCharacteristic to write to 00269 @param[in] buffer 00270 Data to use when updating the characteristic's value 00271 (raw byte array in LSB format) 00272 @param[in] len 00273 The number of bytes in buffer 00274 00275 @returns ble_error_t 00276 00277 @retval BLE_ERROR_NONE 00278 Everything executed properly 00279 00280 @section EXAMPLE 00281 00282 @code 00283 00284 @endcode 00285 */ 00286 /**************************************************************************/ 00287 // <<<ANDREA>>> 00288 ble_error_t BlueNRGGattServer::readValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) { 00289 // Empty by now 00290 return BLE_ERROR_NONE; 00291 } 00292 00293 ble_error_t BlueNRGGattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly) { 00294 // Empty by now 00295 return BLE_ERROR_NONE; 00296 } 00297 00298 ble_error_t BlueNRGGattServer::write(GattAttribute::Handle_t charHandle, const uint8_t buffer[], uint16_t len, bool localOnly) 00299 { 00300 tBleStatus ret; 00301 //uint8_t buff[2]; 00302 00303 DEBUG("updating bleCharacteristic charHandle =%u, corresponding serviceHanle= %u len=%d\n\r", charHandle, bleCharHanldeMap.find(charHandle)->second, len); 00304 /* 00305 for(int i=0; i<len; i++) { 00306 DEBUG("buffer[%d]=%d\n\r", i, buffer[i]); 00307 } 00308 */ 00309 ret = aci_gatt_update_char_value(bleCharHanldeMap.find(charHandle)->second, charHandle, 0, len, buffer); 00310 00311 if (ret != BLE_STATUS_SUCCESS){ 00312 DEBUG("Error while updating characteristic (ret=0x%x).\n\r", ret) ; 00313 return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value 00314 //FIXME: Define Error values equivalent to BlueNRG Error Codes. 00315 } 00316 00317 //Generate Data Sent Event Here? (GattServerEvents::GATT_EVENT_DATA_SENT) //FIXME: Is this correct? 00318 //Check if characteristic property is NOTIFY|INDICATE, if yes generate event 00319 GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(charHandle); 00320 if(p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY 00321 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) { 00322 DEBUG("Generate event after updating\n\r"); 00323 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT, charHandle); 00324 } 00325 00326 return BLE_ERROR_NONE; 00327 } 00328 00329 /**************************************************************************/ 00330 /*! 00331 @brief Reads a value according to the handle provided 00332 00333 @param[in] charHandle 00334 The handle of the GattCharacteristic to read from 00335 00336 @returns ble_error_t 00337 00338 @retval BLE_ERROR_NONE 00339 Everything executed properly 00340 00341 @section EXAMPLE 00342 00343 @code 00344 00345 @endcode 00346 */ 00347 /**************************************************************************/ 00348 ble_error_t BlueNRGGattServer::Read_Request_CB(uint16_t handle) 00349 { 00350 //signed short refvalue; 00351 uint16_t gapConnectionHandle = BlueNRGGap::getInstance().getConnectionHandle(); 00352 00353 GattReadCallbackParams readParams; 00354 readParams.handle = handle; 00355 00356 //DEBUG("readParams.charHandle = %d\n\r", readParams.charHandle); 00357 HCIDataReadEvent(&readParams); 00358 00359 //EXIT: 00360 if(gapConnectionHandle != 0){ 00361 //DEBUG("Calling aci_gatt_allow_read\n\r"); 00362 aci_gatt_allow_read(gapConnectionHandle); 00363 } 00364 00365 return BLE_ERROR_NONE; 00366 } 00367 00368 /**************************************************************************/ 00369 /*! 00370 @brief Returns the GattCharacteristic according to the handle provided 00371 00372 @param[in] charHandle 00373 The handle of the GattCharacteristic 00374 00375 @returns ble_error_t 00376 00377 @retval BLE_ERROR_NONE 00378 Everything executed properly 00379 00380 @section EXAMPLE 00381 00382 @code 00383 00384 @endcode 00385 */ 00386 /**************************************************************************/ 00387 GattCharacteristic* BlueNRGGattServer::getCharacteristicFromHandle(uint16_t attrHandle) 00388 { 00389 GattCharacteristic *p_char = NULL; 00390 int i; 00391 uint16_t handle, handle_1; 00392 00393 DEBUG("BlueNRGGattServer::getCharacteristicFromHandle()>>Attribute Handle received %d\n\r",attrHandle); 00394 for(i=0; i<characteristicCount; i++) 00395 { 00396 handle = p_characteristics[i]->getValueAttribute().getHandle(); 00397 DEBUG("handle(%d)=%d\n\r", i, handle); 00398 if(i==characteristicCount-1)//Last Characteristic check 00399 { 00400 if(attrHandle>=handle) 00401 { 00402 p_char = p_characteristics[i]; 00403 DEBUG("Found Characteristic Properties 0x%x (handle=%d)\n\r",p_char->getProperties(), handle); 00404 break; 00405 } 00406 } 00407 else { 00408 handle_1 = p_characteristics[i+1]->getValueAttribute().getHandle(); 00409 //Testing if attribute handle is between two Characteristic Handles 00410 if(attrHandle>=handle && attrHandle<handle_1) 00411 { 00412 p_char = p_characteristics[i]; 00413 DEBUG("Found Characteristic Properties 0x%x (handle=%d handle_1=%d)\n\r",p_char->getProperties(), handle, handle_1); 00414 break; 00415 } else continue; 00416 } 00417 } 00418 00419 return p_char; 00420 } 00421 00422 void BlueNRGGattServer::HCIDataWrittenEvent(const GattWriteCallbackParams *params) { 00423 this->handleDataWrittenEvent(params); 00424 } 00425 00426 void BlueNRGGattServer::HCIDataReadEvent(const GattReadCallbackParams *params) { 00427 DEBUG("Called HCIDataReadEvent\n\r"); 00428 this->handleDataReadEvent(params); 00429 } 00430 00431 void BlueNRGGattServer::HCIEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) { 00432 this->handleEvent(type, charHandle); 00433 } 00434 00435 void BlueNRGGattServer::HCIDataSentEvent(unsigned count) { 00436 this->handleDataSentEvent(count); 00437 } 00438 00439 00440 ble_error_t BlueNRGGattServer::initializeGATTDatabase(void) { 00441 // <TODO> 00442 return (ble_error_t)0; 00443 }
Generated on Tue Jul 12 2022 17:16:07 by
1.7.2
