HW layer for the Nucleo board, it only work with old BLE_API
Dependents: Hello_BLE F446RE-BLE
Fork of X_NUCLEO_IDB0XA1 by
platform/src/btle.cpp@126:32039585a969, 2015-09-29 (annotated)
- Committer:
- apalmieri
- Date:
- Tue Sep 29 12:39:31 2015 +0000
- Revision:
- 126:32039585a969
- Parent:
- 125:2e468f07a94f
Detect BlueNRG expansion board (X-NUCLEO-IDB04A1 or X-NUCLEO-IDB05A1) at runtime
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wolfgang Betz |
90:26c0c9807ab4 | 1 | /* mbed Microcontroller Library |
Wolfgang Betz |
90:26c0c9807ab4 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Wolfgang Betz |
90:26c0c9807ab4 | 3 | * |
Wolfgang Betz |
90:26c0c9807ab4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Wolfgang Betz |
90:26c0c9807ab4 | 5 | * you may not use this file except in compliance with the License. |
Wolfgang Betz |
90:26c0c9807ab4 | 6 | * You may obtain a copy of the License at |
Wolfgang Betz |
90:26c0c9807ab4 | 7 | * |
Wolfgang Betz |
90:26c0c9807ab4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Wolfgang Betz |
90:26c0c9807ab4 | 9 | * |
Wolfgang Betz |
90:26c0c9807ab4 | 10 | * Unless required by applicable law or agreed to in writing, software |
Wolfgang Betz |
90:26c0c9807ab4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Wolfgang Betz |
90:26c0c9807ab4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Wolfgang Betz |
90:26c0c9807ab4 | 13 | * See the License for the specific language governing permissions and |
Wolfgang Betz |
90:26c0c9807ab4 | 14 | * limitations under the License. |
Wolfgang Betz |
90:26c0c9807ab4 | 15 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 16 | |
Wolfgang Betz |
90:26c0c9807ab4 | 17 | |
Wolfgang Betz |
90:26c0c9807ab4 | 18 | /** |
Wolfgang Betz |
90:26c0c9807ab4 | 19 | ****************************************************************************** |
Wolfgang Betz |
90:26c0c9807ab4 | 20 | * @file btle.cpp |
Wolfgang Betz |
90:26c0c9807ab4 | 21 | * @author STMicroelectronics |
Wolfgang Betz |
90:26c0c9807ab4 | 22 | * @brief Implementation BlueNRG Init and helper functions. |
Wolfgang Betz |
90:26c0c9807ab4 | 23 | ****************************************************************************** |
Wolfgang Betz |
90:26c0c9807ab4 | 24 | * @copy |
Wolfgang Betz |
90:26c0c9807ab4 | 25 | * |
Wolfgang Betz |
90:26c0c9807ab4 | 26 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
Wolfgang Betz |
90:26c0c9807ab4 | 27 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
Wolfgang Betz |
90:26c0c9807ab4 | 28 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY |
Wolfgang Betz |
90:26c0c9807ab4 | 29 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
Wolfgang Betz |
90:26c0c9807ab4 | 30 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
Wolfgang Betz |
90:26c0c9807ab4 | 31 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
Wolfgang Betz |
90:26c0c9807ab4 | 32 | * |
Wolfgang Betz |
90:26c0c9807ab4 | 33 | * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2> |
Wolfgang Betz |
90:26c0c9807ab4 | 34 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 35 | |
Wolfgang Betz |
90:26c0c9807ab4 | 36 | |
Wolfgang Betz |
90:26c0c9807ab4 | 37 | #include "btle.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 38 | #include "ble/Gap.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 39 | #include "ble/GapEvents.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 40 | #include "BlueNRGGap.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 41 | #include "BlueNRGGattServer.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 42 | #include "Utils.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 43 | |
Wolfgang Betz |
90:26c0c9807ab4 | 44 | #ifdef __cplusplus |
Wolfgang Betz |
90:26c0c9807ab4 | 45 | extern "C" { |
Wolfgang Betz |
90:26c0c9807ab4 | 46 | #endif |
Wolfgang Betz |
90:26c0c9807ab4 | 47 | |
Wolfgang Betz |
90:26c0c9807ab4 | 48 | |
Wolfgang Betz |
90:26c0c9807ab4 | 49 | /* C File Includes ------------------------------------------------------------------*/ |
Wolfgang Betz |
90:26c0c9807ab4 | 50 | // ANDREA: Updated includes and changed some types (e.g., tHalUint8 --> uint8_t) |
Wolfgang Betz |
90:26c0c9807ab4 | 51 | #include <stdio.h> |
Wolfgang Betz |
90:26c0c9807ab4 | 52 | #include <string.h> |
Wolfgang Betz |
90:26c0c9807ab4 | 53 | #include "hci.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 54 | #include "hci_const.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 55 | #include "bluenrg_aci.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 56 | #include "bluenrg_hal_aci.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 57 | #include "bluenrg_gap.h" |
apalmieri | 126:32039585a969 | 58 | #include "bluenrg_utils.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 59 | |
Wolfgang Betz |
90:26c0c9807ab4 | 60 | #include "hal_types.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 61 | #include "hal.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 62 | #include "gp_timer.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 63 | #include "osal.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 64 | #include "sm.h" |
Wolfgang Betz |
116:1aa0d2da72e4 | 65 | #include "debug_platform.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 66 | |
Wolfgang Betz |
90:26c0c9807ab4 | 67 | #ifdef __cplusplus |
Wolfgang Betz |
90:26c0c9807ab4 | 68 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 69 | #endif |
Wolfgang Betz |
90:26c0c9807ab4 | 70 | |
apalmieri | 126:32039585a969 | 71 | #define IDB04A1 0 |
apalmieri | 126:32039585a969 | 72 | #define IDB05A1 1 |
Wolfgang Betz |
90:26c0c9807ab4 | 73 | |
Wolfgang Betz |
121:f00e26745564 | 74 | // static void btle_handler(/*ble_evt_t * p_ble_evt*/); |
Wolfgang Betz |
90:26c0c9807ab4 | 75 | void HCI_Input(tHciDataPacket * hciReadPacket); |
Wolfgang Betz |
90:26c0c9807ab4 | 76 | |
Wolfgang Betz |
90:26c0c9807ab4 | 77 | //#define BDADDR_SIZE 6 |
Wolfgang Betz |
90:26c0c9807ab4 | 78 | //tHalUint8 bdaddr[BDADDR_SIZE]= {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02}; |
Wolfgang Betz |
90:26c0c9807ab4 | 79 | |
Wolfgang Betz |
90:26c0c9807ab4 | 80 | uint16_t g_gap_service_handle = 0; |
Wolfgang Betz |
90:26c0c9807ab4 | 81 | uint16_t g_appearance_char_handle = 0; |
Wolfgang Betz |
90:26c0c9807ab4 | 82 | uint16_t g_device_name_char_handle = 0; |
Wolfgang Betz |
90:26c0c9807ab4 | 83 | |
Wolfgang Betz |
90:26c0c9807ab4 | 84 | /* Private variables ---------------------------------------------------------*/ |
Wolfgang Betz |
90:26c0c9807ab4 | 85 | volatile uint8_t set_connectable = 1; |
Wolfgang Betz |
90:26c0c9807ab4 | 86 | // ANDREA |
apalmieri | 126:32039585a969 | 87 | uint8_t bnrg_expansion_board = IDB04A1; /* at startup, suppose the X-NUCLEO-IDB04A1 is used */ |
apalmieri | 126:32039585a969 | 88 | |
Wolfgang Betz |
90:26c0c9807ab4 | 89 | Gap::Address_t bleAddr; |
Wolfgang Betz |
90:26c0c9807ab4 | 90 | Gap::AddressType_t addr_type = Gap::ADDR_TYPE_PUBLIC; |
Wolfgang Betz |
90:26c0c9807ab4 | 91 | |
Wolfgang Betz |
90:26c0c9807ab4 | 92 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 93 | /*! |
Wolfgang Betz |
90:26c0c9807ab4 | 94 | @brief Initialises BTLE and the underlying HW/Device |
Wolfgang Betz |
90:26c0c9807ab4 | 95 | @param isSetAddress boolean if address has been set |
Wolfgang Betz |
90:26c0c9807ab4 | 96 | @param mosi MOSI Pin |
Wolfgang Betz |
90:26c0c9807ab4 | 97 | @param miso MISO Pin |
Wolfgang Betz |
90:26c0c9807ab4 | 98 | @param sclk clock Pin |
Wolfgang Betz |
90:26c0c9807ab4 | 99 | @returns void |
Wolfgang Betz |
90:26c0c9807ab4 | 100 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 101 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 102 | void btle_init(bool isSetAddress) |
Wolfgang Betz |
90:26c0c9807ab4 | 103 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 104 | DEBUG("btle_init>>\n\r"); |
Wolfgang Betz |
90:26c0c9807ab4 | 105 | |
Wolfgang Betz |
90:26c0c9807ab4 | 106 | int ret; |
apalmieri | 126:32039585a969 | 107 | uint8_t hwVersion; |
apalmieri | 126:32039585a969 | 108 | uint16_t fwVersion; |
Wolfgang Betz |
90:26c0c9807ab4 | 109 | uint16_t service_handle, dev_name_char_handle, appearance_char_handle; |
Wolfgang Betz |
90:26c0c9807ab4 | 110 | |
Wolfgang Betz |
90:26c0c9807ab4 | 111 | /* Delay needed only to be able to acces the JTAG interface after reset |
Wolfgang Betz |
90:26c0c9807ab4 | 112 | if it will be disabled later. */ |
Wolfgang Betz |
90:26c0c9807ab4 | 113 | Clock_Wait(500); |
Wolfgang Betz |
90:26c0c9807ab4 | 114 | |
Wolfgang Betz |
90:26c0c9807ab4 | 115 | /* Initialize the BlueNRG HCI */ |
Wolfgang Betz |
90:26c0c9807ab4 | 116 | HCI_Init(); |
Wolfgang Betz |
90:26c0c9807ab4 | 117 | |
Wolfgang Betz |
90:26c0c9807ab4 | 118 | /* Reset BlueNRG SPI interface */ |
Wolfgang Betz |
90:26c0c9807ab4 | 119 | BlueNRG_RST(); |
Wolfgang Betz |
90:26c0c9807ab4 | 120 | |
apalmieri | 126:32039585a969 | 121 | /* get the BlueNRG HW and FW versions */ |
apalmieri | 126:32039585a969 | 122 | getBlueNRGVersion(&hwVersion, &fwVersion); |
apalmieri | 126:32039585a969 | 123 | |
apalmieri | 126:32039585a969 | 124 | /* |
apalmieri | 126:32039585a969 | 125 | * Reset BlueNRG again otherwise we won't |
apalmieri | 126:32039585a969 | 126 | * be able to change its MAC address. |
apalmieri | 126:32039585a969 | 127 | * aci_hal_write_config_data() must be the first |
apalmieri | 126:32039585a969 | 128 | * command after reset otherwise it will fail. |
apalmieri | 126:32039585a969 | 129 | */ |
apalmieri | 126:32039585a969 | 130 | BlueNRG_RST(); |
apalmieri | 126:32039585a969 | 131 | |
apalmieri | 126:32039585a969 | 132 | if (hwVersion > 0x30) { /* X-NUCLEO-IDB05A1 expansion board is used */ |
apalmieri | 126:32039585a969 | 133 | bnrg_expansion_board = IDB05A1; |
apalmieri | 126:32039585a969 | 134 | } |
apalmieri | 126:32039585a969 | 135 | |
Wolfgang Betz |
90:26c0c9807ab4 | 136 | /* The Nucleo board must be configured as SERVER */ |
Wolfgang Betz |
90:26c0c9807ab4 | 137 | //check if isSetAddress is set than set address. |
Wolfgang Betz |
90:26c0c9807ab4 | 138 | // ANDREA |
Wolfgang Betz |
90:26c0c9807ab4 | 139 | if(isSetAddress) |
Wolfgang Betz |
90:26c0c9807ab4 | 140 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 141 | BlueNRGGap::getInstance().getAddress(&addr_type, bleAddr); |
Wolfgang Betz |
90:26c0c9807ab4 | 142 | |
Wolfgang Betz |
90:26c0c9807ab4 | 143 | Gap::Address_t bdaddr; |
Wolfgang Betz |
90:26c0c9807ab4 | 144 | Osal_MemCpy(bdaddr, bleAddr, BDADDR_SIZE); |
Wolfgang Betz |
90:26c0c9807ab4 | 145 | |
Wolfgang Betz |
90:26c0c9807ab4 | 146 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
Wolfgang Betz |
90:26c0c9807ab4 | 147 | CONFIG_DATA_PUBADDR_LEN, |
Wolfgang Betz |
90:26c0c9807ab4 | 148 | bdaddr); |
Wolfgang Betz |
90:26c0c9807ab4 | 149 | } else { |
Wolfgang Betz |
90:26c0c9807ab4 | 150 | |
Wolfgang Betz |
90:26c0c9807ab4 | 151 | const Gap::Address_t BLE_address_BE = {0xFD,0x00,0x25,0xAA,0x02,0x04}; |
Wolfgang Betz |
90:26c0c9807ab4 | 152 | BlueNRGGap::getInstance().setAddress(Gap::ADDR_TYPE_PUBLIC, BLE_address_BE); |
Wolfgang Betz |
90:26c0c9807ab4 | 153 | |
Wolfgang Betz |
90:26c0c9807ab4 | 154 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
Wolfgang Betz |
90:26c0c9807ab4 | 155 | CONFIG_DATA_PUBADDR_LEN, |
Wolfgang Betz |
90:26c0c9807ab4 | 156 | BLE_address_BE); |
Wolfgang Betz |
90:26c0c9807ab4 | 157 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 158 | |
Wolfgang Betz |
90:26c0c9807ab4 | 159 | ret = aci_gatt_init(); |
Wolfgang Betz |
90:26c0c9807ab4 | 160 | if(ret){ |
Wolfgang Betz |
90:26c0c9807ab4 | 161 | PRINTF("GATT_Init failed.\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 162 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 163 | //GAP is always in PERIPHERAL _ROLE as mbed does not support Master role at the moment |
apalmieri | 126:32039585a969 | 164 | if (bnrg_expansion_board == IDB05A1) { |
apalmieri | 126:32039585a969 | 165 | ret = aci_gap_init_IDB05A1(GAP_PERIPHERAL_ROLE_IDB05A1, 0, 0x07, &service_handle, &dev_name_char_handle, &appearance_char_handle); |
apalmieri | 126:32039585a969 | 166 | } else { |
apalmieri | 126:32039585a969 | 167 | ret = aci_gap_init_IDB04A1(GAP_PERIPHERAL_ROLE_IDB04A1, &service_handle, &dev_name_char_handle, &appearance_char_handle); |
apalmieri | 126:32039585a969 | 168 | } |
apalmieri | 126:32039585a969 | 169 | |
Wolfgang Betz |
90:26c0c9807ab4 | 170 | if(ret != BLE_STATUS_SUCCESS){ |
Wolfgang Betz |
90:26c0c9807ab4 | 171 | PRINTF("GAP_Init failed.\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 172 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 173 | |
Wolfgang Betz |
90:26c0c9807ab4 | 174 | //ANDREA -- FIXME: Security and passkey set by default |
Wolfgang Betz |
90:26c0c9807ab4 | 175 | ret = aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED, |
Wolfgang Betz |
90:26c0c9807ab4 | 176 | OOB_AUTH_DATA_ABSENT, |
Wolfgang Betz |
90:26c0c9807ab4 | 177 | NULL, |
Wolfgang Betz |
90:26c0c9807ab4 | 178 | 7, |
Wolfgang Betz |
90:26c0c9807ab4 | 179 | 16, |
Wolfgang Betz |
90:26c0c9807ab4 | 180 | USE_FIXED_PIN_FOR_PAIRING, |
Wolfgang Betz |
90:26c0c9807ab4 | 181 | 123456, |
Wolfgang Betz |
90:26c0c9807ab4 | 182 | BONDING); |
Wolfgang Betz |
90:26c0c9807ab4 | 183 | if (ret == BLE_STATUS_SUCCESS) { |
Wolfgang Betz |
90:26c0c9807ab4 | 184 | DEBUG("Auth Req set successfully.\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 185 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 186 | |
Wolfgang Betz |
90:26c0c9807ab4 | 187 | aci_hal_set_tx_power_level(1,4); |
Wolfgang Betz |
90:26c0c9807ab4 | 188 | |
Wolfgang Betz |
90:26c0c9807ab4 | 189 | g_gap_service_handle = service_handle; |
Wolfgang Betz |
90:26c0c9807ab4 | 190 | g_appearance_char_handle = appearance_char_handle; |
Wolfgang Betz |
90:26c0c9807ab4 | 191 | g_device_name_char_handle = dev_name_char_handle; |
Wolfgang Betz |
90:26c0c9807ab4 | 192 | //Device Name is set from Accumulate Adv Data Payload or through setDeviceName API |
Wolfgang Betz |
90:26c0c9807ab4 | 193 | /*ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, |
Wolfgang Betz |
90:26c0c9807ab4 | 194 | strlen(name), (tHalUint8 *)name);*/ |
Wolfgang Betz |
90:26c0c9807ab4 | 195 | |
Wolfgang Betz |
90:26c0c9807ab4 | 196 | return; |
Wolfgang Betz |
90:26c0c9807ab4 | 197 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 198 | |
Wolfgang Betz |
90:26c0c9807ab4 | 199 | void User_Process() |
Wolfgang Betz |
90:26c0c9807ab4 | 200 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 201 | if(set_connectable){ |
Wolfgang Betz |
90:26c0c9807ab4 | 202 | setConnectable(); |
Wolfgang Betz |
90:26c0c9807ab4 | 203 | set_connectable = FALSE; |
Wolfgang Betz |
90:26c0c9807ab4 | 204 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 205 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 206 | |
Wolfgang Betz |
90:26c0c9807ab4 | 207 | void setConnectable(void) |
Wolfgang Betz |
90:26c0c9807ab4 | 208 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 209 | tBleStatus ret; |
Wolfgang Betz |
90:26c0c9807ab4 | 210 | |
Wolfgang Betz |
90:26c0c9807ab4 | 211 | const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G'}; |
Wolfgang Betz |
90:26c0c9807ab4 | 212 | |
Wolfgang Betz |
90:26c0c9807ab4 | 213 | /* disable scan response */ |
Wolfgang Betz |
90:26c0c9807ab4 | 214 | hci_le_set_scan_resp_data(0,NULL); |
Wolfgang Betz |
90:26c0c9807ab4 | 215 | |
Wolfgang Betz |
90:26c0c9807ab4 | 216 | //int t = BlueNRGGap::getInstance()::ADV_IND;// advType; |
Wolfgang Betz |
90:26c0c9807ab4 | 217 | |
Wolfgang Betz |
90:26c0c9807ab4 | 218 | ret = aci_gap_set_discoverable(BlueNRGGap::getInstance().ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE, |
Wolfgang Betz |
90:26c0c9807ab4 | 219 | sizeof(local_name), local_name, 0, NULL, 0, 0); |
Wolfgang Betz |
90:26c0c9807ab4 | 220 | if (ret != BLE_STATUS_SUCCESS) { |
Wolfgang Betz |
90:26c0c9807ab4 | 221 | DEBUG("Error while setting discoverable mode (%d)\n", ret); |
Wolfgang Betz |
90:26c0c9807ab4 | 222 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 223 | |
Wolfgang Betz |
90:26c0c9807ab4 | 224 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 225 | |
Wolfgang Betz |
90:26c0c9807ab4 | 226 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 227 | /*! |
Wolfgang Betz |
90:26c0c9807ab4 | 228 | @brief Not Used |
Wolfgang Betz |
90:26c0c9807ab4 | 229 | |
Wolfgang Betz |
90:26c0c9807ab4 | 230 | @param[in] void |
Wolfgang Betz |
90:26c0c9807ab4 | 231 | |
Wolfgang Betz |
90:26c0c9807ab4 | 232 | @returns |
Wolfgang Betz |
90:26c0c9807ab4 | 233 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 234 | /**************************************************************************/ |
Wolfgang Betz |
121:f00e26745564 | 235 | /* |
Wolfgang Betz |
90:26c0c9807ab4 | 236 | static void btle_handler() |
Wolfgang Betz |
90:26c0c9807ab4 | 237 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 238 | |
Wolfgang Betz |
90:26c0c9807ab4 | 239 | } |
Wolfgang Betz |
121:f00e26745564 | 240 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 241 | |
Wolfgang Betz |
90:26c0c9807ab4 | 242 | /*! |
Wolfgang Betz |
90:26c0c9807ab4 | 243 | @brief Not Used |
Wolfgang Betz |
90:26c0c9807ab4 | 244 | |
Wolfgang Betz |
90:26c0c9807ab4 | 245 | @param[in] void |
Wolfgang Betz |
90:26c0c9807ab4 | 246 | |
Wolfgang Betz |
90:26c0c9807ab4 | 247 | @returns |
Wolfgang Betz |
90:26c0c9807ab4 | 248 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 249 | void SPI_Poll(void) |
Wolfgang Betz |
90:26c0c9807ab4 | 250 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 251 | //HAL_GPIO_EXTI_Callback_Poll(BNRG_SPI_EXTI_PIN); |
Wolfgang Betz |
90:26c0c9807ab4 | 252 | return; |
Wolfgang Betz |
90:26c0c9807ab4 | 253 | } |
apalmieri | 126:32039585a969 | 254 | |
apalmieri | 126:32039585a969 | 255 | void Attribute_Modified_CB(uint16_t attr_handle, uint8_t data_length, uint8_t *att_data, uint8_t offset) |
apalmieri | 126:32039585a969 | 256 | { |
apalmieri | 126:32039585a969 | 257 | //Extract the GattCharacteristic from p_characteristics[] and find the properties mask |
apalmieri | 126:32039585a969 | 258 | GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(attr_handle); |
apalmieri | 126:32039585a969 | 259 | if(p_char!=NULL) { |
apalmieri | 126:32039585a969 | 260 | GattAttribute::Handle_t charHandle = p_char->getValueAttribute().getHandle(); |
apalmieri | 126:32039585a969 | 261 | BlueNRGGattServer::HandleEnum_t currentHandle = BlueNRGGattServer::CHAR_HANDLE; |
apalmieri | 126:32039585a969 | 262 | DEBUG("CharHandle %d, length: %d, Data: %d\n\r", charHandle, data_length, (uint16_t)att_data[0]); |
apalmieri | 126:32039585a969 | 263 | DEBUG("getProperties 0x%x\n\r",p_char->getProperties()); |
apalmieri | 126:32039585a969 | 264 | if(attr_handle == charHandle+CHAR_VALUE_OFFSET) { |
apalmieri | 126:32039585a969 | 265 | currentHandle = BlueNRGGattServer::CHAR_VALUE_HANDLE; |
apalmieri | 126:32039585a969 | 266 | } |
apalmieri | 126:32039585a969 | 267 | |
apalmieri | 126:32039585a969 | 268 | if(attr_handle == charHandle+CHAR_DESC_OFFSET) { |
apalmieri | 126:32039585a969 | 269 | currentHandle = BlueNRGGattServer::CHAR_DESC_HANDLE; |
apalmieri | 126:32039585a969 | 270 | } |
apalmieri | 126:32039585a969 | 271 | DEBUG("currentHandle %d\n\r", currentHandle); |
apalmieri | 126:32039585a969 | 272 | if((p_char->getProperties() & |
apalmieri | 126:32039585a969 | 273 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) && |
apalmieri | 126:32039585a969 | 274 | currentHandle == BlueNRGGattServer::CHAR_DESC_HANDLE) { |
apalmieri | 126:32039585a969 | 275 | |
apalmieri | 126:32039585a969 | 276 | DEBUG("*****NOTIFICATION CASE\n\r"); |
apalmieri | 126:32039585a969 | 277 | //Now Check if data written in Enable or Disable |
apalmieri | 126:32039585a969 | 278 | if((uint16_t)att_data[0]==1) { |
apalmieri | 126:32039585a969 | 279 | //DEBUG("Notify ENABLED\n\r"); |
apalmieri | 126:32039585a969 | 280 | BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_ENABLED, p_char->getValueAttribute().getHandle()); |
apalmieri | 126:32039585a969 | 281 | } else { |
apalmieri | 126:32039585a969 | 282 | //DEBUG("Notify DISABLED\n\r"); |
apalmieri | 126:32039585a969 | 283 | BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_DISABLED, p_char->getValueAttribute().getHandle()); |
apalmieri | 126:32039585a969 | 284 | } |
apalmieri | 126:32039585a969 | 285 | } |
apalmieri | 126:32039585a969 | 286 | |
apalmieri | 126:32039585a969 | 287 | //Check is attr handle property is WRITEABLE, if yes, generate GATT_EVENT_DATA_WRITTEN Event |
apalmieri | 126:32039585a969 | 288 | if((p_char->getProperties() & |
apalmieri | 126:32039585a969 | 289 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)) && |
apalmieri | 126:32039585a969 | 290 | currentHandle == BlueNRGGattServer::CHAR_VALUE_HANDLE) { |
apalmieri | 126:32039585a969 | 291 | |
apalmieri | 126:32039585a969 | 292 | DEBUG("*****WRITE CASE\n\r"); |
apalmieri | 126:32039585a969 | 293 | |
apalmieri | 126:32039585a969 | 294 | GattWriteCallbackParams writeParams; |
apalmieri | 126:32039585a969 | 295 | writeParams.handle = p_char->getValueAttribute().getHandle(); |
apalmieri | 126:32039585a969 | 296 | writeParams.writeOp = GattWriteCallbackParams::OP_WRITE_REQ;//Where to find this property in BLUENRG? |
apalmieri | 126:32039585a969 | 297 | writeParams.len = data_length; |
apalmieri | 126:32039585a969 | 298 | writeParams.data = att_data; |
apalmieri | 126:32039585a969 | 299 | if (bnrg_expansion_board == IDB05A1) { |
apalmieri | 126:32039585a969 | 300 | writeParams.offset = offset; |
apalmieri | 126:32039585a969 | 301 | } |
apalmieri | 126:32039585a969 | 302 | BlueNRGGattServer::getInstance().HCIDataWrittenEvent(&writeParams); |
apalmieri | 126:32039585a969 | 303 | |
apalmieri | 126:32039585a969 | 304 | //BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_WRITTEN, evt->attr_handle); |
apalmieri | 126:32039585a969 | 305 | //Write the actual Data to the Attr Handle? (uint8_1[])att_data contains the data |
apalmieri | 126:32039585a969 | 306 | if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) { |
apalmieri | 126:32039585a969 | 307 | BlueNRGGattServer::getInstance().write(p_char->getValueAttribute().getHandle(), (uint8_t*)att_data, data_length, false); |
apalmieri | 126:32039585a969 | 308 | } |
apalmieri | 126:32039585a969 | 309 | } |
apalmieri | 126:32039585a969 | 310 | } |
apalmieri | 126:32039585a969 | 311 | |
apalmieri | 126:32039585a969 | 312 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 313 | |
Wolfgang Betz |
90:26c0c9807ab4 | 314 | #ifdef __cplusplus |
Wolfgang Betz |
90:26c0c9807ab4 | 315 | extern "C" { |
Wolfgang Betz |
90:26c0c9807ab4 | 316 | #endif |
Wolfgang Betz |
90:26c0c9807ab4 | 317 | |
Wolfgang Betz |
90:26c0c9807ab4 | 318 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 319 | /*! |
Wolfgang Betz |
90:26c0c9807ab4 | 320 | @brief Handle HCI Stack Event |
Wolfgang Betz |
90:26c0c9807ab4 | 321 | |
Wolfgang Betz |
90:26c0c9807ab4 | 322 | @param[in] pckt |
Wolfgang Betz |
90:26c0c9807ab4 | 323 | Event Packet sent by the stack to be decoded |
Wolfgang Betz |
90:26c0c9807ab4 | 324 | |
Wolfgang Betz |
90:26c0c9807ab4 | 325 | @returns |
Wolfgang Betz |
90:26c0c9807ab4 | 326 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 327 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 328 | extern void HCI_Event_CB(void *pckt) { |
Wolfgang Betz |
90:26c0c9807ab4 | 329 | hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt; |
Wolfgang Betz |
90:26c0c9807ab4 | 330 | hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 331 | |
Wolfgang Betz |
90:26c0c9807ab4 | 332 | if(hci_pckt->type != HCI_EVENT_PKT) |
Wolfgang Betz |
90:26c0c9807ab4 | 333 | return; |
Wolfgang Betz |
90:26c0c9807ab4 | 334 | |
Wolfgang Betz |
90:26c0c9807ab4 | 335 | switch(event_pckt->evt){ |
Wolfgang Betz |
90:26c0c9807ab4 | 336 | |
Wolfgang Betz |
90:26c0c9807ab4 | 337 | case EVT_DISCONN_COMPLETE: |
Wolfgang Betz |
90:26c0c9807ab4 | 338 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 339 | DEBUG("EVT_DISCONN_COMPLETE\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 340 | |
Wolfgang Betz |
90:26c0c9807ab4 | 341 | evt_disconn_complete *evt = (evt_disconn_complete*)event_pckt; |
Wolfgang Betz |
90:26c0c9807ab4 | 342 | |
Wolfgang Betz |
90:26c0c9807ab4 | 343 | BlueNRGGap::getInstance().processDisconnectionEvent(evt->handle, BlueNRGGap::REMOTE_USER_TERMINATED_CONNECTION); |
Wolfgang Betz |
90:26c0c9807ab4 | 344 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 345 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 346 | |
Wolfgang Betz |
90:26c0c9807ab4 | 347 | case EVT_LE_META_EVENT: |
Wolfgang Betz |
90:26c0c9807ab4 | 348 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 349 | DEBUG("EVT_LE_META_EVENT\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 350 | |
Wolfgang Betz |
90:26c0c9807ab4 | 351 | evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 352 | |
Wolfgang Betz |
90:26c0c9807ab4 | 353 | switch(evt->subevent){ |
Wolfgang Betz |
90:26c0c9807ab4 | 354 | // ANDREA |
Wolfgang Betz |
90:26c0c9807ab4 | 355 | case EVT_LE_CONN_COMPLETE: |
Wolfgang Betz |
90:26c0c9807ab4 | 356 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 357 | Gap::AddressType_t peerAddrType = Gap::ADDR_TYPE_PUBLIC; |
Wolfgang Betz |
90:26c0c9807ab4 | 358 | DEBUG("EVT_LE_CONN_COMPLETE\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 359 | |
Wolfgang Betz |
90:26c0c9807ab4 | 360 | evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 361 | |
Wolfgang Betz |
90:26c0c9807ab4 | 362 | BlueNRGGap::getInstance().setConnectionHandle(cc->handle); |
Wolfgang Betz |
90:26c0c9807ab4 | 363 | BlueNRGGap::ConnectionParams_t connectionParams; |
Wolfgang Betz |
90:26c0c9807ab4 | 364 | BlueNRGGap::getInstance().getPreferredConnectionParams(&connectionParams); |
Wolfgang Betz |
90:26c0c9807ab4 | 365 | switch (cc->peer_bdaddr_type) { |
Wolfgang Betz |
90:26c0c9807ab4 | 366 | case PUBLIC_ADDR: |
Wolfgang Betz |
90:26c0c9807ab4 | 367 | peerAddrType = Gap::ADDR_TYPE_PUBLIC; |
Wolfgang Betz |
90:26c0c9807ab4 | 368 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 369 | case STATIC_RANDOM_ADDR: |
Wolfgang Betz |
90:26c0c9807ab4 | 370 | peerAddrType = Gap::ADDR_TYPE_RANDOM_STATIC; |
Wolfgang Betz |
90:26c0c9807ab4 | 371 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 372 | case RESOLVABLE_PRIVATE_ADDR: |
Wolfgang Betz |
90:26c0c9807ab4 | 373 | peerAddrType = Gap::ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE; |
Wolfgang Betz |
90:26c0c9807ab4 | 374 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 375 | case NON_RESOLVABLE_PRIVATE_ADDR: |
Wolfgang Betz |
90:26c0c9807ab4 | 376 | peerAddrType = Gap::ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE; |
Wolfgang Betz |
90:26c0c9807ab4 | 377 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 378 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 379 | BlueNRGGap::getInstance().processConnectionEvent(cc->handle, Gap::PERIPHERAL, peerAddrType, cc->peer_bdaddr, addr_type, bleAddr, (const BlueNRGGap::ConnectionParams_t *)&connectionParams); |
Wolfgang Betz |
90:26c0c9807ab4 | 380 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 381 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 382 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 383 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 384 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 385 | |
Wolfgang Betz |
90:26c0c9807ab4 | 386 | case EVT_VENDOR: |
Wolfgang Betz |
90:26c0c9807ab4 | 387 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 388 | evt_blue_aci *blue_evt = (evt_blue_aci*)event_pckt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 389 | //DEBUG("EVT_VENDOR %d\n", blue_evt->ecode); |
Wolfgang Betz |
90:26c0c9807ab4 | 390 | |
Wolfgang Betz |
90:26c0c9807ab4 | 391 | switch(blue_evt->ecode){ |
Wolfgang Betz |
90:26c0c9807ab4 | 392 | |
Wolfgang Betz |
90:26c0c9807ab4 | 393 | case EVT_BLUE_GATT_READ_PERMIT_REQ: |
Wolfgang Betz |
90:26c0c9807ab4 | 394 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 395 | DEBUG("EVT_BLUE_GATT_READ_PERMIT_REQ_OK\n\r"); |
apalmieri | 97:a89d28ad6092 | 396 | evt_gatt_read_permit_req *pr = (evt_gatt_read_permit_req*)blue_evt->data; |
apalmieri | 97:a89d28ad6092 | 397 | BlueNRGGattServer::getInstance().Read_Request_CB(pr->attr_handle-CHAR_VALUE_OFFSET); |
Wolfgang Betz |
90:26c0c9807ab4 | 398 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 399 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 400 | |
Wolfgang Betz |
90:26c0c9807ab4 | 401 | case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED: |
Wolfgang Betz |
90:26c0c9807ab4 | 402 | { |
apalmieri | 126:32039585a969 | 403 | DEBUG("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED\n\r"); |
Wolfgang Betz |
90:26c0c9807ab4 | 404 | /* this callback is invoked when a GATT attribute is modified |
Wolfgang Betz |
90:26c0c9807ab4 | 405 | extract callback data and pass to suitable handler function */ |
apalmieri | 126:32039585a969 | 406 | if (bnrg_expansion_board == IDB05A1) { |
apalmieri | 126:32039585a969 | 407 | evt_gatt_attr_modified_IDB05A1 *evt = (evt_gatt_attr_modified_IDB05A1*)blue_evt->data; |
apalmieri | 126:32039585a969 | 408 | Attribute_Modified_CB(evt->attr_handle, evt->data_length, evt->att_data, evt->offset); |
apalmieri | 126:32039585a969 | 409 | } else { |
apalmieri | 126:32039585a969 | 410 | evt_gatt_attr_modified_IDB04A1 *evt = (evt_gatt_attr_modified_IDB04A1*)blue_evt->data; |
apalmieri | 126:32039585a969 | 411 | Attribute_Modified_CB(evt->attr_handle, evt->data_length, evt->att_data, 0); |
Wolfgang Betz |
90:26c0c9807ab4 | 412 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 413 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 414 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 415 | |
Wolfgang Betz |
90:26c0c9807ab4 | 416 | //Any cases for Data Sent Notifications? |
Wolfgang Betz |
90:26c0c9807ab4 | 417 | case EVT_BLUE_GATT_NOTIFICATION: |
Wolfgang Betz |
90:26c0c9807ab4 | 418 | //This is only relevant for Client Side Event |
Wolfgang Betz |
90:26c0c9807ab4 | 419 | DEBUG("EVT_BLUE_GATT_NOTIFICATION"); |
Wolfgang Betz |
90:26c0c9807ab4 | 420 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 421 | case EVT_BLUE_GATT_INDICATION: |
Wolfgang Betz |
90:26c0c9807ab4 | 422 | //This is only relevant for Client Side Event |
Wolfgang Betz |
90:26c0c9807ab4 | 423 | DEBUG("EVT_BLUE_GATT_INDICATION"); |
Wolfgang Betz |
90:26c0c9807ab4 | 424 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 425 | |
Wolfgang Betz |
90:26c0c9807ab4 | 426 | case EVT_BLUE_GATT_PROCEDURE_COMPLETE: |
Wolfgang Betz |
90:26c0c9807ab4 | 427 | DEBUG("EVT_BLUE_GATT_PROCEDURE_COMPLETE"); |
Wolfgang Betz |
90:26c0c9807ab4 | 428 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 429 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 430 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 431 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 432 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 433 | |
Wolfgang Betz |
90:26c0c9807ab4 | 434 | return ; |
Wolfgang Betz |
90:26c0c9807ab4 | 435 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 436 | |
Wolfgang Betz |
90:26c0c9807ab4 | 437 | |
Wolfgang Betz |
90:26c0c9807ab4 | 438 | #ifdef __cplusplus |
Wolfgang Betz |
90:26c0c9807ab4 | 439 | } |
Wolfgang Betz |
116:1aa0d2da72e4 | 440 | #endif |