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

📄 w3150a.c

📁 W3510A PPPoE code for network use
💻 C
📖 第 1 页 / 共 2 页
字号:
	addr[3] = IINCHIP_READ(DST_HA_PTR(s)+3);
	addr[4] = IINCHIP_READ(DST_HA_PTR(s)+4);
	addr[5] = IINCHIP_READ(DST_HA_PTR(s)+5);
}
void getDestAddr(SOCKET s, uint8 * addr)
{
	addr[0] = IINCHIP_READ(DST_IP_PTR(s));
	addr[1] = IINCHIP_READ(DST_IP_PTR(s)+1);
	addr[2] = IINCHIP_READ(DST_IP_PTR(s)+2);
	addr[3] = IINCHIP_READ(DST_IP_PTR(s)+3);
}
void getDestPort(SOCKET s, uint8 * addr)
{
	addr[0] = IINCHIP_READ(DST_PORT_PTR(s));
	addr[1] = IINCHIP_READ(DST_PORT_PTR(s)+1);
}
/**
 * \brief Set MSS(Maximum Segment Size)
 * This function sets up the MSS value.
 * \param s channel
 * \param mss MSS value
 */
void setMSS(SOCKET s, uint16 mss)
{
	IINCHIP_WRITE(MSS(s),(uint8)((mss & 0xff00) >> 8));
	IINCHIP_WRITE((MSS(s) + 1),(uint8)(mss & 0x00ff));
}
/**
 * \brief Set IP-Protocol Field
 * This function sets up IP-Protocol field using ip raw mode.
 * \param s channel
 * \param proto value of IP-Protocol field
 */
void setIPprotocol(SOCKET s, uint8 proto)
{
	IINCHIP_WRITE(IP_PROTOCOL(s),proto);
}

/**
 * This function returns socket information.
 * socket status or Tx free buffer size or received data size
 * func 
 * SEL_CONTROL(0x00) -> return socket status 
 * SEL_SEND(0x01)    -> return Tx free buffer size
 * SEL_RECV(0x02)    -> return received data size
 */
uint16 select(SOCKET s, uint8 func)
{
	uint16 val=0,val1=0;

	switch (func)
	{
	case SEL_CONTROL :
		val = (uint16)IINCHIP_READ(SOCK_STATUS(s));
		break;
	case SEL_SEND :
		do
		{
			val1 = IINCHIP_READ(TX_FREE_SIZE_PTR(s));
			val1 = (val1 << 8) + IINCHIP_READ(TX_FREE_SIZE_PTR(s) + 1);
         		if(val1 != 0)
			{
	   			val = IINCHIP_READ(TX_FREE_SIZE_PTR(s));
	   			val = (val << 8) + IINCHIP_READ(TX_FREE_SIZE_PTR(s) + 1);
			}
			//if (val != val1) printf("diff recv len : [%.4x] [%.4x]", val, val1);
		} while (val != val1);
		break;
	case SEL_RECV :
   		do
   		{
   			val1 = IINCHIP_READ(RX_RECV_SIZE_PTR(s));
   			val1 = (val1 << 8) + IINCHIP_READ(RX_RECV_SIZE_PTR(s) + 1);
            		if(val1 != 0)
   			{
	      			val = IINCHIP_READ(RX_RECV_SIZE_PTR(s));
	      			val = (val << 8) + IINCHIP_READ(RX_RECV_SIZE_PTR(s) + 1);
   			}
   			//if (val != val1) printf("diff recv len : [%.4x] [%.4x]", val, val1);
   		} while (val != val1);
		break;
	default :
		val = 0;
		break;
	}
	return val;
}

/*
* data, pointer processing
*/
void send_data_processing(SOCKET s, uint8 *data, uint16 len)
{
	uint16 ptr;
	ptr = IINCHIP_READ(TX_WR_PTR(s));
	ptr = ((ptr & 0x00ff) << 8) + IINCHIP_READ(TX_WR_PTR(s) + 1);
	write_data(s, data, (uint8 *)(ptr), len);
	ptr += len;
	IINCHIP_WRITE(TX_WR_PTR(s),(uint8)((ptr & 0xff00) >> 8));
	IINCHIP_WRITE((TX_WR_PTR(s) + 1),(uint8)(ptr & 0x00ff));
}
/*
* data, pointer processing
*/
void recv_data_processing(SOCKET s, uint8 *data, uint16 len)
{
	uint16 ptr;
	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
	read_data(s, (uint8 *)ptr, data, len); // read data
	ptr += len;
	IINCHIP_WRITE(RX_RD_PTR(s),(uint8)((ptr & 0xff00) >> 8));
	IINCHIP_WRITE((RX_RD_PTR(s) + 1),(uint8)(ptr & 0x00ff));
}

/*
* write data from src to dst
*/
void write_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len)
{
	uint16 size;
	uint16 dst_mask;
	uint8 * dst_ptr;

	dst_mask = (uint16)dst & getIINCHIP_TxMASK(s);
	dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s) + dst_mask);
	
	if (dst_mask + len > getIINCHIP_TxMAX(s)) 
	{
		size = getIINCHIP_TxMAX(s) - dst_mask;
		wiz_write_buf((uint16)dst_ptr, (uint8*)src, size);
		src += size;
		size = len - size;
		dst_ptr = (uint8 *)(getIINCHIP_TxBASE(s));
		wiz_write_buf((uint16)dst_ptr, (uint8*)src, size);
	} 
	else
	{
		wiz_write_buf((uint16)dst_ptr, (uint8*)src, len);
	}
}
/*
* read data from src to dst
*/
void read_data(SOCKET s, vuint8 * src, vuint8 * dst, uint16 len)
{
	uint16 size;
	uint16 src_mask;
	uint8 * src_ptr;

	src_mask = (uint16)src & getIINCHIP_RxMASK(s);
	src_ptr = (uint8 *)(getIINCHIP_RxBASE(s) + src_mask);
	
	if( (src_mask + len) > getIINCHIP_RxMAX(s) ) 
	{
		size = getIINCHIP_RxMAX(s) - src_mask;
		wiz_read_buf((uint16)src_ptr, (uint8*)dst,size);
		dst += size;
		size = len - size;
		src_ptr = (uint8 *)(getIINCHIP_RxBASE(s));
		wiz_read_buf((uint16)src_ptr, (uint8*) dst,size);
	} 
	else
	{
		wiz_read_buf((uint16)src_ptr, (uint8*) dst,len);
	}
}


#ifdef __DEF_IINCHIP_PPP__

/*
* make PPPoE connection
* return :
* 1 => success to connect
* 2 => Auth fail
* 3 => timeout
* 4 => Auth type not support
*/
#define PPP_OPTION_BUF_LEN 64
uint8 pppinit_in(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen);

uint8 pppinit(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen)
{
	uint8 ret;
	// enable pppoe mode
	IINCHIP_WRITE(TMODE,IINCHIP_READ(TMODE) | TMODE_PPPOE);

	// open socket in pppoe mode
	IINCHIP_READ(INT_STATUS(0)); // first clear isr(0)
	IINCHIP_WRITE(OPT_PROTOCOL(0),SOCK_PPPOEM);
	IINCHIP_WRITE(COMMAND(0),CSOCKINIT);
	IINCHIP_WRITE(PPP_TIMEOUT,200); // 5sec timeout
	IINCHIP_WRITE(PPP_MAGIC,0x01); // magic number
	
	ret = pppinit_in(id, idlen, passwd, passwdlen);

	// close ppp connection socket
	IINCHIP_WRITE(COMMAND(0),CCLOSE);
	return ret;
}

uint8 pppinit_in(uint8 * id, uint8 idlen, uint8 * passwd, uint8 passwdlen)
{
	uint8 loop_idx = 0;
	uint8 isr = 0;
	uint8 buf[PPP_OPTION_BUF_LEN];
	uint16 len;
	uint8 str[PPP_OPTION_BUF_LEN];
	uint8 str_idx,dst_idx;

	printf("1. Get PPPoE server information ");
	// start to connect pppoe connection
	IINCHIP_WRITE(COMMAND(0),CPPPCON);
	wait_10ms(100);

	loop_idx = 0;
	while (!(IINCHIP_READ(INT_STATUS(0)) & ISR_PPP_NXT))
	{
		printf(".");
		if (loop_idx++ == 10) // timeout
		{
			printf("timeout before LCP\r\n"); 
			return 3;
		}
		wait_10ms(100);
	}
	printf("\r\n");

	printf("2. LCP mode : ");
	// send LCP Request
	{
		// Magic number option (kind+len+data)
	   // using PPP_MAGIC value
		buf[0] = 0x05; buf[1] = 0x06; buf[2] = 0x01; buf[3] = 0x01; buf[4] = 0x01; buf[5]= 0x01;
		// for MRU option, 1492 0x05d4  
		// buf[6] = 0x01; buf[7] = 0x04; buf[8] = 0x05; buf[9] = 0xD4;
	}
	send_data_processing(0, buf, 0x06);
	IINCHIP_WRITE(COMMAND(0),CPPPCR);
	wait_10ms(100);

	while (!((isr = IINCHIP_READ(INT_STATUS(0))) & ISR_PPP_NXT))
	{
		if (isr & ISR_PPP_RECV) // Not support option
		{
			len = select(0, SEL_RECV);
      	if ( len > 0 )
      	{
      		recv_data_processing(0, str, len);
      		IINCHIP_WRITE(COMMAND(0),CRECV);
   			// for debug
   			//printf("LCP proc\r\n"); for (i = 0; i < len; i++) printf ("%02x ", str[i]); printf("\r\n");
   			// get option length
   			len = str[4]; len = ((len & 0x00ff) << 8) + str[5];
   			len += 2;
   			str_idx = 6; dst_idx = 0; // ppp header is 6 byte, so starts at 6.
   			do 
   			{
   				if ((str[str_idx] == 0x01) || (str[str_idx] == 0x02) || (str[str_idx] == 0x03) || (str[str_idx] == 0x05))
   				{
   					// skip as length of support option. str_idx+1 is option's length.
   					str_idx += str[str_idx+1];
   				}
   				else
   				{
   					// not support option , REJECT
   					memcpy((uint8 *)(buf+dst_idx), (uint8 *)(str+str_idx), str[str_idx+1]);
   					dst_idx += str[str_idx+1]; str_idx += str[str_idx+1];
   				}
   				// for debug
   				//printf("s: %d, d: %d, l: %d\r\n", str_idx, dst_idx, len);
   			} while (str_idx != len);
   			// for debug
   			// printf("LCP dst proc\r\n"); for (i = 0; i < dst_idx; i++) printf ("%02x ", dst[i]); printf("\r\n");
   
   			// send LCP REJECT packet
   			send_data_processing(0, buf, dst_idx);
   			IINCHIP_WRITE(COMMAND(0),CPPPCJ);
      	}
		}
		printf(".");
		if (loop_idx++ == 10) // timeout
		{
			printf("timeout after LCP\r\n");
			return 3;
		}
		wait_10ms(100);
	}
	printf("ok\r\n");

	printf("3. Enter PPPoE Authentication mode %.2x %.2x: ", IINCHIP_READ(PPPAUTH), IINCHIP_READ(PPPAUTH+1));
	loop_idx = 0;
	if (IINCHIP_READ(PPPAUTH) == 0xc0 && IINCHIP_READ(PPPAUTH+1) == 0x23)
	{
		printf("PAP"); // in case of adsl normally supports PAP.
		// send authentication data
		// copy (idlen + id + passwdlen + passwd)
		buf[loop_idx] = idlen; loop_idx++;
		memcpy((uint8 *)(buf+loop_idx), (uint8 *)(id), idlen); loop_idx += idlen;
		buf[loop_idx] = passwdlen; loop_idx++;
		memcpy((uint8 *)(buf+loop_idx), (uint8 *)(passwd), passwdlen); loop_idx += passwdlen;
		send_data_processing(0, buf, loop_idx);
		IINCHIP_WRITE(COMMAND(0),CPPPCR);
		wait_10ms(100);
	}
	
#ifdef _20051121_ /* chap included */
	else if (IINCHIP_READ(PPPAUTH) == 0xc2 && IINCHIP_READ(PPPAUTH+1) == 0x23)
	{
		uint8 chal_len;
   	md5_ctx context;
   	uint8  digest[16];

		len = select(0, SEL_RECV);
   	if ( len > 0 )
   	{
   		recv_data_processing(0, str, len);
   		IINCHIP_WRITE(COMMAND(0),CRECV);
#ifdef __DEF_IINCHIP_DBG__
		printf("recv CHAP\r\n");
		int16 i;
		for (i = 0; i < 32; i++) printf ("%02x ", str[i]);
		printf("\r\n");
#endif
// str is C2 23 xx CHAL_ID xx xx CHAP_LEN CHAP_DATA
// index  0  1  2  3       4  5  6        7 ...

   		memset(buf,0x00,64);
   		buf[loop_idx] = str[3]; loop_idx++; // chal_id
   		memcpy((uint8 *)(buf+loop_idx), (uint8 *)(passwd), passwdlen); loop_idx += passwdlen; //passwd
   		chal_len = str[6]; // chal_id
   		memcpy((uint8 *)(buf+loop_idx), (uint8 *)(str+7), chal_len); loop_idx += chal_len; //challenge
		   buf[loop_idx] = 0x80;
#ifdef __DEF_IINCHIP_DBG__
		printf("CHAP proc d1\r\n");
		//int16 i;
		for (i = 0; i < 64; i++) printf ("%02x ", buf[i]);
		printf("\r\n");
#endif

   		md5_init(&context);
   		md5_update(&context, buf, loop_idx);
   		md5_final(digest, &context);

#ifdef __DEF_IINCHIP_DBG__
		printf("CHAP proc d1\r\n");
		for (i = 0; i < 16; i++) printf ("%02x", digest[i]);
		printf("\r\n");
#endif
   		loop_idx = 0;
   		buf[loop_idx] = 16; loop_idx++; // hash_len
     		memcpy((uint8 *)(buf+loop_idx), (uint8 *)(digest), 16); loop_idx += 16; // hashed value
     		memcpy((uint8 *)(buf+loop_idx), (uint8 *)(id), idlen); loop_idx += idlen; // id
   		send_data_processing(0, buf, loop_idx);
   		IINCHIP_WRITE(COMMAND(0),CPPPCR);
   		wait_10ms(100);
      }
   }
#endif

	else
	{
		printf("Not support\r\n");
#ifdef __DEF_IINCHIP_DBG__
		printf("Not support PPP Auth type: %.2x%.2x\r\n",IINCHIP_READ(PPPAUTH), IINCHIP_READ(PPPAUTH+1));
#endif
		return 4;
	}
	printf("\r\n");

	printf("4. Wait PPPoE server's admission : ");
	loop_idx = 0;
	while (!((isr = IINCHIP_READ(INT_STATUS(0))) & ISR_PPP_NXT))
	{
		if (isr & ISR_PPP_FAIL)
		{
			printf("failed\r\nReinput id, password..\r\n");
			return 2;
		}
		printf(".");
		if (loop_idx++ == 10) // timeout
		{
			printf("timeout after PAP\r\n");
			return 3;
		}
		wait_10ms(100);
	}
	printf("ok\r\n");

	printf("5. Get IP Address : ");
	// IP Address
	buf[0] = 0x03; buf[1] = 0x06; buf[2] = 0x00; buf[3] = 0x00; buf[4] = 0x00; buf[5] = 0x00;
	send_data_processing(0, buf, 6);
	IINCHIP_WRITE(COMMAND(0),CPPPCR);
	wait_10ms(100);

	loop_idx = 0;
	while (1)
	{
		if (IINCHIP_READ(INT_STATUS(0)) & ISR_PPP_RECV)
		{
			len = select(0, SEL_RECV);
      	if ( len > 0 )
      	{
      		recv_data_processing(0, str, len);
      		IINCHIP_WRITE(COMMAND(0),CRECV);
   			//for debug
   			//printf("IPCP proc\r\n"); for (i = 0; i < len; i++) printf ("%02x ", str[i]); printf("\r\n");
   			str_idx = 6; dst_idx = 0;
   			if (str[2] == 0x03) // in case of NAK
   			{
   				do 
   				{
   					if (str[str_idx] == 0x03) // request only ip information
   					{
   						memcpy((uint8 *)(buf+dst_idx), (uint8 *)(str+str_idx), str[str_idx+1]);
   						dst_idx += str[str_idx+1]; str_idx += str[str_idx+1];
   					}
   					else
   					{
   						// skip byte
   						str_idx += str[str_idx+1];
   					}
   					// for debug
   					//printf("s: %d, d: %d, l: %d", str_idx, dst_idx, len);
   				} while (str_idx != len);
   				send_data_processing(0, buf, dst_idx);
   				IINCHIP_WRITE(COMMAND(0),CPPPCR); // send ipcp request
   				wait_10ms(100);
   				break;
   			}
      	}
		}
		printf(".");
		if (loop_idx++ == 10) // timeout
		{
			printf("timeout after IPCP\r\n");
			return 3;
		}
		wait_10ms(100);
		send_data_processing(0, buf, 6);
		IINCHIP_WRITE(COMMAND(0),CPPPCR); //ipcp re-request
	}

	loop_idx = 0;
	while (!(IINCHIP_READ(INT_STATUS(0)) & ISR_PPP_NXT))
	{
		printf(".");
		if (loop_idx++ == 10) // timeout
		{
			printf("timeout after IPCP NAK\r\n");
			return 3;
		}
		wait_10ms(100);
		IINCHIP_WRITE(COMMAND(0),CPPPCR); // send ipcp request
	}
	printf("ok\r\n");

	return 1;
	// after this function, User must save the pppoe server's mac address and pppoe session id in current connection
}

/*
* terminate PPPoE connection
*/
uint8 pppterm(uint8 * mac, uint8 * sessionid)
{
	uint16 i;
	uint8 isr;
#ifdef __DEF_IINCHIP_DBG__
	printf("pppterm()\r\n");
#endif
	// enable pppoe mode
	IINCHIP_WRITE(TMODE,IINCHIP_READ(TMODE) | TMODE_PPPOE);
	
	// write pppoe server's mac address and session id 
	// must be setted these value.
	for (i = 0; i < 6; i++) IINCHIP_WRITE((DST_HA_PTR(0)+i),mac[i]);
	for (i = 0; i < 2; i++) IINCHIP_WRITE((DST_PORT_PTR(0)+i),sessionid[i]);
	isr = IINCHIP_READ(INT_STATUS(0));
	
	//open socket in pppoe mode
	IINCHIP_WRITE(OPT_PROTOCOL(0),SOCK_PPPOEM);
	IINCHIP_WRITE(COMMAND(0),CSOCKINIT);
	wait_1us(1);
	// close pppoe connection
	IINCHIP_WRITE(COMMAND(0),CPPPDISCON);
	wait_10ms(100);
	// close socket
	IINCHIP_WRITE(COMMAND(0),CCLOSE);

#ifdef __DEF_IINCHIP_DBG__
	printf("pppterm() end ..\r\n");
#endif

	return 1;
}
#endif

⌨️ 快捷键说明

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