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@97:a89d28ad6092, 2015-07-27 (annotated)
- Committer:
- apalmieri
- Date:
- Mon Jul 27 11:10:19 2015 +0000
- Revision:
- 97:a89d28ad6092
- Parent:
- 94:2993df23d1ac
- Child:
- 98:731050bb5362
Fix bug on searching Char by handle
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" |
Wolfgang Betz |
90:26c0c9807ab4 | 58 | |
Wolfgang Betz |
90:26c0c9807ab4 | 59 | #include "hal_types.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 60 | #include "hal.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 61 | #include "gp_timer.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 62 | #include "osal.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 63 | #include "sm.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 64 | #include "debug.h" |
Wolfgang Betz |
90:26c0c9807ab4 | 65 | |
Wolfgang Betz |
90:26c0c9807ab4 | 66 | #ifdef __cplusplus |
Wolfgang Betz |
90:26c0c9807ab4 | 67 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 68 | #endif |
Wolfgang Betz |
90:26c0c9807ab4 | 69 | |
Wolfgang Betz |
90:26c0c9807ab4 | 70 | |
Wolfgang Betz |
90:26c0c9807ab4 | 71 | static void btle_handler(/*ble_evt_t * p_ble_evt*/); |
Wolfgang Betz |
90:26c0c9807ab4 | 72 | void HCI_Input(tHciDataPacket * hciReadPacket); |
Wolfgang Betz |
90:26c0c9807ab4 | 73 | |
Wolfgang Betz |
90:26c0c9807ab4 | 74 | //#define BDADDR_SIZE 6 |
Wolfgang Betz |
90:26c0c9807ab4 | 75 | //tHalUint8 bdaddr[BDADDR_SIZE]= {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02}; |
Wolfgang Betz |
90:26c0c9807ab4 | 76 | |
Wolfgang Betz |
90:26c0c9807ab4 | 77 | uint16_t g_gap_service_handle = 0; |
Wolfgang Betz |
90:26c0c9807ab4 | 78 | uint16_t g_appearance_char_handle = 0; |
Wolfgang Betz |
90:26c0c9807ab4 | 79 | uint16_t g_device_name_char_handle = 0; |
Wolfgang Betz |
90:26c0c9807ab4 | 80 | |
Wolfgang Betz |
90:26c0c9807ab4 | 81 | /* Private variables ---------------------------------------------------------*/ |
Wolfgang Betz |
90:26c0c9807ab4 | 82 | volatile uint8_t set_connectable = 1; |
Wolfgang Betz |
90:26c0c9807ab4 | 83 | // ANDREA |
Wolfgang Betz |
90:26c0c9807ab4 | 84 | Gap::Address_t bleAddr; |
Wolfgang Betz |
90:26c0c9807ab4 | 85 | Gap::AddressType_t addr_type = Gap::ADDR_TYPE_PUBLIC; |
Wolfgang Betz |
90:26c0c9807ab4 | 86 | |
Wolfgang Betz |
90:26c0c9807ab4 | 87 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 88 | /*! |
Wolfgang Betz |
90:26c0c9807ab4 | 89 | @brief Initialises BTLE and the underlying HW/Device |
Wolfgang Betz |
90:26c0c9807ab4 | 90 | @param isSetAddress boolean if address has been set |
Wolfgang Betz |
90:26c0c9807ab4 | 91 | @param mosi MOSI Pin |
Wolfgang Betz |
90:26c0c9807ab4 | 92 | @param miso MISO Pin |
Wolfgang Betz |
90:26c0c9807ab4 | 93 | @param sclk clock Pin |
Wolfgang Betz |
90:26c0c9807ab4 | 94 | @returns void |
Wolfgang Betz |
90:26c0c9807ab4 | 95 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 96 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 97 | void btle_init(bool isSetAddress) |
Wolfgang Betz |
90:26c0c9807ab4 | 98 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 99 | DEBUG("btle_init>>\n\r"); |
Wolfgang Betz |
90:26c0c9807ab4 | 100 | |
Wolfgang Betz |
90:26c0c9807ab4 | 101 | int ret; |
Wolfgang Betz |
90:26c0c9807ab4 | 102 | uint16_t service_handle, dev_name_char_handle, appearance_char_handle; |
Wolfgang Betz |
90:26c0c9807ab4 | 103 | |
Wolfgang Betz |
90:26c0c9807ab4 | 104 | /* Delay needed only to be able to acces the JTAG interface after reset |
Wolfgang Betz |
90:26c0c9807ab4 | 105 | if it will be disabled later. */ |
Wolfgang Betz |
90:26c0c9807ab4 | 106 | Clock_Wait(500); |
Wolfgang Betz |
90:26c0c9807ab4 | 107 | |
Wolfgang Betz |
90:26c0c9807ab4 | 108 | /* Initialize the BlueNRG HCI */ |
Wolfgang Betz |
90:26c0c9807ab4 | 109 | HCI_Init(); |
Wolfgang Betz |
90:26c0c9807ab4 | 110 | |
Wolfgang Betz |
90:26c0c9807ab4 | 111 | /* Reset BlueNRG SPI interface */ |
Wolfgang Betz |
90:26c0c9807ab4 | 112 | BlueNRG_RST(); |
Wolfgang Betz |
90:26c0c9807ab4 | 113 | |
Wolfgang Betz |
90:26c0c9807ab4 | 114 | /* The Nucleo board must be configured as SERVER */ |
Wolfgang Betz |
90:26c0c9807ab4 | 115 | //check if isSetAddress is set than set address. |
Wolfgang Betz |
90:26c0c9807ab4 | 116 | // ANDREA |
Wolfgang Betz |
90:26c0c9807ab4 | 117 | if(isSetAddress) |
Wolfgang Betz |
90:26c0c9807ab4 | 118 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 119 | BlueNRGGap::getInstance().getAddress(&addr_type, bleAddr); |
Wolfgang Betz |
90:26c0c9807ab4 | 120 | |
Wolfgang Betz |
90:26c0c9807ab4 | 121 | Gap::Address_t bdaddr; |
Wolfgang Betz |
90:26c0c9807ab4 | 122 | Osal_MemCpy(bdaddr, bleAddr, BDADDR_SIZE); |
Wolfgang Betz |
90:26c0c9807ab4 | 123 | |
Wolfgang Betz |
90:26c0c9807ab4 | 124 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
Wolfgang Betz |
90:26c0c9807ab4 | 125 | CONFIG_DATA_PUBADDR_LEN, |
Wolfgang Betz |
90:26c0c9807ab4 | 126 | bdaddr); |
Wolfgang Betz |
90:26c0c9807ab4 | 127 | } else { |
Wolfgang Betz |
90:26c0c9807ab4 | 128 | |
Wolfgang Betz |
90:26c0c9807ab4 | 129 | const Gap::Address_t BLE_address_BE = {0xFD,0x00,0x25,0xAA,0x02,0x04}; |
Wolfgang Betz |
90:26c0c9807ab4 | 130 | BlueNRGGap::getInstance().setAddress(Gap::ADDR_TYPE_PUBLIC, BLE_address_BE); |
Wolfgang Betz |
90:26c0c9807ab4 | 131 | |
Wolfgang Betz |
90:26c0c9807ab4 | 132 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
Wolfgang Betz |
90:26c0c9807ab4 | 133 | CONFIG_DATA_PUBADDR_LEN, |
Wolfgang Betz |
90:26c0c9807ab4 | 134 | BLE_address_BE); |
Wolfgang Betz |
90:26c0c9807ab4 | 135 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 136 | |
Wolfgang Betz |
90:26c0c9807ab4 | 137 | ret = aci_gatt_init(); |
Wolfgang Betz |
90:26c0c9807ab4 | 138 | if(ret){ |
Wolfgang Betz |
90:26c0c9807ab4 | 139 | PRINTF("GATT_Init failed.\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 140 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 141 | //GAP is always in PERIPHERAL _ROLE as mbed does not support Master role at the moment |
Wolfgang Betz |
90:26c0c9807ab4 | 142 | #ifdef BLUENRG_MS |
Wolfgang Betz |
90:26c0c9807ab4 | 143 | ret = aci_gap_init(GAP_PERIPHERAL_ROLE, 0, 0x07, &service_handle, &dev_name_char_handle, &appearance_char_handle); |
Wolfgang Betz |
90:26c0c9807ab4 | 144 | #else |
Wolfgang Betz |
90:26c0c9807ab4 | 145 | ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle); |
Wolfgang Betz |
90:26c0c9807ab4 | 146 | #endif |
Wolfgang Betz |
90:26c0c9807ab4 | 147 | if(ret != BLE_STATUS_SUCCESS){ |
Wolfgang Betz |
90:26c0c9807ab4 | 148 | PRINTF("GAP_Init failed.\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 149 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 150 | |
Wolfgang Betz |
90:26c0c9807ab4 | 151 | //ANDREA -- FIXME: Security and passkey set by default |
Wolfgang Betz |
90:26c0c9807ab4 | 152 | ret = aci_gap_set_auth_requirement(MITM_PROTECTION_REQUIRED, |
Wolfgang Betz |
90:26c0c9807ab4 | 153 | OOB_AUTH_DATA_ABSENT, |
Wolfgang Betz |
90:26c0c9807ab4 | 154 | NULL, |
Wolfgang Betz |
90:26c0c9807ab4 | 155 | 7, |
Wolfgang Betz |
90:26c0c9807ab4 | 156 | 16, |
Wolfgang Betz |
90:26c0c9807ab4 | 157 | USE_FIXED_PIN_FOR_PAIRING, |
Wolfgang Betz |
90:26c0c9807ab4 | 158 | 123456, |
Wolfgang Betz |
90:26c0c9807ab4 | 159 | BONDING); |
Wolfgang Betz |
90:26c0c9807ab4 | 160 | if (ret == BLE_STATUS_SUCCESS) { |
Wolfgang Betz |
90:26c0c9807ab4 | 161 | DEBUG("Auth Req set successfully.\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 162 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 163 | |
Wolfgang Betz |
90:26c0c9807ab4 | 164 | aci_hal_set_tx_power_level(1,4); |
Wolfgang Betz |
90:26c0c9807ab4 | 165 | |
Wolfgang Betz |
90:26c0c9807ab4 | 166 | g_gap_service_handle = service_handle; |
Wolfgang Betz |
90:26c0c9807ab4 | 167 | g_appearance_char_handle = appearance_char_handle; |
Wolfgang Betz |
90:26c0c9807ab4 | 168 | g_device_name_char_handle = dev_name_char_handle; |
Wolfgang Betz |
90:26c0c9807ab4 | 169 | //Device Name is set from Accumulate Adv Data Payload or through setDeviceName API |
Wolfgang Betz |
90:26c0c9807ab4 | 170 | /*ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, |
Wolfgang Betz |
90:26c0c9807ab4 | 171 | strlen(name), (tHalUint8 *)name);*/ |
Wolfgang Betz |
90:26c0c9807ab4 | 172 | |
Wolfgang Betz |
90:26c0c9807ab4 | 173 | return; |
Wolfgang Betz |
90:26c0c9807ab4 | 174 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 175 | |
Wolfgang Betz |
90:26c0c9807ab4 | 176 | void User_Process() |
Wolfgang Betz |
90:26c0c9807ab4 | 177 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 178 | if(set_connectable){ |
Wolfgang Betz |
90:26c0c9807ab4 | 179 | setConnectable(); |
Wolfgang Betz |
90:26c0c9807ab4 | 180 | set_connectable = FALSE; |
Wolfgang Betz |
90:26c0c9807ab4 | 181 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 182 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 183 | |
Wolfgang Betz |
90:26c0c9807ab4 | 184 | void setConnectable(void) |
Wolfgang Betz |
90:26c0c9807ab4 | 185 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 186 | tBleStatus ret; |
Wolfgang Betz |
90:26c0c9807ab4 | 187 | |
Wolfgang Betz |
90:26c0c9807ab4 | 188 | const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G'}; |
Wolfgang Betz |
90:26c0c9807ab4 | 189 | |
Wolfgang Betz |
90:26c0c9807ab4 | 190 | /* disable scan response */ |
Wolfgang Betz |
90:26c0c9807ab4 | 191 | hci_le_set_scan_resp_data(0,NULL); |
Wolfgang Betz |
90:26c0c9807ab4 | 192 | |
Wolfgang Betz |
90:26c0c9807ab4 | 193 | //int t = BlueNRGGap::getInstance()::ADV_IND;// advType; |
Wolfgang Betz |
90:26c0c9807ab4 | 194 | |
Wolfgang Betz |
90:26c0c9807ab4 | 195 | ret = aci_gap_set_discoverable(BlueNRGGap::getInstance().ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE, |
Wolfgang Betz |
90:26c0c9807ab4 | 196 | sizeof(local_name), local_name, 0, NULL, 0, 0); |
Wolfgang Betz |
90:26c0c9807ab4 | 197 | if (ret != BLE_STATUS_SUCCESS) { |
Wolfgang Betz |
90:26c0c9807ab4 | 198 | DEBUG("Error while setting discoverable mode (%d)\n", ret); |
Wolfgang Betz |
90:26c0c9807ab4 | 199 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 200 | |
Wolfgang Betz |
90:26c0c9807ab4 | 201 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 202 | |
Wolfgang Betz |
90:26c0c9807ab4 | 203 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 204 | /*! |
Wolfgang Betz |
90:26c0c9807ab4 | 205 | @brief Not Used |
Wolfgang Betz |
90:26c0c9807ab4 | 206 | |
Wolfgang Betz |
90:26c0c9807ab4 | 207 | @param[in] void |
Wolfgang Betz |
90:26c0c9807ab4 | 208 | |
Wolfgang Betz |
90:26c0c9807ab4 | 209 | @returns |
Wolfgang Betz |
90:26c0c9807ab4 | 210 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 211 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 212 | static void btle_handler() |
Wolfgang Betz |
90:26c0c9807ab4 | 213 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 214 | |
Wolfgang Betz |
90:26c0c9807ab4 | 215 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 216 | |
Wolfgang Betz |
90:26c0c9807ab4 | 217 | /*! |
Wolfgang Betz |
90:26c0c9807ab4 | 218 | @brief Not Used |
Wolfgang Betz |
90:26c0c9807ab4 | 219 | |
Wolfgang Betz |
90:26c0c9807ab4 | 220 | @param[in] void |
Wolfgang Betz |
90:26c0c9807ab4 | 221 | |
Wolfgang Betz |
90:26c0c9807ab4 | 222 | @returns |
Wolfgang Betz |
90:26c0c9807ab4 | 223 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 224 | void SPI_Poll(void) |
Wolfgang Betz |
90:26c0c9807ab4 | 225 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 226 | //HAL_GPIO_EXTI_Callback_Poll(BNRG_SPI_EXTI_PIN); |
Wolfgang Betz |
90:26c0c9807ab4 | 227 | return; |
Wolfgang Betz |
90:26c0c9807ab4 | 228 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 229 | |
Wolfgang Betz |
90:26c0c9807ab4 | 230 | #ifdef __cplusplus |
Wolfgang Betz |
90:26c0c9807ab4 | 231 | extern "C" { |
Wolfgang Betz |
90:26c0c9807ab4 | 232 | #endif |
Wolfgang Betz |
90:26c0c9807ab4 | 233 | |
Wolfgang Betz |
90:26c0c9807ab4 | 234 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 235 | /*! |
Wolfgang Betz |
90:26c0c9807ab4 | 236 | @brief Handle HCI Stack Event |
Wolfgang Betz |
90:26c0c9807ab4 | 237 | |
Wolfgang Betz |
90:26c0c9807ab4 | 238 | @param[in] pckt |
Wolfgang Betz |
90:26c0c9807ab4 | 239 | Event Packet sent by the stack to be decoded |
Wolfgang Betz |
90:26c0c9807ab4 | 240 | |
Wolfgang Betz |
90:26c0c9807ab4 | 241 | @returns |
Wolfgang Betz |
90:26c0c9807ab4 | 242 | */ |
Wolfgang Betz |
90:26c0c9807ab4 | 243 | /**************************************************************************/ |
Wolfgang Betz |
90:26c0c9807ab4 | 244 | extern void HCI_Event_CB(void *pckt) { |
Wolfgang Betz |
90:26c0c9807ab4 | 245 | |
Wolfgang Betz |
90:26c0c9807ab4 | 246 | hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt; |
Wolfgang Betz |
90:26c0c9807ab4 | 247 | hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 248 | |
Wolfgang Betz |
90:26c0c9807ab4 | 249 | if(hci_pckt->type != HCI_EVENT_PKT) |
Wolfgang Betz |
90:26c0c9807ab4 | 250 | return; |
Wolfgang Betz |
90:26c0c9807ab4 | 251 | |
Wolfgang Betz |
90:26c0c9807ab4 | 252 | switch(event_pckt->evt){ |
Wolfgang Betz |
90:26c0c9807ab4 | 253 | |
Wolfgang Betz |
90:26c0c9807ab4 | 254 | case EVT_DISCONN_COMPLETE: |
Wolfgang Betz |
90:26c0c9807ab4 | 255 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 256 | DEBUG("EVT_DISCONN_COMPLETE\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 257 | |
Wolfgang Betz |
90:26c0c9807ab4 | 258 | evt_disconn_complete *evt = (evt_disconn_complete*)event_pckt; |
Wolfgang Betz |
90:26c0c9807ab4 | 259 | |
Wolfgang Betz |
90:26c0c9807ab4 | 260 | BlueNRGGap::getInstance().processDisconnectionEvent(evt->handle, BlueNRGGap::REMOTE_USER_TERMINATED_CONNECTION); |
Wolfgang Betz |
90:26c0c9807ab4 | 261 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 262 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 263 | |
Wolfgang Betz |
90:26c0c9807ab4 | 264 | case EVT_LE_META_EVENT: |
Wolfgang Betz |
90:26c0c9807ab4 | 265 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 266 | DEBUG("EVT_LE_META_EVENT\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 267 | |
Wolfgang Betz |
90:26c0c9807ab4 | 268 | evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 269 | |
Wolfgang Betz |
90:26c0c9807ab4 | 270 | switch(evt->subevent){ |
Wolfgang Betz |
90:26c0c9807ab4 | 271 | // ANDREA |
Wolfgang Betz |
90:26c0c9807ab4 | 272 | case EVT_LE_CONN_COMPLETE: |
Wolfgang Betz |
90:26c0c9807ab4 | 273 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 274 | Gap::AddressType_t peerAddrType = Gap::ADDR_TYPE_PUBLIC; |
Wolfgang Betz |
90:26c0c9807ab4 | 275 | DEBUG("EVT_LE_CONN_COMPLETE\n"); |
Wolfgang Betz |
90:26c0c9807ab4 | 276 | |
Wolfgang Betz |
90:26c0c9807ab4 | 277 | evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 278 | |
Wolfgang Betz |
90:26c0c9807ab4 | 279 | BlueNRGGap::getInstance().setConnectionHandle(cc->handle); |
Wolfgang Betz |
90:26c0c9807ab4 | 280 | BlueNRGGap::ConnectionParams_t connectionParams; |
Wolfgang Betz |
90:26c0c9807ab4 | 281 | BlueNRGGap::getInstance().getPreferredConnectionParams(&connectionParams); |
Wolfgang Betz |
90:26c0c9807ab4 | 282 | switch (cc->peer_bdaddr_type) { |
Wolfgang Betz |
90:26c0c9807ab4 | 283 | case PUBLIC_ADDR: |
Wolfgang Betz |
90:26c0c9807ab4 | 284 | peerAddrType = Gap::ADDR_TYPE_PUBLIC; |
Wolfgang Betz |
90:26c0c9807ab4 | 285 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 286 | case STATIC_RANDOM_ADDR: |
Wolfgang Betz |
90:26c0c9807ab4 | 287 | peerAddrType = Gap::ADDR_TYPE_RANDOM_STATIC; |
Wolfgang Betz |
90:26c0c9807ab4 | 288 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 289 | case RESOLVABLE_PRIVATE_ADDR: |
Wolfgang Betz |
90:26c0c9807ab4 | 290 | peerAddrType = Gap::ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE; |
Wolfgang Betz |
90:26c0c9807ab4 | 291 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 292 | case NON_RESOLVABLE_PRIVATE_ADDR: |
Wolfgang Betz |
90:26c0c9807ab4 | 293 | peerAddrType = Gap::ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE; |
Wolfgang Betz |
90:26c0c9807ab4 | 294 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 295 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 296 | BlueNRGGap::getInstance().processConnectionEvent(cc->handle, Gap::PERIPHERAL, peerAddrType, cc->peer_bdaddr, addr_type, bleAddr, (const BlueNRGGap::ConnectionParams_t *)&connectionParams); |
Wolfgang Betz |
90:26c0c9807ab4 | 297 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 298 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 299 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 300 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 301 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 302 | |
Wolfgang Betz |
90:26c0c9807ab4 | 303 | case EVT_VENDOR: |
Wolfgang Betz |
90:26c0c9807ab4 | 304 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 305 | evt_blue_aci *blue_evt = (evt_blue_aci*)event_pckt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 306 | //DEBUG("EVT_VENDOR %d\n", blue_evt->ecode); |
Wolfgang Betz |
90:26c0c9807ab4 | 307 | |
Wolfgang Betz |
90:26c0c9807ab4 | 308 | switch(blue_evt->ecode){ |
Wolfgang Betz |
90:26c0c9807ab4 | 309 | |
Wolfgang Betz |
90:26c0c9807ab4 | 310 | case EVT_BLUE_GATT_READ_PERMIT_REQ: |
Wolfgang Betz |
90:26c0c9807ab4 | 311 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 312 | DEBUG("EVT_BLUE_GATT_READ_PERMIT_REQ_OK\n\r"); |
apalmieri | 97:a89d28ad6092 | 313 | evt_gatt_read_permit_req *pr = (evt_gatt_read_permit_req*)blue_evt->data; |
apalmieri | 97:a89d28ad6092 | 314 | BlueNRGGattServer::getInstance().Read_Request_CB(pr->attr_handle-CHAR_VALUE_OFFSET); |
Wolfgang Betz |
90:26c0c9807ab4 | 315 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 316 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 317 | |
Wolfgang Betz |
90:26c0c9807ab4 | 318 | case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED: |
Wolfgang Betz |
90:26c0c9807ab4 | 319 | { |
Wolfgang Betz |
90:26c0c9807ab4 | 320 | /* this callback is invoked when a GATT attribute is modified |
Wolfgang Betz |
90:26c0c9807ab4 | 321 | extract callback data and pass to suitable handler function */ |
Wolfgang Betz |
90:26c0c9807ab4 | 322 | evt_gatt_attr_modified *evt = (evt_gatt_attr_modified*)blue_evt->data; |
Wolfgang Betz |
90:26c0c9807ab4 | 323 | DEBUG("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED\n\r"); |
Wolfgang Betz |
90:26c0c9807ab4 | 324 | |
Wolfgang Betz |
90:26c0c9807ab4 | 325 | //DEBUG("CharHandle 0x%x, length: 0x%x, Data: 0x%x\n\r",evt->attr_handle, evt->data_length, (uint16_t)evt->att_data[0]); |
apalmieri | 97:a89d28ad6092 | 326 | |
Wolfgang Betz |
90:26c0c9807ab4 | 327 | |
Wolfgang Betz |
90:26c0c9807ab4 | 328 | //Extract the GattCharacteristic from p_characteristics[] and find the properties mask |
Wolfgang Betz |
90:26c0c9807ab4 | 329 | //If the char has handle 'x', then the value declaration will have the handle 'x+1' |
apalmieri | 97:a89d28ad6092 | 330 | /*uint16_t charHandle = evt->attr_handle-CHAR_VALUE_OFFSET;*/ |
apalmieri | 94:2993df23d1ac | 331 | GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(evt->attr_handle); |
Wolfgang Betz |
90:26c0c9807ab4 | 332 | if(p_char!=NULL) { |
apalmieri | 97:a89d28ad6092 | 333 | DEBUG("CharHandle %d, length: %d, Data: %d\n\r",p_char->getValueAttribute().getHandle(), evt->data_length, (uint16_t)evt->att_data[0]); |
Wolfgang Betz |
90:26c0c9807ab4 | 334 | DEBUG("getProperties 0x%x\n\r",p_char->getProperties()); |
Wolfgang Betz |
90:26c0c9807ab4 | 335 | if((p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
Wolfgang Betz |
90:26c0c9807ab4 | 336 | | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) { |
Wolfgang Betz |
90:26c0c9807ab4 | 337 | |
Wolfgang Betz |
90:26c0c9807ab4 | 338 | //Now Check if data written in Enable or Disable |
Wolfgang Betz |
90:26c0c9807ab4 | 339 | if((uint16_t)evt->att_data[0]==1) { |
Wolfgang Betz |
90:26c0c9807ab4 | 340 | //DEBUG("Notify ENABLED\n\r"); |
apalmieri | 97:a89d28ad6092 | 341 | BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_ENABLED, p_char->getValueAttribute().getHandle()/*evt->attr_handle*/); |
Wolfgang Betz |
90:26c0c9807ab4 | 342 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 343 | else { |
Wolfgang Betz |
90:26c0c9807ab4 | 344 | //DEBUG("Notify DISABLED\n\r"); |
apalmieri | 97:a89d28ad6092 | 345 | BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_DISABLED, p_char->getValueAttribute().getHandle()/*evt->attr_handle*/); |
Wolfgang Betz |
90:26c0c9807ab4 | 346 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 347 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 348 | |
Wolfgang Betz |
90:26c0c9807ab4 | 349 | //Check is attr handle property is WRITEABLE, if yes, generate GATT_EVENT_DATA_WRITTEN Event |
Wolfgang Betz |
90:26c0c9807ab4 | 350 | if((p_char->getProperties() & |
Wolfgang Betz |
90:26c0c9807ab4 | 351 | (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE| |
Wolfgang Betz |
90:26c0c9807ab4 | 352 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) { |
Wolfgang Betz |
90:26c0c9807ab4 | 353 | |
Wolfgang Betz |
90:26c0c9807ab4 | 354 | GattWriteCallbackParams writeParams; |
apalmieri | 97:a89d28ad6092 | 355 | writeParams.handle=p_char->getValueAttribute().getHandle();/*evt->attr_handle-CHAR_VALUE_OFFSET;*/ |
Wolfgang Betz |
90:26c0c9807ab4 | 356 | writeParams.writeOp=GattWriteCallbackParams::OP_WRITE_REQ;//Where to find this property in BLUENRG? |
Wolfgang Betz |
90:26c0c9807ab4 | 357 | writeParams.len=evt->data_length; |
Wolfgang Betz |
90:26c0c9807ab4 | 358 | writeParams.data=evt->att_data; |
Wolfgang Betz |
90:26c0c9807ab4 | 359 | #ifdef BLUENRG_MS |
Wolfgang Betz |
90:26c0c9807ab4 | 360 | writeParams.offset=evt->offset;//Not used in BlueNRG? |
Wolfgang Betz |
90:26c0c9807ab4 | 361 | #endif |
Wolfgang Betz |
90:26c0c9807ab4 | 362 | BlueNRGGattServer::getInstance().HCIDataWrittenEvent(&writeParams); |
Wolfgang Betz |
90:26c0c9807ab4 | 363 | |
Wolfgang Betz |
90:26c0c9807ab4 | 364 | //BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_WRITTEN, evt->attr_handle); |
Wolfgang Betz |
90:26c0c9807ab4 | 365 | //Write the actual Data to the Attr Handle? (uint8_1[])evt->att_data contains the data |
Wolfgang Betz |
90:26c0c9807ab4 | 366 | if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) { |
Wolfgang Betz |
90:26c0c9807ab4 | 367 | BlueNRGGattServer::getInstance().write(p_char->getValueAttribute().getHandle(), |
Wolfgang Betz |
90:26c0c9807ab4 | 368 | p_char->getValueAttribute().getValuePtr(), |
Wolfgang Betz |
90:26c0c9807ab4 | 369 | p_char->getValueAttribute().getInitialLength(), |
Wolfgang Betz |
90:26c0c9807ab4 | 370 | false /* localOnly */); |
Wolfgang Betz |
90:26c0c9807ab4 | 371 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 372 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 373 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 374 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 375 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 376 | |
Wolfgang Betz |
90:26c0c9807ab4 | 377 | //Any cases for Data Sent Notifications? |
Wolfgang Betz |
90:26c0c9807ab4 | 378 | case EVT_BLUE_GATT_NOTIFICATION: |
Wolfgang Betz |
90:26c0c9807ab4 | 379 | //This is only relevant for Client Side Event |
Wolfgang Betz |
90:26c0c9807ab4 | 380 | DEBUG("EVT_BLUE_GATT_NOTIFICATION"); |
Wolfgang Betz |
90:26c0c9807ab4 | 381 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 382 | case EVT_BLUE_GATT_INDICATION: |
Wolfgang Betz |
90:26c0c9807ab4 | 383 | //This is only relevant for Client Side Event |
Wolfgang Betz |
90:26c0c9807ab4 | 384 | DEBUG("EVT_BLUE_GATT_INDICATION"); |
Wolfgang Betz |
90:26c0c9807ab4 | 385 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 386 | |
Wolfgang Betz |
90:26c0c9807ab4 | 387 | case EVT_BLUE_GATT_PROCEDURE_COMPLETE: |
Wolfgang Betz |
90:26c0c9807ab4 | 388 | DEBUG("EVT_BLUE_GATT_PROCEDURE_COMPLETE"); |
Wolfgang Betz |
90:26c0c9807ab4 | 389 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 390 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 391 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 392 | break; |
Wolfgang Betz |
90:26c0c9807ab4 | 393 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 394 | |
Wolfgang Betz |
90:26c0c9807ab4 | 395 | return ; |
Wolfgang Betz |
90:26c0c9807ab4 | 396 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 397 | |
Wolfgang Betz |
90:26c0c9807ab4 | 398 | |
Wolfgang Betz |
90:26c0c9807ab4 | 399 | #ifdef __cplusplus |
Wolfgang Betz |
90:26c0c9807ab4 | 400 | } |
Wolfgang Betz |
90:26c0c9807ab4 | 401 | #endif |