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

📄 opentcp.c

📁 opentcp_mcf5282原代码
💻 C
字号:
/*********************************************************************************
File:			opentcp.c

Date:			22.1.2004

Version:		0.1

Author:			Jari Lahti (jari.lahti@violasystems.com)

Description:	General definions, bindings and includes of OpenTCP.
				Processor depended stuff like enable/disable IRQs.
				Helper functions.

Version Info:	22.1.2004 - First version (JaL)

*********************************************************************************/

#include "opentcp.h"


/* Our network interface	*/

struct netif localmachine;

/* TX buffer 				*/

UINT8 otcp_netbuf[NETWORK_TX_BUFFER_SIZE];	/* Network transmit buffer	*/

/********************************************************************************
Function:		otcp_strlen

Parameters:		UINT8* str - start address of string buffer
				UINT16 len - buffer length

Return val:		INT16 - (-1) Not a string
						(>=0) Length of string

Date:			12.8.2002

Desc:			Calculates the length of given string
*********************************************************************************/


INT16 otcp_strlen (UINT8* buf, UINT16 len)
{
	UINT16 i;

	for(i=0; i<len; i++)
	{
		if(*buf == '\0')
			return( i );

		buf++;
	}

	/* Not found	*/

	return(-1);

}


/********************************************************************************
Function:		otcp_bufsearch

Parameters:		UINT8* startadr - start address of given buffer
				UINT16 len - buffer length
				UINT8* str - given searchstring
				UINT8  caps - 0-actual(case sensitive), 1-case insensitive

Return val:		INT16 - (-1) Not found
						(>=0) Start of matched string from startadr

Date:			12.7.2002

Desc:			Seeks given string from given buffer
*********************************************************************************/

INT16 otcp_bufsearch (UINT8* startadr, UINT16 len, UINT8* str, UINT8 caps)
{
	UINT16 i;
	INT16 position;
	UINT8 matchesneeded;
	UINT8 matchesnow;
	UINT8* target;
	UINT8* key;

	target = startadr;
	position = -1;
	key = str;
	matchesnow = 0;
	matchesneeded = 0;

	/* How many matches we need?	*/

	while( *key++ != '\0' )
	{
		/* Break possible deadlock	*/

		matchesneeded++;
		if(matchesneeded > 30)
			return(-1);
	}

	/* Search for first mark and continue searching if found	*/

	key = str;

	for(i=0; i<len; i++)
	{
		if( ( (*target == *key) && (caps ==  0) ) ||
			( (otcp_toupper(*target) == otcp_toupper(*key)) && (caps == 1)) )
		{
			/* We found matching character		*/

			matchesnow++;

			/* Move to next character of key	*/

			key++;
			target++;

			if(matchesnow == 1)
			{
				/* First character match	*/

				position = i;
			}

			if(matchesneeded == matchesnow)
			{
				/* Whole string matched	*/

				return(position);
			}

		}

		else

		if( matchesnow != 0)
		{
			/* It wasn't a complete match...				*/
			/* Initialize counters and start again			*/

			matchesnow = 0;
			key = str;

			/* Move to next character of target after 		*/
			/* previous matching character					*/

			target = startadr;
			target += position;
			target += 1;

			i = position;
		}

		else
		{
			/* Just continue searching the first match		*/
			target++;
		}

	}

	/* No matches found...	*/

	return(-1);

}

/********************************************************************************
Function:		otcp_toupper

Parameters:		UINT8 ch - ASCII character to be converted UPPERCASE

Return val:		UINT8 - converted character

Date:			21.8.2002

Desc:			If ch is lowercase letter it is converted to UPPERCASE and
				returned. Otherwise original character is returned
*********************************************************************************/

UINT8 otcp_toupper (UINT8 ch)
{
	if( (ch < 123) && (ch > 96) )
		return(ch - 32);

	return(ch);

}

/********************************************************************************
Function:		otcp_tolower

Parameters:		UINT8 ch - ASCII character to be converted lowercase

Return val:		UINT8 - converted character

Date:			21.8.2002

Desc:			If ch is UPPERCASE letter it is converted to lowercase and
				returned. Otherwise original character is returned
*********************************************************************************/

UINT8 otcp_tolower (UINT8 ch)
{
	if( (ch < 91) && (ch > 64) )
		return(ch + 32);

	return(ch);

}


/* Convert ASCII character to numerical	*/
/* e.g. '1' -> 0x01, 'A' ->0x0A			*/

UINT8 otcp_asciitohex (UINT8 ch)
{
	if( (ch < 58) && (ch > 47) )
		return(ch - 48);

	if( (ch < 71 ) && (ch > 64) )
		return(ch - 55);

	return(0);
}

/* Take one byte and return its two ASCII 	*/
/* values for both nibbles					*/

UINT16 otcp_hextoascii (UINT8 c)
{
	UINT8 ch = c;
	UINT8 as1;
	UINT8 as2;

	/* take the char and turn it to ASCII */

	as1 = ch;
	as1 >>= 4;
	as1 &= 0x0F;
	if ( as1<10 )
		as1 += 48;
	else
		as1 += 55;

	as2 = ch;
	as2 &= 0x0F;
	if ( as2<10 )
		as2 += 48;
	else
		as2 += 55;

	return( ((UINT16)(as1)<<8) + as2 );


}

/* Is the given ASCII code numerical	*/
/* e.g. '0','1','2' ... '9'				*/

UINT8 otcp_isnumeric (UINT8 ch)
{
	if( (ch < 58) && (ch > 47) )
		return(1);
	return(0);
}


void otcp_ltoa (UINT32 nmbr, UINT8 *ch )
{
	/* Transforms value of long word to ASCII string */
	/* Makes it iterative						 	 */

	UINT16 multiple;
	UINT32 decade,comp;
	UINT8 i,found;

	/* Init String */

	for( i=0; i<10;i++ )
		*ch++ = '0';

	ch -= 10;

	/* See if Zero */

	if(nmbr == 0)
	{
		*ch++ = '0';
		*ch = '\0';
	}


	decade = 1000000000;

	found = FALSE;

	for( i=0; i<10; i++)
	{

		if(i != 0)
			decade /= 10;

		for( multiple=9; multiple>0; multiple--)
		{
			if( (i==0) && (multiple > 2) )
				continue;

			comp = decade * multiple;

			if(nmbr >= comp)
			{
				*ch = otcp_hextoascii(multiple);
				nmbr -= comp;
				found = TRUE;

				break;				/* Still processing */
			}
		}

		if( found == TRUE)
			ch++;

	}

	*ch = '\0';			/* EOL */

}

/* EOF	*/

⌨️ 快捷键说明

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