Rev 367 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
364 | giacomo | 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 | * Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /* |
||
20 | * Copyright (C) 2002 Paolo Gai |
||
21 | * |
||
22 | * This program is free software; you can redistribute it and/or modify |
||
23 | * it under the terms of the GNU General Public License as published by |
||
24 | * the Free Software Foundation; either version 2 of the License, or |
||
25 | * (at your option) any later version. |
||
26 | * |
||
27 | * This program is distributed in the hope that it will be useful, |
||
28 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
30 | * GNU General Public License for more details. |
||
31 | * |
||
32 | * You should have received a copy of the GNU General Public License |
||
33 | * along with this program; if not, write to the Free Software |
||
34 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
35 | * |
||
36 | */ |
||
37 | |||
38 | #include <ll/sys/types.h> |
||
39 | #include <stdlib.h> |
||
369 | giacomo | 40 | #include <string.h> |
365 | giacomo | 41 | #include <tracer.h> |
369 | giacomo | 42 | #include <unistd.h> |
364 | giacomo | 43 | |
365 | giacomo | 44 | #include <drivers/udpip.h> |
45 | |||
369 | giacomo | 46 | #define TRACER_UDP_DEBUG |
47 | |||
364 | giacomo | 48 | #define UDP_MAXSIZE 1400 |
365 | giacomo | 49 | #define TRACER_PORT 100 |
364 | giacomo | 50 | |
369 | giacomo | 51 | BYTE pkt[UDP_MAXSIZE]; |
52 | int total_pkt_size = 0; |
||
53 | |||
365 | giacomo | 54 | int TracerUDPInit = 0; |
55 | UDP_ADDR target,local; |
||
369 | giacomo | 56 | char local_ip[20],target_ip[20]; |
365 | giacomo | 57 | int socket; |
58 | |||
59 | extern int TracerOutputType; |
||
60 | |||
364 | giacomo | 61 | struct tracer_udp_header { |
62 | char id[12]; |
||
63 | unsigned int pkt_number; |
||
64 | unsigned int number_of_events; |
||
65 | unsigned int total_size; |
||
66 | }; |
||
67 | |||
369 | giacomo | 68 | int init_udp_tracer(char *l_ip, char *t_ip) { |
365 | giacomo | 69 | |
70 | SYS_FLAGS f; |
||
366 | giacomo | 71 | |
72 | struct net_model m = net_base; |
||
365 | giacomo | 73 | |
74 | f = ll_fsave(); |
||
75 | |||
369 | giacomo | 76 | strcpy(local_ip,l_ip); |
77 | strcpy(target_ip,t_ip); |
||
78 | |||
366 | giacomo | 79 | net_setmode(m, TXTASK); |
80 | net_setudpip(m, local_ip, "255.255.255.255"); |
||
81 | |||
369 | giacomo | 82 | if (net_init(&m) != 1) { |
83 | ll_frestore(f); |
||
366 | giacomo | 84 | return -1; |
369 | giacomo | 85 | } |
366 | giacomo | 86 | |
369 | giacomo | 87 | sleep(1); |
88 | |||
365 | giacomo | 89 | ip_str2addr(local_ip,&(local.s_addr)); |
369 | giacomo | 90 | local.s_port = TRACER_PORT+1; |
365 | giacomo | 91 | socket = udp_bind(&local, NULL); |
92 | |||
93 | ip_str2addr(target_ip,&(target.s_addr)); |
||
94 | target.s_port = TRACER_PORT; |
||
369 | giacomo | 95 | |
96 | sleep(1); |
||
365 | giacomo | 97 | |
98 | TracerOutputType = TRACER_UDP_OUTPUT; |
||
99 | TracerUDPInit = 1; |
||
100 | |||
101 | ll_frestore(f); |
||
102 | |||
103 | return 0; |
||
104 | |||
105 | } |
||
106 | |||
364 | giacomo | 107 | int send_udp_event(void *p, int size) { |
108 | |||
109 | static BYTE *current = pkt; |
||
110 | static int events_number = 0; |
||
111 | static int packet_number = 0; |
||
112 | |||
113 | struct tracer_udp_header head = {"TRACER-V1.0",0,0,0}; |
||
114 | struct tracer_udp_header *phead = (struct tracer_udp_header *)(pkt); |
||
115 | |||
116 | if (total_pkt_size + size < UDP_MAXSIZE) { |
||
117 | |||
118 | if (total_pkt_size == 0) { |
||
119 | |||
120 | memcpy(pkt,&head,sizeof(struct tracer_udp_header)); |
||
121 | |||
122 | current = pkt; |
||
123 | current += sizeof(struct tracer_udp_header); |
||
124 | |||
125 | total_pkt_size += sizeof(struct tracer_udp_header); |
||
126 | phead->total_size = total_pkt_size; |
||
127 | |||
128 | packet_number++; |
||
129 | phead->pkt_number = packet_number; |
||
130 | |||
131 | } |
||
132 | |||
133 | events_number++; |
||
134 | phead->number_of_events = events_number; |
||
135 | |||
136 | total_pkt_size += size; |
||
137 | phead->total_size = total_pkt_size; |
||
138 | |||
139 | //Copy the event |
||
140 | memcpy(current,p,size); |
||
141 | current += size; |
||
142 | |||
143 | return 0; |
||
144 | |||
145 | } else { |
||
146 | |||
369 | giacomo | 147 | #ifdef TRACER_UDP_DEBUG |
148 | cprintf("Packet Sent - Nr %d Size %d\n",packet_number,total_pkt_size); |
||
149 | #endif |
||
364 | giacomo | 150 | |
369 | giacomo | 151 | if (TracerUDPInit == 1) |
152 | udp_sendto(socket, pkt, total_pkt_size, &target); |
||
153 | |||
364 | giacomo | 154 | events_number = 0; |
155 | total_pkt_size = 0; |
||
156 | current = pkt; |
||
157 | |||
158 | send_udp_event(p,size); |
||
159 | |||
365 | giacomo | 160 | return 0; |
364 | giacomo | 161 | |
162 | } |
||
163 | |||
164 | return -1; |
||
165 | |||
166 | } |
||
369 | giacomo | 167 | |
168 | void send_remaining_udp_buffer() { |
||
169 | |||
170 | if (TracerUDPInit == 1 && total_pkt_size != 0) { |
||
171 | #ifdef TRACER_UDP_DEBUG |
||
172 | cprintf("Last Packet Sent - Size %d\n",total_pkt_size); |
||
173 | #endif |
||
174 | udp_sendto(socket, pkt, total_pkt_size, &target); |
||
175 | } |
||
176 | |||
177 | } |