ble

Dependencies:   HC_SR04_Ultrasonic_Library Servo mbed

Fork of FIP_REV1 by Robotique FIP

Committer:
julientiron
Date:
Tue May 19 16:43:59 2015 +0000
Revision:
0:3d641e170a74
BLE;

Who changed what in which revision?

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