HW layer for the Nucleo board, it only work with old BLE_API

Dependents:   Hello_BLE F446RE-BLE

Fork of X_NUCLEO_IDB0XA1 by ST

Committer:
Silvio Lucio Oliva
Date:
Mon Jun 08 17:08:01 2015 +0200
Revision:
70:d20d30f59b1c
Child:
74:c4526f8284d5
Initial commit for mbed support

Signed-off-by: Silvio Lucio Oliva <silvio.oliva@st.com>

Who changed what in which revision?

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