📄 cobspackets.cpp
字号:
//--------------------------------------------------------------------------
//
// Filename: CobsPackets.cpp
//
// Description:
//
//--------------------------------------------------------------------------
// $Archive: /WirelessUSB/WUSB Kits/CY3635 N-to-1 DVK/DocSrc/CD_Root/Software/Nto1/common/CobsPackets/CobsPackets.cpp $
// $Modtime: 10/06/04 6:19p $
// $Revision: 1 $
//--------------------------------------------------------------------------
//
// Copyright 2004, Cypress Semiconductor Corporation.
//
// This software is owned by Cypress Semiconductor Corporation (Cypress)
// and is protected by and subject to worldwide patent protection (United
// States and foreign), United States copyright laws and international
// treaty provisions. Cypress hereby grants to licensee a personal,
// non-exclusive, non-transferable license to copy, use, modify, create
// derivative works of, and compile the Cypress Source Code and derivative
// works for the sole purpose of creating custom software in support of
// licensee product to be used only in conjunction with a Cypress integrated
// circuit as specified in the applicable agreement. Any reproduction,
// modification, translation, compilation, or representation of this
// software except as specified above is prohibited without the express
// written permission of Cypress.
//
// Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// Cypress reserves the right to make changes without further notice to the
// materials described herein. Cypress does not assume any liability arising
// out of the application or use of any product or circuit described herein.
// Cypress does not authorize its products for use as critical components in
// life-support systems where a malfunction or failure may reasonably be
// expected to result in significant injury to the user. The inclusion of
// Cypress' product in a life-support systems application implies that the
// manufacturer assumes all risk of such use and in doing so indemnifies
// Cypress against all charges.
//
// Use may be limited by and subject to the applicable Cypress software
// license agreement.
//
//--------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
// includes
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CobsPackets.h"
/////////////////////////////////////////////////////////////////////////////
// defines
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// global data
/////////////////////////////////////////////////////////////////////////////
// Register the standard CCobsPackets COM message
const UINT CCobsPackets::mg_nDefaultCobsMsg = ::RegisterWindowMessage(_T("CCobsPackets_DefaultCobsMsg"));
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// class CCobsPackets
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
CCobsPackets::CCobsPackets()
{
m_hMsgWnd = 0;
m_nCobsMsg = 0;
m_lParam = 0;
m_nMaxPacketSize = 0;
m_pInPacketData = NULL;
m_nInPacketPos = 0;
}
CCobsPackets::~CCobsPackets()
{
if (m_pInPacketData){
delete []m_pInPacketData;
m_pInPacketData = NULL;
}
while (!m_InPacketList.IsEmpty())
{
ListPacket *delPacket = (ListPacket *)m_InPacketList.RemoveTail();
if (delPacket)
{
if (delPacket->packetData){
delete [](delPacket->packetData);
delPacket->packetData = NULL;
}
delete delPacket;
}
}
while (!m_OutPacketList.IsEmpty())
{
ListPacket *delPacket = (ListPacket *)m_OutPacketList.RemoveTail();
if (delPacket)
{
if (delPacket->packetData){
delete [](delPacket->packetData);
delPacket->packetData = NULL;
}
delete delPacket;
}
}
}
bool CCobsPackets::Init(HWND hwndDest, UINT nCobsMsg, LPARAM lParam, UINT nMaxPacketSize)
{
m_hMsgWnd = hwndDest;
m_nCobsMsg = nCobsMsg?nCobsMsg:mg_nDefaultCobsMsg;
m_lParam = lParam;
m_nMaxPacketSize = nMaxPacketSize;
m_nInPacketPos = 0;
if (m_pInPacketData){
delete []m_pInPacketData;
m_pInPacketData = NULL;
}
m_pInPacketData = new unsigned char[m_nMaxPacketSize];
//TRACE0("create m_pInPacketData\n");
if (!m_pInPacketData)
return false;
return true;
}
ListPacket* CCobsPackets::NewPacket()
{
ListPacket *newPacket = NULL;
if (m_nMaxPacketSize)
{
newPacket = new ListPacket;
if (newPacket)
{
newPacket->packetData = new unsigned char[m_nMaxPacketSize];
if (newPacket->packetData)
{
newPacket->packetLength = 0;
memset(newPacket->packetData, 0x00, m_nMaxPacketSize);
}
else
{
delete newPacket;
newPacket = NULL;
}
}
}
return newPacket;
}
void CCobsPackets::SendEvent (EEvent eEvent, EError eError)
{
// Post message to the client window
::PostMessage(m_hMsgWnd,m_nCobsMsg,MAKEWPARAM(eEvent,eError),LPARAM(m_lParam));
}
void CCobsPackets::DataReceived(unsigned char *src, UINT length)
{
while (m_pInPacketData && length)
{
if (m_nInPacketPos < m_nMaxPacketSize)
{
if (*src == 0x00)
{
if (m_nInPacketPos)
{
ListPacket *decodePacket = NewPacket();
if (decodePacket)
{
decodePacket->packetLength = UnStuffData(m_pInPacketData, decodePacket->packetData, m_nInPacketPos);
m_InPacketList.AddTail(decodePacket);
decodePacket->packetCommand = *(decodePacket->packetData);
SendEvent(EEventReceivedPacket);
}
else
{
// Unable to decode Packet Error
SendEvent(EEventPacketError, EErrorPacketDecode);
}
}
else
{
// throw away zero-byte packets
}
memset(m_pInPacketData, 0x00, m_nMaxPacketSize);
m_nInPacketPos = 0;
}
else
{
m_pInPacketData[m_nInPacketPos++] = *src;
}
src++;
}
else
{
// Packet Overrun Error
SendEvent(EEventPacketError, EErrorPacketOverrun);
memset(m_pInPacketData, 0x00, m_nMaxPacketSize);
m_nInPacketPos = 0;
return;
}
length--;
}
}
void CCobsPackets::SendPacket(unsigned char *src, UINT length)
{
ListPacket *encodePacket = NewPacket();
if (encodePacket)
{
encodePacket->packetCommand = *src;
encodePacket->packetLength = StuffData(src, encodePacket->packetData, length);
m_OutPacketList.AddTail(encodePacket);
SendEvent(EEventSendPacket);
}
else
{
// Unable to decode Packet Error
SendEvent(EEventPacketError, EErrorPacketDecode);
}
}
BYTE CCobsPackets::GetPacket(EEvent eEvent, unsigned char *data, UINT *length)
{
ListPacket *pPacket = NULL;
BYTE packetCommand = 0;
switch (eEvent)
{
case EEventReceivedPacket:
if (!m_InPacketList.IsEmpty())
pPacket = (ListPacket *) m_InPacketList.RemoveHead();
break;
case EEventSendPacket:
if (!m_OutPacketList.IsEmpty())
pPacket = (ListPacket *) m_OutPacketList.RemoveHead();
break;
}
if (pPacket)
{
packetCommand = pPacket->packetCommand;
if (pPacket->packetData)
{
if (data && *length && pPacket->packetLength)
{
*length = (*length < pPacket->packetLength) ? *length : pPacket->packetLength;
memcpy(data, pPacket->packetData, *length);
}
else
{
*length = 0;
}
delete [](pPacket->packetData);
pPacket->packetData = NULL;
}
else
{
*length = 0;
}
delete pPacket;
}
return packetCommand;
}
#define FinishBlock(_x_) (*code_ptr = (_x_), \
code_ptr = dst++, \
code = 0x01 )
UINT CCobsPackets::StuffData(unsigned char *src, unsigned char *dst, UINT length)
{
unsigned char *dstStart = dst;
unsigned char *end = src + length;
unsigned char *code_ptr = dst++;
unsigned char code = 0x01;
while (src < end)
{
if (*src == 0) FinishBlock(code);
else
{
*dst++ = *src;
code++;
if (code == 0xFF) FinishBlock(code);
}
src++;
}
FinishBlock(code);
return (UINT)(dst - dstStart);
}
UINT CCobsPackets::UnStuffData(unsigned char *src, unsigned char *dst, UINT length)
{
unsigned char *dstStart = dst;
unsigned char *end = src + length;
while (src < end)
{
int i, code = *src++;
for (i=1; i<code; i++)
{
*dst++ = *src++;
}
if (code < 0xFF)
{
*dst++ = 0;
}
}
return (UINT)(dst - dstStart);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -