这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » VxWorks_etherLib Problem(老站转)

共2条 1/1 1 跳转至

VxWorks_etherLib Problem(老站转)

菜鸟
2002-05-31 22:29:24     打赏
小华 高级工程师 来自: 发表总数:102   查看   短消息   电子邮件 -------------------------------------------------------------------------------- Hi, I didnot use the functions of the etherLib. I want use it to transfer and receive ethernet packets. Anyone used it before? Can you give me some example program? Thanks! -------------------------------------------------------------------------------- 编辑 发表於:2002-01-25 - 10:19:36 IP: 202.119.*.* superme 工程师 来自: 发表总数:40 查看   短消息   电子邮件 -------------------------------------------------------------------------------- Give you a sample code for low level Ethernet routines. It includes three files: etherDemo.h, etherInputDemo.c and etherOutputDemo.c The following are the code: /* etherDemo.h - header for etherInputDemo and etherOutputDemo */ /* Copyright 1984-1997 Wind River Systems, Inc. */ /* modification history -------------------- 01b,06nov97,mm added copyright. 01a,17feb94,ms written */ #define ET_TYPE (u_short)1111 /* user defined protocol type */ #define INTERFACE_NAME "sm0" /* name of your network interface name*/ LOCAL STATUS etBcast(); /* broadcast on ethernet network interface */ /* LOCAL BOOL etHandle (); ether input hook routine for handling input */ LOCAL char etMessage [] = "Hello World!!!"; /* data to be passed*/ /* Broadcast ethernet address is used for simple demonstration. You can * replace the value of the etbcastaddr variable with the destination * ethernet address. */ unsigned char etbcastaddr [6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -------------------------------------------------------------------------------- 编辑 发表於:2002-01-29 - 08:39:04 IP: 24.100.*.* superme 工程师 来自: 发表总数:40 查看   短消息   电子邮件 -------------------------------------------------------------------------------- /* etherInputDemo.c - Demo for using low-level ethernet input routines */ /* Copyright 1984-1997 Wind River Systems, Inc. */ /* modification history -------------------- 01e,06nov97,mm added copyright. 01d,22Sep97,mm removed include changed include "etherDemo.h" to "in_etherDemo.h" 01c,22Sep97,mm added include , , and 01b,22Sep97,mm changed "if.h" to "net/if.h" 01a,16Feb94,ms modified and cleaned up for VxDemo. */ #include #include #include #include #include "net/if.h" #include "netinet/if_ether.h" #include "in_etherDemo.h" /***************************************************************************** * etherInputDemo - Demo for using low-level ethernet input routines * * DESCRIPTION * * Handles (receives) raw input frames (input data) from the network * interface using ether input hook routine etHandle. * * This is a simple demonstration of using low-level input ethernet * routines. The other half of the demonstration of using low-level * output ethernet routines is in etherOutputDemo.c. etherInputDemo runs on * one VxWorks system, and etherOutputDemo runs on an other VxWorks system * in the same physical network. * * RETURNS: OK or ERROR * * EXAMPLE: * * To run this etherInputDemo task from the VxWorks shell do as follows: * -> sp (etherInputDemo) * */ STATUS etherInputDemo () { if (etherInputHookAdd (etHandle) == OK) printf ("etherInputHookAdd successful\n"); else { perror ("etherInputHook failed"); return (ERROR); } return (OK); } /***************************************************************************** * etHandle - ether input hook routine for handling raw input frames from the * network interface. This routine is executed at task level * interrupt service for input packets. * * RETURNS: TRUE or FALSE * */ BOOL etHandle (ifp, buffer, length) struct ifnet *ifp; char *buffer; int length; { struct ether_header *ethHdr; char *buffPtr; int ix; char tmp[25]; ethHdr = (struct ether_header *) buffer; if (ntohs(ethHdr->ether_type) != (u_short) ET_TYPE) return (FALSE); /* Not a user defined protocol type, hence 0 is passed * to indicate further processing of data to be handled by * higher level protocols (IP, TCP etc., ). */ /* Handle raw input frames. * Data integrity check and printing the input message is done for * demonstration purpose. */ /* Check for data integrity error */ buffPtr = (char *) (buffer + sizeof (*ethHdr)); strncpy (tmp, buffPtr, strlen (etMessage)); for (ix = 0; ix < sizeof (etMessage); ++ix) if (*buffPtr++ != etMessage [ix]) break; if (ix != strlen (etMessage)) { logMsg ("etHandle: data integrity error at %d.\n", ix); return (ERROR); } logMsg ("etHandle: data okay - message is : %s \n", tmp); return (TRUE); /* non-zero value to imply hook routine has processed * the input and there is no need for further handling by * any higher level protocols (IP, TCP etc.) */ } -------------------------------------------------------------------------------- 编辑 发表於:2002-01-29 - 08:40:09 IP: 24.100.*.* superme 工程师 来自: 发表总数:40 查看   短消息   电子邮件 -------------------------------------------------------------------------------- /* etherOutputDemo.c - Demo for using low-level output ethernet routines */ /* Copyright 1984-1997 Wind River Systems, Inc. */ /* modification history -------------------- 01d,06nov97,mm added copyright. 01c,22Sep97,mm added include string.h and "etherLib.h" 01b,22Sep97,mm changed "if.h" to "netinet/if_ether.h" 01a,16Feb94,ms modified and cleaned up for VxDemo. */ #include "net/if.h" #include "netinet/if_ether.h" #include "etherDemo.h" #include #include #include "etherLib.h" /***************************************************************************** * etherOutputDemo - Demo for using low-level ethernet output routines * * DESCRIPTION * * Sends (broadcast) data on an ethernet network interface * and handles (receives) raw input frames (input data) from the network * interface using ether input hook routine etHandle. * * This is a simple demonstration of using low-level output ethernet * routines. The other half of the demonstration of using low-level * input ethernet routines is in etherInputDemo.c. etherInputDemo task * runs on one VxWorks system, and etherOutputDemo task runs on other * VxWorks system in the same physical network. * * RETURNS: OK or ERROR * * EXAMPLE: * * To run this etherOutputDemo task from the VxWorks shell do as follows: * -> sp (etherOutputDemo) * */ STATUS etherOutputDemo () { if (etBcast (INTERFACE_NAME) == ERROR) { printf ("etherOutput broadcast failed\n"); return (ERROR); } else printf ("etherOutputDemo completed\n"); return (OK); } /***************************************************************************** * etBcast - Sends (broadcast) data on an ethernet network interface * * RETURNS: OK or ERROR * */ STATUS etBcast ( char *ifname /* name of the network interface eg. "ei0" */ ) { struct ether_header ethHdr; /* ethernet header */ struct ifnet *pIfnet; /*Pointer to your network interface's ifnet struct*/ pIfnet = ifunit (ifname); /* get the ifnet pointer of your network * interface */ if (pIfnet == (struct ifnet *) NULL) { printf ("ifunit failed\n"); return (ERROR); } /* Set the value of destination host's ethernet address - here broadcast ethernet address is set */ bcopy (etbcastaddr, ethHdr.ether_dhost, sizeof (ethHdr.ether_dhost)); ethHdr.ether_type = htons (ET_TYPE); /* user defined protocol */ printf ("Protocol type 0x%x \n\n", (u_short)ethHdr.ether_type); /* send (broadcast) message on an ethernet interface */ if (etherOutput (pIfnet, ðHdr, etMessage, strlen (etMessage)) == ERROR) { perror ("etherOutput failed"); return (ERROR); } else printf ("etherOutput done - message: %s \n", etMessage); return (OK); } -------------------------------------------------------------------------------- 编辑 发表於:2002-01-29 - 08:41:24 IP: 24.100.*.* 小华 高级工程师 来自: 发表总数:102 查看   短消息   电子邮件 -------------------------------------------------------------------------------- 谢谢您,这些代码对我很有帮助,祝您春节愉快!



关键词: VxWorks     etherLib     Problem         

菜鸟
2002-05-31 22:35:00     打赏
2楼
但是调用EtherLib中的etherInput/Output还要在bsp中作一些修改,才能支持此调用。 如果不对,那就是错了,我当时还是选择了调用socket。

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]