예제 #1
0
파일: Frame.cpp 프로젝트: lubing521/edifice
ExtendedFrame* ExtendedFrame::BuildFrameFromBuff(const UCHAR* buff, const UCHAR len, BOOL recved, BOOL check_address)
{
	ExtendedFrame* frame = NULL;
	int addr_checked_pos = 0;
	int apdu_len, frame_expected_len;
	BOOL check_group_address;

	if ( len <= 22 || NULL == buff )
		goto _out;

	if ( 0 != ( *buff & 0x80 ) ) // 扩展帧的bit7位是0
	{
		goto _out;
	}

	// 处理扩展字段的逻辑.暂时木有。
	if ( buff[1] )
	{
		// ...
	}

	check_group_address = FALSE; // 是否把地址当作组地址看待. 
	if ( recved )
	{
		addr_checked_pos = 4;
		if ( buff[1] & 0x80 )
		{
			check_group_address = TRUE;
		}
	}
	else
	{
		addr_checked_pos = 2;
	}

	if ( check_address )
	{
		BOOL matched = FALSE;
		if ( !check_group_address )
		{
			const UCHAR* host_addr_bin = NULL;

			for ( IDevice* dev = ms_dev_manager->NextDevice(true);
				NULL != dev;
				dev = ms_dev_manager->NextDevice(false))
			{
				host_addr_bin = dev->GetHostAddr(NULL);
				if ( host_addr_bin && 0 == memcmp(host_addr_bin, buff + addr_checked_pos, 2) )
				{
					matched = TRUE;
					break;
				}
			}

			if ( !matched && 0 == memcmp(g_myconfig.GetBroadcastAddr(), buff + addr_checked_pos, 2) )
			{
				matched = TRUE;
			}
		}

		if ( check_group_address )
		{
			matched = ms_dev_manager->ExistThisGroupAddr(buff+addr_checked_pos);
		}

		if ( !matched )
		{
			goto _out;
		}
	}

	apdu_len = buff[6];
    frame_expected_len = 6 + 1 + apdu_len + 1;
	if ( frame_expected_len != len || buff[len-1] != xor_check_sum(buff, len-1) )
	{
		goto _out;
	}

	frame = new ExtendedFrame;
	frame->m_frame_len = len;
	frame->m_frame_mem = new UCHAR[len];
	memcpy(frame->m_frame_mem, buff, len);

_out:
	return frame;
}
예제 #2
0
파일: Frame.cpp 프로젝트: lubing521/edifice
StandardFrame* StandardFrame::BuildFrameFromBuff(const UCHAR* buff, UCHAR len, BOOL recved, BOOL check_address)
{
	StandardFrame* frame = NULL;
	int addr_checked_pos = 0;
	int apdu_len, frame_expected_len;
	BOOL check_group_address;
	
	if ( len > 22 || len <= 7 || NULL == buff )
		goto _out;

	if ( 0 == (*buff & 0x80) ) // 标准帧的bit7位是1
	{
		goto _out;
	}

	check_group_address = FALSE; // 是否把地址当作组地址看待. 
	if ( recved )
	{
		addr_checked_pos = 3;
		if ( buff[5] & 0x80 )
		{
			check_group_address = TRUE;
		}
	}
	else
	{
		addr_checked_pos = 1;
	}

	if ( check_address )  // 是否检查地址信息. 
	{
		BOOL matched = FALSE;	
		if ( !check_group_address )  // 检查单个地址. 
		{
			const UCHAR* host_addr_bin = NULL;
			for ( IDevice* dev = ms_dev_manager->NextDevice(true);
				NULL != dev;
				dev = ms_dev_manager->NextDevice(false))
			{
				host_addr_bin = dev->GetHostAddr(NULL); // 没有设置地址的设备有可能访问NULL.
				if ( host_addr_bin && 0 == memcmp(host_addr_bin, buff + addr_checked_pos, 2) )
				{
					matched = TRUE;
					break;
				}
			}

			// 和广播地址比较. 
			if ( !matched && 0 == memcmp(g_myconfig.GetBroadcastAddr(), buff + addr_checked_pos, 2) )
			{
				matched = TRUE;
			}
		}

		if ( check_group_address )
		{
			matched = ms_dev_manager->ExistThisGroupAddr(buff + addr_checked_pos);
		}

		if ( !matched )
		{
			goto _out;
		}
	}

	apdu_len = ( buff[5] & 0x0f );
	frame_expected_len = 5 + 1 + apdu_len + 1;  // 长度必须精确一致. 
	if ( frame_expected_len != len || buff[len-1] != xor_check_sum(buff, len-1) )
	{
		goto _out;
	}

	frame = new StandardFrame;
	frame->m_frame_len = len;
	frame->m_frame_mem = new UCHAR[len];
	memcpy(frame->m_frame_mem, buff, len);

_out:
	return frame;
}