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.
457 lines
11 KiB
457 lines
11 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)
|
|
// Thread Logging
|
|
#define SLOG(X) mThis->m_pLog->Log("ProtoS", mThis, X)
|
|
#define SLOG_ERROR(X) mThis->m_pLog->Log("ProtoS", mThis, _LOG_ERROR, X)
|
|
|
|
// #define DUBUG_EXTRA
|
|
// #define DUBUG_COMMUNICATION
|
|
|
|
protocolParser::protocolParser() :
|
|
m_mBufferProtectorMutex()
|
|
{
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_eConnectionStatus = CS_UNKNOWN;
|
|
m_oDataTransport = NULL;
|
|
m_pParserBuffer = (U8 *) malloc(PROTOCOL_PARSER_BUFFER_SIZE);
|
|
if (m_pParserBuffer)
|
|
{
|
|
memset(m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Cannot allocate Parser Buffer!");
|
|
}
|
|
m_nParserDataCount = 0;
|
|
m_bStatus = 0;
|
|
m_pCustomHandler = NULL;
|
|
m_bBufferAltered = false;
|
|
m_bStatus = 1;
|
|
#ifdef PROTOCOL_PARSER_IN_THREAD
|
|
m_bParserThreadStatus = TH_PRE_INIT;
|
|
#endif
|
|
}
|
|
|
|
protocolParser::~protocolParser()
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Destructor Begin");
|
|
#endif
|
|
m_mBufferProtectorMutex.unlock();
|
|
/*
|
|
#ifdef PROTOCOL_PARSER_IN_THREAD
|
|
if (m_bParserThreadStatus > TH_ARMED ) {
|
|
waitParserThreadEnd( true );
|
|
};
|
|
if (m_bParserThreadStatus < TH_ENDED) {
|
|
waitParserThreadEnd(true);
|
|
};
|
|
#endif
|
|
*/
|
|
// Release packages that were never fetched from the pool
|
|
for (packagePoll::iterator idx = m_vPackagePool.begin(); idx != m_vPackagePool.end(); idx++)
|
|
{
|
|
if (*idx)
|
|
{
|
|
dataEraser((*idx)->m_pData);
|
|
free(*idx);
|
|
};
|
|
};
|
|
m_vPackagePool.clear();
|
|
if (m_pParserBuffer != NULL)
|
|
{
|
|
free(m_pParserBuffer);
|
|
};
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Destructor End");
|
|
#endif
|
|
}
|
|
|
|
bool protocolParser::create(void)
|
|
{
|
|
if (m_pParserBuffer != NULL)
|
|
{
|
|
#ifdef PROTOCOL_PARSER_IN_THREAD
|
|
if (m_bParserThreadStatus == TH_PRE_INIT)
|
|
{
|
|
m_bParserThreadStatus = TH_ARMED;
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
int RetVal = pthread_create(&m_sParserThreadHandler, NULL, m_fParserThreadEntryPoint, (void *) this);
|
|
if (RetVal == -1)
|
|
{
|
|
LOG_ERROR("Thread Creation Failed.");
|
|
return false;
|
|
};
|
|
#else
|
|
m_hParserThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fParserThreadEntryPoint, (LPVOID) this, 0, &m_dwThreadID);
|
|
// TODO: Add thread creation check
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("Thread object not ready to be created.");
|
|
return false;
|
|
};
|
|
#endif
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void protocolParser::setConnectionStatus(CONNECTION_STATUS cStatus)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
if (cStatus == CS_CONNECTED)
|
|
{
|
|
LOG("Connection Status Changed - Connected!");
|
|
}
|
|
if (cStatus == CS_DISCONNECTED)
|
|
{
|
|
LOG("Connection Status Changed - Disconnected!");
|
|
}
|
|
#endif
|
|
m_eConnectionStatus = cStatus;
|
|
}
|
|
|
|
CONNECTION_STATUS protocolParser::getConnectionStatus(void)
|
|
{
|
|
return m_eConnectionStatus;
|
|
}
|
|
|
|
bool protocolParser::setTransport(basicTransport * Transport, bool CAE)
|
|
{
|
|
#ifdef DEBUG_EXTRA
|
|
LOG("Set Transport Req");
|
|
#endif
|
|
if (CAE)
|
|
{
|
|
if (m_oDataTransport != NULL)
|
|
{
|
|
LOG_ERROR("Transport Already Assigned!");
|
|
return false;
|
|
};
|
|
}
|
|
else
|
|
{ // Danger -> If transport Object is deleted the pointer is wrong!
|
|
if (m_oDataTransport != NULL)
|
|
{
|
|
m_oDataTransport->removeRecipient(this);
|
|
};
|
|
};
|
|
m_oDataTransport = Transport;
|
|
if (Transport != NULL)
|
|
{
|
|
setConnectionStatus(CS_CONNECTED);
|
|
return m_oDataTransport->addRecipient(this);
|
|
}
|
|
else
|
|
{
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
basicTransport * protocolParser::getCurrentTransport(void)
|
|
{
|
|
LOG("Get Transport Req");
|
|
if (m_oDataTransport == NULL)
|
|
{
|
|
LOG("Transport is NULL");
|
|
}
|
|
return m_oDataTransport;
|
|
}
|
|
|
|
//#define DEBUG_COMMUNICATION
|
|
|
|
bool protocolParser::parse(U8 * Buffer, size_t BufferSize)
|
|
{
|
|
bool rv = false;
|
|
if (m_pParserBuffer != NULL)
|
|
{
|
|
#ifdef DEBUG_COMMUNICATION
|
|
char ttZZ[1024];
|
|
sprintf(ttZZ, "New %d bytes to Parse (allredy have in buffer: %d)", BufferSize, m_nParserDataCount);
|
|
LOG(ttZZ);
|
|
for (uint8_t i = 0; i < BufferSize; i++) sprintf(ttZZ + (i * 2), "%02X", Buffer[i]);
|
|
LOG(ttZZ);
|
|
#endif
|
|
m_mBufferProtectorMutex.lock();
|
|
if ((m_nParserDataCount + BufferSize) < PROTOCOL_PARSER_BUFFER_SIZE)
|
|
{
|
|
// if( BufferSize ){
|
|
memcpy(m_pParserBuffer + m_nParserDataCount, Buffer, BufferSize);
|
|
m_nParserDataCount += BufferSize;
|
|
#ifdef DEBUG_COMMUNICATION
|
|
for (uint8_t i = 0; i < m_nParserDataCount; i++) sprintf(ttZZ + (i * 2), "%02X", m_pParserBuffer[i]);
|
|
LOG(ttZZ);
|
|
#endif
|
|
m_bBufferAltered = true;
|
|
//};
|
|
#ifndef PROTOCOL_PARSER_IN_THREAD
|
|
while (m_nParserDataCount > 0)
|
|
{
|
|
int RetVal;
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Check Pack");
|
|
#endif
|
|
RetVal = packetChecker( m_pParserBuffer, m_nParserDataCount );
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Check Pack Done");
|
|
#endif
|
|
if (RetVal < 0){
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Clear 1b");
|
|
sprintf( ttZZ, "%d bytes to Eat", m_nParserDataCount );
|
|
LOG(ttZZ);
|
|
#endif
|
|
// Note: RetVal is NEGATIVE!!??
|
|
m_nParserDataCount += RetVal;
|
|
memmove(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
|
|
} else if (RetVal == 0) {
|
|
protocolPackage * tVal = NULL;
|
|
tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
|
|
if (tVal != NULL){
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Package Accepted");
|
|
char tBuf[256];
|
|
sprintf(tBuf, "Pack Size: %d from %d bytes TOT %lld", tVal->m_nSize, m_nParserDataCount, BufferSize);
|
|
LOG(tBuf);
|
|
#endif
|
|
// Move unused data toward buffer's begin to parse at next parse attempt
|
|
m_nParserDataCount -= tVal->m_nSize;
|
|
if (m_nParserDataCount > 0){
|
|
memmove(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
|
|
} else {
|
|
// Clean Buff;
|
|
memset(m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
|
|
m_nParserDataCount = 0;
|
|
};
|
|
// In Case implementation of packetHandler - handler must return !true!
|
|
if (! packetHandler(tVal)) {
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Handler Not Consumed Data!");
|
|
#endif
|
|
m_vPackagePool.push_back(tVal);
|
|
if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
|
|
{
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG_ERROR("Pack Pool Overflow!");
|
|
#endif
|
|
// Delete oldest package in queue (free the removed one, not the newly pooled one)
|
|
protocolPackage * tOld = m_vPackagePool.front();
|
|
m_vPackagePool.erase(m_vPackagePool.begin());
|
|
dataEraser(tOld->m_pData);
|
|
free(tOld);
|
|
};
|
|
} else {
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Handler Consumed Data!");
|
|
#endif
|
|
dataEraser(tVal->m_pData);
|
|
free(tVal);
|
|
};
|
|
rv = true; // Package Extracted From Data
|
|
} else {
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG_ERROR("Packet Not Extracted!");
|
|
#endif
|
|
// All data consumed in MAVLINK proto but not parsed into message
|
|
m_nParserDataCount = 0;
|
|
};
|
|
} else {
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Harvest More!");
|
|
#endif
|
|
break;
|
|
};
|
|
};
|
|
m_bBufferAltered = false;
|
|
// m_mBufferProtectorMutex.unlock();
|
|
#endif
|
|
};
|
|
m_mBufferProtectorMutex.unlock();
|
|
};
|
|
return rv;
|
|
}
|
|
|
|
//#undef DEBUG_COMMUNICATION
|
|
|
|
void protocolParser::dataEraser(U8 *& tPtr)
|
|
{
|
|
if (tPtr == NULL) return;
|
|
free(tPtr);
|
|
tPtr = NULL;
|
|
}
|
|
|
|
#ifdef PROTOCOL_PARSER_IN_THREAD
|
|
void protocolParser::m_fParserThreadProc(void)
|
|
{
|
|
int RetVal;
|
|
m_bParserThreadStatus = TH_WORKING;
|
|
while (m_bParserThreadStatus < TH_ENDED)
|
|
{
|
|
if (m_eConnectionStatus < CS_DISCONNECTED)
|
|
{
|
|
if ((m_bBufferAltered == false) && (m_bParserThreadStatus < TH_ENDED))
|
|
{
|
|
SLEEP(3);
|
|
continue;
|
|
};
|
|
m_mBufferProtectorMutex.lock();
|
|
while ((m_nParserDataCount > 0) && (m_bParserThreadStatus < TH_ENDED))
|
|
{
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Check Pack");
|
|
#endif
|
|
RetVal = packetChecker(m_pParserBuffer, m_nParserDataCount);
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Check Pack Done");
|
|
#endif
|
|
if (RetVal < 0)
|
|
{
|
|
// Note: RetVal is NEGATIVE!!??
|
|
m_nParserDataCount += RetVal;
|
|
memmove(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
|
|
}
|
|
else if (RetVal == 0)
|
|
{
|
|
protocolPackage * tVal = NULL;
|
|
tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
|
|
if (tVal != NULL)
|
|
{
|
|
#ifdef DEBUG_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;
|
|
memmove(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 (free the removed one, not the newly pooled one)
|
|
protocolPackage * tOld = m_vPackagePool.front();
|
|
m_vPackagePool.erase(m_vPackagePool.begin());
|
|
dataEraser(tOld->m_pData);
|
|
free(tOld);
|
|
};
|
|
}
|
|
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_bParserThreadStatus = TH_ENDED;
|
|
};
|
|
};
|
|
}
|
|
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
void * protocolParser::m_fParserThreadEntryPoint(void * ptr)
|
|
{
|
|
#else
|
|
DWORD WINAPI protocolParser::m_fParserThreadEntryPoint(LPVOID ptr)
|
|
{
|
|
#endif
|
|
protocolParser * mThis;
|
|
mThis = (protocolParser *) ptr;
|
|
if (mThis->m_bParserThreadStatus == TH_ARMED)
|
|
{
|
|
mThis->m_fParserThreadProc();
|
|
mThis->m_bParserThreadStatus = TH_EXIT;
|
|
}
|
|
else
|
|
{
|
|
SLOG_ERROR("Thread is not ARMED!");
|
|
};
|
|
#ifdef __linux__
|
|
pthread_exit(NULL);
|
|
return NULL;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
void protocolParser::waitParserThreadEnd(bool forced)
|
|
{
|
|
|
|
if (m_bParserThreadStatus > TH_WORKING || m_bParserThreadStatus < TH_ARMED)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (forced)
|
|
{
|
|
m_bParserThreadStatus = TH_ENDED;
|
|
}
|
|
while (m_bParserThreadStatus != TH_EXIT)
|
|
{
|
|
SLEEP(DEFAULT_THREAD_SLEEP_TIME);
|
|
};
|
|
#if defined(__linux__) || defined(WIN_PTHREAD)
|
|
pthread_join(m_sParserThreadHandler, NULL);
|
|
#else
|
|
WaitForSingleObject(m_hParserThreadHandle, 2000);
|
|
CloseHandle(m_hParserThreadHandle);
|
|
#endif
|
|
}
|
|
|
|
#endif // #ifndef PROTOCOL_PARSER_IN_THREAD
|
|
bool protocolParser::getPackage(U8 ** Package, size_t * Size)
|
|
{
|
|
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());
|
|
dataEraser(tVal->m_pData);
|
|
free(tVal);
|
|
m_mBufferProtectorMutex.unlock();
|
|
return true;
|
|
};
|
|
};
|
|
m_mBufferProtectorMutex.unlock();
|
|
return false;
|
|
}
|
|
|
|
// #undef DUBUG_EXTRA
|
|
// #undef DUBUG_COMMUNICATION
|
|
|
|
#undef SLOG
|
|
#undef SLOG_ERROR
|
|
#undef LOG
|
|
#undef LOG_ERROR
|