AtrasrClient.cpp
Go to the documentation of this file.
00001 #include "AtrasrClient.h"
00002 //#include "wavefile.h"
00003 #include <QtEndian>
00004 #include <QDebug>
00005 
00006 static void _AtrasrSwapFrameData( unsigned char *pBuf, size_t len )
00007 {
00008     unsigned char tmp;
00009     for( ; len > 0; len -= 2 )
00010     {
00011         tmp = *pBuf;
00012         *pBuf = *(pBuf+1);
00013         *(pBuf+1) = tmp;
00014 
00015         pBuf += 2;
00016     }
00017 }
00018 
00019 static void _AtrasrInitSendBuf( ATRASR_CLIENT *client )
00020 {
00021     client->curIndexSendBuf = ATRASR_FRAME_HEADER_SIZE;
00022 }
00023 
00024 static void _AtrasrInitRecvBuf( ATRASR_CLIENT *client )
00025 {
00026     client->curIndexRecvBuf = 0;
00027 }
00028 
00029 int AtrasrConnect( ATRASR_CLIENT *client, const char *srvHost, int srvPort )
00030 {
00031     _AtrasrInitSendBuf( client );
00032     _AtrasrInitRecvBuf( client );
00033     client->sendFrameCount = 0;
00034     client->recvFrameCount = 0;
00035     client->logFilename = NULL;
00036         
00037     client->socket = new QTcpSocket();
00038     client->socket->connectToHost(srvHost, srvPort);
00039     if (client->socket->waitForConnected(5000)){
00040         return 0;
00041     }
00042     else{
00043         AtrasrClose(client);
00044     }
00045 
00046     qCritical() << QString("Could not connect to %1(%2)").arg(srvHost).arg(srvPort);
00047     return -1;
00048 }
00049 
00050 void AtrasrClose( ATRASR_CLIENT *client )
00051 {
00052     if ( client->socket != NULL )
00053     {
00054         client->socket->close();
00055         delete client->socket;
00056         client->socket = NULL;
00057     }
00058 }
00059 
00060 static int _AtrasrSendFrame( ATRASR_CLIENT *client, ATRASR_FRAME_TYPE type )
00061 {
00062     int nRet = -1;
00063     int nSent;
00064 
00065     if ( client->socket != NULL )
00066     {
00067         memset( client->sendBuf, 0, ATRASR_FRAME_SIZE );
00068         *((long *)&client->sendBuf) = type;
00069         
00070         nSent = client->socket->write((const char*)client->sendBuf, ATRASR_FRAME_SIZE);
00071         if( nSent < ATRASR_FRAME_SIZE )
00072         {
00073             qCritical() << "Could not send to ATRASR : frame type " << type;
00074         }
00075         else
00076         {
00077             nRet = 0;
00078             client->sendFrameCount++;
00079         }
00080     }
00081 
00082     return nRet;
00083 }
00084 
00085 int AtrasrSendTOF( ATRASR_CLIENT *client )
00086 {
00087     _AtrasrInitSendBuf( client );
00088 
00089     client->sendFrameCount = 0;
00090     client->recvFrameCount = 0;
00091 
00092     //fprintf( stderr, "ATRASR : send TOF\n" );
00093     return _AtrasrSendFrame( client, ATRASR_TOF );
00094 }
00095 
00096 int AtrasrSendEOF( ATRASR_CLIENT *client )
00097 {
00098     //fprintf( stderr, "ATRASR : send EOF\n" );
00099     return _AtrasrSendFrame( client, ATRASR_EOF );
00100 }
00101 
00102 static int _AtrasrSendDATA( ATRASR_CLIENT *client, const char *data, size_t size )
00103 {
00104     int nRet = -1;
00105     size_t sizeRemain;
00106     size_t sendLen;
00107     int nSent;
00108     unsigned char *pSendBuf = client->sendBuf;
00109 
00110     if ( client->socket != NULL )
00111     {
00112         for (;;)
00113         {
00114             sizeRemain = ATRASR_FRAME_SIZE - ( client->curIndexSendBuf % ATRASR_FRAME_SIZE );
00115             if ( size < sizeRemain )
00116             {
00117                 memcpy( client->sendBuf + client->curIndexSendBuf, data, size );
00118                 client->curIndexSendBuf += size;
00119                 break;
00120             }
00121             else
00122             {
00123                 *((long *)pSendBuf) = ATRASR_DATA;
00124                 memcpy( client->sendBuf + client->curIndexSendBuf, data, sizeRemain );
00125 
00126                 client->curIndexSendBuf += sizeRemain + ATRASR_FRAME_HEADER_SIZE;
00127                 data += sizeRemain;
00128                 size -= sizeRemain;
00129                 pSendBuf += ATRASR_FRAME_SIZE;
00130             }
00131         }
00132 
00133         sendLen = pSendBuf - client->sendBuf;
00134 
00135         if ( sendLen > 0 )
00136         {
00137             nSent = client->socket->write((const char*)client->sendBuf, sendLen);
00138             if ( nSent < sendLen )
00139             {
00140                 qCritical() << "Could not send to ATRASR : frame[" << client->sendFrameCount << "]";
00141             }
00142             else
00143             {
00144                 nRet = 0;
00145                 client->sendFrameCount += sendLen / ATRASR_FRAME_SIZE;
00146             }
00147         }
00148         else
00149         {
00150             nRet = 0;
00151         }
00152 
00153         // move memory to head.
00154         client->curIndexSendBuf = client->curIndexSendBuf % ATRASR_FRAME_SIZE;
00155         memcpy( client->sendBuf + ATRASR_FRAME_HEADER_SIZE, pSendBuf + ATRASR_FRAME_HEADER_SIZE,
00156             client->curIndexSendBuf - ATRASR_FRAME_HEADER_SIZE );
00157     }
00158 
00159     return nRet;
00160 }
00161 
00162 int AtrasrSendDATA( ATRASR_CLIENT *client, const char *data, size_t size )
00163 {
00164     int nRet = -1;
00165     int sizeMaxSend = ATRASR_FRAME_DATA_SIZE * ATRASR_SEND_BUF_FRAME_COUNT;
00166 
00167     for (;;) 
00168     {
00169         if ( size <= sizeMaxSend )
00170         {
00171             nRet = _AtrasrSendDATA( client, data, size );
00172             break;
00173         }
00174         else
00175         {
00176             nRet = _AtrasrSendDATA( client, data, sizeMaxSend );
00177             if( nRet != 0 )
00178             {
00179                 break;
00180             }
00181             data += sizeMaxSend;
00182             size -= sizeMaxSend;
00183         }
00184     }
00185 
00186     return nRet;
00187 }
00188 
00189 int AtrasrSendSTARTPU( ATRASR_CLIENT *client )
00190 {
00191     _AtrasrInitSendBuf( client );
00192 
00193     //fprintf( stderr, "ATRASR : send STARTPU\n" );
00194     return _AtrasrSendFrame( client, ATRASR_START );
00195 }
00196 
00197 int AtrasrSendENDPU( ATRASR_CLIENT *client )
00198 {
00199     //fprintf( stderr, "ATRASR : send ENDPU\n" );
00200     return _AtrasrSendFrame( client, ATRASR_END );
00201 }
00202 
00203 int AtrasrSendCANCELPU( ATRASR_CLIENT *client )
00204 {
00205     //fprintf( stderr, "ATRASR : send CANCEL\n" );
00206     return _AtrasrSendFrame( client, ATRASR_CANCEL );
00207 }
00208 
00209 int AtrasrReceiveFrame( ATRASR_CLIENT *client )
00210 {
00211     int frameCount = -1;
00212     int sizeRecv;
00213     size_t sizeRemain;
00214     int oldIndex;
00215 
00216     if ( client->socket != NULL )
00217     {
00218         // move memory to head.
00219         oldIndex = client->curIndexRecvBuf;
00220         client->curIndexRecvBuf = oldIndex % ATRASR_FRAME_SIZE;
00221         memcpy( client->recvBuf, client->recvBuf + oldIndex - client->curIndexRecvBuf, client->curIndexRecvBuf );
00222 
00223         sizeRemain = ATRASR_RECV_BUF_SIZE - client->curIndexRecvBuf;
00224         sizeRecv = client->socket->read( (char*)client->recvBuf + client->curIndexRecvBuf, sizeRemain );
00225         if ( sizeRecv > 0 )
00226         {
00227             client->curIndexRecvBuf += sizeRecv;
00228             frameCount = client->curIndexRecvBuf / ATRASR_FRAME_SIZE;
00229             
00230             client->recvFrameCount += frameCount;
00231         }
00232         else
00233         {
00234             //fprintf( stderr, "Receive frame error from ATRASR.\n" ); 
00235             frameCount = -1;
00236             AtrasrClose(client);
00237         }
00238     }
00239 
00240     return frameCount;
00241 }
00242 
00243 unsigned char *AtrasrParseFrame( ATRASR_CLIENT *client, int nIndex, long *type )
00244 {
00245     unsigned char *pRecvBuf = client->recvBuf + ATRASR_FRAME_SIZE * nIndex;
00246 
00247     // Big endian to little endian.
00248     *type = qFromBigEndian<qint32>( pRecvBuf );
00249     //*type = *((long *)pRecvBuf);
00250     _AtrasrSwapFrameData( pRecvBuf + ATRASR_FRAME_HEADER_SIZE, ATRASR_FRAME_DATA_SIZE );
00251 
00252     return pRecvBuf + ATRASR_FRAME_HEADER_SIZE;
00253 }


rospeex_audiomonitor
Author(s): Komei Sugiura
autogenerated on Thu Jun 6 2019 18:53:06