! 13 Jul 87    &>>TANDY 2212<<
!
! mdm2212.cmd			tandy dc-2212 modem dialer script
!
!			Based on information and modem provided by
!			Willie Colon, WAC Productions, NY
!
!	v1.0		Terry Lambert, Century software
!
!	Notes:	Beware of dangling control-T or other abort characters, if
!		you have programmed the abort character to something else!
!		abort character is read by call routine, but defaults to
!		^T if "call" not used to dial number.  ^T or other abort
!		you MUST send another character (anything but another abort!)
!		within 3 seconds or this will hang up the phone!  This may be
!		a pain, but it lets you transfer binary files AND hang up the
!		phone without shutting the modem off.
!
! IMPERITIVE:	switch on back of modem MUST be set to ANSWER OUT!
!		Failure to do so will result in this script refusing,
!		unconditionally, to cooperate with you!
!		

dimstr mdmgrikz 14		! must be there in case call wasn't used
				! but hangup was

proc getstatus
	setvar i 0
	repeat
		xmit "*."
		wait "*",1		! should comeback stat
		if _timeout		! didn't comeback
			setvar i i+1
		endif
	until i=5 | !_timeout
	if i=5
		? "Cannot wake up modem..."
		?
		? "  Make sure that your modem is plugged in, on, and that"
		? "  the 'TR' and 'CD' lights are lit.  If just the 'TR'"
		? "  is lit, make sure that the autoanswer switch on the"
		? "  rear of the modem is set to 'OUT', and reset the modem"
		?
		abort
	endif
	xmit "L"			! list current parameters
	wait "L\r\n"			! throw away echoed "L\r\n"
	setvar i 0
	dimstr mdmgrikz 14		! highly unlikely to be used by someone
	repeat
		cread x
		if i=1 & left(x,14)!=" 10 BITS/CHAR:" ! not in list at 300 baud
			setvar i i+1
		endif
		setvar mdmgrikz[i] midstr(x,pos(":",x,1)+1,80)
		setvar i i+1
	until i>=5			! only need this many
	wait "SLOW DIAL"		! blow it out down to this one
	wait "\n"			! zik out the last linefeed
endproc

!
! call [number] [redials] [timeout]
!

proc call
	? "Setting modem parameters, please wait..."
	do @getstatus

	on abort
		display "Call aborted, hanging up"
		cdelay 99
		xmit "\r*."
		cdelay 0
		wait "DISCONNECT",5
		if mdmgrikz[2]!=""
			setvar mdmabort "\\x"+left(mdmgrikz[2],2)
		else
			setvar mdmabort "^T"
		endif
		xmit "$(mdmabort)"
		wait 5			! needs 3 so 5 is overkill
		flush
		return 2
	endon

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

	if '$2' = ''
		setvar mdmredials 3	! default to 3 redial attempts
	else
		setvar mdmredials val('$2')
	endif
	if mdmredials<1 | mdmredials>9
		setvar mdmredials 3	! bad val:default to 3 redial attempts
	endif

	if '$3' != '60' & '$3' !=''
		? "DC-2212 timeout is based on # of allowed rings, which is"
		? "about 60 seconds and cannot be altered.  To make this"
		? "message go away, set 'dtimeout' to 60 in setup."
	endif

* set up redial stuff

	cdelay 99			! DC-2212 is a slow reader
	flush
	xmit "I"				! advanced programming mode
	wait "?"				! autoans rings
	wait 1
	xmit "\r"				! leave the same
	wait "?"				! redial attempts
	wait 1
	xmit "(mdmredials+1)"			! user specified
	wait "?"				! abort after n rings
	wait 1
	xmit "8"				! 8 rings (time=about 60 secs)
	wait "?"				! hangup on lost carrier
	wait 1
	xmit "Y"				! yes, we want to
	wait "?"				! send break on abort char
	wait 1
	xmit "N"				! may confuse UNIX if we do
	wait "?"				! received break disconnects
* display is about here so can put phone on line in 3 sec interval
* so 5 second phone timeout for PBX CBX phones doesn't occur
display "Calling $1"
	wait 1
	xmit "N"				! we want to see breaks in TERM
	wait "?"				! abort char enabled?
	wait 1
	xmit "Y"				! yes.  see comments at start

	wait 1
	xmit "DT"			! D>ial with T>ones
	xmit "$1"			!    this phone number
	xmit "X"			!       execute dial command
	cdelay 0			! done talking to modem
	wait "X"			! throw away echoed command

* main "waiting for a connection" sequence:

	setvar mdmcnnct false			! true if connected
	repeat
		wait "\r\n",80			! wait for completion code
		if _timeout
			display "Modem not responding, call aborted"
			return 1
		endif
		setvar mdmrvlu comin()		! get first character of result
		switch
		case mdmrvlu="B"		! busy signal
						! ignore... handled by modem
		case mdmrvlu="D"
*		Ignore.  "DIALING...xxxxx" can be leftover from placing the call
		case mdmrvlu="R"
*			? "RING"
		case mdmrvlu="O"
			? "Call connected"
			setvar mdmcnnct true	! set connect flag true
		case mdmrvlu="N"
			? "No dial tone... make sure the modem is connected to"
			? "the telephone line properly."
			flush
			return 2
		default
			? "Unknown condition: $(mdmrvlu)"
			abort
		endswitch
	until mdmcnnct
	flush
endproc

!
! hangup line and terminate connection
!
! assumes call procedure was used to dial or that abort char is ^T
proc hangup
	if mdmgrikz[2]!=""
		setvar mdmabort "\\x"+left(mdmgrikz[2],2)
	else
		setvar mdmabort "^T"
	endif
	display "Hanging up phone"
	xmit "$(mdmabort)"
	wait 5			! needs three so 5 is overkill
endproc hangup

!
! answer [code]
!
proc answer
on abort
	xmit "*"
	wait 1
	xmit "OX"		! reenable originate mode
	return 2
endon
if "$1"=""
	? "Usage: @answer code [wait auto now off]"
	return 1
endif
setvar code "$1"

if code = "wait"		! wait for ring
	? "Setting modem parameters, please wait..."
	do @getstatus

	cdelay 99			! DC-2212 is a slow reader
	flush
	xmit "I"				! advanced programming mode
	wait "?"				! autoans rings
	wait 1
	xmit "\r"				! leave the same
	wait "?"				! redial attempts
	wait 1
	xmit "(mdmredials+1)"			! user specified
	wait "?"				! abort after n rings
	wait 1
	xmit "8"				! 8 rings (time=about 60 secs)
	wait "?"				! hangup on lost carrier
	wait 1
	xmit "Y"				! yes, we want to
	wait "?"				! send break on abort char
	wait 1
	xmit "N"				! may confuse UNIX if we do
	wait "?"				! received break disconnects
	wait 1
	xmit "N"				! we want to see breaks in TERM
	wait "?"				! abort char enabled?
	wait 1
	xmit "Y"				! yes.  see comments at start

	wait 1
	? "DC-2212 will answer phone after 2 rings:"
	xmit "AX"
	wait "ON LINE ANSWER"			! wait til this (only valid)
elseif code = "auto"		! autoanswer ON
	? "DC-2212 modems can not perform the 'ANSWER AUTO' function"
	display "Answer aborted"
	return 2
elseif code = "now"		! answer NOW
	? "DC-2212 modems can not perform the 'ANSWER NOW' function"
	display "Answer aborted"
	return 2
elseif code = "auto"		! autoanswer OFF
	? "DC-2212 modems can not perform the 'ANSWER OFF' function"
	display "Answer aborted"
	return 2
endif
endproc answer
