BLE Client UART function

Dependencies:   RingBuffer

This is a BLE Client (Central) program for nRF51.
You can communicate with mbed BLE using "BLE_Uart_Server" program as follows.
/users/kenjiArai/code/BLE_Uart_Server/
Please refer following my notebook.
/users/kenjiArai/notebook/ble-client-and-peripheral-using-switch-sience-ty51/#

Revision:
1:f68a5e55a60e
Parent:
0:0ef6455cbb4d
Child:
2:6fb0b87b041d
--- a/main.cpp	Sat Apr 16 07:32:21 2016 +0000
+++ b/main.cpp	Thu Apr 21 11:20:46 2016 +0000
@@ -7,16 +7,16 @@
  *      http://mbed.org/users/kenjiArai/
  *
  *      Started:  April     8th, 2016
- *      Revised:  April    16th, 2016
+ *      Revised:  April    21st, 2016
  *
  *  Original program (see original.cpp file):
  *      S130 potential unstability case [closed] by Fabien Comte
  *      https://devzone.nordicsemi.com/question/49705/s130-potential-unstability-case/
  *      GitHub Q&A by Fabien COMTE
  *      https://github.com/ARMmbed/ble/issues/69
- *  Tested Peripheral Device:
- *      BLE_Uart_Peripheral
- *      https://developer.mbed.org/users/kenjiArai/code/BLE_Uart_Peripheral/
+ *  Tested Server Device:
+ *      BLE_Uart_Server
+ *      https://developer.mbed.org/users/kenjiArai/code/BLE_Uart_Server/
  *
  */
 
@@ -53,6 +53,8 @@
 const Gap::Address_t    mac_board_0   = {0x59, 0x2c, 0xa8, 0x0e, 0xe2, 0xef};
 const Gap::Address_t    mac_board_1   = {0x50, 0x2b, 0xea, 0x14, 0x95, 0xd2};
 const Gap::Address_t    mac_board_2   = {0x30, 0x74, 0x6d, 0xbd, 0x83, 0xf4};
+const Gap::Address_t    mac_board_3   = {0x0f, 0x72, 0xbf, 0x43, 0xbc, 0xd0};
+const Gap::Address_t    mac_board_4   = {0x98, 0x1a, 0x92, 0x10, 0x7f, 0xd5};
 #warning "You need to confirm your device name."
 const static char       DEVICE_NAME[] = "UART_C";
 static const uint16_t   uuid16_list[] = {UARTServiceShortUUID};
@@ -62,7 +64,7 @@
 DiscoveredCharacteristic uartTXCharacteristic;
 DiscoveredCharacteristic uartRXCharacteristic;
 bool            foundUartRXCharacteristic = false;
-bool            connected2peripheral      = false;
+bool            connected2server      = false;
 UARTService *   uartServicePtr            = NULL;
 Gap::Address_t  my_mac;
 int             my_board_index            = -1;
@@ -105,7 +107,7 @@
     ticker.attach(periodicCallback, 1);
     for (int k = 0; k < 20; k++) { pc.printf("\r\n");}          // clear terminal output
     pc.printf("UART Communication / Client side\r\n");          // opening message
-    pc.printf("  Client(Central) and Peripheral(device)\r\n");  // opening message
+    pc.printf("  Client(Central) and Server(Peripheral)\r\n");  // opening message
     // Mixed role *************************************************************
     ble.init();
     Gap::AddressType_t my_mac_type;
@@ -118,7 +120,7 @@
     );
     ble.gap().onConnection(connectionCallback);
     ble.gap().onDisconnection(disconnectionCallback);
-    // Peripheral(Device) role ************************************************
+    // Server(Peripheral) role ************************************************
     ble.gattServer().onDataWritten(onReceivedDataFromCentralCallback);
     UARTService uartService(ble);
     uartServicePtr = &uartService;
@@ -142,7 +144,7 @@
     ble.startAdvertising();                     // start advertising and scan
     ble.gap().startScan(advertisementCallback);
     while(true) {
-        // allow notifications from Peripheral(device)
+        // allow notifications from Server(Peripheral)
         if (foundUartRXCharacteristic && !ble.gattClient().isServiceDiscoveryActive()){
             foundUartRXCharacteristic = false; // need to do the following only once
             uint16_t value = BLE_HVX_NOTIFICATION;
@@ -170,7 +172,7 @@
         }
         if (line_input == true){
             line_input = false;
-            // Client to Peripheral(central to device)
+            // Client to Server(central to peripheral)
             adjust_line(linebuf);
             int ret = uartTXCharacteristic.write(20, linebuf);
             if (ret == BLE_ERROR_NONE){
@@ -182,7 +184,7 @@
             } else  {
                 DEBUG("\r\ncode %d (c2d)\r\n", ret);
             }
-            // Peripheral to Client(device to central)
+            // Server to Client(peripheral to central)
             if (ble.gap().getState().connected){
                 int ret = ble.gattServer().write(
                     uartServicePtr->getRXCharacteristicHandle(),
@@ -242,7 +244,7 @@
     *(bf + 1) = 0;
 }
 
-// Peripheral(Device) role ****************************************************
+// Server(Peripheral) role ****************************************************
 void onReceivedDataFromCentralCallback(const GattWriteCallbackParams *params){
     if (uartServicePtr != NULL){
         if ((params->handle ==
@@ -274,7 +276,7 @@
 
 void periodicCallback(void){
     alivenessLED = !alivenessLED; // Do blinky on alivenessLED to indicate system aliveness
-    if (connected2peripheral){
+    if (connected2server){
         connectedLED = 1;
     } else {
         connectedLED = 0;
@@ -302,6 +304,8 @@
     if (mac_equals(mac, mac_board_0)) {     return 0;}
     if (mac_equals(mac, mac_board_1)) {     return 1;}
     if (mac_equals(mac, mac_board_2)) {     return 2;}
+    if (mac_equals(mac, mac_board_3)) {     return 3;}
+    if (mac_equals(mac, mac_board_4)) {     return 4;}
     return -1;
 }
 
@@ -372,7 +376,7 @@
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params){
     if (params->role == Gap::CENTRAL) {
         DEBUG("connected as Client(central) (handle = %d)\r\n\r", params->handle);
-        connected2peripheral = true;
+        connected2server = true;
         connectionHandle = params->handle;
         ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
         ble.gattClient().launchServiceDiscovery(
@@ -382,7 +386,7 @@
                 0xa000, 0xa001*/
         );
     } else {
-        DEBUG("connected as device (handle = %d)\r\n\r", params->handle);
+        DEBUG("connected as Server (handle = %d)\r\n\r", params->handle);
         DEBUG(
             "Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n",
             params->connectionParams->minConnectionInterval,
@@ -413,7 +417,7 @@
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
     DEBUG("handle = %d ", params->handle);
     pc.printf(" -> disconnected\r\n", params->handle);
-    connected2peripheral = false;
+    connected2server = false;
     if (params->handle == SOFT_DEVICE_FATHER_HANDLE) {
         ble.startAdvertising();                         // restart advertising
     } else {