Subversion Repositories shark

Rev

Rev 441 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 2002 Paolo Gai
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


#include <ll/sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <tracer.h>
#include <unistd.h>

#include <kernel/kern.h>

#include <drivers/udpip.h>

//#define TRACER_UDP_DEBUG

#define UDP_MAXSIZE 1000
#define TRACER_PORT 20000

#define DELAY_LOOP 10000000

BYTE pkt[UDP_MAXSIZE];
int total_pkt_size = 0;

int TracerUDPInit = 0;
UDP_ADDR target,local;
char local_ip[20],target_ip[20];
int socket;

PID udp_task;

extern int TracerOutputType;

struct tracer_udp_header {
  char id[12];
  unsigned int pkt_number;
  unsigned int number_of_events;
  unsigned int total_size;
};

//Init UDP, if flag = 1 init the network driver
int tracer_init_udp(int flag, char *l_ip, char *t_ip) {

  struct net_model m = net_base;

  strcpy(local_ip,l_ip);
  strcpy(target_ip,t_ip);

  if (flag) {

    net_setudpip(m, local_ip, "255.255.255.255");

    if (net_init(&m) != 1) {
      return -1;
    }

  }

  ip_str2addr(local_ip,&(local.s_addr));
  local.s_port = TRACER_PORT;
  socket = udp_bind(&local, NULL);
 
  ip_str2addr(target_ip,&(target.s_addr));
  target.s_port = TRACER_PORT;

  sleep(1);
 
  TracerOutputType = TRACER_UDP_OUTPUT;
  TracerUDPInit = 1;

  return 0;

}

int send_udp_event(void *p, int size) {

  static BYTE *current = pkt;
  static int events_number = 0;
  static int packet_number = 0;
 
  struct tracer_udp_header head = {"TRACER-V1.0",0,0,0};
  struct tracer_udp_header *phead = (struct tracer_udp_header *)(pkt);

  int i;

  if (total_pkt_size + size < UDP_MAXSIZE) {

    if (total_pkt_size == 0) {

      memcpy(pkt,&head,sizeof(struct tracer_udp_header));

      current = pkt;
      current += sizeof(struct tracer_udp_header);

      total_pkt_size += sizeof(struct tracer_udp_header);
      phead->total_size = total_pkt_size;

      packet_number++;
      phead->pkt_number = packet_number;

    }

    events_number++;
    phead->number_of_events = events_number;
   
    total_pkt_size += size;
    phead->total_size = total_pkt_size;
   
    //Copy the event
    memcpy(current,p,size);
    current += size;

    return 0;

  } else {

    #ifdef TRACER_UDP_DEBUG
      cprintf("Packet Sent - Nr %d Size %d\n",packet_number,total_pkt_size);
    #endif

    if (TracerUDPInit == 1) {
      udp_sendto(socket, pkt, total_pkt_size, &target);
      for (i=0;i<DELAY_LOOP;i++);
    }

    events_number = 0;
    total_pkt_size = 0;
    current = pkt;
   
    send_udp_event(p,size);
   
    return 0;    

  }

  return -1;

}

void send_remaining_udp_buffer() {

  int i;

  if (TracerUDPInit == 1 && total_pkt_size != 0) {
    #ifdef TRACER_UDP_DEBUG
      cprintf("Last Packet Sent - Size %d\n",total_pkt_size);
    #endif
    udp_sendto(socket, pkt, total_pkt_size, &target);
    for (i=0;i<DELAY_LOOP;i++);
  }

}

//Sender Task
TASK udp_sender(void *arg)
{

  int number = (int)(arg);

  while(1) {
 
    #ifdef TRACER_UDP_DEBUG
      cprintf("Send Task...\n");
    #endif

    tracer_send_logged_events(number);  

  }

}

int tracer_create_udp_task(void *m, int EventsForPeriod)
{

  if (m == NULL) {

    NRT_TASK_MODEL st;

    nrt_task_default_model(st);
    nrt_task_def_arg(st,(void *)(EventsForPeriod));
   
    udp_task = task_create("UDP_Sender",udp_sender,&st,NULL);
    if (udp_task == NIL) {
      cprintf("Error creating UDP Sender\n");
      sys_end();
    }
   
    task_activate(udp_task);

  } else {

    udp_task = task_create("UDP_Sender",udp_sender,(TASK_MODEL *)&m,NULL);
    if (udp_task == NIL) {
      cprintf("Error creating UDP Sender\n");
      sys_end();
    }
                                                                                                                             
    task_activate(udp_task);

  }

  return 0;

}