BLE Nano Code.Tested with Integrated mDot code

Dependencies:   BLE_API mbed nRF51822

Fork of eco_Labs_ble_Client by Happiest

ble_uart.cpp

Committer:
jinu
Date:
2016-10-07
Revision:
14:dc3e3d327d7c
Parent:
13:c42bd4ebbbfa
Child:
15:67fbed00edf2

File content as of revision 14:dc3e3d327d7c:

/**
  ******************************************************************************
  * @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)); }

#define BLE_DEVICE_NAME "Ecolab-ICE"

/******************************************************************************/
/* Global Variables                                                           */
/******************************************************************************/

extern void enable_interrupt_line(bool status );
extern void enable_interrupt_line(bool status );
void spi_slave_tx_data(uint8_t *tx_buf, uint8_t len);
bool isDeviceConnected = false;


#define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }

/******************************************************************************/
/* 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)
{    
    uint8_t tx_buf[4];
    tx_buf[0] = 0x01;
    tx_buf[1] = 0xA3;
    tx_buf[2] = 0x00;
    tx_buf[3] = 0x04;
    spi_slave_tx_data(tx_buf, 4);
    isDeviceConnected = true;
 //   enable_interrupt_line(true);    
    toggle_led();
}

/**
 * @brief  Device disconnection callback
 * @param  
 * @retval 
 */
static void ble_disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{    
    uint8_t tx_buf[4];
    tx_buf[0] = 0x01;
    tx_buf[1] = 0xA6;
    tx_buf[2] = 0x00;
    tx_buf[3] = 0x04;
    spi_slave_tx_data(tx_buf, 4);
    
    isDeviceConnected = false;
    //toggle_led();
  //  enable_interrupt_line(false);  
    ble.startAdvertising();
}

/**
 * @brief  onDataWrittenCallback - On data received callback???
 * @param  
 * @retval 
 */
eStatus_t ble_send_data(uint8_t *tx_buf, uint8_t length);
 
static void ble_dataReceiveCallback(const GattWriteCallbackParams *params)                         
{    

    uint8_t buf[32] = {0};
    
    memcpy(buf, params->data, params->len);
        
    if (data_ready_cb != NULL) {
        data_ready_cb(buf, sizeof(buf));
    }
    
    ble_send_data((uint8_t *)params->data, params->len);

}

/******************************************************************************/
/* Global Functions                                                           */
/******************************************************************************/
void print(const char *msg)
{
    DEBUG(msg);
}
/**
 * @brief  Initializes the BLE module
 * @param  none
 * @retval none
 */
void ble_init(void)
{    
    ble.init();
    ble.onConnection(ble_connectionCallback);
    ble.onDisconnection(ble_disconnectionCallback);
    ble.onDataWritten(ble_dataReceiveCallback);

    uart = new UARTService(ble);
#if 0
    /* setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                     (const uint8_t *)BLE_DEVICE_NAME, sizeof(BLE_DEVICE_NAME) - 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. */
#endif

    /* Setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
    ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS, (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                                                                */
/******************************************************************************/