⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 atlasmsg.cpp

📁 cell phone source code
💻 CPP
字号:
// AtlasMsg.cpp: implementation of the CAtlasMsg class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AtlasMsg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CAtlasMsg::CAtlasMsg()
{
	m_bFirstCmd = TRUE;
	Reset();
}

CAtlasMsg::~CAtlasMsg()
{
}

void CAtlasMsg::Reset()
{
	m_iLength = 0;

	m_iType = -1;
	m_iVDataLength = 0;

	m_iCmdCode = m_iCmdReg = m_iCmdVal = -1;

	m_iPackSequence = m_iUnpackSequence = 0;
}

int CAtlasMsg::SetBuf(BYTE * pData, int iLength)
{
	if (iLength + m_iLength > P_MAX_PACKET_LENGTH)
	{
		return -1;
	}
	memcpy(m_pBuf + m_iLength, pData, iLength);
	m_iLength += iLength;
	return m_iLength;
}

int CAtlasMsg::PackNone(int iType)
{
	m_pBuf[0] = (BYTE)iType;
	m_iLength = 1;
	return 1;
}

int CAtlasMsg::Pack(int iType, int iSub, const BYTE * pData, int iLength)
{
	WORD w = (WORD)iLength;
	BYTE * pBuf = m_pBuf;

	if (iLength > P_MAX_PACKET_LENGTH)
	{
		return -1;
	}

	*pBuf ++ = (BYTE)iType;
	*pBuf ++ = (BYTE)iSub;
	*pBuf ++ = LOBYTE(w);
	*pBuf ++ = HIBYTE(w);

	if (iLength > 0)
	{
		memcpy(pBuf, pData, iLength);
		pBuf += iLength;
		w = (WORD)GetCheckSum(pData, iLength);
	}
	else
	{
		w = 0;
	}
	*pBuf ++ = LOBYTE(w);
	*pBuf ++ = HIBYTE(w);

	m_iLength = pBuf - m_pBuf;
	return m_iLength;
}

int CAtlasMsg::PackUnfinishedData(const BYTE * pData, int iLength)
{
	int iSequence = m_iPackSequence;
	m_iPackSequence ++;
	return Pack(P_DATA_UNFINISHED, iSequence, pData, iLength);
}

int CAtlasMsg::PackLastData(const BYTE * pData, int iLength)
{
	int iSequence = m_iPackSequence;
	m_iPackSequence = 0;
	return Pack(P_DATA, iSequence, pData, iLength);
}

int CAtlasMsg::PackCmdSetReg(int iReg, int iVal)
{
	m_pCmd[0] = P_CMD_SET_REG;
	m_pCmd[1] = iReg;
	memcpy(m_pCmd + 2, &iVal, sizeof(int));
	return Pack(P_CMD, P_CMD_NORMAL, m_pCmd, 6);
}

int CAtlasMsg::PackCmdGetReg(int iReg)
{
	m_pCmd[0] = P_CMD_GET_REG;
	m_pCmd[1] = iReg;
	return Pack(P_CMD, P_CMD_NORMAL, m_pCmd, 2);
}

int CAtlasMsg::PackCmdAction(int iAction, int iArgument)
{
	m_pCmd[0] = P_CMD_ACTION;
	m_pCmd[1] = iAction;
	m_pCmd[2] = iArgument;
	return Pack(P_CMD, P_CMD_NORMAL, m_pCmd, 3);
}

int CAtlasMsg::PackCmdSetVData(int iReg, const BYTE * pData, int iLength)
{
	if (iLength + 2 > P_MAX_PACKET_LENGTH)
	{
		return -1;
	}
	m_pCmd[0] = P_CMD_SET_VDATA;
	m_pCmd[1] = iReg;
	memcpy(m_pCmd + 2, pData, iLength);
	return Pack(P_CMD, P_CMD_NORMAL, m_pCmd, iLength + 2);
}

int CAtlasMsg::PackCmdGetVData(int iReg)
{
	m_pCmd[0] = P_CMD_GET_VDATA; 
	m_pCmd[1] = iReg;
	return Pack(P_CMD, P_CMD_NORMAL, m_pCmd, 2);
}

int CAtlasMsg::PackCmdWaitConnect()
{
    *m_pBuf = P_NUL;
    m_iLength = 1;
    return m_iLength;
}
int CAtlasMsg::GetCheckSum(const BYTE * pData, int iLength)
{
    int i;
    WORD iSum = 0;
    
    for (i = 0; i < iLength; i ++)
    {
        iSum += pData[i];
    }
    return (int)iSum;
}

BOOL CAtlasMsg::Unpack(BOOL & bDataError)
{
	CString temp;

	bDataError = FALSE;

	if (m_iLength < 1)		return FALSE;

	bDataError = TRUE;

	m_iType = m_pBuf[0];
	switch (m_iType)
	{
	case P_NUL:
	case P_ENQ:
	case P_ACK:
	case P_DC1:
	case P_NAK:
	case P_END:
		m_iLength --;
		if (m_iLength > 0)
		{
			memmove(m_pBuf, m_pBuf + 1, m_iLength);
		}
		return TRUE;

	case P_DATA_UNFINISHED:
		if (m_iLength > 1)
		{
			if (m_pBuf[1] != m_iUnpackSequence)
			{
				OutputDebugString("Sequence error in CAtlasMsg::Unpack()\n");
				m_iUnpackSequence = 0;
				break;
			}
			if (UnpackData(bDataError))
			{
				m_iUnpackSequence ++;
				return TRUE;
			}
			else
			{
				if (bDataError)
				{
//					m_iUnpackSequence = 0;
				}
				return FALSE;
			}
		}
		bDataError = FALSE;
		return FALSE;

	case P_DATA:
		if (m_iLength > 1)
		{
			if (m_pBuf[1] != m_iUnpackSequence)
			{
				OutputDebugString("Sequence error in CAtlasMsg::Unpack()\n");
				m_iUnpackSequence = 0;
				break;
			}
			if (UnpackData(bDataError))
			{
				m_iUnpackSequence = 0;
				return TRUE;
			}
			else
			{
				if (bDataError)
				{
//					m_iUnpackSequence = 0;
				}
				return FALSE;
			}
		}
		bDataError = FALSE;
		return FALSE;

	case P_CMD:
		if (m_iLength > 1)
		{
			if (m_pBuf[1] != P_CMD_FIRST && m_pBuf[1] != P_CMD_NORMAL)
			{
				break;
			}
			m_bFirstCmd = (m_pBuf[1] == P_CMD_FIRST);
			if (UnpackData(bDataError))
			{
				return UnpackCmd();
			}
			return FALSE;
		}
		bDataError = FALSE;
		return FALSE;

	default:
		temp.Format("Unknown msg type : %d\n", m_iType);
		OutputDebugString(temp);
		OutputDebugString("Unknown msg in CAtlasMsg::Unpack()\n");
		break;
	}
	m_iLength = 0;
	return FALSE;
}

BOOL CAtlasMsg::UnpackData(BOOL & bDataError)
{
	BYTE lo, hi;
	WORD w;
	BOOL bRet;
	int iSum, iLength;

	lo = m_pBuf[2];
	hi = m_pBuf[3];
	w = MAKEWORD(lo, hi);
	m_iVDataLength = iLength = w;

	if (m_iLength < iLength + 6)
	{
		bDataError = FALSE;	// Not fatal, probably it is because we have not receive the whole data packet
		return FALSE;
		OutputDebugString("Part messeage in CAtlasMsg::Unpack(), waiting for more for a complete packet\n");
	}
	if (iLength > P_MAX_PACKET_LENGTH)
	{
		OutputDebugString("Length overflow in CAtlasMsg::UnpackData()\n");
		m_iLength = 0;
		bDataError = TRUE;
		return FALSE;
	}

	iSum = GetCheckSum(m_pBuf + 4, iLength);
	lo = m_pBuf[iLength+4];
	hi = m_pBuf[iLength+5];
	w = MAKEWORD(lo, hi);
	if (iSum != w)
	{
		OutputDebugString("Check sum error in CAtlasMsg::UnpackData()\n");
		bDataError = TRUE;
		bRet = FALSE;
	}
	else
	{
		memcpy(m_pCmd, m_pBuf+4, iLength);
		bRet = TRUE;
	}

	iLength += 6;
	m_iLength -= iLength;
	if (m_iLength > 0)
	{
		memmove(m_pBuf, m_pBuf + iLength, m_iLength);
	}
	return bRet;
}

BOOL CAtlasMsg::UnpackCmd()
{
	m_iCmdCode = m_pCmd[0];
	m_iCmdReg = m_pCmd[1];

	switch (m_iCmdCode)
	{
    case P_CMD_GET_REG:
	case P_CMD_SET_REG:
/*		if (!m_bFirstCmd)
		{
			OutputDebugString("Second command\n");
		}
*/		if (m_iVDataLength != 6)
		{
			OutputDebugString("Length error in CAtlasMsg::UnpackCmd()\n");
			return FALSE;
		}
		memcpy(&m_iCmdVal, m_pCmd + 2, sizeof(int));
		break;

	case P_CMD_ACTION:
		if (m_iVDataLength != 3)
		{
			return FALSE;
		}
		m_iCmdVal = m_pCmd[2];
		break;

	case P_CMD_SET_VDATA:
		if (m_iVDataLength <= 2)
		{
			return FALSE;
		}
		m_iVDataLength -= 2;
		memmove(m_pCmd, m_pCmd + 2, m_iVDataLength);
		break;
  
    case P_CMD_GET_VDATA:
		if (m_iVDataLength != 2)
		{
			return FALSE;
		}
		break;

	default:
		return FALSE;
	}

	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -