HW layer for the Nucleo board, it only work with old BLE_API
Dependents: Hello_BLE F446RE-BLE
Fork of X_NUCLEO_IDB0XA1 by
Diff: platform/src/btle.cpp
- Revision:
- 126:32039585a969
- Parent:
- 125:2e468f07a94f
--- a/platform/src/btle.cpp Thu Sep 17 08:23:17 2015 +0000 +++ b/platform/src/btle.cpp Tue Sep 29 12:39:31 2015 +0000 @@ -55,6 +55,7 @@ #include "bluenrg_aci.h" #include "bluenrg_hal_aci.h" #include "bluenrg_gap.h" +#include "bluenrg_utils.h" #include "hal_types.h" #include "hal.h" @@ -67,6 +68,8 @@ } #endif +#define IDB04A1 0 +#define IDB05A1 1 // static void btle_handler(/*ble_evt_t * p_ble_evt*/); void HCI_Input(tHciDataPacket * hciReadPacket); @@ -81,6 +84,8 @@ /* Private variables ---------------------------------------------------------*/ volatile uint8_t set_connectable = 1; // ANDREA +uint8_t bnrg_expansion_board = IDB04A1; /* at startup, suppose the X-NUCLEO-IDB04A1 is used */ + Gap::Address_t bleAddr; Gap::AddressType_t addr_type = Gap::ADDR_TYPE_PUBLIC; @@ -99,6 +104,8 @@ DEBUG("btle_init>>\n\r"); int ret; + uint8_t hwVersion; + uint16_t fwVersion; uint16_t service_handle, dev_name_char_handle, appearance_char_handle; /* Delay needed only to be able to acces the JTAG interface after reset @@ -111,6 +118,21 @@ /* Reset BlueNRG SPI interface */ BlueNRG_RST(); + /* get the BlueNRG HW and FW versions */ + getBlueNRGVersion(&hwVersion, &fwVersion); + + /* + * Reset BlueNRG again otherwise we won't + * be able to change its MAC address. + * aci_hal_write_config_data() must be the first + * command after reset otherwise it will fail. + */ + BlueNRG_RST(); + + if (hwVersion > 0x30) { /* X-NUCLEO-IDB05A1 expansion board is used */ + bnrg_expansion_board = IDB05A1; + } + /* The Nucleo board must be configured as SERVER */ //check if isSetAddress is set than set address. // ANDREA @@ -139,11 +161,12 @@ PRINTF("GATT_Init failed.\n"); } //GAP is always in PERIPHERAL _ROLE as mbed does not support Master role at the moment -#ifdef BLUENRG_MS - ret = aci_gap_init(GAP_PERIPHERAL_ROLE, 0, 0x07, &service_handle, &dev_name_char_handle, &appearance_char_handle); -#else - ret = aci_gap_init(GAP_PERIPHERAL_ROLE, &service_handle, &dev_name_char_handle, &appearance_char_handle); -#endif + if (bnrg_expansion_board == IDB05A1) { + ret = aci_gap_init_IDB05A1(GAP_PERIPHERAL_ROLE_IDB05A1, 0, 0x07, &service_handle, &dev_name_char_handle, &appearance_char_handle); + } else { + ret = aci_gap_init_IDB04A1(GAP_PERIPHERAL_ROLE_IDB04A1, &service_handle, &dev_name_char_handle, &appearance_char_handle); + } + if(ret != BLE_STATUS_SUCCESS){ PRINTF("GAP_Init failed.\n"); } @@ -228,6 +251,65 @@ //HAL_GPIO_EXTI_Callback_Poll(BNRG_SPI_EXTI_PIN); return; } + +void Attribute_Modified_CB(uint16_t attr_handle, uint8_t data_length, uint8_t *att_data, uint8_t offset) +{ + //Extract the GattCharacteristic from p_characteristics[] and find the properties mask + GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(attr_handle); + if(p_char!=NULL) { + GattAttribute::Handle_t charHandle = p_char->getValueAttribute().getHandle(); + BlueNRGGattServer::HandleEnum_t currentHandle = BlueNRGGattServer::CHAR_HANDLE; + DEBUG("CharHandle %d, length: %d, Data: %d\n\r", charHandle, data_length, (uint16_t)att_data[0]); + DEBUG("getProperties 0x%x\n\r",p_char->getProperties()); + if(attr_handle == charHandle+CHAR_VALUE_OFFSET) { + currentHandle = BlueNRGGattServer::CHAR_VALUE_HANDLE; + } + + if(attr_handle == charHandle+CHAR_DESC_OFFSET) { + currentHandle = BlueNRGGattServer::CHAR_DESC_HANDLE; + } + DEBUG("currentHandle %d\n\r", currentHandle); + if((p_char->getProperties() & + (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) && + currentHandle == BlueNRGGattServer::CHAR_DESC_HANDLE) { + + DEBUG("*****NOTIFICATION CASE\n\r"); + //Now Check if data written in Enable or Disable + if((uint16_t)att_data[0]==1) { + //DEBUG("Notify ENABLED\n\r"); + BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_ENABLED, p_char->getValueAttribute().getHandle()); + } else { + //DEBUG("Notify DISABLED\n\r"); + BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_DISABLED, p_char->getValueAttribute().getHandle()); + } + } + + //Check is attr handle property is WRITEABLE, if yes, generate GATT_EVENT_DATA_WRITTEN Event + if((p_char->getProperties() & + (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)) && + currentHandle == BlueNRGGattServer::CHAR_VALUE_HANDLE) { + + DEBUG("*****WRITE CASE\n\r"); + + GattWriteCallbackParams writeParams; + writeParams.handle = p_char->getValueAttribute().getHandle(); + writeParams.writeOp = GattWriteCallbackParams::OP_WRITE_REQ;//Where to find this property in BLUENRG? + writeParams.len = data_length; + writeParams.data = att_data; + if (bnrg_expansion_board == IDB05A1) { + writeParams.offset = offset; + } + BlueNRGGattServer::getInstance().HCIDataWrittenEvent(&writeParams); + + //BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_WRITTEN, evt->attr_handle); + //Write the actual Data to the Attr Handle? (uint8_1[])att_data contains the data + if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) { + BlueNRGGattServer::getInstance().write(p_char->getValueAttribute().getHandle(), (uint8_t*)att_data, data_length, false); + } + } + } + +} #ifdef __cplusplus extern "C" { @@ -244,7 +326,6 @@ */ /**************************************************************************/ extern void HCI_Event_CB(void *pckt) { - hci_uart_pckt *hci_pckt = (hci_uart_pckt*)pckt; hci_event_pckt *event_pckt = (hci_event_pckt*)hci_pckt->data; @@ -319,67 +400,15 @@ case EVT_BLUE_GATT_ATTRIBUTE_MODIFIED: { + DEBUG("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED\n\r"); /* this callback is invoked when a GATT attribute is modified extract callback data and pass to suitable handler function */ - evt_gatt_attr_modified *evt = (evt_gatt_attr_modified*)blue_evt->data; - DEBUG("EVT_BLUE_GATT_ATTRIBUTE_MODIFIED\n\r"); - - //Extract the GattCharacteristic from p_characteristics[] and find the properties mask - GattCharacteristic *p_char = BlueNRGGattServer::getInstance().getCharacteristicFromHandle(evt->attr_handle); - if(p_char!=NULL) { - GattAttribute::Handle_t charHandle = p_char->getValueAttribute().getHandle(); - BlueNRGGattServer::HandleEnum_t currentHandle = BlueNRGGattServer::CHAR_HANDLE; - DEBUG("CharHandle %d, length: %d, Data: %d\n\r",charHandle, evt->data_length, (uint16_t)evt->att_data[0]); - DEBUG("getProperties 0x%x\n\r",p_char->getProperties()); - if(evt->attr_handle == charHandle+CHAR_VALUE_OFFSET) { - currentHandle = BlueNRGGattServer::CHAR_VALUE_HANDLE; - } - if(evt->attr_handle == charHandle+CHAR_DESC_OFFSET) { - currentHandle = BlueNRGGattServer::CHAR_DESC_HANDLE; - } - DEBUG("currentHandle %d\n\r", currentHandle); - if((p_char->getProperties() & - (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE)) && - currentHandle == BlueNRGGattServer::CHAR_DESC_HANDLE) { - - DEBUG("*****NOTIFICATION CASE\n\r"); - //Now Check if data written in Enable or Disable - if((uint16_t)evt->att_data[0]==1) { - //DEBUG("Notify ENABLED\n\r"); - BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_ENABLED, p_char->getValueAttribute().getHandle()); - } - else { - //DEBUG("Notify DISABLED\n\r"); - BlueNRGGattServer::getInstance().HCIEvent(GattServerEvents::GATT_EVENT_UPDATES_DISABLED, p_char->getValueAttribute().getHandle()); - } - } - - //Check is attr handle property is WRITEABLE, if yes, generate GATT_EVENT_DATA_WRITTEN Event - if((p_char->getProperties() & - (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)) && - currentHandle == BlueNRGGattServer::CHAR_VALUE_HANDLE) { - - DEBUG("*****WRITE CASE\n\r"); - - GattWriteCallbackParams writeParams; - writeParams.handle=p_char->getValueAttribute().getHandle(); - writeParams.writeOp=GattWriteCallbackParams::OP_WRITE_REQ;//Where to find this property in BLUENRG? - writeParams.len=evt->data_length; - writeParams.data=evt->att_data; - #ifdef BLUENRG_MS - writeParams.offset=evt->offset;//Not used in BlueNRG? - #endif - BlueNRGGattServer::getInstance().HCIDataWrittenEvent(&writeParams); - - //BlueNRGGattServer::getInstance().handleEvent(GattServerEvents::GATT_EVENT_DATA_WRITTEN, evt->attr_handle); - //Write the actual Data to the Attr Handle? (uint8_1[])evt->att_data contains the data - if ((p_char->getValueAttribute().getValuePtr() != NULL) && (p_char->getValueAttribute().getInitialLength() > 0)) { - BlueNRGGattServer::getInstance().write(p_char->getValueAttribute().getHandle(), - (uint8_t*)evt->att_data, - evt->data_length, - false /* localOnly */); - } - } + if (bnrg_expansion_board == IDB05A1) { + evt_gatt_attr_modified_IDB05A1 *evt = (evt_gatt_attr_modified_IDB05A1*)blue_evt->data; + Attribute_Modified_CB(evt->attr_handle, evt->data_length, evt->att_data, evt->offset); + } else { + evt_gatt_attr_modified_IDB04A1 *evt = (evt_gatt_attr_modified_IDB04A1*)blue_evt->data; + Attribute_Modified_CB(evt->attr_handle, evt->data_length, evt->att_data, 0); } } break;