BLE test
Fork of X_NUCLEO_IDB0XA1 by
source/platform/btle.cpp@267:cd7870e466b3, 2016-09-15 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Thu Sep 15 10:51:30 2016 +0100
- Branch:
- bde03b1e2c8e385819a23a5b6b31c97b4811d0a3
- Revision:
- 267:cd7870e466b3
- Parent:
- 266:b49e28134d83
- Child:
- 268:c0a1e03c5736
Sync with bde03b1e2c8e385819a23a5b6b31c97b4811d0a3
2016-07-12 11:31:21+01:00: Vincent Coubard
* Handle write authorization requests.
* Fix onDataWritten call (regression from master).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wolfgang Betz |
132:51056160fa4a | 1 | /* mbed Microcontroller Library |
Wolfgang Betz |
132:51056160fa4a | 2 | * Copyright (c) 2006-2013 ARM Limited |
Wolfgang Betz |
132:51056160fa4a | 3 | * |
Wolfgang Betz |
132:51056160fa4a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Wolfgang Betz |
132:51056160fa4a | 5 | * you may not use this file except in compliance with the License. |
Wolfgang Betz |
132:51056160fa4a | 6 | * You may obtain a copy of the License at |
Wolfgang Betz |
132:51056160fa4a | 7 | * |
Wolfgang Betz |
132:51056160fa4a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Wolfgang Betz |
132:51056160fa4a | 9 | * |
Wolfgang Betz |
132:51056160fa4a | 10 | * Unless required by applicable law or agreed to in writing, software |
Wolfgang Betz |
132:51056160fa4a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Wolfgang Betz |
132:51056160fa4a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Wolfgang Betz |
132:51056160fa4a | 13 | * See the License for the specific language governing permissions and |
Wolfgang Betz |
132:51056160fa4a | 14 | * limitations under the License. |
Wolfgang Betz |
132:51056160fa4a | 15 | */ |
Wolfgang Betz |
132:51056160fa4a | 16 | |
Wolfgang Betz |
132:51056160fa4a | 17 | |
Wolfgang Betz |
132:51056160fa4a | 18 | /** |
Wolfgang Betz |
132:51056160fa4a | 19 | ****************************************************************************** |
Vincent Coubard |
256:146bb175ce80 | 20 | * @file btle.cpp |
Wolfgang Betz |
132:51056160fa4a | 21 | * @author STMicroelectronics |
Wolfgang Betz |
132:51056160fa4a | 22 | * @brief Implementation BlueNRG Init and helper functions. |
Wolfgang Betz |
132:51056160fa4a | 23 | ****************************************************************************** |
Wolfgang Betz |
132:51056160fa4a | 24 | * @copy |
Wolfgang Betz |
132:51056160fa4a | 25 | * |
Wolfgang Betz |
132:51056160fa4a | 26 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
Wolfgang Betz |
132:51056160fa4a | 27 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
Wolfgang Betz |
132:51056160fa4a | 28 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
Wolfgang Betz |
132:51056160fa4a | 29 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
Wolfgang Betz |
132:51056160fa4a | 30 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
Wolfgang Betz |
132:51056160fa4a | 31 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
Wolfgang Betz |
132:51056160fa4a | 32 | * |
Wolfgang Betz |
132:51056160fa4a | 33 | * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2> |
Vincent Coubard |
256:146bb175ce80 | 34 | */ |
Wolfgang Betz |
132:51056160fa4a | 35 | |
Wolfgang Betz |
132:51056160fa4a | 36 | |
Wolfgang Betz |
132:51056160fa4a | 37 | #include "btle.h" |
Wolfgang Betz |
132:51056160fa4a | 38 | #include "ble/Gap.h" |
Wolfgang Betz |
132:51056160fa4a | 39 | #include "ble/GapEvents.h" |
Wolfgang Betz |
132:51056160fa4a | 40 | #include "BlueNRGGap.h" |
Wolfgang Betz |
132:51056160fa4a | 41 | #include "BlueNRGGattServer.h" |
Wolfgang Betz |
132:51056160fa4a | 42 | #include "BlueNRGGattClient.h" |
Wolfgang Betz |
132:51056160fa4a | 43 | #include "Utils.h" |
Wolfgang Betz |
132:51056160fa4a | 44 | |
Andrea Palmieri |
241:c13c2e31316d | 45 | #include "x_nucleo_idb0xa1_targets.h" |
Andrea Palmieri |
241:c13c2e31316d | 46 | |
Wolfgang Betz |
132:51056160fa4a | 47 | #ifdef __cplusplus |
Wolfgang Betz |
132:51056160fa4a | 48 | extern "C" { |
Wolfgang Betz |
132:51056160fa4a | 49 | #endif |
Wolfgang Betz |
132:51056160fa4a | 50 | |
Wolfgang Betz |
132:51056160fa4a | 51 | |
Wolfgang Betz |
132:51056160fa4a | 52 | /* C File Includes ------------------------------------------------------------------*/ |
Wolfgang Betz |
132:51056160fa4a | 53 | #include <stdio.h> |
Wolfgang Betz |
132:51056160fa4a | 54 | #include <string.h> |
Wolfgang Betz |
132:51056160fa4a | 55 | #include "hci.h" |
Wolfgang Betz |
132:51056160fa4a | 56 | #include "hci_const.h" |
Wolfgang Betz |
132:51056160fa4a | 57 | #include "bluenrg_aci.h" |
Wolfgang Betz |
132:51056160fa4a | 58 | #include "bluenrg_hal_aci.h" |
Wolfgang Betz |
132:51056160fa4a | 59 | #include "bluenrg_gap.h" |
Wolfgang Betz |
132:51056160fa4a | 60 | #include "bluenrg_utils.h" |
Wolfgang Betz |
132:51056160fa4a | 61 | |
Wolfgang Betz |
132:51056160fa4a | 62 | #include "hal_types.h" |
Wolfgang Betz |
132:51056160fa4a | 63 | #include "hal.h" |
Wolfgang Betz |
132:51056160fa4a | 64 | #include "gp_timer.h" |
Wolfgang Betz |
132:51056160fa4a | 65 | #include "osal.h" |
Wolfgang Betz |
132:51056160fa4a | 66 | #include "sm.h" |
Wolfgang Betz |
132:51056160fa4a | 67 | #include "debug.h" |
Wolfgang Betz |
132:51056160fa4a | 68 | |
Wolfgang Betz |
132:51056160fa4a | 69 | #ifdef __cplusplus |
Wolfgang Betz |
132:51056160fa4a | 70 | } |
Wolfgang Betz |
132:51056160fa4a | 71 | #endif |
Wolfgang Betz |
132:51056160fa4a | 72 | |
Wolfgang Betz |
132:51056160fa4a | 73 | #define IDB04A1 0 |
Wolfgang Betz |
132:51056160fa4a | 74 | #define IDB05A1 1 |
Wolfgang Betz |
132:51056160fa4a | 75 | |
Andrea Palmieri |
241:c13c2e31316d | 76 | /* See file 'x_nucleo_idb0xa1_targets.h' for details regarding the IDB0XA1 STACK_MODE */ |
Andrea Palmieri |
241:c13c2e31316d | 77 | #define STACK_MODE IDB0XA1_STACK_MODE |
Andrea Palmieri |
220:e02936f0d4c7 | 78 | |
Wolfgang Betz |
132:51056160fa4a | 79 | void HCI_Input(tHciDataPacket * hciReadPacket); |
Wolfgang Betz |
132:51056160fa4a | 80 | |
Wolfgang Betz |
132:51056160fa4a | 81 | uint16_t g_gap_service_handle = 0; |
Wolfgang Betz |
132:51056160fa4a | 82 | uint16_t g_appearance_char_handle = 0; |
Wolfgang Betz |
132:51056160fa4a | 83 | uint16_t g_device_name_char_handle = 0; |
Vincent Coubard |
266:b49e28134d83 | 84 | uint16_t g_preferred_connection_parameters_char_handle = 0; |
Wolfgang Betz |
132:51056160fa4a | 85 | |
Wolfgang Betz |
132:51056160fa4a | 86 | /* Private variables ---------------------------------------------------------*/ |
Wolfgang Betz |
132:51056160fa4a | 87 | volatile uint8_t set_connectable = 1; |
Andrea Palmieri |
219:d8e8cd4d4521 | 88 | |
Andrea Palmieri |
219:d8e8cd4d4521 | 89 | static char versionString[32]; |
Wolfgang Betz |
132:51056160fa4a | 90 | uint8_t bnrg_expansion_board = IDB04A1; /* at startup, suppose the X-NUCLEO-IDB04A1 is used */ |
Wolfgang Betz |
132:51056160fa4a | 91 | |
Wolfgang Betz |
132:51056160fa4a | 92 | /**************************************************************************/ |
Wolfgang Betz |
132:51056160fa4a | 93 | /*! |
Antonio Vilei |
245:6830f71c197c | 94 | @brief Init the BTLE stack with the specified role |
Wolfgang Betz |
132:51056160fa4a | 95 | @param isSetAddress boolean if address has been set |
Antonio Vilei |
245:6830f71c197c | 96 | @param role The device role |
Wolfgang Betz |
132:51056160fa4a | 97 | @returns void |
Wolfgang Betz |
132:51056160fa4a | 98 | */ |
Wolfgang Betz |
132:51056160fa4a | 99 | /**************************************************************************/ |
Andrea Palmieri |
219:d8e8cd4d4521 | 100 | void btleInit(bool isSetAddress, uint8_t role) |
Wolfgang Betz |
132:51056160fa4a | 101 | { |
Andrea Palmieri |
219:d8e8cd4d4521 | 102 | PRINTF("btleInit>>\n\r"); |
Andrea Palmieri |
229:9981f62cdb1a | 103 | /* Avoid compiler warnings about unused variables. */ |
Andrea Palmieri |
229:9981f62cdb1a | 104 | (void)isSetAddress; |
Vincent Coubard |
256:146bb175ce80 | 105 | |
Wolfgang Betz |
132:51056160fa4a | 106 | int ret; |
Wolfgang Betz |
132:51056160fa4a | 107 | uint8_t hwVersion; |
Wolfgang Betz |
132:51056160fa4a | 108 | uint16_t fwVersion; |
Wolfgang Betz |
132:51056160fa4a | 109 | uint16_t service_handle, dev_name_char_handle, appearance_char_handle; |
Wolfgang Betz |
132:51056160fa4a | 110 | |
Wolfgang Betz |
132:51056160fa4a | 111 | /* Reset BlueNRG SPI interface */ |
Wolfgang Betz |
132:51056160fa4a | 112 | BlueNRG_RST(); |
Wolfgang Betz |
132:51056160fa4a | 113 | |
Wolfgang Betz |
132:51056160fa4a | 114 | /* get the BlueNRG HW and FW versions */ |
Wolfgang Betz |
132:51056160fa4a | 115 | getBlueNRGVersion(&hwVersion, &fwVersion); |
Wolfgang Betz |
132:51056160fa4a | 116 | |
Vincent Coubard |
256:146bb175ce80 | 117 | /* |
Wolfgang Betz |
132:51056160fa4a | 118 | * Reset BlueNRG again otherwise we won't |
Wolfgang Betz |
132:51056160fa4a | 119 | * be able to change its MAC address. |
Wolfgang Betz |
132:51056160fa4a | 120 | * aci_hal_write_config_data() must be the first |
Wolfgang Betz |
132:51056160fa4a | 121 | * command after reset otherwise it will fail. |
Wolfgang Betz |
132:51056160fa4a | 122 | */ |
Wolfgang Betz |
132:51056160fa4a | 123 | BlueNRG_RST(); |
Wolfgang Betz |
132:51056160fa4a | 124 | |
Wolfgang Betz |
132:51056160fa4a | 125 | if (hwVersion > 0x30) { /* X-NUCLEO-IDB05A1 expansion board is used */ |
Wolfgang Betz |
132:51056160fa4a | 126 | bnrg_expansion_board = IDB05A1; |
Wolfgang Betz |
132:51056160fa4a | 127 | } |
Wolfgang Betz |
132:51056160fa4a | 128 | |
Andrea Palmieri |
219:d8e8cd4d4521 | 129 | /* set BLE version string */ |
Andrea Palmieri |
219:d8e8cd4d4521 | 130 | setVersionString(hwVersion, fwVersion); |
Andrea Palmieri |
219:d8e8cd4d4521 | 131 | |
Andrea Palmieri |
220:e02936f0d4c7 | 132 | if (bnrg_expansion_board == IDB05A1) { |
Andrea Palmieri |
220:e02936f0d4c7 | 133 | uint8_t stackMode = STACK_MODE; |
Andrea Palmieri |
220:e02936f0d4c7 | 134 | ret = aci_hal_write_config_data(CONFIG_DATA_ROLE, |
Andrea Palmieri |
220:e02936f0d4c7 | 135 | CONFIG_DATA_ROLE_LEN, |
Andrea Palmieri |
220:e02936f0d4c7 | 136 | &stackMode); |
Andrea Palmieri |
220:e02936f0d4c7 | 137 | } |
Andrea Palmieri |
220:e02936f0d4c7 | 138 | |
Wolfgang Betz |
132:51056160fa4a | 139 | /* The Nucleo board must be configured as SERVER */ |
Andrea Palmieri |
219:d8e8cd4d4521 | 140 | //check if isSetAddress is set then set address. |
Andrea Palmieri |
227:624ae7aa567d | 141 | #if 0 |
Wolfgang Betz |
132:51056160fa4a | 142 | if(isSetAddress) |
Wolfgang Betz |
132:51056160fa4a | 143 | { |
Andrea Palmieri |
221:7896e6d8b652 | 144 | Gap::Address_t bleAddr; |
Andrea Palmieri |
221:7896e6d8b652 | 145 | Gap::AddressType_t addr_type; |
Andrea Palmieri |
221:7896e6d8b652 | 146 | |
Wolfgang Betz |
132:51056160fa4a | 147 | BlueNRGGap::getInstance().getAddress(&addr_type, bleAddr); |
Andrea Palmieri |
227:624ae7aa567d | 148 | |
Wolfgang Betz |
132:51056160fa4a | 149 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
Wolfgang Betz |
132:51056160fa4a | 150 | CONFIG_DATA_PUBADDR_LEN, |
Andrea Palmieri |
221:7896e6d8b652 | 151 | bleAddr); |
Wolfgang Betz |
132:51056160fa4a | 152 | } else { |
Vincent Coubard |
256:146bb175ce80 | 153 | |
Wolfgang Betz |
140:0cd922dca83c | 154 | const Gap::Address_t BLE_address_BE = {0xFD,0x66,0x05,0x13,0xBE,0xBA}; |
Andrea Palmieri |
227:624ae7aa567d | 155 | BlueNRGGap::getInstance().setAddress(BLEProtocol::AddressType::RANDOM_STATIC, BLE_address_BE); |
Vincent Coubard |
256:146bb175ce80 | 156 | |
Wolfgang Betz |
132:51056160fa4a | 157 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
Wolfgang Betz |
132:51056160fa4a | 158 | CONFIG_DATA_PUBADDR_LEN, |
Wolfgang Betz |
132:51056160fa4a | 159 | BLE_address_BE); |
Wolfgang Betz |
132:51056160fa4a | 160 | } |
Andrea Palmieri |
227:624ae7aa567d | 161 | #endif |
Andrea Palmieri |
229:9981f62cdb1a | 162 | |
Andrea Palmieri |
229:9981f62cdb1a | 163 | const Gap::Address_t BLE_address_BE = {0xFD,0x66,0x05,0x13,0xBE,0xBA}; |
Andrea Palmieri |
229:9981f62cdb1a | 164 | BlueNRGGap::getInstance().setAddress(BLEProtocol::AddressType::RANDOM_STATIC, BLE_address_BE); |
Vincent Coubard |
256:146bb175ce80 | 165 | |
Wolfgang Betz |
132:51056160fa4a | 166 | ret = aci_gatt_init(); |
Vincent Coubard |
253:9665a6f8bbdb | 167 | if(ret != BLE_STATUS_SUCCESS){ |
Wolfgang Betz |
132:51056160fa4a | 168 | PRINTF("GATT_Init failed.\n"); |
Wolfgang Betz |
132:51056160fa4a | 169 | } |
Wolfgang Betz |
132:51056160fa4a | 170 | if (bnrg_expansion_board == IDB05A1) { |
Andrea Palmieri |
229:9981f62cdb1a | 171 | ret = aci_gap_init_IDB05A1(GAP_PERIPHERAL_ROLE_IDB05A1|GAP_CENTRAL_ROLE_IDB05A1|GAP_OBSERVER_ROLE_IDB05A1, |
Andrea Palmieri |
229:9981f62cdb1a | 172 | 0, |
Andrea Palmieri |
229:9981f62cdb1a | 173 | 0x18, |
Andrea Palmieri |
229:9981f62cdb1a | 174 | &service_handle, |
Andrea Palmieri |
229:9981f62cdb1a | 175 | &dev_name_char_handle, |
Andrea Palmieri |
229:9981f62cdb1a | 176 | &appearance_char_handle); |
Wolfgang Betz |
132:51056160fa4a | 177 | } else { |
Wolfgang Betz |
132:51056160fa4a | 178 | ret = aci_gap_init_IDB04A1(role, &service_handle, &dev_name_char_handle, &appearance_char_handle); |
Wolfgang Betz |
132:51056160fa4a | 179 | } |
Vincent Coubard |
256:146bb175ce80 | 180 | |
Wolfgang Betz |
132:51056160fa4a | 181 | if(ret != BLE_STATUS_SUCCESS){ |
Wolfgang Betz |
132:51056160fa4a | 182 | PRINTF("GAP_Init failed.\n"); |
Wolfgang Betz |
132:51056160fa4a | 183 | } |
Wolfgang Betz |
132:51056160fa4a | 184 | |
Andrea Palmieri |
229:9981f62cdb1a | 185 | //FIXME: Security and passkey set by default |
Wolfgang Betz |
132:51056160fa4a | 186 | ret = aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED, |
Wolfgang Betz |
132:51056160fa4a | 187 | OOB_AUTH_DATA_ABSENT, |
Wolfgang Betz |
132:51056160fa4a | 188 | NULL, |
Wolfgang Betz |
132:51056160fa4a | 189 | 7, |
Wolfgang Betz |
132:51056160fa4a | 190 | 16, |
Wolfgang Betz |
132:51056160fa4a | 191 | USE_FIXED_PIN_FOR_PAIRING, |
Wolfgang Betz |
132:51056160fa4a | 192 | 123456, |
Wolfgang Betz |
132:51056160fa4a | 193 | BONDING); |
Vincent Coubard |
253:9665a6f8bbdb | 194 | if (ret != BLE_STATUS_SUCCESS) { |
Vincent Coubard |
253:9665a6f8bbdb | 195 | PRINTF("Auth Req set failed.\n"); |
Wolfgang Betz |
132:51056160fa4a | 196 | } |
Vincent Coubard |
256:146bb175ce80 | 197 | |
Wolfgang Betz |
132:51056160fa4a | 198 | aci_hal_set_tx_power_level(1,4); |
Vincent Coubard |
256:146bb175ce80 | 199 | |
Wolfgang Betz |
132:51056160fa4a | 200 | g_gap_service_handle = service_handle; |
Wolfgang Betz |
132:51056160fa4a | 201 | g_appearance_char_handle = appearance_char_handle; |
Vincent Coubard |
256:146bb175ce80 | 202 | g_device_name_char_handle = dev_name_char_handle; |
Vincent Coubard |
256:146bb175ce80 | 203 | //Device Name is set from Accumulate Adv Data Payload or through setDeviceName API |
Wolfgang Betz |
132:51056160fa4a | 204 | /*ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, |
Wolfgang Betz |
132:51056160fa4a | 205 | strlen(name), (tHalUint8 *)name);*/ |
Wolfgang Betz |
132:51056160fa4a | 206 | |
Vincent Coubard |
266:b49e28134d83 | 207 | // update the peripheral preferred conenction parameters handle |
Vincent Coubard |
266:b49e28134d83 | 208 | // if the device is configured as a peripheral |
Vincent Coubard |
266:b49e28134d83 | 209 | // This value is hardcoded at the moment. |
Vincent Coubard |
266:b49e28134d83 | 210 | if ((role & GAP_PERIPHERAL_ROLE_IDB05A1) || (role & GAP_PERIPHERAL_ROLE_IDB04A1) || |
Vincent Coubard |
266:b49e28134d83 | 211 | (bnrg_expansion_board == IDB05A1 /* role is ignored in this configuration ... */)) { |
Vincent Coubard |
266:b49e28134d83 | 212 | g_preferred_connection_parameters_char_handle = 10; |
Vincent Coubard |
266:b49e28134d83 | 213 | } |
Vincent Coubard |
266:b49e28134d83 | 214 | |
Vincent Coubard |
266:b49e28134d83 | 215 | |
Wolfgang Betz |
138:32e3c4ca7a45 | 216 | #ifdef AST_FOR_MBED_OS |
Wolfgang Betz |
132:51056160fa4a | 217 | minar::Scheduler::postCallback(btle_handler); |
Wolfgang Betz |
132:51056160fa4a | 218 | #endif |
Wolfgang Betz |
132:51056160fa4a | 219 | return; |
Wolfgang Betz |
132:51056160fa4a | 220 | } |
Wolfgang Betz |
132:51056160fa4a | 221 | |
Wolfgang Betz |
132:51056160fa4a | 222 | /**************************************************************************/ |
Wolfgang Betz |
132:51056160fa4a | 223 | /*! |
Andrea Palmieri |
229:9981f62cdb1a | 224 | @brief mbedOS |
Wolfgang Betz |
132:51056160fa4a | 225 | |
Wolfgang Betz |
132:51056160fa4a | 226 | @param[in] void |
Vincent Coubard |
256:146bb175ce80 | 227 | |
Wolfgang Betz |
132:51056160fa4a | 228 | @returns |
Wolfgang Betz |
132:51056160fa4a | 229 | */ |
Wolfgang Betz |
132:51056160fa4a | 230 | /**************************************************************************/ |
Wolfgang Betz |
138:32e3c4ca7a45 | 231 | #ifdef AST_FOR_MBED_OS |
Wolfgang Betz |
134:d2f8d5898d04 | 232 | int btle_handler_pending = 0; |
Wolfgang Betz |
134:d2f8d5898d04 | 233 | |
Wolfgang Betz |
133:1bb8df697f7f | 234 | void btle_handler(void) |
Wolfgang Betz |
132:51056160fa4a | 235 | { |
Wolfgang Betz |
134:d2f8d5898d04 | 236 | btle_handler_pending = 0; |
Wolfgang Betz |
132:51056160fa4a | 237 | HCI_Process(); |
Wolfgang Betz |
132:51056160fa4a | 238 | } |
Wolfgang Betz |
132:51056160fa4a | 239 | #endif |
Wolfgang Betz |
132:51056160fa4a | 240 | |
Andrea Palmieri |
219:d8e8cd4d4521 | 241 | /* set BLE Version string */ |
Andrea Palmieri |
219:d8e8cd4d4521 | 242 | void setVersionString(uint8_t hwVersion, uint16_t fwVersion) |
Andrea Palmieri |
219:d8e8cd4d4521 | 243 | { |
Andrea Palmieri |
219:d8e8cd4d4521 | 244 | if(bnrg_expansion_board == IDB04A1 || bnrg_expansion_board == IDB05A1) { |
Andrea Palmieri |
219:d8e8cd4d4521 | 245 | snprintf(versionString, sizeof(versionString), "ST BLE4.1 HW v%u.%u FW v%u.%u", |
Andrea Palmieri |
219:d8e8cd4d4521 | 246 | hwVersion>>4, (hwVersion&0x0F), |
Andrea Palmieri |
219:d8e8cd4d4521 | 247 | fwVersion>>8, (fwVersion&0x00F0)>>4); |
Andrea Palmieri |
219:d8e8cd4d4521 | 248 | } else { |
Andrea Palmieri |
219:d8e8cd4d4521 | 249 | snprintf(versionString, sizeof(versionString), "ST (unknown spec)"); |
Andrea Palmieri |
219:d8e8cd4d4521 | 250 | } |
Andrea Palmieri |
219:d8e8cd4d4521 | 251 | } |
Andrea Palmieri |
219:d8e8cd4d4521 | 252 | |
Andrea Palmieri |
219:d8e8cd4d4521 | 253 | /* get BLE Version string */ |
Andrea Palmieri |
219:d8e8cd4d4521 | 254 | const char* getVersionString(void) |
Andrea Palmieri |
219:d8e8cd4d4521 | 255 | { |
Andrea Palmieri |
219:d8e8cd4d4521 | 256 | return versionString; |
Andrea Palmieri |
219:d8e8cd4d4521 | 257 | } |
Andrea Palmieri |
219:d8e8cd4d4521 | 258 | |
Andrea Palmieri |
219:d8e8cd4d4521 | 259 | tBleStatus btleStartRadioScan(uint8_t scan_type, |
Andrea Palmieri |
219:d8e8cd4d4521 | 260 | uint16_t scan_interval, |
Andrea Palmieri |
219:d8e8cd4d4521 | 261 | uint16_t scan_window, |
Andrea Palmieri |
219:d8e8cd4d4521 | 262 | uint8_t own_address_type) |
Andrea Palmieri |
219:d8e8cd4d4521 | 263 | { |
Andrea Palmieri |
219:d8e8cd4d4521 | 264 | tBleStatus ret; |
Andrea Palmieri |
219:d8e8cd4d4521 | 265 | |
Andrea Palmieri |
219:d8e8cd4d4521 | 266 | // Observer role is not supported by X-NUCLEO-IDB04A1, return BLE_ERROR_NOT_IMPLEMENTED |
Andrea Palmieri |
219:d8e8cd4d4521 | 267 | if(bnrg_expansion_board == IDB05A1) { |
Andrea Palmieri |
220:e02936f0d4c7 | 268 | PRINTF("scan_interval=%d scan_window=%d\n\r", scan_interval, scan_window); |
Andrea Palmieri |
231:a0d2544b38cd | 269 | PRINTF("scan_type=%d own_address_type=%d\n\r", scan_type, own_address_type); |
Andrea Palmieri |
219:d8e8cd4d4521 | 270 | ret = aci_gap_start_observation_procedure(scan_interval, |
Andrea Palmieri |
220:e02936f0d4c7 | 271 | scan_window, |
Andrea Palmieri |
219:d8e8cd4d4521 | 272 | scan_type, |
Andrea Palmieri |
220:e02936f0d4c7 | 273 | own_address_type, |
Andrea Palmieri |
235:31b976a40f43 | 274 | 0); // 1 to filter duplicates |
Andrea Palmieri |
219:d8e8cd4d4521 | 275 | } else { |
Andrea Palmieri |
219:d8e8cd4d4521 | 276 | ret = BLE_STATUS_INVALID_CID; |
Andrea Palmieri |
219:d8e8cd4d4521 | 277 | } |
Andrea Palmieri |
219:d8e8cd4d4521 | 278 | |
Andrea Palmieri |
219:d8e8cd4d4521 | 279 | return ret; |
Andrea Palmieri |
219:d8e8cd4d4521 | 280 | |
Andrea Palmieri |
219:d8e8cd4d4521 | 281 | } |
Wolfgang Betz |
132:51056160fa4a | 282 | |
Wolfgang Betz |
132:51056160fa4a | 283 | /*! |
Wolfgang Betz |
132:51056160fa4a | 284 | @brief Not Used |
Wolfgang Betz |
132:51056160fa4a | 285 | |
Wolfgang Betz |
132:51056160fa4a | 286 | @param[in] void |
Vincent Coubard |
256:146bb175ce80 | 287 | |
Wolfgang Betz |
132:51056160fa4a | 288 | @returns |
Wolfgang Betz |
132:51056160fa4a | 289 | */ |
Wolfgang Betz |
132:51056160fa4a | 290 | void SPI_Poll(void) |
Wolfgang Betz |
132:51056160fa4a | 291 | { |
Wolfgang Betz |
132:51056160fa4a | 292 | //HAL_GPIO_EXTI_Callback_Poll(BNRG_SPI_EXTI_PIN); |
Wolfgang Betz |
132:51056160fa4a | 293 | return; |
Wolfgang Betz |
132:51056160fa4a | 294 | } |
Vincent Coubard |
256:146bb175ce80 | 295 | |
Andrea Palmieri |
229:9981f62cdb1a | 296 | void Attribute_Modified_CB(evt_blue_aci *blue_evt) |
Andrea Palmieri |
229:9981f62cdb1a | 297 | { |
Andrea Palmieri |
229:9981f62cdb1a | 298 | uint16_t conn_handle; |
Andrea Palmieri |
229:9981f62cdb1a | 299 | uint16_t attr_handle; |
Andrea Palmieri |
229:9981f62cdb1a | 300 | uint8_t data_length; |
Andrea Palmieri |
229:9981f62cdb1a | 301 | uint8_t *att_data; |
Andrea Palmieri |
229:9981f62cdb1a | 302 | uint8_t offset; |
Andrea Palmieri |
229:9981f62cdb1a | 303 | |
Andrea Palmieri |
229:9981f62cdb1a | 304 | if (bnrg_expansion_board == IDB05A1) { |
Andrea Palmieri |
229:9981f62cdb1a | 305 | evt_gatt_attr_modified_IDB05A1 *evt = (evt_gatt_attr_modified_IDB05A1*)blue_evt->data; |
Andrea Palmieri |
229:9981f62cdb1a | 306 | conn_handle = evt->conn_handle; |
Andrea Palmieri |
229:9981f62cdb1a | 307 | attr_handle = evt->attr_handle; |
Andrea Palmieri |
229:9981f62cdb1a | 308 | data_length = evt->data_length; |
Andrea Palmieri |
229:9981f62cdb1a | 309 | att_data = evt->att_data; |
Andrea Palmieri |
229:9981f62cdb1a | 310 | offset = evt->offset; |
Andrea Palmieri |
229:9981f62cdb1a | 311 | } else { |
Andrea Palmieri |
229:9981f62cdb1a | 312 | evt_gatt_attr_modified_IDB04A1 *evt = (evt_gatt_attr_modified_IDB04A1*)blue_evt->data; |
Andrea Palmieri |
229:9981f62cdb1a | 313 | conn_handle = evt->conn_handle; |
Andrea Palmieri |
229:9981f62cdb1a | 314 | attr_handle = evt->attr_handle; |
Andrea Palmieri |
229:9981f62cdb1a | 315 | data_length = evt->data_length; |
Andrea Palmieri |
229:9981f62cdb1a | 316 | att_data = evt->att_data; |
Andrea Palmieri |
229:9981f62cdb1a | 317 | offset = 0; |
Andrea Palmieri |
229:9981f62cdb1a | 318 | } |
Andrea Palmieri |
229:9981f62cdb1a | 319 | |
Wolfgang Betz |
132:51056160fa4a | 320 | //Extract the GattCharacteristic from p_characteristics[] and find the properties mask |
Wolfgang Betz |
132:51056160fa4a | 321 | GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(attr_handle); |
Wolfgang Betz |
132:51056160fa4a | 322 | if(p_char!=NULL) { |
Andrea Palmieri |
229:9981f62cdb1a | 323 | GattAttribute::Handle_t charHandle = p_char->getValueAttribute().getHandle()-BlueNRGGattServer::CHAR_VALUE_HANDLE; |
Wolfgang Betz |
132:51056160fa4a | 324 | BlueNRGGattServer::HandleEnum_t currentHandle = BlueNRGGattServer::CHAR_HANDLE; |
Wolfgang Betz |
132:51056160fa4a | 325 | PRINTF("CharHandle %d, length: %d, Data: %d\n\r", charHandle, data_length, (uint16_t)att_data[0]); |
Wolfgang Betz |
132:51056160fa4a | 326 | PRINTF("getProperties 0x%x\n\r",p_char->getProperties()); |
Andrea Palmieri |
229:9981f62cdb1a | 327 | |
Andrea Palmieri |
229:9981f62cdb1a | 328 | if(attr_handle == charHandle+BlueNRGGattServer::CHAR_VALUE_HANDLE) { |
Wolfgang Betz |
132:51056160fa4a | 329 | currentHandle = BlueNRGGattServer::CHAR_VALUE_HANDLE; |
Wolfgang Betz |
132:51056160fa4a | 330 | } |
Wolfgang Betz |
132:51056160fa4a | 331 | |
Andrea Palmieri |
229:9981f62cdb1a | 332 | if(attr_handle == charHandle+BlueNRGGattServer::CHAR_DESC_HANDLE) { |
Wolfgang Betz |
132:51056160fa4a | 333 | currentHandle = BlueNRGGattServer::CHAR_DESC_HANDLE; |
Wolfgang Betz |
132:51056160fa4a | 334 | } |
Wolfgang Betz |
132:51056160fa4a | 335 | PRINTF("currentHandle %d\n\r", currentHandle); |
Vincent Coubard |
256:146bb175ce80 | 336 | if((p_char->getProperties() & |
Wolfgang Betz |
132:51056160fa4a | 337 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) && |
Wolfgang Betz |
132:51056160fa4a | 338 | currentHandle == BlueNRGGattServer::CHAR_DESC_HANDLE) { |
Wolfgang Betz |
132:51056160fa4a | 339 | |
Andrea Palmieri |
229:9981f62cdb1a | 340 | GattAttribute::Handle_t charDescHandle = p_char->getValueAttribute().getHandle()+1; |
Andrea Palmieri |
229:9981f62cdb1a | 341 | |
Wolfgang Betz |
132:51056160fa4a | 342 | PRINTF("*****NOTIFICATION CASE\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 343 | //Now Check if data written in Enable or Disable |
Wolfgang Betz |
132:51056160fa4a | 344 | if((uint16_t)att_data[0]==1) { |
Vincent Coubard |
256:146bb175ce80 | 345 | //PRINTF("Notify ENABLED\n\r"); |
Andrea Palmieri |
229:9981f62cdb1a | 346 | BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_ENABLED, charDescHandle); |
Wolfgang Betz |
132:51056160fa4a | 347 | } else { |
Vincent Coubard |
256:146bb175ce80 | 348 | //PRINTF("Notify DISABLED\n\r"); |
Andrea Palmieri |
229:9981f62cdb1a | 349 | BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_DISABLED, charDescHandle); |
Wolfgang Betz |
132:51056160fa4a | 350 | } |
Vincent Coubard |
263:8516afb5e29c | 351 | return; |
Wolfgang Betz |
132:51056160fa4a | 352 | } |
Vincent Coubard |
256:146bb175ce80 | 353 | |
Andrea Palmieri |
229:9981f62cdb1a | 354 | //Check if attr handle property is WRITEABLE, in the case generate GATT_EVENT_DATA_WRITTEN Event |
Wolfgang Betz |
132:51056160fa4a | 355 | if((p_char->getProperties() & |
Wolfgang Betz |
132:51056160fa4a | 356 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)) && |
Wolfgang Betz |
132:51056160fa4a | 357 | currentHandle == BlueNRGGattServer::CHAR_VALUE_HANDLE) { |
Vincent Coubard |
256:146bb175ce80 | 358 | |
Wolfgang Betz |
132:51056160fa4a | 359 | PRINTF("*****WRITE CASE\n\r"); |
Vincent Coubard |
256:146bb175ce80 | 360 | |
Wolfgang Betz |
132:51056160fa4a | 361 | GattWriteCallbackParams writeParams; |
Andrea Palmieri |
229:9981f62cdb1a | 362 | writeParams.connHandle = conn_handle; |
Wolfgang Betz |
132:51056160fa4a | 363 | writeParams.handle = p_char->getValueAttribute().getHandle(); |
Wolfgang Betz |
132:51056160fa4a | 364 | writeParams.writeOp = GattWriteCallbackParams::OP_WRITE_REQ;//Where to find this property in BLUENRG? |
Wolfgang Betz |
132:51056160fa4a | 365 | writeParams.len = data_length; |
Andrea Palmieri |
229:9981f62cdb1a | 366 | writeParams.data = att_data; |
Andrea Palmieri |
229:9981f62cdb1a | 367 | writeParams.offset = offset; |
Andrea Palmieri |
229:9981f62cdb1a | 368 | |
Andrea Palmieri |
229:9981f62cdb1a | 369 | //BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_WRITTEN, attr_handle); |
Wolfgang Betz |
132:51056160fa4a | 370 | //Write the actual Data to the Attr Handle? (uint8_1[])att_data contains the data |
Andrea Palmieri |
202:caf4864292c1 | 371 | if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getLength() > 0)) { |
Vincent Coubard |
267:cd7870e466b3 | 372 | BlueNRGGattServer::getInstance().write( |
Vincent Coubard |
267:cd7870e466b3 | 373 | p_char->getValueAttribute().getHandle(), |
Vincent Coubard |
267:cd7870e466b3 | 374 | (uint8_t*)att_data, |
Vincent Coubard |
267:cd7870e466b3 | 375 | data_length, |
Vincent Coubard |
267:cd7870e466b3 | 376 | false |
Vincent Coubard |
267:cd7870e466b3 | 377 | ); |
Vincent Coubard |
267:cd7870e466b3 | 378 | } |
Vincent Coubard |
263:8516afb5e29c | 379 | |
Vincent Coubard |
263:8516afb5e29c | 380 | BlueNRGGattServer::getInstance().HCIDataWrittenEvent(&writeParams); |
Vincent Coubard |
263:8516afb5e29c | 381 | } else { |
Vincent Coubard |
263:8516afb5e29c | 382 | PRINTF("*****WRITE DESCRIPTOR CASE\n\r"); |
Vincent Coubard |
263:8516afb5e29c | 383 | |
Vincent Coubard |
263:8516afb5e29c | 384 | GattWriteCallbackParams writeParams; |
Vincent Coubard |
263:8516afb5e29c | 385 | writeParams.connHandle = conn_handle; |
Vincent Coubard |
263:8516afb5e29c | 386 | writeParams.handle = attr_handle; |
Vincent Coubard |
263:8516afb5e29c | 387 | writeParams.writeOp = GattWriteCallbackParams::OP_WRITE_REQ;//Where to find this property in BLUENRG? |
Vincent Coubard |
263:8516afb5e29c | 388 | writeParams.len = data_length; |
Vincent Coubard |
263:8516afb5e29c | 389 | writeParams.data = att_data; |
Vincent Coubard |
263:8516afb5e29c | 390 | writeParams.offset = offset; |
Vincent Coubard |
263:8516afb5e29c | 391 | |
Vincent Coubard |
263:8516afb5e29c | 392 | BlueNRGGattServer::getInstance().HCIDataWrittenEvent(&writeParams); |
Vincent Coubard |
256:146bb175ce80 | 393 | } |
Wolfgang Betz |
132:51056160fa4a | 394 | } |
Wolfgang Betz |
132:51056160fa4a | 395 | |
Wolfgang Betz |
132:51056160fa4a | 396 | } |
Wolfgang Betz |
132:51056160fa4a | 397 | |
Wolfgang Betz |
132:51056160fa4a | 398 | #ifdef __cplusplus |
Wolfgang Betz |
132:51056160fa4a | 399 | extern "C" { |
Wolfgang Betz |
132:51056160fa4a | 400 | #endif |
Wolfgang Betz |
132:51056160fa4a | 401 | |
Wolfgang Betz |
132:51056160fa4a | 402 | /**************************************************************************/ |
Wolfgang Betz |
132:51056160fa4a | 403 | /*! |
Wolfgang Betz |
132:51056160fa4a | 404 | @brief Handle HCI Stack Event |
Wolfgang Betz |
132:51056160fa4a | 405 | |
Wolfgang Betz |
132:51056160fa4a | 406 | @param[in] pckt |
Wolfgang Betz |
132:51056160fa4a | 407 | Event Packet sent by the stack to be decoded |
Vincent Coubard |
256:146bb175ce80 | 408 | |
Wolfgang Betz |
132:51056160fa4a | 409 | @returns |
Wolfgang Betz |
132:51056160fa4a | 410 | */ |
Wolfgang Betz |
132:51056160fa4a | 411 | /**************************************************************************/ |
Wolfgang Betz |
132:51056160fa4a | 412 | extern void HCI_Event_CB(void *pckt) { |
Wolfgang Betz |
132:51056160fa4a | 413 | hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt; |
Wolfgang Betz |
132:51056160fa4a | 414 | hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data; |
Vincent Coubard |
256:146bb175ce80 | 415 | |
Wolfgang Betz |
132:51056160fa4a | 416 | if(hci_pckt->type != HCI_EVENT_PKT) |
Andrea Palmieri |
221:7896e6d8b652 | 417 | return; |
Wolfgang Betz |
132:51056160fa4a | 418 | |
Wolfgang Betz |
132:51056160fa4a | 419 | switch(event_pckt->evt){ |
Vincent Coubard |
256:146bb175ce80 | 420 | |
Wolfgang Betz |
132:51056160fa4a | 421 | case EVT_DISCONN_COMPLETE: |
Wolfgang Betz |
132:51056160fa4a | 422 | { |
Wolfgang Betz |
132:51056160fa4a | 423 | PRINTF("EVT_DISCONN_COMPLETE\n"); |
Vincent Coubard |
256:146bb175ce80 | 424 | |
Andrea Palmieri |
221:7896e6d8b652 | 425 | evt_disconn_complete *evt = (evt_disconn_complete*)event_pckt->data; |
Vincent Coubard |
256:146bb175ce80 | 426 | |
Andrea Palmieri |
221:7896e6d8b652 | 427 | BlueNRGGap::getInstance().processDisconnectionEvent(evt->handle, (Gap::DisconnectionReason_t)evt->reason); |
Wolfgang Betz |
132:51056160fa4a | 428 | } |
Wolfgang Betz |
132:51056160fa4a | 429 | break; |
Vincent Coubard |
256:146bb175ce80 | 430 | |
Wolfgang Betz |
132:51056160fa4a | 431 | case EVT_LE_META_EVENT: |
Wolfgang Betz |
132:51056160fa4a | 432 | { |
Wolfgang Betz |
132:51056160fa4a | 433 | PRINTF("EVT_LE_META_EVENT\n"); |
Vincent Coubard |
256:146bb175ce80 | 434 | |
Wolfgang Betz |
132:51056160fa4a | 435 | evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data; |
Vincent Coubard |
256:146bb175ce80 | 436 | |
Wolfgang Betz |
132:51056160fa4a | 437 | switch(evt->subevent){ |
Andrea Palmieri |
229:9981f62cdb1a | 438 | |
Wolfgang Betz |
132:51056160fa4a | 439 | case EVT_LE_CONN_COMPLETE: |
Vincent Coubard |
256:146bb175ce80 | 440 | { |
Wolfgang Betz |
132:51056160fa4a | 441 | PRINTF("EVT_LE_CONN_COMPLETE\n"); |
Andrea Palmieri |
221:7896e6d8b652 | 442 | Gap::Address_t ownAddr; |
Andrea Palmieri |
221:7896e6d8b652 | 443 | Gap::AddressType_t ownAddrType; |
Andrea Palmieri |
221:7896e6d8b652 | 444 | BlueNRGGap::getInstance().getAddress(&ownAddrType, ownAddr); |
Andrea Palmieri |
221:7896e6d8b652 | 445 | |
Andrea Palmieri |
227:624ae7aa567d | 446 | Gap::AddressType_t peerAddrType = BLEProtocol::AddressType::RANDOM_STATIC; |
Wolfgang Betz |
132:51056160fa4a | 447 | Gap::Role_t role; |
Vincent Coubard |
256:146bb175ce80 | 448 | |
Wolfgang Betz |
132:51056160fa4a | 449 | evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data; |
Vincent Coubard |
256:146bb175ce80 | 450 | |
Wolfgang Betz |
132:51056160fa4a | 451 | BlueNRGGap::getInstance().setConnectionHandle(cc->handle); |
Vincent Coubard |
256:146bb175ce80 | 452 | BlueNRGGap::ConnectionParams_t connectionParams = { |
Vincent Coubard |
256:146bb175ce80 | 453 | /* minConnectionInterval = */ cc->interval, |
Vincent Coubard |
256:146bb175ce80 | 454 | /* maxConnectionInterval = */ cc->interval, |
Vincent Coubard |
256:146bb175ce80 | 455 | /* slaveLatency = */ cc->latency, |
Vincent Coubard |
256:146bb175ce80 | 456 | /* connectionSupervisionTimeout = */ cc->supervision_timeout |
Vincent Coubard |
256:146bb175ce80 | 457 | }; |
Vincent Coubard |
256:146bb175ce80 | 458 | |
Vincent Coubard |
256:146bb175ce80 | 459 | BlueNRGGap::getInstance().setConnectionInterval(cc->interval); |
Vincent Coubard |
256:146bb175ce80 | 460 | |
Wolfgang Betz |
132:51056160fa4a | 461 | switch (cc->peer_bdaddr_type) { |
Wolfgang Betz |
132:51056160fa4a | 462 | case PUBLIC_ADDR: |
Andrea Palmieri |
210:08c4c85ca679 | 463 | peerAddrType = BLEProtocol::AddressType::PUBLIC; |
Wolfgang Betz |
132:51056160fa4a | 464 | break; |
Wolfgang Betz |
132:51056160fa4a | 465 | case STATIC_RANDOM_ADDR: |
Andrea Palmieri |
210:08c4c85ca679 | 466 | peerAddrType = BLEProtocol::AddressType::RANDOM_STATIC; |
Wolfgang Betz |
132:51056160fa4a | 467 | break; |
Wolfgang Betz |
132:51056160fa4a | 468 | case RESOLVABLE_PRIVATE_ADDR: |
Andrea Palmieri |
210:08c4c85ca679 | 469 | peerAddrType = BLEProtocol::AddressType::RANDOM_PRIVATE_RESOLVABLE; |
Wolfgang Betz |
132:51056160fa4a | 470 | break; |
Wolfgang Betz |
132:51056160fa4a | 471 | case NON_RESOLVABLE_PRIVATE_ADDR: |
Andrea Palmieri |
210:08c4c85ca679 | 472 | peerAddrType = BLEProtocol::AddressType::RANDOM_PRIVATE_NON_RESOLVABLE; |
Wolfgang Betz |
132:51056160fa4a | 473 | break; |
Vincent Coubard |
256:146bb175ce80 | 474 | } |
Wolfgang Betz |
132:51056160fa4a | 475 | //PRINTF("EVT_LE_CONN_COMPLETE LL role=%d\n", cc->role); |
Wolfgang Betz |
132:51056160fa4a | 476 | switch (cc->role) { |
Wolfgang Betz |
133:1bb8df697f7f | 477 | case 0: //master |
Wolfgang Betz |
132:51056160fa4a | 478 | role = Gap::CENTRAL; |
Wolfgang Betz |
132:51056160fa4a | 479 | break; |
Wolfgang Betz |
133:1bb8df697f7f | 480 | case 1: |
Wolfgang Betz |
132:51056160fa4a | 481 | role = Gap::PERIPHERAL; |
Wolfgang Betz |
132:51056160fa4a | 482 | break; |
Wolfgang Betz |
133:1bb8df697f7f | 483 | default: |
Wolfgang Betz |
139:3a75965fd389 | 484 | role = Gap::PERIPHERAL; |
Wolfgang Betz |
133:1bb8df697f7f | 485 | break; |
Wolfgang Betz |
132:51056160fa4a | 486 | } |
Wolfgang Betz |
132:51056160fa4a | 487 | //PRINTF("EVT_LE_CONN_COMPLETE GAP role=%d\n", role); |
Andrea Palmieri |
221:7896e6d8b652 | 488 | BlueNRGGap::getInstance().processConnectionEvent(cc->handle, |
Andrea Palmieri |
221:7896e6d8b652 | 489 | role, |
Andrea Palmieri |
221:7896e6d8b652 | 490 | peerAddrType, |
Andrea Palmieri |
221:7896e6d8b652 | 491 | cc->peer_bdaddr, |
Andrea Palmieri |
221:7896e6d8b652 | 492 | ownAddrType, |
Andrea Palmieri |
221:7896e6d8b652 | 493 | ownAddr, |
Andrea Palmieri |
221:7896e6d8b652 | 494 | &connectionParams); |
Wolfgang Betz |
132:51056160fa4a | 495 | } |
Wolfgang Betz |
132:51056160fa4a | 496 | break; |
Vincent Coubard |
256:146bb175ce80 | 497 | |
Wolfgang Betz |
132:51056160fa4a | 498 | case EVT_LE_ADVERTISING_REPORT: |
Wolfgang Betz |
132:51056160fa4a | 499 | PRINTF("EVT_LE_ADVERTISING_REPORT\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 500 | /* FIXME: comment this otherwise it will be obscure and error prone if BlueNRG FW will be updated */ |
Wolfgang Betz |
132:51056160fa4a | 501 | // This event is generated only by X-NUCLEO-IDB05A1 version but not by X-NUCLEO-IDB04A1 (which generates DEVICE_FOUND EVT) |
Wolfgang Betz |
132:51056160fa4a | 502 | // Formally the structure related to both events are identical except that for the ADV REPORT |
Wolfgang Betz |
132:51056160fa4a | 503 | // there is one more field (number of reports) which is not forwarded to upper layer. |
Vincent Coubard |
256:146bb175ce80 | 504 | // Thus we need to move one byte over (((uint8_t*)evt->data)+1) before persing the ADV REPORT. |
Wolfgang Betz |
132:51056160fa4a | 505 | le_advertising_info *pr = (le_advertising_info*) (((uint8_t*)evt->data)+1); |
Wolfgang Betz |
132:51056160fa4a | 506 | PRINTF("EVT_LE_ADVERTISING_REPORT evt_type=%d\n\r", pr->evt_type); |
Vincent Coubard |
256:146bb175ce80 | 507 | |
Wolfgang Betz |
132:51056160fa4a | 508 | BlueNRGGap::getInstance().Discovery_CB(BlueNRGGap::DEVICE_FOUND, |
Wolfgang Betz |
132:51056160fa4a | 509 | pr->evt_type, |
Andrea Palmieri |
229:9981f62cdb1a | 510 | pr->bdaddr_type, |
Wolfgang Betz |
132:51056160fa4a | 511 | pr->bdaddr, |
Wolfgang Betz |
132:51056160fa4a | 512 | &pr->data_length, |
Wolfgang Betz |
132:51056160fa4a | 513 | &pr->data_RSSI[0], |
Wolfgang Betz |
132:51056160fa4a | 514 | &pr->data_RSSI[pr->data_length]); |
Wolfgang Betz |
132:51056160fa4a | 515 | break; |
Wolfgang Betz |
132:51056160fa4a | 516 | } |
Wolfgang Betz |
132:51056160fa4a | 517 | } |
Wolfgang Betz |
132:51056160fa4a | 518 | break; |
Vincent Coubard |
256:146bb175ce80 | 519 | |
Wolfgang Betz |
132:51056160fa4a | 520 | case EVT_VENDOR: |
Vincent Coubard |
256:146bb175ce80 | 521 | { |
Wolfgang Betz |
132:51056160fa4a | 522 | evt_blue_aci *blue_evt = (evt_blue_aci*)event_pckt->data; |
Wolfgang Betz |
132:51056160fa4a | 523 | //PRINTF("EVT_VENDOR %d\n", blue_evt->ecode); |
Vincent Coubard |
256:146bb175ce80 | 524 | |
Wolfgang Betz |
132:51056160fa4a | 525 | switch(blue_evt->ecode){ |
Vincent Coubard |
256:146bb175ce80 | 526 | |
Vincent Coubard |
267:cd7870e466b3 | 527 | case EVT_BLUE_GATT_WRITE_PERMIT_REQ: |
Vincent Coubard |
267:cd7870e466b3 | 528 | { |
Vincent Coubard |
267:cd7870e466b3 | 529 | PRINTF("EVT_BLUE_GATT_WRITE_PERMIT_REQ\r\n"); |
Vincent Coubard |
267:cd7870e466b3 | 530 | evt_gatt_write_permit_req* write_req = (evt_gatt_write_permit_req*)blue_evt->data; |
Vincent Coubard |
267:cd7870e466b3 | 531 | |
Vincent Coubard |
267:cd7870e466b3 | 532 | // ask the local server if the write operation is authorized |
Vincent Coubard |
267:cd7870e466b3 | 533 | uint8_t err_code = BlueNRGGattServer::getInstance().Write_Request_CB( |
Vincent Coubard |
267:cd7870e466b3 | 534 | write_req->conn_handle, |
Vincent Coubard |
267:cd7870e466b3 | 535 | write_req->attr_handle, |
Vincent Coubard |
267:cd7870e466b3 | 536 | write_req->data_length, |
Vincent Coubard |
267:cd7870e466b3 | 537 | write_req->data |
Vincent Coubard |
267:cd7870e466b3 | 538 | ); |
Vincent Coubard |
267:cd7870e466b3 | 539 | uint8_t write_status = err_code == 0 ? 0 : 1; |
Vincent Coubard |
267:cd7870e466b3 | 540 | |
Vincent Coubard |
267:cd7870e466b3 | 541 | // reply to the shield |
Vincent Coubard |
267:cd7870e466b3 | 542 | tBleStatus err = aci_gatt_write_response( |
Vincent Coubard |
267:cd7870e466b3 | 543 | write_req->conn_handle, |
Vincent Coubard |
267:cd7870e466b3 | 544 | write_req->attr_handle, |
Vincent Coubard |
267:cd7870e466b3 | 545 | write_status, |
Vincent Coubard |
267:cd7870e466b3 | 546 | err_code, |
Vincent Coubard |
267:cd7870e466b3 | 547 | write_req->data_length, |
Vincent Coubard |
267:cd7870e466b3 | 548 | write_req->data |
Vincent Coubard |
267:cd7870e466b3 | 549 | ); |
Vincent Coubard |
267:cd7870e466b3 | 550 | } |
Vincent Coubard |
267:cd7870e466b3 | 551 | break; |
Vincent Coubard |
257:90b0bb40fb45 | 552 | |
Wolfgang Betz |
132:51056160fa4a | 553 | case EVT_BLUE_GATT_READ_PERMIT_REQ: |
Wolfgang Betz |
132:51056160fa4a | 554 | { |
Wolfgang Betz |
132:51056160fa4a | 555 | PRINTF("EVT_BLUE_GATT_READ_PERMIT_REQ_OK\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 556 | evt_gatt_read_permit_req *pr = (evt_gatt_read_permit_req*)blue_evt->data; |
Andrea Palmieri |
229:9981f62cdb1a | 557 | PRINTF("EVT_BLUE_GATT_READ_PERMIT_REQ_OK pr->attr_handle=%u\n\r", pr->attr_handle); |
Andrea Palmieri |
229:9981f62cdb1a | 558 | BlueNRGGattServer::getInstance().Read_Request_CB(pr->attr_handle); |
Wolfgang Betz |
132:51056160fa4a | 559 | } |
Wolfgang Betz |
132:51056160fa4a | 560 | break; |
Vincent Coubard |
256:146bb175ce80 | 561 | |
Vincent Coubard |
256:146bb175ce80 | 562 | case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED: |
Wolfgang Betz |
132:51056160fa4a | 563 | { |
Wolfgang Betz |
132:51056160fa4a | 564 | PRINTF("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 565 | /* this callback is invoked when a GATT attribute is modified |
Wolfgang Betz |
132:51056160fa4a | 566 | extract callback data and pass to suitable handler function */ |
Andrea Palmieri |
229:9981f62cdb1a | 567 | Attribute_Modified_CB(blue_evt); |
Wolfgang Betz |
132:51056160fa4a | 568 | } |
Vincent Coubard |
256:146bb175ce80 | 569 | break; |
Vincent Coubard |
256:146bb175ce80 | 570 | |
Wolfgang Betz |
132:51056160fa4a | 571 | //Any cases for Data Sent Notifications? |
Wolfgang Betz |
132:51056160fa4a | 572 | case EVT_BLUE_GATT_NOTIFICATION: |
Wolfgang Betz |
132:51056160fa4a | 573 | //This is only relevant for Client Side Event |
Wolfgang Betz |
132:51056160fa4a | 574 | PRINTF("EVT_BLUE_GATT_NOTIFICATION"); |
Wolfgang Betz |
132:51056160fa4a | 575 | break; |
Wolfgang Betz |
132:51056160fa4a | 576 | case EVT_BLUE_GATT_INDICATION: |
Wolfgang Betz |
132:51056160fa4a | 577 | //This is only relevant for Client Side Event |
Wolfgang Betz |
132:51056160fa4a | 578 | PRINTF("EVT_BLUE_GATT_INDICATION"); |
Vincent Coubard |
256:146bb175ce80 | 579 | break; |
Vincent Coubard |
256:146bb175ce80 | 580 | |
Wolfgang Betz |
132:51056160fa4a | 581 | case EVT_BLUE_ATT_READ_BY_GROUP_TYPE_RESP: |
Wolfgang Betz |
132:51056160fa4a | 582 | { |
Wolfgang Betz |
132:51056160fa4a | 583 | PRINTF("EVT_BLUE_ATT_READ_BY_GROUP_TYPE_RESP\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 584 | evt_att_read_by_group_resp *pr = (evt_att_read_by_group_resp*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 585 | BlueNRGGattClient::getInstance().primaryServicesCB(pr->conn_handle, |
Wolfgang Betz |
132:51056160fa4a | 586 | pr->event_data_length, |
Wolfgang Betz |
132:51056160fa4a | 587 | pr->attribute_data_length, |
Wolfgang Betz |
132:51056160fa4a | 588 | pr->attribute_data_list); |
Wolfgang Betz |
132:51056160fa4a | 589 | } |
Wolfgang Betz |
132:51056160fa4a | 590 | break; |
Wolfgang Betz |
132:51056160fa4a | 591 | case EVT_BLUE_ATT_READ_BY_TYPE_RESP: |
Wolfgang Betz |
132:51056160fa4a | 592 | { |
Wolfgang Betz |
132:51056160fa4a | 593 | PRINTF("EVT_BLUE_ATT_READ_BY_TYPE_RESP\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 594 | evt_att_read_by_type_resp *pr = (evt_att_read_by_type_resp*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 595 | BlueNRGGattClient::getInstance().serviceCharsCB(pr->conn_handle, |
Wolfgang Betz |
132:51056160fa4a | 596 | pr->event_data_length, |
Wolfgang Betz |
132:51056160fa4a | 597 | pr->handle_value_pair_length, |
Wolfgang Betz |
132:51056160fa4a | 598 | pr->handle_value_pair); |
Wolfgang Betz |
132:51056160fa4a | 599 | } |
Wolfgang Betz |
132:51056160fa4a | 600 | break; |
Wolfgang Betz |
132:51056160fa4a | 601 | case EVT_BLUE_ATT_READ_RESP: |
Wolfgang Betz |
132:51056160fa4a | 602 | { |
Wolfgang Betz |
132:51056160fa4a | 603 | PRINTF("EVT_BLUE_ATT_READ_RESP\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 604 | evt_att_read_resp *pr = (evt_att_read_resp*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 605 | BlueNRGGattClient::getInstance().charReadCB(pr->conn_handle, |
Wolfgang Betz |
132:51056160fa4a | 606 | pr->event_data_length, |
Wolfgang Betz |
132:51056160fa4a | 607 | pr->attribute_value); |
Wolfgang Betz |
132:51056160fa4a | 608 | } |
Wolfgang Betz |
132:51056160fa4a | 609 | break; |
Wolfgang Betz |
132:51056160fa4a | 610 | case EVT_BLUE_ATT_EXEC_WRITE_RESP: |
Wolfgang Betz |
132:51056160fa4a | 611 | { |
Wolfgang Betz |
132:51056160fa4a | 612 | PRINTF("EVT_BLUE_ATT_EXEC_WRITE_RESP\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 613 | evt_att_prepare_write_resp *pr = (evt_att_prepare_write_resp*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 614 | BlueNRGGattClient::getInstance().charWriteExecCB(pr->conn_handle, |
Wolfgang Betz |
132:51056160fa4a | 615 | pr->event_data_length); |
Wolfgang Betz |
132:51056160fa4a | 616 | } |
Wolfgang Betz |
132:51056160fa4a | 617 | break; |
Wolfgang Betz |
132:51056160fa4a | 618 | case EVT_BLUE_ATT_PREPARE_WRITE_RESP: |
Wolfgang Betz |
132:51056160fa4a | 619 | { |
Wolfgang Betz |
132:51056160fa4a | 620 | PRINTF("EVT_BLUE_ATT_PREPARE_WRITE_RESP\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 621 | evt_att_prepare_write_resp *pr = (evt_att_prepare_write_resp*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 622 | BlueNRGGattClient::getInstance().charWritePrepareCB(pr->conn_handle, |
Wolfgang Betz |
132:51056160fa4a | 623 | pr->event_data_length, |
Wolfgang Betz |
132:51056160fa4a | 624 | pr->attribute_handle, |
Wolfgang Betz |
132:51056160fa4a | 625 | pr->offset, |
Wolfgang Betz |
132:51056160fa4a | 626 | pr->part_attr_value); |
Wolfgang Betz |
132:51056160fa4a | 627 | } |
Wolfgang Betz |
132:51056160fa4a | 628 | break; |
Wolfgang Betz |
132:51056160fa4a | 629 | case EVT_BLUE_GATT_DISC_READ_CHAR_BY_UUID_RESP: |
Wolfgang Betz |
132:51056160fa4a | 630 | { |
Wolfgang Betz |
132:51056160fa4a | 631 | PRINTF("EVT_BLUE_GATT_DISC_READ_CHAR_BY_UUID_RESP\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 632 | evt_gatt_disc_read_char_by_uuid_resp *pr = (evt_gatt_disc_read_char_by_uuid_resp*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 633 | BlueNRGGattClient::getInstance().serviceCharByUUIDCB(pr->conn_handle, |
Wolfgang Betz |
132:51056160fa4a | 634 | pr->event_data_length, |
Wolfgang Betz |
132:51056160fa4a | 635 | pr->attr_handle, |
Wolfgang Betz |
132:51056160fa4a | 636 | pr->attr_value); |
Wolfgang Betz |
132:51056160fa4a | 637 | } |
Wolfgang Betz |
132:51056160fa4a | 638 | break; |
Wolfgang Betz |
132:51056160fa4a | 639 | case EVT_BLUE_ATT_FIND_BY_TYPE_VAL_RESP: |
Wolfgang Betz |
132:51056160fa4a | 640 | { |
Wolfgang Betz |
132:51056160fa4a | 641 | PRINTF("EVT_BLUE_ATT_FIND_BY_TYPE_VAL_RESP\n\r"); |
Wolfgang Betz |
132:51056160fa4a | 642 | evt_att_find_by_type_val_resp *pr = (evt_att_find_by_type_val_resp*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 643 | BlueNRGGattClient::getInstance().primaryServiceCB(pr->conn_handle, |
Wolfgang Betz |
132:51056160fa4a | 644 | pr->event_data_length, |
Wolfgang Betz |
132:51056160fa4a | 645 | pr->handles_info_list); |
Wolfgang Betz |
132:51056160fa4a | 646 | } |
Wolfgang Betz |
132:51056160fa4a | 647 | break; |
Andrea Palmieri |
229:9981f62cdb1a | 648 | case EVT_BLUE_ATT_FIND_INFORMATION_RESP: |
Andrea Palmieri |
229:9981f62cdb1a | 649 | { |
Andrea Palmieri |
229:9981f62cdb1a | 650 | PRINTF("EVT_BLUE_ATT_FIND_INFORMATION_RESP\n\r"); |
Andrea Palmieri |
229:9981f62cdb1a | 651 | evt_att_find_information_resp *pr = (evt_att_find_information_resp*)blue_evt->data; |
Andrea Palmieri |
229:9981f62cdb1a | 652 | BlueNRGGattClient::getInstance().discAllCharacDescCB(pr->conn_handle, |
Andrea Palmieri |
229:9981f62cdb1a | 653 | pr->event_data_length, |
Andrea Palmieri |
229:9981f62cdb1a | 654 | pr->format, |
Andrea Palmieri |
229:9981f62cdb1a | 655 | pr->handle_uuid_pair); |
Andrea Palmieri |
229:9981f62cdb1a | 656 | } |
Andrea Palmieri |
229:9981f62cdb1a | 657 | break; |
Andrea Palmieri |
229:9981f62cdb1a | 658 | case EVT_BLUE_GATT_PROCEDURE_COMPLETE: |
Wolfgang Betz |
132:51056160fa4a | 659 | { |
Wolfgang Betz |
132:51056160fa4a | 660 | evt_gatt_procedure_complete *evt = (evt_gatt_procedure_complete*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 661 | PRINTF("EVT_BLUE_GATT_PROCEDURE_COMPLETE error_code=%d\n\r", evt->error_code); |
Wolfgang Betz |
132:51056160fa4a | 662 | BlueNRGGattClient::getInstance().gattProcedureCompleteCB(evt->conn_handle, evt->error_code); |
Wolfgang Betz |
132:51056160fa4a | 663 | } |
Wolfgang Betz |
132:51056160fa4a | 664 | break; |
Vincent Coubard |
256:146bb175ce80 | 665 | |
Wolfgang Betz |
132:51056160fa4a | 666 | case EVT_BLUE_GAP_DEVICE_FOUND: |
Wolfgang Betz |
132:51056160fa4a | 667 | { |
Wolfgang Betz |
132:51056160fa4a | 668 | evt_gap_device_found *pr = (evt_gap_device_found*)blue_evt->data; |
Wolfgang Betz |
132:51056160fa4a | 669 | PRINTF("EVT_BLUE_GAP_DEVICE_FOUND evt_type=%d\n\r", pr->evt_type); |
Vincent Coubard |
256:146bb175ce80 | 670 | |
Wolfgang Betz |
132:51056160fa4a | 671 | BlueNRGGap::getInstance().Discovery_CB(BlueNRGGap::DEVICE_FOUND, |
Wolfgang Betz |
132:51056160fa4a | 672 | pr->evt_type, |
Andrea Palmieri |
229:9981f62cdb1a | 673 | pr->bdaddr_type, |
Wolfgang Betz |
132:51056160fa4a | 674 | pr->bdaddr, |
Wolfgang Betz |
132:51056160fa4a | 675 | &pr->data_length, |
Wolfgang Betz |
132:51056160fa4a | 676 | &pr->data_RSSI[0], |
Wolfgang Betz |
132:51056160fa4a | 677 | &pr->data_RSSI[pr->data_length]); |
Wolfgang Betz |
132:51056160fa4a | 678 | } |
Wolfgang Betz |
132:51056160fa4a | 679 | break; |
Vincent Coubard |
256:146bb175ce80 | 680 | |
Wolfgang Betz |
132:51056160fa4a | 681 | case EVT_BLUE_GAP_PROCEDURE_COMPLETE: |
Wolfgang Betz |
132:51056160fa4a | 682 | { |
Wolfgang Betz |
132:51056160fa4a | 683 | evt_gap_procedure_complete *pr = (evt_gap_procedure_complete*)blue_evt->data; |
Andrea Palmieri |
229:9981f62cdb1a | 684 | //PRINTF("EVT_BLUE_GAP_PROCEDURE_COMPLETE (code=0x%02X)\n\r", pr->procedure_code); |
Vincent Coubard |
256:146bb175ce80 | 685 | |
Wolfgang Betz |
132:51056160fa4a | 686 | switch(pr->procedure_code) { |
Andrea Palmieri |
219:d8e8cd4d4521 | 687 | case GAP_OBSERVATION_PROC_IDB05A1: |
Vincent Coubard |
256:146bb175ce80 | 688 | |
Andrea Palmieri |
229:9981f62cdb1a | 689 | BlueNRGGap::getInstance().Discovery_CB(BlueNRGGap::DISCOVERY_COMPLETE, 0, 0, NULL, NULL, NULL, NULL); |
Wolfgang Betz |
132:51056160fa4a | 690 | break; |
Wolfgang Betz |
132:51056160fa4a | 691 | } |
Wolfgang Betz |
132:51056160fa4a | 692 | } |
Vincent Coubard |
256:146bb175ce80 | 693 | break; |
Wolfgang Betz |
132:51056160fa4a | 694 | } |
Wolfgang Betz |
132:51056160fa4a | 695 | } |
Wolfgang Betz |
132:51056160fa4a | 696 | break; |
Vincent Coubard |
256:146bb175ce80 | 697 | } |
Wolfgang Betz |
132:51056160fa4a | 698 | return ; |
Wolfgang Betz |
132:51056160fa4a | 699 | } |
Wolfgang Betz |
132:51056160fa4a | 700 | |
Wolfgang Betz |
132:51056160fa4a | 701 | |
Wolfgang Betz |
132:51056160fa4a | 702 | #ifdef __cplusplus |
Wolfgang Betz |
132:51056160fa4a | 703 | } |
Vincent Coubard |
256:146bb175ce80 | 704 | #endif |