Esempio Gatt Base

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Revision:
22:f9fd5024f7ea
Parent:
21:0e7c08f5386f
--- a/main.cpp	Wed Oct 05 09:16:58 2016 +0000
+++ b/main.cpp	Wed Jan 10 20:19:29 2018 +0000
@@ -1,108 +1,104 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
 #include "mbed.h"
 #include "ble/BLE.h"
-#include "ble/services/HeartRateService.h"
-
-DigitalOut led1(LED1, 1);
-
-const static char     DEVICE_NAME[]        = "HRM1";
-static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE};
-
-static volatile bool  triggerSensorPolling = false;
-
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+ 
+DigitalOut led(LED1, 1);
+uint16_t customServiceUUID  = 0xA000;
+uint16_t readCharUUID       = 0xA001;
+uint16_t writeCharUUID      = 0xA002;
+ 
+const static char     DEVICE_NAME[]        = "Pippo"; // change this
+static const uint16_t uuid16_list[]        = {0xFFFF}; //Custom UUID, FFFF is reserved for development
+ 
+/* Set Up custom Characteristics */
+static uint8_t readValue[10] = {0};
+ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
+ 
+static uint8_t writeValue[10] = {0};
+WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
+ 
+/* Set up custom service */
+GattCharacteristic *characteristics[] = {&readChar, &writeChar};
+GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
+ 
+ 
+/*
+ *  Restart advertising when phone app disconnects
+*/
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
 {
-    (void)params;
-    BLE::Instance().gap().startAdvertising(); // restart advertising
+    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
 }
-
-void periodicCallback(void)
+ 
+/*
+ *  Handle writes to writeCharacteristic
+*/
+void writeCharCallback(const GattWriteCallbackParams *params)
 {
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
-    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
-     * heavy-weight sensor polling from the main thread. */
-    triggerSensorPolling = true;
+    /* Check to see what characteristic was written, by handle */
+    if(params->handle == writeChar.getValueHandle()) {
+        /* toggle LED if only 1 byte is written */
+        if(params->len == 1) {
+            led = params->data[0];
+            (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle
+        }
+        /* Print the data if more than 1 byte is written */
+        else {
+            printf("Data received: length = %d, data = 0x",params->len);
+            for(int x=0; x < params->len; x++) {
+                printf("%x", params->data[x]);
+            }
+            printf("\n\r");
+        }
+        /* Update the readChar with the value of writeChar */
+        BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
+    }
 }
-
-void onBleInitError(BLE &ble, ble_error_t error)
-{
-    (void)ble;
-    (void)error;
-   /* Initialization error handling should go here */
-}
-
+/*
+ * Initialization callback
+ */
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
-    BLE&        ble   = params->ble;
+    BLE &ble          = params->ble;
     ble_error_t error = params->error;
-
+    
     if (error != BLE_ERROR_NONE) {
-        onBleInitError(ble, error);
-        return;
-    }
-
-    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
         return;
     }
-
+ 
     ble.gap().onDisconnection(disconnectionCallback);
-
-    /* Setup primary service. */
-    uint8_t hrmCounter = 60; // init HRM to 60bps
-    HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
-
-    /* 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));
-    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
+    ble.gattServer().onDataWritten(writeCharCallback);
+ 
+    /* Setup advertising */
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
+    ble.gap().setAdvertisingInterval(100); // 100ms.
+ 
+    /* Add our custom service */
+    ble.addService(customService);
+ 
+    /* Start advertising */
     ble.gap().startAdvertising();
-
-    // 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++;
-
-            //  60 <= HRM bps <= 100
-            if (hrmCounter == 100) {
-                hrmCounter = 60;
-            }
-
-            // update bps
-            hrService.updateHeartRate(hrmCounter);
-        } else {
-            ble.waitForEvent(); // low power wait for event
-        }
-    }
 }
-
+ 
+/*
+ *  Main loop
+*/
 int main(void)
 {
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1); // blink LED every second
-
-    BLE::Instance().init(bleInitComplete);
-}
-
+    /* initialize stuff */
+    printf("\n\r********* Starting Main Loop *********\n\r");
+    
+    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+    ble.init(bleInitComplete);
+    
+    /* SpinWait for initialization to complete. This is necessary because the
+     * BLE object is used in the main loop below. */
+    while (ble.hasInitialized()  == false) { /* spin loop */ }
+ 
+    /* Infinite loop waiting for BLE interrupt events */
+    while (true) {
+        ble.waitForEvent(); /* Save power */
+    }
+}
\ No newline at end of file