home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / amiexpress / source / ae / code / ax3.00 / proto.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-03  |  7.3 KB  |  323 lines

  1. #include "bbs.h"
  2. #include <clib/timer_protos.h>
  3.  
  4. extern int  zresume;
  5. char sbuff[133];
  6.  
  7. BPTR fh;
  8. static struct FileHandle *Fileh;    /* The file handle                  */
  9. static struct StandardPacket *Packet;    /* MUST be longword aligned         */
  10. static struct MsgPort *RPort;        /* A standard EXEC message port     */
  11. static char Pending;            /* operation 'in progress' flag     */
  12.  
  13. static struct IOExtSer *ReadReq, *WriteReq;
  14.  
  15. static struct timerequest *TimeReq;
  16. struct Library *TimerBase; /* for lattice , should be struct Device * */
  17. static struct timeval ms,me;
  18.  
  19. #define RBUFSIZE 1024L
  20. static UBYTE rs_in[RBUFSIZE+128];
  21. static int rlength;
  22. static int count;
  23. char *sendbuff;
  24. int sendlen;
  25. char *zdiskbuff;
  26. char *zbuffer;
  27. long zbufsize;
  28. long zbaud;
  29. int doublebuffer;
  30. int EndSynC;
  31. ULONG STpos;
  32.  
  33. /* Prototypes for functions defined in crctab.c */
  34. short update_crc16(int cp,int crc);
  35. long update_crc32(int cp,long crc);
  36. int startzmodem(char *name,int flg);
  37.  
  38. int proto(struct IOExtSer *RR, struct IOExtSer *WR, struct timerequest *TR,
  39.              char *BF, long BS, long BD, char *flname, int send, int db)
  40. {
  41.  int tempvar;
  42.     ReadReq = RR;
  43.     WriteReq = WR;
  44.     TimeReq = TR;
  45.  
  46.  TimerBase = (struct Library *)TimeReq->tr_node.io_Device;
  47.     zdiskbuff = zbuffer = BF;
  48.     zbufsize = BS;
  49.     zbaud = BD;
  50.     Online_NFiles=0;
  51.     DONF=0;
  52.     FreeDFlag=0;
  53.  
  54.     rlength = count = sendlen = 0;
  55.     sendbuff = 0L;            /* serial init vals */
  56.  
  57.     doublebuffer = 1;
  58.  
  59.     if(send==1)
  60.          tempvar=startzmodem(flname,1);        /* do zmodem send */
  61.      else    tempvar=startzmodem(flname,0);       /* do zmodem receive */
  62.     return(tempvar);
  63. }
  64.  
  65. void sendbyte(int ch)
  66. {
  67.  unsigned char s;
  68.  
  69.  if(sendbuff) {
  70.         sendbuff[sendlen++] = ch & 0xFF;
  71.         return;
  72.     }
  73.  
  74.  s = ch & 0xFF;
  75.  WriteReq->IOSer.io_Length  = 1L;
  76.  WriteReq->IOSer.io_Data    = (APTR)&s;
  77.  WriteReq->IOSer.io_Command = CMD_WRITE;
  78.  DoIO(WriteReq);
  79. }
  80.  
  81. void sendbuffer(char *buf, int len)
  82. {
  83.     WriteReq->IOSer.io_Length = (long)len;
  84.     WriteReq->IOSer.io_Data = (APTR)buf;
  85.     WriteReq->IOSer.io_Command = CMD_WRITE;
  86.     SendIO(WriteReq);
  87. }
  88.  
  89. void waitwrite(void)
  90. {
  91.     WaitIO(WriteReq);
  92. }
  93.  
  94. void purgewrite(void)
  95. {
  96.     AbortIO(WriteReq);
  97.     WaitIO(WriteReq);
  98. }
  99.  
  100. static void stimer(ULONG seconds, ULONG micros)
  101. {
  102.    TimeReq->tr_node.io_Command = TR_ADDREQUEST;
  103.    TimeReq->tr_time.tv_secs    = seconds;
  104.    TimeReq->tr_time.tv_micro   = micros;
  105.    SendIO(TimeReq);
  106. }
  107.  
  108. int readbyte(void)
  109. {
  110.    register int len;
  111.  
  112.    if(rlength)
  113.    {
  114.       --rlength;
  115.       return((int)rs_in[count++]);
  116.    }
  117.  
  118.    ReadReq->IOSer.io_Command = SDCMD_QUERY;
  119.    DoIO(ReadReq);
  120.    ReadReq->IOSer.io_Command = CMD_READ;
  121.    ReadReq->IOSer.io_Data = (APTR)&rs_in[0];
  122.    len = ReadReq->IOSer.io_Actual;
  123.    if(len)
  124.    {
  125.       if(len > RBUFSIZE) len = RBUFSIZE;
  126.       ReadReq->IOSer.io_Length = (long)len;
  127.       DoIO(ReadReq);
  128.       rlength = --len; count = 0;
  129.       return((int)rs_in[count++]);
  130.    }
  131.  
  132.    ReadReq->IOSer.io_Length = 1L;
  133.    ReadReq->IOSer.io_Flags = IOF_QUICK;
  134.    BeginIO(ReadReq);
  135.  
  136.    if(ReadReq->IOSer.io_Flags & IOF_QUICK)
  137.    {
  138.       return((int)rs_in[0]);
  139.    }
  140.    else
  141.    if(CheckIO(ReadReq))     /* if IO request has completed */
  142.     {
  143.        WaitIO(ReadReq);     /* remove the message from the queue */
  144.        return((int)rs_in[0]);
  145.     }
  146.  
  147.    stimer(10L,0L);      /* start timeout */
  148.  
  149. /* wait for a TX window event, the timer to timeout or for a receive character */
  150. for(;;)
  151.     {
  152.    Wait((1L << ReadReq->IOSer.io_Message.mn_ReplyPort->mp_SigBit) |
  153.     (1L << TimeReq->tr_node.io_Message.mn_ReplyPort->mp_SigBit));
  154.  
  155.    if(CheckIO(ReadReq))     /* if IO request has completed */
  156.     {
  157.        AbortIO(TimeReq);
  158.        WaitIO(TimeReq);
  159.        WaitIO(ReadReq);     /* remove the message from the queue */
  160.        return((int)rs_in[0]);
  161.     }
  162. /* see if the timer timed out */
  163.   if(CheckIO(TimeReq))          /* was timeout */
  164.    {
  165.      AbortIO(ReadReq);
  166.      WaitIO(ReadReq);
  167.      return TIMEOUT;
  168.    }
  169.    if(!CheckCarrier()) {
  170.                if(!CheckIO(TimeReq))
  171.             AbortIO(TimeReq);
  172.  
  173.             WaitIO(TimeReq);
  174.                if(!CheckIO(ReadReq))
  175. AbortIO(ReadReq);
  176.      WaitIO(ReadReq);
  177.             return(TIMEOUT);
  178.             }
  179.     }                     /* wait for Timeout or character */
  180. }
  181.  
  182. void purgeline(void)
  183. {
  184.    rlength = count = 0;
  185.  
  186.    ReadReq->IOSer.io_Command = CMD_CLEAR;
  187.    DoIO(ReadReq);                       /* flush receive buffer  */
  188. }
  189.  
  190. int checkline(void)
  191. {
  192.    if(rlength) return(rlength);
  193.  
  194.    ReadReq->IOSer.io_Command = SDCMD_QUERY;
  195.    DoIO(ReadReq);
  196.    return((int)(ReadReq->IOSer.io_Actual));
  197. }
  198.  
  199. void itp(long size,UBYTE r)
  200. {
  201. ULONG csecs,csize;
  202.  
  203.  TimeReq->tr_node.io_Command = TR_GETSYSTIME;
  204.  DoIO(TimeReq);
  205.  if(size==0)    {
  206.         ms.tv_secs = TimeReq->tr_time.tv_secs;
  207.         ms.tv_micro = TimeReq->tr_time.tv_micro;
  208.     } else {
  209.         me.tv_secs = TimeReq->tr_time.tv_secs;
  210.         me.tv_micro = TimeReq->tr_time.tv_micro;
  211.         SubTime(&me,&ms);
  212.         me.tv_secs += 1L;
  213.         if(Cmds->AcLvl[LVL_VARYING_LINK_RATE]==0&&r==0)
  214.          me.tv_secs+=2L;
  215.  
  216.      csecs=me.tv_secs*100+(me.tv_micro/10000);
  217.         csize=size*100;
  218.         sprintf(sbuff, "12H%lu Efficency %lu%%  ",(csize/csecs),(((csize/csecs)*100L)/(zbaud/10L)));
  219.         ZmodemStatPrint(sbuff);
  220.     }
  221. }
  222.  
  223. void throughput(long size)
  224. {
  225.  ULONG csecs,csize;
  226.  long TEFF,TCPS,TTM,TTS;
  227.  
  228.  if (size==0)      itp(0L,0);
  229.  else  {
  230.         TimeReq->tr_node.io_Command = TR_GETSYSTIME;
  231.         DoIO(TimeReq);
  232.         me.tv_secs = TimeReq->tr_time.tv_secs;
  233.         me.tv_micro = TimeReq->tr_time.tv_micro;
  234.         SubTime(&me,&ms);
  235.         me.tv_secs += 1L;
  236.         csecs=me.tv_secs*100+(me.tv_micro/10000);
  237.         csize=size*100;
  238.  
  239.      TEFF=(((csize/csecs)*100L)/(zbaud/10L));
  240.      TCPS=(csize/csecs);
  241.      TTEFF+=TEFF;
  242.      TTCPS+=TCPS;
  243.      TTM=(me.tv_secs/60L);
  244.      TTS=(me.tv_secs%60L);
  245.      ZModemInfo.LastTime=me.tv_secs;
  246.      TTTM+=me.tv_secs;
  247.      TBT+=TMPBT;
  248.      if(!FreeDFlag)    DTBT+=TMPBT;
  249.  
  250.      sprintf(sbuff, "2H%lu Mins %lu Secs",TTM,TTS);
  251.      ZmodemStatPrint(sbuff);
  252.      ZmodemStatPrint("2H             ");
  253.      sprintf(sbuff, "12H%lu Efficency %lu%%  ", TCPS,TEFF);
  254.      ZmodemStatPrint(sbuff);
  255.      ZModemInfo.Cps=TCPS;
  256.      ZModemInfo.Eff=TEFF;
  257.    }
  258. }
  259.  
  260. void InitAsync(void)
  261. {
  262.     EndSynC=1;
  263.     Fileh = (struct FileHandle *)BADDR(fh);
  264.     Packet = (struct StandardPacket *)AllocMem((long)sizeof(struct StandardPacket),
  265.           MEMF_CLEAR|MEMF_PUBLIC);
  266.     RPort = CreatePort(0L, 0L);
  267.     Pending = 0;
  268. }
  269.  
  270. void WriteAsync(char *buf, long bytes)
  271. {
  272.     if(Pending) {
  273.          WaitPort(RPort);       /* wait for previous packet to return */
  274.          GetMsg(RPort);         /* remove packet from port            */
  275.          Pending = 0;
  276.  }
  277.     if(buf) {
  278.          /*
  279.           * DOS requires a pointer to the dos packet be placed
  280.           * in the ln)Name field of the message, and a pointer
  281.           * to the message be placed in the dp_Link field of the
  282.           * dos packet.
  283.           */
  284.          Packet->sp_Msg.mn_Node.ln_Name = (char *)&(Packet->sp_Pkt);
  285.          Packet->sp_Pkt.dp_Link  = &Packet->sp_Msg;
  286.          Packet->sp_Pkt.dp_Port  = RPort;
  287.          Packet->sp_Pkt.dp_Type  = ACTION_WRITE;
  288.          Packet->sp_Pkt.dp_Arg1  = Fileh->fh_Arg1;
  289.          Packet->sp_Pkt.dp_Arg2  = (long)buf;
  290.          Packet->sp_Pkt.dp_Arg3  = bytes;
  291.          PutMsg(Fileh->fh_Type,(struct Message *)Packet);
  292.          Pending = 1;
  293.     }
  294. }
  295.  
  296. void EndAsync(void)
  297. {
  298.     if(Pending)    {
  299.          WaitPort(RPort);
  300.          GetMsg(RPort);
  301.     }
  302.     DeletePort(RPort);
  303.     FreeMem(Packet, (long)sizeof(struct StandardPacket));
  304.     EndSynC=0;
  305. }
  306.  
  307. BPTR Aopen(char *name, long mode)
  308. {
  309.  fh = Open(name, mode);
  310.  InitAsync();
  311.  return(fh);
  312. }
  313.  
  314. void Aclose(void)
  315. {
  316.  if(EndSynC) {
  317.       EndAsync();
  318.       Close(fh);
  319.       fh = 0L;
  320.  }
  321. }
  322.  
  323.