Subversion Repositories shark

Rev

Rev 376 | Rev 431 | Go to most recent revision | 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
 
376 giacomo 53
#define DELAY_LOOP 100000
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
 
77
  SYS_FLAGS f;
366 giacomo 78
 
79
  struct net_model m = net_base;
365 giacomo 80
 
81
  f = ll_fsave();
82
 
369 giacomo 83
  strcpy(local_ip,l_ip);
84
  strcpy(target_ip,t_ip);
85
 
370 giacomo 86
  if (flag) {
366 giacomo 87
 
370 giacomo 88
    net_setmode(m, TXTASK);
89
    net_setudpip(m, local_ip, "255.255.255.255");
90
 
91
    if (net_init(&m) != 1) {
92
      ll_frestore(f);
93
      return -1;
94
    }
95
 
96
    sleep(1);
97
 
369 giacomo 98
  }
366 giacomo 99
 
365 giacomo 100
  ip_str2addr(local_ip,&(local.s_addr));
371 giacomo 101
  local.s_port = TRACER_PORT;
365 giacomo 102
  socket = udp_bind(&local, NULL);
103
 
104
  ip_str2addr(target_ip,&(target.s_addr));
105
  target.s_port = TRACER_PORT;
369 giacomo 106
 
107
  sleep(1);
365 giacomo 108
 
109
  TracerOutputType = TRACER_UDP_OUTPUT;
110
  TracerUDPInit = 1;
111
 
112
  ll_frestore(f);
113
 
114
  return 0;
115
 
116
}
117
 
364 giacomo 118
int send_udp_event(void *p, int size) {
119
 
120
  static BYTE *current = pkt;
121
  static int events_number = 0;
122
  static int packet_number = 0;
123
 
124
  struct tracer_udp_header head = {"TRACER-V1.0",0,0,0};
125
  struct tracer_udp_header *phead = (struct tracer_udp_header *)(pkt);
126
 
376 giacomo 127
  int i;
128
 
364 giacomo 129
  if (total_pkt_size + size < UDP_MAXSIZE) {
130
 
131
    if (total_pkt_size == 0) {
132
 
133
      memcpy(pkt,&head,sizeof(struct tracer_udp_header));
134
 
135
      current = pkt;
136
      current += sizeof(struct tracer_udp_header);
137
 
138
      total_pkt_size += sizeof(struct tracer_udp_header);
139
      phead->total_size = total_pkt_size;
140
 
141
      packet_number++;
142
      phead->pkt_number = packet_number;
143
 
144
    }
145
 
146
    events_number++;
147
    phead->number_of_events = events_number;
148
 
149
    total_pkt_size += size;
150
    phead->total_size = total_pkt_size;
151
 
152
    //Copy the event
153
    memcpy(current,p,size);
154
    current += size;
155
 
156
    return 0;
157
 
158
  } else {
159
 
369 giacomo 160
    #ifdef TRACER_UDP_DEBUG
161
      cprintf("Packet Sent - Nr %d Size %d\n",packet_number,total_pkt_size);
162
    #endif
364 giacomo 163
 
376 giacomo 164
    if (TracerUDPInit == 1) {
369 giacomo 165
      udp_sendto(socket, pkt, total_pkt_size, &target);
376 giacomo 166
      for (i=0;i<DELAY_LOOP;i++);
167
    }
369 giacomo 168
 
364 giacomo 169
    events_number = 0;
170
    total_pkt_size = 0;
171
    current = pkt;
172
 
173
    send_udp_event(p,size);
174
 
365 giacomo 175
    return 0;    
364 giacomo 176
 
177
  }
178
 
179
  return -1;
180
 
181
}
369 giacomo 182
 
183
void send_remaining_udp_buffer() {
184
 
376 giacomo 185
  int i;
186
 
369 giacomo 187
  if (TracerUDPInit == 1 && total_pkt_size != 0) {
188
    #ifdef TRACER_UDP_DEBUG
189
      cprintf("Last Packet Sent - Size %d\n",total_pkt_size);
190
    #endif
191
    udp_sendto(socket, pkt, total_pkt_size, &target);
376 giacomo 192
    for (i=0;i<DELAY_LOOP;i++);
369 giacomo 193
  }
194
 
370 giacomo 195
}
196
 
197
//Sender Task
198
TASK udp_sender(void *arg)
199
{
200
 
201
  int number = (int)(arg);
202
 
203
  while(1) {
378 giacomo 204
 
205
    #ifdef TRACER_UDP_DEBUG
206
      cprintf("Send Task...\n");
207
    #endif
370 giacomo 208
 
209
    tracer_send_logged_events(number);  
210
 
211
    task_endcycle();
212
 
213
  }
214
 
215
}
216
 
373 giacomo 217
int tracer_create_udp_task(void *m, int EventsForPeriod)
370 giacomo 218
{
219
 
220
  if (m == NULL) {
221
 
222
    SOFT_TASK_MODEL st;
223
 
224
    soft_task_default_model(st);
225
    soft_task_def_arg(st,(void *)(EventsForPeriod));
378 giacomo 226
    soft_task_def_period(st,50000);
370 giacomo 227
    soft_task_def_met(st,10000);
228
 
229
    udp_task = task_create("UDP_Sender",udp_sender,&st,NULL);
230
    if (udp_task == NIL) {
231
      cprintf("Error creating UDP Sender\n");
232
      sys_end();
233
    }
234
 
235
    task_activate(udp_task);
236
 
237
  } else {
238
 
373 giacomo 239
    udp_task = task_create("UDP_Sender",udp_sender,(TASK_MODEL *)&m,NULL);
370 giacomo 240
    if (udp_task == NIL) {
241
      cprintf("Error creating UDP Sender\n");
242
      sys_end();
243
    }
244
 
245
    task_activate(udp_task);
246
 
247
  }
248
 
249
  return 0;
250
 
369 giacomo 251
}