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.

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