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.

87 lines
2.6 KiB

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