home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / stefanb_src / private_projects / fax / serio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-06  |  6.1 KB  |  244 lines

  1. /*
  2.  * serio.c  V0.00
  3.  *
  4.  * Serial device utility routines
  5.  *
  6.  * (c) 1992 Stefan Becker
  7.  *
  8.  */
  9.  
  10. #include "serio.h"
  11.  
  12. /* Create a serial stream */
  13. struct SerialStream *CreateSerialStream(char *DeviceName, ULONG Unit,
  14.                                         ULONG SerFlags)
  15. {
  16.  register struct SerialStream *ss;
  17.  
  18.  /* Get memory for struct */
  19.  if (ss=AllocMem(sizeof(struct SerialStream),MEMF_CLEAR|MEMF_PUBLIC))
  20.   {
  21.    /* Create reply port for I/O Requests */
  22.    if (ss->ss_RPort=CreateMsgPort())
  23.     {
  24.      /* Create serial I/O command request */
  25.      if (ss->ss_Cmd=CreateIORequest(ss->ss_RPort,sizeof(struct IOExtSer)))
  26.       {
  27.        /* Create serial I/O read reuqest */
  28.        if (ss->ss_Read=CreateIORequest(ss->ss_RPort,sizeof(struct IOExtSer)))
  29.         {
  30.          /* Create serial I/O write reuqest */
  31.          if (ss->ss_Write=CreateIORequest(ss->ss_RPort,sizeof(struct IOExtSer)))
  32.           {
  33.            /* Init I/O read request */
  34.            ss->ss_Cmd->io_SerFlags=SerFlags;
  35.  
  36.            /* Open serial device */
  37.            if (!OpenDevice(DeviceName,Unit,(struct IORequest *) ss->ss_Cmd,0))
  38.             {
  39.              /* Copy I/O request */
  40.              *ss->ss_Read=*ss->ss_Cmd;
  41.              *ss->ss_Write=*ss->ss_Cmd;
  42.  
  43.              /* Safety code for WaitIO()! */
  44.              ReplyMsg((struct Message *) ss->ss_Read);
  45.              ReplyMsg((struct Message *) ss->ss_Write);
  46.  
  47.              /* Init rest of struct */
  48.              ss->ss_Mask=1L<<ss->ss_RPort->mp_SigBit;
  49.              ss->ss_Baud=ss->ss_Cmd->io_Baud;
  50.  
  51.              /* All OK! */
  52.              return(ss);
  53.             }
  54.  
  55.            /* Something has gone wrong */
  56.            DeleteIORequest(ss->ss_Write);
  57.           }
  58.          DeleteIORequest(ss->ss_Read);
  59.         }
  60.        DeleteIORequest(ss->ss_Cmd);
  61.       }
  62.      DeleteMsgPort(ss->ss_RPort);
  63.     }
  64.    FreeMem(ss,sizeof(struct SerialStream));
  65.   }
  66.  return(NULL);
  67. }
  68.  
  69. /* Delete a serial stream */
  70. void DeleteSerialStream(struct SerialStream *stream)
  71. {
  72.  /* Abort all pending I/O requests */
  73.  RemoveIORequest((struct IORequest *) stream->ss_Read);
  74.  RemoveIORequest((struct IORequest *) stream->ss_Write);
  75.  
  76.  /* Free all resources */
  77.  CloseDevice((struct IORequest *) stream->ss_Cmd);
  78.  DeleteIORequest(stream->ss_Write);
  79.  DeleteIORequest(stream->ss_Read);
  80.  DeleteIORequest(stream->ss_Cmd);
  81.  DeleteMsgPort(stream->ss_RPort);
  82.  FreeMem(stream,sizeof(struct SerialStream));
  83. }
  84.  
  85. /* Set serial stream parameters */
  86. BOOL SetSerialParamsTagList(struct SerialStream *stream,
  87.                             struct TagItem *TagArray)
  88. {
  89.  struct TagItem *tstate,*ti;
  90.  struct IOExtSer *ior=stream->ss_Cmd;
  91.  
  92.  /* Wait until all read/write I/O requests are finished */
  93.  WaitIO((struct IORequest *) stream->ss_Read);
  94.  WaitIO((struct IORequest *) stream->ss_Write);
  95.  
  96.  /* Scan all tags */
  97.  tstate=TagArray;
  98.  while (ti=NextTagItem(&tstate))
  99.   switch(ti->ti_Tag)
  100.    {
  101.     case SIO_Baud:ior->io_Baud=ti->ti_Data;
  102.                   break;
  103.    }
  104.  
  105.  /* Execute I/O command */
  106.  ior->IOSer.io_Command=SDCMD_SETPARAMS;
  107.  if (DoIO((struct IORequest *) ior)) return(FALSE); /* Couldn't set params! */
  108.  
  109.  /* Read new values */
  110.  *(stream->ss_Read)=*ior;
  111.  *(stream->ss_Write)=*ior;
  112.  stream->ss_Baud=ior->io_Baud;
  113.  return(TRUE);
  114. }
  115.  
  116. BOOL SetSerialParamsTags(struct SerialStream *stream, Tag Tag1, ...)
  117. {
  118.  return(SetSerialParamsTagList(stream,(struct TagItem *) &Tag1));
  119. }
  120.  
  121. /* Query actual parameters of serial stream */
  122. BOOL QuerySerial(struct SerialStream *stream)
  123. {
  124.  struct IOExtSer *ior=stream->ss_Cmd;
  125.  
  126.  /* Execute I/O command */
  127.  ior->IOSer.io_Command=SDCMD_QUERY;
  128.  if (DoIO((struct IORequest *) ior)) return(FALSE); /* I/O Error */
  129.  
  130.  /* Read new status */
  131.  stream->ss_Status=ior->io_Status;
  132.  stream->ss_Unread=ior->IOSer.io_Actual;
  133.  return(TRUE);
  134. }
  135.  
  136. /* Clear serial read buffer */
  137. BOOL ClearSerial(struct SerialStream *stream)
  138. {
  139.  struct IORequest *ior=stream->ss_Cmd;
  140.  
  141.  /* Execute I/O command */
  142.  ior->io_Command=CMD_CLEAR;
  143.  if (DoIO(ior)) return(FALSE); /* I/O Error */
  144.  
  145.  return(TRUE);
  146. }
  147.  
  148. /* Perfomr a synchronous read request */
  149. ULONG ReadSerialSynch(struct SerialStream *stream, void *buf, ULONG buflen)
  150. {
  151.  struct IOStdReq *ior=stream->ss_Read;
  152.  
  153.  /* Get replied I/O request */
  154.  WaitIO((struct IORequest *) ior);
  155.  
  156.  /* Send Read request */
  157.  ior->io_Command=CMD_READ;
  158.  ior->io_Data=buf;
  159.  ior->io_Length=buflen;
  160.  DoIO((struct IORequest *) ior);
  161.  return(ior->io_Actual);
  162. }
  163.  
  164. /* Start an asynchronous read request */
  165. void ReadSerialASynchStart(struct SerialStream *stream, void *buf,
  166.                            ULONG buflen)
  167. {
  168.  struct IOStdReq *ior=stream->ss_Read;
  169.  
  170.  /* Get replied I/O request */
  171.  WaitIO((struct IORequest *) ior);
  172.  
  173.  /* Send Read request */
  174.  ior->io_Command=CMD_READ;
  175.  ior->io_Data=buf;
  176.  ior->io_Length=buflen;
  177.  SendIO((struct IORequest *) ior);
  178. }
  179.  
  180. /* End an asynchronous read request */
  181. ULONG ReadSerialASynchEnd(struct SerialStream *stream)
  182. {
  183.  struct IOStdReq *ior=stream->ss_Read;
  184.  
  185.  /* Get replied I/O request */
  186.  WaitIO((struct IORequest *) ior);
  187.  
  188.  /* Return number of bytes read */
  189.  return(ior->io_Actual);
  190. }
  191.  
  192. /* Perform a synchronous write request */
  193. ULONG WriteSerialSynch(struct SerialStream *stream, void *buf, ULONG buflen)
  194. {
  195.  struct IOStdReq *ior=stream->ss_Write;
  196.  
  197.  /* Get replied I/O request */
  198.  WaitIO((struct IORequest *) ior);
  199.  
  200.  /* Init I/O request */
  201.  ior->io_Command=CMD_WRITE;
  202.  ior->io_Data=buf;
  203.  ior->io_Length=buflen;
  204.  DoIO((struct IORequest *) ior);
  205.  return(ior->io_Actual);
  206. }
  207.  
  208. /* Start an asynchronous write request */
  209. void WriteSerialASynchStart(struct SerialStream *stream, void *buf,
  210.                             ULONG buflen)
  211. {
  212.  struct IOStdReq *ior=stream->ss_Write;
  213.  
  214.  /* Get replied I/O request */
  215.  WaitIO((struct IORequest *) ior);
  216.  
  217.  /* Init I/O request */
  218.  ior->io_Command=CMD_WRITE;
  219.  ior->io_Data=buf;
  220.  ior->io_Length=buflen;
  221.  SendIO((struct IORequest *) ior);
  222. }
  223.  
  224. /* Start an asynchronous write request */
  225. ULONG WriteSerialASynchEnd(struct SerialStream *stream)
  226. {
  227.  struct IOStdReq *ior=stream->ss_Write;
  228.  
  229.  /* Get replied I/O request */
  230.  WaitIO((struct IORequest *) ior);
  231.  
  232.  /* Return number of bytes read */
  233.  return(ior->io_Actual);
  234. }
  235.  
  236. /* Remove an I/O request */
  237. void RemoveIORequest(struct IORequest *ior)
  238. {
  239.  /* No. I/O request still active? Yes --> abort it */
  240.  if (!CheckIO(ior)) AbortIO(ior);
  241.  
  242.  WaitIO(ior);
  243. }
  244.