/*
	skyeye_net.c - skyeye general net device file support functions
	Copyright (C) 2003 Skyeye Develop Group
	for help please send mail to <skyeye-developer@lists.sf.linuxforum.net> 
	
	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 
 
*/

/*
 * 04/27/2003 	initial version
 *				chenyu <chenyu@hpclab.cs.tsinghua.edu.cn>
 */
 
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "armdefs.h"
//#include "skyeye_net.h"

#define DEBUG 1
#if DEBUG
#define DBG_PRINT(a...) fprintf(stderr, ##a)
#else
#define DBG_PRINT(a...)
#endif

int skyeye_net_maxfd=0;
int skyeye_net_on=0; //0 means skyeye has no net, 1 means skyeye has net

void skyeye_net_init(void)
{
 int i;
 for(i=0;i<NET_MAXNICNUM_PER_HOST;i++){
    skyeye_config.net[i].state=0;
    memset(skyeye_config.net[i].macaddr,0,6);
    memset(skyeye_config.net[i].hostip,0,4);
    skyeye_config.net[i].ethmod=-1;
    skyeye_config.net[i].fd=-1;
    skyeye_config.net[i].hubindex=-1;
    skyeye_config.net[i].net_init=NULL;
 } 
 skyeye_net_on=0;
}
 
//chy 2003-04-26: will improve performance in future
//return index of net according to fd
//return -1 means can not find 
int skyeye_net_fd2index(int fd)
{ int i;
  for(i=0;i<NET_MAXNICNUM_PER_HOST;i++){
    if(skyeye_config.net[i].state==1 && fd==skyeye_config.net[i].fd) return i;
  }
  return -1;
}  

//chy 2003-04-26: will improve performance in future
int skyeye_net_fdset(fd_set *rfdsp)
{
  int i;
  for(i=0;i<NET_MAXNICNUM_PER_HOST;i++){
      if(skyeye_config.net[i].state==1) 
         FD_SET(skyeye_config.net[i].fd, rfdsp);
  }  
}

int skyeye_net_input(fd_set * rfdsp, ARMul_State *state)
{
  int i;
  for(i=0;i<NET_MAXNICNUM_PER_HOST;i++){
       if(skyeye_config.net[i].state==1){ 
          if(FD_ISSET(skyeye_config.net[i].fd, rfdsp))
                   skyeye_config.net[i].net_input(skyeye_config.net[i].fd, state);	   
	}       
  }
}

int skyeye_net_output(int index, ARMul_State *state,INT8U startpage,INT16U packet_len)
{
  if(skyeye_config.net[index].state==1){
       skyeye_config.net[index].net_output(skyeye_config.net[index].fd, state, startpage, packet_len);
  }
}       
