; Dynalink
modem_initialise ()
  {
  integer oldtx=port_txspeed(),oldrx=port_rxspeed()

  port_dtr(1)
  port_rxclear()
  return(1)
  }

modem_shutdown ()
  {
  port_dtr(1)
  port_rts(1)
  return(1)
  }

modem_connect ()
  {
  port_dtr(1)
  port_rts(1)
  type "ATO"+cr

  return(_processmessage(""))
  }

modem_disconnect ()
  {
  string response[30]

  port_rts(0)
  port_dtr(0)
  pause 50
  port_dtr(1)
  port_rts(1)
  pause 50
  type cr+"ATS86?"+cr
  repeat
    {
    response=$modeminput(30,modem_replywait)
    }
  until(strcmp(response,"")!=0)
  switch(response)
    {
    case$("000")
      {
      prints "Normal disconnect, no error occurred."+newline
      }
    case$("004")
      {
      prints "Carrier lost (other modem disconnected)"+newline
      }
    case$("005")
      {
      prints "V42 negotiation failed to detect an error-correcting modem."+newline
      }
    case$("009")
      {
      prints "Modems could not find a common protocol."+newline
      }
    case$("012")
      {
      prints "Normal disconnect initiated by the remote modem."+newline
      }
    case$("013")
      {
      prints "Remote modem did not respond after 10 retransmissions."+newline
      }
    case$("014")
      {
      prints "Protocol violation."+newline
      }
    }
  }

modem_dial (string number[40],integer how)
  {
  string dialtype[1]

  port_dtr(1)
  port_rts(1)
  pause(30)
  port_rxclear()

  if (how==0)
    {
    dialtype="P"
    }
  else
    {
    dialtype="T"
    }

  type "ATD"+dialtype+number+cr

  return(_processmessage(""))

 }

modem_errorcontrol (string option[10])
  {
  set(linklevel,none)

  if (comparei(option,"off"))
    {
    type("AT&Q6N1"+cr)
    }                           

  if (comparei($left(option,3),"mnp"))
    {
    integer level=val($mid(option,4,1))
 
    if (level>=5)
      {
      type("AT&Q5N1"+cr)
      }
    else
      {
      type("AT&Q6N1"+cr)
      }
    }

  if (comparei(option,"vasscom"))
    {
    type("AT&Q5N0S37=9"+cr)
    set(linklevel,vasscom)
    }

  waitfor("OK"+$chr(13)+$chr(10),100)
  pause(50)

  }

modem_answer ()
  {  
  string response[30]

  type "ATS0=1"+cr

  repeat
    {
    response=$modeminput(30,modem_replywait)
    }
  until(strcmp($left(response,7),"CONNECT")==0)

  return(_processmessage(response))
  }

modem_standard (string option[10])
  {
  if (comparei(option,"v21"))
    {
    port_txspeed(300)
    port_rxspeed(300)
    return
    }
  if (comparei(option,"v22"))
    {
    port_txspeed(1200)
    port_rxspeed(1200)
    return
    }
  if (comparei(option,"v22bis"))
    {
    port_txspeed(2400)
    port_rxspeed(2400)
    return
    }
  if (comparei(option,"v27"))
    {
    port_txspeed(4800)
    port_rxspeed(4800)
    return
    }
  if (comparei(option,"v32"))
    {
    port_txspeed(9600)
    port_rxspeed(9600)
    return
    }
  if (comparei(option,"v32bis"))
    {
    port_txspeed(19200)
    port_rxspeed(19200)
    return
    }
  prints "Modem standard "+option+" is not supported."+newline
  }

_processmessage (string already[30])
  {
  string retcode[30]
                                   
  if (len(already)==0)
    {
    ; Wait for message
    retcode=$modeminput(30,modem_replywait)
    if (len(retcode)==0)
      {
      retcode=$modeminput(30,modem_replywait)
      }
    }
  else
    {
    retcode=already
    }

  ; Check for NO DIAL TONE/BLACKLISTED/BUSY
  switch(retcode)
    {
    case$("BUSY")
      {
      return(2)
      }
    case$("NO DIAL TONE")
      {
      return(3)
      }
    case$("BLACKLISTED")
      {
      return(4)
      }
    }
               
  if (compare($left(retcode,7),"CONNECT"))
    {
    prints retcode+newline
    return(0)
    }

  prints retcode+newline
  modem_disconnect()
  return(1)
  }
