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.

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