2021-07-13 19:19:25 +02:00
|
|
|
#ifndef BSP_Q7S_COMIF_UARTCOMIF_H_
|
|
|
|
#define BSP_Q7S_COMIF_UARTCOMIF_H_
|
|
|
|
|
2022-10-06 11:14:00 +02:00
|
|
|
#include "UartCookie.h"
|
|
|
|
#include "helper.h"
|
|
|
|
|
2021-07-13 19:19:25 +02:00
|
|
|
#include <fsfw/devicehandlers/DeviceCommunicationIF.h>
|
2022-02-02 10:29:30 +01:00
|
|
|
#include <fsfw/objectmanager/SystemObject.h>
|
2021-07-13 19:19:25 +02:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
|
2021-07-13 19:19:25 +02:00
|
|
|
/**
|
|
|
|
* @brief This is the communication interface to access serial ports on linux based operating
|
|
|
|
* systems.
|
|
|
|
*
|
|
|
|
* @details The implementation follows the instructions from https://blog.mbedded.ninja/programming/
|
|
|
|
* operating-systems/linux/linux-serial-ports-using-c-cpp/#disabling-canonical-mode
|
|
|
|
*
|
|
|
|
* @author J. Meier
|
|
|
|
*/
|
2022-02-02 10:29:30 +01:00
|
|
|
class UartComIF : public DeviceCommunicationIF, public SystemObject {
|
|
|
|
public:
|
|
|
|
static constexpr uint8_t uartRetvalId = CLASS_ID::HAL_UART;
|
|
|
|
|
2022-08-22 15:02:16 +02:00
|
|
|
static constexpr ReturnValue_t UART_READ_FAILURE = returnvalue::makeCode(uartRetvalId, 1);
|
|
|
|
static constexpr ReturnValue_t UART_READ_SIZE_MISSMATCH = returnvalue::makeCode(uartRetvalId, 2);
|
|
|
|
static constexpr ReturnValue_t UART_RX_BUFFER_TOO_SMALL = returnvalue::makeCode(uartRetvalId, 3);
|
2022-02-02 10:29:30 +01:00
|
|
|
|
|
|
|
UartComIF(object_id_t objectId);
|
|
|
|
|
|
|
|
virtual ~UartComIF();
|
|
|
|
|
|
|
|
ReturnValue_t initializeInterface(CookieIF* cookie) override;
|
|
|
|
ReturnValue_t sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) override;
|
|
|
|
ReturnValue_t getSendSuccess(CookieIF* cookie) override;
|
|
|
|
ReturnValue_t requestReceiveMessage(CookieIF* cookie, size_t requestLen) override;
|
|
|
|
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;
|
|
|
|
|
|
|
|
struct UartElements {
|
|
|
|
int fileDescriptor;
|
|
|
|
std::vector<uint8_t> replyBuffer;
|
|
|
|
/** Number of bytes read will be written to this variable */
|
|
|
|
size_t replyLen;
|
|
|
|
};
|
|
|
|
|
|
|
|
using UartDeviceMap = std::unordered_map<UartDeviceFile_t, UartElements>;
|
|
|
|
using UartDeviceMapIter = UartDeviceMap::iterator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The uart devie map stores informations of initialized uart ports.
|
|
|
|
*/
|
|
|
|
UartDeviceMap uartDeviceMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This function opens and configures a uart device by using the information stored
|
|
|
|
* in the uart cookie.
|
|
|
|
* @param uartCookie Pointer to uart cookie with information about the uart. Contains the
|
|
|
|
* uart device file, baudrate, parity, stopbits etc.
|
|
|
|
* @return The file descriptor of the configured uart.
|
|
|
|
*/
|
|
|
|
int configureUartPort(UartCookie* uartCookie);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This function adds the parity settings to the termios options struct.
|
|
|
|
*
|
|
|
|
* @param options Pointer to termios options struct which will be modified to enable or disable
|
|
|
|
* parity checking.
|
|
|
|
* @param uartCookie Pointer to uart cookie containing the information about the desired
|
|
|
|
* parity settings.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void setParityOptions(struct termios* options, UartCookie* uartCookie);
|
|
|
|
|
|
|
|
void setStopBitOptions(struct termios* options, UartCookie* uartCookie);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This function sets options which are not configurable by the uartCookie.
|
|
|
|
*/
|
|
|
|
void setFixedOptions(struct termios* options);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief With this function the datasize settings are added to the termios options struct.
|
|
|
|
*/
|
|
|
|
void setDatasizeOptions(struct termios* options, UartCookie* uartCookie);
|
|
|
|
|
|
|
|
ReturnValue_t handleCanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
|
|
|
|
size_t requestLen);
|
|
|
|
ReturnValue_t handleNoncanonicalRead(UartCookie& uartCookie, UartDeviceMapIter& iter,
|
|
|
|
size_t requestLen);
|
2021-07-13 19:19:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* BSP_Q7S_COMIF_UARTCOMIF_H_ */
|