home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / sysembed / samppdi.elh < prev    next >
Encoding:
Text File  |  1995-03-30  |  5.4 KB  |  197 lines

  1. [ 6. Sample PDI Application]
  2.                  EMBEDDED LAN SAMPLE PDI APPLICATION
  3. ═════════════════════════════════════════════════════════════════════════
  4. The Embedded LAN Physical Driver Interface is easily called in C, and
  5. a real communications program that is LAN aware can be built in a matter
  6. of minutes.  The following is a simple PDI application program that sends
  7. and receives raw packets on the LAN.
  8.  
  9. //
  10. // PROGRAM NAME:  TESTPDI.C.
  11. //
  12. // FUNCTIONAL DESCRIPTION.
  13. //      This program is a test program for the PDI interface.
  14. //
  15. // MODIFICATION HISTORY.
  16. //      S. E. Jones     92/11/20.       Original for Embedded LAN.
  17. //
  18. // NOTICE:  Copyright (C) 1992 General Software, Inc.
  19. //          All rights reserved.
  20. //
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <conio.h>
  25. #include <malloc.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <time.h>
  29. #include <dos.h>
  30.  
  31. #define UCHAR unsigned char
  32. #define USHORT unsigned short
  33. #define ULONG unsigned long
  34. #define BOOLEAN unsigned short
  35. #define FALSE 0
  36. #define TRUE (!FALSE)
  37. #define NULL 0L
  38. #define VOID void
  39. #define PVOID void *
  40. #define IN
  41. #define OUT
  42.  
  43. #include "pdi.h"                        // physical driver interface.
  44.  
  45. #define DRIVER_NAME     "NE2000$"
  46.  
  47. #define DATA_LEN        (64-((PHYSICAL_NODE_ADDRESS_LENGTH*2)+2))
  48.  
  49. typedef struct _PACKET {
  50.     UCHAR DestAddr [PHYSICAL_NODE_ADDRESS_LENGTH];
  51.     UCHAR SrcAddr [PHYSICAL_NODE_ADDRESS_LENGTH];
  52.     UCHAR Etype [2];
  53.     UCHAR Data [DATA_LEN];              // make a 64-byte packet.
  54. } PACKET, *PPACKET;
  55.  
  56. static PACKET PacketBuf;
  57. static PHYSICAL_ADDRESS LocalAddress;   // our own physical address.
  58. static USHORT ReceiveCount=0;
  59. static USHORT BytesReceived=0;
  60.  
  61. #ifdef _DOS
  62. static UCHAR *Screen = (UCHAR *)0xb8000000L;
  63. #endif
  64.  
  65. PIND_PDIRECEIVE TestReceiveHandler (
  66.     PVOID EndpointContext,
  67.     PDIBITMASK ReceiveFlags,
  68.     PVOID Buffer,
  69.     USHORT BufferLength)
  70. {
  71. #ifdef _DOS
  72.     (*(Screen+40))++;
  73. #endif
  74.     ReceiveCount++;                     // count packets received.
  75.     BytesReceived += BufferLength;      // count bytes received.
  76. } // TestReceiveHandler
  77.  
  78. VOID main (argc, argv)
  79.     USHORT argc;
  80.     UCHAR *argv [];
  81. {
  82.     USHORT i, j, run=0, rc, limit=0;
  83.     PDIHANDLE Handle;
  84.     PDIBITMASK QualityOfService;
  85.  
  86.     if (argc == 2) {
  87.         limit = atoi (argv [1]);
  88.     }
  89.     printf ("Initializing each packet to be sent %u times.\n", limit);
  90.  
  91.     LocalAddress.Psap = 0;              // LLC=0, else ETYPE.
  92.     for (i=0; i<PHYSICAL_NODE_ADDRESS_LENGTH; i++) {
  93.         LocalAddress.NodeAddress [i] = (UCHAR) PHYSICAL_NODE_ADDRESS_LENGTH-i;
  94.     }
  95.     LocalAddress.Flags = 0;             // PSAP/NODE are NOT valid.
  96.  
  97.     //
  98.     // Open an endpoint to the physical driver interface.
  99.     //
  100.  
  101.     QualityOfService = PDI_QOS_CONNECTIONLESS_MODE |
  102.                        PDI_QOS_BROADCAST |
  103.                        PDI_QOS_DIRECTED;
  104.  
  105.     printf ("\nSent   Pkts Rec'd  Bytes Rec'd\n");
  106.  
  107.     rc = PdiOpenEndpoint (
  108.             DRIVER_NAME,
  109.             &LocalAddress,
  110.             QualityOfService,
  111.             0L,
  112.             &Handle);
  113.     if (rc != PDISTATUS_SUCCESS) {
  114.         printf ("PdiOpenEndpoint returned rc=%u.\n", rc);
  115.         exit (1);
  116.     }
  117.  
  118.     while (!kbhit ()) {
  119.  
  120.         //
  121.         // Connect to the physical link.
  122.         //
  123.  
  124.         rc = PdiConnect (Handle, NULL);
  125.         if (rc != PDISTATUS_SUCCESS) {
  126.             printf ("PdiConnect returned rc=%u.\n", rc);
  127.             PdiCloseEndpoint (Handle);
  128.             exit (2);
  129.         }
  130.  
  131.         rc = PdiSetIndicationHandler (
  132.                 Handle,
  133.                 PDI_IND_RECEIVE,
  134.                 (PPDI_INDICATION)TestReceiveHandler);
  135.  
  136.         if (rc != PDISTATUS_SUCCESS) {
  137.             printf ("PdiSetIndicationHandler returned rc=%u.\n", rc);
  138.             PdiCloseEndpoint (Handle);
  139.             exit (3);
  140.         }
  141.  
  142.         //
  143.         // Send packets.
  144.         //
  145.  
  146.         PacketBuf.Etype [0] = 0x99;         // custom ethertype.
  147.         PacketBuf.Etype [1] = 0x98;
  148.  
  149.         for (i=0; i<PHYSICAL_NODE_ADDRESS_LENGTH; i++) {
  150.             PacketBuf.DestAddr [i] = (UCHAR) i;
  151.             PacketBuf.SrcAddr [i] = LocalAddress.NodeAddress [i];
  152.         }
  153.  
  154.         for (j=0; j<limit; j++) {
  155.             for (i=0; i<DATA_LEN; i++) {
  156.                 PacketBuf.Data [i] = (UCHAR) ((i+run) % 256);
  157.             }
  158.         }
  159.  
  160.         printf ("\r%05u  %05u       %05u", run++, ReceiveCount, BytesReceived);
  161.  
  162.         rc = PdiSend (Handle, PDI_SEND_FLAGS_EOR, &PacketBuf, sizeof (PacketBuf));
  163.         if (rc != PDISTATUS_SUCCESS) {
  164.             printf ("PdiSend returned rc=%u.\n", rc);
  165.             PdiDisconnect (Handle, PDI_DISCONNECT_ABORT);
  166.             PdiCloseEndpoint (Handle);
  167.             exit (4);
  168.         }
  169.  
  170.         //
  171.         // Disconnect from the endpoint.
  172.         //
  173.  
  174.         rc = PdiDisconnect (Handle, 0);
  175.         if (rc != PDISTATUS_SUCCESS) {
  176.             printf ("PdiDisconnect returned rc=%u.\n", rc);
  177.         }
  178.     }
  179.  
  180.     //
  181.     // Close the endpoint.
  182.     //
  183.  
  184.     rc = PdiCloseEndpoint (Handle);
  185.     if (rc != PDISTATUS_SUCCESS) {
  186.         printf ("PdiCloseEndpoint returned rc=%u.\n", rc);
  187.     }
  188.  
  189.     //
  190.     // Eat the character and exit the program.
  191.     //
  192.  
  193.     getch ();                           // eat the character.
  194.     printf ("\nPDI test completed.\n");
  195.     exit (0);                           // exit to DOS with success.
  196. } /* testpdi.c */
  197.