first version of message parsing

This commit is contained in:
Volker Fischer 2006-02-19 16:35:35 +00:00
parent d8d0072eb7
commit dee9e32f81
3 changed files with 53 additions and 19 deletions

View file

@ -360,6 +360,10 @@
<name>text</name>
<string>C&amp;onnect</string>
</property>
<property stdset="1">
<name>default</name>
<bool>true</bool>
</property>
</widget>
</hbox>
</widget>

View file

@ -41,16 +41,15 @@ Protocol message definition
/* Implementation *************************************************************/
#define MESS_HEADER_LENGTH_BYTE 5 /* ID, cnt, length */
#define MESS_LEN_WITHOUT_DATA_BYTE ( MESS_HEADER_LENGTH_BYTE + 2 /* CRC */ )
bool CProtocol::ParseMessage ( const CVector<uint8_t>& vecIn )
bool CProtocol::ParseMessage ( const CVector<uint8_t>& vecIn,
int& iCnt,
int& iID,
CVector<uint8_t>& vecData )
{
/*
return code: true -> ok; false -> error
*/
int iID, iCnt, iLenBy, i;
int iLenBy, i;
unsigned int iCurPos;
// query length of input vector
@ -82,9 +81,9 @@ bool CProtocol::ParseMessage ( const CVector<uint8_t>& vecIn )
// now check CRC -----
CCRC CRCObj;
iCurPos = 0; // start from beginning
const int iLenCRCCalc = MESS_HEADER_LENGTH_BYTE + iLenBy;
iCurPos = 0; // start from beginning
for ( i = 0; i < iLenCRCCalc; i++ )
{
CRCObj.AddByte ( static_cast<uint8_t> (
@ -96,9 +95,13 @@ bool CProtocol::ParseMessage ( const CVector<uint8_t>& vecIn )
return false; // return error code
}
// TODO actual parsing of message data
// decode data -----
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 ) );
}
return true; // everything was ok
}
@ -127,8 +130,10 @@ uint32_t CProtocol::GetValFromStream ( const CVector<uint8_t>& vecIn,
void CProtocol::GenMessage ( CVector<uint8_t>& vecOut,
const int iCnt,
const int iID,
const CVector<uint8_t>& vecData)
const CVector<uint8_t>& vecData )
{
int i;
// query length of data vector
const int iDataLenByte = vecData.Size();
@ -154,12 +159,28 @@ void CProtocol::GenMessage ( CVector<uint8_t>& vecOut,
PutValOnStream ( vecOut, iCurPos,
static_cast<uint32_t> ( iDataLenByte ), 2 );
// TODO data, CRC
// encode data -----
for ( i = 0; i < iDataLenByte; i++ )
{
PutValOnStream ( vecOut, iCurPos,
static_cast<uint32_t> ( vecData[i] ), 1 );
}
// encode CRC -----
CCRC CRCObj;
iCurPos = 0; // start from beginning
const int iLenCRCCalc = MESS_HEADER_LENGTH_BYTE + iDataLenByte;
for ( i = 0; i < iLenCRCCalc; i++ )
{
CRCObj.AddByte ( static_cast<uint8_t> (
GetValFromStream ( vecOut, iCurPos, 1 ) ) );
}
PutValOnStream ( vecOut, iCurPos,
static_cast<uint32_t> ( CRCObj.GetCRC () ), 2 );
}
void CProtocol::PutValOnStream ( CVector<uint8_t>& vecIn,
unsigned int& iPos,
const uint32_t iVal,

View file

@ -36,6 +36,10 @@
#define PROTMESSID_JITT_BUF_SIZE 10 // jitter buffer size
#define PROTMESSID_PING 11 // for measuring ping time
// lengths of message as defined in protocol.cpp file
#define MESS_HEADER_LENGTH_BYTE 5 /* ID, cnt, length */
#define MESS_LEN_WITHOUT_DATA_BYTE ( MESS_HEADER_LENGTH_BYTE + 2 /* CRC */ )
/* Classes ********************************************************************/
class CProtocol
@ -45,7 +49,11 @@ public:
virtual ~CProtocol() {}
protected:
bool ParseMessage ( const CVector<uint8_t>& vecIn );
bool ParseMessage ( const CVector<uint8_t>& vecIn,
int& iCnt,
int& iID,
CVector<uint8_t>& vecData );
void GenMessage ( CVector<uint8_t>& vecOut,
const int iCnt,
const int iID,
@ -55,9 +63,10 @@ protected:
unsigned int& iPos,
const uint32_t iVal,
const unsigned int iNumOfBytes );
uint32_t GetValFromStream ( const CVector<uint8_t>& vecIn,
unsigned int& iPos,
const unsigned int iNumOfBytes );
uint32_t GetValFromStream ( const CVector<uint8_t>& vecIn,
unsigned int& iPos,
const unsigned int iNumOfBytes );
uint8_t iCounter;
};