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.

111 lines
3.1 KiB

7 years ago
6 years ago
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
​
​
​
​
  1. #include "transportSerial.h"
  2. #include "../../data/protocol/protocol.h"
  3. #define LOG( X ) m_pLog->Log( "TSER", this, X )
  4. #define LOG_ERROR( X ) m_pLog->Log( "TSER", this, _LOG_ERROR, X )
  5. transportSerial::transportSerial() :basicTransport(), basicDualThread(){
  6. m_pLog = basicLogCollection::getInstance();
  7. m_nInputBufferSize = -1;
  8. m_nOutputBufferSize = -1;
  9. m_pInputBuffer = ( U8 * ) malloc ( MAXIMAL_SERIAL_PACKET_LENGTH );
  10. if( m_pInputBuffer != NULL ) {
  11. m_nInputBufferSize = 0;
  12. m_pOutputBuffer = ( U8 * ) malloc ( MAXIMAL_SERIAL_PACKET_LENGTH );
  13. if( m_pOutputBuffer != NULL ) {
  14. m_nOutputBufferSize = 0;
  15. //LOG("Starting");
  16. //init(); // Moved to SerialPort
  17. };
  18. };
  19. };
  20. transportSerial::~transportSerial(){
  21. //waitThreadAEnd( true );
  22. //waitThreadBEnd( true ); Moved to SerialPort
  23. if( m_pInputBuffer != NULL ) {
  24. free(m_pInputBuffer);
  25. };
  26. if( m_pOutputBuffer != NULL ) {
  27. free(m_pOutputBuffer);
  28. };
  29. };
  30. //void *transportSerial::senderProc( void *param ){
  31. void transportSerial::m_fThreadAProc( void ){
  32. m_bThreadAStatus = TH_WORKING;
  33. #ifdef DEBUG_COMMUNICATION
  34. LOG("Output Thread Started");
  35. #endif
  36. while( m_bThreadAStatus < TH_ENDED ) {
  37. m_mBufferProtectorMutex.lock();
  38. if( m_nOutputBufferSize > 0 ) {
  39. int index;
  40. int RetVal;
  41. index = 0;
  42. #ifdef DEBUG_COMMUNICATION
  43. LOG("Buffer ready to Send");
  44. #endif
  45. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
  46. RetVal = sendBlock( m_pOutputBuffer + index, m_nOutputBufferSize - index );
  47. if( RetVal > 0 ) index += RetVal;
  48. if (RetVal < 0) {
  49. setConnectionStatus(CS_DISCONNECTED);
  50. m_bThreadAStatus = TH_ENDED;
  51. };
  52. };
  53. #ifdef DEBUG_COMMUNICATION
  54. LOG("Buffer is Sent");
  55. #endif
  56. m_nOutputBufferSize = 0;
  57. };
  58. m_mBufferProtectorMutex.unlock();
  59. //SLEEP(10);
  60. SLEEP(1);
  61. };
  62. };
  63. //void *transportSerial::receiverProc( void *param ){
  64. void transportSerial::m_fThreadBProc(void){
  65. m_bThreadBStatus = TH_WORKING;
  66. #ifdef DEBUG_COMMUNICATION
  67. LOG("Input Thread Started");
  68. #endif
  69. while (m_bThreadBStatus < TH_ENDED) {
  70. int retVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
  71. if (retVal < 0) {
  72. setConnectionStatus(CS_DISCONNECTED);
  73. m_bThreadBStatus = TH_ENDED;
  74. } else {
  75. if (m_nInputBufferSize > 0 && m_bThreadBStatus == TH_WORKING) {
  76. m_mQueueProtectorMutex.lock();
  77. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) {
  78. if (m_bThreadBStatus == TH_WORKING) {
  79. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  80. }
  81. };
  82. m_mQueueProtectorMutex.unlock();
  83. };
  84. //SLEEP(5);
  85. SLEEP(1);
  86. };
  87. };
  88. };
  89. bool transportSerial::sendData( unsigned char *Data, int DataSize ){
  90. m_mBufferProtectorMutex.lock();
  91. if( m_nOutputBufferSize+DataSize > MAXIMAL_SERIAL_PACKET_LENGTH ) {
  92. m_mBufferProtectorMutex.unlock();
  93. LOG_ERROR("Data Too Long");
  94. return false;
  95. };
  96. #ifdef DEBUG_COMMUNICATION
  97. LOG("Output Data Set");
  98. #endif
  99. memcpy( m_pOutputBuffer+m_nOutputBufferSize, Data, DataSize );
  100. m_nOutputBufferSize += DataSize;
  101. m_mBufferProtectorMutex.unlock();
  102. return true;
  103. };
  104. #undef LOG
  105. #undef LOG_ERROR