Subversion Repositories shark

Rev

Rev 927 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
551 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>
1027 tullio 10
 *   Tullio Facchinetti  <tullio.facchinetti@unipv.it>
551 giacomo 11
 *   (see the web pages for full authors list)
12
 *
13
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
14
 *
15
 * http://www.sssup.it
16
 * http://retis.sssup.it
17
 * http://shark.sssup.it
1027 tullio 18
 *
19
 * Robotib Lab (University of Pavia)
20
 * http://robot.unipv.it
551 giacomo 21
 */
22
 
23
/*
24
 * This program is free software; you can redistribute it and/or modify
25
 * it under the terms of the GNU General Public License as published by
26
 * the Free Software Foundation; either version 2 of the License, or
27
 * (at your option) any later version.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32
 * GNU General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU General Public License
35
 * along with this program; if not, write to the Free Software
36
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37
 */
38
 
39
#include <ll/sys/types.h>
40
#include <stdlib.h>
41
#include <string.h>
42
#include <tracer.h>
43
#include <unistd.h>
44
 
45
#include <FTrace_udp.h>
46
#include <FTrace_OSD.h>
47
#include <FTrace_chunk.h>
48
 
49
#include <kernel/kern.h>
50
 
51
#include <drivers/udpip.h>
52
 
907 mauro 53
//#define TRACER_UDP_DEBUG
551 giacomo 54
 
55
#define UDP_MAXSIZE 1000
56
#define TRACER_PORT 20000
57
 
778 giacomo 58
#define DELAY_USEC 5000
551 giacomo 59
 
60
BYTE pkt[UDP_MAXSIZE];
61
int total_pkt_size = 0;
62
 
63
int TracerUDPInit = 0;
64
UDP_ADDR target,local;
65
char local_ip[20],target_ip[20];
66
int socket;
67
 
68
PID udp_task;
69
 
552 giacomo 70
int sending_flag = 0;
71
 
551 giacomo 72
struct FTrace_udp_header {
73
  char id[12];
74
  unsigned int pkt_number;
75
  unsigned int number_of_events;
76
  unsigned int total_size;
77
};
78
 
1027 tullio 79
/**
80
 * Initialize the Tracer chunk sender through the network using the UDP
81
 * protocol supported by S.Ha.R.K.
82
 * If flag = 1 initializes the network driver, otherwise it considers
83
 * that the network layer has already been initialized.
84
 * It also sets the internal chunk sender to the function that initializes
85
 * the task for sending the chunk.
86
 */
551 giacomo 87
int FTrace_OSD_init_udp(int flag, char *l_ip, char *t_ip) {
88
 
89
  struct net_model m = net_base;
1027 tullio 90
 
91
  FTrace_set_internal_chunk_sender(FTrace_OSD_create_udp_task);  // Tool
551 giacomo 92
 
93
  strcpy(local_ip,l_ip);
94
  strcpy(target_ip,t_ip);
95
 
96
  if (flag) {
97
 
98
    net_setudpip(m, local_ip, "255.255.255.255");
99
 
100
    if (net_init(&m) != 1) {
101
      return -1;
102
    }
103
 
104
  }
105
 
106
  ip_str2addr(local_ip,&(local.s_addr));
107
  local.s_port = TRACER_PORT;
108
  socket = udp_bind(&local, NULL);
109
 
110
  ip_str2addr(target_ip,&(target.s_addr));
111
  target.s_port = TRACER_PORT;
112
 
113
  sleep(1);
114
 
115
  TracerUDPInit = 1;
116
 
117
  return 0;
118
 
119
}
120
 
121
int send_udp_event(void *p, int size) {
122
 
123
  static BYTE *current = pkt;
124
  static int events_number = 0;
125
  static int packet_number = 0;
126
 
127
  struct FTrace_udp_header head = {"TRACER-V1.0",0,0,0};
128
  struct FTrace_udp_header *phead = (struct FTrace_udp_header *)(pkt);
129
 
130
  if ((total_pkt_size + size < UDP_MAXSIZE) && p != NULL ) {
131
 
132
    if (total_pkt_size == 0) {
133
 
134
      memcpy(pkt,&head,sizeof(struct FTrace_udp_header));
135
 
136
      current = pkt;
137
      current += sizeof(struct FTrace_udp_header);
138
 
139
      total_pkt_size += sizeof(struct FTrace_udp_header);
140
      phead->total_size = total_pkt_size;
141
 
142
      packet_number++;
143
      phead->pkt_number = packet_number;
144
 
145
    }
146
 
147
    events_number++;
148
    phead->number_of_events = events_number;
149
 
150
    total_pkt_size += size;
151
    phead->total_size = total_pkt_size;
152
 
153
    //Copy the event
154
    memcpy(current,p,size);
155
    current += size;
156
 
157
    return 0;
158
 
159
  } else {
160
 
161
    #ifdef TRACER_UDP_DEBUG
778 giacomo 162
      cprintf(".");
551 giacomo 163
    #endif
164
 
165
    if (TracerUDPInit == 1) {
166
      udp_sendto(socket, pkt, total_pkt_size, &target);
778 giacomo 167
      usleep(DELAY_USEC);
551 giacomo 168
    }
169
 
170
    events_number = 0;
171
    total_pkt_size = 0;
172
    current = pkt;
173
 
174
    if (p != NULL) send_udp_event(p,size);
175
 
176
    return 0;    
177
 
178
  }
179
 
180
  return -1;
181
 
182
}
183
 
184
//Sender Task
185
TASK udp_sender(void *arg)
186
{
187
  FTrace_Chunk_Ptr c = (FTrace_Chunk_Ptr)(arg);
188
 
189
  DWORD total_size = (DWORD)(c->size) + (DWORD)(c->emergency_size);
190
  DWORD start = (DWORD)(c->osd + FTRACE_OSD_CHUNK_HEAD);
191
  DWORD current = start;
192
  DWORD count = 0;  
193
 
194
  while (current+16 <= start+total_size) {
195
 
561 giacomo 196
        if (*(WORD *)(current) != 0) {
551 giacomo 197
                send_udp_event((void *)(current), 16);
198
                count++;
199
        }
200
 
201
        current += 16;
202
 
203
  }
204
 
205
  // Flush the buffer out
206
  send_udp_event(NULL, 0);
1027 tullio 207
 
208
  FTrace_chunck_output_end();  // Tool
551 giacomo 209
 
210
  #ifdef TRACER_UDP_DEBUG
778 giacomo 211
    cprintf("\nTotal Chunk Event Sent: %d\n",(int)count);
551 giacomo 212
  #endif  
213
 
214
  return 0;  
215
 
216
}
217
 
218
int FTrace_OSD_create_udp_task(FTrace_Chunk_Ptr c)
219
{
220
 
221
  NRT_TASK_MODEL st;
222
 
1027 tullio 223
  FTrace_chunck_output_start();   // Tool
552 giacomo 224
 
551 giacomo 225
  nrt_task_default_model(st);
226
  nrt_task_def_arg(st,(void *)c);
227
 
228
  udp_task = task_create("UDP_Sender",udp_sender,&st,NULL);
229
  if (udp_task == NIL) {
230
    cprintf("Error creating UDP Sender\n");
927 pj 231
    exit(1);
551 giacomo 232
  }
233
 
234
  task_activate(udp_task);
235
 
236
  return 0;
237
 
238
}