 
/*  err.c
    for use by tm40.c
    Stephen D. Anderson --- October 29, 1994

    Function to indicate a communications error.
    Function err.c first checks for communication error by examining
    the 1st status byte (global variable "status1").  If it finds
    communication error, it says so, prints the status1 byte and then 
    quits.  Else it ANDs status1 with 0x7f to get the lower 7 bits.  

    This value is used as an index into the array "errwarn[]" (in
    file "errors.h") to determine the respone.  The content of the
    array location is used in a "switch" statement to determine the
    response.

    If the array location contains a 6, this indicates a command-
    specific warning.  The command number is then used to determine
    the response.

    If the array location contains a 7, this indicates a command-
    specific error.  The command number is then used to determine
    the respone.

    As an example, suppose that the command is 37 and the value of
    "status1" is 8.  The array "errwarn" has a number 6 at location
    8, which means that the status1 value is a command specific warning.
    The execution goes to case 6 and then to sub-case 37.  The only
    valid status for command 37 is 14.  Since the status1 value was 8 
    rather than 14, the indicated status for this command is not valid and 
    function "not_valid(cmd)" is called.

    Once the response is given, waits for user to enter a keystroke
    to resume.

    ON ENTRY:
        cmd is the HART command that failed or that is suspect.

    ON EXIT:
        returns nothing.

    REVISION LOG:


*/

#include "tm40.h"

void err(byte cmd)
{
    extern byte status1;
    extern void not_valid();

    byte s;

    #include "errors.h"     /*  error array. */

    Wopen(2,15,15,65,2,WHITE|_RED,WHITE|_RED);

    s = 0x80 & status1;     /*  comm error?  */

    if (s == 0x80)
    {
        /*  yes, comm error. */
        Wputs("\n\n    Field Instrument reporting");
        Wputs("\n   Communication Error on HART CMD");
        Wputf("\n\n       %x (hex)",cmd);
        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
        Wputs("\n\n    Hit any key to resume.");
        KeyWait(); Wclose(); return;
    }

    s = 0x7f & status1;     /*  other error or warning?  */

    switch(errwarn[s])
    {
        case 0: /*  unknown error. */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    Undefined Error on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 1: /*  Invalid Selection. */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    'Invalid Selection' Error on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 2: /*  Parameter too large.  */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    'Parameter Too Large' Error on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 3: /*  Parameter too small.  */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    'Parameter Too Small' Error on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 4: /*  Too few bytes.  */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    'Too Few Bytes Received' Error on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 5: /*  write protected. */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    it is Write Protected on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 6: /*  command specific warning.  */
        {
            /*  the warning depends on the command.
            issue the warning message based on the command.
            The order of the commands is the result of
            doing a fgrep on 'hartcmd' for all files. */

            switch(cmd)
            {

                case 37:
                {
                    if(s == 14)
                    {
                        Wputs("\n\n   Field Instrument warning");
                        Wputs("\n    of New Lower Range Value");
                        Wputs("\n    Pushed Upper Range Value");
                        Wputs("\n    Over Sensor Limit");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else
                    {
                        not_valid(cmd); return;
                    }
                }

                case 36:
                case 35:
                {
                    if(s == 14)
                    {
                        Wputs("\n\n   Field Instrument warning");
                        Wputs("\n    of Span Too Small");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else
                    {
                        not_valid(cmd); return;
                    }
                }

                case 3:
                {
                    if (s == 8)
                    {
                        Wputs("\n\n   Field Instrument warning");
                        Wputs("\n    of Update Failure");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else
                    {
                        not_valid(cmd); return;
                    }
                }

                case 34:
                {
                    if (s == 8)
                    {
                        Wputs("\n\n   Field Instrument warning of");
                        Wputs("\n    Set to Nearest Possible Value");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else
                    {
                        not_valid(cmd); return;
                    }
                }

                default:
                {
                    not_valid(cmd); return;
                }
            }
        }

        case 7: /*  command specific error.  */
        {
            /*  the error depends on the command.
            issue the error message based on the command.
            The order of the commands is the result of
            doing a fgrep on 'hartcmd' for all files. */

            switch(cmd)
            {

                case 37:
                case 36:
                {
                    if (s == 9)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 9 -- ");
                        Wputs("\n    Applied Process Too High");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else if (s == 10)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 10 -- ");
                        Wputs("\n    Applied Process Too Low");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else
                    {
                        not_valid(cmd); return;
                    }
                }

                case 40:
                {
                    if (s == 11)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 11 -- ");
                        Wputs("\n    It is in Multidrop Mode");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else
                    {
                        not_valid(cmd); return;
                    }
                }

                case 45:
                case 46:
                {
                    if (s == 9)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 9 -- ");
                        Wputs("\n    Not in Proper Current Mode");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else if (s == 11)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 11 -- ");
                        Wputs("\n    In Multidrop Mode");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else
                    {
                        not_valid(cmd); return;
                    }
                }

                case 35:
                {
                    if (s == 9)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 9 -- ");
                        Wputs("\n    Lower Range Value Too High");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else if (s == 10)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 10 -- ");
                        Wputs("\n    Lower Range Value Too Low");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else if (s == 11)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 11 -- ");
                        Wputs("\n    Upper Range Value Too High");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else if (s == 12)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 12 -- ");
                        Wputs("\n    Upper Range Value Too Low");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else if (s == 13)
                    {
                        Wputs("\n\n   Field Instrument reports");
                        Wputs("\n    Error 13 -- ");
                        Wputs("\n    Upper and Lower Range Values");
                        Wputs("\n    Out of Limits");
                        Wputf("\n\n    Status1 Byte = %x (hex)",status1);
                        Wputs("\n\n    Hit any key to resume.");
                        KeyWait(); Wclose(); return;
                    }
                    else
                    {
                        not_valid(cmd); return;
                    }
                }

                default:
                {
                    not_valid(cmd); return;
                }
            }
        }

        case 8: /*  access restricted.  */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    'Access Restricted' Error on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 9: /*  unknown warning.  */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n   Undefined Warning on HART CMD");
            Wputf("\n     %x (hex)", cmd);
            Wputf("\n\n   Status 1 Byte = %x (hex)", status1);
            Wputs("\n\n   Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 10:    /*  busy    */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    it is Busy on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 11:    /*  not implemented.  */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    'Command Not Implemented' on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 12:    /*  invalid range units code error.  */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    'Invalid Range Units Code' Error on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 13:    /*  unexp byte count.  */
        {
            Wputs("\n\n   Received Byte Count");
            Wputs("\n    is incorrect for HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }

        case 14:    /*  xmitter specific cmd error.  */
        {
            Wputs("\n\n   Field Instrument reporting");
            Wputs("\n    'Xmitter Specific Command Error' on HART CMD");
            Wputf("\n       %x (hex)",cmd);
            Wputf("\n\n    Status1 Byte = %x (hex)",status1);
            Wputs("\n\n    Hit any key to resume.");
            KeyWait(); Wclose(); return;
        }
    }
}

void not_valid(byte cmd)

/*  function gives message that field instrument reporting a
warning or error that is not valid for the command.  */

{
    extern byte status1;

    Wputs("\n\n    Field Instrument reporting");
    Wputs("\n    a warning or error that is not");
    Wputs("\n    valid for HART CMD");
    Wputf("\n    %x (hex)",cmd);
    Wputf("\n\n    Status1 Byte \ %x (hex)",status1);
    Wputs("\n\n    Hit any key to resume.");
    KeyWait(); Wclose(); return;
}

