/*
 ******************************************************************************
 *  Copyright 1997 DeltaLink bv. All Rights Reserved.
 ******************************************************************************
 *
 * DeltaLink Serial Interface Software
 *
 * File: modem.c
 *
 * This file contains the serial communication routines for modem support
 *
 */

#ifdef UNIX
#include <pthread.h>
#include <fl_sync.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <memory.h>
#include <malloc.h>

#ifdef NT
#include <windows.h>
#endif

#include <serial.h>

/*-----------------------------------------------------------------------------
 * FUNCTION:   Serial_Mdm_Command
 *
 * PURPOSE:    Execute the modem with the string as specified in the cfg file.
 *
 -----------------------------------------------------------------------------*/
int Serial_Mdm_Command ( COMPORT *port, ES *es, int type )
{
  char    answer[ 80 ];
  int     error;
  unsigned short   answer_len;

  /* check if the command exists */

  if( !strlen( es->mdm_command[ type ] ) )
  {
    return SER_GOOD;
  }

  /* flush the send and receive buffers first */

  Serial_TX_Clear( port);
  Serial_RX_Clear( port);

  /* send the init string to the modem */

  if( type == MDM_CONNECT )
  {
    if ( ( error = Serial_TX( port, ( short)strlen( es->mdm_command[ type ] ), es->mdm_command[ type ])) != SER_GOOD )
      return error;

    if ( ( error = Serial_TX( port, ( short)strlen( es->address[ 0 ] ), es->address[ 0 ])) != SER_GOOD )
      return error;

    if ( ( error = Serial_TX( port, 1, "\r")) != SER_GOOD )
      return error;
  }
  else
  {
    if ( ( error = Serial_TX( port, ( short)strlen( es->mdm_command[ type ] ), es->mdm_command[ type ])) != SER_GOOD )
      return error;
  }

  /* receive the answer of the modem */

  answer_len = strlen( es->mdm_answer[ type ] );

  if ( ( error = Serial_RX( port, &answer_len )) == SER_GOOD )
  {
    /* did we receive a connect indication string */

    Serial_Get_Packet( port,
                       answer_len, 
                       NULL, 
                       answer);

    if( strncmp( &answer[ 0 ], es->mdm_answer[ type ], strlen( es->mdm_answer[ type ] )) )
      error = SER_E_MDMCOMMAND;
  }

  return error;
}

/*-----------------------------------------------------------------------------
 * FUNCTION:   Serial_Mdm_Listen
 *
 * PURPOSE:    Listen on the port for an incoming connection.
 *
 -----------------------------------------------------------------------------*/
int Serial_Mdm_Listen ( COMPORT *port, ES *es )
{
  char          listen_str[ 80];
  int           error;
  unsigned short         listen_len;

  /* reset the connection indication */

  es->mdm_connected = 0;

  /*
   * first lock the port for setting out a listen
   */
  if( ( error = Serial_Lock( port, SER_ABORT_ALLOWED, SER_WAIT_READ)) != SER_GOOD )
    return error;

  /*
   * set timeout values 
   */
  if( Serial_Set_Timeouts( port, 0 ) != SER_GOOD )
  {
    error = SER_E_STIME;
  }

  /*
   * set out a receive for incoming connect requests
   */
  listen_len = strlen( es->mdm_listen );

  if ( Serial_RX( port, &listen_len ) == SER_GOOD )
  {
    /* did we receive a connect indication string */

    Serial_Get_Packet( port, 
                       listen_len, 
                       NULL, 
                       listen_str);

    if( !strncmp( listen_str, es->mdm_listen, listen_len) )
      es->mdm_connected = 1;
  }

  /*
   * restore time-outs
   */
  if( Serial_Set_Timeouts( port, -1 ) != SER_GOOD )
  {
    error = SER_E_STIME;
  }

  Serial_UnLock( port );

  return error;
}
