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.

153 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. if( m_pParserBuffer != NULL ) {
  46. m_mBufferProtectorMutex.lock();
  47. memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
  48. m_nParserDataCount+=BufferSize;
  49. m_bBufferAltered = true;
  50. m_mBufferProtectorMutex.unlock();
  51. };
  52. return m_bBufferAltered;
  53. };
  54. void protocolParser::dataEraser( U8 *&tPtr ){
  55. if (tPtr == NULL) return;
  56. free(tPtr);
  57. tPtr = NULL;
  58. };
  59. void protocolParser::m_fThreadProc(void){
  60. int RetVal;
  61. m_bThreadStatus = TH_WORKING;
  62. while(m_bThreadStatus < TH_ENDED){
  63. if( m_eConnectionStatus < CS_DISCONNECTED ){
  64. if( ( m_bBufferAltered == false ) && ( m_bThreadStatus < TH_ENDED ) ) {
  65. SLEEP(3);
  66. continue;
  67. };
  68. m_mBufferProtectorMutex.lock();
  69. while ((m_nParserDataCount > 0) && (m_bThreadStatus < TH_ENDED)){
  70. #ifdef DUBUG_COMMUNICATION
  71. LOG("Check Pack");
  72. #endif
  73. RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
  74. #ifdef DUBUG_COMMUNICATION
  75. LOG("Check Pack Done");
  76. #endif
  77. if( RetVal < 0 ){
  78. // Note: RetVal is NEGATIVE!!??
  79. m_nParserDataCount+=RetVal;
  80. memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount );
  81. } else if ( RetVal == 0 ) {
  82. protocolPackage* tVal = NULL;
  83. tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
  84. if (tVal != NULL) {
  85. #ifdef DUBUG_COMMUNICATION
  86. LOG("Package Handling");
  87. char tBuf[256];
  88. sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
  89. LOG(tBuf);
  90. #endif
  91. //Move unused data toward buffer's begin to parse at next parse attempt
  92. m_nParserDataCount -= tVal->m_nSize;
  93. memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
  94. // In Case implementation of packetHandler - handler must return !true!
  95. if( !packetHandler(tVal) ){
  96. m_vPackagePool.push_back( tVal );
  97. if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
  98. // Delete oldest package in queue
  99. m_vPackagePool.erase(m_vPackagePool.begin());
  100. dataEraser(tVal->m_pData);
  101. free(tVal);
  102. };
  103. } else {
  104. dataEraser(tVal->m_pData);
  105. free(tVal);
  106. };
  107. } else {
  108. // All data consumed in MAVLINK proto but not parsed into message
  109. m_nParserDataCount = 0;
  110. };
  111. } else {
  112. break;
  113. };
  114. };
  115. m_bBufferAltered = false;
  116. m_mBufferProtectorMutex.unlock();
  117. } else {
  118. // Handle Disconnect
  119. m_bThreadStatus = TH_ENDED;
  120. };
  121. };
  122. };
  123. bool protocolParser::getPackage( U8 **Package, size_t *Size ){
  124. if( this ) {
  125. m_mBufferProtectorMutex.lock();
  126. if( m_vPackagePool.size() ){
  127. protocolPackage *tVal;
  128. tVal = m_vPackagePool.front();
  129. *Package =( U8 * ) malloc( tVal->m_nSize );
  130. if( *Package != NULL && tVal != NULL ) {
  131. memcpy( *Package, tVal->m_pData, tVal->m_nSize );
  132. *Size = tVal->m_nSize;
  133. m_vPackagePool.erase( m_vPackagePool.begin() );
  134. free( tVal );
  135. m_mBufferProtectorMutex.unlock();
  136. return true;
  137. };
  138. };
  139. m_mBufferProtectorMutex.unlock();
  140. };
  141. return false;
  142. };
  143. #undef LOG
  144. #undef LOG_ERROR