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

📄 socket.c

📁 W3510A PPPoE code for network use
💻 C
📖 第 1 页 / 共 2 页
字号:
      send_data_processing(s, (uint8 *)buf, ret);
		IINCHIP_WRITE(COMMAND(s),CSEND);

		// wait for completion

		while (IINCHIP_READ(COMMAND(s)))
		{
      	status = IINCHIP_READ(SOCK_STATUS(s));
			if (status == SOCK_CLOSED)
			{
#ifdef __DEF_IINCHIP_DBG__
			printf("SOCK_CLOSED.\r\n");
#endif
				ret = 0; break;
			}
		}

#ifdef _20051111_
		while ((IINCHIP_READ(TX_RD_PTR(s)) != IINCHIP_READ(TX_WR_PTR(s))) || (IINCHIP_READ(TX_RD_PTR(s)+1) != IINCHIP_READ(TX_WR_PTR(s)+1)))
		{
      	status = IINCHIP_READ(SOCK_STATUS(s));
			if ((status == SOCK_CLOSED) || (loop_cnt++ == 20))
			{
 #ifdef __DEF_IINCHIP_DBG__
			   printf("socket %d is closed. loop_cnt=%d\r\n", s, loop_cnt);
 #endif
				close(s);
				ret = 0; break;
			}
			wait_10ms(200);
		}
#else
   // added 2005.11.02
		while ((IINCHIP_READ(TX_RD_PTR(s)) != IINCHIP_READ(TX_WR_PTR(s))) || (IINCHIP_READ(TX_RD_PTR(s)+1) != IINCHIP_READ(TX_WR_PTR(s)+1)))
		{
      	status = IINCHIP_READ(SOCK_STATUS(s));
			if (status == SOCK_CLOSED)
			{
				ret = 0; break;
			}
  		}
#endif
  	}
  	return ret;
}

/**
 * \brief receive tcp data packet
 * This function is to receive TCP data.
 * The recv() function is an application I/F function.
 * It continues to waits for as much data as the application wants to receive.
 * \param s socket number
 * \param buf a pointer to copy the data to be received
 * \param len the size of the data to read
 * \return Succeed: received data size, Failed:  -1
*/
uint16 recv(SOCKET s, uint8 * buf, uint16 len)
{
	uint16 ret=0;
#ifdef __DEF_IINCHIP_DBG__
	printf("recv()\r\n");
#endif
	if ( len > 0 )
	{
		recv_data_processing(s, buf, len);
		IINCHIP_WRITE(COMMAND(s),CRECV);
		ret = len;
	}
	return ret;
}

/**
 * \brief send udp data packet
 * This function sends UDP data.
 * The send() function is an application I/F function.
 * It continues to call the send_in() function to complete the sending of the data up to the size of the data to be sent
 * when the application is called. Unlike TCP transmission, The peer's destination address and the port is need.
 * \param s socket number
 * \param buf a pointer to data
 * \param len the data size to send
 * \param addr the peer's destination IP address
 * \param port the peer's destination port number
 * \return Succeed: sent data size, Failed:  -1
*/
uint16 sendto(SOCKET s, const uint8 * buf, uint16 len, uint8 * addr, uint16 port)
{
	uint8 local=0;
	uint8 status=0;
	uint8 isr=0;
	uint16 ret=0;
	
#ifdef __DEF_IINCHIP_DBG__
	printf("sendto()\r\n");
#endif
   if (len > getIINCHIP_TxMAX(s)) ret = getIINCHIP_TxMAX(s); // check size not to exceed MAX size.
   else ret = len;

	if
		(
		 	((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
		 	((port == 0x00)) ||(ret == 0)
		) 
 	{
 	   ;
#ifdef __DEF_IINCHIP_DBG__
	printf("%d Fail[%.2x.%.2x.%.2x.%.2x, %.d, %d]\r\n",s, addr[0], addr[1], addr[2], addr[3] , port, len);
	printf("Fail[invalid ip,port]\r\n");
#endif
	}
	else
	{
      // for UDP defection
      if ((local = issubnet(addr)) == 0)
      {
   		IINCHIP_WRITE(DST_HA_PTR(s),GW_MAC[0]);
   		IINCHIP_WRITE((DST_HA_PTR(s) + 1),GW_MAC[1]);
   		IINCHIP_WRITE((DST_HA_PTR(s) + 2),GW_MAC[2]);
   		IINCHIP_WRITE((DST_HA_PTR(s) + 3),GW_MAC[3]);
   		IINCHIP_WRITE((DST_HA_PTR(s) + 4),GW_MAC[4]);
   		IINCHIP_WRITE((DST_HA_PTR(s) + 5),GW_MAC[5]);
      }
      else
      {
        	// 2005.10.25 added fix subnet check error
        	if (is_gw_samenet == 0)
        	{
           	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]);
         }
      }
         
		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));

      // copy data
      send_data_processing(s, (uint8 *)buf, ret);

      // for UDP defection
		if (local == 0) IINCHIP_WRITE(COMMAND(s),CSENDMAC);
		else IINCHIP_WRITE(COMMAND(s),CSEND);
   
		while (IINCHIP_READ(COMMAND(s)))
		{
      	status = IINCHIP_READ(SOCK_STATUS(s));
      	isr = IINCHIP_READ(INT_STATUS(s));
			if ((status == SOCK_CLOSED) || (isr & ISR_TIMEOUT) || (getISR(s) & ISR_TIMEOUT))
			{
#ifdef __DEF_IINCHIP_DBG__
			printf("send fail.\r\n");
#endif
				ret = 0; break;
			}
		}
     	
     	// 2005.10.25 added fix subnet check error
     	if (is_gw_samenet == 0)
     	{
        	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]);
      }

	}
	return ret;
}

/**
 * \brief receive udp data packet
 * The function is to receive the UDP or IP layer RAW mode data, and handling the data header.
 * \param s socket number
 * \param buf a pointer to copy the data to be received
 * \param len the size of the data to read
 * \param addr a pointer to store the peer's IP address
 * \param port a pointer to store the peer's port number
 * \return Succeed: received data size, Failed:  -1
*/
uint16 recvfrom(SOCKET s, uint8 * buf, uint16 len, uint8 * addr, uint16 *port)
{
	uint8 head[8];
	uint16 data_len=0;
	uint16 total_len=0;
	uint16 ptr=0;
#ifdef __DEF_IINCHIP_DBG__
	printf("recvfrom()\r\n");
#endif

	if ( len > 0 )
	{
   	ptr = IINCHIP_READ(RX_RD_PTR(s));
   	ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(RX_RD_PTR(s) + 1);
#ifdef __DEF_IINCHIP_DBG__
   	printf("ISR_RX: rd_ptr : %.4x\r\n", ptr);
#endif
   	switch (IINCHIP_READ(OPT_PROTOCOL(s)) & 0x07)
   	{
   	case SOCK_DGRAM :
   			read_data(s, (uint8 *)ptr, head, 0x08);
   			ptr += 8;
   			// read peer's IP address, port number.
    			addr[0] = head[0];
   			addr[1] = head[1];
   			addr[2] = head[2];
   			addr[3] = head[3];
   			*port = head[4];
   			*port = (*port << 8) + head[5];
   			data_len = head[6];
   			data_len = (data_len << 8) + head[7];
   			
#ifdef __DEF_IINCHIP_DBG__
   			printf("UDP msg arrived\r\n");
   			printf("source Port : %d\r\n", *port);
   			printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
#endif
   			if (data_len > 1472 ) // invalid size -> correction error
				{
					total_len = IINCHIP_READ(RX_RECV_SIZE_PTR(s));
					total_len = (total_len << 8) + IINCHIP_READ(RX_RECV_SIZE_PTR(s) + 1);
#ifdef __DEF_IINCHIP_DBG__
 					printf("invalid data size : d=>%.4x, t=>%.4x", data_len, total_len);
#endif
					data_len = 0;
					ptr += total_len;
				}
   			else
				{
					read_data(s, (uint8 *)ptr, buf, data_len); // data copy.
					ptr += data_len;
				}
            IINCHIP_WRITE(RX_RD_PTR(s),(uint8)((ptr & 0xff00) >> 8));
            IINCHIP_WRITE((RX_RD_PTR(s) + 1),(uint8)(ptr & 0x00ff));
   			break;
   
   	case SOCK_IPL_RAWM :
   			read_data(s, (uint8 *)ptr, head, 0x06);
   			ptr += 6;
   
   			addr[0] = head[0];
   			addr[1] = head[1];
   			addr[2] = head[2];
   			addr[3] = head[3];
   			data_len = head[4];
   			data_len = (data_len << 8) + head[5];
   	
#ifdef __DEF_IINCHIP_DBG__
   			printf("IP RAW msg arrived\r\n");
   			printf("source IP : %d.%d.%d.%d\r\n", addr[0], addr[1], addr[2], addr[3]);
#endif
				read_data(s, (uint8 *)ptr, buf, data_len); // data copy.
				ptr += data_len;

            IINCHIP_WRITE(RX_RD_PTR(s),(uint8)((ptr & 0xff00) >> 8));
            IINCHIP_WRITE((RX_RD_PTR(s) + 1),(uint8)(ptr & 0x00ff));
   			break;
   
   	default :
   			break;
   	}
		IINCHIP_WRITE(COMMAND(s),CRECV);
	}
#ifdef __DEF_IINCHIP_DBG__
	printf("recvfrom() end ..\r\n");
#endif
 	return data_len;
}

⌨️ 快捷键说明

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