Dependencies:   BLE_API mbed nRF51822

Fork of eco_Labs_ble_Client by Happiest

Revision:
10:09d1a403eb14
Child:
11:b8e687d97537
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ble_uart.cpp	Tue Oct 04 11:05:45 2016 +0000
@@ -0,0 +1,153 @@
+/**
+  ******************************************************************************
+  * @file    ble_uart.cpp
+  * @author  Happiesstminds Firmware Team
+  * @version v1.0
+  * @date    4-Oct-2016
+  * @brief   
+  *
+  ******************************************************************************
+  * @attention
+  *  
+  *
+  ******************************************************************************
+  */
+
+/******************************************************************************/
+/* Include Files*/
+/******************************************************************************/
+#include <string.h>
+#include "mbed.h"
+#include "BLE.h"
+#include "ble_types.h"
+#include "UARTService.h"
+
+/******************************************************************************/
+/* Local Defines                                                              */
+/******************************************************************************/
+#define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
+
+/******************************************************************************/
+/* Global Variables                                                           */
+/******************************************************************************/
+bool isDeviceConnected = false;
+
+/******************************************************************************/
+/* Static Variable Declarations */
+/******************************************************************************/
+static BLEDevice  ble;
+static UARTService *uart;
+static ble_data_ready_callback_t data_ready_cb;
+
+/******************************************************************************/
+/* Static Functions                                                           */
+/******************************************************************************/
+/**
+ * @brief  Device Connection callback
+ * @param  
+ * @retval 
+ */
+static void ble_connectionCallback(const Gap::ConnectionCallbackParams_t *params)
+{
+    isDeviceConnected = true;
+}
+
+/**
+ * @brief  Device disconnection callback
+ * @param  
+ * @retval 
+ */
+static void ble_disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    DEBUG("Disconnected!\n\r");
+    DEBUG("Restarting the advertising process\n\r");
+    isDeviceConnected = false;
+    ble.startAdvertising();
+}
+
+/**
+ * @brief  onDataWrittenCallback - On data received callback???
+ * @param  
+ * @retval 
+ */
+static void ble_dataReceiveCallback(const GattWriteCallbackParams *params)                         
+{    
+    uint8_t buf[32] = {0};
+    DEBUG("onDataReceiveCallback\n\r");
+    
+    memcpy(buf, params->data, params->len);
+        
+    if (data_ready_cb != NULL) {
+        data_ready_cb(buf, sizeof(buf));
+    }
+}
+
+/******************************************************************************/
+/* Global Functions                                                           */
+/******************************************************************************/
+/**
+ * @brief  Initializes the BLE module
+ * @param  none
+ * @retval none
+ */
+void ble_init(void)
+{
+    DEBUG("Initialising the nRF51822\n\r");
+    ble.init();
+    ble.onConnection(ble_connectionCallback);
+    ble.onDisconnection(ble_disconnectionCallback);
+    ble.onDataWritten(ble_dataReceiveCallback);
+
+    uart = new UARTService(ble);
+
+    /* setup advertising */
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
+                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
+
+    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
+    ble.startAdvertising();
+}
+
+/**
+ * @brief  Function used to register ble data receive callback 
+ * @param  none
+ * @retval none
+ */
+void ble_data_rx_cb_register(ble_data_ready_callback_t data_rx_callback)
+{
+    data_ready_cb = data_rx_callback;
+}
+
+/**
+ * @brief  Send data over BLE UART
+ * @param  
+ * @retval 
+ */
+eStatus_t ble_send_data(uint8_t *tx_buf, uint8_t length)
+{
+    eStatus_t ret_code = eSuccess;
+    if (isDeviceConnected) {
+        uart->write(tx_buf, length);
+    } else {
+        ret_code = eFailure;
+    }
+    return ret_code;
+}
+
+/**
+ * @brief  Wait for the BLE events
+ * @param  
+ * @retval 
+ */
+void ble_event_wait(void)
+{
+    ble.waitForEvent();
+}
+
+/******************************************************************************/
+/* END OF FILE                                                                */
+/******************************************************************************/