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.

106 lines
2.9 KiB

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