
/* This program waits 6 seconds for a RING from the modem and exits with */
/*  an errorlevel of 1 if gets one within that time.  Also handles KB:.  */
/* The 1st 2 args (# and #####) are the comm port number (1-4) and the   */
/*  baud rate (usually your locked baud rate, or a rate passed here).    */
/*  If arg 3 is blank, then the program waits 7 seconds for a ring.      */
/*  If arg 3 is -r, then the program simply waits for rings from the     */
/*  modem, printing out the date/time for each one.  If arg 3 is anything*/
/*  else, args 3 and 4 are taken to mean the minimum seconds to wait for */
/*  a ring and the maximum random number of rings to wait for.           */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <conio.h>
#include <time.h>
#include <dos.h>  /* Dos-specific functions are used to get date/time. */

#include "common.h"
#include "modem.h"     /* Routines for accessing the modem. */


/*---------------------------- The program! ----------------------------*/
int main(int argc, char *argv[])
{
char arg_port[64],arg_baud[64],init_string[8]={'A','T',13,0};
int modem_port,modem_speed;
time_t starting_time,ending_time,current_time;
int the_char,seconds_to_wait;
int minimum_seconds=6,maximum_random_seconds=10;  /* Wait time defaults. */
char arg_minimum_seconds[8],arg_maximum_random_seconds[8];

printf("RINGCHEK -- Alton Moore -- Fidonet 1:397/5264, WWIVnet 1@1042\n");
if (argc < 3)   /* Were the port and/or baud arguments left out? */
  {
  printf("?Illegal number of arguments.%c\n",BELL_string[0]);
  printf("\nSpecify in the command line:\n");
  printf("  Comm port, baud rate, minimum secs to wait, and random secs. to add, eg:\n\n");
  printf("  RINGCHEK [port] [baud] [minsecs] [maxrandsecs]\n");
  printf("  RINGCHEK 2 19200 6 10\n\n");
  printf("The last two arguments default to 6 and 10 if omitted.\n");
  printf("If the 3rd argument is -r then the date/time of rings is logged.\n\n");
  printf("Error levels returned:  0 -- No RING; no keyboard abort (SUCCESS)\n");
  printf("                        1 -- RING was detected from modem\n");
  printf("                        2 -- Key pressed to abort callout\n");
  printf("                        3 -- Early RING detected         \n");
  printf("                        9 -- Parameter or program's error\n");
  printf("\nExiting with errorlevel 9....\n");
  return(9);
  }
strcpy(arg_port,argv[1]);      /* The 2 required arguments. */
strcpy(arg_baud,argv[2]);
modem_port  = atoi(arg_port);
modem_speed = atoi(arg_baud);

if (common_set_log_filename("ringchek.log"))
  fatal_error("Couldn't use ringchek.log as a log file name!");

/* This is a medium-sized if block. */
if (argc < 4)   /* Was there a 3rd argument (argc = 4) at all? */
  {
  printf("Using default wait values.\n");
  }
else
if (!strcmp("-r",argv[3]))   /* Was 3rd argument -r? */
  {
  if (initSerial(modem_port))
    {
    printf("?Couldn't initialize serial port; exiting with errorlevel 9!\n");
    return(9);
    }
  fossil_set_dtr(modem_port,1);
  port_set_handshaking(modem_port,1);  /* Hardware handshaking. */
  send_to_modem(modem_port,init_string);
  printf("Logging RINGs received from modem to screen; hit a key to end.\n");
  time(&current_time);
  printf("\n\nStarting to log RINGs from modem at %s\n",ctime(&current_time));
  trace_start("RING");
  while (1)
    {
    ending_time = time(NULL) + 60;
    while (time(NULL) < ending_time)
      {
      if (kbhit())  /* Was a key hit? */
        {
        getch();
        printf("Key pressed; quitting....\n");
        deinitSerial(modem_port,0);
        trace_stop_all();
        return(0);
        }
      the_char = port_in(modem_port);             /* Anything from the modem? */
      if (the_char != -1)
        trace_process_received_character(the_char);
      if (trace_fired("RING"))
        {
        time(&current_time);
        sprintf(temp_string,"RING detected at %s",ctime(&current_time));
        printf("%s",temp_string);
        common_write_log_record(temp_string);
        trash_modem_output(modem_port,0);
        trace_stop_all();
        trace_start("RING");
        }
      }
    }
  }
else
if (argc >= 5)   /* Were the seconds arguments included (both!)? */
  {
  printf("Waiting with specified minimum/random values....\n");
  strcpy(arg_minimum_seconds,argv[3]);
  strcpy(arg_maximum_random_seconds,argv[4]);
  minimum_seconds = atoi(arg_minimum_seconds);
  maximum_random_seconds = atoi(arg_maximum_random_seconds);
  }

/* Now open the port and get going.... */
if (initSerial(modem_port))
  {
  printf("?Couldn't initialize serial port; exiting with errorlevel 9!\n");
  return(9);
  }
fossil_set_dtr(modem_port,1);
port_set_handshaking(modem_port,1);  /* Hardware handshaking. */
send_to_modem(modem_port,init_string);
randomize();
seconds_to_wait = minimum_seconds + (rand() % (maximum_random_seconds+1));
printf("Waiting %d seconds for RING from modem.  Hit space bar to\n",seconds_to_wait);
printf("hurry up or press any other key to abort next operation.\n");

trace_start("RING");
starting_time = time(NULL);
ending_time = starting_time + seconds_to_wait;
while (time(NULL) < ending_time)
  {
  if (kbhit())
    {
    deinitSerial(modem_port,0);
    trace_stop_all();
    if (getch() == ' ')
      {
      printf("Quitting and exiting with errorlevel 0!\n");
      return(0);
      }
    else
      {
      printf("Key pressed; aborting and exiting with errorlevel 2!\n");
      return(2);
      }
    }
  the_char = port_in(modem_port);             /* Anything from the modem? */
  if (the_char != -1)
    trace_process_received_character(the_char);
  if (trace_fired("RING"))
    {
/*     THIS CODE IS SUPPOSED TO DETECT SOME INSTANCES OF WHEN ONE CALL STOPS
       RINGING AND THE NEXT STARTS BY NOTING THE < 6 SECONDS BETWEEN RINGS.
    time(&current_time);
    if (current_time < (ending_time - 1))
      {
      printf("RING detected early from modem; exiting with errorlevel 3!\n");
      trace_stop_all();
      trash_modem_output(modem_port,0);
      deinitSerial(modem_port,0);
      return(3);
      }
    else
*/
      {
      printf("RING detected from modem; exiting with errorlevel 1!\n");
      trace_stop_all();
      trash_modem_output(modem_port,0);
      deinitSerial(modem_port,0);
      return(1);
      }
    }
  }
printf("Nothing detected; exiting with errorlevel 0.\n");
trace_stop_all();
deinitSerial(modem_port,0);
return(0);
}



