Added support for obtaining BLE device name by parsing advertising data.

Fork of BLE_BlueNRG by Mridupawan Das

Committer:
mridup
Date:
Wed Jul 16 04:59:51 2014 +0000
Revision:
0:309c845d289d
Child:
2:a2b623661316
Full BlueNRG library

Who changed what in which revision?

UserRevisionLine numberNew 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 "sensor_service.h"
mridup 0:309c845d289d 39 #include "role_type.h"
mridup 0:309c845d289d 40 #include "debug.h"
mridup 0:309c845d289d 41
mridup 0:309c845d289d 42 /* SPI handler declaration */
mridup 0:309c845d289d 43 SPI_HandleTypeDef SpiHandle;
mridup 0:309c845d289d 44
mridup 0:309c845d289d 45 #ifdef __cplusplus
mridup 0:309c845d289d 46 }
mridup 0:309c845d289d 47 #endif
mridup 0:309c845d289d 48
mridup 0:309c845d289d 49
mridup 0:309c845d289d 50 static void btle_handler(/*ble_evt_t * p_ble_evt*/);
mridup 0:309c845d289d 51 void HCI_Input(tHciDataPacket * hciReadPacket);
mridup 0:309c845d289d 52
mridup 0:309c845d289d 53 #define BDADDR_SIZE 6
mridup 0:309c845d289d 54 tHalUint8 bdaddr[BDADDR_SIZE]= {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02};
mridup 0:309c845d289d 55
mridup 0:309c845d289d 56 /* Private variables ---------------------------------------------------------*/
mridup 0:309c845d289d 57 volatile uint8_t set_connectable = 1;
mridup 0:309c845d289d 58
mridup 0:309c845d289d 59 /**************************************************************************/
mridup 0:309c845d289d 60 /*!
mridup 0:309c845d289d 61 @brief Initialises BTLE and the underlying HW/SoftDevice
mridup 0:309c845d289d 62
mridup 0:309c845d289d 63 @returns
mridup 0:309c845d289d 64 */
mridup 0:309c845d289d 65 /**************************************************************************/
mridup 0:309c845d289d 66 void btle_init(void)
mridup 0:309c845d289d 67 {
mridup 0:309c845d289d 68 const char *name = "BlueNRG";
mridup 0:309c845d289d 69 tHalUint8 SERVER_BDADDR[] = {0x12, 0x34, 0x00, 0xE1, 0x80, 0x02};//MPD: This is the public address of the Server/Client
mridup 0:309c845d289d 70 tHalUint8 bdaddr[BDADDR_SIZE];
mridup 0:309c845d289d 71 int ret;
mridup 0:309c845d289d 72 uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
mridup 0:309c845d289d 73
mridup 0:309c845d289d 74 HAL_Init();
mridup 0:309c845d289d 75
mridup 0:309c845d289d 76 /* Configure LED2 */
mridup 0:309c845d289d 77 //BSP_LED_Init(LED2);
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 //BSP_LED_Toggle(LED2);
mridup 0:309c845d289d 99
mridup 0:309c845d289d 100 /* The Nucleo board must be configured as SERVER */
mridup 0:309c845d289d 101 Osal_MemCpy(bdaddr, SERVER_BDADDR, sizeof(SERVER_BDADDR));
mridup 0:309c845d289d 102
mridup 0:309c845d289d 103 ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET,
mridup 0:309c845d289d 104 CONFIG_DATA_PUBADDR_LEN,
mridup 0:309c845d289d 105 bdaddr);
mridup 0:309c845d289d 106
mridup 0:309c845d289d 107 ret = aci_gatt_init();
mridup 0:309c845d289d 108
mridup 0:309c845d289d 109 ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle);
mridup 0:309c845d289d 110
mridup 0:309c845d289d 111 ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0,
mridup 0:309c845d289d 112 strlen(name), (tHalUint8 *)name);
mridup 0:309c845d289d 113
mridup 0:309c845d289d 114 return;
mridup 0:309c845d289d 115 }
mridup 0:309c845d289d 116
mridup 0:309c845d289d 117 void User_Process()
mridup 0:309c845d289d 118 {
mridup 0:309c845d289d 119 if(set_connectable){
mridup 0:309c845d289d 120 setConnectable();
mridup 0:309c845d289d 121 set_connectable = FALSE;
mridup 0:309c845d289d 122 }
mridup 0:309c845d289d 123 }
mridup 0:309c845d289d 124
mridup 0:309c845d289d 125 void setConnectable(void)
mridup 0:309c845d289d 126 {
mridup 0:309c845d289d 127 tBleStatus ret;
mridup 0:309c845d289d 128
mridup 0:309c845d289d 129 const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G'};
mridup 0:309c845d289d 130
mridup 0:309c845d289d 131 /* disable scan response */
mridup 0:309c845d289d 132 hci_le_set_scan_resp_data(0,NULL);
mridup 0:309c845d289d 133
mridup 0:309c845d289d 134
mridup 0:309c845d289d 135 ret = aci_gap_set_discoverable(ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE,
mridup 0:309c845d289d 136 8, local_name, 0, NULL, 0, 0);
mridup 0:309c845d289d 137
mridup 0:309c845d289d 138 }
mridup 0:309c845d289d 139
mridup 0:309c845d289d 140 /**************************************************************************/
mridup 0:309c845d289d 141 /*!
mridup 0:309c845d289d 142 @brief
mridup 0:309c845d289d 143
mridup 0:309c845d289d 144 @param[in] p_ble_evt
mridup 0:309c845d289d 145
mridup 0:309c845d289d 146 @returns
mridup 0:309c845d289d 147 */
mridup 0:309c845d289d 148 /**************************************************************************/
mridup 0:309c845d289d 149 static void btle_handler(/*ble_evt_t * p_ble_evt*/)
mridup 0:309c845d289d 150 {
mridup 0:309c845d289d 151
mridup 0:309c845d289d 152 }
mridup 0:309c845d289d 153
mridup 0:309c845d289d 154
mridup 0:309c845d289d 155 #ifdef __cplusplus
mridup 0:309c845d289d 156 extern "C" {
mridup 0:309c845d289d 157 #endif
mridup 0:309c845d289d 158
mridup 0:309c845d289d 159 extern void HCI_Event_CB(void *ptr) {
mridup 0:309c845d289d 160 int a=3;
mridup 0:309c845d289d 161 int b=5;
mridup 0:309c845d289d 162
mridup 0:309c845d289d 163 int sum = a+b;
mridup 0:309c845d289d 164
mridup 0:309c845d289d 165 return ;
mridup 0:309c845d289d 166 }
mridup 0:309c845d289d 167 #ifdef __cplusplus
mridup 0:309c845d289d 168 }
mridup 0:309c845d289d 169 #endif