From a86715f217f41a66d26439e46cb4d970d5053f35 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Fri, 2 Apr 2021 17:09:00 +0200
Subject: [PATCH] feat(ble): Don't send DM and ATT events until disc/cfg
 complete

---
 epicardium/ble/epic_ble_api.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/epicardium/ble/epic_ble_api.c b/epicardium/ble/epic_ble_api.c
index 13f6a3e1b..3b39a7c0a 100644
--- a/epicardium/ble/epic_ble_api.c
+++ b/epicardium/ble/epic_ble_api.c
@@ -26,6 +26,8 @@ static uint8_t adv_data_buf[HCI_ADV_DATA_LEN];
 static uint8_t sr_data_buf[HCI_ADV_DATA_LEN];
 static dmEvt_t connection_open_event;
 
+static bool connection_open;
+
 int epic_ble_free_event(struct epic_ble_event *e)
 {
 	if (e->data) {
@@ -46,6 +48,25 @@ void ble_epic_ble_api_trigger_event(enum epic_ble_event_type type, void *data)
 
 	struct epic_ble_event e = { .type = type, .data = data };
 
+	if (type == BLE_EVENT_DM_EVENT) {
+		dmEvt_t *dm_event = data;
+		if (dm_event->hdr.event == DM_CONN_OPEN_IND) {
+			connection_open = true;
+		}
+
+		if (dm_event->hdr.event == DM_CONN_CLOSE_IND) {
+			connection_open = false;
+		}
+	}
+
+	if (!connection_open &&
+	    (type == BLE_EVENT_ATT_EVENT || type == BLE_EVENT_DM_EVENT)) {
+		// Don't forward DM and ATT events until epicardium is done setting up
+		// the connection
+		epic_ble_free_event(&e);
+		return;
+	}
+
 	if (enabled) {
 		if (xQueueSend(ble_event_queue, &e, 0) != pdTRUE) {
 			/* Print a warning if the app is missing events. Missing scan results
-- 
GitLab