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.

88 lines
2.6 KiB

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. LOG("Thread A Started");
  33. while( m_bThreadAStatus < TH_ENDED ) {
  34. m_mBufferProtectorMutex.lock();
  35. if( m_nOutputBufferSize > 0 ) {
  36. int index;
  37. int RetVal;
  38. index = 0;
  39. LOG("Buffer ready to Send");
  40. while ((index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED)){
  41. RetVal = sendBlock( m_pOutputBuffer + index, m_nOutputBufferSize - index );
  42. if( RetVal > 0 ) index += RetVal;
  43. if (RetVal < 0) m_bThreadAStatus = TH_ENDED;
  44. };
  45. LOG("Buffer is Sent");
  46. m_nOutputBufferSize = 0;
  47. };
  48. m_mBufferProtectorMutex.unlock();
  49. SLEEP(10);
  50. };
  51. };
  52. //void *transportSerial::receiverProc( void *param ){
  53. void transportSerial::m_fThreadBProc(void){
  54. m_bThreadBStatus = TH_WORKING;
  55. LOG("Thread B Started");
  56. while (m_bThreadBStatus < TH_ENDED) {
  57. recptBlock( m_pInputBuffer, &m_nInputBufferSize );
  58. if( m_nInputBufferSize > 0 && m_bThreadBStatus == TH_WORKING ) {
  59. m_mQueueProtectorMutex.lock();
  60. for( parseQueue::iterator index=m_vParsers.begin(); index != m_vParsers.end(); index++ ){
  61. if( m_bThreadBStatus == TH_WORKING ) (*index)->parse( m_pInputBuffer, m_nInputBufferSize );
  62. };
  63. m_mQueueProtectorMutex.unlock();
  64. };
  65. SLEEP(5);
  66. };
  67. };
  68. bool transportSerial::sendData( unsigned char *Data, int DataSize ){
  69. m_mBufferProtectorMutex.lock();
  70. if( m_nOutputBufferSize+DataSize > MAXIMAL_SERIAL_PACKET_LENGTH ) {
  71. m_mBufferProtectorMutex.unlock();
  72. LOG("Data Too Long");
  73. return false;
  74. };
  75. LOG("Output Data Set");
  76. memcpy( m_pOutputBuffer+m_nOutputBufferSize, Data, DataSize );
  77. m_nOutputBufferSize += DataSize;
  78. m_mBufferProtectorMutex.unlock();
  79. return true;
  80. };
  81. #undef LOG
  82. #undef LOG_ERROR