⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 net__drv.c

📁 基于vxworks操作系统的电话语音平台系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************/
/*                                                              		*/
/*                                                              		*/
/* (C) Copyright Bangyan Information Technology Ltd, 2000/8.            */
/*  All right reserved                                          		*/
/*                                                              		*/
/* Author: Zhuguosheng 	                                  				*/
/*                                                              		*/
/* Description:                                                 		*/
/*                                                              		*/
/* Routines                                                     		*/
/*                                                              		*/
/************************************************************************/

#include "nms.h"

#define MFLR				1518
#define NET_IRQ            	5
#define NET_IO_ADDR        	0x0300L

/*
 * global vars
 */
UC 					MAC_Addr[6] = { 0x00, 0x60, 0x08, 0x09, 0x0a, 0x0C};
DV_DEVICE_ENTRY 	*ETHER_Device; /* this var will be used by netdriver.c */

char iwatcher_ip[] = {192,168,8,88} ;
char iwatcher_subnet[] = {255,255,255,0} ;

CHAR    iwatcher_serv_ip_addr[] = {192,168,8,72};
CHAR    umics_ip_addr[] = {192,168,8,88};
CHAR    iwatcher_serv_subnet[] = {255,255,255,0};
INT 	iwatcher_port = 2000;

/*
 * functions
 */
STATUS 	NET_Init(DV_DEVICE_ENTRY *device);
void 	PRINTF(char* string, ...) ;
STATUS  Ether_open(UINT8 *id, DV_DEVICE_ENTRY *device);
STATUS  Ether_start(DV_DEVICE_ENTRY *device, NET_BUFFER *buf_ptr) ;
STATUS  Ether_receive(DV_DEVICE_ENTRY *device) ;
STATUS  Ether_ioctl(DV_DEVICE_ENTRY *device, INT id, DV_REQ *dev_req) ;
STATUS  Ether_event(DV_DEVICE_ENTRY *device, UNSIGNED event) ;
STATUS  EthernetInit(CHAR *ip, CHAR *subnet, INT  mode) ;
STATUS  SendPackageToiWatcherDaemon(UC *buffer, SI nbytes, SI commandtype);
STATUS  TransMsgToSI(SI scc_num, VOID *buf, SI length);


/*
 * vars defined  in other files
 */
extern NU_MEMORY_POOL   	System_Memory;
extern NET_BUFFER_HEADER    MEM_Buffer_List;
extern NET_BUFFER_HEADER    MEM_Buffer_Freelist;
extern NU_SEMAPHORE 		Semaphore_EthernetInit ;
extern SOCKET_TASK_LIST		Command_Task_List ;
extern LISTEN_DAEMON_LIST	Command_Listen_Daemon_List ;

extern SI ethernet_tx(SI scc_num, VOID *buf, SI length);


extern NU_PIPE Pipe_HdlcSendToTcpip ;
extern NU_PIPE Pipe_Comm ;
/*
 * functions defines in other files
 */
extern STATUS uart_tx(SI scc_num, VOID *buf, SI length) ;
extern STATUS Task_List_Init(SOCKET_TASK_LIST *socket_task_list) ;
extern STATUS Listen_Daemon_List_Init(LISTEN_DAEMON_LIST *listen_daemon_list) ;

/* function define in nms.c */
extern INT channel_send(int socketWhich, char *buffer, int len, int flags) ;
extern INT channel_recv(int socketWhich, char *buffer, int len, int flags) ;
extern INT channel_close(int socket) ;
extern INT channel_connect(char *cli_ip_addr, char *subnet, char *serv_ip_addr, int port)  ;
extern INT channel_listen(int listenchannel, char *peer_addr) ;
extern INT channel_open_as_server(char *serv_ip_addr, char *subnet, int port, int maxconnect) ;


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      PRINTF                                                 			 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/* 		replace this funtions to BYPrintf the debug or monitor messages	 */
/*		to serial port.												     */
/*************************************************************************/
void PRINTF(char* message, ...) 
{ 
	CHAR 	buffer[512] ;
	va_list	ap ;
	int		arg ;
	UINT16	bytes_printf ;

	va_start(ap, message) ;
	bytes_printf = vsprintf(buffer, message, ap) ;
	va_end(ap) ;
	
	if (bytes_printf > 0)
	{
		TransMsgToSI(1, buffer, bytes_printf) ;	
	}
} 
	
/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      NET_Init                                                 		 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/* 		This Procedure used by the Device init, when Init_Device()		 */
/*		calls, it will call this NET_Init func.						     */
/*************************************************************************/
STATUS NET_Init(DV_DEVICE_ENTRY *device)
{
	UI	i ;

	for (i=0 ; i<6; i++)
    	device->dev_mac_addr[i] = MAC_Addr[i];   /* Address of device. */

    /* procedure handles */
    device->dev_start =  	Ether_start ;
    device->dev_open = 		Ether_open ;
    device->dev_receive = 	Ether_receive ;
    device->dev_ioctl = 	Ether_ioctl ;
    device->dev_event = 	Ether_event ;
    
    device->dev_output = 	NET_Ether_Send ;
	device->dev_input = 	NET_Ether_Input ;

    device->dev_type = DVT_ETHER ;
    device->dev_addrlen = (UINT8)6;    /* media address length */
	device->dev_hdrlen =  (UINT8)14;    /* media header length */
    device->dev_mtu = (UINT32)MTU ;    /* maximum transmission unit, excluding media */
    device->dev_transq.head = NULL ;
    device->dev_transq.tail = NULL ;
    device->dev_transq_length = 0 ;

    ETHER_Device = device ;

    return NU_SUCCESS ;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      EthernetInit		                                       		 */
/*                                                                       */
/* Input																 */
/*		ip :  the ip of interface										 */
/*      subnet : subnet of interface									 */
/*		mode   : 1( server mode)										 */
/*			   : 0( client mode)										 */
/* DESCRIPTION                                                           */
/*     This functions init the interface, give the ipaddress to the      */
/*	   hardware															 */
/*                                                                       */
/*************************************************************************/
STATUS EthernetInit(CHAR *ip, CHAR *subnet, INT  mode)
{
	STATIC	INT			haved_init = 0 ;
    STATUS              status;
    NU_DEVICE           devices[1];    

	if (haved_init >=2 )
		return NU_SUCCESS ;
		
	status =  NU_Obtain_Semaphore(&Semaphore_EthernetInit, NU_SUSPEND);
    if (haved_init  < 2  )
    {
		if ( haved_init < 1)
    	{
    		if (NU_Init_Net(&System_Memory) != NU_SUCCESS)
    		{
				status =  NU_Release_Semaphore(&Semaphore_EthernetInit);        		
        		return -1 ;
    		}
    		haved_init = 1 ;
    	}

    	if ( haved_init < 2)
    	{
    		devices[0].dv_name = "ZZX_SDH_NMS";
    		devices[0].dv_hw.ether.dv_irq = NET_IRQ;
    		devices[0].dv_hw.ether.dv_io_addr = NET_IO_ADDR;
    		devices[0].dv_hw.ether.dv_shared_addr = 0;
    		devices[0].dv_init = NET_Init;
    		devices[0].dv_flags = 0;
    		memcpy (devices[0].dv_ip_addr, ip, 4);
			memcpy (devices[0].dv_subnet_mask, subnet, 4);

    		if (NU_Init_Devices(devices, 1) != NU_SUCCESS)
    		{
        		BYPrintf("NU_Init_Devices failed in TCP_Server_Task.\n");
        		status =  NU_Release_Semaphore(&Semaphore_EthernetInit);
        		return -2;
    		}
    		haved_init = 2 ;
			Task_List_Init(&Command_Task_List) ;
			Listen_Daemon_List_Init(&Command_Listen_Daemon_List);
    	}
    }
	status =  NU_Release_Semaphore(&Semaphore_EthernetInit);
  	return NU_SUCCESS ;
}  	
    

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      SendTcpipToEtherProc                                       		 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*     This task Hand the packages from Tcpip core to the Ethernet       */
/*	   driver															 */
/*                                                                       */
/*************************************************************************/
void  SendTcpipToEtherProc(UNSIGNED argc, VOID *argv)
{
	STATUS          status;
	UL i;

	/* Access argc and argv just to avoid compilation warnings.  */
	status =  (STATUS) argc + (STATUS) argv;

	while(1)
	{
		NU_Sleep(1);

		/*
	 	 * added by linmao
	 	 * which send the packages from tcp/ip core to 68360 driver
	 	 */
    	SendTcpipToEther_Interrupt(ETHER_Device) ;
    	NU_Relinquish() ;
	}
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      SendEtherToTcpip                                           		 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*                                                                       */
/* AUTHOR                                                                */
/*                                                                       */
/* CALLED BY                                                             */
/*                                                                       */
/*                                                                       */
/* CALLS                                                                 */
/*                                                                       */
/*                                                                       */
/* INPUTS                                                                */
/*      buffer          Pointer to the data to be sent.                  */
/*      nbytes          The number of bytes of data to send.             */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*                                                                       */
/* HISTORY                                                               */
/*         NAME            DATE                    REMARKS               */
/*                                                                       */
/*                                                                       */
/*************************************************************************/
STATUS SendEtherToTcpip(UC *buffer, SI nbytes)
{
    UINT16      nsent=0, s;
    NET_BUFFER  *buf_ptr, *work_buf;
    INT32       numbytes, bytes_left, bc;

	if ((buffer == NU_NULL) || (nbytes <= 0))
		return NU_SUCCESS ;

	/* we just init the ethernet when send the package */
	/*
	EthernetInit(iwatcher_ip, iwatcher_subnet, 0) ;	
	*/
	
	bytes_left = nbytes ;

    /* Allocate a buffer chain to stick the data in. */
    buf_ptr = MEM_Buffer_Chain_Dequeue (&MEM_Buffer_Freelist,bytes_left) ;

	if (buf_ptr == NU_NULL)
		return -1;
		
    /* Compute the data size with the TCP header. */
    buf_ptr->mem_total_data_len = bytes_left;

    /* Clear the flags field. */
    buf_ptr->mem_flags = 0;

    work_buf = buf_ptr;

    /* Copy the data into the buffer chain. */
    while (bytes_left && work_buf)
    {
        /* Check to see if the current buffer is the first one in the chain.
           The first one is treated differntly from the rest. */
        if (work_buf == buf_ptr)
        {
            /* Set the number of bytes to copy to the minimum of the space 
              available or the bytes left. */ 
            bc = (bytes_left < NET_PARENT_BUFFER_SIZE )
					? bytes_left : NET_PARENT_BUFFER_SIZE ;
                
            /* Point to where the data will begin. */
            work_buf->data_ptr = work_buf->mem_parent_packet ;
        }
        else
        {
            /* Set the number of bytes to copy to the minimum of the space
               available or the bytes left. */
            bc = (bytes_left < NET_MAX_BUFFER_SIZE) ? bytes_left : NET_MAX_BUFFER_SIZE;
                
            /* Point to where the data will begin. */
            work_buf->data_ptr = work_buf->mem_packet;
        }

        /* Copy the data into the buffer. */
        memcpy(work_buf->data_ptr, buffer, bc);

        /* update the number of bytes still to be copied. */
        bytes_left -= bc;

        /* Initialize the number of bytes of data in this buffer. */
        work_buf->data_len = bc;

        /* Advance the pointer to the userd data. */
        buffer += bc;

        /* Point to the next buffer in the chain. */
        work_buf = work_buf->next_buffer;
    }

    /* Initialize other fields of this buffer. */
    buf_ptr->mem_option_len = 0;
    buf_ptr->mem_buf_device = ETHER_Device ;

    MEM_Buffer_Enqueue(&MEM_Buffer_List, buf_ptr) ;
    NU_Set_Events(&Buffers_Available, 2, NU_OR);

    return NU_SUCCESS ;

}

STATUS SendTcpipToEther(DV_DEVICE_ENTRY *device, NET_BUFFER *buf_ptr)
{
    SI			length ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -