From 15e030fc78124ddfb66d43371e5d928fed277f9c Mon Sep 17 00:00:00 2001 From: schneider <schneider@blinkenlichts.net> Date: Sun, 17 Jan 2021 22:05:30 +0100 Subject: [PATCH] change(ble): correctly handle connections in ble uart --- epicardium/ble/uart.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/epicardium/ble/uart.c b/epicardium/ble/uart.c index 8fbe737db..31575fa5f 100644 --- a/epicardium/ble/uart.c +++ b/epicardium/ble/uart.c @@ -1,10 +1,13 @@ #include "uart.h" +#include "cccd.h" #include "modules/modules.h" #include "wsf_types.h" #include "util/bstream.h" #include "att_api.h" +#include "dm_api.h" +#include "app_api.h" #include "FreeRTOS.h" #include "timers.h" @@ -99,8 +102,6 @@ static const attsAttr_t uartAttrCfgList[] = { }; -dmConnId_t active_connection = 0; - static uint8_t UARTWriteCback( dmConnId_t connId, uint16_t handle, @@ -110,7 +111,6 @@ static uint8_t UARTWriteCback( uint8_t *pValue, attsAttr_t *pAttr ) { - active_connection = connId; static bool was_r = false; int i; @@ -129,27 +129,32 @@ static int ble_uart_lasttick = 0; void ble_uart_flush(void) { - if (ble_uart_buf_tx_fill > 0) { - if (active_connection) { + if (ble_uart_buf_tx_fill == 0) { + return; + } + + 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. - */ + * TODO: Ugly hack if we already + * sent something recently. + * Use ATTS_HANDLE_VALUE_CNF instead. + */ vTaskDelay(100 - x); } AttsHandleValueNtf( - active_connection, + connId, UART_TX_HDL, ble_uart_buf_tx_fill, ble_uart_tx_buf ); ble_uart_lasttick = xTaskGetTickCount(); } - ble_uart_buf_tx_fill = 0; } + ble_uart_buf_tx_fill = 0; } void ble_uart_write_char(uint8_t c) @@ -185,3 +190,14 @@ void bleuart_init(void) /* Add the UART service */ AttsAddGroup(&uartCfgGroup); } + +void UartProcMsg(wsfMsgHdr_t *pMsg) +{ + if (pMsg->event == ATTS_HANDLE_VALUE_CNF) { + if (pMsg->status == ATT_SUCCESS) { + } + } + if (pMsg->event == DM_CONN_OPEN_IND) { + ble_uart_buf_tx_fill = 0; + } +} -- GitLab