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.

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