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

📄 socket.c

📁 W3510A PPPoE code for network use
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * (c)COPYRIGHT
 * ALL RIGHT RESERVED
 *
 * FileName : socket.c
 * Revision History :
 * ----------  -------  -----------  ---------------------------------------------
 * Date        version  Name         Description
 * ----------  -------  -----------  ---------------------------------------------
 * 09/23/2005  1.0      Bong         Create version
 * ----------  -------  -----------  ---------------------------------------------
 * 10/12/2005  2.0      Woo          Release version
 * ----------  -------  -----------  ---------------------------------------------
 * 10/13/2005  2.0.1    Bong         modify sendto() function for UDP defection 
 * ----------  -------  -----------  ---------------------------------------------
 * 10/25/2005  2.0.2    Bong         modify connect(),sendto() function fix subnet bug.
 * ----------  -------  -----------  ---------------------------------------------
 * 11/03/2005  2.0.3    Bong         modify send() function fix send completion bug.
 * ----------  -------  -----------  ---------------------------------------------
 * 11/11/2005  2.0.4    Bong         #define _20051111_
 *                                   modify send() function fix send completion bug.  
 * ----------  -------  -----------  ---------------------------------------------
 * 
 * last update : 11/03/2005 
 */
#define _20051111_ // version 2.0.4

#include "socket.h"

#ifdef _20051111_
   #include "../mcu/delay.h" // for wait function
#endif


extern uint8 GW_MAC[6]; // gateway mac address defined in w3150a.c
extern uint8 GW_IP[6]; // gateway mac address defined in w3150a.c
extern uint8 is_gw_samenet;
uint16 local_port;

/**
* Internal Functions
*/

/**
 * \brief socket initialization
 * This function initializes a specified socket and waits until the W3150 has done.
 * \param s socket number
 * \param protocol the protocol for the socket
 * \param port the source port for the socket
 * \param flag the option for the socket
 * \return When succeeded : 1, failed :0
*/
uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag)
{
	uint8 ret;
#ifdef __DEF_IINCHIP_DBG__
	printf("socket()\r\n");
#endif
	if ((protocol == SOCK_STREAM) || (protocol == SOCK_DGRAM) || (protocol == SOCK_ICMPM) || (protocol == SOCK_IPL_RAWM) || (protocol == SOCK_MACL_RAWM) || (protocol == SOCK_PPPOEM))
	{
		ret = 1;
		if (IINCHIP_READ(SOCK_STATUS(s)) != SOCK_CLOSED) close(s);
		IINCHIP_WRITE(OPT_PROTOCOL(s),protocol | flag);
		if (port != 0) {
			IINCHIP_WRITE(SRC_PORT_PTR(s),(uint8)((port & 0xff00) >> 8));
			IINCHIP_WRITE((SRC_PORT_PTR(s) + 1),(uint8)(port & 0x00ff));
		} else {
			local_port++; // if don't set the source port, set local_port number.
			IINCHIP_WRITE(SRC_PORT_PTR(s),(uint8)((local_port & 0xff00) >> 8));
			IINCHIP_WRITE((SRC_PORT_PTR(s) + 1),(uint8)(local_port & 0x00ff));
		}
		IINCHIP_WRITE(COMMAND(s),CSOCKINIT);
	}
	else
	{
		ret = 0;
	}
#ifdef __DEF_IINCHIP_DBG__
		printf("SOCK_STATUS = %.2x , Protocol = %.2x\r\n", IINCHIP_READ(SOCK_STATUS(s)), IINCHIP_READ(OPT_PROTOCOL(s)));
		printf("socket() end ..\r\n");
#endif
	return ret;
}

/**
 * \brief close socket
 * This function is to close the socket.
 * \param s socket number
*/
void close(SOCKET s)
{
#ifdef __DEF_IINCHIP_DBG__
	printf("close()\r\n");
#endif
	IINCHIP_WRITE(COMMAND(s),CCLOSE);
#ifdef __DEF_IINCHIP_DBG__
	printf("close() end ..\r\n");
#endif
}

/**
 * \brief establish connection (passive)
 * This function waits for connection request from a peer. (TCP Server mode)
 * \param s socket number
 * \return When succeeded : 1, failed : 0
*/
uint8 listen(SOCKET s)
{
	uint8 ret;
#ifdef __DEF_IINCHIP_DBG__
	printf("listen()\r\n");
#endif
	if (IINCHIP_READ(SOCK_STATUS(s)) == SOCK_INIT)
	{
		ret = 1;
		IINCHIP_WRITE(COMMAND(s),CLISTEN);
	}
	else
	{
		ret = 0;
#ifdef __DEF_IINCHIP_DBG__
	printf("Fail[invalid ip,port]\r\n");
#endif
	}
#ifdef __DEF_IINCHIP_DBG__
	printf("listen() end ..\r\n");
#endif
	return ret;
}

/**
 * \brief establish connection (active)
 * This function establishs a connection to the peer,
 * and wait until the connection is established successfully. (TCP client mode)
 * \param s socket number
 * \param addr the peer's IP address
 * \param port the peer's port number
 * \return when succeeded : 1, failed : 0
*/
uint8 connect(SOCKET s, uint8 * addr, uint16 port)
{
	uint8 ret;
#ifdef __DEF_IINCHIP_DBG__
	printf("connect()\r\n");
#endif

	if 
		(
			((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
		 	((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
		 	(port == 0x00) 
		) 
 	{
 		ret = 0;
#ifdef __DEF_IINCHIP_DBG__
	printf("Fail[invalid ip,port]\r\n");
#endif
	}
	else
	{

		ret = 1;

      // for UDP defection
      if (issubnet(addr) == 1)
      {
        	// 2005.10.25 added fix subnet check error
        	if (is_gw_samenet == 0)
        	{
#ifdef __DEF_IINCHIP_DBG__
        	   printf("subnet defect");
#endif
           	IINCHIP_WRITE((GATEWAY_PTR + 0),addr[0]);
         	IINCHIP_WRITE((GATEWAY_PTR + 1),addr[1]);
         	IINCHIP_WRITE((GATEWAY_PTR + 2),addr[2]);
         	IINCHIP_WRITE((GATEWAY_PTR + 3),addr[3]);
         }
      }

		// set destination IP
		IINCHIP_WRITE(DST_IP_PTR(s),addr[0]);
		IINCHIP_WRITE((DST_IP_PTR(s) + 1),addr[1]);
		IINCHIP_WRITE((DST_IP_PTR(s) + 2),addr[2]);
		IINCHIP_WRITE((DST_IP_PTR(s) + 3),addr[3]);
		IINCHIP_WRITE(DST_PORT_PTR(s),(uint8)((port & 0xff00) >> 8));
		IINCHIP_WRITE((DST_PORT_PTR(s) + 1),(uint8)(port & 0x00ff));
		IINCHIP_WRITE(COMMAND(s),CCONNECT);
		// wait for completion
		while (IINCHIP_READ(COMMAND(s)))
		{
			if (IINCHIP_READ(SOCK_STATUS(s)) == SOCK_CLOSED)
			{
#ifdef __DEF_IINCHIP_DBG__
			printf("SOCK_CLOSED.\r\n");
#endif
				ret = 0; break;
			}
		}
     	// 2005.10.25 added fix subnet check error
     	if (is_gw_samenet == 0)
     	{
#ifdef __DEF_IINCHIP_DBG__
        	   printf("subnet defect");
#endif
        	IINCHIP_WRITE((GATEWAY_PTR + 0),GW_IP[0]);
      	IINCHIP_WRITE((GATEWAY_PTR + 1),GW_IP[1]);
      	IINCHIP_WRITE((GATEWAY_PTR + 2),GW_IP[2]);
      	IINCHIP_WRITE((GATEWAY_PTR + 3),GW_IP[3]);
      }
	}
#ifdef __DEF_IINCHIP_DBG__
	printf("connect() end ..\r\n");
#endif
	return ret;
}

/**
 * \brief close socket
 * This function is to close the connection of the socket.
 * \param s socket number
*/
void disconnect(SOCKET s)
{
#ifdef __DEF_IINCHIP_DBG__
	printf("disconnect()\r\n");
#endif
	IINCHIP_WRITE(COMMAND(s),CDISCONNECT);
#ifdef __DEF_IINCHIP_DBG__
	printf("disconnect() end ..\r\n");
#endif
}

/**
 * \brief send tcp data packet
 * This function sends TCP data.
 * \param s socket number
 * \param buf a pointer to data
 * \param len the data size to send
 * \return Succeed: sent data size, Failed:  0
*/
uint16 send(SOCKET s, const uint8 * buf, uint16 len)
{
	uint8 status=0;
	uint16 ret=0;
	uint16 freesize=0;

#ifdef _20051111_
	uint16 loop_cnt=0;
#endif

#ifdef __DEF_IINCHIP_DBG__
	printf("send()\r\n");
#endif

   if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
   else ret = len;

   // if freebuf is available, start.
   do 
   {
   	freesize = IINCHIP_READ(TX_FREE_SIZE_PTR(s));
   	freesize = (freesize<<8) + IINCHIP_READ(TX_FREE_SIZE_PTR(s)+1);
   	status = IINCHIP_READ(SOCK_STATUS(s));
		if ((status != SOCK_ESTABLISHED) && (status != SOCK_CLOSE_WAIT)){ret = 0; break;}
#ifdef __DEF_IINCHIP_DBG__
		printf("socket %d freesize(%d) empty or error\r\n", s, freesize);
#endif
   } while (freesize < ret);

   if (ret != 0)
   {
      // copy data

⌨️ 快捷键说明

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