2006-02-12 15:26:46 +01:00
|
|
|
/******************************************************************************\
|
|
|
|
* Copyright (c) 2004-2006
|
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer
|
|
|
|
*
|
2006-02-18 23:05:46 +01:00
|
|
|
|
|
|
|
Protocol message definition
|
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
|
|
|
|
MAIN FRAME
|
|
|
|
----------
|
|
|
|
|
|
|
|
+------------+------------+------------------+--------------+-------------+
|
|
|
|
| 2 bytes ID | 1 byte cnt | 2 bytes length n | n bytes data | 2 bytes CRC |
|
|
|
|
+------------+------------+------------------+--------------+-------------+
|
2006-02-18 23:05:46 +01:00
|
|
|
|
|
|
|
- message ID defined by the defines PROTMESSID_x
|
2006-02-19 09:14:21 +01:00
|
|
|
- cnt: counter which is increment for each message and wraps around at 255
|
2006-02-18 23:05:46 +01:00
|
|
|
- length n in bytes of the data
|
|
|
|
- actual data, dependent on message type
|
|
|
|
- 16 bits CRC, calculating over the entire message, is transmitted inverted
|
|
|
|
Generator polynom: G_16(x) = x^16 + x^12 + x^5 + 1, initial state: all ones
|
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
|
|
|
|
MESSAGES
|
|
|
|
--------
|
|
|
|
|
|
|
|
- Jitter buffer size: PROTMESSID_JITT_BUF_SIZE
|
|
|
|
|
|
|
|
+--------------------------+
|
|
|
|
| 2 bytes number of blocks |
|
|
|
|
+--------------------------+
|
|
|
|
|
2006-02-27 20:45:27 +01:00
|
|
|
This message requires acknowledgement
|
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-12 15:26:46 +01:00
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#include "protocol.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Implementation *************************************************************/
|
2006-02-26 11:50:47 +01:00
|
|
|
CVector<unsigned char> CProtocol::GetSendMessage ()
|
|
|
|
{
|
|
|
|
// TEST, TODO implement protocol handling here (timers, etc.)
|
|
|
|
|
|
|
|
// convert unsigned uint8_t in char, TODO convert all buffers in uint8_t
|
|
|
|
CVector<unsigned char> vecbyDataConv ( vecMessage.Size () );
|
|
|
|
for ( int i = 0; i < vecMessage.Size (); i++ ) {
|
|
|
|
vecbyDataConv[i] = static_cast<unsigned char> ( vecMessage[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return vecbyDataConv;
|
|
|
|
}
|
|
|
|
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-26 11:50:47 +01:00
|
|
|
|
|
|
|
void CProtocol::EnqueueMessage ( CVector<uint8_t>& vecMessage )
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
|
|
|
|
emit MessReadyForSending ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool CProtocol::ParseMessage ( const CVector<unsigned char>& vecbyData,
|
|
|
|
const int iNumBytes )
|
2006-02-20 22:09:36 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
return code: true -> ok; false -> error
|
|
|
|
*/
|
2006-02-27 20:45:27 +01:00
|
|
|
int iRecCounter, iRecID, iData;
|
|
|
|
unsigned int iPos;
|
2006-02-20 22:09:36 +01:00
|
|
|
CVector<uint8_t> vecData;
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-26 11:50:47 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
// convert unsigned char in uint8_t, TODO convert all buffers in uint8_t
|
2006-02-26 12:53:38 +01:00
|
|
|
CVector<uint8_t> vecbyDataConv ( iNumBytes );
|
|
|
|
for ( int i = 0; i < iNumBytes; i++ ) {
|
2006-02-20 22:09:36 +01:00
|
|
|
vecbyDataConv[i] = static_cast<uint8_t> ( vecbyData[i] );
|
|
|
|
}
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-27 20:45:27 +01:00
|
|
|
|
|
|
|
// In case we received a message and returned an answer but our answer did
|
|
|
|
// not make it to the receiver, he will resend his message. We check here
|
|
|
|
// if the message is the same as the old one, and if this is the case, just
|
|
|
|
// resend our old answer again
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
|
2006-02-26 12:53:38 +01:00
|
|
|
// important: vecbyDataConv must have iNumBytes to get it work!!!
|
2006-02-20 22:09:36 +01:00
|
|
|
if ( ParseMessageFrame ( vecbyDataConv, iRecCounter, iRecID, vecData ) )
|
|
|
|
{
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-26 12:01:30 +01:00
|
|
|
|
|
|
|
// TEST
|
|
|
|
qDebug ( "parsing successful" );
|
|
|
|
|
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
switch ( iRecID )
|
|
|
|
{
|
|
|
|
case PROTMESSID_ACKN:
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
// TODO implement acknowledge code -> do implementation in CProtocol since
|
|
|
|
// it can be used in the server protocol, too
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
break;
|
2006-02-27 20:45:27 +01:00
|
|
|
|
|
|
|
case PROTMESSID_JITT_BUF_SIZE:
|
|
|
|
|
|
|
|
// TODO acknowledgement
|
|
|
|
|
|
|
|
// extract data from stream and emit signal for received value
|
|
|
|
iPos = 0;
|
|
|
|
iData = static_cast<int> ( GetValFromStream ( vecData, iPos, 2 ) );
|
|
|
|
|
|
|
|
emit ChangeJittBufSize ( iData );
|
|
|
|
break;
|
2006-02-20 22:09:36 +01:00
|
|
|
}
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
return true; // everything was ok
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false; // return error code
|
|
|
|
}
|
|
|
|
}
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-26 11:50:47 +01:00
|
|
|
void CProtocol::CreateJitBufMes ( const int iJitBufSize )
|
2006-02-20 22:09:36 +01:00
|
|
|
{
|
|
|
|
CVector<uint8_t> vecData ( 2 );
|
|
|
|
unsigned int iPos = 0;
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
// build data vector
|
|
|
|
PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( iJitBufSize ), 2 );
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
// build complete message
|
|
|
|
GenMessageFrame ( vecMessage, iCounter, PROTMESSID_JITT_BUF_SIZE, vecData );
|
2006-02-26 11:50:47 +01:00
|
|
|
|
|
|
|
// increase counter (wraps around automatically)
|
|
|
|
// TODO: make it thread safe!!!!!!!!!!!!
|
|
|
|
iCounter++;
|
|
|
|
|
|
|
|
// enqueue message
|
|
|
|
EnqueueMessage ( vecMessage );
|
2006-02-20 22:09:36 +01:00
|
|
|
}
|
2006-02-19 18:24:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* Message generation (parsing) *
|
|
|
|
\******************************************************************************/
|
2006-02-20 22:09:36 +01:00
|
|
|
bool CProtocol::ParseMessageFrame ( const CVector<uint8_t>& vecIn,
|
|
|
|
int& iCnt,
|
|
|
|
int& iID,
|
|
|
|
CVector<uint8_t>& vecData )
|
2006-02-19 09:14:21 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
return code: true -> ok; false -> error
|
|
|
|
*/
|
2006-02-19 17:35:35 +01:00
|
|
|
int iLenBy, i;
|
2006-02-19 09:14:21 +01:00
|
|
|
unsigned int iCurPos;
|
|
|
|
|
|
|
|
// query length of input vector
|
|
|
|
const int iVecInLenByte = vecIn.Size();
|
|
|
|
|
|
|
|
// vector must be at least "MESS_LEN_WITHOUT_DATA_BYTE" bytes long
|
|
|
|
if ( iVecInLenByte < MESS_LEN_WITHOUT_DATA_BYTE )
|
|
|
|
{
|
|
|
|
return false; // return error code
|
|
|
|
}
|
|
|
|
|
2006-02-26 12:49:28 +01:00
|
|
|
|
2006-02-19 09:14:21 +01:00
|
|
|
// decode header -----
|
|
|
|
iCurPos = 0; // start from beginning
|
|
|
|
|
2006-02-19 15:50:18 +01:00
|
|
|
/* 2 bytes ID */
|
|
|
|
iID = static_cast<int> ( GetValFromStream ( vecIn, iCurPos, 2 ) );
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-02-19 15:50:18 +01:00
|
|
|
/* 1 byte cnt */
|
|
|
|
iCnt = static_cast<int> ( GetValFromStream ( vecIn, iCurPos, 1 ) );
|
|
|
|
|
|
|
|
/* 2 bytes length */
|
|
|
|
iLenBy = static_cast<int> ( GetValFromStream ( vecIn, iCurPos, 2 ) );
|
2006-02-19 09:14:21 +01:00
|
|
|
|
|
|
|
// make sure the length is correct
|
|
|
|
if ( iLenBy != iVecInLenByte - MESS_LEN_WITHOUT_DATA_BYTE )
|
|
|
|
{
|
|
|
|
return false; // return error code
|
|
|
|
}
|
|
|
|
|
2006-02-26 12:49:28 +01:00
|
|
|
|
2006-02-19 09:14:21 +01:00
|
|
|
// now check CRC -----
|
|
|
|
CCRC CRCObj;
|
|
|
|
const int iLenCRCCalc = MESS_HEADER_LENGTH_BYTE + iLenBy;
|
2006-02-19 17:35:35 +01:00
|
|
|
|
|
|
|
iCurPos = 0; // start from beginning
|
2006-02-19 09:14:21 +01:00
|
|
|
for ( i = 0; i < iLenCRCCalc; i++ )
|
|
|
|
{
|
|
|
|
CRCObj.AddByte ( static_cast<uint8_t> (
|
|
|
|
GetValFromStream ( vecIn, iCurPos, 1 ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( CRCObj.GetCRC () != GetValFromStream ( vecIn, iCurPos, 2 ) )
|
|
|
|
{
|
|
|
|
return false; // return error code
|
|
|
|
}
|
|
|
|
|
2006-02-26 12:49:28 +01:00
|
|
|
|
2006-02-19 17:35:35 +01:00
|
|
|
// decode data -----
|
2006-02-26 13:18:37 +01:00
|
|
|
vecData.Init ( iLenBy );
|
2006-02-19 17:35:35 +01:00
|
|
|
iCurPos = MESS_HEADER_LENGTH_BYTE; // start from beginning of data
|
|
|
|
for ( i = 0; i < iLenBy; i++ )
|
|
|
|
{
|
|
|
|
vecData[i] = static_cast<uint8_t> (
|
|
|
|
GetValFromStream ( vecIn, iCurPos, 1 ) );
|
|
|
|
}
|
2006-02-19 09:14:21 +01:00
|
|
|
|
|
|
|
return true; // everything was ok
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CProtocol::GetValFromStream ( const CVector<uint8_t>& vecIn,
|
|
|
|
unsigned int& iPos,
|
|
|
|
const unsigned int iNumOfBytes )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
note: iPos is automatically incremented in this function
|
|
|
|
*/
|
|
|
|
// 4 bytes maximum since we return uint32
|
|
|
|
ASSERT ( ( iNumOfBytes > 0 ) && ( iNumOfBytes <= 4 ) );
|
|
|
|
ASSERT ( vecIn.Size() >= iPos + iNumOfBytes );
|
|
|
|
|
|
|
|
uint32_t iRet = 0;
|
|
|
|
for ( int i = 0; i < iNumOfBytes; i++ )
|
|
|
|
{
|
2006-02-19 15:50:18 +01:00
|
|
|
iRet |= vecIn[iPos] << ( i * 8 /* size of byte */ );
|
|
|
|
iPos++;
|
2006-02-19 09:14:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return iRet;
|
|
|
|
}
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
void CProtocol::GenMessageFrame ( CVector<uint8_t>& vecOut,
|
|
|
|
const int iCnt,
|
|
|
|
const int iID,
|
|
|
|
const CVector<uint8_t>& vecData )
|
2006-02-19 15:50:18 +01:00
|
|
|
{
|
2006-02-19 17:35:35 +01:00
|
|
|
int i;
|
|
|
|
|
2006-02-19 15:50:18 +01:00
|
|
|
// query length of data vector
|
|
|
|
const int iDataLenByte = vecData.Size();
|
|
|
|
|
|
|
|
// total length of message = 7 + "iDataLenByte"
|
|
|
|
// 2 byte ID + 1 byte cnt + 2 byte length + n bytes data + 2 bytes CRC
|
|
|
|
const int iTotLenByte = 7 + iDataLenByte;
|
|
|
|
|
|
|
|
// init message vector
|
|
|
|
vecOut.Init( iTotLenByte );
|
|
|
|
|
|
|
|
// encode header -----
|
|
|
|
unsigned int iCurPos = 0; // init position pointer
|
|
|
|
|
|
|
|
/* 2 bytes ID */
|
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( iID ), 2 );
|
|
|
|
|
|
|
|
/* 1 byte cnt */
|
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( iCnt ), 1 );
|
|
|
|
|
|
|
|
/* 2 bytes length */
|
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( iDataLenByte ), 2 );
|
|
|
|
|
2006-02-19 17:35:35 +01:00
|
|
|
// encode data -----
|
|
|
|
for ( i = 0; i < iDataLenByte; i++ )
|
|
|
|
{
|
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( vecData[i] ), 1 );
|
|
|
|
}
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-02-19 17:35:35 +01:00
|
|
|
// encode CRC -----
|
|
|
|
CCRC CRCObj;
|
|
|
|
iCurPos = 0; // start from beginning
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-02-19 17:35:35 +01:00
|
|
|
const int iLenCRCCalc = MESS_HEADER_LENGTH_BYTE + iDataLenByte;
|
|
|
|
for ( i = 0; i < iLenCRCCalc; i++ )
|
|
|
|
{
|
|
|
|
CRCObj.AddByte ( static_cast<uint8_t> (
|
|
|
|
GetValFromStream ( vecOut, iCurPos, 1 ) ) );
|
|
|
|
}
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-02-19 17:35:35 +01:00
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( CRCObj.GetCRC () ), 2 );
|
|
|
|
}
|
2006-02-19 15:50:18 +01:00
|
|
|
|
|
|
|
void CProtocol::PutValOnStream ( CVector<uint8_t>& vecIn,
|
|
|
|
unsigned int& iPos,
|
|
|
|
const uint32_t iVal,
|
|
|
|
const unsigned int iNumOfBytes )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
note: iPos is automatically incremented in this function
|
|
|
|
*/
|
|
|
|
// 4 bytes maximum since we use uint32
|
|
|
|
ASSERT ( ( iNumOfBytes > 0 ) && ( iNumOfBytes <= 4 ) );
|
|
|
|
ASSERT ( vecIn.Size() >= iPos + iNumOfBytes );
|
|
|
|
|
|
|
|
for ( int i = 0; i < iNumOfBytes; i++ )
|
|
|
|
{
|
|
|
|
vecIn[iPos] =
|
|
|
|
( iVal >> ( i * 8 /* size of byte */ ) ) & 255 /* 11111111 */;
|
|
|
|
|
|
|
|
iPos++;
|
|
|
|
}
|
|
|
|
}
|