Lab 6 of Measurement for Network course at University of Sannio

Dependencies:   mbed X_NUCLEO_IDB0XA1 BLE_API

Revision:
23:bcc213904a44
Parent:
22:94afa3edefb3
--- a/main.cpp	Thu Aug 09 14:07:14 2018 +0000
+++ b/main.cpp	Thu Apr 29 16:55:17 2021 +0000
@@ -16,34 +16,59 @@
 
 #include "mbed.h"
 #include "ble/BLE.h"
-#include "ble/services/HeartRateService.h"
+#include "HeartRateService.h"
+#include "BatteryService.h"
+#include "DeviceInformationService.h"
+
+Serial  pc1(USBTX, USBRX);
+#define DEBUG(...) { pc1.printf(__VA_ARGS__); }
 
-#if !defined(IDB0XA1_D13_PATCH)
-DigitalOut led1(LED1, 1);   // LED conflicts SPI_CLK in case of D13 patch
-#endif  
+// ??? Initialize Ticker and DigitalOut objects
+// 1 DigitalOut object for Led 1
+// 2 Ticker objects: for aliveness and updating measurements
+DigitalOut ???????????
+Ticker ????????;
+Ticker ????????;
 
-const static char     DEVICE_NAME[]        = "HRM1";
-static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE};
+// Initialize variables and tables
+// Define the device name (add your group number)
+const static char     ?????????[]        = "MY_BLE_HRM??";
+static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE,
+                                              GattService::UUID_BATTERY_SERVICE,
+                                              GattService::UUID_DEVICE_INFORMATION_SERVICE};
 
-static volatile bool  triggerSensorPolling = false;
+// ??? define flag for measurement updating
+static volatile bool  ?????????????????????????
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
+    DEBUG("Disconnected!\n\r");
+    DEBUG("Restarting the advertising process\n\r");
     (void)params;
-    BLE::Instance().gap().startAdvertising(); // restart advertising
+      // ??? Restart advertising
+      ??????????????????????????
+}
+
+// Connection Handler
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
+{
+    DEBUG("Connected\r\n");
 }
 
-void periodicCallback(void)
-{
-#if !defined(IDB0XA1_D13_PATCH)
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
-#endif
+// ??? Define update Characteristics Handler update_handler
+// It must set the flag for update the heart rate
+void ???????????(void)
+{    
+      ??????????????????????????
+}
 
-    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
-     * heavy-weight sensor polling from the main thread. */
-    triggerSensorPolling = true;
+// ??? Define the Aliveness Handler, it must toggle the led 1
+void aliveness_handler (void)
+{   
+      ????????????????????????
 }
 
+
 void onBleInitError(BLE &ble, ble_error_t error)
 {
     (void)ble;
@@ -55,6 +80,15 @@
 {
     BLE&        ble   = params->ble;
     ble_error_t error = params->error;
+    
+    // ??? Attach ticker objects to functions
+    // Two handlers:
+    // 1. aliveness_handler, with 500 ms timing, which toggles led1
+    // 2. update_handler, with 2 s timing, which sets a flag for the heart rate updating
+        ???????????????????????????
+        ???????????????????????????
+
+    DEBUG("Initialising \n\r");
 
     if (error != BLE_ERROR_NONE) {
         onBleInitError(ble, error);
@@ -66,37 +100,68 @@
     }
 
     ble.gap().onDisconnection(disconnectionCallback);
+    ble.gap().onConnection(connectionCallback);
+    
+        // Setup BLE device services
 
-    /* Setup primary service. */
-    uint8_t hrmCounter = 60; // init HRM to 60bps
-    HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+    /* ??? Setup primary service. */
+    // Define and init variable for heart rate simulation (it can be a uint8_t)
+    uint8_t ????????????????? = ???;
+    /* Declare and init (call constructor) HeartRateService object
+     * Constructor is:
+     * HeartRateService(BLE &_ble, uint8_t hrmCounter, uint8_t location) 
+     * for location you can use HeartRateService::LOCATION_FINGER */
+    HeartRateService ????????????????;
+    
+    /* ??? Setup battery level service. */
+    // Define and init variable for battery level simulation (it can be a uint8_t)
+    uint8_t ?????????? = ???;
+    /* Declare and init (call constructor) BatteryService object
+     * Constructor is:
+     * BatteryService(BLE &_ble, uint8_t level = 100) */
+    BatteryService   ????????????????;
+    /* Declare and init (call constructor) DeviceInformationService object
+     * Constructor is:
+     * DeviceInformationService(BLE        &_ble,
+                         const char *manufacturersName = NULL,
+                         const char *modelNumber       = NULL,
+                         const char *serialNumber      = NULL,
+                         const char *hardwareRevision  = NULL,
+                         const char *firmwareRevision  = NULL,
+                         const char *softwareRevision  = NULL) 
+     * You can use manufacturerName = "ST", modelNumber = "Nucleo", serialNumber = "SN1"*/
+    DeviceInformationService ???????????????;
 
     /* Setup advertising. */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
-    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    // ??? Add the complete list of services provided to the advertising payload (GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS)
+    ble.gap().??????????????????????????????????
+    // ??? Add the heart rate service to the advertising payload (GapAdvertisingData::GENERIC_HEART_RATE_SENSOR)
+    ble.gap().??????????????????????????????????
+    // ??? Add the device name to the advertising payload (GapAdvertisingData::COMPLETE_LOCAL_NAME)
+    ble.gap().??????????????????????????????????
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
-    ble.gap().startAdvertising();
+    // Set advertising interval to 1 s
+    ble.gap().??????????????????????????????????
+    // ??? Start advertising, call the appropriate methods of the GAP object
+    ble.gap().????????????????;
 
     // infinite loop
     while (true) {
-        // check for trigger from periodicCallback()
-        if (triggerSensorPolling && ble.getGapState().connected) {
-            triggerSensorPolling = false;
-
-            // Do blocking calls or whatever is necessary for sensor polling.
-            // In our case, we simply update the HRM measurement.
-            hrmCounter++;
+        // check for  from update_handler()
+        if (???????????????????? && ble.getGapState().connected) {
+            //??? Reset flag
+            ?????????????????????
+            
+            // ??? Update the heart rate counter
+            // increase by 1. If equal to 150, then reset to 80
+            ?????????????
+            ?????????????
+            ?????????????
+            ?????????????
 
-            //  60 <= HRM bps <= 100
-            if (hrmCounter == 100) {
-                hrmCounter = 60;
-            }
-
-            // update bps
-            hrService.updateHeartRate(hrmCounter);
+            // ??? Update value of heart rate service
+            ?????????????????????????
         } else {
             ble.waitForEvent(); // low power wait for event
         }
@@ -105,9 +170,6 @@
 
 int main(void)
 {
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1); // blink LED every second
-
     BLE::Instance().init(bleInitComplete);
 }