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:
apalmieri
Date:
Thu Jul 09 08:38:06 2015 +0000
Revision:
84:2beeb1cc9d57
Parent:
78:13e75d352d53
Fix bug on searching Char by handle

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