static int SetBaudSender( void ) { int data; /* storing sync string data to other machine */ int i; /* loop index */ int wait_time; if( !SenderHandshake() ) return( FAIL ); /* sync byte received ... send string */ StartBlockTrans(); for( i = data = 0; i < SYNC_LEN; i++, data = (data + SYNC_INC) & 0xff ) { SendByte( data ); /* send sync string bytes */ } StopBlockTrans(); wait_time = WaitCount() + SYNC_TIME_OUT; /* limit for time out */ /* If MaxBaud == MIN_BAUD, we're talking over a modem and it might have buffered characters that haven't been transmitted yet. */ if( MaxBaud == MIN_BAUD ) wait_time += SEC(2); for( ;; ) { if( WaitByte( 1 ) == SDATA_TAK ) { SendByte( SDATA_ACK ); if( WaitByte( SEC(1)/2 ) == SDATA_TAK ) { return( SUCCESS ); } else { return( FAIL ); } } else if( WaitCount() >= wait_time ) { /* break not found; other end have not acknowledged string */ return( FAIL ); } } }
void Wait( unsigned timer_ticks ) { unsigned wait_time; wait_time = WaitCount() + timer_ticks; while( WaitCount() < wait_time ); }
void Wait( unsigned timer_ticks ) { unsigned wait_time; wait_time = WaitCount() + timer_ticks; while( WaitCount() < wait_time ) { DosSleep( MILLISEC_PER_TICK/2 ); /* half a timer tick */ } }
int WaitByte( unsigned timer_ticks ) { unsigned wait_time; /* timer variable for testing time-out */ int data; /* storing data from receive buffer */ wait_time = WaitCount() + timer_ticks; for( ;; ) { data = GetByte(); if( data != SDATA_NO_DATA ) break; /* character received */ if( WaitCount() >= wait_time ) break; NothingToDo(); } return( data ); }
static int SenderHandshake( void ) { unsigned wait_time; /* used to test for time-out */ int reply; /* storing data received from other machine */ wait_time = WaitCount() + SYNC_TIME_OUT; /* limit for time out */ if( MaxBaud == MIN_BAUD ) wait_time += SEC(1); SendByte( SYNC_BYTE ); /* send SYNC_BYTE */ for( ;; ) { /* loop until ACK received or time out */ reply = WaitByte( 1 ); /* get reply */ if( reply == SDATA_ACK ) break; /* ACK received; go to next operation */ if( reply == SDATA_HI ) { /* return HI received */ SendByte( SDATA_HI ); } else if( WaitCount() > wait_time ) { /* time out */ return( FAIL ); } /* not yet time out; loop */ } SendByte( SYNC_END ); return( SUCCESS ); }
static int WaitReceive( byte *err, trap_elen max_len, byte *p, unsigned timeout ) { unsigned wait_time; /* timer */ int data; /* data from other machine */ int result; /* result of BlockReceive() */ ZeroWaitCount(); wait_time = WaitCount() + timeout; for( ;; ) { data = WaitByte( 1 ); if( data == SDATA_STX ) { /* STX received, get block */ result = BlockReceive( err, max_len, p ); if( result ) return( result ); } else if( data == SDATA_RLR ) { /* RLR received */ SendByte( SDATA_NAK ); /* tell the other end to resend block */ } else if( (timeout != FOREVER) && (WaitCount() >= wait_time) ) { return( FAIL ); /* time-out */ } } }
static int ReceiverHandshake( void ) { int reply; /* storing data received from other machine */ int wait_time; wait_time = WaitCount() + SYNC_TIME_OUT; if( MaxBaud == MIN_BAUD ) wait_time += SEC(1); for( ;; ) { /* loop until SYNC_END received or time out */ reply = WaitByte( 1 ); /* get character */ if( reply == SYNC_END ) break; /* SYNC_END received; continue */ if( reply == SYNC_BYTE ) { /* SYNC_BYTE received; send ACK */ SendByte( SDATA_ACK ); } else if( reply == SDATA_HI ) { /* return HI received */ SendByte( SDATA_HI ); } else if( WaitCount() >= wait_time ) { /* 2 sec time out */ return( FAIL ); } /* not yet time out; loop */ } return( SUCCESS ); }
void SyncPoint( unsigned tick ) { Wait( tick - WaitCount() ); }
static int BlockSend( trap_elen num, byte *p, unsigned timeout ) { word crc_value; /* crc value of block */ unsigned wait_time; /* timer for testing time-out */ trap_elen i; /* loop index */ int reply; /* reply message from other machine */ unsigned char crc_low, crc_hi; unsigned char len_low, len_hi; byte extra[3]; /* ..[0]=blkno_low, ..[1]=blkno_hi, ..[2]=err */ unsigned wait; ZeroWaitCount(); extra[2] = PrevErrors = Errors; if( Errors > 255 ) { extra[2] = 255; /* because it is a char, not an int */ } Errors = 0; ClearCom(); /* compose send buffer contents */ len_low = num & 0xff; /* low 8 bits of data block length */ len_hi = num >> 8; /* high 8 bits of data block length */ extra[0] = SendBlkNo & 0xff; /* low 8 bits of send block no */ extra[1] = SendBlkNo >> 8; /* high 8 bits of send block no */ crc_value = CRC( extra, num, p ); /* calculate crc for (blk#+err+data) */ crc_low = crc_value & 0xff; /* low 8 bits of crc_value */ crc_hi = crc_value >> 8; /* high 8 bits of crc_value */ wait = (MaxBaud == MIN_BAUD) ? SEC(2) : SEC(1); for( ;; ) { /* send block loop */ /* send the block */ StartBlockTrans(); SendByte( SDATA_STX ); SendByte( crc_low ); SendByte( crc_hi ); SendByte( len_low ); SendByte( len_hi ); SendByte( extra[0] ); /* blkno_low */ SendByte( extra[1] ); /* blkno_hi */ SendByte( extra[2] ); /* err */ for( i = 0; i < num; ++i ) { SendByte( p[i] ); } SendByte( SDATA_ETX ); StopBlockTrans(); wait_time = WaitCount() + timeout; for( ;; ) { /* wait proper acknowledgement loop */ reply = WaitByte( wait ); /* receive reply */ if( reply == SDATA_NO_DATA ) { if( (timeout != FOREVER) && (WaitCount() >= wait_time) ) { return( FAIL ); /* time-out */ } else { SendByte( SDATA_RLR ); /* request last response */ ++Errors; /* increment error counter */ } } else { if( reply == SDATA_ACK ) { ++SendBlkNo; return( SUCCESS ); /* done, exit from BlockSend() */ } else if( reply == SDATA_NAK ) { /* unsuccessful, re-send block */ ++Errors; break; /* break out of acknowledgement loop; i.e. back to send block loop */ } else if( reply == SDATA_RLR ) { /* request last response */ SendByte( LastResponse ); break; /* break out ackno loop; re-send block */ } else { /* things are totally messed up */ while( WaitByte( SEC(3)/4 ) != SDATA_NO_DATA ) ; /* discard all characters sent */ SendByte( SDATA_RLR ); /* request last response */ ++Errors; } } } } }