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@14:baa7a1464517, 2014-08-06 (annotated)
- Committer:
- hemddabral
- Date:
- Wed Aug 06 11:00:23 2014 +0000
- Revision:
- 14:baa7a1464517
- Parent:
- 6:08cfc94b5f49
- Child:
- 20:718d56821672
added support for advertising device appearance
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 | 0:309c845d289d | 23 | |
mridup | 0:309c845d289d | 24 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 25 | extern "C" { |
mridup | 0:309c845d289d | 26 | #endif |
mridup | 0:309c845d289d | 27 | |
mridup | 0:309c845d289d | 28 | /* C File Includes ------------------------------------------------------------------*/ |
mridup | 0:309c845d289d | 29 | #include "hal_types.h" |
mridup | 0:309c845d289d | 30 | #include "hci.h" |
mridup | 0:309c845d289d | 31 | #include "bluenrg_hci.h" |
mridup | 0:309c845d289d | 32 | #include "gp_timer.h" |
mridup | 0:309c845d289d | 33 | #include "hal.h" |
mridup | 0:309c845d289d | 34 | #include "osal.h" |
mridup | 0:309c845d289d | 35 | #include "hci_internal.h" |
mridup | 0:309c845d289d | 36 | #include "bluenrg_hci_internal.h" |
mridup | 0:309c845d289d | 37 | #include "gap.h" |
mridup | 0:309c845d289d | 38 | #include "sm.h" |
mridup | 0:309c845d289d | 39 | #include <stdio.h> |
mridup | 0:309c845d289d | 40 | #include <string.h> |
mridup | 0:309c845d289d | 41 | #include <stm32l0xx_bluenrg_shield_bsp.h> |
mridup | 0:309c845d289d | 42 | #include "role_type.h" |
mridup | 0:309c845d289d | 43 | #include "debug.h" |
mridup | 0:309c845d289d | 44 | |
mridup | 0:309c845d289d | 45 | /* SPI handler declaration */ |
mridup | 0:309c845d289d | 46 | SPI_HandleTypeDef SpiHandle; |
mridup | 0:309c845d289d | 47 | |
mridup | 0:309c845d289d | 48 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 49 | } |
mridup | 0:309c845d289d | 50 | #endif |
mridup | 0:309c845d289d | 51 | |
mridup | 0:309c845d289d | 52 | |
mridup | 0:309c845d289d | 53 | static void btle_handler(/*ble_evt_t * p_ble_evt*/); |
mridup | 0:309c845d289d | 54 | void HCI_Input(tHciDataPacket * hciReadPacket); |
mridup | 0:309c845d289d | 55 | |
mridup | 0:309c845d289d | 56 | #define BDADDR_SIZE 6 |
mridup | 0:309c845d289d | 57 | tHalUint8 bdaddr[BDADDR_SIZE]= {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02}; |
mridup | 0:309c845d289d | 58 | |
hemddabral | 14:baa7a1464517 | 59 | uint16_t g_gap_service_handle = 0; |
hemddabral | 14:baa7a1464517 | 60 | uint16_t g_appearance_char_handle = 0; |
hemddabral | 14:baa7a1464517 | 61 | uint16_t g_device_name_char_handle = 0; |
hemddabral | 14:baa7a1464517 | 62 | |
mridup | 0:309c845d289d | 63 | /* Private variables ---------------------------------------------------------*/ |
mridup | 0:309c845d289d | 64 | volatile uint8_t set_connectable = 1; |
mridup | 0:309c845d289d | 65 | |
mridup | 0:309c845d289d | 66 | /**************************************************************************/ |
mridup | 0:309c845d289d | 67 | /*! |
mridup | 0:309c845d289d | 68 | @brief Initialises BTLE and the underlying HW/SoftDevice |
mridup | 0:309c845d289d | 69 | |
mridup | 0:309c845d289d | 70 | @returns |
mridup | 0:309c845d289d | 71 | */ |
mridup | 0:309c845d289d | 72 | /**************************************************************************/ |
mridup | 0:309c845d289d | 73 | void btle_init(void) |
mridup | 0:309c845d289d | 74 | { |
hemddabral | 14:baa7a1464517 | 75 | const char *name = "BlueNRG_1"; |
mridup | 0:309c845d289d | 76 | tHalUint8 SERVER_BDADDR[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02};//MPD: This is the public address of the Server/Client |
mridup | 0:309c845d289d | 77 | tHalUint8 bdaddr[BDADDR_SIZE]; |
mridup | 0:309c845d289d | 78 | int ret; |
mridup | 0:309c845d289d | 79 | uint16_t service_handle, dev_name_char_handle, appearance_char_handle; |
mridup | 0:309c845d289d | 80 | |
mridup | 0:309c845d289d | 81 | HAL_Init(); |
mridup | 0:309c845d289d | 82 | |
mridup | 0:309c845d289d | 83 | /* Configure the User Button in GPIO Mode */ |
mridup | 0:309c845d289d | 84 | BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); |
mridup | 0:309c845d289d | 85 | |
mridup | 0:309c845d289d | 86 | /* Configure the system clock */ |
mridup | 0:309c845d289d | 87 | SystemClock_Config(); |
mridup | 0:309c845d289d | 88 | |
mridup | 0:309c845d289d | 89 | /* Delay needed only to be able to acces the JTAG interface after reset |
mridup | 0:309c845d289d | 90 | if it will be disabled later. */ |
mridup | 0:309c845d289d | 91 | Clock_Wait(500); |
mridup | 0:309c845d289d | 92 | |
mridup | 0:309c845d289d | 93 | /* Initialize the BlueNRG SPI driver */ |
mridup | 0:309c845d289d | 94 | BNRG_SPI_Init(); |
mridup | 0:309c845d289d | 95 | |
mridup | 0:309c845d289d | 96 | /* Initialize the BlueNRG HCI */ |
mridup | 0:309c845d289d | 97 | HCI_Init(); |
mridup | 0:309c845d289d | 98 | |
mridup | 0:309c845d289d | 99 | /* Reset BlueNRG SPI interface */ |
mridup | 0:309c845d289d | 100 | BlueNRG_RST(); |
mridup | 0:309c845d289d | 101 | |
mridup | 0:309c845d289d | 102 | /* The Nucleo board must be configured as SERVER */ |
mridup | 2:a2b623661316 | 103 | /*Osal_MemCpy(bdaddr, SERVER_BDADDR, sizeof(SERVER_BDADDR)); |
mridup | 0:309c845d289d | 104 | |
mridup | 0:309c845d289d | 105 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
mridup | 0:309c845d289d | 106 | CONFIG_DATA_PUBADDR_LEN, |
mridup | 2:a2b623661316 | 107 | bdaddr);*/ |
mridup | 0:309c845d289d | 108 | |
mridup | 0:309c845d289d | 109 | ret = aci_gatt_init(); |
mridup | 2:a2b623661316 | 110 | //GAP is always in PERIPHERAL _ROLE as mbed does not support Master role at the moment |
mridup | 0:309c845d289d | 111 | ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle); |
hemddabral | 14:baa7a1464517 | 112 | |
hemddabral | 14:baa7a1464517 | 113 | g_gap_service_handle = service_handle; |
hemddabral | 14:baa7a1464517 | 114 | g_appearance_char_handle = appearance_char_handle; |
hemddabral | 14:baa7a1464517 | 115 | g_device_name_char_handle = dev_name_char_handle; |
mridup | 0:309c845d289d | 116 | ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, |
mridup | 0:309c845d289d | 117 | strlen(name), (tHalUint8 *)name); |
mridup | 0:309c845d289d | 118 | |
mridup | 0:309c845d289d | 119 | return; |
mridup | 0:309c845d289d | 120 | } |
mridup | 0:309c845d289d | 121 | |
mridup | 0:309c845d289d | 122 | void User_Process() |
mridup | 0:309c845d289d | 123 | { |
mridup | 0:309c845d289d | 124 | if(set_connectable){ |
mridup | 0:309c845d289d | 125 | setConnectable(); |
mridup | 0:309c845d289d | 126 | set_connectable = FALSE; |
mridup | 0:309c845d289d | 127 | } |
mridup | 0:309c845d289d | 128 | } |
mridup | 0:309c845d289d | 129 | |
mridup | 0:309c845d289d | 130 | void setConnectable(void) |
mridup | 0:309c845d289d | 131 | { |
mridup | 0:309c845d289d | 132 | tBleStatus ret; |
mridup | 0:309c845d289d | 133 | |
hemddabral | 14:baa7a1464517 | 134 | const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G', '1', '2'}; |
mridup | 0:309c845d289d | 135 | |
mridup | 0:309c845d289d | 136 | /* disable scan response */ |
mridup | 0:309c845d289d | 137 | hci_le_set_scan_resp_data(0,NULL); |
mridup | 0:309c845d289d | 138 | |
mridup | 0:309c845d289d | 139 | |
mridup | 0:309c845d289d | 140 | ret = aci_gap_set_discoverable(ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE, |
mridup | 0:309c845d289d | 141 | 8, local_name, 0, NULL, 0, 0); |
mridup | 0:309c845d289d | 142 | |
mridup | 0:309c845d289d | 143 | } |
mridup | 0:309c845d289d | 144 | |
mridup | 0:309c845d289d | 145 | /**************************************************************************/ |
mridup | 0:309c845d289d | 146 | /*! |
mridup | 0:309c845d289d | 147 | @brief |
mridup | 0:309c845d289d | 148 | |
mridup | 0:309c845d289d | 149 | @param[in] p_ble_evt |
mridup | 0:309c845d289d | 150 | |
mridup | 0:309c845d289d | 151 | @returns |
mridup | 0:309c845d289d | 152 | */ |
mridup | 0:309c845d289d | 153 | /**************************************************************************/ |
mridup | 6:08cfc94b5f49 | 154 | static void btle_handler() |
mridup | 0:309c845d289d | 155 | { |
mridup | 0:309c845d289d | 156 | |
mridup | 0:309c845d289d | 157 | } |
mridup | 0:309c845d289d | 158 | |
mridup | 0:309c845d289d | 159 | |
mridup | 0:309c845d289d | 160 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 161 | extern "C" { |
mridup | 0:309c845d289d | 162 | #endif |
mridup | 0:309c845d289d | 163 | |
mridup | 3:9c4c13795643 | 164 | extern void HCI_Event_CB(void *pckt) { |
mridup | 3:9c4c13795643 | 165 | |
mridup | 3:9c4c13795643 | 166 | hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt; |
mridup | 3:9c4c13795643 | 167 | hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data; |
mridup | 0:309c845d289d | 168 | |
mridup | 3:9c4c13795643 | 169 | if(hci_pckt->type != HCI_EVENT_PKT) |
mridup | 3:9c4c13795643 | 170 | return; |
mridup | 3:9c4c13795643 | 171 | |
mridup | 3:9c4c13795643 | 172 | switch(event_pckt->evt){ |
mridup | 3:9c4c13795643 | 173 | |
mridup | 3:9c4c13795643 | 174 | case EVT_DISCONN_COMPLETE: |
mridup | 3:9c4c13795643 | 175 | { |
mridup | 4:fa5b5693c1b5 | 176 | BlueNRGGap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED); |
mridup | 3:9c4c13795643 | 177 | } |
mridup | 3:9c4c13795643 | 178 | break; |
mridup | 3:9c4c13795643 | 179 | |
mridup | 3:9c4c13795643 | 180 | case EVT_LE_META_EVENT: |
mridup | 3:9c4c13795643 | 181 | { |
mridup | 3:9c4c13795643 | 182 | evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data; |
mridup | 3:9c4c13795643 | 183 | |
mridup | 3:9c4c13795643 | 184 | switch(evt->subevent){ |
mridup | 3:9c4c13795643 | 185 | case EVT_LE_CONN_COMPLETE: |
mridup | 4:fa5b5693c1b5 | 186 | { |
mridup | 4:fa5b5693c1b5 | 187 | evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data; |
mridup | 4:fa5b5693c1b5 | 188 | |
mridup | 4:fa5b5693c1b5 | 189 | BlueNRGGap::getInstance().setConnectionHandle(cc->handle); |
mridup | 4:fa5b5693c1b5 | 190 | BlueNRGGap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED); |
mridup | 4:fa5b5693c1b5 | 191 | |
mridup | 3:9c4c13795643 | 192 | } |
mridup | 3:9c4c13795643 | 193 | break; |
mridup | 3:9c4c13795643 | 194 | } |
mridup | 3:9c4c13795643 | 195 | } |
mridup | 3:9c4c13795643 | 196 | break; |
mridup | 3:9c4c13795643 | 197 | |
mridup | 3:9c4c13795643 | 198 | case EVT_VENDOR: |
mridup | 3:9c4c13795643 | 199 | { |
mridup | 3:9c4c13795643 | 200 | evt_blue_aci *blue_evt = (evt_blue_aci*)event_pckt->data; |
mridup | 3:9c4c13795643 | 201 | switch(blue_evt->ecode){ |
mridup | 3:9c4c13795643 | 202 | |
mridup | 3:9c4c13795643 | 203 | case EVT_BLUE_GATT_READ_PERMIT_REQ: |
mridup | 3:9c4c13795643 | 204 | { |
mridup | 3:9c4c13795643 | 205 | //evt_gatt_read_permit_req *pr = (void*)blue_evt->data; |
mridup | 3:9c4c13795643 | 206 | //Read_Request_CB(pr->attr_handle); |
mridup | 3:9c4c13795643 | 207 | } |
mridup | 3:9c4c13795643 | 208 | break; |
mridup | 3:9c4c13795643 | 209 | } |
mridup | 3:9c4c13795643 | 210 | } |
mridup | 3:9c4c13795643 | 211 | break; |
mridup | 3:9c4c13795643 | 212 | } |
mridup | 0:309c845d289d | 213 | |
mridup | 0:309c845d289d | 214 | return ; |
mridup | 3:9c4c13795643 | 215 | } |
mridup | 3:9c4c13795643 | 216 | |
mridup | 3:9c4c13795643 | 217 | |
mridup | 0:309c845d289d | 218 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 219 | } |
mridup | 0:309c845d289d | 220 | #endif |