 
/*  userdef.c
    For use with tm40.c
    Stephen D. Anderson --- April 28, 1995

    Function to send user command to field instrument.  Does the following
    in order:

    Checks for existence of field instrument.  Searches if doesn't exist.

    Opens window and asks user for a command number.  Repeats this until
    it gets valid command number (number from 0 through 253).

    Checks whether file command.CMD exists where command is the number
    just entered.  If not, error is indicated and returns to calling
    program.  If so, reads the file and gets the number of command bytes.
    
    If number of command bytes is 60 or less, proceeds.  Otherwise,
    indicates error and returns to calling program.

    Reads the command bytes into an array and closes the file.

    Sends the user command and indicates to user that it has been sent.

    If any errors, indicates these and returns to calling program.
    Else tries to create file called command.RSP.  If errors in creating 
    file, indicates errors and returns to calling program.  If not,
    writes response to file and indicates this to user.


    ON ENTRY:   Has no arguments.

    ON EXIT:    Returns nothing.

    REVISION LOG:


*/

#include "tm40.h"

void userdef()

{
    extern byte status1,status2;
    extern byte addr[];
    extern byte found;
    extern byte search(void);
    extern int hartcmd(byte *, byte, byte *, byte, byte *, byte);

    FILE *f;
    byte cmd;
    int n_bytes;
    int i,j;
    byte s[20];
    byte ss[20];
    byte x[60];
    byte r[60]; /*  receive data. */
    byte *px, *pr;

    /*  need field instr info?  */
    if (!found) found = search();
    if (!found) return;

    Wopen(5,10,15,70,2,WHITE|_BLUE,WHITE|_BLUE);

    while (1)   /*  get valid command number.  */
    {
        Wclear();
        for (j=0; j<4; ++j) s[j] = 0;
        KwGetFmt(s, "'\n\n  Enter command number: '!R!#!R!##");
        /*  check for ESC key.  */
        if (TcxlErr == W_ESCPRESS)
        {
            Wclose(); return;
        }
        i = atoi(s);
        if (i > 253)
        {
            Wclear();
            Wputs("\n\n  Error -- Number must be less than 254");
            Wputs("\n\n  Hit any key to continue.");
            KeyWait();
        }
        else break;
    }

    cmd = (byte)i;  /*  set command byte. */
    strcpy(ss, s);
    strcat(ss, ".CMD");  /*  build the file name.

    /*  check whether file exists.  */
    if ((f = (fopen(ss, "r"))) == 0)
    {
        /*  doesn't exist.  */
        Wclear();
        Wputs("\n\n    Error -- Specified Command File\n");
        Wputs("     doesn't exist.");
        Wputs("\n\n    Hit any key to continue.");
        KeyWait(); Wclose(); return;
    }

    fscanf(f, "%x\n", &n_bytes);    /*  get number of bytes.  */

    if (n_bytes > 60)   /*  Too many?  */
    {
        Wclear();
        Wputs("\n\n    Error -- Too many command bytes");
        Wputs("\n\n    Hit any key to continue.");
        KeyWait(); fclose(f); Wclose(); return;
    }

    for (i = 0, px = x; i<n_bytes; ++i) /*  get command bytes.  */
        fscanf(f, "%x\n", px++);

    fclose(f);

    /*  send the command.  */
    Wclear();
    Wputf("\n\n    Sending User Command %d ... ", cmd);
    i = hartcmd(x, n_bytes, r, 60, addr, cmd);
    delay(1000);

    /*  did it work?  */
    if ((i & 1) != 0)
    {
        /*  no, didn't work. */
        Wclear();
        Wputf("\n\n   Error -- User Command %d\n", cmd);
        Wputs("    Failed.\n");
        Wputs("\n\n    Hit any key to continue.");
        KeyWait(); Wclose(); return;
    }

    /*  save it to disk.  */
    strcpy(ss, s);
    strcat(ss, ".RSP");

    /*  open response file.  */
    if ((f = (fopen(ss, "w"))) == 0)
    {
        /*  file creation didn't work.  */
        Wclear();
        Wputs("\n\n     Error -- Couldn't open response file\n");
        Wputs("\n\n     Hit any key to continue.");
        KeyWait(); Wclose(); return;
    }

    /*  write response file.  */
    n_bytes = i/256;
    fprintf(f, "%x (byte count)\n", n_bytes);
    for (i = 0, pr = r; i<n_bytes; ++i) fprintf(f, "%x\n", *pr++);
    fclose(f);

    Wclear();
    Wputs("\n\n    Response file written.");
    Wputs("\n\n    Hit any key to continue.");
    KeyWait(); Wclose();
}

