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