Čeština / English
Login

SVN Repository / Prohlížení

Aktuální adresář: FITkit / trunk / mcu / libs / enc28j60 /

enc28j60_udp.c

   1  /*******************************************************************************
   2  
   3     Copyright (C) 2010 Brno University of Technology,
   4                        Faculty of Information Technology
   5     Author(s): Martin Musil <xmusil34 AT fit.vutbr.cz>
   6  
   7     LICENSE TERMS
   8  
   9     Redistribution and use in source and binary forms, with or without
  10     modification, are permitted provided that the following conditions
  11     are met:
  12     1. Redistributions of source code must retain the above copyright
  13        notice, this list of conditions and the following disclaimer.
  14     2. Redistributions in binary form must reproduce the above copyright
  15        notice, this list of conditions and the following disclaimer in
  16        the documentation and/or other materials provided with the
  17        distribution.
  18     3. All advertising materials mentioning features or use of this software
  19        or firmware must display the following acknowledgement:
  20  
  21          This product includes software developed by the University of
  22          Technology, Faculty of Information Technology, Brno and its
  23          contributors.
  24  
  25     4. Neither the name of the Company nor the names of its contributors
  26        may be used to endorse or promote products derived from this
  27        software without specific prior written permission.
  28  
  29     This software or firmware is provided ``as is'', and any express or implied
  30     warranties, including, but not limited to, the implied warranties of
  31     merchantability and fitness for a particular purpose are disclaimed.
  32     In no event shall the company or contributors be liable for any
  33     direct, indirect, incidental, special, exemplary, or consequential
  34     damages (including, but not limited to, procurement of substitute
  35     goods or services; loss of use, data, or profits; or business
  36     interruption) however caused and on any theory of liability, whether
  37     in contract, strict liability, or tort (including negligence or
  38     otherwise) arising in any way out of the use of this software, even
  39     if advised of the possibility of such damage.
  40  
  41     $Id$
  42  
  43  *******************************************************************************/
  44  
  45  #include "enc28j60_udp.h"
  46  #include "enc28j60_udp.h"
  47  #include "enc28j60_spi.h"
  48  
  49  struct udp_socket udp_socket_table[SOCKET_COUNT];
  50  extern unsigned long local_ip;
  51  
  52  /**
  53  
  54   **/
  55  void udp_recv(struct ip_h* ip_header){
  56  
  57  	struct udp_h udp_header;
  58  
  59  	if(rx_read(&udp_header, UDP_HEADER_LEN) != UDP_HEADER_LEN)
  60  		return;
  61  
  62  	unsigned char i;
  63  	/*kontrola portu, volani obsluzne aplikace*/
  64  	//vyhledani existujiciho zaznamu
  65  	for(i=0; i<SOCKET_COUNT; i++){
  66  
  67  		if(udp_socket_table[i].port == NTOHS(udp_header.dest_port)){		//prepsani stavajiciho handleru
  68  			(*(udp_socket_table[i].app_handler))(ip_header, &udp_header);
  69  			return;
  70  		}
  71  	}
  72  return;
  73  }
  74  
  75  /**
  76  
  77   **/
  78  char udp_send(unsigned int dest_port,unsigned int src_port, unsigned long ip){
  79  /*
  80  	if(!link_up()){			//kabel odpojen,nebudeme odesilat
  81  		tx_unlock(TX1);
  82  		return -1;
  83  	}
  84  */
  85  	struct udp_h udp_header;
  86  
  87  	tx_seek(UDP_HEADER);
  88  
  89  	udp_header.src_port = HTONS(src_port);			//sestaveni hlavicky
  90  	udp_header.dest_port = HTONS(dest_port);
  91  	udp_header.length = HTONS(tx_data_len());
  92  	udp_header.checksum = 0x0000;
  93  
  94  	tx_write(&udp_header, UDP_HEADER_LEN);			//zapsani hlavicky s prazdnym checksumem
  95  
  96  	tx_seek(UDP_HEADER);
  97  	udp_header.checksum = HTONS( udp_checksum(ip,tx_data_len() ));	//vypocitani checksumu
  98  	tx_write(&udp_header, UDP_HEADER_LEN);			//prepsani hlavicky s platnym checksumem
  99  
 100  
 101  return ip_send(ip,IP_PROTO_UDP);
 102  }
 103  
 104  /**
 105  
 106   **/
 107  unsigned int udp_checksum(unsigned long dest_ip,int length){
 108  
 109  	unsigned long sum = 0;
 110  	unsigned int* ptr;
 111  
 112  	ptr = (unsigned int*)&local_ip;
 113  	sum += *ptr;
 114  	ptr++;
 115  	sum += *ptr;
 116  
 117  	ptr = (unsigned int*)&dest_ip;
 118  	sum += *ptr;
 119  	ptr++;
 120  	sum += *ptr;
 121  
 122  	sum += IP_PROTO_UDP;
 123  	sum += length;
 124  
 125  	sum += (unsigned int) ~tx_checksum();
 126  
 127  	while(sum >>16)
 128  		sum = (sum & 0xFFFF) + (sum >> 16);
 129  
 130  return (unsigned int) ~sum;
 131  }
 132  
 133  
 134  //*********************************************
 135  //fce pro praci s handlery k jednotlivym portum
 136  
 137  /**
 138  
 139   **/
 140  unsigned int udp_bind(unsigned int port,void* handler){
 141  
 142  	unsigned char i;
 143  
 144  	//vyhledani existujiciho zaznamu
 145  	for(i=0; i<SOCKET_COUNT; i++){
 146  		if(udp_socket_table[i].port == port){		//prepsani existujiciho handleru
 147  			udp_socket_table[i].app_handler = handler;
 148  			return port;
 149  		}
 150  	}
 151  
 152  	//hledani volneho mista v tabulce
 153  	for(i=0; i<SOCKET_COUNT; i++){
 154  		if(udp_socket_table[i].port == 0){			//zaregistrovani noveho portu
 155  			udp_socket_table[i].port = port;
 156  			udp_socket_table[i].app_handler = handler;
 157  			return port;
 158  		}
 159  	}
 160  
 161  return 0;
 162  }
 163  
 164  /**
 165  
 166   **/
 167  void udp_unbind(unsigned int port){
 168  
 169  	unsigned char i;
 170  	//vyhledani existujiciho zaznamu
 171  	for(i=0; i<SOCKET_COUNT; i++){
 172  		if(udp_socket_table[i].port == port){		//prepsani stavajiciho handleru
 173  			udp_socket_table[i].port = 0;
 174  			return;
 175  		}
 176  	}
 177  
 178  return;
 179  }
 180  
 181  
Zobrazeno: 683840x Naposledy: 5.7.2022 05:00:55