
/* This copy is for recording the outgoing message and the password string. */


#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"    /* Definitions/routines Al uses lots. */
#include "modem.h"     /* Routines for accessing the modem.  */
#include "voicemdm.h"  /* Voicemodem routines.               */

int port_number;       /* ...from the command line. */


/*------------------------------- Main Program -------------------------------*/

int main(int argc, char *argv[])
{
FILE *voice_file;
char input_char,input_char_2,voice_file_name[64];

if (argc <= 1)                 /* Our 1 required argument not specified? */
  fatal_error("Missing argument; supply the comm. port number.");
port_number = atoi(argv[1]);

if (fossil_init(port_number))
  fatal_error("Unable to initialize comm. port / fossil driver");
fossil_set_handshaking(port_number,2);
fossil_set_dtr(port_number,1);
voicemdm_reset_modem(port_number);
voicemdm_set_modem_parameters(port_number);

printf("Enter the name of the voice file to write out: ");
gets(voice_file_name);
voice_file = fopen(voice_file_name,"wb");
if (voice_file == NULL)
  fatal_error("Couldn't open file in record_voice_file().");
printf("Voice file opened.  Pick up phone and prepare to record message.\n");
pause();
send_to_modem(port_number,"AT #VSS=1 #VLS=1");
send_to_modem(port_number,CR_string);
if (waitfor(port_number,"VCON",5,1))
  fatal_error("Didn't get VCON after switching to handset mode.");
wait(1);
send_to_modem(port_number,"AT #VRX");
send_to_modem(port_number,CR_string);
if (waitfor(port_number,"CONNECT",5,1))
  fatal_error("Didn't get CONNECT after starting to receive.");
printf("Start talking!\n");
while (1)          /* Copy characters from port to voice file. */
  {
  if (fossil_available(port_number))
    {
    input_char = fossil_receive(port_number);
    if (input_char != 16)   /* Not the "Escape" character? */
      {
      fwrite(&input_char,1,1,voice_file);
      continue;
      }
    printf("DLE received; waiting for second character.\n");
    while (!fossil_available(port_number))  { };   /* Wait for 2nd character. */
    input_char_2 = fossil_receive(port_number);
    printf("The 2nd character's value was %d, picture is %c.\n",input_char_2,input_char_2);
    switch (input_char_2)
      {
      case  3 : printf("DLE/ETX received!  Quitting nicely.\n");
                send_to_modem(port_number,"!");
                break;                  /* This is the most proper end. */
      case 'b':
      case 'd': printf("Busy or dialtone (%c) received; quitting nicely.\n",input_char_2);
                send_to_modem(port_number,"!");
                break;
      case 'o': printf("DLE/o received; overrun detected!\n");
                continue;
      case 'q': printf("Quiet received; quitting nicely.\n");
                send_to_modem(port_number,"!");
                break;
      case 's': printf("Silence received; breaking out nicely.\n");
                send_to_modem(port_number,"!");
                break;
      case '*': printf("Asterisk received; exiting nicely.\n");
                send_to_modem(port_number,"!");
                break;
      case '#': printf("Pound sign received; exiting nicely.\n");
                send_to_modem(port_number,"!"); /* Send key abort to modem. */
                break;                   /* Pretty much done immediately. */
      case  16: fwrite(&input_char,1,1,voice_file);
                continue;
      default : printf("2nd character (ignored) looked like %c with value of %d\n",input_char_2,input_char_2);
                continue;
      }
    break;  /* If anything in the switch statement broke out, then we end up here. */
    }
  }

fclose(voice_file);
send_to_modem(port_number,"ATZ");
send_to_modem(port_number,CR_string);
if (waitfor(port_number,"OK",5,1))
  fatal_error("Didn't get OK after ATZ command to reset modem!");
printf("Closing modem port and dropping DTR.\n");
fossil_close(port_number,0);   /* Close port, dropping DTR. */
printf("Successful exit!\n");
return(0);
}


