L0_BlueNRG_Test_Client

Dependencies:   BLE_API BLE_BlueNRG mbed

Fork of L0_BlueNRG_Test by Mridupawan Das

Revision:
2:cf54be3a9e6a
Parent:
1:3b1c20952274
Child:
4:17e1861df93f
diff -r 3b1c20952274 -r cf54be3a9e6a main.cpp
--- a/main.cpp	Fri Jul 25 07:08:14 2014 +0000
+++ b/main.cpp	Thu Aug 28 04:47:58 2014 +0000
@@ -2,22 +2,14 @@
 
 DigitalOut myled(LED1);
 
+#include "debug.h"
 #include "btle.h"
 //#include "BlueNRGDevice.h"//User does not use any platform specific header file
 #include "BLEDevice.h"
 #include "UUID.h"
+#include "Utils.h"
 
 BLEDevice dev;
-
-#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
-                               * it will have an impact on code-size and power consumption. */
- 
-#if NEED_CONSOLE_OUTPUT
-Serial  pc(USBTX, USBRX);
-#define DEBUG(...) { pc.printf(__VA_ARGS__); }
-#else
-#define DEBUG(...) /* nothing */
-#endif /* #if NEED_CONSOLE_OUTPUT */
  
 const static char  DEVICE_NAME[] = "STBLUE_NRG";
 const uint8_t device_address[6] = { 0x12, 0x34, 0x00, 0xe1, 0x80, 0x02 }; //Peripheral address
@@ -35,10 +27,56 @@
 
 uint8_t UUID_Count=0;
 
+static uint8_t hrmCounter = 100;
+static uint8_t bpm[2] = {0x00, hrmCounter};
+static uint8_t hcpCounter = 99;
+static uint8_t hcp[2] = {0x00, hrmCounter};
 
-void onConnectionCallback(void) {
-    myled = 1; // LED is ON
-    DEBUG("Connected BlueNRG!!");
+GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm), 
+                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
+                            /*GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE*/);
+
+GattCharacteristic hcpControlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, hcp, sizeof(hcp), sizeof(hcp), 
+                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
+                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);                            
+
+GattCharacteristic *hrmChars[] = {&hrmRate, &hcpControlPoint };
+UUID HRMSERVICE(GattService::UUID_HEART_RATE_SERVICE);
+GattService hrmService(HRMSERVICE/*GattService::UUID_HEART_RATE_SERVICE*/, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
+
+uint8_t uuid128_list[16*MAX_SERVICES_NOS];// = {HRM_SERVICE_UUID_128[0], HRM_SERVICE_UUID_128[1]};
+static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
+    
+void disconnectionCallback(uint16_t Handle_t)
+{
+    DEBUG("Disconnected!\n\r");
+    DEBUG("Restarting the advertising process\n\r");
+    dev.startAdvertising();
+}
+
+void onWriteCallback(uint16_t attributeHandle) {
+    DEBUG("Write Callback!!\n\r");
+    }
+    
+void onConnectionCallback(uint16_t Handle_t) {
+    //myled = 1; // LED is ON
+    DEBUG("Connected BlueNRG!!\n\r");
+    }
+
+
+void onNotifyEnabled(uint16_t charHandle) {
+    //myled = 1; // LED is ON
+    DEBUG("onNotifyEnabled!!\n\r");
+    }
+    
+void onNotifyDisabled(uint16_t charHandle) {
+    //myled = 1; // LED is ON
+    DEBUG("onNotifyDisabled!!\n\r");
+    }
+
+void onDataSentNotify() {
+    //myled = 1; // LED is ON
+    DEBUG("on Data Sent Notify!!\n\r");
     }
 
 void Append128bitUUID(uint8_t *array, const LongUUID_t SERVICE_UUID_128) 
@@ -51,10 +89,41 @@
     return; 
 }
 
+/**
+ * Triggered periodically by the 'ticker' interrupt; updates hrmCounter.
+ */
+void periodicCallback(void)
+{
+    //myled = !myled; /* Do blinky on LED1 while we're waiting for BLE events */
+     
+    if (dev.getGapState().connected) {
+        hrmCounter++;
+        if (hrmCounter == 175) {
+            //hrmCounter = 100;
+            dev.disconnect();
+        }
+        bpm[1] = hrmCounter;
+        uint16_t t = sizeof(bpm);
+        //DEBUG("Char Handle 0x%x OK.",hrmRate.getHandle());
+        //dev.readCharacteristicValue(hrmRate.getHandle(), bpm, (uint16_t *const)t);
+        dev.updateCharacteristicValue(hrmRate.getHandle(), bpm, sizeof(bpm));
+        //DEBUG("Ticker CB..\n\r");
+    } //else DEBUG("Not Connected..\n\r");
+    else //if not connected
+    {
+        hrmCounter++;
+        if (hrmCounter == 150) {
+            //hrmCounter = 100;
+            //dev.stopAdvertising();//can also test stop Advertising from here?
+        }
+        }
+}
+
 int main() {   
     
     Ticker ticker; //For Tick interrupt if used later on (periodic data updates?)
     
+    
     //LongUUID_t HEART_RATE_CHAR_UUID_128, HRM_SERVICE_UUID_128;
     //COPY_HRM_SERVICE_UUID(HRM_SERVICE_UUID_128);
     //COPY_HRM_CHAR_UUID(HEART_RATE_CHAR_UUID_128); 
@@ -63,24 +132,19 @@
     
     myled = 0;//Switch OFF LED1
      
-    DEBUG("Initializing BlueNRG...");
+    DEBUG("Initializing BlueNRG...\n\r");
     dev.init();
     
     dev.onConnection(onConnectionCallback);
-    //TODO.
-    //dev.setAddress(Gap::ADDR_TYPE_PUBLIC, device_address);//Does not work after gap/gatt init.
-    //TODO.
+    dev.onDisconnection(disconnectionCallback);
+    dev.onDataWritten(onWriteCallback);
+    dev.onUpdatesEnabled(onNotifyEnabled);
+    dev.onDataSent(onDataSentNotify);
+    dev.onUpdatesDisabled(onNotifyDisabled);
     
-    static uint8_t hrmCounter = 100;
-    static uint8_t bpm[2] = {0x00, hrmCounter};
-    GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm), 
-                                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
-
-    GattCharacteristic *hrmChars[] = {&hrmRate };
-    GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
-
-    uint8_t uuid128_list[16*MAX_SERVICES_NOS];// = {HRM_SERVICE_UUID_128[0], HRM_SERVICE_UUID_128[1]};
-    static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
+    //TODO.
+    //dev.setAddress(Gap::ADDR_TYPE_PUBLIC, device_address);//Will reset the device and re-init()
+    //TODO.    
     
     //Append128bitUUID(uuid128_list, HRM_SERVICE_UUID_128);
     
@@ -95,17 +159,21 @@
     dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
     dev.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     dev.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
-    DEBUG("Starting Advertising...");
+    DEBUG("Starting Advertising...\n\r");
     dev.startAdvertising();
     
     dev.addService(hrmService);
+    //ticker.attach(periodicCallback, 1); Multi threading and called from ISR context does not work!
     
     while(1) {
-        //myled = 1; // LED is ON
-        //wait(0.5); // 500 ms
-        //myled = 0; // LED is OFF
-        //wait(0.5); // 500 ms
-        //DEBUG("Testing BlueNRG!!");
+        
+        myled = 1; // LED is ON
+        wait(0.5); // 500 ms
+        myled = 0; // LED is OFF
+        wait(0.5); // 500 ms
+        //DEBUG("tic!\n\r");
+        periodicCallback();//Works from here!!
+    
         dev.waitForEvent();
     }
 }