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.
Dependencies: HC_SR04_Ultrasonic_Library Servo mbed
Fork of FIP_REV1 by
btle.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 00018 #include "btle.h" 00019 #include "public/Gap.h" 00020 #include "public/GapEvents.h" 00021 #include "BlueNRGGap.h" 00022 #include "BlueNRGGattServer.h" 00023 #include "Utils.h" 00024 00025 #ifdef __cplusplus 00026 extern "C" { 00027 #endif 00028 00029 00030 /* C File Includes ------------------------------------------------------------------*/ 00031 #include "hal_types.h" 00032 #include "hci.h" 00033 #include "bluenrg_hci.h" 00034 #include "gp_timer.h" 00035 #include "hal.h" 00036 #include "osal.h" 00037 #include "hci_internal.h" 00038 #include "bluenrg_hci_internal.h" 00039 #include "gap.h" 00040 #include "sm.h" 00041 #include <stdio.h> 00042 #include <string.h> 00043 #include "role_type.h" 00044 #include "debug.h" 00045 00046 /* SPI handler declaration */ 00047 SPI_HandleTypeDef SpiHandle; 00048 00049 #ifdef __cplusplus 00050 } 00051 #endif 00052 00053 00054 static void btle_handler(/*ble_evt_t * p_ble_evt*/); 00055 void HCI_Input(tHciDataPacket * hciReadPacket); 00056 00057 //#define BDADDR_SIZE 6 00058 //tHalUint8 bdaddr[BDADDR_SIZE]= {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02}; 00059 00060 uint16_t g_gap_service_handle = 0; 00061 uint16_t g_appearance_char_handle = 0; 00062 uint16_t g_device_name_char_handle = 0; 00063 00064 /* Private variables ---------------------------------------------------------*/ 00065 volatile uint8_t set_connectable = 1; 00066 00067 /**************************************************************************/ 00068 /*! 00069 @brief Initialises BTLE and the underlying HW/Device 00070 00071 @returns 00072 */ 00073 /**************************************************************************/ 00074 void btle_init(bool isSetAddress) 00075 { 00076 DEBUG("btle_init>>\n\r"); 00077 const char *name = "FearInProgress"; 00078 tHalUint8 *bleAddr; 00079 int ret; 00080 uint16_t service_handle, dev_name_char_handle, appearance_char_handle; 00081 00082 HAL_Init(); 00083 00084 /* Configure the User Button in GPIO Mode */ 00085 BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); 00086 00087 /* Configure the system clock */ 00088 SystemClock_Config(); 00089 00090 /* Delay needed only to be able to acces the JTAG interface after reset 00091 if it will be disabled later. */ 00092 Clock_Wait(500); 00093 00094 /* Initialize the BlueNRG SPI driver */ 00095 BNRG_SPI_Init(); 00096 00097 /* Initialize the BlueNRG HCI */ 00098 HCI_Init(); 00099 00100 /* Reset BlueNRG SPI interface */ 00101 BlueNRG_RST(); 00102 00103 /* The Nucleo board must be configured as SERVER */ 00104 //check if issetAddress is set than set address. 00105 if(isSetAddress) 00106 { 00107 bleAddr = BlueNRGGap::getInstance().getAddress(); 00108 00109 tHalUint8 bdaddr[BDADDR_SIZE]; 00110 Osal_MemCpy(bdaddr, bleAddr, BDADDR_SIZE); 00111 00112 ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, 00113 CONFIG_DATA_PUBADDR_LEN, 00114 bdaddr); 00115 } 00116 00117 ret = aci_gatt_init(); 00118 //GAP is always in PERIPHERAL _ROLE as mbed does not support Master role at the moment 00119 ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle); 00120 00121 g_gap_service_handle = service_handle; 00122 g_appearance_char_handle = appearance_char_handle; 00123 g_device_name_char_handle = dev_name_char_handle; 00124 /*ret = aci_gatt_update_char_value(service_handle, dev_name_char_handle, 0, 00125 strlen(name), (tHalUint8 *)name);*/ 00126 00127 return; 00128 } 00129 00130 void User_Process() 00131 { 00132 if(set_connectable){ 00133 setConnectable(); 00134 set_connectable = FALSE; 00135 } 00136 } 00137 00138 void setConnectable(void) 00139 { 00140 tBleStatus ret; 00141 00142 const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'F','I','P'}; 00143 00144 /* disable scan response */ 00145 hci_le_set_scan_resp_data(0,NULL); 00146 00147 00148 ret = aci_gap_set_discoverable(ADV_IND, 0, 0, PUBLIC_ADDR, NO_WHITE_LIST_USE, 00149 8, local_name, 0, NULL, 0, 0); 00150 00151 } 00152 00153 /**************************************************************************/ 00154 /*! 00155 @brief 00156 00157 @param[in] p_ble_evt 00158 00159 @returns 00160 */ 00161 /**************************************************************************/ 00162 static void btle_handler() 00163 { 00164 00165 } 00166 00167 00168 #ifdef __cplusplus 00169 extern "C" { 00170 #endif 00171 00172 extern void HCI_Event_CB(void *pckt) { 00173 00174 hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt; 00175 hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data; 00176 00177 if(hci_pckt->type != HCI_EVENT_PKT) 00178 return; 00179 00180 switch(event_pckt->evt){ 00181 00182 case EVT_DISCONN_COMPLETE: 00183 { 00184 evt_disconn_complete *evt = (evt_disconn_complete*)event_pckt; 00185 00186 BlueNRGGap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_DISCONNECTED, evt->handle); 00187 } 00188 break; 00189 00190 case EVT_LE_META_EVENT: 00191 { 00192 evt_le_meta_event *evt = (evt_le_meta_event *)event_pckt->data; 00193 00194 switch(evt->subevent){ 00195 case EVT_LE_CONN_COMPLETE: 00196 { 00197 evt_le_connection_complete *cc = (evt_le_connection_complete *)evt->data; 00198 00199 BlueNRGGap::getInstance().setConnectionHandle(cc->handle); 00200 BlueNRGGap::getInstance().processHandleSpecificEvent(GapEvents::GAP_EVENT_CONNECTED, cc->handle); 00201 } 00202 break; 00203 } 00204 } 00205 break; 00206 00207 case EVT_VENDOR: 00208 { 00209 evt_blue_aci *blue_evt = (evt_blue_aci*)event_pckt->data; 00210 switch(blue_evt->ecode){ 00211 00212 case EVT_BLUE_GATT_READ_PERMIT_REQ: 00213 { 00214 DEBUG("EVT_BLUE_GATT_READ_PERMIT_REQ_OK\n\r"); 00215 evt_gatt_read_permit_req *pr = (evt_gatt_read_permit_req*)blue_evt->data; 00216 BlueNRGGattServer::getInstance().Read_Request_CB(pr->attr_handle); 00217 } 00218 break; 00219 00220 case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED: 00221 { 00222 /* this callback is invoked when a GATT attribute is modified 00223 extract callback data and pass to suitable handler function */ 00224 evt_gatt_attr_modified *evt = (evt_gatt_attr_modified*)blue_evt->data; 00225 DEBUG("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED\n\r"); 00226 DEBUG("CharHandle 0x%x, length: 0x%x, Data: 0x%x\n\r",evt->attr_handle, evt->data_length, (uint16_t)evt->att_data[0]); 00227 00228 00229 //Extract the GattCharacteristic from p_characteristics[] and find the properties mask 00230 GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(evt->attr_handle); 00231 DEBUG("getProperties 0x%x\n\r",p_char->getProperties()); 00232 if((p_char->getProperties() & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY 00233 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE))) { 00234 00235 //Now Check if data written in Enable or Disable 00236 if((uint16_t)evt->att_data[0]==1) { 00237 //DEBUG("Notify ENABLED\n\r"); 00238 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_UPDATES_ENABLED, evt->attr_handle); 00239 } 00240 else { 00241 //DEBUG("Notify DISABLED\n\r"); 00242 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_UPDATES_DISABLED, evt->attr_handle); 00243 } 00244 } 00245 00246 //Check is attr handle property is WRITEABLE, if yes, generate GATT_EVENT_DATA_WRITTEN Event 00247 if((p_char->getProperties() & 00248 (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE| 00249 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE))) { 00250 00251 BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_WRITTEN, evt->attr_handle); 00252 //Write the actual Data to the Attr Handle? (uint8_1[])evt->att_data contains the data 00253 //handleUpdated = evt->attr_handle; 00254 //dataUpdated = evt->att_data; 00255 if ((p_char->getValuePtr() != NULL) && (p_char->getInitialLength() > 0)) { 00256 BlueNRGGattServer::getInstance().updateValue(p_char->getHandle(), evt->att_data, p_char->getInitialLength(), false /* localOnly */); 00257 DEBUG("%d", p_char->getValuePtr()); 00258 } 00259 } 00260 } 00261 break; 00262 00263 //Any cases for Data Sent Notifications? 00264 case EVT_BLUE_GATT_NOTIFICATION: 00265 //This is only relevant for Client Side Event 00266 DEBUG("EVT_BLUE_GATT_NOTIFICATION"); 00267 break; 00268 case EVT_BLUE_GATT_INDICATION: 00269 //This is only relevant for Client Side Event 00270 DEBUG("EVT_BLUE_GATT_INDICATION"); 00271 break; 00272 00273 case EVT_BLUE_GATT_PROCEDURE_COMPLETE: 00274 DEBUG("EVT_BLUE_GATT_PROCEDURE_COMPLETE"); 00275 break; 00276 } 00277 } 00278 break; 00279 } 00280 00281 return ; 00282 } 00283 00284 00285 #ifdef __cplusplus 00286 } 00287 #endif
Generated on Tue Jul 12 2022 21:41:59 by
1.7.2
