Aktuální adresář: FITkit /
trunk /
mcu /
libs /
enc28j60 /
enc28j60_icmp.c
1 /*******************************************************************************
2 enc28j60_icmp.h: Implementace protokolu ICMP
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 #include "enc28j60_spi.h"
45 #include "enc28j60_ip.h"
46 #include "enc28j60_icmp.h"
47
48 ICMP_APP_HANDLER(*icmp_handler_);
49
50 unsigned int icmp_seq_num = 1;
51 unsigned char handler_set = 0;
52
53
54
55 void icmp_recv(struct ip_h* ip_header){
56
57 struct icmp_h icmp_header;
58
59 rx_read(&icmp_header,ICMP_HEADER_LEN);
60
61 if(handler_set == 1){
62 //volani handleru
63 (*(icmp_handler_))(ip_header, &icmp_header);
64 }
65 else{
66
67
68 //obsluha podle typu a kodu
69 switch(icmp_header.type){
70 case ICMP_ECHO: //echo pozadavek
71
72 if( tx_init(ICMP_PROTO) < 0)
73 return;
74
75 unsigned int len;
76 unsigned char temp[5]; //buffer, pres ktery se budou kopirovat data ECHO zpravy
77
78 while(rx_left() > 0){ //precte vsechny data a da je do odesilaciho bufferu
79 len = rx_read(&temp,5);
80 tx_write(&temp,len);
81 }
82 tx_close();
83
84 //odesle ICMP odpoved
85 icmp_send(NTOHL(ip_header->src_ip),ICMP_ECHO_REPLY, ICMP_ECHO_CODE, NTOHS(icmp_header.id), NTOHS(icmp_header.seq));
86 break;
87 }
88 }
89
90 return;
91 }
92
93 char icmp_send(unsigned long ip, unsigned char type, unsigned char code, unsigned int id, unsigned int sequence ){
94 /*
95 if(!link_up()){ //kabel odpojen,nebudeme odesilat
96 tx_unlock(TX2);
97 return -1;
98 }
99 */
100
101 struct icmp_h icmp_header;
102
103 icmp_header.type = type; //zapsani dat do hlavicky
104 icmp_header.code = code;
105 icmp_header.checksum = 0x0000;
106 icmp_header.id = HTONS(id);
107 icmp_header.seq = HTONS(sequence);
108
109 tx_seek(ICMP_HEADER);
110 tx_write(&icmp_header,ICMP_HEADER_LEN);
111
112 tx_seek(ICMP_HEADER);
113 icmp_header.checksum = HTONS(tx_checksum());
114
115 tx_write(&icmp_header,ICMP_HEADER_LEN);
116
117 return ip_send(ip,IP_PROTO_ICMP);
118 }
119
120
121 void icmp_bind(void* handler){
122
123 icmp_handler_ = handler;
124 handler_set = 1;
125 }
126
127 void icmp_unbind(){
128 handler_set = 0;
129 }
130