Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Hello_BLE F446RE-BLE
Fork of X_NUCLEO_IDB0XA1 by
btle/src/btle.cpp@20:718d56821672, 2014-08-13 (annotated)
- Committer:
- mridup
- Date:
- Wed Aug 13 08:14:50 2014 +0000
- Revision:
- 20:718d56821672
- Parent:
- 14:baa7a1464517
- Child:
- 23:72e56ddfad5b
Support for Attr Modify and correction of Characteristic Addition bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:309c845d289d | 1 | /* mbed Microcontroller Library |
mridup | 0:309c845d289d | 2 | * Copyright (c) 2006-2013 ARM Limited |
mridup | 0:309c845d289d | 3 | * |
mridup | 0:309c845d289d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mridup | 0:309c845d289d | 5 | * you may not use this file except in compliance with the License. |
mridup | 0:309c845d289d | 6 | * You may obtain a copy of the License at |
mridup | 0:309c845d289d | 7 | * |
mridup | 0:309c845d289d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mridup | 0:309c845d289d | 9 | * |
mridup | 0:309c845d289d | 10 | * Unless required by applicable law or agreed to in writing, software |
mridup | 0:309c845d289d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mridup | 0:309c845d289d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mridup | 0:309c845d289d | 13 | * See the License for the specific language governing permissions and |
mridup | 0:309c845d289d | 14 | * limitations under the License. |
mridup | 0:309c845d289d | 15 | */ |
mridup | 0:309c845d289d | 16 | |
mridup | 0:309c845d289d | 17 | |
mridup | 0:309c845d289d | 18 | #include "btle.h" |
mridup | 4:fa5b5693c1b5 | 19 | #include "hw/Gap.h" |
mridup | 4:fa5b5693c1b5 | 20 | #include "hw/GapEvents.h" |
mridup | 4:fa5b5693c1b5 | 21 | #include "BlueNRGGap.h" |
mridup | 4:fa5b5693c1b5 | 22 | #include "BlueNRGGattServer.h" |
mridup | 20:718d56821672 | 23 | #include "Utils.h" |
mridup | 0:309c845d289d | 24 | |
mridup | 0:309c845d289d | 25 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 26 | extern "C" { |
mridup | 0:309c845d289d | 27 | #endif |
mridup | 0:309c845d289d | 28 | |
mridup | 0:309c845d289d | 29 | /* C File Includes ------------------------------------------------------------------*/ |
mridup | 0:309c845d289d | 30 | #include "hal_types.h" |
mridup | 0:309c845d289d | 31 | #include "hci.h" |
mridup | 0:309c845d289d | 32 | #include "bluenrg_hci.h" |
mridup | 0:309c845d289d | 33 | #include "gp_timer.h" |
mridup | 0:309c845d289d | 34 | #include "hal.h" |
mridup | 0:309c845d289d | 35 | #include "osal.h" |
mridup | 0:309c845d289d | 36 | #include "hci_internal.h" |
mridup | 0:309c845d289d | 37 | #include "bluenrg_hci_internal.h" |
mridup | 0:309c845d289d | 38 | #include "gap.h" |
mridup | 0:309c845d289d | 39 | #include "sm.h" |
mridup | 0:309c845d289d | 40 | #include <stdio.h> |
mridup | 0:309c845d289d | 41 | #include <string.h> |
mridup | 0:309c845d289d | 42 | #include <stm32l0xx_bluenrg_shield_bsp.h> |
mridup | 0:309c845d289d | 43 | #include "role_type.h" |
mridup | 0:309c845d289d | 44 | #include "debug.h" |
mridup | 0:309c845d289d | 45 | |
mridup | 0:309c845d289d | 46 | /* SPI handler declaration */ |
mridup | 0:309c845d289d | 47 | SPI_HandleTypeDef SpiHandle; |
mridup | 0:309c845d289d | 48 | |
mridup | 0:309c845d289d | 49 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 50 | } |
mridup | 0:309c845d289d | 51 | #endif |
mridup | 0:309c845d289d | 52 | |
mridup | 0:309c845d289d | 53 | |
mridup | 0:309c845d289d | 54 | static void btle_handler(/*ble_evt_t * p_ble_evt*/); |
mridup | 0:309c845d289d | 55 | void HCI_Input(tHciDataPacket * hciReadPacket); |
mridup | 0:309c845d289d | 56 | |
mridup | 0:309c845d289d | 57 | #define BDADDR_SIZE 6 |
mridup | 0:309c845d289d | 58 | tHalUint8 bdaddr[BDADDR_SIZE]= {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02}; |
mridup | 0:309c845d289d | 59 | |
hemddabral | 14:baa7a1464517 | 60 | uint16_t g_gap_service_handle = 0; |
hemddabral | 14:baa7a1464517 | 61 | uint16_t g_appearance_char_handle = 0; |
hemddabral | 14:baa7a1464517 | 62 | uint16_t g_device_name_char_handle = 0; |
hemddabral | 14:baa7a1464517 | 63 | |
mridup | 0:309c845d289d | 64 | /* Private variables ---------------------------------------------------------*/ |
mridup | 0:309c845d289d | 65 | volatile uint8_t set_connectable = 1; |
mridup | 0:309c845d289d | 66 | |
mridup | 0:309c845d289d | 67 | /**************************************************************************/ |
mridup | 0:309c845d289d | 68 | /*! |
mridup | 20:718d56821672 | 69 | @brief Initialises BTLE and the underlying HW/Device |
mridup | 0:309c845d289d | 70 | |
mridup | 0:309c845d289d | 71 | @returns |
mridup | 0:309c845d289d | 72 | */ |
mridup | 0:309c845d289d | 73 | /**************************************************************************/ |
mridup | 0:309c845d289d | 74 | void btle_init(void) |
mridup | 0:309c845d289d | 75 | { |
hemddabral | 14:baa7a1464517 | 76 | const char *name = "BlueNRG_1"; |
mridup | 0:309c845d289d | 77 | tHalUint8 SERVER_BDADDR[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02};//MPD: This is the public address of the Server/Client |
mridup | 0:309c845d289d | 78 | tHalUint8 bdaddr[BDADDR_SIZE]; |
mridup | 0:309c845d289d | 79 | int ret; |
mridup | 0:309c845d289d | 80 | uint16_t service_handle, dev_name_char_handle, appearance_char_handle; |
mridup | 0:309c845d289d | 81 | |
mridup | 0:309c845d289d | 82 | HAL_Init(); |
mridup | 0:309c845d289d | 83 | |
mridup | 0:309c845d289d | 84 | /* Configure the User Button in GPIO Mode */ |
mridup | 0:309c845d289d | 85 | BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); |
mridup | 0:309c845d289d | 86 | |
mridup | 0:309c845d289d | 87 | /* Configure the system clock */ |
mridup | 0:309c845d289d | 88 | SystemClock_Config(); |
mridup | 0:309c845d289d | 89 | |
mridup | 0:309c845d289d | 90 | /* Delay needed only to be able to acces the JTAG interface after reset |
mridup | 0:309c845d289d | 91 | if it will be disabled later. */ |
mridup | 0:309c845d289d | 92 | Clock_Wait(500); |
mridup | 0:309c845d289d | 93 | |
mridup | 0:309c845d289d | 94 | /* Initialize the BlueNRG SPI driver */ |
mridup | 0:309c845d289d | 95 | BNRG_SPI_Init(); |
mridup | 0:309c845d289d | 96 | |
mridup | 0:309c845d289d | 97 | /* Initialize the BlueNRG HCI */ |
mridup | 0:309c845d289d | 98 | HCI_Init(); |
mridup | 0:309c845d289d | 99 | |
mridup | 0:309c845d289d | 100 | /* Reset BlueNRG SPI interface */ |
mridup | 0:309c845d289d | 101 | BlueNRG_RST(); |
mridup | 0:309c845d289d | 102 | |
mridup | 0:309c845d289d | 103 | /* The Nucleo board must be configured as SERVER */ |
mridup | 2:a2b623661316 | 104 | /*Osal_MemCpy(bdaddr, SERVER_BDADDR, sizeof(SERVER_BDADDR)); |
mridup | 0:309c845d289d | 105 | |
mridup | 0:309c845d289d | 106 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
mridup | 0:309c845d289d | 107 | CONFIG_DATA_PUBADDR_LEN, |
mridup | 2:a2b623661316 | 108 | bdaddr);*/ |
mridup | 0:309c845d289d | 109 | |
mridup | 0:309c845d289d | 110 | ret = aci_gatt_init(); |
mridup | 2:a2b623661316 | 111 | //GAP is always in PERIPHERAL _ROLE as mbed does not support Master role at the moment |
mridup | 0:309c845d289d | 112 | ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle); |
hemddabral | 14:baa7a1464517 | 113 | |
hemddabral | 14:baa7a1464517 | 114 | g_gap_service_handle = service_handle; |
hemddabral | 14:baa7a1464517 | 115 | g_appearance_char_handle = appearance_char_handle; |
hemddabral | 14:baa7a1464517 | 116 | g_device_name_char_handle = dev_name_char_handle; |
mridup | 0:309c845d289d | 117 | ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, |
mridup | 0:309c845d289d | 118 | strlen(name), (tHalUint8 *)name); |
mridup | 0:309c845d289d | 119 | |
mridup | 0:309c845d289d | 120 | return; |
mridup | 0:309c845d289d | 121 | } |
mridup | 0:309c845d289d | 122 | |
mridup | 0:309c845d289d | 123 | void User_Process() |
mridup | 0:309c845d289d | 124 | { |
mridup | 0:309c845d289d | 125 | if(set_connectable){ |
mridup | 0:309c845d289d | 126 | setConnectable(); |
mridup | 0:309c845d289d | 127 | set_connectable = FALSE; |
mridup | 0:309c845d289d | 128 | } |
mridup | 0:309c845d289d | 129 | } |
mridup | 0:309c845d289d | 130 | |
mridup | 0:309c845d289d | 131 | void setConnectable(void) |
mridup | 0:309c845d289d | 132 | { |
mridup | 0:309c845d289d | 133 | tBleStatus ret; |
mridup | 0:309c845d289d | 134 | |
hemddabral | 14:baa7a1464517 | 135 | const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G', '1', '2'}; |
mridup | 0:309c845d289d | 136 | |
mridup | 0:309c845d289d | 137 | /* disable scan response */ |
mridup | 0:309c845d289d | 138 | hci_le_set_scan_resp_data(0,NULL); |
mridup | 0:309c845d289d | 139 | |
mridup | 0:309c845d289d | 140 | |
mridup | 0:309c845d289d | 141 | ret = aci_gap_set_discoverable(ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE, |
mridup | 0:309c845d289d | 142 | 8, local_name, 0, NULL, 0, 0); |
mridup | 0:309c845d289d | 143 | |
mridup | 0:309c845d289d | 144 | } |
mridup | 0:309c845d289d | 145 | |
mridup | 0:309c845d289d | 146 | /**************************************************************************/ |
mridup | 0:309c845d289d | 147 | /*! |
mridup | 0:309c845d289d | 148 | @brief |
mridup | 0:309c845d289d | 149 | |
mridup | 0:309c845d289d | 150 | @param[in] p_ble_evt |
mridup | 0:309c845d289d | 151 | |
mridup | 0:309c845d289d | 152 | @returns |
mridup | 0:309c845d289d | 153 | */ |
mridup | 0:309c845d289d | 154 | /**************************************************************************/ |
mridup | 6:08cfc94b5f49 | 155 | static void btle_handler() |
mridup | 0:309c845d289d | 156 | { |
mridup | 0:309c845d289d | 157 | |
mridup | 0:309c845d289d | 158 | } |
mridup | 0:309c845d289d | 159 | |
mridup | 0:309c845d289d | 160 | |
mridup | 0:309c845d289d | 161 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 162 | extern "C" { |
mridup | 0:309c845d289d | 163 | #endif |
mridup | 0:309c845d289d | 164 | |
mridup | 3:9c4c13795643 | 165 | extern void HCI_Event_CB(void *pckt) { |
mridup | 3:9c4c13795643 | 166 | |
mridup | 3:9c4c13795643 | 167 | hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt; |
mridup | 3:9c4c13795643 | 168 | hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data; |
mridup | 0:309c845d289d | 169 | |
mridup | 3:9c4c13795643 | 170 | if(hci_pckt->type != HCI_EVENT_PKT) |
mridup | 3:9c4c13795643 | 171 | return; |
mridup | 3:9c4c13795643 | 172 | |
mridup | 3:9c4c13795643 | 173 | switch(event_pckt->evt){ |
mridup | 3:9c4c13795643 | 174 | |
mridup | 3:9c4c13795643 | 175 | case EVT_DISCONN_COMPLETE: |
mridup | 3:9c4c13795643 | 176 | { |
mridup | 4:fa5b5693c1b5 | 177 | BlueNRGGap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED); |
mridup | 3:9c4c13795643 | 178 | } |
mridup | 3:9c4c13795643 | 179 | break; |
mridup | 3:9c4c13795643 | 180 | |
mridup | 3:9c4c13795643 | 181 | case EVT_LE_META_EVENT: |
mridup | 3:9c4c13795643 | 182 | { |
mridup | 3:9c4c13795643 | 183 | evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data; |
mridup | 3:9c4c13795643 | 184 | |
mridup | 3:9c4c13795643 | 185 | switch(evt->subevent){ |
mridup | 3:9c4c13795643 | 186 | case EVT_LE_CONN_COMPLETE: |
mridup | 4:fa5b5693c1b5 | 187 | { |
mridup | 4:fa5b5693c1b5 | 188 | evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data; |
mridup | 4:fa5b5693c1b5 | 189 | |
mridup | 4:fa5b5693c1b5 | 190 | BlueNRGGap::getInstance().setConnectionHandle(cc->handle); |
mridup | 4:fa5b5693c1b5 | 191 | BlueNRGGap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED); |
mridup | 4:fa5b5693c1b5 | 192 | |
mridup | 3:9c4c13795643 | 193 | } |
mridup | 3:9c4c13795643 | 194 | break; |
mridup | 3:9c4c13795643 | 195 | } |
mridup | 3:9c4c13795643 | 196 | } |
mridup | 3:9c4c13795643 | 197 | break; |
mridup | 3:9c4c13795643 | 198 | |
mridup | 3:9c4c13795643 | 199 | case EVT_VENDOR: |
mridup | 3:9c4c13795643 | 200 | { |
mridup | 3:9c4c13795643 | 201 | evt_blue_aci *blue_evt = (evt_blue_aci*)event_pckt->data; |
mridup | 3:9c4c13795643 | 202 | switch(blue_evt->ecode){ |
mridup | 3:9c4c13795643 | 203 | |
mridup | 3:9c4c13795643 | 204 | case EVT_BLUE_GATT_READ_PERMIT_REQ: |
mridup | 3:9c4c13795643 | 205 | { |
mridup | 3:9c4c13795643 | 206 | //evt_gatt_read_permit_req *pr = (void*)blue_evt->data; |
mridup | 20:718d56821672 | 207 | //Read_Request_CB(pr->attr_handle); |
mridup | 20:718d56821672 | 208 | DEBUG("EVT_BLUE_GATT_READ_PERMIT_REQ\n\r"); |
mridup | 3:9c4c13795643 | 209 | } |
mridup | 3:9c4c13795643 | 210 | break; |
mridup | 20:718d56821672 | 211 | |
mridup | 20:718d56821672 | 212 | case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED: |
mridup | 20:718d56821672 | 213 | { |
mridup | 20:718d56821672 | 214 | /* this callback is invoked when a GATT attribute is modified |
mridup | 20:718d56821672 | 215 | extract callback data and pass to suitable handler function */ |
mridup | 20:718d56821672 | 216 | evt_gatt_attr_modified *evt = (evt_gatt_attr_modified*)blue_evt->data; |
mridup | 20:718d56821672 | 217 | DEBUG("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED\n\r"); |
mridup | 20:718d56821672 | 218 | |
mridup | 20:718d56821672 | 219 | DEBUG("CharHandle 0x%x, Data: 0x%x\n\r",evt->attr_handle, evt->att_data); |
mridup | 20:718d56821672 | 220 | //Attribute_Modified_CB(evt->attr_handle, evt->data_length, evt->att_data); |
mridup | 20:718d56821672 | 221 | } |
mridup | 20:718d56821672 | 222 | break; |
mridup | 20:718d56821672 | 223 | |
mridup | 20:718d56821672 | 224 | //Any cases for Data Sent Notifications? |
mridup | 20:718d56821672 | 225 | case EVT_BLUE_GATT_NOTIFICATION: |
mridup | 20:718d56821672 | 226 | DEBUG("EVT_BLUE_GATT_NOTIFICATION"); |
mridup | 20:718d56821672 | 227 | break; |
mridup | 20:718d56821672 | 228 | case EVT_BLUE_GATT_INDICATION: |
mridup | 20:718d56821672 | 229 | DEBUG("EVT_BLUE_GATT_INDICATION"); |
mridup | 20:718d56821672 | 230 | break; |
mridup | 3:9c4c13795643 | 231 | } |
mridup | 3:9c4c13795643 | 232 | } |
mridup | 3:9c4c13795643 | 233 | break; |
mridup | 3:9c4c13795643 | 234 | } |
mridup | 0:309c845d289d | 235 | |
mridup | 0:309c845d289d | 236 | return ; |
mridup | 3:9c4c13795643 | 237 | } |
mridup | 3:9c4c13795643 | 238 | |
mridup | 3:9c4c13795643 | 239 | |
mridup | 0:309c845d289d | 240 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 241 | } |
mridup | 0:309c845d289d | 242 | #endif |