ble

Dependencies:   HC_SR04_Ultrasonic_Library Servo mbed

Fork of FIP_REV1 by Robotique FIP

Committer:
julientiron
Date:
Thu Jul 09 13:33:36 2015 +0000
Revision:
4:69a35a56ac48
Parent:
2:b5166e24c7a6
BLE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
julientiron 2:b5166e24c7a6 1 /* mbed Microcontroller Library
julientiron 2:b5166e24c7a6 2 * Copyright (c) 2006-2013 ARM Limited
julientiron 2:b5166e24c7a6 3 *
julientiron 2:b5166e24c7a6 4 * Licensed under the Apache License, Version 2.0 (the "License");
julientiron 2:b5166e24c7a6 5 * you may not use this file except in compliance with the License.
julientiron 2:b5166e24c7a6 6 * You may obtain a copy of the License at
julientiron 2:b5166e24c7a6 7 *
julientiron 2:b5166e24c7a6 8 * http://www.apache.org/licenses/LICENSE-2.0
julientiron 2:b5166e24c7a6 9 *
julientiron 2:b5166e24c7a6 10 * Unless required by applicable law or agreed to in writing, software
julientiron 2:b5166e24c7a6 11 * distributed under the License is distributed on an "AS IS" BASIS,
julientiron 2:b5166e24c7a6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
julientiron 2:b5166e24c7a6 13 * See the License for the specific language governing permissions and
julientiron 2:b5166e24c7a6 14 * limitations under the License.
julientiron 2:b5166e24c7a6 15 */
julientiron 2:b5166e24c7a6 16
julientiron 2:b5166e24c7a6 17
julientiron 2:b5166e24c7a6 18 #include "btle.h"
julientiron 2:b5166e24c7a6 19 #include "public/Gap.h"
julientiron 2:b5166e24c7a6 20 #include "public/GapEvents.h"
julientiron 2:b5166e24c7a6 21 #include "BlueNRGGap.h"
julientiron 2:b5166e24c7a6 22 #include "BlueNRGGattServer.h"
julientiron 2:b5166e24c7a6 23 #include "Utils.h"
julientiron 2:b5166e24c7a6 24
julientiron 2:b5166e24c7a6 25 #ifdef __cplusplus
julientiron 2:b5166e24c7a6 26 extern "C" {
julientiron 2:b5166e24c7a6 27 #endif
julientiron 2:b5166e24c7a6 28
julientiron 2:b5166e24c7a6 29
julientiron 2:b5166e24c7a6 30 /* C File Includes ------------------------------------------------------------------*/
julientiron 2:b5166e24c7a6 31 #include "hal_types.h"
julientiron 2:b5166e24c7a6 32 #include "hci.h"
julientiron 2:b5166e24c7a6 33 #include "bluenrg_hci.h"
julientiron 2:b5166e24c7a6 34 #include "gp_timer.h"
julientiron 2:b5166e24c7a6 35 #include "hal.h"
julientiron 2:b5166e24c7a6 36 #include "osal.h"
julientiron 2:b5166e24c7a6 37 #include "hci_internal.h"
julientiron 2:b5166e24c7a6 38 #include "bluenrg_hci_internal.h"
julientiron 2:b5166e24c7a6 39 #include "gap.h"
julientiron 2:b5166e24c7a6 40 #include "sm.h"
julientiron 2:b5166e24c7a6 41 #include <stdio.h>
julientiron 2:b5166e24c7a6 42 #include <string.h>
julientiron 2:b5166e24c7a6 43 #include "role_type.h"
julientiron 2:b5166e24c7a6 44 #include "debug.h"
julientiron 2:b5166e24c7a6 45
julientiron 2:b5166e24c7a6 46 /* SPI handler declaration */
julientiron 2:b5166e24c7a6 47 SPI_HandleTypeDef SpiHandle;
julientiron 2:b5166e24c7a6 48
julientiron 2:b5166e24c7a6 49 #ifdef __cplusplus
julientiron 2:b5166e24c7a6 50 }
julientiron 2:b5166e24c7a6 51 #endif
julientiron 2:b5166e24c7a6 52
julientiron 2:b5166e24c7a6 53
julientiron 2:b5166e24c7a6 54 static void btle_handler(/*ble_evt_t * p_ble_evt*/);
julientiron 2:b5166e24c7a6 55 void HCI_Input(tHciDataPacket * hciReadPacket);
julientiron 2:b5166e24c7a6 56
julientiron 2:b5166e24c7a6 57 //#define BDADDR_SIZE 6
julientiron 2:b5166e24c7a6 58 //tHalUint8 bdaddr[BDADDR_SIZE]= {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02};
julientiron 2:b5166e24c7a6 59
julientiron 2:b5166e24c7a6 60 uint16_t g_gap_service_handle = 0;
julientiron 2:b5166e24c7a6 61 uint16_t g_appearance_char_handle = 0;
julientiron 2:b5166e24c7a6 62 uint16_t g_device_name_char_handle = 0;
julientiron 2:b5166e24c7a6 63
julientiron 2:b5166e24c7a6 64 /* Private variables ---------------------------------------------------------*/
julientiron 2:b5166e24c7a6 65 volatile uint8_t set_connectable = 1;
julientiron 2:b5166e24c7a6 66
julientiron 2:b5166e24c7a6 67 /**************************************************************************/
julientiron 2:b5166e24c7a6 68 /*!
julientiron 2:b5166e24c7a6 69 @brief Initialises BTLE and the underlying HW/Device
julientiron 2:b5166e24c7a6 70
julientiron 2:b5166e24c7a6 71 @returns
julientiron 2:b5166e24c7a6 72 */
julientiron 2:b5166e24c7a6 73 /**************************************************************************/
julientiron 2:b5166e24c7a6 74 void btle_init(bool isSetAddress)
julientiron 2:b5166e24c7a6 75 {
julientiron 2:b5166e24c7a6 76 DEBUG("btle_init>>\n\r");
julientiron 2:b5166e24c7a6 77 const char *name = "FearInProgress";
julientiron 2:b5166e24c7a6 78 tHalUint8 *bleAddr;
julientiron 2:b5166e24c7a6 79 int ret;
julientiron 2:b5166e24c7a6 80 uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
julientiron 2:b5166e24c7a6 81
julientiron 2:b5166e24c7a6 82 HAL_Init();
julientiron 2:b5166e24c7a6 83
julientiron 2:b5166e24c7a6 84 /* Configure the User Button in GPIO Mode */
julientiron 2:b5166e24c7a6 85 BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
julientiron 2:b5166e24c7a6 86
julientiron 2:b5166e24c7a6 87 /* Configure the system clock */
julientiron 2:b5166e24c7a6 88 SystemClock_Config();
julientiron 2:b5166e24c7a6 89
julientiron 2:b5166e24c7a6 90 /* Delay needed only to be able to acces the JTAG interface after reset
julientiron 2:b5166e24c7a6 91 if it will be disabled later. */
julientiron 2:b5166e24c7a6 92 Clock_Wait(500);
julientiron 2:b5166e24c7a6 93
julientiron 2:b5166e24c7a6 94 /* Initialize the BlueNRG SPI driver */
julientiron 2:b5166e24c7a6 95 BNRG_SPI_Init();
julientiron 2:b5166e24c7a6 96
julientiron 2:b5166e24c7a6 97 /* Initialize the BlueNRG HCI */
julientiron 2:b5166e24c7a6 98 HCI_Init();
julientiron 2:b5166e24c7a6 99
julientiron 2:b5166e24c7a6 100 /* Reset BlueNRG SPI interface */
julientiron 2:b5166e24c7a6 101 BlueNRG_RST();
julientiron 2:b5166e24c7a6 102
julientiron 2:b5166e24c7a6 103 /* The Nucleo board must be configured as SERVER */
julientiron 2:b5166e24c7a6 104 //check if issetAddress is set than set address.
julientiron 2:b5166e24c7a6 105 if(isSetAddress)
julientiron 2:b5166e24c7a6 106 {
julientiron 2:b5166e24c7a6 107 bleAddr = BlueNRGGap::getInstance().getAddress();
julientiron 2:b5166e24c7a6 108
julientiron 2:b5166e24c7a6 109 tHalUint8 bdaddr[BDADDR_SIZE];
julientiron 2:b5166e24c7a6 110 Osal_MemCpy(bdaddr, bleAddr, BDADDR_SIZE);
julientiron 2:b5166e24c7a6 111
julientiron 2:b5166e24c7a6 112 ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET,
julientiron 2:b5166e24c7a6 113 CONFIG_DATA_PUBADDR_LEN,
julientiron 2:b5166e24c7a6 114 bdaddr);
julientiron 2:b5166e24c7a6 115 }
julientiron 2:b5166e24c7a6 116
julientiron 2:b5166e24c7a6 117 ret = aci_gatt_init();
julientiron 2:b5166e24c7a6 118 //GAP is always in PERIPHERAL _ROLE as mbed does not support Master role at the moment
julientiron 2:b5166e24c7a6 119 ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle);
julientiron 2:b5166e24c7a6 120
julientiron 2:b5166e24c7a6 121 g_gap_service_handle = service_handle;
julientiron 2:b5166e24c7a6 122 g_appearance_char_handle = appearance_char_handle;
julientiron 2:b5166e24c7a6 123 g_device_name_char_handle = dev_name_char_handle;
julientiron 2:b5166e24c7a6 124 /*ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0,
julientiron 2:b5166e24c7a6 125 strlen(name), (tHalUint8 *)name);*/
julientiron 2:b5166e24c7a6 126
julientiron 2:b5166e24c7a6 127 return;
julientiron 2:b5166e24c7a6 128 }
julientiron 2:b5166e24c7a6 129
julientiron 2:b5166e24c7a6 130 void User_Process()
julientiron 2:b5166e24c7a6 131 {
julientiron 2:b5166e24c7a6 132 if(set_connectable){
julientiron 2:b5166e24c7a6 133 setConnectable();
julientiron 2:b5166e24c7a6 134 set_connectable = FALSE;
julientiron 2:b5166e24c7a6 135 }
julientiron 2:b5166e24c7a6 136 }
julientiron 2:b5166e24c7a6 137
julientiron 2:b5166e24c7a6 138 void setConnectable(void)
julientiron 2:b5166e24c7a6 139 {
julientiron 2:b5166e24c7a6 140 tBleStatus ret;
julientiron 2:b5166e24c7a6 141
julientiron 4:69a35a56ac48 142 const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'F','I','P'};
julientiron 2:b5166e24c7a6 143
julientiron 2:b5166e24c7a6 144 /* disable scan response */
julientiron 2:b5166e24c7a6 145 hci_le_set_scan_resp_data(0,NULL);
julientiron 2:b5166e24c7a6 146
julientiron 2:b5166e24c7a6 147
julientiron 2:b5166e24c7a6 148 ret = aci_gap_set_discoverable(ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE,
julientiron 2:b5166e24c7a6 149 8, local_name, 0, NULL, 0, 0);
julientiron 2:b5166e24c7a6 150
julientiron 2:b5166e24c7a6 151 }
julientiron 2:b5166e24c7a6 152
julientiron 2:b5166e24c7a6 153 /**************************************************************************/
julientiron 2:b5166e24c7a6 154 /*!
julientiron 2:b5166e24c7a6 155 @brief
julientiron 2:b5166e24c7a6 156
julientiron 2:b5166e24c7a6 157 @param[in] p_ble_evt
julientiron 2:b5166e24c7a6 158
julientiron 2:b5166e24c7a6 159 @returns
julientiron 2:b5166e24c7a6 160 */
julientiron 2:b5166e24c7a6 161 /**************************************************************************/
julientiron 2:b5166e24c7a6 162 static void btle_handler()
julientiron 2:b5166e24c7a6 163 {
julientiron 2:b5166e24c7a6 164
julientiron 2:b5166e24c7a6 165 }
julientiron 2:b5166e24c7a6 166
julientiron 2:b5166e24c7a6 167
julientiron 2:b5166e24c7a6 168 #ifdef __cplusplus
julientiron 2:b5166e24c7a6 169 extern "C" {
julientiron 2:b5166e24c7a6 170 #endif
julientiron 2:b5166e24c7a6 171
julientiron 2:b5166e24c7a6 172 extern void HCI_Event_CB(void *pckt) {
julientiron 2:b5166e24c7a6 173
julientiron 2:b5166e24c7a6 174 hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt;
julientiron 2:b5166e24c7a6 175 hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data;
julientiron 2:b5166e24c7a6 176
julientiron 2:b5166e24c7a6 177 if(hci_pckt->type != HCI_EVENT_PKT)
julientiron 2:b5166e24c7a6 178 return;
julientiron 2:b5166e24c7a6 179
julientiron 2:b5166e24c7a6 180 switch(event_pckt->evt){
julientiron 2:b5166e24c7a6 181
julientiron 2:b5166e24c7a6 182 case EVT_DISCONN_COMPLETE:
julientiron 2:b5166e24c7a6 183 {
julientiron 2:b5166e24c7a6 184 evt_disconn_complete *evt = (evt_disconn_complete*)event_pckt;
julientiron 2:b5166e24c7a6 185
julientiron 2:b5166e24c7a6 186 BlueNRGGap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_DISCONNECTED, evt->handle);
julientiron 2:b5166e24c7a6 187 }
julientiron 2:b5166e24c7a6 188 break;
julientiron 2:b5166e24c7a6 189
julientiron 2:b5166e24c7a6 190 case EVT_LE_META_EVENT:
julientiron 2:b5166e24c7a6 191 {
julientiron 2:b5166e24c7a6 192 evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data;
julientiron 2:b5166e24c7a6 193
julientiron 2:b5166e24c7a6 194 switch(evt->subevent){
julientiron 2:b5166e24c7a6 195 case EVT_LE_CONN_COMPLETE:
julientiron 2:b5166e24c7a6 196 {
julientiron 2:b5166e24c7a6 197 evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data;
julientiron 2:b5166e24c7a6 198
julientiron 2:b5166e24c7a6 199 BlueNRGGap::getInstance().setConnectionHandle(cc->handle);
julientiron 2:b5166e24c7a6 200 BlueNRGGap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_CONNECTED, cc->handle);
julientiron 2:b5166e24c7a6 201 }
julientiron 2:b5166e24c7a6 202 break;
julientiron 2:b5166e24c7a6 203 }
julientiron 2:b5166e24c7a6 204 }
julientiron 2:b5166e24c7a6 205 break;
julientiron 2:b5166e24c7a6 206
julientiron 2:b5166e24c7a6 207 case EVT_VENDOR:
julientiron 2:b5166e24c7a6 208 {
julientiron 2:b5166e24c7a6 209 evt_blue_aci *blue_evt = (evt_blue_aci*)event_pckt->data;
julientiron 2:b5166e24c7a6 210 switch(blue_evt->ecode){
julientiron 2:b5166e24c7a6 211
julientiron 2:b5166e24c7a6 212 case EVT_BLUE_GATT_READ_PERMIT_REQ:
julientiron 2:b5166e24c7a6 213 {
julientiron 2:b5166e24c7a6 214 DEBUG("EVT_BLUE_GATT_READ_PERMIT_REQ_OK\n\r");
julientiron 2:b5166e24c7a6 215 evt_gatt_read_permit_req *pr = (evt_gatt_read_permit_req*)blue_evt->data;
julientiron 2:b5166e24c7a6 216 BlueNRGGattServer::getInstance().Read_Request_CB(pr->attr_handle);
julientiron 2:b5166e24c7a6 217 }
julientiron 2:b5166e24c7a6 218 break;
julientiron 2:b5166e24c7a6 219
julientiron 2:b5166e24c7a6 220 case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED:
julientiron 2:b5166e24c7a6 221 {
julientiron 2:b5166e24c7a6 222 /* this callback is invoked when a GATT attribute is modified
julientiron 2:b5166e24c7a6 223 extract callback data and pass to suitable handler function */
julientiron 2:b5166e24c7a6 224 evt_gatt_attr_modified *evt = (evt_gatt_attr_modified*)blue_evt->data;
julientiron 2:b5166e24c7a6 225 DEBUG("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED\n\r");
julientiron 2:b5166e24c7a6 226 DEBUG("CharHandle 0x%x, length: 0x%x, Data: 0x%x\n\r",evt->attr_handle, evt->data_length, (uint16_t)evt->att_data[0]);
julientiron 2:b5166e24c7a6 227
julientiron 2:b5166e24c7a6 228
julientiron 2:b5166e24c7a6 229 //Extract the GattCharacteristic from p_characteristics[] and find the properties mask
julientiron 2:b5166e24c7a6 230 GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(evt->attr_handle);
julientiron 2:b5166e24c7a6 231 DEBUG("getProperties 0x%x\n\r",p_char->getProperties());
julientiron 2:b5166e24c7a6 232 if((p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY
julientiron 2:b5166e24c7a6 233 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) {
julientiron 2:b5166e24c7a6 234
julientiron 2:b5166e24c7a6 235 //Now Check if data written in Enable or Disable
julientiron 2:b5166e24c7a6 236 if((uint16_t)evt->att_data[0]==1) {
julientiron 2:b5166e24c7a6 237 //DEBUG("Notify ENABLED\n\r");
julientiron 2:b5166e24c7a6 238 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_UPDATES_ENABLED, evt->attr_handle);
julientiron 2:b5166e24c7a6 239 }
julientiron 2:b5166e24c7a6 240 else {
julientiron 2:b5166e24c7a6 241 //DEBUG("Notify DISABLED\n\r");
julientiron 2:b5166e24c7a6 242 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_UPDATES_DISABLED, evt->attr_handle);
julientiron 2:b5166e24c7a6 243 }
julientiron 2:b5166e24c7a6 244 }
julientiron 2:b5166e24c7a6 245
julientiron 2:b5166e24c7a6 246 //Check is attr handle property is WRITEABLE, if yes, generate GATT_EVENT_DATA_WRITTEN Event
julientiron 2:b5166e24c7a6 247 if((p_char->getProperties() &
julientiron 2:b5166e24c7a6 248 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
julientiron 2:b5166e24c7a6 249 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) {
julientiron 2:b5166e24c7a6 250
julientiron 2:b5166e24c7a6 251 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_WRITTEN, evt->attr_handle);
julientiron 2:b5166e24c7a6 252 //Write the actual Data to the Attr Handle? (uint8_1[])evt->att_data contains the data
julientiron 4:69a35a56ac48 253 //handleUpdated = evt->attr_handle;
julientiron 4:69a35a56ac48 254 //dataUpdated = evt->att_data;
julientiron 2:b5166e24c7a6 255 if ((p_char->getValuePtr() != NULL) && (p_char->getInitialLength() > 0)) {
julientiron 4:69a35a56ac48 256 BlueNRGGattServer::getInstance().updateValue(p_char->getHandle(), evt->att_data, p_char->getInitialLength(), false /* localOnly */);
julientiron 4:69a35a56ac48 257 DEBUG("%d", p_char->getValuePtr());
julientiron 2:b5166e24c7a6 258 }
julientiron 2:b5166e24c7a6 259 }
julientiron 2:b5166e24c7a6 260 }
julientiron 2:b5166e24c7a6 261 break;
julientiron 2:b5166e24c7a6 262
julientiron 2:b5166e24c7a6 263 //Any cases for Data Sent Notifications?
julientiron 2:b5166e24c7a6 264 case EVT_BLUE_GATT_NOTIFICATION:
julientiron 2:b5166e24c7a6 265 //This is only relevant for Client Side Event
julientiron 2:b5166e24c7a6 266 DEBUG("EVT_BLUE_GATT_NOTIFICATION");
julientiron 2:b5166e24c7a6 267 break;
julientiron 2:b5166e24c7a6 268 case EVT_BLUE_GATT_INDICATION:
julientiron 2:b5166e24c7a6 269 //This is only relevant for Client Side Event
julientiron 2:b5166e24c7a6 270 DEBUG("EVT_BLUE_GATT_INDICATION");
julientiron 2:b5166e24c7a6 271 break;
julientiron 2:b5166e24c7a6 272
julientiron 2:b5166e24c7a6 273 case EVT_BLUE_GATT_PROCEDURE_COMPLETE:
julientiron 2:b5166e24c7a6 274 DEBUG("EVT_BLUE_GATT_PROCEDURE_COMPLETE");
julientiron 2:b5166e24c7a6 275 break;
julientiron 2:b5166e24c7a6 276 }
julientiron 2:b5166e24c7a6 277 }
julientiron 2:b5166e24c7a6 278 break;
julientiron 2:b5166e24c7a6 279 }
julientiron 2:b5166e24c7a6 280
julientiron 2:b5166e24c7a6 281 return ;
julientiron 2:b5166e24c7a6 282 }
julientiron 2:b5166e24c7a6 283
julientiron 2:b5166e24c7a6 284
julientiron 2:b5166e24c7a6 285 #ifdef __cplusplus
julientiron 2:b5166e24c7a6 286 }
julientiron 2:b5166e24c7a6 287 #endif