From ee5e69a4b13fedf4a1b6385a1e56efdd4ec40191 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Sun, 17 Jan 2021 22:56:22 +0100
Subject: [PATCH] change(ble): More reliability for ble uart

---
 epicardium/ble/ble_api.h  | 13 +++++++++++
 epicardium/ble/ble_main.c | 15 ++-----------
 epicardium/ble/uart.c     | 45 +++++++++++++++++++++++----------------
 epicardium/ble/uart.h     |  3 +++
 4 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/epicardium/ble/ble_api.h b/epicardium/ble/ble_api.h
index baad5eaf1..971ed38e1 100644
--- a/epicardium/ble/ble_api.h
+++ b/epicardium/ble/ble_api.h
@@ -29,3 +29,16 @@ void bleValueUpdate(attEvt_t *pMsg);
 void bleDiscCback(dmConnId_t connId, uint8_t status);
 void ble_trigger_event(enum ble_event_type event);
 void ble_epic_att_api_init(void);
+
+/**************************************************************************************************
+  Data Types
+**************************************************************************************************/
+
+/*! Application message type */
+typedef union
+{
+  wsfMsgHdr_t     hdr;
+  dmEvt_t         dm;
+  attsCccEvt_t    ccc;
+  attEvt_t        att;
+} bleMsg_t;
diff --git a/epicardium/ble/ble_main.c b/epicardium/ble/ble_main.c
index a6e734308..1992815e4 100644
--- a/epicardium/ble/ble_main.c
+++ b/epicardium/ble/ble_main.c
@@ -52,19 +52,6 @@ static struct epic_scan_report scan_reports[SCAN_REPORTS_NUM];
 static int scan_reports_head;
 static int scan_reports_tail;
 
-/**************************************************************************************************
-  Data Types
-**************************************************************************************************/
-
-/*! Application message type */
-typedef union
-{
-  wsfMsgHdr_t     hdr;
-  dmEvt_t         dm;
-  attsCccEvt_t    ccc;
-  attEvt_t        att;
-} bleMsg_t;
-
 /**************************************************************************************************
   Configurable Parameters
 **************************************************************************************************/
@@ -734,6 +721,7 @@ static void bleProcMsg(bleMsg_t *pMsg)
 
     case ATTS_HANDLE_VALUE_CNF:
       HidProcMsg(&pMsg->hdr);
+      UartProcMsg(pMsg);
       break;
 
     case ATTS_CCC_STATE_IND:
@@ -768,6 +756,7 @@ static void bleProcMsg(bleMsg_t *pMsg)
                connOpen->peerAddr[1], connOpen->peerAddr[0]);
       bleESS_ccc_update();
       HidProcMsg(&pMsg->hdr);
+      UartProcMsg(pMsg);
       e.hdr.event = ATT_CONNECTION_OPENED; e.hdr.param = connOpen->hdr.param;
       send_att_event(&e);
       break;
diff --git a/epicardium/ble/uart.c b/epicardium/ble/uart.c
index 31575fa5f..2a178fd82 100644
--- a/epicardium/ble/uart.c
+++ b/epicardium/ble/uart.c
@@ -126,6 +126,8 @@ static uint8_t UARTWriteCback(
 }
 
 static int ble_uart_lasttick = 0;
+static bool done;
+static bool again;
 
 void ble_uart_flush(void)
 {
@@ -136,21 +138,23 @@ void ble_uart_flush(void)
 	dmConnId_t connId = AppConnIsOpen();
 	if (connId != DM_CONN_ID_NONE) {
 		if (AttsCccEnabled(connId, UART_TX_CH_CCC_IDX)) {
-			int x = xTaskGetTickCount() - ble_uart_lasttick;
-			if (x < 100) {
-				/*
-					* TODO: Ugly hack if we already
-					* sent something recently.
-					* Use ATTS_HANDLE_VALUE_CNF instead.
-					*/
-				vTaskDelay(100 - x);
+			done  = false;
+			again = true;
+
+			// TODO: Modify for no initial delay
+			while (!done && ((xTaskGetTickCount() -
+					  ble_uart_lasttick) < 1000)) {
+				if (again) {
+					again = false;
+					AttsHandleValueNtf(
+						connId,
+						UART_TX_HDL,
+						ble_uart_buf_tx_fill,
+						ble_uart_tx_buf
+					);
+				}
+				vTaskDelay(5);
 			}
-			AttsHandleValueNtf(
-				connId,
-				UART_TX_HDL,
-				ble_uart_buf_tx_fill,
-				ble_uart_tx_buf
-			);
 			ble_uart_lasttick = xTaskGetTickCount();
 		}
 	}
@@ -191,13 +195,18 @@ void bleuart_init(void)
 	AttsAddGroup(&uartCfgGroup);
 }
 
-void UartProcMsg(wsfMsgHdr_t *pMsg)
+void UartProcMsg(bleMsg_t *pMsg)
 {
-	if (pMsg->event == ATTS_HANDLE_VALUE_CNF) {
-		if (pMsg->status == ATT_SUCCESS) {
+	if (pMsg->hdr.event == ATTS_HANDLE_VALUE_CNF) {
+		if (pMsg->att.handle == UART_TX_HDL) {
+			if (pMsg->hdr.status == ATT_SUCCESS) {
+				done = true;
+			} else if (pMsg->hdr.status == ATT_ERR_OVERFLOW) {
+				again = true;
+			}
 		}
 	}
-	if (pMsg->event == DM_CONN_OPEN_IND) {
+	if (pMsg->hdr.event == DM_CONN_OPEN_IND) {
 		ble_uart_buf_tx_fill = 0;
 	}
 }
diff --git a/epicardium/ble/uart.h b/epicardium/ble/uart.h
index ff34e90db..4d3079b5e 100644
--- a/epicardium/ble/uart.h
+++ b/epicardium/ble/uart.h
@@ -1,5 +1,7 @@
 #pragma once
 
+#include "ble_api.h"
+
 #define UART_START_HDL 0x800            /*!< \brief Service start handle. */
 #define UART_END_HDL (UART_MAX_HDL - 1) /*!< \brief Service end handle. */
 
@@ -13,4 +15,5 @@ enum { UART_SVC_HDL = UART_START_HDL, /*!< \brief UART service declaration */
        UART_MAX_HDL                   /*!< \brief Maximum handle. */
 };
 
+void UartProcMsg(bleMsg_t *pMsg);
 
-- 
GitLab