Test

Revision:
73:a91805f9e9f0
Parent:
43:fb2855f7754b
Child:
74:12b9444a2fb4
--- a/source/main.cpp	Fri Dec 14 13:15:37 2018 +0000
+++ b/source/main.cpp	Tue Jan 22 08:36:23 2019 +0000
@@ -1,71 +1,217 @@
-/* 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.
- */
+#define END_OF_JSON                 0xFE
 
-#include <events/mbed_events.h>
-#include <mbed.h>
+#include "mbed.h"
+#include "picojson.h"
+#include "stats_report.h"
+
+/* Librerías para BLE - Servicio GATT */
 #include "ble/BLE.h"
 #include "ble/Gap.h"
-#include "ble/services/HeartRateService.h"
+#include "ble/DiscoveredCharacteristic.h"
+#include "ble/DiscoveredService.h"
+
+DiscoveredCharacteristic         testServiceptr;
+
+bool serviceDiscovered     = false;
+
+DigitalOut led3Test(LED3);
+DigitalOut led4BLE(LED4);
+Serial pcSerial(USBTX, USBRX); // Abrimos conexión serial con el puerto USB
+
+EventQueue eventQueue;
 
-DigitalOut led1(LED1, 1);
+Thread threadLED(osPriorityAboveNormal1, 400);
+//Thread threadSerial(osPriorityAboveNormal2, 2000);
+Thread threadBLE(osPriorityRealtime3, 2000);
+   
+int getNum(char ch)
+{
+    int num=0;
+    if(ch>='0' && ch<='9')
+    {
+        num=ch-0x30;
+    }
+    else
+    {
+        switch(ch)
+        {
+            case 'A': case 'a': num=10; break;
+            case 'B': case 'b': num=11; break;
+            case 'C': case 'c': num=12; break;
+            case 'D': case 'd': num=13; break;
+            case 'E': case 'e': num=14; break;
+            case 'F': case 'f': num=15; break;
+            default: num=0;
+        }
+    }
+    return num;
+}
+ 
+//function : hex2int
+//this function will return integer value against
+//hexValue - which is in string format
+ 
+unsigned int hex2int(unsigned char hex[])
+{
+    unsigned int x=0;
+    x=(getNum(hex[0]))*16+(getNum(hex[1]));
+    return x;
+    
+}
+
+/**
+ * Tarea encargada de parpadear un LED continuamente
+ */
+void blinkLED3() {
+    while(true) {
+        led3Test = !led3Test;
+        wait(0.2);
+    }
+}
+
+/**
+ * Método encargado de enviar un string por el puerto serie char a char
+ */
+void sendCharArrayToSerial(char const *array, Serial *serial) {
+    int i = 0;
+    while(array[i] != '\0') {
+        serial->putc(array[i]);
+        i++;
+    }
+    serial->putc('\0');
+}
 
-const static char     DEVICE_NAME[] = "HRM";
-static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
+/**
+ * Tarea encragada de enviar JSONs por el puerto serie
+ */
+void sendJsonOverSerial() {
+  /* Contruimos el objeto JSON */
+  picojson::object worker;
+  picojson::object vehicle;
+  picojson::object device;
+  picojson::object event;
+  picojson::object data;
+  
+  string tmpValue = "1234";
+  char tmp[1024];
+  string str;
+  char final = END_OF_JSON; // Caracter que indica el final del JSON
+  
+  while(true) {
+    worker["idDevice"] = picojson::value(tmpValue);
+    tmpValue = "12-12-2019";
+    worker["tsLastSeen"] = picojson::value(tmpValue);
+    tmpValue = "Baby Driver";
+    worker["role"] = picojson::value(tmpValue);
+    tmpValue = "12345";
+    worker["idVehicle"] = picojson::value(tmpValue);
+    
+    tmpValue = "123456";
+    vehicle["idDevice"] = picojson::value(tmpValue);
+    tmpValue = "18-12-2019";
+    vehicle["tsLastSeen"] = picojson::value(tmpValue);
+    
+    tmpValue = "11111";
+    device["idDevice"] = picojson::value(tmpValue);
+    device["battLevel"] = picojson::value(45.0);
+    tmpValue = "Pulsera";
+    device["deviceType"] = picojson::value(tmpValue);
+    
+    tmpValue = "256745";
+    event["idEvent"] = picojson::value(tmpValue);
+    event["hazardousDevice"] = picojson::value(false);
+    event["affectedDevice"] = picojson::value(true);
+    tmpValue = "Critical";
+    event["eventType"] = picojson::value(tmpValue);
+    tmpValue = "19-12-2018";
+    event["tsEvent"] = picojson::value(tmpValue);
+    
+    data["worker"] = picojson::value(worker);
+    data["vehicle"] = picojson::value(vehicle);
+    data["device"] = picojson::value(device);
+    data["event"] = picojson::value(event);
+    
+    str = picojson::value(data).serialize();
+  
+    // Convertimos el string a char *
+    strncpy(tmp, str.c_str(), sizeof(tmp));
+    strncat(tmp, &final, sizeof(final)); // Añadimos el caracter al final
+    tmp[sizeof(tmp) - 1] = 0;
 
-static uint8_t hrmCounter = 100; // init HRM to 100bps
-static HeartRateService *hrServicePtr;
+    //sendCharArrayToSerial(tmp, &pcSerial);
+    
+    wait(0.5);
+  }
+}
+
+void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
+    if (params->peerAddr[0] != 0x8E) return;
+    printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\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);
+
+    BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+}
 
-static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
+void serviceDiscoveryCallback(const DiscoveredService *service) {
+    if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
+        printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
+    } else {
+        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]);
+        }
+        printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
+    }
+}
+
+void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
+    printf("  C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
+    if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */
+        testServiceptr             = *characteristicP;
+        serviceDiscovered          =  true;
+    }
+}
+
+void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
+    printf("terminated SD for handle %u\r\n", connectionHandle);
+}
+
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
+    if (params->role == Gap::CENTRAL) {
+        BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
+        BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xA000, 0xA001);
+    }
+}
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    BLE::Instance().gap().startAdvertising(); // restart advertising
+    /* Si se desconecta el dispositivo, volvemos a entrar en estado Advertising*/
+    (void) params;
+    printf("Desconectado. Se comienza la fase de escaneo de nuevo\n\r");
+    serviceDiscovered = false;
+    BLE::Instance().gap().startScan(advertisementCallback);
 }
 
-void updateSensorValue() {
-    // Do blocking calls or whatever is necessary for sensor polling.
-    // In our case, we simply update the HRM measurement.
-    hrmCounter++;
-
-    //  100 <= HRM bps <=175
-    if (hrmCounter == 175) {
-        hrmCounter = 100;
-    }
-
-    hrServicePtr->updateHeartRate(hrmCounter);
-}
-
-void periodicCallback(void)
-{
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
-
-    if (BLE::Instance().getGapState().connected) {
-        eventQueue.call(updateSensorValue);
+void onDataReadClientCallback(const GattReadCallbackParams *response) {
+   if (response->handle == testServiceptr.getValueHandle()) {
+        printf("onDataReadClientCallback: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
+        for (unsigned index = 0; index < response->len; index++) {
+            printf("[%02x]", response->data[index]);
+        }
+        printf("\r\n");
     }
 }
 
-void onBleInitError(BLE &ble, ble_error_t error)
-{
-    (void)ble;
-    (void)error;
-   /* Initialization error handling should go here */
+/**
+ * Esta función se llama si ha habido algún error en el proceso de inicialización del BLE
+ */
+void onBleInitError(BLE &ble, ble_error_t error) {
+    printf("Ha ocurrido un error al inicializar la configuracion del BLE\n");
 }
 
-void printMacAddress()
-{
+void printMacAddress() {
     /* Print out device MAC address to the console*/
     Gap::AddressType_t addr_type;
     Gap::Address_t address;
@@ -77,51 +223,66 @@
     printf("%02x\r\n", address[0]);
 }
 
+/**
+ * Callback triggered when the ble initialization process has finished
+ */
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
 {
     BLE&        ble   = params->ble;
     ble_error_t error = params->error;
 
     if (error != BLE_ERROR_NONE) {
+        /* In case of error, forward the error handling to onBleInitError */
         onBleInitError(ble, error);
         return;
     }
 
-    if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
+    /* Ensure that it is the default instance of BLE */
+    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
         return;
     }
-
+  
+    ble.gap().onConnection(connectionCallback);
     ble.gap().onDisconnection(disconnectionCallback);
 
-    /* Setup primary service. */
-    hrServicePtr = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+    ble.gattClient().onDataRead(onDataReadClientCallback);
 
-    /* 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.gap().startAdvertising();
-
-    printMacAddress();
+    ble.gap().setScanParams(500, 400);
+    ble.gap().startScan(advertisementCallback);
+    
+    //printMacAddress();
 }
 
 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
     BLE &ble = BLE::Instance();
     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
+} 
+
+void readVoltageValue() {
+    BLE &ble = BLE::Instance();
+    if (serviceDiscovered && !ble.gattClient().isServiceDiscoveryActive()) {
+        testServiceptr.read();
+    }     
 }
 
-int main()
-{
-    eventQueue.call_every(500, periodicCallback);
-
+void BLEServiceManagment() {
+    eventQueue.call_every(2000, readVoltageValue);
+        
     BLE &ble = BLE::Instance();
-    ble.onEventsToProcess(scheduleBleEventsProcessing);
+    ble.onEventsToProcess(scheduleBleEventsProcessing); 
     ble.init(bleInitComplete);
-
+    
     eventQueue.dispatch_forever();
+}
 
+int main() { 
+    threadLED.start(blinkLED3);
+    threadBLE.start(BLEServiceManagment);
+    //threadSerial.start(sendJsonOverSerial);
+    
+    threadLED.join();
+    threadBLE.join();
+    //threadSerial.join();
+    
     return 0;
-}
+}
\ No newline at end of file