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
153 lines
4.5 KiB
#include "protocol.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "Proto", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "Proto", this, _LOG_ERROR, X )
|
|
|
|
protocolParser::protocolParser(config_setting_t *Settings) :basicThread(),m_mBufferProtectorMutex(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_pParserBuffer = (U8*) malloc(PROTOCOL_PARSER_BUFFER_SIZE);
|
|
m_nParserDataCount = 0;
|
|
m_bStatus = 0;
|
|
m_pCustomHandler = NULL;
|
|
m_bBufferAltered = false;
|
|
m_bStatus = 1;
|
|
m_eConnectionStatus = CS_UNKNOWN;
|
|
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, (CSTR)PROTO_TAG_GROUP);
|
|
};
|
|
|
|
protocolParser::~protocolParser(){
|
|
# ifdef DEBUG
|
|
LOG("Destructor Begin");
|
|
# endif
|
|
m_mBufferProtectorMutex.unlock();
|
|
if (m_bThreadStatus < TH_ENDED ) {
|
|
waitThreadEnd( true );
|
|
};
|
|
if( m_pParserBuffer != NULL ) {
|
|
free( m_pParserBuffer );
|
|
};
|
|
# ifdef DEBUG
|
|
LOG("Destructor End");
|
|
# endif
|
|
};
|
|
|
|
bool protocolParser::create(void){
|
|
if (m_pSettings == NULL)return false;
|
|
if (m_pParserBuffer != NULL) {
|
|
return basicThread::init();
|
|
} else return false;
|
|
};
|
|
|
|
void protocolParser::setConnectionStatus( CONNECTION_STATUS cStatus ){
|
|
if (cStatus == CS_CONNECTED)LOG("Connection Status Changed - Connected!");
|
|
if (cStatus == CS_DISCONNECTED)LOG("Connection Status Changed - Disconnected!");
|
|
m_eConnectionStatus = cStatus;
|
|
};
|
|
CONNECTION_STATUS protocolParser::getConnectionStatus( void ){
|
|
return m_eConnectionStatus;
|
|
};
|
|
bool protocolParser::parse( U8 *Buffer, size_t BufferSize ){
|
|
if( m_pParserBuffer != NULL ) {
|
|
m_mBufferProtectorMutex.lock();
|
|
memcpy( m_pParserBuffer+m_nParserDataCount, Buffer, BufferSize );
|
|
m_nParserDataCount+=BufferSize;
|
|
m_bBufferAltered = true;
|
|
m_mBufferProtectorMutex.unlock();
|
|
};
|
|
return m_bBufferAltered;
|
|
};
|
|
|
|
void protocolParser::dataEraser( U8 *&tPtr ){
|
|
if (tPtr == NULL) return;
|
|
free(tPtr);
|
|
tPtr = NULL;
|
|
};
|
|
|
|
void protocolParser::m_fThreadProc(void){
|
|
int RetVal;
|
|
m_bThreadStatus = TH_WORKING;
|
|
while(m_bThreadStatus < TH_ENDED){
|
|
if( m_eConnectionStatus < CS_DISCONNECTED ){
|
|
if( ( m_bBufferAltered == false ) && ( m_bThreadStatus < TH_ENDED ) ) {
|
|
SLEEP(3);
|
|
continue;
|
|
};
|
|
m_mBufferProtectorMutex.lock();
|
|
while ((m_nParserDataCount > 0) && (m_bThreadStatus < TH_ENDED)){
|
|
#ifdef DUBUG_COMMUNICATION
|
|
LOG("Check Pack");
|
|
#endif
|
|
RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
|
|
#ifdef DUBUG_COMMUNICATION
|
|
LOG("Check Pack Done");
|
|
#endif
|
|
if( RetVal < 0 ){
|
|
// Note: RetVal is NEGATIVE!!??
|
|
m_nParserDataCount+=RetVal;
|
|
memcpy( m_pParserBuffer, m_pParserBuffer-RetVal, m_nParserDataCount );
|
|
} else if ( RetVal == 0 ) {
|
|
protocolPackage* tVal = NULL;
|
|
tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
|
|
if (tVal != NULL) {
|
|
#ifdef DUBUG_COMMUNICATION
|
|
LOG("Package Handling");
|
|
char tBuf[256];
|
|
sprintf(tBuf, "Pack Size: %d from %d bytes", tVal->m_nSize, m_nParserDataCount);
|
|
LOG(tBuf);
|
|
#endif
|
|
//Move unused data toward buffer's begin to parse at next parse attempt
|
|
m_nParserDataCount -= tVal->m_nSize;
|
|
memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
|
|
// In Case implementation of packetHandler - handler must return !true!
|
|
if( !packetHandler(tVal) ){
|
|
m_vPackagePool.push_back( tVal );
|
|
if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES) {
|
|
// Delete oldest package in queue
|
|
m_vPackagePool.erase(m_vPackagePool.begin());
|
|
dataEraser(tVal->m_pData);
|
|
free(tVal);
|
|
};
|
|
} else {
|
|
dataEraser(tVal->m_pData);
|
|
free(tVal);
|
|
};
|
|
} else {
|
|
// All data consumed in MAVLINK proto but not parsed into message
|
|
m_nParserDataCount = 0;
|
|
};
|
|
} else {
|
|
break;
|
|
};
|
|
};
|
|
m_bBufferAltered = false;
|
|
m_mBufferProtectorMutex.unlock();
|
|
} else {
|
|
// Handle Disconnect
|
|
m_bThreadStatus = TH_ENDED;
|
|
};
|
|
};
|
|
};
|
|
|
|
bool protocolParser::getPackage( U8 **Package, size_t *Size ){
|
|
if( this ) {
|
|
m_mBufferProtectorMutex.lock();
|
|
if( m_vPackagePool.size() ){
|
|
protocolPackage *tVal;
|
|
tVal = m_vPackagePool.front();
|
|
*Package =( U8 * ) malloc( tVal->m_nSize );
|
|
if( *Package != NULL && tVal != NULL ) {
|
|
memcpy( *Package, tVal->m_pData, tVal->m_nSize );
|
|
*Size = tVal->m_nSize;
|
|
m_vPackagePool.erase( m_vPackagePool.begin() );
|
|
free( tVal );
|
|
m_mBufferProtectorMutex.unlock();
|
|
return true;
|
|
};
|
|
};
|
|
m_mBufferProtectorMutex.unlock();
|
|
};
|
|
return false;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|