ftp.nice.ch/pub/next/unix/communication/newam.0.1.s.tar.gz#/newam-0.1/src/dial.c

This is dial.c in view mode; [Download] [Up]

/*********************************************************************/
/*                                                                   */
/* Programmer:                                                       */
/*   Olaf Mueller <olaf@orest.escape.de>                             */
/*                                                                   */
/* Purpose:                                                          */
/*   Answering Machine                                               */
/*       dialing with a ZyXEL modem                                  */
/*                                                                   */
/* History:                                                          */
/*   29-08-96   Initial Release           Olaf Mueller               */
/*                                                                   */
/* Notes:                                                            */
/*                                                                   */
/*********************************************************************/

#include <libc.h>

#include "const.h"
#include "misc.h"
#include "device.h"
#include "iodev.h"


#define NoDEBUG


static int		line = 0 ;
static char		port ;
static int		hangupNeeded = 0 ;


#ifdef DEBUG

static int dialog (int line,long msec,char *until,char *question,...)
{
	va_list		ap ;
	char		buffer [1000] ;
	long		usedtime ;
	char		sbuf [1000] , rbuf [1000] ;
	int			len , rc = -1 ;

	va_start (ap,question) ;
	vsprintf (buffer,question,ap) ;
	va_end (ap) ;

	debugsputs (sbuf,buffer) ; debugsputs (rbuf,until) ;
	printf ("send \"%s\" wait for \"%s\"\n",sbuf,rbuf) ;
	len = strlen (buffer) ;
	if (write(line,buffer,len) == len)
	{
		if ((usedtime = readUntil(line,msec,until)) >= 0L)
		{
			printf ("got it after %ld msec\n",usedtime) ;
			rc = 0 ;
		}
		else
			printf ("reading failed (Timeout after %ld msec)\n",msec) ;
	}
	else
		printf ("writing failed\n") ;
	return  rc ;
}

#else

static int dialog (int line,long msec,char *until,char *question,...)
{
	va_list		ap ;
	char		buffer [1000] ;
	long		usedtime ;
	int			len , rc = -1 ;

	va_start (ap,question) ;
	vsprintf (buffer,question,ap) ;
	va_end (ap) ;

	len = strlen (buffer) ;
	if (write(line,buffer,len) == len)
		if ((usedtime = readUntil(line,msec,until)) >= 0L)
			rc = 0 ;
	return  rc ;
}

#endif


static void signalhandler (int sig)
{
	switch (sig)
	{
	case SIGINT:
	case SIGTERM:
	case SIGTSTP:
		if (line > 0)
		{
			if (hangupNeeded)
				dialog (line,2000L,"OK\r\n","ATH\r") ;
			printf ("\ndial: break\n") ;
			closeDevice (line) ;
			unlockDevice (DIALOUT,port) ;
			exit (1) ;
		}
		break ;
    }
}




int main (int argc,char *argv[])
{
	int			rc , ringcount ;
	char		buf [1000] ;


	if (argc != 3)
	{
		printf ("usage: dial <port> <number_to_dial>\n") ;
		return 1 ;
	}
	port = *argv[1] ;
	if (strlen(argv[1]) != 1  ||  (port != 'a'  &&  port != 'b'))
	{
		printf ("dial: bad port description (use \'a\' or \'b\')\n") ;
		return  1 ;
	}

	signal (SIGALRM,signalhandler) ;
	siginterrupt (SIGALRM,1) ;
    signal (SIGINT,signalhandler) ;
    signal (SIGTERM,signalhandler) ;
    signal (SIGTSTP,signalhandler) ;

	if ((rc = lockDevice(DIALOUT,port)) == 0)
	{
		if ((line = openDevice(DIALOUT,port)) >= 0)
		{
			initDevice (line,B38400) ;
			printf ("dial: initializing modem\n") ;
			dialog (line,2000L,"OK\r\n","AT\r") ;
			dialog (line,2000L,"OK\r\n","ATZ\r") ;
			dialog (line,2000L,"OK\r\n","ATE1M2L5&D3S0=0\r") ;
			dialog (line,2000L,"OK\r\n","AT+FCLASS=8\r") ;
			printf ("dial: dialing...\n") ;
			dialog (line,2000L,"\r","ATDT%s\r",argv[2]) ;
			for ( ringcount = 0 ; ; )
			{
				if (readUntil(line,40000L,"\r\n") != -1L  &&
							readAnswerUntil(line,500L,buf,sizeof(buf),"\r\n") != -1L)
				{
					if (!strcmp(buf,"BUSY"))
					{
						printf ("dial: Sorry: line busy\n") ;
						hangupNeeded = 0 ;
						break ;
					}
					else if (!strcmp(buf,"NO DIALTONE"))
					{
						printf ("dial: Sorry: no dial tone\n") ;
						hangupNeeded = 0 ;
						break ;
					}
					else if (!strcmp(buf,"NO CARRIER"))
					{
						printf ("dial: Sorry: no carrier\n") ;
						hangupNeeded = 0 ;
						break ;
					}
					else if (!strcmp(buf,"NO ANSWER"))
					{
						printf ("dial: Sorry: no answer\n") ;
						hangupNeeded = 0 ;
						break ;
					}
					else if (!strcmp(buf,"VCON"))
					{
						printf ("dial: voice connection\n") ;
						hangupNeeded = 1 ;
						break ;
					}
					else if (!strcmp(buf,"RINGING"))
					{
						printf ("dial: ringing %d\n",++ringcount) ;
						hangupNeeded = 1 ;
					}
				}
				else
				{
					hangupNeeded = 1 ;
					break ;
				}
			}
			if (hangupNeeded)
				while (1)
					sleep (1000) ;
			closeDevice (line) ;
		}
		else
			printf ("dial: cannot open dial out line\n") ;
		unlockDevice (DIALOUT,port) ;
	}
	else
		printf ("dial: cannot lock dial out line\n") ;
	return  rc ;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.