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

📄 wlan_phoenix.c

📁 利用PIC单片机来驱动WLAN卡
💻 C
📖 第 1 页 / 共 5 页
字号:
	  packet[arp_tipaddr+i] = remoteipaddri[i];	//target ip addr
	  packet[arp_sipaddr+i] = ipaddri[i];		//source ip addr
	 } 
	bytecount += 8;
	bytecount +=6;
	packet[enetpacketLen11] = bytecount;
	packet[enetpacketLen03] = swapbytes(bytecount);
     rc = bap_write(TxFID,rxdatalength_offset,(char*)packet, packet[enetpacketLen11]+16);
	 rc=send_command(TransmitReclaim_Cmd,TxFID);
	 
     do_event_housekeeping();
    } 

   return(rc);
 }
//******************************************************************
//*	Perform ICMP Function
//*   This routine responds to a ping.
//******************************************************************
	unsigned int iphdrlen,ippktlen;	
    char tempcH,tempcL;

void icmp()
{
	char rc;
   //set echo reply
   packet[ICMP_typecode]=0x00;

   //clear the ICMP checksum
   packet[ICMP_cksum]=0x00;

   //setup the IP header
   setipaddrs();

   //calculate the ICMP checksum
   hdr_chksum =0;
   ippktlen=swapbytes(packet[ip_pktlen]);
   iphdrlen =(LOW_BYTE(packet[ip_vers_len]) & 0x0F) *4;
   	
   hdrlen = ippktlen - iphdrlen;
   addr = &packet[ICMP_typecode];
   cksum();
   packet[ICMP_cksum]= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
   //send the ICMP packet along on its way
   rc = echo_packet();
}
//******************************************************************
//*	init_udp Function
//*   This function sends UDP packet
//******************************************************************
void init_udp()
{

	udp_packet[enetpacketLen11] = 0x0036; 
   	udp_packet[enetpacketDest01]=remotemacaddri[0];
   	udp_packet[enetpacketDest23]=remotemacaddri[1];
   	udp_packet[enetpacketDest45]=remotemacaddri[2];
   	udp_packet[enetpacketSrc01]=macaddri[0];
   	udp_packet[enetpacketSrc23]=macaddri[1];
   	udp_packet[enetpacketSrc45]=macaddri[2];
	udp_packet[enetpacketLen03] = 0x3600;
	udp_packet[enetpacketSnap] = 0xAAAA;
	udp_packet[enetpacketCntrl] = 0x0003;
	udp_packet[enetpacketCntrl+1] = 0x0000;
	udp_packet[enetpacketType] = 0x0008;
	udp_packet[ip_vers_len] = 0x0045;
	udp_packet[ip_pktlen] = 0x2400;
	udp_packet[ip_id] = ++cntri;
	udp_packet[ip_frag_offset] = 0x0000;
	udp_packet[ip_ttlproto] = 0x1180;
	//upd_packet[ip_hdr_cksum] = 0x0000;
   	udp_packet[ip_srcaddr]=ipaddri[0];
   	udp_packet[ip_srcaddr+1]=ipaddri[1];
   	udp_packet[ip_destaddr]=remoteipaddri[0];
   	udp_packet[ip_destaddr+1]=remoteipaddri[1];
   	udp_packet[UDP_srcport] = make16(ee_parms[local_udpaddr_lo],ee_parms[local_udpaddr_hi]); //0x8813;
   	//udp_packet[UDP_destport] = 0x0900;
	udp_packet[UDP_len] = 0x1000;
	//udp_packet[UDP_cksum] = 0x0000;
	//udp_packet[UDP_data]   = 0x3231;
	//udp_packet[UDP_data+1] = 0x3433;
	//udp_packet[UDP_data+2] = 0x3635;
	//udp_packet[UDP_data+3] = 0x3837;
}

//******************************************************************
//*	send_udp Function
//*   This function sends UDP data
//******************************************************************
char send_udp(void)
{
	unsigned int TxFID;
	char rc;

   //this is all done before this datagram is sent
   //setup destination IP address
   //packet[ip_destaddr]=remoteipaddri[0];
   //packet[ip_destaddr+1]=remoteipaddri[1];
   //make phoenix module IP address source address
   //packet[ip_srcaddr]=ipaddri[0];
   //packet[ip_srcaddr+1]=ipaddri[1];
   //move hardware source address to destination address
   //packet[enetpacketDest01]=remotemacaddri[0];
   //packet[enetpacketDest23]=remotemacaddri[1];
   //packet[enetpacketDest45]=remotemacaddri[2];
   //make phoenix module mac address the source address
   //packet[enetpacketSrc01]=macaddri[0];
   //packet[enetpacketSrc23]=macaddri[1];
   //packet[enetpacketSrc45]=macaddri[2];
   
	// here's where your 8 bytes of data go..
	udp_packet[UDP_data]   = 0x3231;
	udp_packet[UDP_data+1] = 0x3433;
	udp_packet[UDP_data+2] = 0x3635;
	udp_packet[UDP_data+3] = 0x3837;

   //calculate the IP header checksum
   udp_packet[ip_hdr_cksum]=0x00;

   hdr_chksum =0;
   hdrlen = (LOW_BYTE(udp_packet[ip_vers_len]) & 0x0F) *4;
   addr = &udp_packet[ip_vers_len];
   cksum();
   udp_packet[ip_hdr_cksum]= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));

   	udp_packet[UDP_destport] = make16(ee_parms[remote_udpaddr_lo],ee_parms[remote_udpaddr_hi]);

   //calculate the UDP checksum
   udp_packet[UDP_cksum] = 0x00;
   hdr_chksum =0;
   hdrlen = 0x08;
   addr = &udp_packet[ip_srcaddr];
   cksum();
   hdr_chksum = hdr_chksum + ((256*HIGH_BYTE(udp_packet[ip_ttlproto]))+udp_packet[UDP_len]);
   hdrlen = swapbytes(udp_packet[UDP_len]);
   addr = &udp_packet[UDP_srcport];
   cksum();
   udp_packet[UDP_cksum]= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
      //echo the incoming data back to the VB program

	TxFID = get_free_TxFID();
	if(TxFID == 0)
	 rc = 1;
	else
	{
     rc = bap_write(TxFID,rxdatalength_offset,(char*)udp_packet,udp_packet[enetpacketLen11]+16);
	 if(rc=send_command(TransmitReclaim_Cmd,TxFID))
		printf("\r\nTransmit failed");
	 else
 
	  //printf("\r\nSending UDP Data Packet to UDP port %d..\r\n",make16(ee_parms[remote_udpaddr_hi],ee_parms[remote_udpaddr_lo]));
	 
     do_event_housekeeping();
	}
	return(rc);
 }
//******************************************************************
//*	send_udp_discard Function
//*   This function sends a dummy UDP data
//******************************************************************
char send_udp_discard(void)
{
	unsigned int TxFID;
	char rc;

	//printf("\r\nSending Dummy UDP Packet To Discard Port 9..");

   //this is all done before we get here	
   //setup destination IP address
   //packet[ip_destaddr]=remoteipaddri[0];
   //packet[ip_destaddr+1]=remoteipaddri[1];
   //make phoenix module IP address source address
   //packet[ip_srcaddr]=ipaddri[0];
   //packet[ip_srcaddr+1]=ipaddri[1];
   //move hardware source address to destination address
   //packet[enetpacketDest01]=remotemacaddri[0];
   //packet[enetpacketDest23]=remotemacaddri[1];
   //packet[enetpacketDest45]=remotemacaddri[2];
   //make phoenix module mac address the source address
   //packet[enetpacketSrc01]=macaddri[0];
   //packet[enetpacketSrc23]=macaddri[1];
   //packet[enetpacketSrc45]=macaddri[2];

	udp_packet[UDP_data]   = 0x3231;
	udp_packet[UDP_data+1] = 0x3433;
	udp_packet[UDP_data+2] = 0x3635;
	udp_packet[UDP_data+3] = 0x3837;

   //calculate the IP header checksum
   udp_packet[ip_hdr_cksum]=0x00;

   hdr_chksum =0;
   hdrlen = (LOW_BYTE(udp_packet[ip_vers_len]) & 0x0F) *4;
   addr = &udp_packet[ip_vers_len];
   cksum();
   udp_packet[ip_hdr_cksum]= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));

   udp_packet[UDP_destport] = 0x0900;
   //packet[UDP_srcport] = 0x8813;
   //calculate the UDP checksum
   udp_packet[UDP_cksum] = 0x00;
   hdr_chksum =0;
   hdrlen = 0x08;
   addr = &udp_packet[ip_srcaddr];
   cksum();
   hdr_chksum = hdr_chksum + ((256*HIGH_BYTE(udp_packet[ip_ttlproto]))+udp_packet[UDP_len]);
   hdrlen = swapbytes(udp_packet[UDP_len]);
   addr = &udp_packet[UDP_srcport];
   cksum();
   udp_packet[UDP_cksum]= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
      //echo the incoming data back to the VB program

	TxFID = get_free_TxFID();
	if(TxFID == 0)
	 rc = 1;
	else
	{
     rc = bap_write(TxFID,rxdatalength_offset,(char*)udp_packet,udp_packet[enetpacketLen11]+16);
	 if(rc=send_command(TransmitReclaim_Cmd,TxFID))
		printf("\r\nTransmit failed");
	 
     do_event_housekeeping();
	}
	return(rc);
 }
//******************************************************************
//*	UDP Function
//*   This function uses a Visual Basic UDP program to echo the
//*   data back to the VB program and set or reset PORT A bit 4
//*   under control of the VB program.
//******************************************************************
void udp()
{
	unsigned int temp;
	char rc;

   //port 7 is the well-known echo port
   if(packet[UDP_destport] == 0x0700)
   {
      //build the IP header
      setipaddrs();

      //swap the UDP source and destination ports
      temp = packet[UDP_srcport];
      packet[UDP_srcport] = packet[UDP_destport];
      packet[UDP_destport] = temp;

      //calculate the UDP checksum
      packet[UDP_cksum] = 0x00;

      hdr_chksum =0;
      hdrlen = 0x08;
      addr = &packet[ip_srcaddr];
      cksum();
      hdr_chksum = hdr_chksum + ((256*HIGH_BYTE(packet[ip_ttlproto]))+packet[UDP_len]);
      hdrlen = swapbytes(packet[UDP_len]);
      addr = &packet[UDP_srcport];
      cksum();
      packet[UDP_cksum]= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
      //echo the incoming data back to the VB program
      rc = echo_packet();
   }

   //buttons on the VB GUI are pointed towards port address 5000 decimal
   else if(packet[UDP_destport] == 0x8813)
	  {
      if(packet[UDP_data] == '0')
         //received a "0" from the VB program
         LATA5=0;
      else if(packet[UDP_data] == '1')
         //received a "1" from the VB program
         LATA5=1;
      else if(packet[UDP_data] == 0x00)
         //received a 0x00 from the VB program
         LATA=0x00;
       else if(packet[UDP_data] == 0x01)
         //received a 0x01 from the VB program
         LATA1=1;
      else if(packet[UDP_data] == 0x02)
         //received a 0x02 from the VB program
         LATA2=1;
      else if(packet[UDP_data] == 0x03)
         //received a 0x03 from the VB program
         LATA3=1;
      else if(packet[UDP_data] == 0x04)
         //received a 0x04 from the VB program
		 LATA4=1;

     }
    

}
//******************************************************************
//*	TELNET SERVER BANNER STATEMENT CONSTANT
//******************************************************************
char const telnet_banner[] = "\r\nPHOENIX>";
//******************************************************************
//*	Port Definitions
//*   This address is used by TCP and the Telnet function.
//*   This can be changed to any valid port number as long as
//*   you modify your code to recognize the new port number.
//******************************************************************
#define  MY_PORT_ADDRESS      0x981F  // 8088 DECIMAL
//******************************************************************
//*	Application Code
//*   Your application code goes here.
//******************************************************************
void application_code()
{
   char i,j,k;

   i=0;
   if(aux_data[i] == 0x0D && aux_data[i+1] == 0x0A)
   {
      tcpdatalen_out = 0x00;
      j = sizeof(telnet_banner);
	  tcpdatalen_out = j;         //length of the banner message
	  i=0;
	  k=0;
	  while(j > 1)
	  {
	   packet[TCP_data+k] = (telnet_banner[i+1] << 8) | telnet_banner[i];
		 i+=2;
		 ++k;
		 j-=2;
      }
	 while(j > 0)

	 {
	  packet[TCP_data+k] = telnet_banner[i];
	  --j;
	 }	  
   }
   else
      tcpdatalen_out = tcpdatalen_in;
}

//******************************************************************
//*	TCP Function
//*   This function uses TCP protocol to act as a Telnet server on
//*   port 8088 decimal.  The application function is called with
//*   every incoming character.
//******************************************************************
void tcp()
{
	char i,j,k;
	unsigned int temp;

   //assemble the destination port address from the incoming packet
   portaddr = packet[TCP_destport];

   //calculate the length of the data coming in with the packet
   tcpdatalen_in = (swapbytes(packet[ip_pktlen]))-((packet[ip_vers_len] & 0x0F) * 4)-(((packet[TCP_hdrflags] & 0x00FF) >> 4) * 4);

   //If an ACK is received and the destination port address is valid and no data is in the packet
   if(ACK_IN && portaddr == MY_PORT_ADDRESS && tcpdatalen_in == 0x00)
   {
      //assemble the acknowledgment number from the incoming packet
      incoming_ack =make32i(packet[TCP_acknum],packet[TCP_acknum+1]);

      //if the incoming packet is a result of session establishment
      if(flags.synflag)
      {
         //clear the SYN flag
         flags.synflag = 0;

         //the incoming acknowledgment is my new sequence number
         my_seqnum = incoming_ack;

         //send the Telnet server banner
         //limit the character count to 40 decimal
                j = sizeof(telnet_banner);
				tcpdatalen_out = j;         //length of the banner message
				i=0;
				k=0;
				do{
			       packet[TCP_data+k] = (telnet_banner[i+1] << 8) | telnet_banner[i];
				   i+=2;
				   ++k;
				  }while(j-- > 1);
			    do{
				   packet[TCP_data+k] = telnet_banner[i];
				  }while(j-- > 0);

         //expect to get an acknowledgment of the banner message
         expected_ack = my_seqnum + swapbytes(tcpdatalen_out);

         //send the TCP/IP packet
         send_tcp_packet();
      }
   }

   //if an ack is received and the port address is valid and there is data in the incoming packet
   if(ACK_IN && portaddr == MY_PORT_ADDRESS && tcpdatalen_in)
   {
      //receive the data and put it into the incoming data buffer
	  temp = tcpdatalen_in;
	  i=0;
      while(temp > 1)
	  {
	  	aux_data[i] = LOW_BYTE(packet[TCP_data+i]);
		aux_data[i+1] = HIGH_BYTE(packet[TCP_data+i]);
		i+=2;
		temp-=2;
	  }	
	  while(temp > 0)
	  {
	  	aux_data[i] = LOW_BYTE(packet[TCP_data+i]);
		--temp;
	  }	
         //run the TCP application
         application_code();

      //assemble the acknowledgment number from the incoming packet
      incoming_ack =make32i(packet[TCP_acknum],packet[TCP_acknum+1]);

      //check for the number of bytes acknowledged
      //determine how many bytes are outstanding and adjust the outgoing sequence number accordingly
      if(incoming_ack <= expected_ack)
         my_seqnum = expected_ack - (expected_ack - incoming_ack);

⌨️ 快捷键说明

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