diff --git a/hal/src/fsfw/hal/linux/uart/UartComIF.cpp b/hal/src/fsfw/hal/linux/uart/UartComIF.cpp index e5fc90c3..e543f60e 100644 --- a/hal/src/fsfw/hal/linux/uart/UartComIF.cpp +++ b/hal/src/fsfw/hal/linux/uart/UartComIF.cpp @@ -443,6 +443,51 @@ ReturnValue_t UartComIF::readReceivedMessage(CookieIF *cookie, return RETURN_OK; } +ReturnValue_t UartComIF::flushUartRxBuffer(CookieIF *cookie) { + std::string deviceFile; + UartDeviceMapIter uartDeviceMapIter; + UartCookie* uartCookie = dynamic_cast(cookie); + if(uartCookie == nullptr) { + sif::warning << "UartComIF::flushUartRxBuffer: Invalid uart cookie!" << std::endl; + return NULLPOINTER; + } + deviceFile = uartCookie->getDeviceFile(); + uartDeviceMapIter = uartDeviceMap.find(deviceFile); + int fd = uartDeviceMapIter->second.fileDescriptor; + tcflush(fd, TCIFLUSH); + return RETURN_OK; +} + +ReturnValue_t UartComIF::flushUartTxBuffer(CookieIF *cookie) { + std::string deviceFile; + UartDeviceMapIter uartDeviceMapIter; + UartCookie* uartCookie = dynamic_cast(cookie); + if(uartCookie == nullptr) { + sif::warning << "UartComIF::flushUartTxBuffer: Invalid uart cookie!" << std::endl; + return NULLPOINTER; + } + deviceFile = uartCookie->getDeviceFile(); + uartDeviceMapIter = uartDeviceMap.find(deviceFile); + int fd = uartDeviceMapIter->second.fileDescriptor; + tcflush(fd, TCOFLUSH); + return RETURN_OK; +} + +ReturnValue_t UartComIF::flushUartTxAndRxBuf(CookieIF *cookie) { + std::string deviceFile; + UartDeviceMapIter uartDeviceMapIter; + UartCookie* uartCookie = dynamic_cast(cookie); + if(uartCookie == nullptr) { + sif::warning << "UartComIF::flushUartTxAndRxBuf: Invalid uart cookie!" << std::endl; + return NULLPOINTER; + } + deviceFile = uartCookie->getDeviceFile(); + uartDeviceMapIter = uartDeviceMap.find(deviceFile); + int fd = uartDeviceMapIter->second.fileDescriptor; + tcflush(fd, TCIOFLUSH); + return RETURN_OK; +} + void UartComIF::setUartMode(struct termios *options, UartCookie &uartCookie) { UartModes uartMode = uartCookie.getUartMode(); if(uartMode == UartModes::NON_CANONICAL) { diff --git a/hal/src/fsfw/hal/linux/uart/UartComIF.h b/hal/src/fsfw/hal/linux/uart/UartComIF.h index e513aa86..68d2b9f5 100644 --- a/hal/src/fsfw/hal/linux/uart/UartComIF.h +++ b/hal/src/fsfw/hal/linux/uart/UartComIF.h @@ -41,6 +41,21 @@ public: ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **buffer, size_t *size) override; + /** + * @brief This function discards all data received but not read in the UART buffer. + */ + ReturnValue_t flushUartRxBuffer(CookieIF *cookie); + + /** + * @brief This function discards all data in the transmit buffer of the UART driver. + */ + ReturnValue_t flushUartTxBuffer(CookieIF *cookie); + + /** + * @brief This function discards both data in the transmit and receive buffer of the UART. + */ + ReturnValue_t flushUartTxAndRxBuf(CookieIF *cookie); + private: using UartDeviceFile_t = std::string;