
/*
 *  MODEM.C
 *
 *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
 *
 *  $Header: Beta:src/uucp/src/uucico/RCS/modem.c,v 1.1 90/02/02 11:56:00 dillon Exp Locker: dillon $
 */

#include "includes.h"
#include "uucp.h"
#include "log.h"

Prototype void openline(void);
Prototype int  get_baud(void);
Prototype void modem_init(void);
Prototype int  dial_nbr(const char *);
Prototype void reset_modem(void);

/*
 *  NOTE:   modem stuff pretty much ignored if we are run from
 *	    a getty.
 */

#define MULTIMODEM	/*  I have a multi-modem    */

extern int Getty;
extern int IgnoreCD;



void
openline()
{
    signal(SIGINT,sigint);
    IgnoreCD |= 4;
start_again:
    chkabort();
    if (instr("CONNECT", 7))
	goto start_again;


#ifdef MULTIMODEM

#else
    set_baud(get_baud());
#endif
    Delay(120); /* sleep 2 seconds */

    IgnoreCD &= ~4;
}

#ifndef MULTIMODEM

int
get_baud()
{
/* We've seen the CONNECT message, now we must see what baud rate
   we've connected with */
/* gather input until \r then see if it's 300, 1200, or 2400 */
/* this is for hayes compatibles */

	int data;
	char rate[10];
	int rate_inx = 0;

	DEBUG(2,"looking for baud rate\n",0);

	while ( ((data = xgetc(BYTE_TO)) != EOF) && ((char)data != '\r')) {
	    if ((char)data == ' ') continue;
	    rate[rate_inx++] = (char)data;
	}
	DEBUG(2, "found baud rate of %s\n", rate);
	if (strncmp(rate,"1200",4) == 0) return 1200;
	if (strncmp(rate,"2400",4) == 0) return 2400;
	if (rate_inx == 0) return 300;
	return 1200;  /* default */
}

#endif

void
modem_init()
{
    reset_modem();
}

/*
 * Simple dialer routine.  Needs replacement with a full blown
 * script driven dialer.  Next week maybe :-).	FIXME.
 */

int
dial_nbr(nbr)
const char *nbr;
{
    char  *dial;
    int   i;

    IgnoreCD |= 4;

    dial = malloc(strlen(nbr) + 16);

    dial[0] = 0;
    if (strncmp(nbr, "AT", 2) != 0 && strncmp(nbr, "at", 2) != 0)
	strcpy(dial, "ATDT");
    strcat(dial, nbr);
    strcat(dial, "\r");

    DEBUG(2,"dialing %s\n", dial);

    twrite(dial, strlen(dial));

    i = instr("CONNECT", 7);

    if (i == 0)     /*  for those modems which don't bring CD up */
	Delay(50);  /*  immediately                              */

    IgnoreCD &= ~4;

    free(dial);

    return (i);
}

/*
 *  RESET_MODEM()
 *
 *  If run from a Getty we do NOT reset the modem, which would
 *  disconnect an already connected connection.
 *
 *  Note that the delay between CloseSerial() and OpenSerial() only
 *  serves to give the Getty, if running, time to lock the port and
 *  begin a disconnect sequence.
 */

void
reset_modem()
{
    if (Getty)          /*  called from a getty             */
	return;
    DEBUG(4, "Beg-Reset\n", 0);
    CloseSerial();      /*  drop dtr            */
    Delay(50*3);        /*  delay 3 seconds     */
    DEBUG(4, "End-Reset-1\n", 0);
    OpenSerial();       /*  re-open serial      */
    DEBUG(4, "End-Reset-2\n", 0);
}

#ifdef NOTDEF

    if (GettyCmd(DeviceName, DeviceUnit, '0', NULL) == 0)
	return;

    /*
     *	Getty doesn't exist, we have to reset the modem ourselves!
     */

    IgnoreCD |= 4;

#ifdef MULTIMODEM
    set_baud(19200);

    if (CheckCarrier()) {
	Delay(60);
	twrite("+++", 3);
	Delay(120);
    }
    twrite("ATH0\r", 5);
    instr("OK\r\n", 4);
    twrite("ATZ\r", 4);
    instr("OK\r\n", 4);
    twrite("ATZ\r", 4);
    instr("OK\r\n", 4);

    /*amiga_closeopen();*/
    sprintf(init, "%s\r", "ATM0S0=2X4$BA0");
    if (debug > 0)
	init[3] = '1';
    twrite(init, strlen(init));
    instr("OK\r\n",4);
#else
    twrite("+++", 3);
    Delay(60);
    twrite("ATH0\r", 5);
    Delay(120);
    twrite("ATZ\r", 4);
    Delay(60);
    twrite("ATZ\r", 4);
    Delay(60);
    sprintf(init, "%s\r", "ATS2");
    twrite(init, strlen(init));
    Delay(60);
#endif
    IgnoreCD &= ~4;
}

#endif

