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.

134 lines
4.2 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
3 years ago
​
​
​
​
​
​
​
​
7 years ago
7 years ago
7 years ago
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
7 years ago
3 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. //For Select IO Mode
  16. pipe( m_iControlPipeFD );
  17. FD_ZERO( &m_sSenderSet );
  18. FD_ZERO( &m_sReaderSet );
  19. FD_SET( m_iControlPipeFD[0], &m_sSenderSet );
  20. m_nSenderSetMax = m_iControlPipeFD[0];
  21. FD_SET( m_iControlPipeFD[1], &m_sReaderSet );
  22. m_nReaderSetMax = m_iControlPipeFD[1];
  23. };
  24. };
  25. };
  26. transportSerial::~transportSerial(){
  27. _close();
  28. if( m_pInputBuffer != NULL ) free( m_pInputBuffer );
  29. if( m_pOutputBuffer != NULL ) free( m_pOutputBuffer );
  30. };
  31. bool transportSerial::create( void ){
  32. m_bInitComplete = init();
  33. if( m_bInitComplete ) setConnectionStatus( CS_CONNECTED );
  34. return m_bInitComplete;
  35. };
  36. //void *transportSerial::senderProc( void *param ){
  37. void transportSerial::m_fThreadAProc( void ){
  38. m_bThreadAStatus = TH_WORKING;
  39. uint32_t nSleepTime = 1;
  40. #ifdef DEBUG_COMMUNICATION
  41. LOG("Output Thread Started ");
  42. #endif
  43. while( m_bThreadAStatus < TH_ENDED ) {
  44. m_mBufferProtectorMutex.lock();
  45. if( m_nOutputBufferSize > 0 ) {
  46. int index;
  47. int RetVal;
  48. index = 0;
  49. #ifdef DEBUG_COMMUNICATION
  50. LOG("Buffer ready to Send");
  51. #endif
  52. while( (index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED) ){
  53. if ( m_mIsBusy.try_lock() ) {
  54. RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
  55. m_mIsBusy.unlock();
  56. if( RetVal > 0 ) index += RetVal;
  57. if (RetVal < 0) {
  58. setConnectionStatus(CS_DISCONNECTED);
  59. m_bThreadAStatus = TH_ENDED;
  60. };
  61. } else SLEEP( 3 );
  62. };
  63. #ifdef DEBUG_COMMUNICATION
  64. LOG("Buffer is Sent");
  65. #endif
  66. m_nOutputBufferSize = 0;
  67. nSleepTime = 1;
  68. } else {
  69. nSleepTime = 10;
  70. };
  71. m_mBufferProtectorMutex.unlock();
  72. SLEEP( nSleepTime );
  73. };
  74. };
  75. //void *transportSerial::receiverProc( void *param ){
  76. void transportSerial::m_fThreadBProc(void){
  77. m_bThreadBStatus = TH_WORKING;
  78. uint32_t nSleepTime = 1;
  79. #ifdef DEBUG_COMMUNICATION
  80. LOG("Input Thread Started");
  81. #endif
  82. while (m_bThreadBStatus < TH_ENDED) {
  83. if ( m_mIsBusy.try_lock() ) {
  84. int retVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
  85. m_mIsBusy.unlock();
  86. if (retVal < 0) {
  87. setConnectionStatus(CS_DISCONNECTED);
  88. m_bThreadBStatus = TH_ENDED;
  89. continue;
  90. } else {
  91. if (m_nInputBufferSize > 0 && m_bThreadBStatus == TH_WORKING) {
  92. m_mQueueProtectorMutex.lock();
  93. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) {
  94. if (m_bThreadBStatus == TH_WORKING) {
  95. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  96. }
  97. };
  98. m_mQueueProtectorMutex.unlock();
  99. nSleepTime = 1;
  100. } else {
  101. nSleepTime = 10;
  102. if( m_bThreadBStatus != TH_WORKING ) continue;
  103. };
  104. };
  105. SLEEP( nSleepTime );
  106. } else SLEEP( 3 );
  107. };
  108. };
  109. bool transportSerial::sendData( unsigned char *Data, int DataSize ){
  110. m_mBufferProtectorMutex.lock();
  111. if( m_nOutputBufferSize+DataSize > MAXIMAL_SERIAL_PACKET_LENGTH ) {
  112. m_mBufferProtectorMutex.unlock();
  113. LOG_ERROR("Data Too Long");
  114. return false;
  115. };
  116. #ifdef DEBUG_COMMUNICATION
  117. LOG("Output Data Set");
  118. #endif
  119. memcpy( m_pOutputBuffer+m_nOutputBufferSize, Data, DataSize );
  120. m_nOutputBufferSize += DataSize;
  121. m_mBufferProtectorMutex.unlock();
  122. return true;
  123. };
  124. #undef LOG
  125. #undef LOG_ERROR