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.

168 lines
4.8 KiB

7 years ago
​
​
​
​
6 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
6 years ago
7 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
6 years ago
7 years ago
​
6 years ago
​
​
​
​
7 years ago
​
6 years ago
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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocol.h"
  2. #define LOG( X ) m_pLog->Log( "Proto", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "Proto", this, _LOG_ERROR, X )
  4. protocolParser::protocolParser() :basicThread(),m_mBufferProtectorMutex(){
  5. m_pLog = basicLogCollection::getInstance();
  6. m_pParserBuffer = (U8*) malloc(PROTOCOL_PARSER_BUFFER_SIZE);
  7. m_nParserDataCount = 0;
  8. m_bStatus = 0;
  9. m_pCustomHandler = NULL;
  10. m_bBufferAltered = false;
  11. m_bStatus = 1;
  12. m_eConnectionStatus = CS_UNKNOWN;
  13. };
  14. protocolParser::~protocolParser(){
  15. # ifdef DEBUG
  16. LOG("Destructor Begin");
  17. # endif
  18. m_mBufferProtectorMutex.unlock();
  19. if (m_bThreadStatus < TH_ENDED ) {
  20. waitThreadEnd( true );
  21. };
  22. if( m_pParserBuffer != NULL ) {
  23. free( m_pParserBuffer );
  24. };
  25. # ifdef DEBUG
  26. LOG("Destructor End");
  27. # endif
  28. };
  29. bool protocolParser::create(void){
  30. if (m_pParserBuffer != NULL) {
  31. return basicThread::init();
  32. } else return false;
  33. };
  34. void protocolParser::setConnectionStatus( CONNECTION_STATUS cStatus ){
  35. if (cStatus == CS_CONNECTED)LOG("Connection Status Changed - Connected!");
  36. if (cStatus == CS_DISCONNECTED)LOG("Connection Status Changed - Disconnected!");
  37. m_eConnectionStatus = cStatus;
  38. };
  39. CONNECTION_STATUS protocolParser::getConnectionStatus( void ){
  40. return m_eConnectionStatus;
  41. };
  42. bool protocolParser::setTransport( basicTransport* Transport, bool check_if_allready ){
  43. if (Transport == NULL) return false;
  44. if( check_if_allready ) {
  45. if (m_oDataTransport != NULL) {
  46. LOG_ERROR("Transport Already Assigned!");
  47. return false;
  48. };
  49. };
  50. m_oDataTransport = Transport;
  51. return m_oDataTransport->addRecipient( this );
  52. };
  53. bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){
  54. bool rv = false;
  55. if( m_pParserBuffer != NULL ) {
  56. m_mBufferProtectorMutex.lock();
  57. if( ( m_nParserDataCount + BufferSize ) < PROTOCOL_PARSER_BUFFER_SIZE ) {
  58. memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
  59. m_nParserDataCount+=BufferSize;
  60. rv = m_bBufferAltered = true;
  61. };
  62. m_mBufferProtectorMutex.unlock();
  63. };
  64. return rv;
  65. };
  66. void protocolParser::dataEraser( U8 *&tPtr ){
  67. if (tPtr == NULL) return;
  68. free(tPtr);
  69. tPtr = NULL;
  70. };
  71. void protocolParser::m_fThreadProc(void){
  72. int RetVal;
  73. m_bThreadStatus = TH_WORKING;
  74. while(m_bThreadStatus < TH_ENDED){
  75. if( m_eConnectionStatus < CS_DISCONNECTED ){
  76. if( ( m_bBufferAltered == false ) && ( m_bThreadStatus < TH_ENDED ) ) {
  77. SLEEP(3);
  78. continue;
  79. };
  80. m_mBufferProtectorMutex.lock();
  81. while ((m_nParserDataCount > 0) && (m_bThreadStatus < TH_ENDED)){
  82. #ifdef DUBUG_COMMUNICATION
  83. LOG("Check Pack");
  84. #endif
  85. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  86. #ifdef DUBUG_COMMUNICATION
  87. LOG("Check Pack Done");
  88. #endif
  89. if( RetVal < 0 ){
  90. // Note: RetVal is NEGATIVE!!??
  91. m_nParserDataCount+=RetVal;
  92. memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount );
  93. } else if ( RetVal == 0 ) {
  94. protocolPackage* tVal = NULL;
  95. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  96. if (tVal != NULL) {
  97. #ifdef DUBUG_COMMUNICATION
  98. LOG("Package Handling");
  99. char tBuf[256];
  100. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  101. LOG(tBuf);
  102. #endif
  103. //Move unused data toward buffer's begin to parse at next parse attempt
  104. m_nParserDataCount -= tVal->m_nSize;
  105. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  106. // In Case implementation of packetHandler - handler must return !true!
  107. if( !packetHandler(tVal) ){
  108. m_vPackagePool.push_back( tVal );
  109. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  110. // Delete oldest package in queue
  111. m_vPackagePool.erase(m_vPackagePool.begin());
  112. dataEraser(tVal->m_pData);
  113. free(tVal);
  114. };
  115. } else {
  116. dataEraser(tVal->m_pData);
  117. free(tVal);
  118. };
  119. } else {
  120. // All data consumed in MAVLINK proto but not parsed into message
  121. m_nParserDataCount = 0;
  122. };
  123. } else {
  124. break;
  125. };
  126. };
  127. m_bBufferAltered = false;
  128. m_mBufferProtectorMutex.unlock();
  129. } else {
  130. // Handle Disconnect
  131. m_bThreadStatus = TH_ENDED;
  132. };
  133. };
  134. };
  135. bool protocolParser::getPackage( U8 **Package, size_t *Size ){
  136. if( this ) {
  137. m_mBufferProtectorMutex.lock();
  138. if( m_vPackagePool.size() ){
  139. protocolPackage *tVal;
  140. tVal = m_vPackagePool.front();
  141. *Package =( U8 * ) malloc( tVal->m_nSize );
  142. if( *Package != NULL && tVal != NULL ) {
  143. memcpy( *Package, tVal->m_pData, tVal->m_nSize );
  144. *Size = tVal->m_nSize;
  145. m_vPackagePool.erase( m_vPackagePool.begin() );
  146. free( tVal );
  147. m_mBufferProtectorMutex.unlock();
  148. return true;
  149. };
  150. };
  151. m_mBufferProtectorMutex.unlock();
  152. };
  153. return false;
  154. };
  155. #undef LOG
  156. #undef LOG_ERROR