home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource3
/
140_01
/
cmodem.c
< prev
next >
Wrap
Text File
|
1985-03-11
|
14KB
|
611 lines
/*
CMODEM.C
Revision history: Latest revision first.
04/Jun/82 Removed PMMI defines and ifdef for Australia, much
more extensive documentation added, modem port
defines now obtained from BDSCIO.H. Code tidied up
a bit and VARBAUD ifdefs added, some messages
changed and numerous small changes. Bill Bolton
??/???/81 Patched together from existing Cnet and YAM code by
Steve Passe, Cnode SYSOP, USA.
This program uses the "Christensen" protocol as (used in
YAM/MODEM7) for file transfers.
When you execute CMODEM it signs on and immediately goes into
the terminal mode (without any messages) and is ready to send
ASCII text from your console keyboard out your modem port and
receive ASCII text from your modem port and display it on your
console. An "esc" convention allows file transfers without
leaving the program.
To send a file:
type '<esc> s <cr>',
To receive one:
type '<esc> r <cr>'
(you will be prompted for the file name in both cases)
For other commands:
type '<esc> ? <cr>'
(<esc> is the ASCII escape character)
If you should need to send an <esc> to the remote system simply
hit <esc> twice in a row for each <esc> that you wish to
send. While transfering files CMODEM echos everything, from the
file it sends or receives, to the console so be prepared to see
some weird things when transfering object files. It doesn't
send non-ASCII data (i.e. control codes) to the console but
replaces them with a full stop.
This is really just a very minimal implementation of a program
to send and receive files in using the "Christensen" protocols.
For a "full feature" communications program you should
definitely look at YAM.
Add whatever code necessary to the initialize_port() function
to setup your port for:
8 bits,
1 stop bit,
no parity,
baud rate of 300 if necessary,
etc.).
If you are certain that your modem port will always be
initialised in the correct data format before entry to CMODEM
(perhaps initialised at cold boot time) you can just leave this
function empty.
If your modem UART has programmable baud rates you (2651, 8250
or whatever) and you want to be able to set them from CMODEM
you should insert code in the baud() function and define
VARBAUD. This is probably not really worthwhile for Australian
conditions but I left the code in place in case anyone wants to
try it. It is easier to leave your UART set at 300 baud as the
CCITT V21 modems currently available in Australia just wont run
any faster than 300 baud....maybe that will change with digital
filtering in the future.
If you are using a direct connect modem and you want to be able
to drop the line when finished you should insert code in the
hangup() function.
The defines for the modem port must be set up in the BDSCIO.H
file for your system. Note that this program uses BDOS call 6
(direct console I/O) for all console access and therefore
should be easily usable by those with odd consoles that are not
easily defined in BDSCIO.H (such as Sorcerer or memory mapped).
If you have TELNET running you should be able to compile CMODEM
and run it without any modifications (make sure thae data
format as described above is correct).
This program uses large chunks of the YAM package for file
transfers (thanks Chuck).
*/
#include "bdscio.h"
#define SOH 0x01
#define EOT 0x04
#define ACK 0x06
#define NAK 0x15
#define CAN 0x18
#define RETRYMAX 10
#define TIMEOUT (-1)
#define PATHLEN 20
#define WCEOT (-2)
#define CLKMHZ 4 /* CPU speed in Mhz */
#define CONSTAT 2
#define CONIN 3
#define DIR_IO 0x06
#define INPUT 0xff
#define NORMAL 0x1c
#define ORIG 0x01
#define ANSWER 0x02
#define READY 0x5f
#define OPT300 0x20
#define OPT600 0X00
#define CLEAR 0x3f
#define FLAG char
int Baud;
FLAG Tfile, Rfile;
char Tname[PATHLEN], Rname[PATHLEN];
unsigned T1pause, Timeout;
char File_buf[BUFSIZ];
char Checksum, Lastrx;
int Wcj, Firstch;
main()
{
char received, to_send, in_modem(), getch(), escflag;
Tfile = Rfile = FALSE;
T1pause = 311*CLKMHZ;
escflag = NULL;
Baud = 300;
initialize_port();
printf("\n\CMODEM a 'Christensen' protocol file transfer program\n\n");
while (TRUE) {
if (received = in_modem()) {
putch(received);
}
else if (to_send = getch()) {
if (to_send == ESC) {
if (escflag) {
escflag = NULL;
out_modem(to_send);
}
else escflag = TRUE;
}
else {
if (escflag) {
escflag = NULL;
commands(to_send);
}
else out_modem(to_send);
}
}
}
}
char /* get incoming byte from modem */
in_modem()
{
if (inp(MSTAT) & MIMASK) { /* status & char ready bit */
return inp(MDATA); /* ok, get the char */
} /* or */
else return FALSE; /* return empty */
}
out_modem(out_char) /* send byte to modem */
char out_char;
{
while (!(inp(MSTAT) & MOMASK))
; /* wait */
outp(MDATA, out_char); /* finally, send it */
}
char /* keyboard 'hook' and 'filter' */
getch()
{
return bdos(DIR_IO, INPUT);
}
commands(cmd)
char cmd;
{
int baudrate;
switch (tolower(cmd)) {
#ifdef VARBAUD
case 'b':
printf("\n\n\tbaudrate: ");
scanf("%d", &baudrate);
baud(baudrate);
break;
#endif
case 'q':
hangup();
break;
case 'r':
printf("\n\n\treceive: ");
scanf("%s", Rname);
download(Rname);
break;
case 's':
printf("\n\n\tsend: ");
scanf("%s", Tname);
upload(Tname);
break;
case '?':
case 'h':
help();
break;
default:
printf("\nbad command: '%c'\n", cmd);
}
}
/*
Most of the following functions taken from YAM package by
Chuck Forsberg, BDS "C" UG disk Utilities #, some modifications made for
compatibility with cnode code.
*/
upload(filename)
char *filename;
{
int err_flag;
if(opentx(Tname)==ERROR) {
printf("\ncan't open '%s'\n", filename);
return ERROR;
}
printf("\n'%s' open for transmission\n", Tname);
if(wctx()==ERROR) {
abort('t');
}
return OK;
}
wctx()
{
int sectnum, attempts;
char txbuf[SECSIZ];
printf("Awaiting initial NAK - ");
while((Firstch=readbyt(400))!=NAK && Firstch!=TIMEOUT && Firstch!=CAN)
{
printf("\n\tgot 0x%2x, not NAK", Firstch);
/* let user see it if strange char */
}
if(Firstch==CAN)
return ERROR;
if (Firstch == TIMEOUT) {
printf("\ntimeout on initial NAK!");
return ERROR;
}
sectnum=1;
while(filbuf(txbuf, SECSIZ)) {
if(wcputsec(txbuf, sectnum)==ERROR) {
return ERROR;
}
else {
sectnum++;
}
}
closetx();
attempts=0;
do {
out_modem(EOT);
purgeline(); /* why? */
attempts++;
}
while((Firstch=(readbyt(100)) != ACK) && attempts < RETRYMAX)
; /* wait for ACK */
if(attempts == RETRYMAX) {
printf("\nNo ACK on EOT; Aborting... ");
return ERROR;
}
else
return OK;
}
wcputsec(txbuf, sectnum)
char *txbuf;
int sectnum;
{
char attempts, *cp, xbyt;
Firstch=0; /* part of logic to detect CAN CAN */
for(attempts=0; attempts <= RETRYMAX; attempts++) {
Lastrx= Firstch;
out_modem(SOH);
out_modem(sectnum);
out_modem(~sectnum);
Checksum=0;
for(Wcj=SECSIZ,cp=txbuf; --Wcj>=0; ) {
out_modem(*cp);
isprint(*cp) ? putchar(*cp) : putchar('.');
Checksum += *cp++;
}
out_modem(Checksum);
purgeline();
Firstch=readbyt(100);
if(Firstch==CAN && Lastrx==CAN) {
cancan:
printf("\nReceiver CANcelled transmission ");
return ERROR;
}
else if(Firstch==ACK)
return OK;
else if(Firstch==TIMEOUT)
printf("\nTimeout on sector ack attempt %d", attempts);
else {
printf("\nGot %2x for sector ACK attempt %d", Firstch, attempts);
for(;;) {
Lastrx=Firstch;
if((Firstch=readbyt(1))==TIMEOUT)
break;
if(Firstch==CAN && Lastrx==CAN)
goto cancan;
}
}
}
printf("\nNo ACK on sector; Abort ");
retur