Indoor positioning. Central unit.

Dependencies:   aconno_SEGGER_RTT

Files at this revision

API Documentation at this revision

Comitter:
dbartolovic
Date:
Wed Mar 07 11:43:39 2018 +0000
Parent:
1:2bdc506d8baa
Commit message:
Central module now sends messages as soon as it receives them.

Changed in this revision

aconno_ble/aconno_ble.cpp Show annotated file Show diff for this revision Revisions of this file
aconno_ble/aconno_ble.h Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
source/main.h Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.h Show annotated file Show diff for this revision Revisions of this file
diff -r 2bdc506d8baa -r 1b85a28b1e68 aconno_ble/aconno_ble.cpp
--- a/aconno_ble/aconno_ble.cpp	Tue Mar 06 15:45:34 2018 +0000
+++ b/aconno_ble/aconno_ble.cpp	Wed Mar 07 11:43:39 2018 +0000
@@ -4,10 +4,13 @@
  *
  */
 
+#include "main.h"
+#include "tasks.h"
 #include "aconno_ble.h"
 
 
 char MSD[MSD_SIZE_b];
+rssi_buff_t rssi_buff;
 
 /**
 * Callback triggered when the ble initialization process has finished
@@ -25,34 +28,29 @@
         return;
     }
     
+    // Setup rssi_buff
+    rssi_buff.cntr = 0;
+    
     /* setup advertising */
     //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE_b);
     ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS);
+    ble.gap().startAdvertising();
 }
 
 /* advertisement callback is called every time a new ble device is discovered */
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params){
-    static addr_rssi_pair_t pairs[PER_MOD_CNT];
-    static uint8_t cntr = 0;
     
     bool is_valid_device = false;
     
     uint8_t payloadSize;
     const uint8_t *payload;
     uint8_t dataSize=0, dataType=0, tempIndex=0;
-    static uint8_t tryToConnect = 0;
     
     payloadSize = params->advertisingDataLen;
     payload = params->advertisingData;
     dataSize = *(payload);
     dataType = *(payload+1);
-    /*
-    printf("%X %X %X\n", payload[0], payload[1], payload[2]);
-    printf("%d %d\n", payloadSize, dataSize);
-    */
-    uint8_t *tmp = (uint8_t*)(params->peerAddr);
-    //printf("%X %X %X %X %X %X\n", tmp[5], tmp[4], tmp[3], tmp[2], (params->peerAddr)[1], tmp[0]);
     
     do{
         if(dataType == DEVICE_NAME_ID){
@@ -67,17 +65,15 @@
                 }
                 */
             }
-            tryToConnect = 1;
             printf("\r\n");
             printf("RSSI: %d\r\n", params->rssi);
+            
+            dataType = 0;
         }
         else if (dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA){
-            /*for(int i=tempIndex; i<=tempIndex+dataSize; i++){
-                printf("%X ", (uint8_t)*(payload+tempIndex+i));
-            }*/
+            
             if (!memcmp((uint8_t*)(payload+tempIndex+2), (uint8_t*)CONTROL, CONTROL_SIZE))
                 is_valid_device = true;
-            //printf("Ikada?\n");
             
             dataType = 0;
         }
@@ -86,41 +82,30 @@
             dataSize = *(payload+tempIndex);
             dataType = *(payload+tempIndex+1);
         }
-    }while(tempIndex<payloadSize && !tryToConnect && !is_valid_device);
+    }while(tempIndex<payloadSize && !is_valid_device);
     
     
     if (is_valid_device){
         
         bool is_present = false;
-        //printf("Is valid %d\n", cntr);
+        uint8_t *tmp = (uint8_t*)(params->peerAddr);
         
         // Check if MAC is already pressent in pairs array. If it is don't add
         // it, just update rssi value.
-        for (int i = 0; i < cntr; i++)
-            if (!memcmp(pairs[i].addr, (params->peerAddr), ADDR_LEN)){
-                pairs[i].rssi = params->rssi;
+        for (int i = 0; i < rssi_buff.cntr; i++)
+            if (!memcmp(rssi_buff.pairs[i].addr, (params->peerAddr), ADDR_LEN)){
+                rssi_buff.pairs[i].rssi = params->rssi;
                 is_present = true;
             }
         
-        if (!is_present){
-            memcpy(pairs[cntr].addr, (params->peerAddr), ADDR_LEN);
-            pairs[cntr++].rssi = params->rssi;
+        if (!is_present && (rssi_buff.cntr < PER_MOD_CNT)){
+            memcpy(rssi_buff.pairs[rssi_buff.cntr].addr, (params->peerAddr), ADDR_LEN);
+            rssi_buff.pairs[rssi_buff.cntr++].rssi = params->rssi;
+            tBleUpdateData.signal_set(UPDATE_DATA_SIG);
             printf("%X %X %X %X %X %X\n", tmp[5], tmp[4], tmp[3], tmp[2], tmp[1], tmp[0]);
         }
     }
     
-    if (cntr == PER_MOD_CNT){
-        cntr = 0;
-        memcpy(MSD, pairs, sizeof(pairs));
-        
-        for (int i = 0; i < sizeof(pairs); i++){
-            printf("%X ", MSD[i]);
-            if ((i%7) == 6)
-                printf("\n");
-        }
-            
-    }
-    
     /*
     if(tryToConnect){
         SEND("Trying to establise connection...\r\n");
diff -r 2bdc506d8baa -r 1b85a28b1e68 aconno_ble/aconno_ble.h
--- a/aconno_ble/aconno_ble.h	Tue Mar 06 15:45:34 2018 +0000
+++ b/aconno_ble/aconno_ble.h	Wed Mar 07 11:43:39 2018 +0000
@@ -11,7 +11,8 @@
 #include "ble/BLE.h"
 #include "GapAdvertisingData.h"
 
-#define MSD_SIZE_b                  (7*4)
+#define UUID_SIZE                   (10)
+#define MSD_SIZE_b                  (10+6+1)
 #define CONTROL_SIZE                (10)
 #define ADV_INTERVAL_MS             (100)
 #define ADVERTISING_DURATION_S      (1)
@@ -34,8 +35,15 @@
     int8_t rssi;
 } addr_rssi_pair_t;
 
+typedef struct {
+    uint8_t cntr;
+    addr_rssi_pair_t pairs[PER_MOD_CNT];
+} rssi_buff_t;
+
+extern rssi_buff_t rssi_buff;
+
 /* Global variables and constants */
-const char DEVICE_NAME[] = "aconno beacon";
+const char uuid[UUID_SIZE] = {0x59, 0x00, 0x22, 0xF1, 0xDA, 0x7A, 0x42, 0xA4, 0xBB, 0xDF};
 extern char MSD[MSD_SIZE_b];
 const char CONTROL[CONTROL_SIZE] = {0x59, 0x00, 0x9C, 0x23, 0x9D, 0x82, 0x1B, 0xD4, 0x11, 0xE8};
 
diff -r 2bdc506d8baa -r 1b85a28b1e68 source/main.cpp
--- a/source/main.cpp	Tue Mar 06 15:45:34 2018 +0000
+++ b/source/main.cpp	Wed Mar 07 11:43:39 2018 +0000
@@ -5,6 +5,18 @@
  
 #include "main.h"
 
+
+#if DEBUG_LED
+    DigitalOut advLed(p22);
+    DigitalOut scanLed(p23);
+    DigitalOut periodicLed(p24);
+#endif
+
+Thread tBleUpdateData;
+Thread tBleStartAdvertising;
+Thread tBleStartScanning;
+Thread tPeriodicCallback;
+
 int main()
 {
     printf("Main program started.\r\n");
@@ -20,6 +32,7 @@
         periodicLed = 1;
     #endif
        
+    tBleUpdateData.start(callback(bleUpdateData, &ble));
     tBleStartAdvertising.start(callback(bleStartAdvertising, &ble));
     tBleStartScanning.start(callback(bleStartScanning, &ble));
     tPeriodicCallback.start(callback(periodicCallback, &ble));
diff -r 2bdc506d8baa -r 1b85a28b1e68 source/main.h
--- a/source/main.h	Tue Mar 06 15:45:34 2018 +0000
+++ b/source/main.h	Wed Mar 07 11:43:39 2018 +0000
@@ -22,14 +22,9 @@
     #define printf(...)
 #endif
 
-#if DEBUG_LED
-    DigitalOut advLed(p22);
-    DigitalOut scanLed(p23);
-    DigitalOut periodicLed(p24);
-#endif
-
-Thread tBleStartAdvertising;
-Thread tBleStartScanning;
-Thread tPeriodicCallback;
+extern Thread tBleUpdateData;
+extern Thread tBleStartAdvertising;
+extern Thread tBleStartScanning;
+extern Thread tPeriodicCallback;
 
 #endif // MAIN_H
\ No newline at end of file
diff -r 2bdc506d8baa -r 1b85a28b1e68 tasks/tasks.cpp
--- a/tasks/tasks.cpp	Tue Mar 06 15:45:34 2018 +0000
+++ b/tasks/tasks.cpp	Wed Mar 07 11:43:39 2018 +0000
@@ -15,6 +15,7 @@
 
 // BLE global data
 GapAdvertisingData adv_data = GapAdvertisingData();
+uint16_t adv_cntr = 0;
 
 
 void periodicCallback(BLE *ble)
@@ -29,23 +30,42 @@
     }       
 }
 
+void bleUpdateData(BLE *ble)
+{
+    while (true)
+    {
+        Thread::signal_wait(UPDATE_DATA_SIG);
+        Thread::signal_clr(UPDATE_DATA_SIG);
+        
+        while (rssi_buff.cntr != 0)
+        {
+            memcpy(MSD, uuid, sizeof(uuid));
+            memcpy(MSD + sizeof(uuid), &(rssi_buff.pairs[rssi_buff.cntr-1]), sizeof(rssi_buff.pairs[0]));
+            
+            rssi_buff.cntr--;
+            
+            adv_data = ble->getAdvertisingData();
+            adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t*)MSD, MSD_SIZE_b);
+            ble->setAdvertisingData(adv_data);
+            
+            wait_ms(ADV_INTERVAL_MS*2);
+        }
+    }
+}
+
 void bleStartAdvertising(BLE *ble)
 {
     while(true)
     {
-        adv_data = ble->getAdvertisingData();
-        adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t*)MSD, MSD_SIZE_b);
-        ble->setAdvertisingData(adv_data);
-        
-        ble->gap().startAdvertising();
-        printf("Advertisement started.\r\n");
+        //ble->gap().startAdvertising();
+        //printf("Advertisement started.\r\n");
         #if DEBUG_LED
             advLed = 0;
         #endif
         wait(ADVERTISING_DURATION_S);
         wait_ms(1000);
-        ble->gap().stopAdvertising();
-        printf("Advertisement stopped.\r\n");
+        //ble->gap().stopAdvertising();
+        //printf("Advertisement stopped.\r\n");
         #if DEBUG_LED
             advLed = 1;
         #endif
@@ -58,7 +78,7 @@
 {
     while(true)
     {
-        ble->gap().setScanParams(1500, 400);
+        ble->gap().setScanParams(300, 250);
         ble->gap().startScan(advertisementCallback);
         Thread::signal_wait(0x00023456);
         //Thread::signal_clr(ACC_INT_SIG);
diff -r 2bdc506d8baa -r 1b85a28b1e68 tasks/tasks.h
--- a/tasks/tasks.h	Tue Mar 06 15:45:34 2018 +0000
+++ b/tasks/tasks.h	Wed Mar 07 11:43:39 2018 +0000
@@ -17,6 +17,8 @@
 #define PERIODIC_CALLBACK_S     (1)
 #define PRINT_ON_RTT            (1)
 
+#define UPDATE_DATA_SIG             (0x00001020)
+
 #if PRINT_ON_RTT
     #include "SEGGER_RTT.h"
     #define printf(...)                      SEGGER_RTT_printf(0, __VA_ARGS__)
@@ -24,6 +26,7 @@
     #define printf(...)
 #endif
 
+void bleUpdateData(BLE *ble);
 void bleStartAdvertising(BLE *ble);
 void bleStartScanning(BLE *ble);
 void periodicCallback(BLE *ble);