adaptation for bkap
Fork of X_NUCLEO_IDB0XA1 by
source/BlueNRGGattServer.cpp@132:51056160fa4a, 2015-10-07 (annotated)
- Committer:
- Wolfgang Betz
- Date:
- Wed Oct 07 08:39:04 2015 +0200
- Revision:
- 132:51056160fa4a
- Parent:
- 130:770ce14d3d15
- Child:
- 142:adf1567d3900
Andrea's version as of mail from 10/06/2015 05:56 PM
From: Andrea PALMIERI <andrea.palmieri@st.com>
To: Wolfgang BETZ <wolfgang.betz@st.com>, Rohit Grover <rohit.grover@arm.com>,
Antonio VILEI <antonio.vilei@st.com>, Mihail Stoyanov
<Mihail.Stoyanov@arm.com>
CC: Nicola CAPOVILLA <nicola.capovilla@st.com>, Silvio Lucio OLIVA
<silvio.oliva@st.com>, "jonathan.austin@arm.com" <jonathan.austin@arm.com>
Content-Class: urn:content-classes:message
Date: Tue, 6 Oct 2015 17:56:34 +0200
Subject: RE: Arduino Compatibility of X-NUCLEO-IDB04A1
Who changed what in which revision?
User | Revision | Line number | New 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>© COPYRIGHT 2013 STMicroelectronics</center></h2> |
Wolfgang Betz |
130:770ce14d3d15 | 32 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 33 | |
Wolfgang Betz |
130:770ce14d3d15 | 34 | |
Wolfgang Betz |
130:770ce14d3d15 | 35 | // ANDREA: Changed some types (e.g., tHalUint8 --> uint8_t) |
Wolfgang Betz |
130:770ce14d3d15 | 36 | |
Wolfgang Betz |
130:770ce14d3d15 | 37 | /** @defgroup BlueNRGGATTSERVER |
Wolfgang Betz |
130:770ce14d3d15 | 38 | * @brief BlueNRG BLE_API GattServer Adaptation |
Wolfgang Betz |
130:770ce14d3d15 | 39 | * @{ |
Wolfgang Betz |
130:770ce14d3d15 | 40 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 41 | |
Wolfgang Betz |
130:770ce14d3d15 | 42 | #include "BlueNRGGattServer.h" |
Wolfgang Betz |
130:770ce14d3d15 | 43 | #include "mbed.h" |
Wolfgang Betz |
130:770ce14d3d15 | 44 | #include "BlueNRGGap.h" |
Wolfgang Betz |
130:770ce14d3d15 | 45 | #include "Utils.h" |
Wolfgang Betz |
132:51056160fa4a | 46 | #include "debug.h" |
Wolfgang Betz |
130:770ce14d3d15 | 47 | |
Wolfgang Betz |
130:770ce14d3d15 | 48 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 49 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 50 | @brief Adds a new service to the GATT table on the peripheral |
Wolfgang Betz |
130:770ce14d3d15 | 51 | |
Wolfgang Betz |
130:770ce14d3d15 | 52 | @params[in] service |
Wolfgang Betz |
130:770ce14d3d15 | 53 | Pointer to instance of the Gatt Server to add |
Wolfgang Betz |
130:770ce14d3d15 | 54 | |
Wolfgang Betz |
130:770ce14d3d15 | 55 | @returns ble_error_t |
Wolfgang Betz |
130:770ce14d3d15 | 56 | |
Wolfgang Betz |
130:770ce14d3d15 | 57 | @retval BLE_ERROR_NONE |
Wolfgang Betz |
130:770ce14d3d15 | 58 | Everything executed properly |
Wolfgang Betz |
130:770ce14d3d15 | 59 | |
Wolfgang Betz |
130:770ce14d3d15 | 60 | @section EXAMPLE |
Wolfgang Betz |
130:770ce14d3d15 | 61 | |
Wolfgang Betz |
130:770ce14d3d15 | 62 | @code |
Wolfgang Betz |
130:770ce14d3d15 | 63 | |
Wolfgang Betz |
130:770ce14d3d15 | 64 | @endcode |
Wolfgang Betz |
130:770ce14d3d15 | 65 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 66 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 67 | ble_error_t BlueNRGGattServer::addService(GattService &service) |
Wolfgang Betz |
130:770ce14d3d15 | 68 | { |
Wolfgang Betz |
130:770ce14d3d15 | 69 | /* ToDo: Make sure we don't overflow the array, etc. */ |
Wolfgang Betz |
130:770ce14d3d15 | 70 | /* ToDo: Make sure this service UUID doesn't already exist (?) */ |
Wolfgang Betz |
130:770ce14d3d15 | 71 | /* ToDo: Basic validation */ |
Wolfgang Betz |
130:770ce14d3d15 | 72 | |
Wolfgang Betz |
130:770ce14d3d15 | 73 | tBleStatus ret; |
Wolfgang Betz |
130:770ce14d3d15 | 74 | uint8_t type; |
Wolfgang Betz |
130:770ce14d3d15 | 75 | uint16_t short_uuid; |
Wolfgang Betz |
130:770ce14d3d15 | 76 | uint8_t primary_short_uuid[2]; |
Wolfgang Betz |
130:770ce14d3d15 | 77 | uint8_t primary_base_uuid[16]; |
Wolfgang Betz |
130:770ce14d3d15 | 78 | uint8_t char_base_uuid[16]; |
Wolfgang Betz |
130:770ce14d3d15 | 79 | const uint8_t *base_uuid; |
Wolfgang Betz |
130:770ce14d3d15 | 80 | const uint8_t *base_char_uuid; |
Wolfgang Betz |
130:770ce14d3d15 | 81 | |
Wolfgang Betz |
130:770ce14d3d15 | 82 | uint8_t charsCount = 0; |
Wolfgang Betz |
130:770ce14d3d15 | 83 | uint8_t maxAttrRecords = 0; |
Wolfgang Betz |
130:770ce14d3d15 | 84 | |
Wolfgang Betz |
130:770ce14d3d15 | 85 | type = (service.getUUID()).shortOrLong(); |
Wolfgang Betz |
132:51056160fa4a | 86 | PRINTF("AddService(): Type:%d\n\r", type); |
Wolfgang Betz |
130:770ce14d3d15 | 87 | |
Wolfgang Betz |
130:770ce14d3d15 | 88 | /* Add the service to the BlueNRG */ |
Wolfgang Betz |
130:770ce14d3d15 | 89 | short_uuid = (service.getUUID()).getShortUUID(); |
Wolfgang Betz |
130:770ce14d3d15 | 90 | STORE_LE_16(primary_short_uuid, short_uuid); |
Wolfgang Betz |
130:770ce14d3d15 | 91 | |
Wolfgang Betz |
130:770ce14d3d15 | 92 | if(type==UUID::UUID_TYPE_LONG) { |
Wolfgang Betz |
130:770ce14d3d15 | 93 | base_uuid = (service.getUUID()).getBaseUUID(); |
Wolfgang Betz |
130:770ce14d3d15 | 94 | |
Wolfgang Betz |
130:770ce14d3d15 | 95 | 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 |
130:770ce14d3d15 | 96 | 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 |
130:770ce14d3d15 | 97 | } |
Wolfgang Betz |
130:770ce14d3d15 | 98 | |
Wolfgang Betz |
130:770ce14d3d15 | 99 | charsCount = service.getCharacteristicCount(); |
Wolfgang Betz |
130:770ce14d3d15 | 100 | //1(service record)+2records*char+1record*char_desc |
Wolfgang Betz |
130:770ce14d3d15 | 101 | maxAttrRecords = 1+3*charsCount; |
Wolfgang Betz |
130:770ce14d3d15 | 102 | |
Wolfgang Betz |
130:770ce14d3d15 | 103 | if(type==UUID::UUID_TYPE_SHORT) { |
Wolfgang Betz |
130:770ce14d3d15 | 104 | ret = aci_gatt_add_serv(UUID_TYPE_16, |
Wolfgang Betz |
130:770ce14d3d15 | 105 | primary_short_uuid, |
Wolfgang Betz |
130:770ce14d3d15 | 106 | PRIMARY_SERVICE, |
Wolfgang Betz |
130:770ce14d3d15 | 107 | maxAttrRecords/*7*/, |
Wolfgang Betz |
130:770ce14d3d15 | 108 | &servHandle); |
Wolfgang Betz |
132:51056160fa4a | 109 | PRINTF("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret); |
Wolfgang Betz |
130:770ce14d3d15 | 110 | } |
Wolfgang Betz |
130:770ce14d3d15 | 111 | else if(type==UUID::UUID_TYPE_LONG) { |
Wolfgang Betz |
130:770ce14d3d15 | 112 | ret = aci_gatt_add_serv(UUID_TYPE_128, |
Wolfgang Betz |
130:770ce14d3d15 | 113 | primary_base_uuid, |
Wolfgang Betz |
130:770ce14d3d15 | 114 | PRIMARY_SERVICE, |
Wolfgang Betz |
130:770ce14d3d15 | 115 | maxAttrRecords/*7*/, |
Wolfgang Betz |
130:770ce14d3d15 | 116 | &servHandle); |
Wolfgang Betz |
132:51056160fa4a | 117 | PRINTF("aci_gatt_add_serv UUID_TYPE_LONG ret=%d\n\r", ret); |
Wolfgang Betz |
130:770ce14d3d15 | 118 | } |
Wolfgang Betz |
130:770ce14d3d15 | 119 | |
Wolfgang Betz |
130:770ce14d3d15 | 120 | service.setHandle(servHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 121 | //serviceHandleVector.push_back(servHandle); |
Wolfgang Betz |
132:51056160fa4a | 122 | PRINTF("added servHandle handle =%u\n\r", servHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 123 | uint16_t bleCharacteristic; |
Wolfgang Betz |
130:770ce14d3d15 | 124 | |
Wolfgang Betz |
130:770ce14d3d15 | 125 | //iterate to include all characteristics |
Wolfgang Betz |
130:770ce14d3d15 | 126 | for (uint8_t i = 0; i < charsCount; i++) { |
Wolfgang Betz |
130:770ce14d3d15 | 127 | GattCharacteristic *p_char = service.getCharacteristic(i); |
Wolfgang Betz |
130:770ce14d3d15 | 128 | uint16_t char_uuid = (p_char->getValueAttribute().getUUID()).getShortUUID(); |
Wolfgang Betz |
130:770ce14d3d15 | 129 | |
Wolfgang Betz |
130:770ce14d3d15 | 130 | uint8_t int_8_uuid[2]; |
Wolfgang Betz |
130:770ce14d3d15 | 131 | STORE_LE_16(int_8_uuid, char_uuid); |
Wolfgang Betz |
130:770ce14d3d15 | 132 | |
Wolfgang Betz |
130:770ce14d3d15 | 133 | if(type==UUID::UUID_TYPE_LONG) { |
Wolfgang Betz |
130:770ce14d3d15 | 134 | base_char_uuid = (p_char->getValueAttribute().getUUID()).getBaseUUID(); |
Wolfgang Betz |
130:770ce14d3d15 | 135 | |
Wolfgang Betz |
130:770ce14d3d15 | 136 | 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 |
130:770ce14d3d15 | 137 | 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 |
130:770ce14d3d15 | 138 | } |
Wolfgang Betz |
130:770ce14d3d15 | 139 | |
Wolfgang Betz |
132:51056160fa4a | 140 | PRINTF("Char Properties 0x%x\n\r", p_char->getProperties()); |
Wolfgang Betz |
130:770ce14d3d15 | 141 | /* |
Wolfgang Betz |
130:770ce14d3d15 | 142 | * Gatt_Evt_Mask -> HardCoded (0) |
Wolfgang Betz |
130:770ce14d3d15 | 143 | * Encryption_Key_Size -> Hardcoded (16) |
Wolfgang Betz |
130:770ce14d3d15 | 144 | * isVariable (variable length value field) -> Hardcoded (1) |
Wolfgang Betz |
130:770ce14d3d15 | 145 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 146 | uint8_t Gatt_Evt_Mask = 0x0; |
Wolfgang Betz |
130:770ce14d3d15 | 147 | |
Wolfgang Betz |
130:770ce14d3d15 | 148 | if((p_char->getProperties() & |
Wolfgang Betz |
130:770ce14d3d15 | 149 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE| |
Wolfgang Betz |
130:770ce14d3d15 | 150 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) { |
Wolfgang Betz |
132:51056160fa4a | 151 | PRINTF("Setting up Gatt GATT_NOTIFY_ATTRIBUTE_WRITE Mask\n\r"); |
Wolfgang Betz |
130:770ce14d3d15 | 152 | Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_ATTRIBUTE_WRITE; |
Wolfgang Betz |
130:770ce14d3d15 | 153 | } |
Wolfgang Betz |
130:770ce14d3d15 | 154 | if((p_char->getProperties() & |
Wolfgang Betz |
130:770ce14d3d15 | 155 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ| |
Wolfgang Betz |
130:770ce14d3d15 | 156 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) { |
Wolfgang Betz |
132:51056160fa4a | 157 | PRINTF("Setting up Gatt GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP Mask\n\r"); |
Wolfgang Betz |
130:770ce14d3d15 | 158 | Gatt_Evt_Mask = Gatt_Evt_Mask | GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP; |
Wolfgang Betz |
130:770ce14d3d15 | 159 | } //This will support also GATT_SERVER_ATTR_READ_WRITE since it will be covered by previous if() check. |
Wolfgang Betz |
130:770ce14d3d15 | 160 | |
Wolfgang Betz |
130:770ce14d3d15 | 161 | if(type==UUID::UUID_TYPE_SHORT) { |
Wolfgang Betz |
130:770ce14d3d15 | 162 | ret = aci_gatt_add_char(service.getHandle(), |
Wolfgang Betz |
130:770ce14d3d15 | 163 | UUID_TYPE_16, |
Wolfgang Betz |
130:770ce14d3d15 | 164 | int_8_uuid, |
Wolfgang Betz |
130:770ce14d3d15 | 165 | p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/, |
Wolfgang Betz |
130:770ce14d3d15 | 166 | p_char->getProperties(), |
Wolfgang Betz |
130:770ce14d3d15 | 167 | ATTR_PERMISSION_NONE, |
Wolfgang Betz |
130:770ce14d3d15 | 168 | Gatt_Evt_Mask /*Gatt_Evt_Mask*/, |
Wolfgang Betz |
130:770ce14d3d15 | 169 | 16 /*Encryption_Key_Size*/, |
Wolfgang Betz |
130:770ce14d3d15 | 170 | 1 /*isVariable*/, |
Wolfgang Betz |
130:770ce14d3d15 | 171 | &bleCharacteristic); |
Wolfgang Betz |
130:770ce14d3d15 | 172 | |
Wolfgang Betz |
132:51056160fa4a | 173 | PRINTF("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 |
130:770ce14d3d15 | 174 | |
Wolfgang Betz |
130:770ce14d3d15 | 175 | } else if(type==UUID::UUID_TYPE_LONG) { |
Wolfgang Betz |
130:770ce14d3d15 | 176 | ret = aci_gatt_add_char(service.getHandle(), |
Wolfgang Betz |
130:770ce14d3d15 | 177 | UUID_TYPE_128, |
Wolfgang Betz |
130:770ce14d3d15 | 178 | char_base_uuid, |
Wolfgang Betz |
130:770ce14d3d15 | 179 | p_char->getValueAttribute().getMaxLength() /*2*/ /*Value Length*/, |
Wolfgang Betz |
130:770ce14d3d15 | 180 | p_char->getProperties(), |
Wolfgang Betz |
130:770ce14d3d15 | 181 | ATTR_PERMISSION_NONE, |
Wolfgang Betz |
130:770ce14d3d15 | 182 | Gatt_Evt_Mask /*Gatt_Evt_Mask*/, |
Wolfgang Betz |
130:770ce14d3d15 | 183 | 16 /*Encryption_Key_Size*/, |
Wolfgang Betz |
130:770ce14d3d15 | 184 | 1 /*isVariable*/, |
Wolfgang Betz |
130:770ce14d3d15 | 185 | &bleCharacteristic); |
Wolfgang Betz |
130:770ce14d3d15 | 186 | |
Wolfgang Betz |
132:51056160fa4a | 187 | PRINTF("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 |
130:770ce14d3d15 | 188 | } |
Wolfgang Betz |
130:770ce14d3d15 | 189 | |
Wolfgang Betz |
130:770ce14d3d15 | 190 | /* Update the characteristic handle */ |
Wolfgang Betz |
130:770ce14d3d15 | 191 | //uint16_t charHandle = characteristicCount; |
Wolfgang Betz |
130:770ce14d3d15 | 192 | |
Wolfgang Betz |
130:770ce14d3d15 | 193 | bleCharHanldeMap.insert(std::pair<uint16_t, uint16_t>(bleCharacteristic, servHandle)); |
Wolfgang Betz |
130:770ce14d3d15 | 194 | |
Wolfgang Betz |
130:770ce14d3d15 | 195 | p_characteristics[characteristicCount++] = p_char; |
Wolfgang Betz |
130:770ce14d3d15 | 196 | p_char->getValueAttribute().setHandle(bleCharacteristic); //Set the characteristic count as the corresponding char handle |
Wolfgang Betz |
132:51056160fa4a | 197 | PRINTF("added bleCharacteristic handle =%u\n\r", bleCharacteristic); |
Wolfgang Betz |
130:770ce14d3d15 | 198 | |
Wolfgang Betz |
130:770ce14d3d15 | 199 | if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) { |
Wolfgang Betz |
130:770ce14d3d15 | 200 | write(p_char->getValueAttribute().getHandle(), p_char->getValueAttribute().getValuePtr(), p_char->getValueAttribute().getInitialLength(), false /* localOnly */); |
Wolfgang Betz |
130:770ce14d3d15 | 201 | } |
Wolfgang Betz |
130:770ce14d3d15 | 202 | |
Wolfgang Betz |
130:770ce14d3d15 | 203 | // add descriptors now |
Wolfgang Betz |
130:770ce14d3d15 | 204 | uint16_t descHandle = 0; |
Wolfgang Betz |
132:51056160fa4a | 205 | PRINTF("p_char->getDescriptorCount()=%d\n\r", p_char->getDescriptorCount()); |
Wolfgang Betz |
130:770ce14d3d15 | 206 | |
Wolfgang Betz |
130:770ce14d3d15 | 207 | for(uint8_t descIndex=0; descIndex<p_char->getDescriptorCount(); descIndex++) { |
Wolfgang Betz |
130:770ce14d3d15 | 208 | GattAttribute *descriptor = p_char->getDescriptor(descIndex); |
Wolfgang Betz |
130:770ce14d3d15 | 209 | uint16_t shortUUID = descriptor->getUUID().getShortUUID(); |
Wolfgang Betz |
130:770ce14d3d15 | 210 | const uint8_t uuidArray[] = {(uint8_t)((shortUUID>>8)&0xFF), (uint8_t)((shortUUID&0xFF))}; |
Wolfgang Betz |
130:770ce14d3d15 | 211 | ret = aci_gatt_add_char_desc(service.getHandle(), p_char->getValueAttribute().getHandle(), |
Wolfgang Betz |
130:770ce14d3d15 | 212 | CHAR_DESC_TYPE_16_BIT, uuidArray, descriptor->getMaxLength(), descriptor->getInitialLength(), |
Wolfgang Betz |
130:770ce14d3d15 | 213 | descriptor->getValuePtr(), CHAR_DESC_SECURITY_PERMISSION, CHAR_DESC_ACCESS_PERMISSION, GATT_NOTIFY_ATTRIBUTE_WRITE, |
Wolfgang Betz |
130:770ce14d3d15 | 214 | MIN_ENCRY_KEY_SIZE, CHAR_ATTRIBUTE_LEN_IS_FIXED, &descHandle); |
Wolfgang Betz |
132:51056160fa4a | 215 | PRINTF("Adding Descriptor descriptor handle=%d ret=%d\n\r", descHandle, ret); |
Wolfgang Betz |
130:770ce14d3d15 | 216 | if(ret==(tBleStatus)0) { |
Wolfgang Betz |
132:51056160fa4a | 217 | PRINTF("Descriptor added successfully, descriptor handle=%d\n\r", descHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 218 | descriptor->setHandle(descHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 219 | } |
Wolfgang Betz |
130:770ce14d3d15 | 220 | } |
Wolfgang Betz |
130:770ce14d3d15 | 221 | |
Wolfgang Betz |
130:770ce14d3d15 | 222 | } |
Wolfgang Betz |
130:770ce14d3d15 | 223 | |
Wolfgang Betz |
130:770ce14d3d15 | 224 | serviceCount++; |
Wolfgang Betz |
130:770ce14d3d15 | 225 | |
Wolfgang Betz |
130:770ce14d3d15 | 226 | //FIXME: There is no GattService pointer array in GattServer. |
Wolfgang Betz |
130:770ce14d3d15 | 227 | // There should be one? (Only the user is aware of GattServices!) Report to forum. |
Wolfgang Betz |
130:770ce14d3d15 | 228 | |
Wolfgang Betz |
130:770ce14d3d15 | 229 | return BLE_ERROR_NONE; |
Wolfgang Betz |
130:770ce14d3d15 | 230 | } |
Wolfgang Betz |
130:770ce14d3d15 | 231 | |
Wolfgang Betz |
130:770ce14d3d15 | 232 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 233 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 234 | @brief Reads the value of a characteristic, based on the service |
Wolfgang Betz |
130:770ce14d3d15 | 235 | and characteristic index fields |
Wolfgang Betz |
130:770ce14d3d15 | 236 | |
Wolfgang Betz |
130:770ce14d3d15 | 237 | @param[in] charHandle |
Wolfgang Betz |
130:770ce14d3d15 | 238 | The handle of the GattCharacteristic to read from |
Wolfgang Betz |
130:770ce14d3d15 | 239 | @param[in] buffer |
Wolfgang Betz |
130:770ce14d3d15 | 240 | Buffer to hold the the characteristic's value |
Wolfgang Betz |
130:770ce14d3d15 | 241 | (raw byte array in LSB format) |
Wolfgang Betz |
130:770ce14d3d15 | 242 | @param[in] lengthP |
Wolfgang Betz |
130:770ce14d3d15 | 243 | The number of bytes read into the buffer |
Wolfgang Betz |
130:770ce14d3d15 | 244 | |
Wolfgang Betz |
130:770ce14d3d15 | 245 | @returns ble_error_t |
Wolfgang Betz |
130:770ce14d3d15 | 246 | |
Wolfgang Betz |
130:770ce14d3d15 | 247 | @retval BLE_ERROR_NONE |
Wolfgang Betz |
130:770ce14d3d15 | 248 | Everything executed properly |
Wolfgang Betz |
130:770ce14d3d15 | 249 | |
Wolfgang Betz |
130:770ce14d3d15 | 250 | @section EXAMPLE |
Wolfgang Betz |
130:770ce14d3d15 | 251 | |
Wolfgang Betz |
130:770ce14d3d15 | 252 | @code |
Wolfgang Betz |
130:770ce14d3d15 | 253 | |
Wolfgang Betz |
130:770ce14d3d15 | 254 | @endcode |
Wolfgang Betz |
130:770ce14d3d15 | 255 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 256 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 257 | ble_error_t BlueNRGGattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t *const lengthP) |
Wolfgang Betz |
130:770ce14d3d15 | 258 | { |
Wolfgang Betz |
132:51056160fa4a | 259 | PRINTF("ReadValue() Not Supported\n\r"); |
Wolfgang Betz |
130:770ce14d3d15 | 260 | return BLE_ERROR_NONE; |
Wolfgang Betz |
130:770ce14d3d15 | 261 | } |
Wolfgang Betz |
130:770ce14d3d15 | 262 | |
Wolfgang Betz |
130:770ce14d3d15 | 263 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 264 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 265 | @brief Updates the value of a characteristic, based on the service |
Wolfgang Betz |
130:770ce14d3d15 | 266 | and characteristic index fields |
Wolfgang Betz |
130:770ce14d3d15 | 267 | |
Wolfgang Betz |
130:770ce14d3d15 | 268 | @param[in] charHandle |
Wolfgang Betz |
130:770ce14d3d15 | 269 | The handle of the GattCharacteristic to write to |
Wolfgang Betz |
130:770ce14d3d15 | 270 | @param[in] buffer |
Wolfgang Betz |
130:770ce14d3d15 | 271 | Data to use when updating the characteristic's value |
Wolfgang Betz |
130:770ce14d3d15 | 272 | (raw byte array in LSB format) |
Wolfgang Betz |
130:770ce14d3d15 | 273 | @param[in] len |
Wolfgang Betz |
130:770ce14d3d15 | 274 | The number of bytes in buffer |
Wolfgang Betz |
130:770ce14d3d15 | 275 | |
Wolfgang Betz |
130:770ce14d3d15 | 276 | @returns ble_error_t |
Wolfgang Betz |
130:770ce14d3d15 | 277 | |
Wolfgang Betz |
130:770ce14d3d15 | 278 | @retval BLE_ERROR_NONE |
Wolfgang Betz |
130:770ce14d3d15 | 279 | Everything executed properly |
Wolfgang Betz |
130:770ce14d3d15 | 280 | |
Wolfgang Betz |
130:770ce14d3d15 | 281 | @section EXAMPLE |
Wolfgang Betz |
130:770ce14d3d15 | 282 | |
Wolfgang Betz |
130:770ce14d3d15 | 283 | @code |
Wolfgang Betz |
130:770ce14d3d15 | 284 | |
Wolfgang Betz |
130:770ce14d3d15 | 285 | @endcode |
Wolfgang Betz |
130:770ce14d3d15 | 286 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 287 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 288 | // <<<ANDREA>>> |
Wolfgang Betz |
130:770ce14d3d15 | 289 | ble_error_t BlueNRGGattServer::readValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) { |
Wolfgang Betz |
130:770ce14d3d15 | 290 | // Empty by now |
Wolfgang Betz |
130:770ce14d3d15 | 291 | return BLE_ERROR_NONE; |
Wolfgang Betz |
130:770ce14d3d15 | 292 | } |
Wolfgang Betz |
130:770ce14d3d15 | 293 | |
Wolfgang Betz |
130:770ce14d3d15 | 294 | ble_error_t BlueNRGGattServer::write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly) { |
Wolfgang Betz |
130:770ce14d3d15 | 295 | // Empty by now |
Wolfgang Betz |
130:770ce14d3d15 | 296 | return BLE_ERROR_NONE; |
Wolfgang Betz |
130:770ce14d3d15 | 297 | } |
Wolfgang Betz |
130:770ce14d3d15 | 298 | |
Wolfgang Betz |
130:770ce14d3d15 | 299 | ble_error_t BlueNRGGattServer::write(GattAttribute::Handle_t charHandle, const uint8_t buffer[], uint16_t len, bool localOnly) |
Wolfgang Betz |
130:770ce14d3d15 | 300 | { |
Wolfgang Betz |
130:770ce14d3d15 | 301 | tBleStatus ret; |
Wolfgang Betz |
130:770ce14d3d15 | 302 | //uint8_t buff[2]; |
Wolfgang Betz |
130:770ce14d3d15 | 303 | |
Wolfgang Betz |
132:51056160fa4a | 304 | PRINTF("updating bleCharacteristic charHandle =%u, corresponding serviceHanle= %u len=%d\n\r", charHandle, bleCharHanldeMap.find(charHandle)->second, len); |
Wolfgang Betz |
130:770ce14d3d15 | 305 | /* |
Wolfgang Betz |
130:770ce14d3d15 | 306 | for(int i=0; i<len; i++) { |
Wolfgang Betz |
132:51056160fa4a | 307 | PRINTF("buffer[%d]=%d\n\r", i, buffer[i]); |
Wolfgang Betz |
130:770ce14d3d15 | 308 | } |
Wolfgang Betz |
130:770ce14d3d15 | 309 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 310 | ret = aci_gatt_update_char_value(bleCharHanldeMap.find(charHandle)->second, charHandle, 0, len, buffer); |
Wolfgang Betz |
130:770ce14d3d15 | 311 | |
Wolfgang Betz |
130:770ce14d3d15 | 312 | if (ret != BLE_STATUS_SUCCESS){ |
Wolfgang Betz |
132:51056160fa4a | 313 | PRINTF("Error while updating characteristic (ret=0x%x).\n\r", ret) ; |
Wolfgang Betz |
130:770ce14d3d15 | 314 | return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value |
Wolfgang Betz |
130:770ce14d3d15 | 315 | //FIXME: Define Error values equivalent to BlueNRG Error Codes. |
Wolfgang Betz |
130:770ce14d3d15 | 316 | } |
Wolfgang Betz |
130:770ce14d3d15 | 317 | |
Wolfgang Betz |
130:770ce14d3d15 | 318 | //Generate Data Sent Event Here? (GattServerEvents::GATT_EVENT_DATA_SENT) //FIXME: Is this correct? |
Wolfgang Betz |
130:770ce14d3d15 | 319 | //Check if characteristic property is NOTIFY|INDICATE, if yes generate event |
Wolfgang Betz |
130:770ce14d3d15 | 320 | GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(charHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 321 | if(p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
Wolfgang Betz |
130:770ce14d3d15 | 322 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) { |
Wolfgang Betz |
132:51056160fa4a | 323 | PRINTF("Generate event after updating\n\r"); |
Wolfgang Betz |
130:770ce14d3d15 | 324 | BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT, charHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 325 | } |
Wolfgang Betz |
130:770ce14d3d15 | 326 | |
Wolfgang Betz |
130:770ce14d3d15 | 327 | return BLE_ERROR_NONE; |
Wolfgang Betz |
130:770ce14d3d15 | 328 | } |
Wolfgang Betz |
130:770ce14d3d15 | 329 | |
Wolfgang Betz |
130:770ce14d3d15 | 330 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 331 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 332 | @brief Reads a value according to the handle provided |
Wolfgang Betz |
130:770ce14d3d15 | 333 | |
Wolfgang Betz |
130:770ce14d3d15 | 334 | @param[in] charHandle |
Wolfgang Betz |
130:770ce14d3d15 | 335 | The handle of the GattCharacteristic to read from |
Wolfgang Betz |
130:770ce14d3d15 | 336 | |
Wolfgang Betz |
130:770ce14d3d15 | 337 | @returns ble_error_t |
Wolfgang Betz |
130:770ce14d3d15 | 338 | |
Wolfgang Betz |
130:770ce14d3d15 | 339 | @retval BLE_ERROR_NONE |
Wolfgang Betz |
130:770ce14d3d15 | 340 | Everything executed properly |
Wolfgang Betz |
130:770ce14d3d15 | 341 | |
Wolfgang Betz |
130:770ce14d3d15 | 342 | @section EXAMPLE |
Wolfgang Betz |
130:770ce14d3d15 | 343 | |
Wolfgang Betz |
130:770ce14d3d15 | 344 | @code |
Wolfgang Betz |
130:770ce14d3d15 | 345 | |
Wolfgang Betz |
130:770ce14d3d15 | 346 | @endcode |
Wolfgang Betz |
130:770ce14d3d15 | 347 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 348 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 349 | ble_error_t BlueNRGGattServer::Read_Request_CB(uint16_t handle) |
Wolfgang Betz |
130:770ce14d3d15 | 350 | { |
Wolfgang Betz |
130:770ce14d3d15 | 351 | //signed short refvalue; |
Wolfgang Betz |
130:770ce14d3d15 | 352 | uint16_t gapConnectionHandle = BlueNRGGap::getInstance().getConnectionHandle(); |
Wolfgang Betz |
130:770ce14d3d15 | 353 | |
Wolfgang Betz |
130:770ce14d3d15 | 354 | GattReadCallbackParams readParams; |
Wolfgang Betz |
130:770ce14d3d15 | 355 | readParams.handle = handle; |
Wolfgang Betz |
130:770ce14d3d15 | 356 | |
Wolfgang Betz |
132:51056160fa4a | 357 | //PRINTF("readParams.charHandle = %d\n\r", readParams.charHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 358 | HCIDataReadEvent(&readParams); |
Wolfgang Betz |
130:770ce14d3d15 | 359 | |
Wolfgang Betz |
130:770ce14d3d15 | 360 | //EXIT: |
Wolfgang Betz |
130:770ce14d3d15 | 361 | if(gapConnectionHandle != 0){ |
Wolfgang Betz |
132:51056160fa4a | 362 | //PRINTF("Calling aci_gatt_allow_read\n\r"); |
Wolfgang Betz |
130:770ce14d3d15 | 363 | aci_gatt_allow_read(gapConnectionHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 364 | } |
Wolfgang Betz |
130:770ce14d3d15 | 365 | |
Wolfgang Betz |
130:770ce14d3d15 | 366 | return BLE_ERROR_NONE; |
Wolfgang Betz |
130:770ce14d3d15 | 367 | } |
Wolfgang Betz |
130:770ce14d3d15 | 368 | |
Wolfgang Betz |
130:770ce14d3d15 | 369 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 370 | /*! |
Wolfgang Betz |
130:770ce14d3d15 | 371 | @brief Returns the GattCharacteristic according to the handle provided |
Wolfgang Betz |
130:770ce14d3d15 | 372 | |
Wolfgang Betz |
130:770ce14d3d15 | 373 | @param[in] charHandle |
Wolfgang Betz |
130:770ce14d3d15 | 374 | The handle of the GattCharacteristic |
Wolfgang Betz |
130:770ce14d3d15 | 375 | |
Wolfgang Betz |
130:770ce14d3d15 | 376 | @returns ble_error_t |
Wolfgang Betz |
130:770ce14d3d15 | 377 | |
Wolfgang Betz |
130:770ce14d3d15 | 378 | @retval BLE_ERROR_NONE |
Wolfgang Betz |
130:770ce14d3d15 | 379 | Everything executed properly |
Wolfgang Betz |
130:770ce14d3d15 | 380 | |
Wolfgang Betz |
130:770ce14d3d15 | 381 | @section EXAMPLE |
Wolfgang Betz |
130:770ce14d3d15 | 382 | |
Wolfgang Betz |
130:770ce14d3d15 | 383 | @code |
Wolfgang Betz |
130:770ce14d3d15 | 384 | |
Wolfgang Betz |
130:770ce14d3d15 | 385 | @endcode |
Wolfgang Betz |
130:770ce14d3d15 | 386 | */ |
Wolfgang Betz |
130:770ce14d3d15 | 387 | /**************************************************************************/ |
Wolfgang Betz |
130:770ce14d3d15 | 388 | GattCharacteristic* BlueNRGGattServer::getCharacteristicFromHandle(uint16_t attrHandle) |
Wolfgang Betz |
130:770ce14d3d15 | 389 | { |
Wolfgang Betz |
130:770ce14d3d15 | 390 | GattCharacteristic *p_char = NULL; |
Wolfgang Betz |
130:770ce14d3d15 | 391 | int i; |
Wolfgang Betz |
130:770ce14d3d15 | 392 | uint16_t handle, handle_1; |
Wolfgang Betz |
130:770ce14d3d15 | 393 | |
Wolfgang Betz |
132:51056160fa4a | 394 | PRINTF("BlueNRGGattServer::getCharacteristicFromHandle()>>Attribute Handle received %d\n\r",attrHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 395 | for(i=0; i<characteristicCount; i++) |
Wolfgang Betz |
130:770ce14d3d15 | 396 | { |
Wolfgang Betz |
130:770ce14d3d15 | 397 | handle = p_characteristics[i]->getValueAttribute().getHandle(); |
Wolfgang Betz |
132:51056160fa4a | 398 | PRINTF("handle(%d)=%d\n\r", i, handle); |
Wolfgang Betz |
130:770ce14d3d15 | 399 | if(i==characteristicCount-1)//Last Characteristic check |
Wolfgang Betz |
130:770ce14d3d15 | 400 | { |
Wolfgang Betz |
130:770ce14d3d15 | 401 | if(attrHandle>=handle) |
Wolfgang Betz |
130:770ce14d3d15 | 402 | { |
Wolfgang Betz |
130:770ce14d3d15 | 403 | p_char = p_characteristics[i]; |
Wolfgang Betz |
132:51056160fa4a | 404 | PRINTF("Found Characteristic Properties 0x%x (handle=%d)\n\r",p_char->getProperties(), handle); |
Wolfgang Betz |
130:770ce14d3d15 | 405 | break; |
Wolfgang Betz |
130:770ce14d3d15 | 406 | } |
Wolfgang Betz |
130:770ce14d3d15 | 407 | } |
Wolfgang Betz |
130:770ce14d3d15 | 408 | else { |
Wolfgang Betz |
130:770ce14d3d15 | 409 | handle_1 = p_characteristics[i+1]->getValueAttribute().getHandle(); |
Wolfgang Betz |
130:770ce14d3d15 | 410 | //Testing if attribute handle is between two Characteristic Handles |
Wolfgang Betz |
130:770ce14d3d15 | 411 | if(attrHandle>=handle && attrHandle<handle_1) |
Wolfgang Betz |
130:770ce14d3d15 | 412 | { |
Wolfgang Betz |
130:770ce14d3d15 | 413 | p_char = p_characteristics[i]; |
Wolfgang Betz |
132:51056160fa4a | 414 | PRINTF("Found Characteristic Properties 0x%x (handle=%d handle_1=%d)\n\r",p_char->getProperties(), handle, handle_1); |
Wolfgang Betz |
130:770ce14d3d15 | 415 | break; |
Wolfgang Betz |
130:770ce14d3d15 | 416 | } else continue; |
Wolfgang Betz |
130:770ce14d3d15 | 417 | } |
Wolfgang Betz |
130:770ce14d3d15 | 418 | } |
Wolfgang Betz |
130:770ce14d3d15 | 419 | |
Wolfgang Betz |
130:770ce14d3d15 | 420 | return p_char; |
Wolfgang Betz |
130:770ce14d3d15 | 421 | } |
Wolfgang Betz |
130:770ce14d3d15 | 422 | |
Wolfgang Betz |
130:770ce14d3d15 | 423 | void BlueNRGGattServer::HCIDataWrittenEvent(const GattWriteCallbackParams *params) { |
Wolfgang Betz |
130:770ce14d3d15 | 424 | this->handleDataWrittenEvent(params); |
Wolfgang Betz |
130:770ce14d3d15 | 425 | } |
Wolfgang Betz |
130:770ce14d3d15 | 426 | |
Wolfgang Betz |
130:770ce14d3d15 | 427 | void BlueNRGGattServer::HCIDataReadEvent(const GattReadCallbackParams *params) { |
Wolfgang Betz |
132:51056160fa4a | 428 | PRINTF("Called HCIDataReadEvent\n\r"); |
Wolfgang Betz |
130:770ce14d3d15 | 429 | this->handleDataReadEvent(params); |
Wolfgang Betz |
130:770ce14d3d15 | 430 | } |
Wolfgang Betz |
130:770ce14d3d15 | 431 | |
Wolfgang Betz |
130:770ce14d3d15 | 432 | void BlueNRGGattServer::HCIEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) { |
Wolfgang Betz |
130:770ce14d3d15 | 433 | this->handleEvent(type, charHandle); |
Wolfgang Betz |
130:770ce14d3d15 | 434 | } |
Wolfgang Betz |
130:770ce14d3d15 | 435 | |
Wolfgang Betz |
130:770ce14d3d15 | 436 | void BlueNRGGattServer::HCIDataSentEvent(unsigned count) { |
Wolfgang Betz |
130:770ce14d3d15 | 437 | this->handleDataSentEvent(count); |
Wolfgang Betz |
130:770ce14d3d15 | 438 | } |
Wolfgang Betz |
130:770ce14d3d15 | 439 | |
Wolfgang Betz |
130:770ce14d3d15 | 440 | |
Wolfgang Betz |
130:770ce14d3d15 | 441 | ble_error_t BlueNRGGattServer::initializeGATTDatabase(void) { |
Wolfgang Betz |
130:770ce14d3d15 | 442 | // <TODO> |
Wolfgang Betz |
130:770ce14d3d15 | 443 | return (ble_error_t)0; |
Wolfgang Betz |
130:770ce14d3d15 | 444 | } |