hal_serial.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * hal_serial.h
  3. *
  4. * Copyright 2013-2021 Michael Zillgith
  5. *
  6. * This file is part of Platform Abstraction Layer (libpal)
  7. * for libiec61850, libmms, and lib60870.
  8. */
  9. #ifndef HAL_SERIAL_H
  10. #define HAL_SERIAL_H
  11. #include "hal_base.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /**
  16. * \file hal_serial.h
  17. * \brief Abstraction layer for serial ports.
  18. * Has to be implemented for the serial link layer of CS 101.
  19. */
  20. /*! \addtogroup hal Platform (Hardware/OS) abstraction layer
  21. *
  22. * @{
  23. */
  24. /**
  25. * @defgroup HAL_SERIAL Access to serial interfaces
  26. *
  27. * Serial interface abstraction layer. This functions have to be implemented to
  28. * port lib60870 to new platforms when the serial link layers are required.
  29. *
  30. * @{
  31. */
  32. typedef struct sSerialPort* SerialPort;
  33. typedef enum {
  34. SERIAL_PORT_ERROR_NONE = 0,
  35. SERIAL_PORT_ERROR_INVALID_ARGUMENT = 1,
  36. SERIAL_PORT_ERROR_INVALID_BAUDRATE = 2,
  37. SERIAL_PORT_ERROR_OPEN_FAILED = 3,
  38. SERIAL_PORT_ERROR_UNKNOWN = 99
  39. } SerialPortError;
  40. /**
  41. * \brief Create a new SerialPort instance
  42. *
  43. * \param interfaceName identifier or name of the serial interface (e.g. "/dev/ttyS1" or "COM4")
  44. * \param baudRate the baud rate in baud (e.g. 9600)
  45. * \param dataBits the number of data bits (usually 8)
  46. * \param parity defines what kind of parity to use ('E' - even parity, 'O' - odd parity, 'N' - no parity)
  47. * \param stopBits the number of stop buts (usually 1)
  48. *
  49. * \return the new SerialPort instance
  50. */
  51. PAL_API SerialPort
  52. SerialPort_create(const char* interfaceName, int baudRate, uint8_t dataBits, char parity, uint8_t stopBits);
  53. /**
  54. * \brief Destroy the SerialPort instance and release all resources
  55. */
  56. PAL_API void
  57. SerialPort_destroy(SerialPort self);
  58. /**
  59. * \brief Open the serial interface
  60. *
  61. * \return true in case of success, false otherwise (use \ref SerialPort_getLastError for a detailed error code)
  62. */
  63. PAL_API bool
  64. SerialPort_open(SerialPort self);
  65. /**
  66. * \brief Close (release) the serial interface
  67. */
  68. PAL_API void
  69. SerialPort_close(SerialPort self);
  70. /**
  71. * \brief Get the baudrate used by the serial interface
  72. *
  73. * \return the baud rate in baud
  74. */
  75. PAL_API int
  76. SerialPort_getBaudRate(SerialPort self);
  77. /**
  78. * \brief Set the timeout used for message reception
  79. *
  80. * \param timeout the timeout value in ms.
  81. */
  82. PAL_API void
  83. SerialPort_setTimeout(SerialPort self, int timeout);
  84. /**
  85. * \brief Discard all data in the input buffer of the serial interface
  86. */
  87. PAL_API void
  88. SerialPort_discardInBuffer(SerialPort self);
  89. /**
  90. * \brief Read a byte from the interface
  91. *
  92. * \return number of read bytes of -1 in case of an error
  93. */
  94. PAL_API int
  95. SerialPort_readByte(SerialPort self);
  96. /**
  97. * \brief Write the number of bytes from the buffer to the serial interface
  98. *
  99. * \param buffer the buffer containing the data to write
  100. * \param startPos start position in the buffer of the data to write
  101. * \param numberOfBytes number of bytes to write
  102. *
  103. * \return number of bytes written, or -1 in case of an error
  104. */
  105. PAL_API int
  106. SerialPort_write(SerialPort self, uint8_t* buffer, int startPos, int numberOfBytes);
  107. /**
  108. * \brief Get the error code of the last operation
  109. */
  110. PAL_API SerialPortError
  111. SerialPort_getLastError(SerialPort self);
  112. /*! @} */
  113. /*! @} */
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* SRC_IEC60870_LINK_LAYER_SERIAL_PORT_H_ */