📄 common.cpp
字号:
// NAME: common.cpp
//--------------------------------------------------------------------------------------------------
//
// GENERAL DESCRIPTION
//
// This module provides the implementation of the common functions for all other procedures.
//--------------------------------------------------------------------------------------------------
//
// Revision History:
// Modification Tracking
// Author Date Number Description of Changes
// ------------------------- ------------ ---------- ----------------------------------------
//
// Portability:
// This module can be portable to other C++ compilers or Win32 platforms.
//
//--------------------------------------------------------------------------------------------------
// INCLUDE FILES
//--------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "common.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//--------------------------------------------------------------------------------------------------
// GLOBAL FUNCTIONS
//--------------------------------------------------------------------------------------------------
// FUNCTION
// add_byte_to_packet
// DESCRIPTION:
// This function adds a single byte to a packet that is being built dynamically. It takes care
// of byte stuffing and checks for buffer overflow.
// This function is a helper function for the packet building macros and should not be called
// directly.
// ARGUMENTS PASSED:
// pkt the buffer which the specified byte is added to;
// val the value which will be added to the buffer;
// RETURN VALUE:
// None
//
// PRE-CONDITIONS:
// None
//
// POST-CONDITIONS:
// None
//
// IMPORTANT NOTES:
// None
//--------------------------------------------------------------------------------------------------
void add_byte_to_packet(pkt_buffer_type *pkt, const BYTE val)
{
// Whether the packet is broken already,
if (pkt->broken != FALSE)
{
return;
}
// Check if the byte needs escaping for transparency.
if (val == ASYNC_HDLC_FLAG || val == ASYNC_HDLC_ESC)
{
// Check for an impending overflow.
if (pkt->length+2+ROOM_FOR_CRC+ROOM_FOR_FLAG >= MAX_PACKET_LEN)
{
pkt->broken = TRUE;
return;
}
// No overflow. Escape the byte into the buffer.
pkt->buf[pkt->length++] = ASYNC_HDLC_ESC;
pkt->buf[pkt->length++] = val ^ (BYTE)ASYNC_HDLC_ESC_MASK;
}
else
{
// Check for an impending overflow.
if (pkt->length+1+ROOM_FOR_CRC+ROOM_FOR_FLAG >= MAX_PACKET_LEN)
{
// Overflow. Mark this packet broken.
pkt->broken = TRUE;
return;
}
// No overflow. Place the byte into the buffer.
pkt->buf[pkt->length++] = val;
}
}
//--------------------------------------------------------------------------------------------------
// FUNCTION
// finish_building_packet
// DESCRIPTION:
// This function completes the process of building a packet dynamically.If all is well, it adds
// the CRC and a trailing flag to the buffer.If an error has been encountered in building the
// packet, it substitutes a NAK packet for whatever has been built.
//
// This function is a helper function for the packet building macros and should not be called
// directly.
// ARGUMENTS PASSED:
// None
// RETURN VALUE:
// None
// PRE-CONDITIONS:
// None
// POST-CONDITIONS:
// None
// IMPORTANT NOTES:
// None
//--------------------------------------------------------------------------------------------------
void finish_building_packet(pkt_buffer_type *pkt)
{
// Cyclic Redundancy Check for the packet we've built.
WORD crc;
if (pkt->broken == FALSE)
{
// Compute the CRC for all the bytes in the packet.
crc = CRC_16_L_SEED;
for (WORD i=0; i < pkt->length; i++)
{
if(pkt->buf[i] == ASYNC_HDLC_ESC)
{
i++;
WORD tmp = pkt->buf[i] ^ (BYTE)ASYNC_HDLC_ESC_MASK;
crc = CRC_16_L_STEP(crc, tmp);
}
else
crc = CRC_16_L_STEP(crc, (WORD) pkt->buf[i]);
}
crc ^= CRC_16_L_SEED;
// Add the CRC to the packet
ADD_CRC_TO_PACKET(pkt,crc);
// Add a flag to the packet.This can't use the regular add_byte_to_packet()
// function because it's a flag.
pkt->buf[pkt->length] = ASYNC_HDLC_FLAG;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -