Subversion Repositories shark

Rev

Rev 372 | 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
 
370 giacomo 44
#include <kernel/kern.h>
45
 
365 giacomo 46
#include <drivers/udpip.h>
47
 
372 giacomo 48
//#define TRACER_UDP_DEBUG
369 giacomo 49
 
371 giacomo 50
#define UDP_MAXSIZE 1000
51
#define TRACER_PORT 20000
364 giacomo 52
 
369 giacomo 53
BYTE pkt[UDP_MAXSIZE];
54
int total_pkt_size = 0;
55
 
365 giacomo 56
int TracerUDPInit = 0;
57
UDP_ADDR target,local;
369 giacomo 58
char local_ip[20],target_ip[20];
365 giacomo 59
int socket;
60
 
370 giacomo 61
PID udp_task;
62
 
365 giacomo 63
extern int TracerOutputType;
64
 
364 giacomo 65
struct tracer_udp_header {
66
  char id[12];
67
  unsigned int pkt_number;
68
  unsigned int number_of_events;
69
  unsigned int total_size;
70
};
71
 
370 giacomo 72
//Init UDP, if flag = 1 init the network driver
73
int tracer_init_udp(int flag, char *l_ip, char *t_ip) {
365 giacomo 74
 
75
  SYS_FLAGS f;
366 giacomo 76
 
77
  struct net_model m = net_base;
365 giacomo 78
 
79
  f = ll_fsave();
80
 
369 giacomo 81
  strcpy(local_ip,l_ip);
82
  strcpy(target_ip,t_ip);
83
 
370 giacomo 84
  if (flag) {
366 giacomo 85
 
370 giacomo 86
    net_setmode(m, TXTASK);
87
    net_setudpip(m, local_ip, "255.255.255.255");
88
 
89
    if (net_init(&m) != 1) {
90
      ll_frestore(f);
91
      return -1;
92
    }
93
 
94
    sleep(1);
95
 
369 giacomo 96
  }
366 giacomo 97
 
365 giacomo 98
  ip_str2addr(local_ip,&(local.s_addr));
371 giacomo 99
  local.s_port = TRACER_PORT;
365 giacomo 100
  socket = udp_bind(&local, NULL);
101
 
102
  ip_str2addr(target_ip,&(target.s_addr));
103
  target.s_port = TRACER_PORT;
369 giacomo 104
 
105
  sleep(1);
365 giacomo 106
 
107
  TracerOutputType = TRACER_UDP_OUTPUT;
108
  TracerUDPInit = 1;
109
 
110
  ll_frestore(f);
111
 
112
  return 0;
113
 
114
}
115
 
364 giacomo 116
int send_udp_event(void *p, int size) {
117
 
118
  static BYTE *current = pkt;
119
  static int events_number = 0;
120
  static int packet_number = 0;
121
 
122
  struct tracer_udp_header head = {"TRACER-V1.0",0,0,0};
123
  struct tracer_udp_header *phead = (struct tracer_udp_header *)(pkt);
124
 
125
  if (total_pkt_size + size < UDP_MAXSIZE) {
126
 
127
    if (total_pkt_size == 0) {
128
 
129
      memcpy(pkt,&head,sizeof(struct tracer_udp_header));
130
 
131
      current = pkt;
132
      current += sizeof(struct tracer_udp_header);
133
 
134
      total_pkt_size += sizeof(struct tracer_udp_header);
135
      phead->total_size = total_pkt_size;
136
 
137
      packet_number++;
138
      phead->pkt_number = packet_number;
139
 
140
    }
141
 
142
    events_number++;
143
    phead->number_of_events = events_number;
144
 
145
    total_pkt_size += size;
146
    phead->total_size = total_pkt_size;
147
 
148
    //Copy the event
149
    memcpy(current,p,size);
150
    current += size;
151
 
152
    return 0;
153
 
154
  } else {
155
 
369 giacomo 156
    #ifdef TRACER_UDP_DEBUG
157
      cprintf("Packet Sent - Nr %d Size %d\n",packet_number,total_pkt_size);
158
    #endif
364 giacomo 159
 
369 giacomo 160
    if (TracerUDPInit == 1)
161
      udp_sendto(socket, pkt, total_pkt_size, &target);
162
 
364 giacomo 163
    events_number = 0;
164
    total_pkt_size = 0;
165
    current = pkt;
166
 
167
    send_udp_event(p,size);
168
 
365 giacomo 169
    return 0;    
364 giacomo 170
 
171
  }
172
 
173
  return -1;
174
 
175
}
369 giacomo 176
 
177
void send_remaining_udp_buffer() {
178
 
179
  if (TracerUDPInit == 1 && total_pkt_size != 0) {
180
    #ifdef TRACER_UDP_DEBUG
181
      cprintf("Last Packet Sent - Size %d\n",total_pkt_size);
182
    #endif
183
    udp_sendto(socket, pkt, total_pkt_size, &target);
184
  }
185
 
370 giacomo 186
}
187
 
188
//Sender Task
189
TASK udp_sender(void *arg)
190
{
191
 
192
  int number = (int)(arg);
193
 
194
  while(1) {
195
 
196
    tracer_send_logged_events(number);  
197
    tracer_flush_sent_events();
198
 
199
    task_endcycle();
200
 
201
  }
202
 
203
}
204
 
373 giacomo 205
int tracer_create_udp_task(void *m, int EventsForPeriod)
370 giacomo 206
{
207
 
208
  if (m == NULL) {
209
 
210
    SOFT_TASK_MODEL st;
211
 
212
    soft_task_default_model(st);
213
    soft_task_def_arg(st,(void *)(EventsForPeriod));
214
    soft_task_def_period(st,500000);
215
    soft_task_def_met(st,10000);
216
 
217
    udp_task = task_create("UDP_Sender",udp_sender,&st,NULL);
218
    if (udp_task == NIL) {
219
      cprintf("Error creating UDP Sender\n");
220
      sys_end();
221
    }
222
 
223
    task_activate(udp_task);
224
 
225
  } else {
226
 
373 giacomo 227
    udp_task = task_create("UDP_Sender",udp_sender,(TASK_MODEL *)&m,NULL);
370 giacomo 228
    if (udp_task == NIL) {
229
      cprintf("Error creating UDP Sender\n");
230
      sys_end();
231
    }
232
 
233
    task_activate(udp_task);
234
 
235
  }
236
 
237
  return 0;
238
 
369 giacomo 239
}