#include #define INCL_DOS #include #include #include "ttyio.h" extern int Debug; int _modem; /* a readwrite attachment to the modem */ DCBINFO _modcontrol; /* the way the modem looks */ DCBINFO _modsavedctl; /* .. and looked, before we got to it */ /* * ttyopen() attaches ourself to a modem port */ int ttyopen(dev, flow) char *dev; int flow; { short info; char device[20]; static char control[3] = { 8, 0, 0 }; sprintf(device, "/dev/%s", dev); if (DosOpen(device, &_modem, &info, 0L, 0, 0x01, 0x2042, 0L) == 0) { /* opened the file - so now get the existing modem config * and replace it with our own */ DosDevIOCtl(&_modsavedctl, (int*)0, 0x73, 1, _modem); _modcontrol = _modsavedctl; _modcontrol.usWriteTimeout = 0; /* no write timeout */ _modcontrol.usReadTimeout = 0; /* no read timeout */ _modcontrol.fbCtlHndShake = (flow==TTYF_RTSCTS) ? 0x08 : 0; _modcontrol.fbFlowReplace = (flow==TTYF_RTSCTS) ? 0xc0 : 0; _modcontrol.fbTimeout = 0x04; /* normal timeout processing */ DosDevIOCtl((int*)0, &_modcontrol, 0x53, 1, _modem); /* set the modem up for 8 characters, no parity, 1 stop bit */ DosDevIOCtl((int*)0, control, 0x42, 1, _modem); enable(); return 1; } _modem = -1; return 0; } /* ttyopen() */ /* * ttyclose() detaches ourself from a modem port and restores it to * the state it was in before we started hacking on it */ void ttyclose() { disable(); DosDevIOCtl((int*)0, &_modsavedctl, 0x53, 1, _modem); DosClose(_modem); } /* ttyclose() */ /* * ttystat() tells us if there are characters waiting on input */ int ttystat() { int control[2]; DosDevIOCtl(control, (int*)0, 0x68, 1, _modem); return (control[0] > 0); } /* ttystat */ /* * dobreak() set or clear a break condition */ void dobreak(setit) int setit; { int status; DosDevIOCtl(&status, (int*)0, setit ? 0x4b : 0x45, 1, _modem); } /* * ttyspeed() set tty speed */ int ttyspeed(speed) long speed; { int intspeed; if (speed < 0 || speed > 19200) return 0; intspeed = (int)speed; return (DosDevIOCtl((int*)0, &intspeed, 0x41, 1, _modem) == 0); } /* * carrier() - detects carrier * * note: we don't want to spend too much time doing ioctls, so we'll only * check for carrier on 1-second intervals; if we've checked within the * last second, we'll return the value we found then. */ int carrier() { static char _cd_carrier = 0; /* latest carrier-detection flag */ static char _cd_active = 0; /* have we checked at all? */ static clock_t _cd_stale= 0; /* time when _cd_carrier goes stale */ unsigned char flags; clock_t now; now = clock(); if (_cd_active == 0 || now > _cd_stale) { DosDevIOCtl(&flags, (int*)0, 0x67, 1, _modem); _cd_stale = now+1000L; /* only check cd at 1 sec intervals */ _cd_active = 1; _cd_carrier = flags & (0x80); } return _cd_carrier; } /* carrier */ /* * disable the modem */ void disable() { char control[2]; int status; control[0] = 0x00; control[1] = 0xfe; DosDevIOCtl(&status, control, 0x46, 1, _modem); } /* disable */ /* * enable the modem */ void enable() { char control[2]; int status; control[0] = 0x01; control[1] = 0xff; DosDevIOCtl(&status, control, 0x46, 1, _modem); } /* enable */ /* * flush() the modem input buffer */ void flush() { while (receive(0) >= 0) ; } /* flush */ /* * receive() a character from the modem, timing out after a while */ int receive(timeout) int timeout; { ttytimeout(timeout); return ttyin(); } /* receive */ /* * ttyin() gets a character from the modem */ int ttyin() { unsigned char c; return (read(_modem, &c, 1) == 1) ? (0xff & c) : (-1); } /* receive */ /* * ttytimeout() changes the tty read timeout */ ttytimeout(timeout) int timeout; { static int _prev_timeout = -1; if (timeout == _prev_timeout) return 0; _prev_timeout = timeout; /* do an ioctl to set up the new improved read timeout */ _modcontrol.usReadTimeout = timeout * 100; if (timeout == 0) _modcontrol.fbTimeout |= 0x06; else _modcontrol.fbTimeout = (_modcontrol.fbTimeout & ~ 0x06) | 0x04; DosDevIOCtl((int*)0, &_modcontrol, 0x53, 1, _modem); } /* ttytimeout */ /* * slowputs() writes a string to modem s*l*o*w*l*y */ void slowputs(s) register char *s; { register long delay; while (*s) { if (*s & 0200) { /* anything > 127 causes delay */ delay = (0xff & *++s); nap(delay * 100L); if (Debug > 1) fprintf(stderr, "{%ld}", delay); } else { if (Debug > 1) putc((*s == '\r') ? '\n' : (*s), stderr); nap(80L); ttyout(*s); } ++s; } } /* receive */ /* * ttyout() writes a single character to the modem */ void ttyout(c) char c; { int sz; DosWrite(_modem, &c, 1, &sz); } /* ttyout */ /* * ttywrite() writes a string to the modem as fast as possible */ void ttywrite(bfr, size) register char *bfr; register size; { int sz; DosWrite(_modem, bfr, size, &sz); } /* ttywrite */