! 17 Aug 90     &>>AT&T 2224 CEO Modem<<
!
! Terrence 
! Devan R. Green
!
! mdm2224.cmd		-	AT&T 2224 CEO modem dialer
!
! AT&T 2224 CEO modem dialer (AT&T 2224 CEO users manual used as reference;
!				 same modem used for testing)
!
! this dialer script has entry procedures dial, hangup, and answer
!

!
! the "not responding" message.  It's a little more verbose, but it's worth
! saving them a phone call by making sure that:
!		1) modem is reset
!		2) cable is ok
!		3) The front and rear switches are set correctly
!
proc notrsp
	? "The modem dialer you are using assumes that you are using the AT&T"
	? "command set (referred to as 'The AT&T Command Protocol').  If"
	? "you have been using the Hayes ('AT') command set with another"
	? "software package, you should probably use the Hayes modem dialer"
	? "rather than this one.  If you are still having problems, check"
	? "your cable and switch settings to see that they are correct..."
	? "switches should be as follows:   1  2  3  4  5  6  7  8 "
	? "                                 dn dn dn dn up dn dn dn" 
	? "Other settings should be as follows:"
	? "   o12=y   o13=n   o22=:   o21=0"
	? ""
endproc notrsp

!
! call number [redials [timeout]]
!
proc call
on abort
! toggle it to disconnect
  init
  display "Call aborted, hanging up"
  xmit "@\r"		! Cancel character stops command execution
  wait 2		! defaults to "@"
  flush
  return 2
endon
on error
  do @dhangup
  return 1
endon

! Determine Phone Number and Baud Rate
if '$1' = ''
  display "Usage: dial number [redials] [timeout]"
  return 1
endif

if '$2' = ''
  setvar n 3
else
  setvar n $2
endif
if '$3' = ''
  setvar tmout 30
else
  setvar tmout $3
endif

display "Calling $1"
     repeat
	xmit "\$:\r\$B\r@\r"
	xmit "\r\r"
	wait ":",10
	if _timeout
	setvar n n-1
	 if n <= 0
		? "Modem not responding"
		do @notrsp
		return 2
	 endif
	endif
	flush
     until !_timeout
	! set options to necessary values
	xmit "E0\r"			! local echo off
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "I0\r"			! Immediate dialing off
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "O7=y\r"			! auto answer on
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "O20=1\r"			! answer on ring 1
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "\\V0\r"			! turn on result codes
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "\\Q0\r"			! disable modem<->computer flow control
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "\\G0\r"			! disable modem<->modem flow control
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "F0\r"			! disable flow control
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "\\X1\r"			! Pass ^s ^q through
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "\\N1\r"			! Operating mode direct 
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "O55=y\r"			! Carrier detect follows carrier
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "O63=y\r"			! CTS, DSR always on
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	xmit "O54=n\r"			! Hang up if we drop DTR
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
	flush
repeat
	xmit "t$1\r"			! dial the number
	wait "Answered|Connected|No answer|Busy|No dial tone",$tmout
	setvar comstat _retval
! _retval -- ans=0 con=1 noans=2 busy=3 nodt=4
	if _timeout
		setvar n n-1
		if n <= 0
			display "Too many retries, call aborted"
			do @dhangup
			return 2
		endif
		do @dhangup
		display "Redialing..."
	else
	switch
	case comstat=0
		display "Call connected"
		return 0
	case comstat=1
		display "Call connected"
		return 0
	case comstat=2
		display "No Answer"
		setvar n n-1
		if n <=0
			display "Too many retries, call aborted"
			do @dhangup
			return 2
		endif
		do @dhangup
		display "Redialing..."
	case comstat=3
		display "Busy"
		setvar n n-1
		if n <=0
			display "Too many retries, call aborted"
			do @dhangup
			return 2
		endif
		do @dhangup
		display "Redialing..."
	case comstat=4 
		display "No Dial Tone"
		setvar n n-1
		if n <=0
			display "Too many retries, call aborted"
			do @dhangup
			return 2
		endif
		do @dhangup
		display "Redialing..."
	default
		display "Unable to connect"
		setvar n n-1
		if n <=0
			display "Too many retries, call aborted"
			do @dhangup
			return 2
		endif
		do @dhangup
		display "Redialing..."
	endswitch
	endif
until f
endproc call

!
! hangup line and terminate connection
!
proc hangup
display "Hanging up phone"
! toggle DTR to disconnect
init
xmit "@\r"
xmit "\$:"		! go to interactive mode
wait ":",4
if _timeout
	! toggle DTR to disconnect
	init
	xmit "@\r"
	xmit "\$:\r"		! go to interactive mode
	xmit "\$B\r"
	wait ":",4
	xmit "q\r"
endif
xmit "q\r"
endproc hangup

!
! answer [code]
!
proc answer
if "$1"=""
	? "Usage: @answer code [off wait auto now]"
	return 1
endif
setvar code "$1"
	on abort
	  display "Answer aborted"
	  return 2
	endon
	xmit "@\r\$:\r\$B\r\r"			! 8,none,1 for file transfers
	wait ":",1
	if _timeout
		do @notrsp
		return 1		! modem not responding
	endif
if code = "wait"			! wait for ring BLOWNOUT by S7 timeout
	xmit "O7=y\r"
	wait ":",1
	xmit "O20=1\r"
	display "Waiting for Ring..."
	dwait "Incoming",250	! only way to do it.  Sorry.
	xmit "ans\r"
	wait 1
	flush
	return 0
elseif code = "auto"					! autoanswer ON
	xmit "O7=y\r"
	wait ":",1
	xmit "O20=1\r"
	wait ":",1
	if _timeout
		? "Modem not responding..."
		? "Autoanswer can NOT be set"
		return 1		! modem not responding
	endif
	xmit "q\r"
	? "Autoanswer now on."
	return 0
	flush
elseif code = "now"					! answer NOW
	display "Answering phone"
	xmit "ANS\r"		! only accessable if on hook
	wait 1
	flush
	return 0
elseif code = "off"					! autoanswer OFF
	xmit "O7=n\r"
	wait ":",1
	if _timeout
		? "Modem not responding..."
		? "Autoanswer can NOT be reset"
		return 1		! modem not responding
	endif
	? "Autoanswer now off."
	return 0
	flush
endif
endproc answer

!
! hangup for redial attempts, local procedure
!
proc dhangup
xmit "@\r"	! an at (@) sign will abort a dial in progress
wait 1
flush
endproc hangup

!set ddtr on		! dtr required, most modems
!set dtr on		! dtr required, most modems
! next line in case of prior carrier loss on system V systems
!init
