Subversion Repositories shark

Rev

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