Test

Revision:
80:5e52c5847273
Parent:
79:9f3aca04de4e
Child:
81:dded8c042cca
--- a/source/main.cpp	Thu Jan 31 12:55:00 2019 +0000
+++ b/source/main.cpp	Fri Feb 01 14:08:20 2019 +0000
@@ -1,5 +1,4 @@
 #define END_OF_JSON          0xFE
-#define MAC_FILTER           0x8E
 #define VERBOSE_MODE            1  // Habilita la generacion de logs por el puerto USB      
 
 #include "mbed.h"
@@ -20,8 +19,9 @@
 char finalCharacterJSON    = END_OF_JSON;
 
 typedef struct {
-    string           MAC;       /* Dirección MAC */
-    float            ToF;       /* Tiempo de vuelo */
+    string           MAC1;       /* Dirección MAC1*/
+    string           MAC2;       /* Dirección MAC2*/
+    double            ToF;       /* Tiempo de vuelo*/
 } BLEdata_t;
 
 DigitalOut led3Test(LED3);
@@ -31,12 +31,13 @@
 TOFService* tofService;
 
 EventQueue eventQueue;
+//Queue<EventData_t, 32> Eventqueue; WIP
 Queue<BLEdata_t, 32> BLEqueue;
 MemoryPool<BLEdata_t, 32> BLEmpool;
 
-Thread threadLED(osPriorityAboveNormal1, 1000);
+Thread threadLED(osPriorityAboveNormal1, 400);
 Thread threadSerial(osPriorityAboveNormal2, 2500);
-Thread threadBLE(osPriorityRealtime3, 1000);
+Thread threadBLE(osPriorityRealtime3, 2500);
 
 template<typename arg>
 void printLog(const char * log, arg data) {
@@ -88,7 +89,7 @@
             BLEmpool.free(BLEdata);
                 
             int type = rand() % 4;
-            event["idEvent"] = picojson::value(BLEdata->MAC);
+            event["idEvent"] = picojson::value(BLEdata->MAC1);
             event["type"] = picojson::value((double) type);
             str = "E9:ED:F4:AC:BF:8E";
             extraInfo["hazardousDevice"] = picojson::value(str);
@@ -119,18 +120,42 @@
     printLog("Conectado al servidor\r\n");    
 }
 
+void parseRawJSONToQueue(string JSONsource) {
+    picojson::value v;
+    string err;
+    BLEdata_t* data = BLEmpool.alloc();
+    
+    picojson::parse(v, JSONsource.c_str(), JSONsource.c_str() + strlen(JSONsource.c_str()), &err);
+    printLog("res error? %s\r\n", err);
+    
+    data->ToF  = v.get("TOF").get<double>();
+    data->MAC1 = v.get("MAC1").get<string>();
+    data->MAC2 = v.get("MAC2").get<string>();
+    
+    printLog("ToF = %f", data->ToF);
+    printLog(" MAC1 = %s", data->MAC1);
+    printLog(" MAC2 = %s\r\n", data->MAC2);
+    
+    //EventQueue.put(data); WIP
+}
+
 void writeCharCallback(const GattWriteCallbackParams *params) {
     if(params->handle == tofService->getValueHandle()) {
-        char toChar [TOFService::TOF_CHAR_ARRAY_SIZE + 1];
+        //BLEdata_t* data = BLEmpool.alloc();
+        //BLEdata_t data;
+        char toChar [TOFService::TOF_CHAR_ARRAY_SIZE];
         printLog("Data received: length = %d, data = 0x", params->len);
         for(int x=0; x < params->len; x++) {
             toChar[x] = (char) params->data[x];
             printLog("%x", params->data[x]);
         }
-        toChar[params->len] = '\0';
+        //toChar[params->len] = '\0';
+        printLog("\n\r");
+        
+        printLog("Cadena: %s\n\r", toChar);
         string str(toChar);
-        printLog("\n\r");
-        printLog("Dato parseado a string: %s\n\r", str);
+        eventQueue.call(parseRawJSONToQueue, str);
+        //BLEqueue.put(data);
     }
 }
 
@@ -141,18 +166,6 @@
     printLog("Ha ocurrido un error al inicializar la configuracion del BLE\n");
 }
 
-void printMacAddress() {
-    /* Print out device MAC address to the console*/
-    Gap::AddressType_t addr_type;
-    Gap::Address_t address;
-    BLE::Instance().gap().getAddress(&addr_type, address);
-    printLog("DEVICE MAC ADDRESS: ");
-    for (int i = 5; i >= 1; i--){
-        printLog("%02x:", address[i]);
-    }
-    printLog("%02x\r\n", address[0]);
-}
-
 /**
  * Callback triggered when the ble initialization process has finished
  */
@@ -198,7 +211,7 @@
     srand(time(NULL));
     threadLED.start(callback(blinkLED3));
     threadBLE.start(callback(BLEServiceManagment));
-    //threadSerial.start(callback(sendJsonOverSerial));
+    //threadSerial.start(callback(sendJsonOverSerial)); WIP
             
     threadLED.join();
     threadBLE.join();