diff --git a/epicardium/ble/ble_api.h b/epicardium/ble/ble_api.h index baad5eaf144028a0db21684d2e73c6c3b2c113dc..971ed38e15ae48049feef8a8162183a190adef81 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 a6e7343088a4609eeaf2c22314f6a8350a44a47e..1992815e416261cd20797ada305a2c448a1e81a6 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 31575fa5f99e78fc5bf1041be343fe6788df4276..2a178fd82c072a45ef224950db000718b88f64df 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 ff34e90db74b41b5042c17d1aa87d2ca1cdae64e..4d3079b5e4bb167e93498e6e87e77704a11c226d 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);