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.

112 lines
3.3 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
​
​
​
​
​
​
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. RetVal = sendBlock( m_pOutputBuffer + index, m_nOutputBufferSize - index );
  40. if( RetVal > 0 ) index += RetVal;
  41. if (RetVal < 0) {
  42. setConnectionStatus(CS_DISCONNECTED);
  43. m_bThreadAStatus = TH_ENDED;
  44. };
  45. };
  46. #ifdef DEBUG_COMMUNICATION
  47. LOG("Buffer is Sent");
  48. #endif
  49. m_nOutputBufferSize = 0;
  50. nSleepTime = 1;
  51. } else {
  52. nSleepTime = 10;
  53. };
  54. m_mBufferProtectorMutex.unlock();
  55. SLEEP( nSleepTime );
  56. };
  57. };
  58. //void *transportSerial::receiverProc( void *param ){
  59. void transportSerial::m_fThreadBProc(void){
  60. m_bThreadBStatus = TH_WORKING;
  61. uint32_t nSleepTime = 1;
  62. #ifdef DEBUG_COMMUNICATION
  63. LOG("Input Thread Started");
  64. #endif
  65. while (m_bThreadBStatus < TH_ENDED) {
  66. int retVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
  67. if (retVal < 0) {
  68. setConnectionStatus(CS_DISCONNECTED);
  69. m_bThreadBStatus = TH_ENDED;
  70. continue;
  71. } else {
  72. if (m_nInputBufferSize > 0 && m_bThreadBStatus == TH_WORKING) {
  73. m_mQueueProtectorMutex.lock();
  74. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) {
  75. if (m_bThreadBStatus == TH_WORKING) {
  76. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  77. }
  78. };
  79. m_mQueueProtectorMutex.unlock();
  80. nSleepTime = 1;
  81. } else {
  82. nSleepTime = 10;
  83. if( m_bThreadBStatus != TH_WORKING ) continue;
  84. };
  85. };
  86. SLEEP( nSleepTime );
  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