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.
134 lines
4.2 KiB
134 lines
4.2 KiB
#include "transportSerial.h"
|
|
#include "../../data/protocol/protocol.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "TSER", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "TSER", this, _LOG_ERROR, X )
|
|
|
|
transportSerial::transportSerial() :basicTransport(), basicDualThread(){
|
|
m_pLog = basicLogCollection::getInstance();
|
|
m_nInputBufferSize = -1;
|
|
m_nOutputBufferSize = -1;
|
|
m_pInputBuffer = ( U8 * ) malloc ( MAXIMAL_SERIAL_PACKET_LENGTH );
|
|
if( m_pInputBuffer != NULL ) {
|
|
m_nInputBufferSize = 0;
|
|
m_pOutputBuffer = ( U8 * ) malloc ( MAXIMAL_SERIAL_PACKET_LENGTH );
|
|
if( m_pOutputBuffer != NULL ) {
|
|
m_nOutputBufferSize = 0;
|
|
//For Select IO Mode
|
|
pipe( m_iControlPipeFD );
|
|
FD_ZERO( &m_sSenderSet );
|
|
FD_ZERO( &m_sReaderSet );
|
|
|
|
FD_SET( m_iControlPipeFD[0], &m_sSenderSet );
|
|
m_nSenderSetMax = m_iControlPipeFD[0];
|
|
FD_SET( m_iControlPipeFD[1], &m_sReaderSet );
|
|
m_nReaderSetMax = m_iControlPipeFD[1];
|
|
};
|
|
};
|
|
};
|
|
transportSerial::~transportSerial(){
|
|
_close();
|
|
if( m_pInputBuffer != NULL ) free( m_pInputBuffer );
|
|
if( m_pOutputBuffer != NULL ) free( m_pOutputBuffer );
|
|
};
|
|
|
|
bool transportSerial::create( void ){
|
|
m_bInitComplete = init();
|
|
if( m_bInitComplete ) setConnectionStatus( CS_CONNECTED );
|
|
return m_bInitComplete;
|
|
};
|
|
|
|
//void *transportSerial::senderProc( void *param ){
|
|
void transportSerial::m_fThreadAProc( void ){
|
|
m_bThreadAStatus = TH_WORKING;
|
|
uint32_t nSleepTime = 1;
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Output Thread Started ");
|
|
#endif
|
|
while( m_bThreadAStatus < TH_ENDED ) {
|
|
m_mBufferProtectorMutex.lock();
|
|
if( m_nOutputBufferSize > 0 ) {
|
|
int index;
|
|
int RetVal;
|
|
index = 0;
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Buffer ready to Send");
|
|
#endif
|
|
while( (index < m_nOutputBufferSize) && (m_bThreadAStatus < TH_ENDED) ){
|
|
if ( m_mIsBusy.try_lock() ) {
|
|
RetVal = sendBlock(m_pOutputBuffer + index, m_nOutputBufferSize - index);
|
|
m_mIsBusy.unlock();
|
|
if( RetVal > 0 ) index += RetVal;
|
|
if (RetVal < 0) {
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
m_bThreadAStatus = TH_ENDED;
|
|
};
|
|
} else SLEEP( 3 );
|
|
};
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Buffer is Sent");
|
|
#endif
|
|
m_nOutputBufferSize = 0;
|
|
nSleepTime = 1;
|
|
} else {
|
|
nSleepTime = 10;
|
|
};
|
|
m_mBufferProtectorMutex.unlock();
|
|
SLEEP( nSleepTime );
|
|
};
|
|
};
|
|
|
|
//void *transportSerial::receiverProc( void *param ){
|
|
|
|
void transportSerial::m_fThreadBProc(void){
|
|
m_bThreadBStatus = TH_WORKING;
|
|
uint32_t nSleepTime = 1;
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Input Thread Started");
|
|
#endif
|
|
while (m_bThreadBStatus < TH_ENDED) {
|
|
if ( m_mIsBusy.try_lock() ) {
|
|
int retVal = recptBlock( m_pInputBuffer, &m_nInputBufferSize );
|
|
m_mIsBusy.unlock();
|
|
if (retVal < 0) {
|
|
setConnectionStatus(CS_DISCONNECTED);
|
|
m_bThreadBStatus = TH_ENDED;
|
|
continue;
|
|
} else {
|
|
if (m_nInputBufferSize > 0 && m_bThreadBStatus == TH_WORKING) {
|
|
m_mQueueProtectorMutex.lock();
|
|
for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) {
|
|
if (m_bThreadBStatus == TH_WORKING) {
|
|
(*index)->parse(m_pInputBuffer, m_nInputBufferSize);
|
|
}
|
|
};
|
|
m_mQueueProtectorMutex.unlock();
|
|
nSleepTime = 1;
|
|
} else {
|
|
nSleepTime = 10;
|
|
if( m_bThreadBStatus != TH_WORKING ) continue;
|
|
};
|
|
};
|
|
SLEEP( nSleepTime );
|
|
} else SLEEP( 3 );
|
|
};
|
|
};
|
|
|
|
bool transportSerial::sendData( unsigned char *Data, int DataSize ){
|
|
m_mBufferProtectorMutex.lock();
|
|
if( m_nOutputBufferSize+DataSize > MAXIMAL_SERIAL_PACKET_LENGTH ) {
|
|
m_mBufferProtectorMutex.unlock();
|
|
LOG_ERROR("Data Too Long");
|
|
return false;
|
|
};
|
|
#ifdef DEBUG_COMMUNICATION
|
|
LOG("Output Data Set");
|
|
#endif
|
|
memcpy( m_pOutputBuffer+m_nOutputBufferSize, Data, DataSize );
|
|
m_nOutputBufferSize += DataSize;
|
|
m_mBufferProtectorMutex.unlock();
|
|
return true;
|
|
};
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|