home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d145 / dnet.lha / Dnet / amiga / dnet / sernet.c < prev    next >
C/C++ Source or Header  |  1988-05-26  |  4KB  |  205 lines

  1.  
  2. /*
  3.  *  SERNET.C
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  NetWork device interface.
  8.  */
  9.  
  10. #include "dnet.h"
  11.  
  12. typedef struct IOExtSer IOSER;
  13.  
  14. #define WPEND    1
  15. #define WONE    1        /*  defined if WPEND == 1   */
  16.  
  17. IOSER Iowq[WPEND];
  18. ubyte Iowact[WPEND];
  19.  
  20.  
  21. NetOpen(pior, piow, iosink, devname, unitnum, baud)
  22. IOSER **pior, **piow;
  23. PORT *iosink;
  24. char *devname;
  25. {
  26.     IOSER ios;
  27.     IOSER *ior, *iow;
  28.     short i;
  29.  
  30.     bzero(&ios, sizeof(ios));
  31.     ios.io_CtlChar = 0x11130102;
  32.     ios.io_SerFlags = SERF_SHARED|SERF_XDISABLED|SERF_RAD_BOOGIE;
  33.     if (OpenDevice(devname, unitnum, &ios, 0))
  34.     dneterror(devname);
  35.     *pior = ior = (IOSER *)AllocMem(sizeof(IOSER), MEMF_PUBLIC);
  36.     *piow = iow = (IOSER *)AllocMem(sizeof(IOSER), MEMF_PUBLIC);
  37.     *ior = ios;
  38.     *iow = ios;
  39.     ior->IOSer.io_Command = SDCMD_QUERY;
  40.     DoIO(ior);
  41.     ior->IOSer.io_Command = SDCMD_SETPARAMS;
  42.     ior->io_SerFlags = SERF_SHARED|SERF_XDISABLED|SERF_RAD_BOOGIE;
  43.     ior->io_ExtFlags = 0;
  44.     ior->io_ReadLen = ior->io_WriteLen = 8;
  45.     ior->io_CtlChar = 0x01020304;
  46.     if (baud)
  47.     ior->io_Baud = baud;
  48.     Baud = ior->io_Baud;
  49.     if (DoIO(ior) != 0)
  50.     dneterror("Unable to set serial parameters");
  51.     ior->IOSer.io_Command = CMD_READ;
  52.     iow->IOSer.io_Command = CMD_WRITE;
  53.     ior->IOSer.io_Message.mn_ReplyPort = iosink;
  54.     iow->IOSer.io_Message.mn_ReplyPort = iosink;
  55.     iow->IOSer.io_Message.mn_Node.ln_Name = (char *)WNET_REQ;
  56.     ior->IOSer.io_Message.mn_Node.ln_Name = (char *)RNET_REQ;
  57.  
  58.     for (i = 0; i < WPEND; ++i)
  59.     Iowq[i] = *iow;
  60. }
  61.  
  62. /*
  63.  *  Closedown the network.
  64.  */
  65.  
  66. NetClose(pior, piow)
  67. IOSER **pior, **piow;
  68. {
  69.     IOSER *ior = *pior;
  70.     IOSER *iow = *piow;
  71.  
  72.     if (ior) {
  73.     CloseDevice(ior);
  74.     FreeMem(ior, sizeof(IOSER));
  75.     FreeMem(iow, sizeof(IOSER));
  76.     }
  77.     *pior = NULL;
  78.     *piow = NULL;
  79. }
  80.  
  81. /*
  82.  *  NETCLWRITE()
  83.  *
  84.  *  Clear write request which was GetMsg()'d in DNET.C instead of
  85.  *  WaitIO()'d here.
  86.  */
  87.  
  88. NetClWrite(ior)
  89. IOSER *ior;
  90. {
  91.     short i = ior - &Iowq[0];
  92.     if (i >= WPEND) {
  93.     printf("NetClWrite: Software error %ld\n", i);
  94.     } else {
  95.     Iowact[i] = 0;
  96.     }
  97. }
  98.  
  99. /*
  100.  *  NETWRITE()
  101.  *
  102.  *  Write data to the network.    Up to WPEND requests may be made pending
  103.  *  before this call blocks.  This call also controls whether the last
  104.  *  sent packet is to start a timeout sequence or not (by being returned
  105.  *  as WNET_REQ or not).
  106.  */
  107.  
  108. void
  109. NetWrite(buf, bytes, expectreply)
  110. APTR buf;
  111. {
  112.     short i;
  113.     static short j;
  114. #ifdef WONE
  115.     static short last = -1;
  116. #else
  117.     static short last;
  118. #endif
  119.  
  120.     if (buf == NULL) {
  121.     for (i = 0; i < WPEND; ++i) {
  122.         if (Iowact[i]) {
  123.         WaitIO(&Iowq[i]);
  124.         Iowact[i] = 0;
  125.         }
  126.     }
  127.     fixsignal(Iowq[0].IOSer.io_Message.mn_ReplyPort);
  128.     return;
  129.     }
  130.  
  131.     /*
  132.      *    Find an open slot.  Do not use the last timeout-ok used slot.
  133.      *    Remove requests that are done.
  134.      */
  135.  
  136.     for (i = 0; i < WPEND; ++i) {
  137.     if (Iowact[i] && CheckIO(&Iowq[i]) && last != i) {
  138.         WaitIO(&Iowq[i]);
  139.         Iowact[i] = 0;
  140.     }
  141.     if (!Iowact[i])
  142.         break;
  143.     }
  144.     if (bytes) {
  145.     if (i == WPEND) {       /*  all requests active, wait for one   */
  146.         if (j == last)      /*  but not the WNET_REQ one (if any)   */
  147.         j = (j + 1) % WPEND;
  148.         WaitIO(&Iowq[i=j]);
  149.         Iowact[i] = 0;
  150.         j = (j + 1) % WPEND;
  151.     }
  152.  
  153.     /*
  154.      *  If we expect a reply to this packet, name it WNET_REQ (when
  155.      *  returned starts a timeout sequence), else name it IGWNET_REQ.
  156.      */
  157.  
  158. #ifndef WONE
  159.     if (expectreply) {
  160.         Iowq[last].IOSer.io_Message.mn_Node.ln_Name = (char *)IGWNET_REQ;
  161.         Iowq[i].IOSer.io_Message.mn_Node.ln_Name = (char *)WNET_REQ;
  162.         last = i;
  163.     } else {
  164.         Iowq[i].IOSer.io_Message.mn_Node.ln_Name = (char *)IGWNET_REQ;
  165.     }
  166. #endif
  167.     Iowq[i].IOSer.io_Data = buf;
  168.     Iowq[i].IOSer.io_Length = bytes;
  169.     SendIO(&Iowq[i]);
  170.     Iowact[i] = 1;
  171.     }
  172.     fixsignal(Iowq[0].IOSer.io_Message.mn_ReplyPort);
  173. }
  174.  
  175. NetBreak()
  176. {
  177.     NetWrite(NULL,0,0);
  178.     Iowq[0].IOSer.io_Command = SDCMD_BREAK;
  179.     DoIO(&Iowq[0]);
  180.     Iowq[0].IOSer.io_Command = CMD_WRITE;
  181.     fixsignal(Iowq[0].IOSer.io_Message.mn_ReplyPort);
  182. }
  183.  
  184. /*
  185.  *  NETREADY()
  186.  *
  187.  *  Using the specified inactive request, return the # bytes ready and
  188.  *  the carrier status.
  189.  */
  190.  
  191. NetReady(ior, cd)
  192. IOSER *ior;
  193. ubyte *cd;
  194. {
  195.     register ubyte oldcmd = ior->IOSer.io_Command;
  196.     register long n = 0;
  197.     ior->IOSer.io_Command = SDCMD_QUERY;
  198.     if (DoIO(ior) == 0)
  199.     n = ior->IOSer.io_Actual;
  200.     ior->IOSer.io_Command = oldcmd;
  201.     *cd = !(ior->io_Status & (1 << 5));
  202.     return(n);
  203. }
  204.  
  205.