From repo, no change

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_LED_IDB0XA1 by ST

Revision:
7:8490fe113598
Parent:
3:e0efdb741bd4
Child:
8:60033323cbcb
--- a/main.cpp	Fri Sep 16 10:36:43 2016 +0000
+++ b/main.cpp	Tue Apr 25 15:26:50 2017 +0000
@@ -16,44 +16,77 @@
 
 #include "mbed.h"
 #include "ble/BLE.h"
-#include "LEDService.h"
+#include "ble/services/UARTService.h"
+#include "Serial.h"
+
+#define UART_BUFFER (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN*10)
 
-DigitalOut actuatedLED(LED1, 0);
-
-const static char     DEVICE_NAME[] = "LED";
-static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
+const static char DEVICE_NAME[] = "UART";
+UARTService *uartServicePtr;
+static uint8_t uartBuff[UART_BUFFER];
+static uint8_t uartBuffPos=0;
 
-LEDService *ledServicePtr;
+void onBleError(ble_error_t error);
+
+DigitalOut led1(LED1);
+Serial uart1(USBTX,USBRX);
+Ticker ticker;
 
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+#define DEBUG uart1.printf
+
+/* Ticker */
+void periodicCallback(void)
 {
-    (void)params;
-    BLE::Instance().gap().startAdvertising(); // restart advertising
+    led1 = !led1;
 }
 
-/**
- * This callback allows the LEDService to receive updates to the ledState Characteristic.
- *
- * @param[in] params
- *     Information about the characterisitc being updated.
- */
-void onDataWrittenCallback(const GattWriteCallbackParams *params) {
-    if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
-        actuatedLED = *(params->data);
+/* UART */
+void uartRx(void)
+{
+    if(uart1.readable()){
+        uartBuff[uartBuffPos] = uart1.getc();
+        if((uartBuff[uartBuffPos] == '\r') || (uartBuff[uartBuffPos] == '\n') || (uartBuffPos >= UART_BUFFER)) {
+            uartBuff[uartBuffPos] = '\0';
+            /* We are sending the whole string even if less than BLE_UART_SERVICE_MAX_DATA_LEN otherwise we need to wait */
+            uartServicePtr->write(uartBuff, (uartBuffPos/UARTService::BLE_UART_SERVICE_MAX_DATA_LEN +1) * UARTService::BLE_UART_SERVICE_MAX_DATA_LEN);
+            DEBUG("TX : %s\r\n", uartBuff);
+            memset(uartBuff, 0, UART_BUFFER);
+            uartBuffPos = 0;
+        }
+        else
+            uartBuffPos++;
     }
 }
 
-/** 
- * This function is called when the ble initialization process has failled 
- */ 
-void onBleInitError(BLE &ble, ble_error_t error) 
-{ 
-    /* Initialization error handling should go here */ 
+
+/* BLE */
+void BleConnectionCallback(const Gap::ConnectionCallbackParams_t *params) {
+    DEBUG("BLE Client Connected!\n\r");
+    DEBUG("Please type a string and press return\r\n");
+
+    ticker.attach(periodicCallback, 1);
+}
+
+void BleDisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    (void)params;
+    DEBUG("BLE Client Disconnected!\r\n");
+    ticker.detach();
+    BLE::Instance().gap().startAdvertising(); // restart advertising
+    led1=0;
+}
+
+void BleOnDataWrittenCallback(const GattWriteCallbackParams *params) {
+    if (params->handle == uartServicePtr->getTXCharacteristicHandle()){
+        DEBUG("RX: %s\r\n", params->data);
+    }
+}
+
+void onBleError(ble_error_t error) { 
+    DEBUG("BLE Error: %d\r\n");
+    /* Handle error now */
 } 
 
-/** 
- * Callback triggered when the ble initialization process has finished 
- */ 
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 
 {
     BLE&        ble   = params->ble;
@@ -61,7 +94,7 @@
 
     if (error != BLE_ERROR_NONE) {
         /* In case of error, forward the error handling to onBleInitError */
-        onBleInitError(ble, error);
+        onBleError(error);
         return;
     }
 
@@ -70,18 +103,22 @@
         return;
     }
 
-    ble.gap().onDisconnection(disconnectionCallback);
-    ble.gattServer().onDataWritten(onDataWrittenCallback);
+    ble.gap().onConnection(BleConnectionCallback);
+    ble.gap().onDisconnection(BleDisconnectionCallback);
+    ble.gattServer().onDataWritten(BleOnDataWrittenCallback);
 
-    bool initialValueForLEDCharacteristic = true;
-    ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
+    DEBUG("BLE UARTService: ");
+    /* Setup primary service. */
+    UARTService uartService(ble);
+    uartServicePtr = &uartService;
+    DEBUG("Started\r\n");
 
     /* setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
+    ble.gap().setAdvertisingInterval(500); /* 500ms. */
     ble.gap().startAdvertising();
 
     while (true) {
@@ -91,8 +128,11 @@
 
 int main(void)
 {
+    led1=0;
+        
+    uart1.baud(9600);
+    uart1.attach(uartRx,Serial::RxIrq);
     BLE &ble = BLE::Instance();
-    
     ble.init(bleInitComplete);
 }