Added support for obtaining BLE device name by parsing advertising data.
Fork of BLE_BlueNRG by
btle/src/btle.cpp@6:08cfc94b5f49, 2014-07-25 (annotated)
- Committer:
- mridup
- Date:
- Fri Jul 25 09:09:13 2014 +0000
- Revision:
- 6:08cfc94b5f49
- Parent:
- 4:fa5b5693c1b5
Removing code references. Renaming some functions/variables, comments.
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 | |
mridup | 0:309c845d289d | 59 | /* Private variables ---------------------------------------------------------*/ |
mridup | 0:309c845d289d | 60 | volatile uint8_t set_connectable = 1; |
mridup | 0:309c845d289d | 61 | |
mridup | 0:309c845d289d | 62 | /**************************************************************************/ |
mridup | 0:309c845d289d | 63 | /*! |
mridup | 0:309c845d289d | 64 | @brief Initialises BTLE and the underlying HW/SoftDevice |
mridup | 0:309c845d289d | 65 | |
mridup | 0:309c845d289d | 66 | @returns |
mridup | 0:309c845d289d | 67 | */ |
mridup | 0:309c845d289d | 68 | /**************************************************************************/ |
mridup | 0:309c845d289d | 69 | void btle_init(void) |
mridup | 0:309c845d289d | 70 | { |
mridup | 0:309c845d289d | 71 | const char *name = "BlueNRG"; |
mridup | 0:309c845d289d | 72 | tHalUint8 SERVER_BDADDR[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02};//MPD: This is the public address of the Server/Client |
mridup | 0:309c845d289d | 73 | tHalUint8 bdaddr[BDADDR_SIZE]; |
mridup | 0:309c845d289d | 74 | int ret; |
mridup | 0:309c845d289d | 75 | uint16_t service_handle, dev_name_char_handle, appearance_char_handle; |
mridup | 0:309c845d289d | 76 | |
mridup | 0:309c845d289d | 77 | HAL_Init(); |
mridup | 0:309c845d289d | 78 | |
mridup | 0:309c845d289d | 79 | /* Configure the User Button in GPIO Mode */ |
mridup | 0:309c845d289d | 80 | BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); |
mridup | 0:309c845d289d | 81 | |
mridup | 0:309c845d289d | 82 | /* Configure the system clock */ |
mridup | 0:309c845d289d | 83 | SystemClock_Config(); |
mridup | 0:309c845d289d | 84 | |
mridup | 0:309c845d289d | 85 | /* Delay needed only to be able to acces the JTAG interface after reset |
mridup | 0:309c845d289d | 86 | if it will be disabled later. */ |
mridup | 0:309c845d289d | 87 | Clock_Wait(500); |
mridup | 0:309c845d289d | 88 | |
mridup | 0:309c845d289d | 89 | /* Initialize the BlueNRG SPI driver */ |
mridup | 0:309c845d289d | 90 | BNRG_SPI_Init(); |
mridup | 0:309c845d289d | 91 | |
mridup | 0:309c845d289d | 92 | /* Initialize the BlueNRG HCI */ |
mridup | 0:309c845d289d | 93 | HCI_Init(); |
mridup | 0:309c845d289d | 94 | |
mridup | 0:309c845d289d | 95 | /* Reset BlueNRG SPI interface */ |
mridup | 0:309c845d289d | 96 | BlueNRG_RST(); |
mridup | 0:309c845d289d | 97 | |
mridup | 0:309c845d289d | 98 | /* The Nucleo board must be configured as SERVER */ |
mridup | 2:a2b623661316 | 99 | /*Osal_MemCpy(bdaddr, SERVER_BDADDR, sizeof(SERVER_BDADDR)); |
mridup | 0:309c845d289d | 100 | |
mridup | 0:309c845d289d | 101 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, |
mridup | 0:309c845d289d | 102 | CONFIG_DATA_PUBADDR_LEN, |
mridup | 2:a2b623661316 | 103 | bdaddr);*/ |
mridup | 0:309c845d289d | 104 | |
mridup | 0:309c845d289d | 105 | ret = aci_gatt_init(); |
mridup | 2:a2b623661316 | 106 | //GAP is always in PERIPHERAL _ROLE as mbed does not support Master role at the moment |
mridup | 0:309c845d289d | 107 | ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle); |
mridup | 0:309c845d289d | 108 | |
mridup | 0:309c845d289d | 109 | ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, |
mridup | 0:309c845d289d | 110 | strlen(name), (tHalUint8 *)name); |
mridup | 0:309c845d289d | 111 | |
mridup | 0:309c845d289d | 112 | return; |
mridup | 0:309c845d289d | 113 | } |
mridup | 0:309c845d289d | 114 | |
mridup | 0:309c845d289d | 115 | void User_Process() |
mridup | 0:309c845d289d | 116 | { |
mridup | 0:309c845d289d | 117 | if(set_connectable){ |
mridup | 0:309c845d289d | 118 | setConnectable(); |
mridup | 0:309c845d289d | 119 | set_connectable = FALSE; |
mridup | 0:309c845d289d | 120 | } |
mridup | 0:309c845d289d | 121 | } |
mridup | 0:309c845d289d | 122 | |
mridup | 0:309c845d289d | 123 | void setConnectable(void) |
mridup | 0:309c845d289d | 124 | { |
mridup | 0:309c845d289d | 125 | tBleStatus ret; |
mridup | 0:309c845d289d | 126 | |
mridup | 0:309c845d289d | 127 | const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G'}; |
mridup | 0:309c845d289d | 128 | |
mridup | 0:309c845d289d | 129 | /* disable scan response */ |
mridup | 0:309c845d289d | 130 | hci_le_set_scan_resp_data(0,NULL); |
mridup | 0:309c845d289d | 131 | |
mridup | 0:309c845d289d | 132 | |
mridup | 0:309c845d289d | 133 | ret = aci_gap_set_discoverable(ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE, |
mridup | 0:309c845d289d | 134 | 8, local_name, 0, NULL, 0, 0); |
mridup | 0:309c845d289d | 135 | |
mridup | 0:309c845d289d | 136 | } |
mridup | 0:309c845d289d | 137 | |
mridup | 0:309c845d289d | 138 | /**************************************************************************/ |
mridup | 0:309c845d289d | 139 | /*! |
mridup | 0:309c845d289d | 140 | @brief |
mridup | 0:309c845d289d | 141 | |
mridup | 0:309c845d289d | 142 | @param[in] p_ble_evt |
mridup | 0:309c845d289d | 143 | |
mridup | 0:309c845d289d | 144 | @returns |
mridup | 0:309c845d289d | 145 | */ |
mridup | 0:309c845d289d | 146 | /**************************************************************************/ |
mridup | 6:08cfc94b5f49 | 147 | static void btle_handler() |
mridup | 0:309c845d289d | 148 | { |
mridup | 0:309c845d289d | 149 | |
mridup | 0:309c845d289d | 150 | } |
mridup | 0:309c845d289d | 151 | |
mridup | 0:309c845d289d | 152 | |
mridup | 0:309c845d289d | 153 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 154 | extern "C" { |
mridup | 0:309c845d289d | 155 | #endif |
mridup | 0:309c845d289d | 156 | |
mridup | 3:9c4c13795643 | 157 | extern void HCI_Event_CB(void *pckt) { |
mridup | 3:9c4c13795643 | 158 | |
mridup | 3:9c4c13795643 | 159 | hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt; |
mridup | 3:9c4c13795643 | 160 | hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data; |
mridup | 0:309c845d289d | 161 | |
mridup | 3:9c4c13795643 | 162 | if(hci_pckt->type != HCI_EVENT_PKT) |
mridup | 3:9c4c13795643 | 163 | return; |
mridup | 3:9c4c13795643 | 164 | |
mridup | 3:9c4c13795643 | 165 | switch(event_pckt->evt){ |
mridup | 3:9c4c13795643 | 166 | |
mridup | 3:9c4c13795643 | 167 | case EVT_DISCONN_COMPLETE: |
mridup | 3:9c4c13795643 | 168 | { |
mridup | 4:fa5b5693c1b5 | 169 | BlueNRGGap::getInstance().handleEvent(GapEvents::GAP_EVENT_DISCONNECTED); |
mridup | 3:9c4c13795643 | 170 | } |
mridup | 3:9c4c13795643 | 171 | break; |
mridup | 3:9c4c13795643 | 172 | |
mridup | 3:9c4c13795643 | 173 | case EVT_LE_META_EVENT: |
mridup | 3:9c4c13795643 | 174 | { |
mridup | 3:9c4c13795643 | 175 | evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data; |
mridup | 3:9c4c13795643 | 176 | |
mridup | 3:9c4c13795643 | 177 | switch(evt->subevent){ |
mridup | 3:9c4c13795643 | 178 | case EVT_LE_CONN_COMPLETE: |
mridup | 4:fa5b5693c1b5 | 179 | { |
mridup | 4:fa5b5693c1b5 | 180 | evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data; |
mridup | 4:fa5b5693c1b5 | 181 | |
mridup | 4:fa5b5693c1b5 | 182 | BlueNRGGap::getInstance().setConnectionHandle(cc->handle); |
mridup | 4:fa5b5693c1b5 | 183 | BlueNRGGap::getInstance().handleEvent(GapEvents::GAP_EVENT_CONNECTED); |
mridup | 4:fa5b5693c1b5 | 184 | |
mridup | 3:9c4c13795643 | 185 | } |
mridup | 3:9c4c13795643 | 186 | break; |
mridup | 3:9c4c13795643 | 187 | } |
mridup | 3:9c4c13795643 | 188 | } |
mridup | 3:9c4c13795643 | 189 | break; |
mridup | 3:9c4c13795643 | 190 | |
mridup | 3:9c4c13795643 | 191 | case EVT_VENDOR: |
mridup | 3:9c4c13795643 | 192 | { |
mridup | 3:9c4c13795643 | 193 | evt_blue_aci *blue_evt = (evt_blue_aci*)event_pckt->data; |
mridup | 3:9c4c13795643 | 194 | switch(blue_evt->ecode){ |
mridup | 3:9c4c13795643 | 195 | |
mridup | 3:9c4c13795643 | 196 | case EVT_BLUE_GATT_READ_PERMIT_REQ: |
mridup | 3:9c4c13795643 | 197 | { |
mridup | 3:9c4c13795643 | 198 | //evt_gatt_read_permit_req *pr = (void*)blue_evt->data; |
mridup | 3:9c4c13795643 | 199 | //Read_Request_CB(pr->attr_handle); |
mridup | 3:9c4c13795643 | 200 | } |
mridup | 3:9c4c13795643 | 201 | break; |
mridup | 3:9c4c13795643 | 202 | } |
mridup | 3:9c4c13795643 | 203 | } |
mridup | 3:9c4c13795643 | 204 | break; |
mridup | 3:9c4c13795643 | 205 | } |
mridup | 0:309c845d289d | 206 | |
mridup | 0:309c845d289d | 207 | return ; |
mridup | 3:9c4c13795643 | 208 | } |
mridup | 3:9c4c13795643 | 209 | |
mridup | 3:9c4c13795643 | 210 | |
mridup | 0:309c845d289d | 211 | #ifdef __cplusplus |
mridup | 0:309c845d289d | 212 | } |
mridup | 0:309c845d289d | 213 | #endif |