Rev 1616 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1616 | boinc | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Tullio Facchinetti <tullio.facchinetti@unipv.it> |
||
10 | * Luca M. Capisani <luca.capisani@unipv.it> |
||
11 | * |
||
12 | * Robotic Lab (Dipartimento di ingegneria informatica - Pavia - Italy) |
||
13 | * |
||
14 | * http://robot.unipv.it |
||
15 | * http://www.sssup.it |
||
16 | * http://retis.sssup.it |
||
17 | * http://shark.sssup.it |
||
18 | */ |
||
19 | |||
20 | /* |
||
21 | * udp_notify demo: this simple demo shows how to set up a simple UDP/IP |
||
22 | * packet handler function in order to avoid busy waits or task creation |
||
23 | * for the packet receiver function. |
||
24 | * usage: after compiling this demo, with the "make" command, start it with: |
||
25 | * x net <loc_ip> <loc_port> |
||
26 | * where <loc_ip> stands for the local ip of the receiver host and |
||
27 | * <loc_port> stands for the local UDP port where the receiver listens. |
||
28 | */ |
||
29 | |||
30 | /* |
||
31 | * This program is free software; you can redistribute it and/or modify |
||
32 | * it under the terms of the GNU General Public License as published by |
||
33 | * the Free Software Foundation; either version 2 of the License, or |
||
34 | * (at your option) any later version. |
||
35 | * |
||
36 | * This program is distributed in the hope that it will be useful, |
||
37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
39 | * GNU General Public License for more details. |
||
40 | * |
||
41 | * You should have received a copy of the GNU General Public License |
||
42 | * along with this program; if not, write to the Free Software |
||
43 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
44 | * |
||
45 | */ |
||
46 | |||
47 | #include <kernel/kern.h> |
||
48 | #include <drivers/udpip.h> |
||
49 | #include <string.h> //for strcpy() |
||
50 | #include <unistd.h> //for sleep() |
||
51 | #include "drivers/shark_keyb26.h" //for keyboard |
||
52 | |||
53 | |||
54 | //Received Packet Counter |
||
55 | long recvcount=0; |
||
56 | |||
57 | //The Internet Address of this Host |
||
58 | char local_ip[20]; |
||
59 | |||
60 | //A index for the receiver socket |
||
61 | int socket; |
||
62 | |||
63 | //Declaring a structure containing the receiver UDP port |
||
64 | UDP_ADDR local; |
||
65 | |||
66 | //UDP receiver handler: this function is called upon UDP packet received. |
||
67 | //No Busy waits are present. |
||
68 | int recvFun(int len, unsigned char *buff, void *p){ |
||
69 | |||
70 | recvcount++; |
||
71 | |||
72 | //Printing on screen the data contained, assuming that is text data |
||
73 | cprintf("Packet # %ld, len %d: %s.\n",recvcount, len,buff); |
||
74 | |||
75 | return 1; |
||
76 | } |
||
77 | |||
78 | /* Init the network stack */ |
||
79 | int init_network(char *local_ip) |
||
80 | { |
||
81 | //Declaring a network model and asigning the UDP/IP model; |
||
82 | struct net_model m = net_base; |
||
83 | |||
84 | |||
85 | //Setting up the UDP/IP stack, assigning a UDP/IP/Ethernet Network Model, |
||
86 | //a host and a network broadcast address. |
||
87 | net_setudpip(m,local_ip,"255.255.255.0"); |
||
88 | |||
89 | //Initializing the network stack |
||
90 | if (net_init(&m) != 1) { |
||
91 | cprintf("Network: Init Error.\n"); |
||
92 | return -1; |
||
93 | } |
||
94 | |||
95 | |||
96 | //Binding of the UDP port in the system network stack |
||
97 | //without assigning a peer IP-list in the second parameter |
||
98 | socket = udp_bind(&local, NULL); |
||
99 | |||
100 | //Assigning a receiver notify handler called on packed received |
||
101 | udp_notify(socket, &recvFun,NULL); |
||
102 | |||
103 | return 0; |
||
104 | |||
105 | } |
||
106 | |||
107 | void program_key_end(KEY_EVT* e) |
||
108 | { |
||
109 | exit(1); |
||
110 | } |
||
111 | |||
112 | |||
113 | int main(int argc, char **argv) |
||
114 | { |
||
115 | //Assigning a CTRL+C exit key shortcut |
||
116 | KEY_EVT k; |
||
117 | k.flag = CNTL_BIT; |
||
118 | k.scan = KEY_C; |
||
119 | k.ascii = 'c'; |
||
120 | k.status = KEY_PRESSED; |
||
121 | keyb_hook(k,program_key_end,FALSE); |
||
122 | |||
123 | if (argc==3){ |
||
124 | //Copy the input parameters values into the UDP/IP stack |
||
125 | strcpy(local_ip, argv[1]); |
||
126 | |||
127 | //A local UDP port number where the receiver listens |
||
128 | local.s_port=atoi(argv[2]); |
||
129 | }else{ |
||
130 | //The user has given a wrong number of parameters: |
||
131 | cprintf("S.Ha.R.K. udp_notify usage: x net <localIP> <localUDPport>\n"); |
||
132 | return(1); |
||
133 | } |
||
134 | |||
135 | cprintf("Initializing the network stack... \n"); |
||
136 | |||
137 | //Initializing the network settings |
||
138 | if (init_network(local_ip)) exit(1); |
||
139 | |||
140 | cprintf("Waiting for packets on port %d... \n",local.s_port); |
||
141 | |||
142 | //Waiting for packets |
||
143 | while(1){ |
||
144 | sleep(1); |
||
145 | } |
||
146 | |||
147 | |||
148 | return 0; |
||
149 | } |