You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
5 months ago
​
​
  1. #ifndef __DFSL_HARDWARE_SERIAL_PROVIDER_HEADER__
  2. #define __DFSL_HARDWARE_SERIAL_PROVIDER_HEADER__
  3. #include "../components/base/basicTransport.h"
  4. #include "hardwareResource.h"
  5. #define HALF_DUPLEX
  6. // #define FIO_SELECT_MODE
  7. #ifndef HALF_DUPLEX
  8. #include "../components/base/basicDualThread.h"
  9. #else
  10. #include "../components/base/basicThread.h"
  11. #endif
  12. #include <errno.h> // Error number definitions
  13. #include <fcntl.h> // File control definitions
  14. #include <stdio.h> // standard input / output functions
  15. #include <stdlib.h>
  16. #include <string.h> // string function definitions
  17. #include <sys/select.h>
  18. #include <sys/types.h>
  19. #include <unistd.h>
  20. #ifdef __linux__
  21. #include <termios.h> // POSIX terminal control definitions
  22. #include <unistd.h> // UNIX standard function definitions
  23. #if defined(__arm__) || defined(__aarch64__) || defined(RASPI_HARDWARE)
  24. #define USE_WIRINGX
  25. #include <wiringx.h>
  26. #ifdef __aarch64__
  27. #define WIRINGX_PLATFORM "rock4"
  28. #define RTS_PIN_NUMBER 0
  29. #else
  30. #define WIRINGX_PLATFORM "raspberrypi4"
  31. #define RTS_PIN_NUMBER 0
  32. #endif
  33. #endif
  34. #endif
  35. #ifdef DEBUG_EXTRA
  36. // #define DEBUG_SERIAL_COMMUNICATION
  37. #endif
  38. #ifndef MAXIMAL_SERIAL_IO_BUFFER_LENGTH
  39. #define MAXIMAL_SERIAL_IO_BUFFER_LENGTH 102400
  40. #endif
  41. #if defined( HALF_DUPLEX ) || defined( SERIAL_PING_PONG )
  42. #include <queue>
  43. typedef struct S_SIO_TZ {
  44. bool wReplay;
  45. bool finished;
  46. bool success;
  47. int inp_size;
  48. int out_size;
  49. #ifdef SERIAL_PING_PONG
  50. protocolParser * boundParser;
  51. #endif
  52. uint8_t out_buffer[MAXIMAL_SERIAL_IO_BUFFER_LENGTH];
  53. uint8_t inp_buffer[MAXIMAL_SERIAL_IO_BUFFER_LENGTH];
  54. } SIO_TZ;
  55. typedef std::queue<SIO_TZ *> Q_SIO_TZ;
  56. class hardwareResourceSerialProvider : public hardwareResource, public basicTransport, public basicThread
  57. {
  58. #else // ! HALF_DUPLEX SERIAL_PING_PONG
  59. class hardwareResourceSerialProvider : public hardwareResource, public basicTransport, public basicDualThread
  60. {
  61. #endif // ! HALF_DUPLEX SERIAL_PING_PONG
  62. private:
  63. DBC_UART_DATA m_sUartData;
  64. int m_iDeviceFD;
  65. int m_iMasterFD;
  66. #ifdef __linux__
  67. struct termios m_sTTY;
  68. int m_iInFID;
  69. int m_iOutFID;
  70. bool m_bManualRTS;
  71. void setRTS(uint8_t Val);
  72. #else
  73. DCB PortDCB;
  74. HANDLE m_ioFID;
  75. #endif
  76. bool pingDevice();
  77. bool create_SOL();
  78. bool create_HW();
  79. bool closePort();
  80. int sendBlock(unsigned char * Data, int DataSize);
  81. int recptBlock(unsigned char * Buffer, int * BufferLength);
  82. protected:
  83. #ifdef FIO_SELECT_MODE
  84. int m_nSenderSetMax;
  85. int m_nReaderSetMax;
  86. fd_set m_sSenderSet;
  87. fd_set m_sReaderSet;
  88. int m_iControlPipeFD[2];
  89. #endif
  90. #if defined( HALF_DUPLEX ) || defined( SERIAL_PING_PONG )
  91. virtual void m_fThreadProc(void);
  92. Q_SIO_TZ m_qIO;
  93. // Q_SIO_TZ m_qREADY;
  94. #else
  95. U8 m_pInputBuffer[MAXIMAL_SERIAL_IO_BUFFER_LENGTH]; // 100KB
  96. U8 m_pOutputBuffer[MAXIMAL_SERIAL_IO_BUFFER_LENGTH];
  97. int m_nInputBufferSize;
  98. int m_nOutputBufferSize;
  99. virtual void m_fThreadAProc(void);
  100. virtual void m_fThreadBProc(void);
  101. #endif
  102. public:
  103. explicit hardwareResourceSerialProvider(uint32_t dbID);
  104. virtual ~hardwareResourceSerialProvider();
  105. virtual bool create(void);
  106. virtual bool validate(void);
  107. #ifdef SERIAL_PING_PONG
  108. virtual bool sendData(uint8_t * Data, int DataSize, protocolParser *src = NULL );
  109. #else
  110. virtual bool sendData(uint8_t * Data, int DataSize );
  111. #endif
  112. virtual bool reconnect(void);
  113. virtual bool activate( void );
  114. };
  115. #endif