home *** CD-ROM | disk | FTP | other *** search
- [ 6. Sample PDI Application]
- EMBEDDED LAN SAMPLE PDI APPLICATION
- ═════════════════════════════════════════════════════════════════════════
- The Embedded LAN Physical Driver Interface is easily called in C, and
- a real communications program that is LAN aware can be built in a matter
- of minutes. The following is a simple PDI application program that sends
- and receives raw packets on the LAN.
-
- //
- // PROGRAM NAME: TESTPDI.C.
- //
- // FUNCTIONAL DESCRIPTION.
- // This program is a test program for the PDI interface.
- //
- // MODIFICATION HISTORY.
- // S. E. Jones 92/11/20. Original for Embedded LAN.
- //
- // NOTICE: Copyright (C) 1992 General Software, Inc.
- // All rights reserved.
- //
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <malloc.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
- #include <dos.h>
-
- #define UCHAR unsigned char
- #define USHORT unsigned short
- #define ULONG unsigned long
- #define BOOLEAN unsigned short
- #define FALSE 0
- #define TRUE (!FALSE)
- #define NULL 0L
- #define VOID void
- #define PVOID void *
- #define IN
- #define OUT
-
- #include "pdi.h" // physical driver interface.
-
- #define DRIVER_NAME "NE2000$"
-
- #define DATA_LEN (64-((PHYSICAL_NODE_ADDRESS_LENGTH*2)+2))
-
- typedef struct _PACKET {
- UCHAR DestAddr [PHYSICAL_NODE_ADDRESS_LENGTH];
- UCHAR SrcAddr [PHYSICAL_NODE_ADDRESS_LENGTH];
- UCHAR Etype [2];
- UCHAR Data [DATA_LEN]; // make a 64-byte packet.
- } PACKET, *PPACKET;
-
- static PACKET PacketBuf;
- static PHYSICAL_ADDRESS LocalAddress; // our own physical address.
- static USHORT ReceiveCount=0;
- static USHORT BytesReceived=0;
-
- #ifdef _DOS
- static UCHAR *Screen = (UCHAR *)0xb8000000L;
- #endif
-
- PIND_PDIRECEIVE TestReceiveHandler (
- PVOID EndpointContext,
- PDIBITMASK ReceiveFlags,
- PVOID Buffer,
- USHORT BufferLength)
- {
- #ifdef _DOS
- (*(Screen+40))++;
- #endif
- ReceiveCount++; // count packets received.
- BytesReceived += BufferLength; // count bytes received.
- } // TestReceiveHandler
-
- VOID main (argc, argv)
- USHORT argc;
- UCHAR *argv [];
- {
- USHORT i, j, run=0, rc, limit=0;
- PDIHANDLE Handle;
- PDIBITMASK QualityOfService;
-
- if (argc == 2) {
- limit = atoi (argv [1]);
- }
- printf ("Initializing each packet to be sent %u times.\n", limit);
-
- LocalAddress.Psap = 0; // LLC=0, else ETYPE.
- for (i=0; i<PHYSICAL_NODE_ADDRESS_LENGTH; i++) {
- LocalAddress.NodeAddress [i] = (UCHAR) PHYSICAL_NODE_ADDRESS_LENGTH-i;
- }
- LocalAddress.Flags = 0; // PSAP/NODE are NOT valid.
-
- //
- // Open an endpoint to the physical driver interface.
- //
-
- QualityOfService = PDI_QOS_CONNECTIONLESS_MODE |
- PDI_QOS_BROADCAST |
- PDI_QOS_DIRECTED;
-
- printf ("\nSent Pkts Rec'd Bytes Rec'd\n");
-
- rc = PdiOpenEndpoint (
- DRIVER_NAME,
- &LocalAddress,
- QualityOfService,
- 0L,
- &Handle);
- if (rc != PDISTATUS_SUCCESS) {
- printf ("PdiOpenEndpoint returned rc=%u.\n", rc);
- exit (1);
- }
-
- while (!kbhit ()) {
-
- //
- // Connect to the physical link.
- //
-
- rc = PdiConnect (Handle, NULL);
- if (rc != PDISTATUS_SUCCESS) {
- printf ("PdiConnect returned rc=%u.\n", rc);
- PdiCloseEndpoint (Handle);
- exit (2);
- }
-
- rc = PdiSetIndicationHandler (
- Handle,
- PDI_IND_RECEIVE,
- (PPDI_INDICATION)TestReceiveHandler);
-
- if (rc != PDISTATUS_SUCCESS) {
- printf ("PdiSetIndicationHandler returned rc=%u.\n", rc);
- PdiCloseEndpoint (Handle);
- exit (3);
- }
-
- //
- // Send packets.
- //
-
- PacketBuf.Etype [0] = 0x99; // custom ethertype.
- PacketBuf.Etype [1] = 0x98;
-
- for (i=0; i<PHYSICAL_NODE_ADDRESS_LENGTH; i++) {
- PacketBuf.DestAddr [i] = (UCHAR) i;
- PacketBuf.SrcAddr [i] = LocalAddress.NodeAddress [i];
- }
-
- for (j=0; j<limit; j++) {
- for (i=0; i<DATA_LEN; i++) {
- PacketBuf.Data [i] = (UCHAR) ((i+run) % 256);
- }
- }
-
- printf ("\r%05u %05u %05u", run++, ReceiveCount, BytesReceived);
-
- rc = PdiSend (Handle, PDI_SEND_FLAGS_EOR, &PacketBuf, sizeof (PacketBuf));
- if (rc != PDISTATUS_SUCCESS) {
- printf ("PdiSend returned rc=%u.\n", rc);
- PdiDisconnect (Handle, PDI_DISCONNECT_ABORT);
- PdiCloseEndpoint (Handle);
- exit (4);
- }
-
- //
- // Disconnect from the endpoint.
- //
-
- rc = PdiDisconnect (Handle, 0);
- if (rc != PDISTATUS_SUCCESS) {
- printf ("PdiDisconnect returned rc=%u.\n", rc);
- }
- }
-
- //
- // Close the endpoint.
- //
-
- rc = PdiCloseEndpoint (Handle);
- if (rc != PDISTATUS_SUCCESS) {
- printf ("PdiCloseEndpoint returned rc=%u.\n", rc);
- }
-
- //
- // Eat the character and exit the program.
- //
-
- getch (); // eat the character.
- printf ("\nPDI test completed.\n");
- exit (0); // exit to DOS with success.
- } /* testpdi.c */
-