this is a new program based on BLE_Uart_Peripheral. Change program name due to relation Client&Peripheral to Client&Server

Dependencies:   RingBuffer nRF51_Vdd nRF51_WakeUp

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

Revision:
7:0d94f4cf2a69
Parent:
6:16e1f9d065a3
Child:
8:dd23c6d67b58
--- a/main.cpp	Sun Oct 22 09:47:18 2017 +0000
+++ b/main.cpp	Fri Feb 09 22:31:30 2018 +0000
@@ -20,19 +20,23 @@
  *      --- Tested on Switch Science mbed TY51822r3 ---
  *
  *      http://www.page.sannet.ne.jp/kenjia/index.html
- *      http://mbed.org/users/kenjiArai/
+ *      https://os.mbed.com/users/kenjiArai/
  *
  *      Started:  March     7th, 2016
  *      Revised:  June     13th, 2016
  *      Revised:  October  22nd, 2017   Run on mbed-OS-5.6.2
+ *      Revised:  Feburary 10th, 2018   Not set mac addr but use device name
  *
  *  Original program:
  *      BLE_LoopbackUART
  *      https://developer.mbed.org/teams/Bluetooth-Low-Energy/
  *                                  code/BLE_LoopbackUART/
- *  Tested Controller Device:
+ *  Reference program:
+ *      BLE_Peripheral_test by noboru koshinaka
+ *      https://os.mbed.com/users/noboruk/code/BLE_Peripheral_test/
+ *  Tested Client Device:
  *      BLE_Uart_Client
- *      https://developer.mbed.org/users/kenjiArai/code/BLE_Uart_Client/
+ *      https://os.mbed.com/users/kenjiArai/code/BLE_Uart_Client/
  */
 
 //  Include --------------------------------------------------------------------
@@ -44,13 +48,15 @@
 #include "RingBuffer.h"
 
 //  Definition -----------------------------------------------------------------
+//#define     USE_MAC           // if you use mac address, please define it
+
 #define     NUM_ONCE            20
 #define     BFSIZE              (NUM_ONCE+4)
 
 // If you set 1, you need to connected LED1 and P_0
 // Please refer nRF51_WakeUP library
-#define     GOTO_SLEEP_MODE     0   
-                 
+#define     GOTO_SLEEP_MODE     0
+
 //#define    USE_DEBUG_MODE
 #ifdef USE_DEBUG_MODE
 #define DEBUG(...) { printf(__VA_ARGS__); }
@@ -59,12 +65,13 @@
 #endif
 
 //  Object ---------------------------------------------------------------------
-BLE             ble;
+BLE&            ble_uart = BLE::Instance();
 DigitalOut      connectedLED(LED2);
 InterruptIn     wake_up_sw(P0_1);
 nRF51_WakeUp    wakeup(LED1, P0_0);
 nRF51_Vdd       vdd(3.0f, 2.2f);
 Serial          pc(USBTX, USBRX, 115200);
+//Serial        pc(P0_3, P0_1, 115200);     // for another board
 UARTService     *uartServicePtr;
 Ticker          ticker;
 RingBuffer      ser_bf(1024);
@@ -73,7 +80,7 @@
 
 //  ROM / Constant data --------------------------------------------------------
 #warning "You need to confirm your device name."
-const static char DEVICE_NAME[] = "UART_S";
+const static char DEVICE_NAME[] = "UART_PJL";
 
 //  RAM ------------------------------------------------------------------------
 Gap::Address_t  my_mac;
@@ -120,85 +127,101 @@
     ticker.attach(periodicCallback, 1);
     tsk.start(pc_ser_rx);
     // clear terminal output
-    for (int k = 0; k < 5; k++) { pc.printf("\r\n");}
-    // opening message          
+    for (int k = 0; k < 3; k++) {
+        pc.printf("\r\n");
+    }
+    // opening message
     pc.printf("UART Communication / Server(Peripheral) side\r\n");
     pc.printf("  need Client module (run BLE_Uart_Client program)\r\n");
     // Interrupt by switch
     wake_up_sw.fall(&interrupt_by_sw);
-    ble.init();
+    ble_uart.init();
     Gap::AddressType_t my_mac_type;
-    ble.gap().getAddress(&my_mac_type, my_mac);
+    ble_uart.gap().getAddress(&my_mac_type, my_mac);
     DEBUG(
         "  my_MAC %02x:%02x:%02x:%02x:%02x:%02x (%s)\r\n",
         my_mac[5], my_mac[4], my_mac[3], my_mac[2], my_mac[1], my_mac[0],
         (my_mac_type == Gap::ADDR_TYPE_PUBLIC) ? "public" : "random"
     );
     pc.printf(
-        "  mac_board_n = {0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x};\r\n",
+        "  My device name : %s\r\n", DEVICE_NAME);
+    pc.printf(
+        "  My mac data %02x:%02x:%02x:%02x:%02x:%02x\r\n",
+        my_mac[5], my_mac[4], my_mac[3], my_mac[2], my_mac[1], my_mac[0]
+    );
+#ifdef USE_MAC
+    pc.printf(
+        "  mac_board_x   = {0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x};\r\n",
         my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5]
     );
     pc.printf(
-        "  Please write above data(mac_board_n line) into mac_board_0\r\n");
+        "  Please write above data(mac_board_x line (x=0,1,2,...))\r\n");
     pc.printf(
-        "    or follows(n=1,2,...)) in Client/main.cpp\r\n");
-    ble.onDisconnection(disconnectionCallback);
-    ble.onDataWritten(onDataWritten_action);
+        "  into Client/main.cpp [ROM / Constant data] area\r\n");
+#endif
+    ble_uart.onDisconnection(disconnectionCallback);
+    ble_uart.onDataWritten(onDataWritten_action);
     /* setup advertising */
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(
+    ble_uart.accumulateAdvertisingPayload(
+        GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    ble_uart.setAdvertisingType(
+        GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble_uart.accumulateAdvertisingPayload(
         GapAdvertisingData::COMPLETE_LOCAL_NAME,
         (const uint8_t *)DEVICE_NAME,
         sizeof(DEVICE_NAME)
     );
-    ble.accumulateAdvertisingPayload(
+    ble_uart.accumulateAdvertisingPayload(
         GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
         (const uint8_t *)UARTServiceUUID_reversed,
-         sizeof(UARTServiceUUID_reversed)
+        sizeof(UARTServiceUUID_reversed)
     );
     // Advertize Interval
-    ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
+    ble_uart.setAdvertisingInterval(1000); /* 1000ms;in multiples of 0.625ms.*/
     // Start
-    ble.startAdvertising();
-    UARTService uartService(ble);
+    ble_uart.startAdvertising();
+    UARTService uartService(ble_uart);
     uartServicePtr = &uartService;
-    while(true){
-        if (time_out){
+    while(true) {
+        if (time_out) {
 #if GOTO_SLEEP_MODE
             wakeup.set_and_wait(sleep_time);
-            while(true){    // never come here but just in case
+            while(true) {   // never come here but just in case
                 deepsleep();
             }
 #endif
         }
-        if (trigger_transmit){
+        if (trigger_transmit) {
             static uint8_t cmd_buf[BFSIZE];
             static volatile bool   flag_continue = 0;
             trigger_transmit = false;
             pc.printf((const char*)rx_buf);
-            if (flag_continue == true){
+            if (flag_continue == true) {
                 strcat((char *)cmd_buf, (char *)rx_buf);
-                if (strchr((const char*)cmd_buf,(int)'\r') == 0){
+                if (strchr((const char*)cmd_buf,(int)'\r') == 0) {
                     flag_continue = true;
                 } else {
                     command(cmd_buf);
-                    for(uint8_t i = 0; i < BFSIZE; i++){ cmd_buf[i] = 0;}
+                    for(uint8_t i = 0; i < BFSIZE; i++) {
+                        cmd_buf[i] = 0;
+                    }
                     flag_continue = false;
                 }
             }
-            if ((rx_buf[0] == '~')){
+            if ((rx_buf[0] == '~')) {
                 strcpy((char *)cmd_buf, (char *)rx_buf);
-                if (strchr((const char*)cmd_buf,(int)'\r') == 0){
+                if (strchr((const char*)cmd_buf,(int)'\r') == 0) {
                     flag_continue = true;
                 } else {
                     command(cmd_buf);
-                    for(uint8_t i = 0; i < BFSIZE; i++){ cmd_buf[i] = 0;}
-                    flag_continue = false; 
+                    for(uint8_t i = 0; i < BFSIZE; i++) {
+                        cmd_buf[i] = 0;
+                    }
+                    flag_continue = false;
                 }
             }
         }
-        ble.waitForEvent();        
+        ble_uart.waitForEvent();
     }
 }
 
@@ -206,12 +229,16 @@
 {
     uint8_t *p = cmd;
 
-    while(*p == ' '){ ++p;}  // skip space
-    if (*p++ == '~'){
-        while(*p < '!'){ ++p;}  // skip space
+    while(*p == ' ') {
+        ++p;   // skip space
+    }
+    if (*p++ == '~') {
+        while(*p < '!') {
+            ++p;   // skip space
+        }
         uint8_t c = *p;
         //pc.printf("c=%c\r\n", c);
-        switch (c){
+        switch (c) {
             case 'v':
                 action_tx_vdd();
                 break;
@@ -238,11 +265,11 @@
 void periodicCallback(void)
 {
 #if GOTO_SLEEP_MODE
-    if (--time_out_cntr == 0){
+    if (--time_out_cntr == 0) {
         time_out = true;
     }
 #endif
-    if (rx_isr_busy == true){
+    if (rx_isr_busy == true) {
         rx_isr_busy = false;
     } else {
         tsk.signal_set(0x01);
@@ -261,15 +288,15 @@
     static uint8_t linebf_irq[BFSIZE];
     static volatile uint8_t linebf_irq_len = 0;
 
-    while(true){
+    while(true) {
         Thread::signal_wait(0x01);
-        if (ser_bf.check() == 0){
-            if (linebf_irq_len != 0){
+        if (ser_bf.check() == 0) {
+            if (linebf_irq_len != 0) {
                 linebf_irq[linebf_irq_len] = 0;
                 adjust_line(linebf_irq);
                 linebf_irq_len = 0;
                 bletx_mutex.lock();
-                ble.updateCharacteristicValue(
+                ble_uart.updateCharacteristicValue(
                     uartServicePtr->getRXCharacteristicHandle(),
                     linebf_irq,
                     NUM_ONCE
@@ -277,17 +304,17 @@
                 bletx_mutex.unlock();
             }
         }
-        while(ser_bf.check() != 0){
+        while(ser_bf.check() != 0) {
             char c = ser_bf.read();
-            if (c == '\b'){
+            if (c == '\b') {
                 linebf_irq_len--;
                 pc.putc(c);
                 pc.putc(' ');
                 pc.putc(c);
-            } else if ((c >= ' ') || (c == '\r') || (c == '\n')){
+            } else if ((c >= ' ') || (c == '\r') || (c == '\n')) {
                 bool overflow = false;
                 if ((c == '\r') || (c == '\n')) {
-                    if (linebf_irq_len == NUM_ONCE - 1){// remain only 1 buffer
+                    if (linebf_irq_len == NUM_ONCE - 1) { // remain only 1 buffer
                         overflow = true;
                         linebf_irq[linebf_irq_len++] = '\r';
                         pc.putc('\r');
@@ -301,18 +328,18 @@
                     linebf_irq[linebf_irq_len++] = c;
                     pc.putc(c);
                 }
-                if (linebf_irq_len >= NUM_ONCE ){
+                if (linebf_irq_len >= NUM_ONCE ) {
                     linebf_irq[linebf_irq_len] = 0;
                     adjust_line(linebf_irq);
                     linebf_irq_len = 0;
                     bletx_mutex.lock();
-                    ble.updateCharacteristicValue(
+                    ble_uart.updateCharacteristicValue(
                         uartServicePtr->getRXCharacteristicHandle(),
                         linebf_irq,
                         NUM_ONCE
                     );
                     bletx_mutex.unlock();
-                    if (overflow == true){
+                    if (overflow == true) {
                         overflow = false;
                         linebf_irq[linebf_irq_len++] = '\n';
                         pc.putc('\n');
@@ -327,19 +354,22 @@
 {
     uint8_t i, c;
 
-    for (i = 0; i <NUM_ONCE; bf++, i++){
+    for (i = 0; i <NUM_ONCE; bf++, i++) {
         c = *bf;
-        if (c == 0){ break;}
+        if (c == 0) {
+            break;
+        }
     }
-    for (; i < NUM_ONCE; bf++, i++){ *bf = 0x11;}
+    for (; i < NUM_ONCE; bf++, i++) {
+        *bf = 0x11;
+    }
     *(bf + 1) = 0;
 }
 
 void onDataWritten_action(const GattWriteCallbackParams *params)
 {
-    if ((uartServicePtr != NULL) && 
-        (params->handle == uartServicePtr->getTXCharacteristicHandle()))
-    {
+    if ((uartServicePtr != NULL) &&
+            (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
         strcpy((char *)rx_buf, (const char *)params->data);
         trigger_transmit = true;
     }
@@ -347,29 +377,29 @@
 
 void action_tx_help()
 {
-                //          12345678901234567890
+    //          12345678901234567890
     sprintf((char *)tx_buf,"  ~?:help\r\n");
-    tx_len = strlen((const char *)tx_buf); 
+    tx_len = strlen((const char *)tx_buf);
     Update_Values();
     wait(0.2);
-                //          12345678901234567890
+    //          12345678901234567890
     sprintf((char *)tx_buf,"  ~v:vdd\r\n");
-    tx_len = strlen((const char *)tx_buf); 
+    tx_len = strlen((const char *)tx_buf);
     Update_Values();
     wait(0.2);
-                //          12345678901234567890
+    //          12345678901234567890
     sprintf((char *)tx_buf,"  ~t:temperature\r\n");
-    tx_len = strlen((const char *)tx_buf); 
+    tx_len = strlen((const char *)tx_buf);
     Update_Values();
     wait(0.2);
-                //          12345678901234567890
+    //          12345678901234567890
     sprintf((char *)tx_buf,"  ~w:wait, w 120\r\n");
-    tx_len = strlen((const char *)tx_buf); 
+    tx_len = strlen((const char *)tx_buf);
     Update_Values();
     wait(0.2);
-                //          12345678901234567890
+    //          12345678901234567890
     sprintf((char *)tx_buf,"  ~q:quit/sleep\r\n");
-    tx_len = strlen((const char *)tx_buf); 
+    tx_len = strlen((const char *)tx_buf);
     Update_Values();
     wait(0.2);
 }
@@ -403,8 +433,10 @@
 
     p = (char *)(cmd);
     p += 2; // point to time value
-    if (xatoi(&p, &dt)){
-        if (dt <= 5){ dt = 5;}
+    if (xatoi(&p, &dt)) {
+        if (dt <= 5) {
+            dt = 5;
+        }
         sleep_time = dt;    // set next wake-up period
     } else {
         DEBUG("data is unknown!\r\n");
@@ -412,7 +444,7 @@
     }
     DEBUG("slp_t:%d\r\n", sleep_time);
     //pc.printf("slp_t:%d\r\n", sleep_time);
-                //          12345678901234567890
+    //          12345678901234567890
     sprintf((char *)tx_buf, "W: %d sec\r\n", sleep_time);
     tx_len = strlen((const char *)tx_buf);
     Update_Values();
@@ -420,15 +452,15 @@
 
 void action_tx_quit()
 {
-#if GOTO_SLEEP_MODE 
+#if GOTO_SLEEP_MODE
     ticker.detach();
-                //          12345678901234567890
+    //          12345678901234567890
     sprintf((char *)tx_buf,"Terminated the BLE");
-    tx_len = strlen((const char *)tx_buf); 
+    tx_len = strlen((const char *)tx_buf);
     Update_Values();
     wait(1.0);
     wakeup.set_and_wait(sleep_time);
-    while(true){    // never come here but just in case
+    while(true) {   // never come here but just in case
         deepsleep();
     }
 #else
@@ -442,15 +474,19 @@
     unsigned long val;
     unsigned char c, radix, s = 0;
 
-    for (;;){
+    for (;;) {
         c = **str;
-        if (c == 0){ return 0;}
-        if (c == '-'){ break;}
-        if (c == '+'){
+        if (c == 0) {
+            return 0;
+        }
+        if (c == '-') {
+            break;
+        }
+        if (c == '+') {
             (*str)++;
             c = **str;
         }
-        if (c>='0'&& c<='9'){
+        if (c>='0'&& c<='9') {
             break;
         } else {
             (*str)++;
@@ -516,13 +552,13 @@
 {
     DEBUG("Disconnected!\r\n");
     DEBUG("Restarting the advertising process\r\n");
-    ble.startAdvertising();
+    ble_uart.startAdvertising();
 }
 
 void Update_Values(void)
 {
     bletx_mutex.lock();
-    ble.updateCharacteristicValue(
+    ble_uart.updateCharacteristicValue(
         uartServicePtr->getRXCharacteristicHandle(),
         tx_buf,
         tx_len