2006-02-12 15:26:46 +01:00
|
|
|
/******************************************************************************\
|
|
|
|
* Copyright (c) 2004-2006
|
|
|
|
*
|
|
|
|
* Author(s):
|
2006-11-25 15:46:57 +01:00
|
|
|
* Volker Fischer
|
2006-02-12 15:26:46 +01:00
|
|
|
*
|
2006-02-18 23:05:46 +01:00
|
|
|
|
|
|
|
Protocol message definition
|
2006-03-01 20:46:44 +01:00
|
|
|
---------------------------
|
|
|
|
|
|
|
|
- All messages received need to be acknowledged by an acknowledge packet
|
|
|
|
|
2006-02-18 23:05:46 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
|
|
|
|
MAIN FRAME
|
|
|
|
----------
|
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
+------------+------------+------------------+--------------+-------------+
|
|
|
|
| 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
|
|
|
|
--------
|
|
|
|
|
2006-03-12 12:50:35 +01:00
|
|
|
- Acknowledgement message: PROTMESSID_ACKN
|
2006-03-01 20:46:44 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
+-----------------------------------+
|
|
|
|
| 2 bytes ID of message to be ackn. |
|
|
|
|
+-----------------------------------+
|
2006-03-01 20:46:44 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
note: the cnt value is the same as of the message to be acknowledged
|
2006-03-01 20:46:44 +01:00
|
|
|
|
|
|
|
|
2006-03-12 12:50:35 +01:00
|
|
|
- Jitter buffer size: PROTMESSID_JITT_BUF_SIZE
|
2006-02-20 22:09:36 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
+--------------------------+
|
|
|
|
| 2 bytes number of blocks |
|
|
|
|
+--------------------------+
|
2006-02-20 22:09:36 +01:00
|
|
|
|
2006-03-12 12:50:35 +01:00
|
|
|
- Request jitter buffer size: PROTMESSID_REQ_JITT_BUF_SIZE
|
2006-03-08 19:44:21 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
note: does not have any data -> n = 0
|
2006-03-07 22:26:40 +01:00
|
|
|
|
2006-11-27 23:35:22 +01:00
|
|
|
- Server full message: PROTMESSID_SERVER_FULL
|
|
|
|
|
|
|
|
note: does not have any data -> n = 0
|
|
|
|
|
|
|
|
|
2006-11-04 10:57:59 +01:00
|
|
|
- Network buffer block size factor PROTMESSID_NET_BLSI_FACTOR
|
2006-03-12 12:50:35 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
note: size, relative to minimum block size
|
2006-03-12 12:50:35 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
+----------------+
|
|
|
|
| 2 bytes factor |
|
|
|
|
+----------------+
|
2006-02-27 20:45:27 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
- Gain of channel PROTMESSID_CHANNEL_GAIN
|
2006-11-04 10:57:59 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
+-------------------+--------------+
|
|
|
|
| 1 byte channel ID | 2 bytes gain |
|
|
|
|
+-------------------+--------------+
|
2006-11-04 10:57:59 +01:00
|
|
|
|
|
|
|
|
2006-10-30 22:14:44 +01:00
|
|
|
- IP number and name of connected clients PROTMESSID_CONN_CLIENTS_LIST
|
2006-02-20 22:09:36 +01:00
|
|
|
|
2006-10-30 22:14:44 +01:00
|
|
|
for each connected client append following data:
|
2006-02-20 22:09:36 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
+-------------------+--------------------+------------------+----------------------+
|
|
|
|
| 1 byte channel ID | 4 bytes IP address | 2 bytes number n | n bytes UTF-8 string |
|
|
|
|
+-------------------+--------------------+------------------+----------------------+
|
2006-02-20 22:09:36 +01:00
|
|
|
|
2006-12-08 19:19:16 +01:00
|
|
|
- Request connected clients list: PROTMESSID_REQ_CONN_CLIENTS_LIST
|
|
|
|
|
|
|
|
note: does not have any data -> n = 0
|
|
|
|
|
|
|
|
|
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-03-04 11:24:40 +01:00
|
|
|
CProtocol::CProtocol() : iCounter ( 0 ), iOldRecID ( PROTMESSID_ILLEGAL ),
|
2006-11-25 15:46:57 +01:00
|
|
|
iOldRecCnt ( 0 )
|
2006-03-04 11:24:40 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
SendMessQueue.clear();
|
2006-02-26 11:50:47 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// connections
|
|
|
|
QObject::connect ( &TimerSendMess, SIGNAL ( timeout() ),
|
|
|
|
this, SLOT ( OnTimerSendMess() ) );
|
2006-02-26 11:50:47 +01:00
|
|
|
}
|
|
|
|
|
2006-03-04 11:24:40 +01:00
|
|
|
void CProtocol::EnqueueMessage ( CVector<uint8_t>& vecMessage,
|
2006-11-25 15:46:57 +01:00
|
|
|
const int iCnt,
|
|
|
|
const int iID )
|
2006-03-04 11:24:40 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
bool bListWasEmpty;
|
|
|
|
|
|
|
|
Mutex.lock();
|
|
|
|
{
|
|
|
|
// check if list is empty so that we have to initiate a send process
|
|
|
|
bListWasEmpty = SendMessQueue.empty();
|
|
|
|
}
|
|
|
|
Mutex.unlock();
|
|
|
|
|
|
|
|
// create send message object for the queue
|
|
|
|
CSendMessage SendMessageObj ( vecMessage, iCnt, iID );
|
|
|
|
|
|
|
|
Mutex.lock();
|
|
|
|
{
|
|
|
|
// we want to have a FIFO: we add at the end and take from the beginning
|
|
|
|
SendMessQueue.push_back ( SendMessageObj );
|
|
|
|
}
|
|
|
|
Mutex.unlock();
|
|
|
|
|
|
|
|
// if list was empty, initiate send process
|
|
|
|
if ( bListWasEmpty )
|
|
|
|
{
|
|
|
|
SendMessage();
|
|
|
|
}
|
2006-03-04 11:24:40 +01:00
|
|
|
}
|
2006-02-26 11:50:47 +01:00
|
|
|
|
2006-03-04 11:24:40 +01:00
|
|
|
void CProtocol::SendMessage()
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
CVector<uint8_t> vecMessage;
|
|
|
|
bool bSendMess = false;
|
|
|
|
|
|
|
|
Mutex.lock();
|
|
|
|
{
|
|
|
|
// we have to check that list is not empty, since in another thread the
|
|
|
|
// last element of the list might have been erased
|
|
|
|
if ( !SendMessQueue.empty() )
|
|
|
|
{
|
|
|
|
vecMessage.Init ( SendMessQueue.front().vecMessage.Size() );
|
|
|
|
vecMessage = SendMessQueue.front().vecMessage;
|
|
|
|
|
|
|
|
bSendMess = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Mutex.unlock();
|
|
|
|
|
|
|
|
if ( bSendMess )
|
|
|
|
{
|
|
|
|
// send message
|
|
|
|
emit MessReadyForSending ( vecMessage );
|
|
|
|
|
|
|
|
// start time-out timer if not active
|
|
|
|
if ( !TimerSendMess.isActive() )
|
|
|
|
{
|
|
|
|
TimerSendMess.start ( SEND_MESS_TIMEOUT_MS );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no message to send, stop timer
|
|
|
|
TimerSendMess.stop();
|
|
|
|
}
|
2006-03-04 11:24:40 +01:00
|
|
|
}
|
2006-02-26 11:50:47 +01:00
|
|
|
|
2006-03-08 21:13:42 +01:00
|
|
|
void CProtocol::CreateAndSendMessage ( const int iID,
|
2006-11-25 15:46:57 +01:00
|
|
|
const CVector<uint8_t>& vecData )
|
2006-03-08 21:13:42 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
CVector<uint8_t> vecNewMessage;
|
|
|
|
int iCurCounter;
|
2006-03-08 21:13:42 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
Mutex.lock();
|
|
|
|
{
|
|
|
|
// store current counter value
|
|
|
|
iCurCounter = iCounter;
|
2006-03-08 21:13:42 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// increase counter (wraps around automatically)
|
|
|
|
iCounter++;
|
|
|
|
}
|
|
|
|
Mutex.unlock();
|
2006-03-08 21:13:42 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// build complete message
|
|
|
|
GenMessageFrame ( vecNewMessage, iCurCounter, iID, vecData );
|
2006-03-08 21:13:42 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// enqueue message
|
|
|
|
EnqueueMessage ( vecNewMessage, iCurCounter, iID );
|
2006-03-08 21:13:42 +01:00
|
|
|
}
|
|
|
|
|
2006-03-06 18:04:07 +01:00
|
|
|
void CProtocol::CreateAndSendAcknMess ( const int& iID, const int& iCnt )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
CVector<uint8_t> vecAcknMessage;
|
|
|
|
CVector<uint8_t> vecData ( 2 ); // 2 bytes of data
|
|
|
|
unsigned int iPos = 0; // init position pointer
|
2006-03-06 18:04:07 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// build data vector
|
|
|
|
PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( iID ), 2 );
|
2006-03-06 18:04:07 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// build complete message
|
|
|
|
GenMessageFrame ( vecAcknMessage, iCnt, PROTMESSID_ACKN, vecData );
|
2006-03-06 18:04:07 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// immediately send acknowledge message
|
|
|
|
emit MessReadyForSending ( vecAcknMessage );
|
2006-03-06 18:04:07 +01:00
|
|
|
}
|
|
|
|
|
2006-03-07 21:22:52 +01:00
|
|
|
void CProtocol::DeleteSendMessQueue()
|
2006-03-06 18:04:07 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
Mutex.lock();
|
|
|
|
{
|
|
|
|
// delete complete "send message queue"
|
|
|
|
SendMessQueue.clear();
|
|
|
|
}
|
|
|
|
Mutex.unlock();
|
2006-03-04 11:24:40 +01:00
|
|
|
}
|
2006-02-26 11:50:47 +01:00
|
|
|
|
|
|
|
bool CProtocol::ParseMessage ( const CVector<unsigned char>& vecbyData,
|
2006-11-25 15:46:57 +01:00
|
|
|
const int iNumBytes )
|
2006-02-20 22:09:36 +01:00
|
|
|
{
|
|
|
|
/*
|
2006-11-25 15:46:57 +01:00
|
|
|
return code: true -> ok; false -> error
|
2006-02-20 22:09:36 +01:00
|
|
|
*/
|
2006-11-25 15:46:57 +01:00
|
|
|
bool bRet, bSendNextMess;
|
|
|
|
int iRecCounter, iRecID;
|
|
|
|
unsigned int iPos;
|
|
|
|
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-11-25 15:46:57 +01:00
|
|
|
vecbyDataConv[i] = static_cast<uint8_t> ( vecbyData[i] );
|
2006-02-20 22:09:36 +01:00
|
|
|
}
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-02-27 20:45:27 +01:00
|
|
|
|
2006-02-26 12:53:38 +01:00
|
|
|
// important: vecbyDataConv must have iNumBytes to get it work!!!
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( ParseMessageFrame ( vecbyDataConv, iRecCounter, iRecID, vecData ) )
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
if ( ( iOldRecID == iRecID ) && ( iOldRecCnt == iRecCounter ) )
|
|
|
|
{
|
|
|
|
// acknowledgments are not acknowledged
|
|
|
|
if ( iRecID != PROTMESSID_ACKN )
|
|
|
|
{
|
|
|
|
// re-send acknowledgement
|
|
|
|
CreateAndSendAcknMess ( iRecID, iRecCounter );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// special treatment for acknowledge messages
|
|
|
|
if ( iRecID == PROTMESSID_ACKN )
|
|
|
|
{
|
|
|
|
// extract data from stream and emit signal for received value
|
|
|
|
iPos = 0;
|
|
|
|
const int iData =
|
|
|
|
static_cast<int> ( GetValFromStream ( vecData, iPos, 2 ) );
|
|
|
|
|
|
|
|
Mutex.lock();
|
|
|
|
{
|
|
|
|
// check if this is the correct acknowledgment
|
|
|
|
if ( ( SendMessQueue.front().iCnt == iRecCounter ) &&
|
|
|
|
( SendMessQueue.front().iID == iData ) )
|
|
|
|
{
|
|
|
|
// message acknowledged, remove from queue
|
|
|
|
SendMessQueue.pop_front();
|
|
|
|
|
|
|
|
// send next message in queue
|
|
|
|
bSendNextMess = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bSendNextMess = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Mutex.unlock();
|
|
|
|
|
|
|
|
if ( bSendNextMess )
|
|
|
|
{
|
|
|
|
SendMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// init position pointer which is used for extracting data from
|
|
|
|
// received data vector
|
|
|
|
iPos = 0;
|
|
|
|
|
|
|
|
// check which type of message we received and do action
|
|
|
|
switch ( iRecID )
|
|
|
|
{
|
|
|
|
case PROTMESSID_JITT_BUF_SIZE:
|
|
|
|
|
|
|
|
EvaluateJitBufMes ( iPos, vecData );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROTMESSID_REQ_JITT_BUF_SIZE:
|
|
|
|
|
|
|
|
EvaluateReqJitBufMes ( iPos, vecData );
|
|
|
|
break;
|
|
|
|
|
2006-11-27 23:35:22 +01:00
|
|
|
case PROTMESSID_SERVER_FULL:
|
|
|
|
|
|
|
|
EvaluateServerFullMes ( iPos, vecData );
|
|
|
|
break;
|
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
case PROTMESSID_NET_BLSI_FACTOR:
|
|
|
|
|
|
|
|
EvaluateNetwBlSiFactMes ( iPos, vecData );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROTMESSID_CHANNEL_GAIN:
|
|
|
|
|
|
|
|
EvaluateChanGainMes ( iPos, vecData );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROTMESSID_CONN_CLIENTS_LIST:
|
|
|
|
|
|
|
|
EvaluateConClientListMes ( iPos, vecData );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send acknowledge message
|
|
|
|
CreateAndSendAcknMess ( iRecID, iRecCounter );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save current message ID and counter to find out if message was re-sent
|
|
|
|
iOldRecID = iRecID;
|
|
|
|
iOldRecCnt = iRecCounter;
|
|
|
|
|
|
|
|
bRet = true; // everything was ok
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bRet = false; // return error code
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
2006-03-01 20:46:44 +01:00
|
|
|
}
|
|
|
|
|
2006-03-08 21:13:42 +01:00
|
|
|
|
2006-11-05 11:09:32 +01:00
|
|
|
/* Access-functions for creating and parsing messages ----------------------- */
|
2006-02-26 11:50:47 +01:00
|
|
|
void CProtocol::CreateJitBufMes ( const int iJitBufSize )
|
2006-02-20 22:09:36 +01:00
|
|
|
{
|
2006-11-27 23:35:22 +01:00
|
|
|
CVector<uint8_t> vecData ( 2 ); // 2 bytes of data
|
|
|
|
unsigned int iPos = 0; // init position pointer
|
2006-03-08 19:44:21 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// build data vector
|
|
|
|
PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( iJitBufSize ), 2 );
|
2006-03-04 17:49:11 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
CreateAndSendMessage ( PROTMESSID_JITT_BUF_SIZE, vecData );
|
2006-02-20 22:09:36 +01:00
|
|
|
}
|
2006-02-19 18:24:30 +01:00
|
|
|
|
2006-11-05 11:09:32 +01:00
|
|
|
void CProtocol::EvaluateJitBufMes ( unsigned int iPos, const CVector<uint8_t>& vecData )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// extract jitter buffer size
|
|
|
|
const int iData =
|
|
|
|
static_cast<int> ( GetValFromStream ( vecData, iPos, 2 ) );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// invoke message action
|
|
|
|
emit ChangeJittBufSize ( iData );
|
2006-11-05 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2006-03-07 22:26:40 +01:00
|
|
|
void CProtocol::CreateReqJitBufMes()
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
CreateAndSendMessage ( PROTMESSID_REQ_JITT_BUF_SIZE, CVector<uint8_t> ( 0 ) );
|
2006-03-07 22:26:40 +01:00
|
|
|
}
|
|
|
|
|
2006-11-05 11:09:32 +01:00
|
|
|
void CProtocol::EvaluateReqJitBufMes ( unsigned int iPos, const CVector<uint8_t>& vecData )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// invoke message action
|
|
|
|
emit ReqJittBufSize();
|
2006-11-05 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2006-11-27 23:35:22 +01:00
|
|
|
void CProtocol::CreateServerFullMes()
|
|
|
|
{
|
|
|
|
CreateAndSendMessage ( PROTMESSID_SERVER_FULL, CVector<uint8_t> ( 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void CProtocol::EvaluateServerFullMes ( unsigned int iPos, const CVector<uint8_t>& vecData )
|
|
|
|
{
|
|
|
|
// invoke message action
|
|
|
|
emit ServerFull();
|
|
|
|
}
|
|
|
|
|
2006-03-12 12:50:35 +01:00
|
|
|
void CProtocol::CreateNetwBlSiFactMes ( const int iNetwBlSiFact )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
CVector<uint8_t> vecData ( 2 ); // 2 bytes of data
|
|
|
|
unsigned int iPos = 0; // init position pointer
|
2006-03-12 12:50:35 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// build data vector
|
|
|
|
PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( iNetwBlSiFact ), 2 );
|
2006-03-12 12:50:35 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
CreateAndSendMessage ( PROTMESSID_NET_BLSI_FACTOR, vecData );
|
2006-03-12 12:50:35 +01:00
|
|
|
}
|
|
|
|
|
2006-11-05 11:09:32 +01:00
|
|
|
void CProtocol::EvaluateNetwBlSiFactMes ( unsigned int iPos, const CVector<uint8_t>& vecData )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
const int iData =
|
|
|
|
static_cast<int> ( GetValFromStream ( vecData, iPos, 2 ) );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// invoke message action
|
|
|
|
emit ChangeNetwBlSiFact ( iData );
|
2006-11-05 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2006-11-04 10:57:59 +01:00
|
|
|
void CProtocol::CreateChanGainMes ( const int iChanID, const double dGain )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
CVector<uint8_t> vecData ( 3 ); // 3 bytes of data
|
|
|
|
unsigned int iPos = 0; // init position pointer
|
2006-11-04 10:57:59 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// build data vector
|
|
|
|
// channel ID
|
|
|
|
PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( iChanID ), 1 );
|
2006-11-04 10:57:59 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// actual gain, we convert from double with range 0..1 to integer
|
|
|
|
const int iCurGain = (int) ( dGain * ( 1 << 16 ) );
|
|
|
|
PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( iCurGain ), 2 );
|
2006-10-30 22:14:44 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
CreateAndSendMessage ( PROTMESSID_CHANNEL_GAIN, vecData );
|
2006-11-04 10:57:59 +01:00
|
|
|
}
|
2006-10-30 22:14:44 +01:00
|
|
|
|
2006-11-05 11:09:32 +01:00
|
|
|
void CProtocol::EvaluateChanGainMes ( unsigned int iPos, const CVector<uint8_t>& vecData )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// channel ID
|
|
|
|
const int iCurID =
|
|
|
|
static_cast<int> ( GetValFromStream ( vecData, iPos, 1 ) );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// actual gain, we convert from integer to double with range 0..1
|
|
|
|
const int iData =
|
|
|
|
static_cast<int> ( GetValFromStream ( vecData, iPos, 2 ) );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
const double dNewGain = (double) iData / ( 1 << 16 );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// invoke message action
|
|
|
|
emit ChangeChanGain ( iCurID, dNewGain );
|
2006-11-05 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2006-11-26 22:25:56 +01:00
|
|
|
void CProtocol::CreateConClientListMes ( const CVector<CChannelShortInfo>& vecChanInfo )
|
2006-11-04 10:57:59 +01:00
|
|
|
{
|
2006-11-26 22:25:56 +01:00
|
|
|
const int iNumClients = vecChanInfo.Size();
|
2006-11-25 15:46:57 +01:00
|
|
|
|
|
|
|
// build data vector
|
2006-11-27 23:35:22 +01:00
|
|
|
CVector<uint8_t> vecData ( 0 );
|
|
|
|
unsigned int iPos = 0; // init position pointer
|
2006-11-25 15:46:57 +01:00
|
|
|
|
|
|
|
for ( int i = 0; i < iNumClients; i++ )
|
|
|
|
{
|
|
|
|
// current string size
|
2006-11-26 22:25:56 +01:00
|
|
|
const int iCurStrLen = vecChanInfo[i].vecstrName.size();
|
2006-11-25 15:46:57 +01:00
|
|
|
|
|
|
|
// size of current list entry
|
|
|
|
const int iCurListEntrLen =
|
|
|
|
1 /* chan ID */ + 4 /* IP addr. */ + 2 /* str. size */ + iCurStrLen;
|
|
|
|
|
|
|
|
// make space for new data
|
|
|
|
vecData.Enlarge ( iCurListEntrLen );
|
|
|
|
|
|
|
|
// channel ID
|
|
|
|
PutValOnStream ( vecData, iPos,
|
2006-11-26 22:25:56 +01:00
|
|
|
static_cast<uint32_t> ( vecChanInfo[i].veciChanID ), 1 );
|
2006-11-25 15:46:57 +01:00
|
|
|
|
|
|
|
// IP address (4 bytes)
|
|
|
|
PutValOnStream ( vecData, iPos,
|
2006-11-26 22:25:56 +01:00
|
|
|
static_cast<uint32_t> ( vecChanInfo[i].veciIpAddr ), 4 );
|
2006-11-25 15:46:57 +01:00
|
|
|
|
|
|
|
// number of bytes for name string (2 bytes)
|
|
|
|
PutValOnStream ( vecData, iPos,
|
|
|
|
static_cast<uint32_t> ( iCurStrLen ), 2 );
|
|
|
|
|
|
|
|
// name string (n bytes)
|
|
|
|
for ( int j = 0; j < iCurStrLen; j++ )
|
|
|
|
{
|
|
|
|
// byte-by-byte copying of the string data
|
|
|
|
PutValOnStream ( vecData, iPos,
|
2006-11-26 22:25:56 +01:00
|
|
|
static_cast<uint32_t> ( vecChanInfo[i].vecstrName[j] ), 1 );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CreateAndSendMessage ( PROTMESSID_CONN_CLIENTS_LIST, vecData );
|
2006-11-04 10:57:59 +01:00
|
|
|
}
|
2006-10-30 22:14:44 +01:00
|
|
|
|
2006-11-05 11:09:32 +01:00
|
|
|
void CProtocol:: EvaluateConClientListMes ( unsigned int iPos, const CVector<uint8_t>& vecData )
|
|
|
|
{
|
2006-11-27 23:35:22 +01:00
|
|
|
int iData;
|
|
|
|
const int iDataLen = vecData.Size();
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( 0 );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
while ( iPos < iDataLen )
|
|
|
|
{
|
|
|
|
// channel ID (1 byte)
|
2006-11-26 22:25:56 +01:00
|
|
|
const int iChanID = static_cast<int> ( GetValFromStream ( vecData, iPos, 1 ) );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// IP address (4 bytes)
|
2006-11-26 22:25:56 +01:00
|
|
|
const int iIpAddr = static_cast<int> ( GetValFromStream ( vecData, iPos, 4 ) );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// number of bytes for name string (2 bytes)
|
|
|
|
const int iStringLen =
|
|
|
|
static_cast<int> ( GetValFromStream ( vecData, iPos, 2 ) );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// name string (n bytes)
|
2006-11-26 22:25:56 +01:00
|
|
|
std::string strCurStr = "";
|
2006-11-25 15:46:57 +01:00
|
|
|
for ( int j = 0; j < iStringLen; j++ )
|
|
|
|
{
|
|
|
|
// byte-by-byte copying of the string data
|
|
|
|
iData = static_cast<int> ( GetValFromStream ( vecData, iPos, 1 ) );
|
2006-11-26 22:25:56 +01:00
|
|
|
strCurStr += std::string ( (char*) &iData );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-11-26 22:25:56 +01:00
|
|
|
// add channel information to vector
|
|
|
|
vecChanInfo.Add ( CChannelShortInfo ( iChanID, iIpAddr, strCurStr ) );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
2006-11-26 22:25:56 +01:00
|
|
|
|
|
|
|
// invoke message action
|
|
|
|
emit ConClientListMesReceived ( vecChanInfo );
|
2006-11-05 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2006-12-08 19:19:16 +01:00
|
|
|
void CProtocol::CreateReqConnClientsList()
|
|
|
|
{
|
|
|
|
CreateAndSendMessage ( PROTMESSID_REQ_CONN_CLIENTS_LIST, CVector<uint8_t> ( 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void CProtocol::EvaluateReqConnClientsList ( unsigned int iPos, const CVector<uint8_t>& vecData )
|
|
|
|
{
|
|
|
|
// invoke message action
|
|
|
|
emit ReqConnClientsList();
|
|
|
|
}
|
|
|
|
|
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,
|
2006-11-25 15:46:57 +01:00
|
|
|
int& iCnt,
|
|
|
|
int& iID,
|
|
|
|
CVector<uint8_t>& vecData )
|
2006-02-19 09:14:21 +01:00
|
|
|
{
|
|
|
|
/*
|
2006-11-25 15:46:57 +01:00
|
|
|
return code: true -> ok; false -> error
|
2006-02-19 09:14:21 +01:00
|
|
|
*/
|
2006-11-25 15:46:57 +01:00
|
|
|
int iLenBy, i;
|
|
|
|
unsigned int iCurPos;
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// query length of input vector
|
|
|
|
const int iVecInLenByte = vecIn.Size();
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// 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-19 09:14:21 +01:00
|
|
|
|
2006-02-26 12:49:28 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// decode header -----
|
|
|
|
iCurPos = 0; // start from beginning
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
/* 2 bytes ID */
|
|
|
|
iID = static_cast<int> ( GetValFromStream ( vecIn, iCurPos, 2 ) );
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
/* 1 byte cnt */
|
|
|
|
iCnt = static_cast<int> ( GetValFromStream ( vecIn, iCurPos, 1 ) );
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
/* 2 bytes length */
|
|
|
|
iLenBy = static_cast<int> ( GetValFromStream ( vecIn, iCurPos, 2 ) );
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// make sure the length is correct
|
|
|
|
if ( iLenBy != iVecInLenByte - MESS_LEN_WITHOUT_DATA_BYTE )
|
|
|
|
{
|
|
|
|
return false; // return error code
|
|
|
|
}
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-02-26 12:49:28 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// now check CRC -----
|
|
|
|
CCRC CRCObj;
|
|
|
|
const int iLenCRCCalc = MESS_HEADER_LENGTH_BYTE + iLenBy;
|
2006-02-19 17:35:35 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
iCurPos = 0; // start from beginning
|
|
|
|
for ( i = 0; i < iLenCRCCalc; i++ )
|
|
|
|
{
|
|
|
|
CRCObj.AddByte ( static_cast<uint8_t> (
|
|
|
|
GetValFromStream ( vecIn, iCurPos, 1 ) ) );
|
|
|
|
}
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( CRCObj.GetCRC () != GetValFromStream ( vecIn, iCurPos, 2 ) )
|
|
|
|
{
|
|
|
|
return false; // return error code
|
|
|
|
}
|
2006-02-19 09:14:21 +01:00
|
|
|
|
2006-02-26 12:49:28 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// decode data -----
|
|
|
|
vecData.Init ( iLenBy );
|
|
|
|
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
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
return true; // everything was ok
|
2006-02-19 09:14:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CProtocol::GetValFromStream ( const CVector<uint8_t>& vecIn,
|
2006-11-27 23:35:22 +01:00
|
|
|
unsigned int& iPos,
|
|
|
|
const unsigned int iNumOfBytes )
|
2006-02-19 09:14:21 +01:00
|
|
|
{
|
|
|
|
/*
|
2006-11-25 15:46:57 +01:00
|
|
|
note: iPos is automatically incremented in this function
|
2006-02-19 09:14:21 +01:00
|
|
|
*/
|
2006-11-25 15:46:57 +01:00
|
|
|
// 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++ )
|
|
|
|
{
|
|
|
|
iRet |= vecIn[iPos] << ( i * 8 /* size of byte */ );
|
|
|
|
iPos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iRet;
|
2006-02-19 09:14:21 +01:00
|
|
|
}
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
void CProtocol::GenMessageFrame ( CVector<uint8_t>& vecOut,
|
2006-11-25 15:46:57 +01:00
|
|
|
const int iCnt,
|
|
|
|
const int iID,
|
|
|
|
const CVector<uint8_t>& vecData )
|
2006-02-19 15:50:18 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
int i;
|
2006-02-19 17:35:35 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// query length of data vector
|
|
|
|
const int iDataLenByte = vecData.Size();
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// 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;
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// init message vector
|
|
|
|
vecOut.Init( iTotLenByte );
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// encode header -----
|
|
|
|
unsigned int iCurPos = 0; // init position pointer
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
/* 2 bytes ID */
|
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( iID ), 2 );
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
/* 1 byte cnt */
|
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( iCnt ), 1 );
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
/* 2 bytes length */
|
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( iDataLenByte ), 2 );
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +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-11-25 15:46:57 +01:00
|
|
|
// encode CRC -----
|
|
|
|
CCRC CRCObj;
|
|
|
|
iCurPos = 0; // start from beginning
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +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-11-25 15:46:57 +01:00
|
|
|
PutValOnStream ( vecOut, iCurPos,
|
|
|
|
static_cast<uint32_t> ( CRCObj.GetCRC () ), 2 );
|
2006-02-19 17:35:35 +01:00
|
|
|
}
|
2006-02-19 15:50:18 +01:00
|
|
|
|
|
|
|
void CProtocol::PutValOnStream ( CVector<uint8_t>& vecIn,
|
2006-11-25 15:46:57 +01:00
|
|
|
unsigned int& iPos,
|
|
|
|
const uint32_t iVal,
|
|
|
|
const unsigned int iNumOfBytes )
|
2006-02-19 15:50:18 +01:00
|
|
|
{
|
|
|
|
/*
|
2006-11-25 15:46:57 +01:00
|
|
|
note: iPos is automatically incremented in this function
|
2006-02-19 15:50:18 +01:00
|
|
|
*/
|
2006-11-25 15:46:57 +01:00
|
|
|
// 4 bytes maximum since we use uint32
|
|
|
|
ASSERT ( ( iNumOfBytes > 0 ) && ( iNumOfBytes <= 4 ) );
|
|
|
|
ASSERT ( vecIn.Size() >= iPos + iNumOfBytes );
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
for ( int i = 0; i < iNumOfBytes; i++ )
|
|
|
|
{
|
|
|
|
vecIn[iPos] =
|
|
|
|
( iVal >> ( i * 8 /* size of byte */ ) ) & 255 /* 11111111 */;
|
2006-02-19 15:50:18 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
iPos++;
|
|
|
|
}
|
2006-02-19 15:50:18 +01:00
|
|
|
}
|