Subversion Repositories shark

Rev

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