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.

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