
#include "serial.h"          /* These are the actual fossil routines. */
#include "trace.h"

/*----------------------------------------------------------------------------*/

void send_to_modem(int modem_port_no,char *the_string)  /* Sends string to modem (but not null at end). */
{
int temp;
for (temp=0;temp<strlen(the_string);temp++)
  {
/*  printf("MODEM.H->send_to_modem(): Waiting to send character %c to the fossil \n(and therefore modem).\n",the_string[temp]); */
  while (!fossil_ok_to_send(modem_port_no))
    {
    printf("Waiting to send character %c to the fossil.\n",the_string[temp]);
    wait(1);
    }
  fossil_send(modem_port_no,the_string[temp]);
  }
}

void trash_modem_output(int modem_port_no,char debug_flag)
{
if (debug_flag)
  printf("Trashing modem output.\n");
while (fossil_available(modem_port_no))
  fossil_receive(modem_port_no);       /* ...totally ignoring the data. */
}

int waitfor_any_trace(int modem_port_no,int timeout_seconds,char debug_flag)
/* Returns 0 if a trace fired; non-zero if a timeout occurred. */
{
clock_t starting_clock,ending_clock;
int char_from_port;
char_from_port = modem_port_no; /* This line exists to get rid of warning error.*/
starting_clock = clock();
ending_clock = starting_clock + (CLK_TCK * timeout_seconds);
while (1)   /* Go through this repeatedly. */
  {
  if (clock() > ending_clock)  /* Timeout? */
    break;                     /*   Break out of loop and return error. */
  if (!fossil_available(modem_port_no))  /* No character(s) waiting? */
    continue;
  char_from_port = fossil_receive(modem_port_no);
  if (debug_flag)
    printf("%c",(char)char_from_port);
  trace_process_received_character((char)char_from_port);
  trace_list_current_ptr = trace_list_head_ptr;
  while (trace_list_current_ptr != NULL)
    {
    if (trace_list_current_ptr->fired_flag)
      return(0);
    trace_list_current_ptr = trace_list_current_ptr->next;
    }
  }
return(1);
}

int waitfor(int modem_port_no,char *the_string,int timeout_seconds,char debug_flag)
/* Returns 0 if string found or non-zero if a timeout occurred. */
{
int result;
result = modem_port_no;  /* This line exists to get rid of warning error. */
trace_start(the_string);
result = waitfor_any_trace(modem_port_no,timeout_seconds,debug_flag);
trace_stop(the_string,debug_flag);
return(result);
}

int get_char_from_modem_with_timeout(int modem_port_no,int timeout_seconds) /* Returns char or -1 for timeout. */
{
clock_t ending_clock;
if (fossil_available(modem_port_no))
  return(fossil_receive(modem_port_no));
ending_clock = clock() + (CLK_TCK * timeout_seconds);  /* Well, we'll have to wait. */
while (clock() <= ending_clock)   /* Go through this repeatedly looking for characters. */
  {
  if (fossil_available(modem_port_no))
    return(fossil_receive(modem_port_no));
  }
return(-1);
}

void modem_terminal(int port)
{
char input_char;
printf("Entering terminal mode; hit escape to exit from it....\n");
while (1)   /* Character loop. */
  {
  if (kbhit())
    {
    input_char = getch();
    if (input_char == 27)
      break;
    fossil_send(port,input_char);
    }
  if (fossil_available(port))
    {
    input_char = fossil_receive(port);
    printf("%c",input_char);
    }
  }
printf("\nEscape hit; breaking out of terminal function.\n");
}


