From 1bcf5229943af01ccefa7ddf2ff5554bcf19f67a Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Thu, 25 Jun 2020 02:13:50 +0200
Subject: [PATCH] hack(ble): Receive covid tracing beacons

---
 epicardium/ble/ble_main.c                     | 62 ++++++++++++++++++-
 epicardium/ble/stack.c                        | 15 ++---
 lib/sdk/Libraries/BTLE/meson.build            |  1 +
 .../sources/apps/app/app_master_leg.c         |  2 +-
 4 files changed, 68 insertions(+), 12 deletions(-)

diff --git a/epicardium/ble/ble_main.c b/epicardium/ble/ble_main.c
index 89af35a38..fc8336dac 100644
--- a/epicardium/ble/ble_main.c
+++ b/epicardium/ble/ble_main.c
@@ -188,6 +188,15 @@ static const uint8_t bleAdvDataConn[] =
   DM_FLAG_LE_BREDR_NOT_SUP,
 };
 
+static const appMasterCfg_t scannerMasterCfg =
+{
+  420,                                     /*! The scan interval, in 0.625 ms units */
+  420,                                     /*! The scan window, in 0.625 ms units  */
+  0,                                       /*! The scan duration in ms */
+  DM_DISC_MODE_NONE,                       /*! The GAP discovery mode */
+  DM_SCAN_TYPE_PASSIVE
+                                           /*!< The scan type (active or passive) */
+};
 
 /**************************************************************************************************
   Client Characteristic Configuration Descriptors
@@ -470,9 +479,13 @@ static void bleSetup(bleMsg_t *pMsg)
   AppAdvSetData(APP_ADV_DATA_CONNECTABLE, sizeof(bleAdvDataConn), (uint8_t *) bleAdvDataConn);
   AppAdvSetData(APP_SCAN_DATA_CONNECTABLE, 0, NULL);
 
-  active = true;
+  //active = true;
   /* TODO: Sadly, not advertising leads to a higher current consumption... */
   epic_ble_set_bondable(false);
+
+  //AppScanStart(scannerMasterCfg.discMode, scannerMasterCfg.scanType, scannerMasterCfg.scanDuration);
+  DmScanSetInterval(HCI_SCAN_PHY_LE_1M_BIT, &pAppMasterCfg->scanInterval, &pAppMasterCfg->scanWindow);
+  DmScanStart(HCI_SCAN_PHY_LE_1M_BIT, scannerMasterCfg.discMode, &scannerMasterCfg.scanType, FALSE, scannerMasterCfg.scanDuration, 0);
 }
 
 void epic_ble_set_bondable(bool bondable)
@@ -576,6 +589,46 @@ static void bleHandleNumericComparison(dmSecCnfIndEvt_t *pCnfInd)
 	trigger_event(1);
 }
 
+static void hexdump(uint8_t *data, int len)
+{
+    while(len--) printf("%02x ", *data++);
+}
+
+static void scannerScanReport(dmEvt_t *pMsg)
+{
+	uint8_t *pData;
+	static bool c;
+	pData = DmFindAdType(0x16,
+			pMsg->scanReport.len, pMsg->scanReport.pData);
+	if(pData) {
+		uint8_t len= pData[0];
+		uint8_t uuid_1 = pData[2];
+		uint8_t uuid_2 = pData[3];
+		if(uuid_1 == 0x6f && uuid_2 == 0xfd) {
+			printf("COVID : %02x:%02x:%02x:%02x:%02x:%02x",
+					pMsg->scanReport.addr[5],
+					pMsg->scanReport.addr[4],
+					pMsg->scanReport.addr[3],
+					pMsg->scanReport.addr[2],
+					pMsg->scanReport.addr[1],
+					pMsg->scanReport.addr[0]);
+
+			printf(" rssi %d, data ", pMsg->scanReport.rssi);
+			hexdump(&pData[4], len - 4);
+			printf("\n");
+			epic_vibra_vibrate(30);
+			if(c) {
+				epic_leds_set(14, 255, 0, 0);
+				c = false;
+			} else {
+				epic_leds_set(14, 0, 255, 0);
+				c = true;
+			}
+		}
+	}
+}
+
+
 /*************************************************************************************************/
 /*!
  *  \brief  Process messages from the event handler.
@@ -720,6 +773,10 @@ static void bleProcMsg(bleMsg_t *pMsg)
       bleHandleNumericComparison(&pMsg->dm.cnfInd);
       break;
 
+    case DM_SCAN_REPORT_IND:
+      scannerScanReport((dmEvt_t *)pMsg);
+      break;
+
     case DM_HW_ERROR_IND:
       LOG_ERR("ble", "HW Error");
       break;
@@ -750,6 +807,7 @@ static void BleHandlerInit(void)
   pAppSlaveCfg = (appSlaveCfg_t *) &bleSlaveCfg;
   pAppSecCfg = (appSecCfg_t *) &bleSecCfg;
   pAppUpdateCfg = (appUpdateCfg_t *) &bleUpdateCfg;
+  pAppMasterCfg = (appMasterCfg_t *) &scannerMasterCfg;
 
   /* Initialize application framework */
   AppSlaveInit();
@@ -780,7 +838,7 @@ static void BleHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg)
 
     if (pMsg->event >= DM_CBACK_START && pMsg->event <= DM_CBACK_END)
     {
-      LOG_INFO("ble", "Ble got evt %d: %s", pMsg->event, dm_events[pMsg->event - DM_CBACK_START]);
+      if(pMsg->event != DM_SCAN_REPORT_IND) LOG_INFO("ble", "Ble got evt %d: %s", pMsg->event, dm_events[pMsg->event - DM_CBACK_START]);
       /* process advertising and connection-related messages */
       AppSlaveProcDmMsg((dmEvt_t *) pMsg);
 
diff --git a/epicardium/ble/stack.c b/epicardium/ble/stack.c
index a7896b68f..3d24b0c59 100644
--- a/epicardium/ble/stack.c
+++ b/epicardium/ble/stack.c
@@ -130,11 +130,7 @@ void StackInit(void)
       .freeMemAvail = LL_MEMORY_FOOTPRINT
   };
 
-#ifdef DATS_APP_USE_LEGACY_API
   memUsed = LlInitControllerExtInit(&ll_init_cfg);
-#else /* DATS_APP_USE_LEGACY_API */
-  memUsed = LlInitControllerExtInit(&ll_init_cfg);
-#endif /* DATS_APP_USE_LEGACY_API */
   if(memUsed != LL_MEMORY_FOOTPRINT)
   {
       printf("Controller memory mismatch 0x%x != 0x%x\n", (unsigned int)memUsed,
@@ -142,6 +138,11 @@ void StackInit(void)
   }
 #endif
 
+  SecInit();
+  SecRandInit();
+  SecAesInit();
+  SecCmacInit();
+  SecEccInit();
 
   /* card10:
    * These calls register a queue for callbacks in the OS abstraction
@@ -153,14 +154,10 @@ void StackInit(void)
   handlerId = WsfOsSetNextHandler(HciHandler);
   HciHandlerInit(handlerId);
 
-  SecInit();
-  SecAesInit();
-  SecCmacInit();
-  SecEccInit();
-
   handlerId = WsfOsSetNextHandler(DmHandler);
   DmDevVsInit(0);
   DmAdvInit();
+  DmScanInit();
   DmConnInit();
   DmConnSlaveInit();
   DmSecInit();
diff --git a/lib/sdk/Libraries/BTLE/meson.build b/lib/sdk/Libraries/BTLE/meson.build
index 6dd10ee6a..0f301cf18 100644
--- a/lib/sdk/Libraries/BTLE/meson.build
+++ b/lib/sdk/Libraries/BTLE/meson.build
@@ -421,6 +421,7 @@ ble_compileargs = [
   '-DINIT_BROADCASTER',
   '-DINIT_PERIPHERAL',
   '-DINIT_ENCRYPTED',
+  '-DINIT_OBSERVER',
 ]
 
 if get_option('ble_trace')
diff --git a/lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/app/app_master_leg.c b/lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/app/app_master_leg.c
index 6a56651a7..91613609e 100644
--- a/lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/app/app_master_leg.c
+++ b/lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/app/app_master_leg.c
@@ -73,7 +73,7 @@ void AppScanStart(uint8_t mode, uint8_t scanType, uint16_t duration)
   {
     DmScanSetInterval(HCI_SCAN_PHY_LE_1M_BIT, &pAppMasterCfg->scanInterval, &pAppMasterCfg->scanWindow);
 
-    DmScanStart(HCI_SCAN_PHY_LE_1M_BIT, mode, &scanType, TRUE, duration, 0);
+    DmScanStart(HCI_SCAN_PHY_LE_1M_BIT, mode, &scanType, FALSE, duration, 0);
   }
 }
 
-- 
GitLab