{ ROS.MDM - Remote Operating System Modem Dependent Routines }

{ File:        SM-CLOSE.MDM   --- modified for AMSTRAD PCW 8xxx ---
  Description: This  driver  is designed to support modems which use the  'AT'
               command set such as the Hayes Smartmodem, the Courier 2400, and
               others.   It ensures that the modem has correctly received  the
               command  sent from the computer by monitoring the echo from the
               modem.   This  prevents  potential problems from  modems  which
               reset or lose characters when a call and a command are received
               simultaneously.
  Date:        7/28/88
  Authors:     Jacques Durosier, Steve Fox
  Credits:     Chris Hueuser, Richard Transue, Steve Holtzclaw

  Description: Update comments and cleanup code.
  Date:        9/7/85
  Authors:     Mick Gaitor, Steve Fox

  The following hardware configuration is assumed:

     DCD (pin 8) is supported
     DTR (pin 20) is supported
     RI  (pin 22) is not supported

The following modem default switches are assumed:

     1 = up    DTR supported, do not force to always logic true.
     2 = down  Send result codes as digits when in command state.
     3 = down  Result codes are sent to the terminal.
     4 = up    Echo characters when in command state.
     5 = down  Do not answer the telephone.
     6 = up    DCD supported, do not force to always logic true.
     7 = up    Single line RJ11 telephone connection to modem.
     8 = down  Enables modem command recognition when in command state.
}

const

{ Modem result codes }

  OKAY        = '0';                        { Command executed with no errors }
  CONNECT300  = '1';                        { Carrier detect at 300 bps }
  RING        = '2';                        { Ring signal detected }
  NOCARRIER   = '3';                        { Carrier lost or never heard }
  ERROR       = '4';                        { Error in command execution }
  CONNECT1200 = '5';                        { Carrier detect at 1200 bps }
  CONNECT2400 = '10';                       { Carrier detect at 2400 bps }

function mdresult: Str3;
{ Get result code from modem }
var
  count        : integer;
  ch           : char;
  result       : Str3;
begin
  result := '';
  repeat
    repeat until ch_inprdy;
    ch := chr(ch_inp);
    if ch in ['0'..'9','O','K'] then result := result + ch;
    if length(result) > 2       then delete(result, 1, 1)
  until (ch = CR) and (result<>'');
  if result = 'OK' then mdresult := OKAY
                   else mdresult := result;
  end;

procedure mdsend(st: StrStd);
{ Send a command string to the modem and continue sending until the modem
  echoes exactly what was sent. }
var
  bt           : byte;
  i, j         : integer;
begin
  while ch_inprdy do bt := ch_inp;
  repeat
    i := 1;
    repeat
      bt := ord(st[i]);
      ch_out(bt);                         { Send the character }
      j := 500;
      repeat                              { Loop until ready or timeout }
        j := pred(j)
      until ch_inprdy or (j <= 0);
      if j>0 then OK := bt = ch_inp
             else OK := FALSE;
      i := succ(i)
    until (not OK) or (i > length(st));
  until OK
end;

procedure mdhangup;
{ Hangup modem }
var
  i            : integer;
begin
  repeat
  { Break before disconnect not implemented }
    ch_off;                               { Hangup NOW! }
    delay(1000);
    ch_on;
    if ch_carck then
    begin
      for i := 1 to 3 do ch_out(ord(ETX));
      delay(1500);
      repeat
        mdsend('AT H0' + CR)
      until mdresult = OKAY
    end
  until not ch_carck
end;

procedure mdbusy;
{ Take modem off hook to present a busy signal to incoming callers }
begin
  repeat
    mdsend('AT H1' + CR)                  { Take modem off hook }
  until mdresult = OKAY
end;

function mdring: boolean;
{ Determine if the phone is ringing }
begin
  if ch_inprdy then mdring := (RING = mdresult)
               else mdring := FALSE
end;

procedure mdans;
{ Detect and set system to rate at which modem answered phone }
var
  bt           : byte;
  result       : Str3;
begin
  mdsend('AT A' + CR);                    { Let the modem answer }
  result := mdresult;
  if                result = CONNECT300  then ch_set(300)
       else if      result = CONNECT1200 then ch_set(1200)
            else if result = CONNECT2400 then ch_set(2400)
                 else mdhangup;
  delay(500);                             { Make sure carrier is stable }
  while ch_inprdy do bt := ch_inp;        { Clear any junk }
end;

procedure mdinit;
{ Ensure the modem is hung up, initialized, and ready to wait for a ring. }
var
  bt           : byte;
begin
  ch_init;                                { Initialize the remote channel }
  ch_on;
  ch_set(1200);                           { Set the channel speed }
  repeat
    delay(500);
    mdsend('AT Z' + CR);                  { Tell the modem to reset }
  until mdresult = OKAY;
  remote_copy := FALSE;                   { added by JEB }
  repeat
    delay(500);
    mdsend('AT B0 V0 E1 M1 S0=0 S1=0 S2=3' + CR);
  until mdresult = OKAY;
  while ch_inprdy do bt := ch_inp;        { Clear any inputs }
end;


