! 13 Oct 87     &>>Racal-Vadic<<
!
! Terrence R. Lambert
!
! mdmrv24p.cmd	- Any Racal-Vadic modem, Rev 2.1
!
! 12 Oct 87	- it seems they just can't decide on a standard user interface
!		  for their modems.  Had to worry about a new space after the
!		  "HELLO:", so I just cut it off.  Next it'll be a space before
!		  or something.  Oh well.
! 13 Jul 87	- fixed answer procedure
! 27 May 87	- fixed "INVALID COMMAND" to find it even though RACAL-VADIC
!		  is different from modem to modem (hardly believed it myself!)
! 14 May 87	- original working version
!
! Racal-Vadic modem dialer (2400pc modem and manual used as reference)
! 			   (2400pa modem and manual used for 12 Oct 87 fix)
!
! 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
!
proc notrsp
	? "Modem not responding.  If you have been using the 'AT' command set"
	? "with the modem, you must either reset it, wait for it to time out,"
	? "or use the HAYES modem dialer.  If you have not been using the 'AT'"
	? "commands, make sure that your modem is plugged in, the power is on,"
	? "and that your cable is configured and connected properly.  R-V type"
	? "modems require you provide CTS and DTR signals.  Check to see that"
	? "the switches (if any) are set properly.  This is doubley important"
	? "for internal modems."
endproc notrsp

!
! take modem from unknown to (hopefully) known state.  Due to the way
! the racal vadic works, it is not possible to enter commands unless you are
! at a point where the phone is hung up; this means you may get an unhappy
! message if you aren't hung up before trying something.
!
proc init
	set dtr on		! dtr required, most racal vadics
	setvar rac_tries 0
	repeat
		flush
		xmit "^E\r"	! wakeup modem to * prompt if not there already
		cread rsp,2,80	! 2; resolution of alarms on unix is 1 second
		setvar rac_tries rac_tries+1
		if rac_tries > 4
			do @notrsp
			return 512
		endif
	until pos("HELLO:",rsp,1)!=0 | pos("INVALID COMMAND",rsp,1)!=0
	xmit "\r"
	wait "*",1
	if _timeout		! it should already be there
		do @notrsp
		return 512
	endif
! software switch settings:
!	       v------------ option 01: not used			 - 1
!	       |v----------- option 02: DSR follows DTR			 - 1
!	       ||v---------- option 03: CXR follows true carrier	 - 1
!	       |||v--------- option 04: Autoanswer disabled		 - 2
!	       ||||v-------- option 05: Do not echo commands		 - 2
!	       |||||v------- option 06: Tone first, pulse second	 - 1
!	       ||||||v------ option 07: Make sure of dial tone		 - 1
!	       |||||||v----- option 08: Use call progress reporting	 - 1
!	       ||||||||v---- option 09: Full (verbose) responses	 - 1
!	       |||||||||v--- option 10: 10 bits(8+start+stop | 7+parity) - 1
!	       ||||||||||v-- option 11: Allow ^CD to hangup (disconnect) - 1
!	       |||||||||||
	xmit "O11122111111\r"
	wait "*",2
	if _timeout
		? "? Unable to set one or more required modem options"
		return 512
	endif
endproc init

!
! call number [redials [timeout]]
!
proc call
on abort
  set dtr off		! turn it off to disconnect... ^CD not work every time
  display "Call aborted, hanging up"
  xmit "\r"		! <CR> aborts call in progress, as well as other cmds
  wait 2
  flush
  set dtr on		!1 turn it back on to make it happy otherwise
  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

setvar rac_tout 60
if '$3' != ''
  setvar rac_tout val("$3")
endif

display "Calling $1"

	do @init		! modem initialization
	if _errno=512		! error on init
		return 1	! so abort process
	endif
repeat
	xmit "D $1\r"		! dial the number
	wait 5
	wait "ON LINE",$(rac_tout)
	if _timeout
		flush
		do @dhangup
		return 1	! dial error
	else
		display "Call connected"
		return 0
	endif
until f
endproc call

!
! hangup line and terminate connection
!
proc hangup
	display "Hanging up phone"
	set dtr off	! turn it off to disconnect... ^CD not work every time
	wait 2
	xmit "^CD"
	dwait "IDLE",4
	if _timeout
		wait 2
		xmit "^CD"
		dwait "IDLE",4
	endif
	set dtr on	! 1 turn it back on to make it happy otherwise
endproc hangup

!
! answer [code]
!
proc answer
	if "$1"=""
		? "Usage: @answer code [now wait off auto]"
		return 1
	endif
	setvar code "$1"
	do @init		! modem initialization
	if _errno=512		! error on init
		return 1	! so abort process
	endif
	on abort
	  display "Answer aborted"
	  return 2
	endon
if code = "wait"		! wait for ring BLOWNOUT
	xmit "O11122111111\r"	! only change answer option
	wait "*",4
	if _timeout
		display "Cannot wait for ring, modem not responding"
		return 1
	endif
	flush
	dwait "RINGING...\r\n"
	display "Answering phone"
	xmit "A\r"
	wait 1
	return 0
elseif code = "auto"		! autoanswer ON
	xmit "O11112111111\r"	! only change answer option
	wait "*",4
	if _timeout
		display "Autoanswer NOT set, modem not responding"
		return 1
	endif
	flush
elseif code = "now"		! answer NOW
	display "Answering phone"
	xmit "A\r"
	return 0
elseif code = "off"		! autoanswer OFF
	xmit "O11122111111\r"	! only change answer option
	wait "*",4
	if _timeout
		display "Autoanswer NOT reset, modem not responding"
		return 1
	endif
	flush
endif
endproc answer

!
! hangup for redial attempts, local procedure
!
proc dhangup
	xmit "\r"	! a carriage return will abort a dial in progress
	wait 2
	flush
endproc hangup
