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.

110 lines
3.0 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
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. SLEEP(1);
  60. };
  61. };
  62. //void *transportSerial::receiverProc( void *param ){
  63. void transportSerial::m_fThreadBProc(void){
  64. m_bThreadBStatus = TH_WORKING;
  65. #ifdef DEBUG
  66. LOG("Thread B Started");
  67. #endif
  68. while (m_bThreadBStatus < TH_ENDED) {
  69. int retVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
  70. if (retVal < 0) {
  71. setConnectionStatus(CS_DISCONNECTED);
  72. m_bThreadBStatus = TH_ENDED;
  73. } else {
  74. if (m_nInputBufferSize > 0 && m_bThreadBStatus == TH_WORKING) {
  75. m_mQueueProtectorMutex.lock();
  76. for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) {
  77. if (m_bThreadBStatus == TH_WORKING) {
  78. (*index)->parse(m_pInputBuffer, m_nInputBufferSize);
  79. }
  80. };
  81. m_mQueueProtectorMutex.unlock();
  82. };
  83. //SLEEP(5);
  84. SLEEP(1);
  85. };
  86. };
  87. };
  88. bool transportSerial::sendData( unsigned char *Data, int DataSize ){
  89. m_mBufferProtectorMutex.lock();
  90. if( m_nOutputBufferSize+DataSize > MAXIMAL_SERIAL_PACKET_LENGTH ) {
  91. m_mBufferProtectorMutex.unlock();
  92. LOG_ERROR("Data Too Long");
  93. return false;
  94. };
  95. #ifdef DEBUG
  96. LOG("Output Data Set");
  97. #endif
  98. memcpy( m_pOutputBuffer+m_nOutputBufferSize, Data, DataSize );
  99. m_nOutputBufferSize += DataSize;
  100. m_mBufferProtectorMutex.unlock();
  101. return true;
  102. };
  103. #undef LOG
  104. #undef LOG_ERROR