Ble Demo with Raspberry PI

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_DEMO_SPI_HARDCODE by HM_IOT_Demo

Revision:
18:0eefac249a4b
Parent:
17:2e0273d799e2
--- a/main.cpp	Thu Oct 12 09:07:57 2017 +0000
+++ b/main.cpp	Fri Oct 13 07:25:54 2017 +0000
@@ -6,7 +6,7 @@
 #include "hm_config.h"
 
 DiscoveredCharacteristic lightCharacteristic;
-Gap::Handle_t connectionHandle = 0xFFFF;
+//Gap::Handle_t connectionHandle = 0xFFFF;
 //static s_serviceInfo lightChar = {NULL,NULL};
 Serial pc(USBTX, USBRX);
 void bleint();
@@ -16,6 +16,9 @@
 SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10);
 lightCharacteristic_t ptr_temp_char;
 
+
+void read_Characteristics();
+
 //DiscoveredCharacteristic ptr_temp_char;
 
 bool connection_handle = 0;
@@ -29,9 +32,9 @@
      //0x51 for Magic light 
     //0x5A
     
-               pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\ndata: %s\r\n",
+        /*pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\ndata: %s\r\n",
            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
-           params->rssi, params->isScanResponse, params->type, params->advertisingData);
+           params->rssi, params->isScanResponse, params->type, params->advertisingData);*/
         //if (params->peerAddr[0] == 0x5A){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
         if (params->peerAddr[0] == 0xE5){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
        
@@ -55,30 +58,30 @@
  }
 
 void serviceDiscoveryCallback(const DiscoveredService *service) {
-    //pc.printf("Service Discovery Callback\r\n");
+    pc.printf("Service Discovery Callback\r\n");
     if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
-        //pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
+        pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
     } else {
         //pc.printf("S UUID-");
         const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
         for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
             printf("%02x", longUUIDBytes[i]);
         }
-        //pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
+        pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
     }
 }
 
 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
     pc.printf("  C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
     
-    if (characteristicP->getUUID().getShortUUID() == 0xec0e) { /* !ALERT! Alter this filter to suit your device. */
+    if (characteristicP->getUUID().getShortUUID() == 0xEC0E) { /* !ALERT! Alter this filter to suit your device. */
        lightCharacteristic = *characteristicP;
        pc.printf("Matched char UUID\r\n");
        printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle());
        printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle());
        AddCharNodes(lightCharacteristic);
        charDiscover = 1;
-       serviceDiscover = true;
+       serviceDiscover = false;
      }else {
         printf("Not Matched char UUID\r\n");
     }
@@ -87,7 +90,7 @@
 void AddCharNodes(DiscoveredCharacteristic light_Characteristic)
 {
     //printf("Add Char Nodes\r\n");
-   // lightCharacteristic_t ptr_temp_char;
+    //lightCharacteristic_t ptr_temp_char;
     /*
     ptr_temp_char = lightChar.services_Char[0];
 
@@ -104,7 +107,7 @@
                     ptr_temp_char = ptr_temp_char->nextChar;
                 }  
             
-        /* add a new node 
+        // add a new node 
         ptr_temp_char->nextChar = new lightCharacteristic_t;
         ptr_temp_char = ptr_temp_char->nextChar;
     }*/
@@ -121,15 +124,18 @@
 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
     pc.printf("terminated SD for handle %u\r\n", connectionHandle);
     serviceDiscover = false;
+    pc.printf("Service Discover handle %d\r\n", serviceDiscover);
 }
 
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
-    //pc.printf("Connection Callback\n\r");
+    pc.printf("Connection Callback\n\r");
     if (params->role == Gap::CENTRAL) {
-        connectionHandle = params->handle;
-        //pc.printf("Service and characterstics discovery callback\r\n");
+        //connectionHandle = params->handle;
+        pc.printf("Handle %u\r\n", params->handle);
+        pc.printf("Service and characterstics discovery callback\r\n");
         BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
-        BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9);
+        error_status = BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xEC00, 0xEC0E);
+        printf("Service Discovery Error Status =%d\n\r",error_status);
         // 0xffe5 --> Services UUID
         // 0xffe9 --> Characteristics UUID
     }
@@ -142,7 +148,10 @@
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
     pc.printf("disconnected\r\n");
-    BLE::Instance().gap().startScan(advertisementCallback);
+    //ble.gap().startScan(advertisementCallback);
+    connect_status = 0;
+    charDiscover = 0;
+    bleint();
 }
 
 
@@ -173,7 +182,7 @@
  */
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
-    BLE&        ble   = params->ble;
+    BLE&        ble = params->ble;
     
     pc.printf("Ble Init\n\r");
     /* Ensure that it is the default instance of BLE */
@@ -186,12 +195,43 @@
     ble.gap().setScanParams(1000, 800);
     ble.gap().startScan(advertisementCallback);
     //ble.gattClient().onHVX(hvxCallback);
-     ble.gattClient().onDataRead(triggerRead);
+    //ble.gattClient().onDataRead(triggerRead);
 
 }
 
+void write_Characteristics()
+{   uint8_t write_data  = 0x45;
+    
+    error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_REQ, ptr_temp_char.connHandle,ptr_temp_char.valueHandle, sizeof(write_data), &write_data);
+    
+    printf("Error Status =%d\n\r",error_status);
+    //int i;
+    printf("write VALUE : ");
+    //for(i=0; i<16;i++)
+    //{
+        printf("%d \r\n", write_data);
+    //}
+    BLE::Instance().processEvents();
+}
+
+void read_Characteristics()
+{   uint16_t write_data;
+    
+    error_status = BLE::Instance().gattClient().read(ptr_temp_char.connHandle,ptr_temp_char.valueHandle, write_data);
+    
+    printf("Error Status =%d\n\r",error_status);
+    //int i;
+    printf("READ VALUE : ");
+    //for(i=0; i<16;i++)
+    //{
+        printf("%x \r\n", write_data);
+    //}
+    BLE::Instance().processEvents();
+}
 void light_actions(int * service_char)
 {
+
+/*
     uint8_t lightColor[MAX_COLOR][3] = {{0xFF,0x00,0x00},{0x00,0xFF,0x00},
                                     {0x00,0x00,0xFF},{0xFF,0xFF,0x00},
                                     {0x00,0xFF,0xFF},{0xFF,0x00,0xFF}
@@ -261,6 +301,7 @@
         } 
             printf("Error Status =%d\n\r",error_status);
     }
+*/
 }
 
 void datancpy(char pre_write[], char writeData[], int data_size){
@@ -285,21 +326,35 @@
     wait(8.0);
     pc.printf("Start\n\r");
 
-    spiSlave.reply(191);
+    //spiSlave.reply(191);
     statusWR=1;
     bleint();
     bufferSize = 0;
-    int spiRX[5];
+    /*int spiRX[5];
     
     int temp = 0x00;
     int color = 0x00;
-    int con_status,i;
+    int con_status,i;*/
     while(1){
         pc.printf("loop\r\n");
-        wait(3.0);
+        //wait(1.0);
+        printf( "Connect Status %d  and Char Discover %d \r\n", connect_status, charDiscover);
         if(!(connect_status && charDiscover))
-           waitBleEvent();
+        {
+            printf( " Wait Event Connect Status %d  and Char Discover %d \r\n", connect_status, charDiscover);
+            waitBleEvent();
+        }
+        printf("Readchar\r\n");
+        if(connect_status && charDiscover)
+        {
+            pc.printf("Read\r\n");
             
+            write_Characteristics();
+            wait(2.0);
+            read_Characteristics();
+        }
+           
+        /* 
         if (temp == 2 && color == 3) {
             temp = 0x00;
             color = 0x00;
@@ -349,7 +404,7 @@
                  wait_ms(3000);
             }
             
-        //}     
+        //}  */   
         
     }
 
@@ -357,8 +412,13 @@
 
 void waitBleEvent()
 {
-    while(serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){
+    //|| BLE::Instance().gattClient().isServiceDiscoveryActive()
+    while(serviceDiscover ){
+                    printf("Service Discover =%d\r\n",serviceDiscover);
+                    printf(" Service Status = %d \r\n",BLE::Instance().gattClient().isServiceDiscoveryActive());
                     BLE::Instance().waitForEvent();
-    }               
+                    printf( "waitBleEvent loop\r\n");
+    }
+    printf( "waitBleEvent End\r\n"); 
 }