Code:
#include "DataServerPort.h"
#include "DataServerLog.h"
#include</usr/lib/qt-3.3/include/qstring.h>
//#include "DCSPacket.h"
extern QString packet;
extern DataServerLog d_LogObject;
extern DataServerLog CCUComLog;
extern DataServerPort *DSPComTh1;
extern DataServerPort *DSPComTh2;
extern bool g_bIsCCUCOMPortInitialized;
DataServerPort *p_DataportWrite;
int g_iLinkFailCounter = 0;
bool g_bLinkFailedTimerStartFlag = false;
// Queue
//extern CGCQueue DataPortEventQueue;
//extern CGCQueue DataPortCommandQueue;
//Threads
pthread_t DataReadThread;
pthread_t DataWriteThread;
//mutex
pthread_mutex_t g_csDataportMutex=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t g_csDataportEventQueueMutex=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t g_csDataportComandQueueMutex=PTHREAD_MUTEX_INITIALIZER;
DataServerPort:ataServerPort()
{
}
DataServerPort::~DataServerPort()
{
}
long DataServerPort::initializeDataServerPort(QString &sPortnumber)
{
//CCUComLog.writeDataLog("Dataserver Com port init function\n");
//QString sCom = "/dev/ttyS";
//sPortnumber = sPortnumber.stripWhiteSpace();
//sCom = sCom + sPortnumber;
initPortSection();
//m_hCreateFile = open(sCom,O_RDWR|O_NOCTTY|O_NDELAY);
m_hCreateFile = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(m_hCreateFile == -1)
{
CCUComLog.writeDataLog("Com port open failed\n");
return -1;exit(0);
}
CCUComLog.writeDataLog("Dataserver Comport before thread\n");
// pthread_create(&DataReadThread,NULL,DataServerThread_readfun,NULL);
pthread_create(&DataReadThread,NULL,DataServerThread_readfun,DSPComTh1);
CCUComLog.writeDataLog("DataServer Comport after the thread\n");
return 1;
}
void DataServerPort::initPortSection()
{
tcgetattr( m_hCreateFile , &s_mTermios);
bzero(&s_mTermios,sizeof(s_mTermios));
cfsetispeed(&s_mTermios,B9600);
cfsetospeed(&s_mTermios,B9600);
s_mTermios.c_cc[VMIN] = 1;
s_mTermios.c_cc[VTIME]=0;
s_mTermios.c_cc[VERASE]=0;
s_mTermios.c_cc[VEOL2]=0;
s_mTermios.c_cc[VEOL]=0;
s_mTermios.c_cc[VKILL]=0;
s_mTermios.c_cc[VSTART]=0;
s_mTermios.c_cc[VSTOP]=0;
s_mTermios.c_cc[VREPRINT]=0;
s_mTermios.c_iflag &= ~BRKINT;
s_mTermios.c_iflag = ISIG | IUCLC;
s_mTermios.c_oflag &= (OLCUC);
s_mTermios.c_cflag = B9600 | CS8 | CREAD | CLOCAL| HUPCL;
tcflush(m_hCreateFile,TCIFLUSH);
tcsetattr(m_hCreateFile,TCSANOW,&s_mTermios);
printf("exiting function initportsection\n");
}
void* DataServerThread_readfun(void* arg)
{ printf("I am in thread 1\n");
int iResultselect , iCheckallfd , iRbyte;
bool b_newPacketStarted=false;
unsigned char cTempbuf[2];
QString sReceivedData = "" ;
QString SendBuff="";
CCUComLog.writeDataLog("DataCom threadfun \n");
DataServerPort* p_Dataport = (DataServerPort*)arg;
//DataServerEvent* TheCEvent = NULL;
bool bFirsttime = true;
//bool b_newPacketStarted=false;
p_DataportWrite = new DataServerPort();
tcsetattr(p_Dataport->m_hCreateFile,TCSANOW,&(p_Dataport->s_mTermios));
p_DataportWrite->s_mTermios = p_Dataport->s_mTermios;
p_DataportWrite->m_hCreateFile = p_Dataport->m_hCreateFile;
CCUComLog.writeDataLog("DataServer Comport Read Thread: p_Dataport->m_hCreateFile",p_Dataport->m_hCreateFile);
fd_set testfds,readfds;
struct timeval timeout;
FD_ZERO(&readfds);
FD_SET(p_Dataport->m_hCreateFile,&readfds);///add p_Dataport into readfds...
p_Dataport->initPortSection();
//tcsetattr(p_Dataport->m_hCreateFile,TCSANOW,&(p_Dataport->s_mTermios));
pthread_create(&DataWriteThread,NULL,DataServerThread_writefun,p_DataportWrite);
try
{
do
{
//printf("you r with try and catch\n");
usleep(20*1000);
testfds = readfds;
timeout.tv_sec=0;
timeout.tv_usec=10;
iResultselect = select(FD_SETSIZE,&testfds,(fd_set *)0,(fd_set *)0,&timeout);
if(iResultselect > 0)
{ printf("hello");
for ( iCheckallfd = 0 ; iCheckallfd < FD_SETSIZE ; iCheckallfd ++ )
{
if ( FD_ISSET ( iCheckallfd , &testfds ) )
{
if ( iCheckallfd == p_Dataport->m_hCreateFile )
{
pthread_mutex_lock(&g_csDataportMutex);
iRbyte= read(p_Dataport->m_hCreateFile,cTempbuf,1);
pthread_mutex_unlock(&g_csDataportMutex);
if(b_newPacketStarted==false)
{
if(iRbyte > 0)
{
if(cTempbuf[0]=='{')
{
b_newPacketStarted=true;
sReceivedData ="";
sReceivedData.sprintf("%c",cTempbuf[0]);
}
}
} //if(b_newPacketStarted==false)
else if(b_newPacketStarted==true)
{
if(iRbyte==1)
{
if(cTempbuf[0]=='{')
{
b_newPacketStarted=true;
sReceivedData ="";
sReceivedData.sprintf("%c",cTempbuf[0]);
}
else if(cTempbuf[0]=='}')
{
sReceivedData.sprintf("%s%c",sReceivedData.ascii(),cTempbuf[0]);
//cout<<"\n\n The Received with bracket data is..."<<sReceivedData;
if(qstrlen(sReceivedData) == 11)
{
cout<<"\n\n The Received with full check data is....."<<sReceivedData;
CCUComLog.writeDataLog("\nData Received at Conc. -");
CCUComLog.writeDataLog(sReceivedData.ascii());
CCUComLog.writeTimeStamp();
//TheCEvent = new DataServerEvent();
//p_Dataport->ExtSmsPkt(TheCEvent,sReceivedData);
p_Dataport->ExtSmsPkt(sReceivedData);
pthread_mutex_lock(&g_csDataportEventQueueMutex);
//DataPortEventQueue.AddHead(TheCEvent);
pthread_mutex_unlock(&g_csDataportEventQueueMutex);
sReceivedData = "";
}
b_newPacketStarted=false ;
}
else // if data is not '{' or not '}'
{
sReceivedData.sprintf("%s%c",sReceivedData.ascii(),cTempbuf[0]);
}
} ////////if(iRbyte==1)
} //////////////else if(b_newPacketStarted==true)
} // //if fd==mcreatefile
} // if FD IsSet
} //for
} //if(i_Resultselect > 0)
else if(iResultselect<0)
{
// Error at comm Port
}
}while(!0);
}// try
catch(...)
{ }
if(p_Dataport->m_hCreateFile)
close(p_Dataport->m_hCreateFile);
d_LogObject.writeDataLog("\n\n CCU Port Thread STOPPED...\n\n");
return NULL;
}
void *DataServerThread_writefun(void *arg)
{
DataServerPort *p_Write = (DataServerPort *)arg;
// DataServerCommand* TheCCommand = NULL;
bool bFirsttime=true;
QString SendBuff="";
//int iCounter=0;
int iStatus;
printf("you r in write thread function\n");
CCUComLog.writeDataLog("Dataserver write thread : ",p_Write->m_hCreateFile);
//CCUPortLog.writeDebugLog("\n CCU Write Thread : p_CCUPort->m_hCreateFile ",p_CCUPort->m_hCreateFile);
do{
usleep(20*1000);
if(bFirsttime == true)
{
iStatus = ioctl(p_Write->m_hCreateFile,TCSETS,p_Write->s_mTermios);
bFirsttime = false;
}
//if(!(DataPortCommandQueue.IsEmpty()))
//{
// pthread_mutex_lock(&g_csDataportComandQueueMutex);
// TheCCommand = (DataPortCommand *)DataPortCommandQueue.RemoveTail();
// pthread_mutex_unlock(&g_csDataportComandQueueMutex);
//p_Write->GenSmsPkt(TheCCommand,SendBuff);
p_Write->GenSmsPkt(11);
//iCounter = 0;
//cout<<"\n Sending Packet to DataServer"<<SendBuff;
CCUComLog.writeDataLog("\n Sending Packet to DataServer---");
CCUComLog.writeDataLog(SendBuff.ascii());
CCUComLog.writeTimeStamp();
//while(iCounter < 3)
// {
pthread_mutex_lock(&g_csDataportMutex);
write(p_Write->m_hCreateFile,SendBuff,qstrlen(SendBuff));
pthread_mutex_unlock(&g_csDataportMutex);
//usleep(300*1000);
// iCounter++;
// }
//DataServerLog.writeDebugLog("\n 3 times write completed");
//}
}while(!0);
if(p_Write->m_hCreateFile)
close(p_Write->m_hCreateFile);
d_LogObject.writeDataLog("\n\n Dataport Write Thread STOPPED...\n\n");
return NULL;
}