test

Dependencies:   BufferedSerial WatchdogTimer

Revision:
0:ad0d1afb4462
Child:
1:d2ebf1c3ae5a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jun 01 06:26:08 2018 +0000
@@ -0,0 +1,237 @@
+//  Include --------------------------------------------------------------------
+#include "mbed.h"
+#include "BLE.h"
+#include "DiscoveredCharacteristic.h"
+#include "DiscoveredService.h"
+#include "UARTService.h"
+
+//  Definition -----------------------------------------------------------------
+#define     NUM_ONCE            20
+#define     BFSIZE              (NUM_ONCE+4)
+
+#define SOFT_DEVICE_FATHER_HANDLE   3
+
+//  Object ---------------------------------------------------------------------
+BLE&        ble_uart = BLE::Instance();
+Serial      pc(P0_1, P0_3, 115200);     // for another board
+Ticker      main_timer;      // メインタイマー
+
+//  ROM / Constant data --------------------------------------------------------
+const Gap::Address_t    mac_board_0   = {0xFF, 0x42, 0xD5, 0x9C, 0x9C, 0xE1};
+
+//  RAM ------------------------------------------------------------------------
+Gap::Handle_t   connectionHandle        = 0xFFFF;
+DiscoveredCharacteristic uartTXCharacteristic;
+DiscoveredCharacteristic uartRXCharacteristic;
+bool            foundUartRXCharacteristic = false;
+bool            connected2server        = false;
+bool            connection_tx           = false;
+bool            connection_rx           = false;
+UARTService *   uartServicePtr          = NULL;
+Gap::Address_t  my_mac;
+int             my_board_index          = -1;
+bool            received_uart_dat       = false;
+int8_t          uart_buffer[BFSIZE];
+uint8_t         uart_bf_len;
+volatile bool   rx_isr_busy             = false;
+
+//  Function prototypes --------------------------------------------------------
+//      BLE
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *);
+void serviceDiscoveryCallback(const DiscoveredService *);
+void characteristicDiscoveryCallback(const DiscoveredCharacteristic *);
+void discoveryTerminationCallback(Gap::Handle_t );
+void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *);
+void connectionCallback(const Gap::ConnectionCallbackParams_t *);
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *);
+//      Pre-check
+bool mac_equals(const Gap::Address_t, const Gap::Address_t);
+int  get_board_index(const Gap::Address_t);
+
+//------------------------------------------------------------------------------
+//  Control Program
+//------------------------------------------------------------------------------
+int main(void)
+{
+    // opening message
+    pc.printf("UART Communication / Client(Central) side\r\n");
+    pc.printf("  need Server module (run BLE_Uart_Server program)\r\n");
+    // Mixed role **************************************************************
+    ble_uart.init();
+    ble_uart.gap().onConnection(connectionCallback);
+    ble_uart.gap().onDisconnection(disconnectionCallback);
+    // Client(Central) role ****************************************************
+    ble_uart.gattClient().onHVX(onReceivedDataFromDeviceCallback);
+    ble_uart.gap().setScanParams(500, 450);
+    ble_uart.gap().startScan(advertisementCallback);
+    while(true) {
+        // allow notifications from Server(Peripheral)
+        if (foundUartRXCharacteristic &&
+                !ble_uart.gattClient().isServiceDiscoveryActive()) {
+            // need to do the following only once
+            foundUartRXCharacteristic = false;
+            uint16_t value = BLE_HVX_NOTIFICATION;
+            ble_uart.gattClient().write(
+                GattClient::GATT_OP_WRITE_REQ,
+                connectionHandle,
+                uartRXCharacteristic.getValueHandle() + 1,
+                sizeof(uint16_t),
+                reinterpret_cast<const uint8_t *>(&value)
+            );
+            
+            if (received_uart_dat == true) {
+            received_uart_dat = false;
+            for(int i = 0; i < uart_bf_len; i++) {
+                //pc.printf("%c", uart_buffer[i]);
+                pc.putc(uart_buffer[i]);
+            }
+        }
+        }
+
+        ble_uart.waitForEvent();
+    }
+}
+
+void onReceivedDataFromDeviceCallback(const GattHVXCallbackParams *params)
+{
+    if (params->type == BLE_HVX_NOTIFICATION) {
+        if ((params->handle
+                == uartRXCharacteristic.getValueHandle()) && (params->len > 0)) {
+            uart_bf_len = params->len;
+            strcpy((char *)uart_buffer, (char *)params->data);
+            received_uart_dat = true;
+        }
+    }
+}
+
+bool mac_equals(const Gap::Address_t mac_1, const Gap::Address_t mac_2)
+{
+    for (int i = 0; i < 6; i++) {
+
+    }
+
+    for (int i = 0; i < 6; i++) {
+        if (mac_1[i] != mac_2[i]) {
+            return false;
+        } else {
+        }
+    }
+    return true;
+}
+
+int get_board_index(const Gap::Address_t mac)
+{
+    if (mac_equals(mac, mac_board_0)) {
+        return 0;
+    }
+#if 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;
+    }
+#endif
+    return -1;
+}
+
+// Client(Central) role ********************************************************
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
+{
+    // connections
+    int peer_board_index = get_board_index(params->peerAddr);
+    if (peer_board_index != -1) {
+        pc.printf("");
+        pc.printf(
+            "adv peerAddr [%02x %02x %02x %02x %02x %02x]\r\n",
+            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3],
+            params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]
+        );
+        pc.printf(
+            "rssi=%+4d, isScanResponse %u, AdvertisementType %u\r\n",
+            params->rssi, params->isScanResponse, params->type
+        );
+        ble_uart.gap().connect(
+            params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+    }
+}
+
+void serviceDiscoveryCallback(const DiscoveredService *service)
+{
+    if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
+        
+    } else {
+        const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
+        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
+            
+        }
+    }
+}
+
+void characteristicDiscoveryCallback(
+    const DiscoveredCharacteristic *characteristicP)
+{
+    
+    if (characteristicP->getUUID().getShortUUID()
+            == UARTServiceTXCharacteristicShortUUID) {
+        uartTXCharacteristic = *characteristicP;
+        connection_tx = true;
+    } else if (characteristicP->getUUID().getShortUUID()
+               == UARTServiceRXCharacteristicShortUUID) {
+        uartRXCharacteristic = *characteristicP;
+        foundUartRXCharacteristic = true;
+        connection_rx = true;
+    }
+}
+
+void discoveryTerminationCallback(Gap::Handle_t connectionHandle)
+{
+}
+
+// Mixed role ******************************************************************
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
+{
+    if (params->role == Gap::CENTRAL) {
+        pc.printf("connected as Client(Central) (handle = %d)\r\n\r",
+                  params->handle);
+        connected2server = true;
+        connectionHandle = params->handle;
+        ble_uart.gattClient().onServiceDiscoveryTermination(
+            discoveryTerminationCallback);
+        ble_uart.gattClient().launchServiceDiscovery(
+            params->handle,
+            serviceDiscoveryCallback,
+            characteristicDiscoveryCallback
+        );
+    }
+    pc.printf(
+        "Client(Central/Myself)       %02x:%02x:%02x:%02x:%02x:%02x\r\n",
+        params->ownAddr[5], params->ownAddr[4], params->ownAddr[3],
+        params->ownAddr[2], params->ownAddr[1], params->ownAddr[0]
+    );
+    pc.printf(
+        "Connected Server(Peripheral) %02x:%02x:%02x:%02x:%02x:%02x\r\n",
+        params->peerAddr[5], params->peerAddr[4], params->peerAddr[3],
+        params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]
+    );
+}
+
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    pc.printf(" -> disconnected\r\n", params->handle);
+    connected2server = false;
+//    connection_1st = false;
+    connection_tx = false;
+    connection_rx = false;
+    if (params->handle == SOFT_DEVICE_FATHER_HANDLE) {
+        ble_uart.startAdvertising();                    // restart advertising
+    } else {
+        ble_uart.gap().startScan(advertisementCallback);// restart scan
+    }
+}