
 
/*  getinfo.c
    used with tm40.c.
    Stephen D. Anderson --- October 29, 1994

    Function gets information about the field instrument.  Does the
    following:

    Checks whether field instrument exists, searches if not.

    Tells user that it is getting field instrument info.

    Clears variable "status2_OK".

    Saves any existing mfr, mfr type, software rev, hardware rev, and
    flags.

    Gets tag, descriptor, and date by sending HART command 13.  If there
    is an error it sets each of these to "Not Available".  

    Else it unpacks the tag and descriptor and stores them.  It checks the 
    date.  It increments "status2_OK" and makes a copy of "status2".

    Any errors in getting tag, descriptor, date are reported, "status1"
    is saved.

    Gets the field instrument message by sending HART command 12.  If
    error it sets message to "Not Available".  Else it unpacks message,
    increments "status2_OK" and saves "status2".  It reports any errors
    associated with getting the message and saves "status1".

    Gets finaly assembly number by sending HART command 16.  If error it
    sets message to "Not Available".  Else it converts the number to a
    string for future use.  It increments "status2_OK" and saves "status2".
    It reports any errors associated with getting the final assembly
    number and saves "status1".

    If one of the preceding HART commands was successful, as indicated by
    a non-zero "status2_OK", the "status2" from the most recent successful
    command is gotten and used in a display of all the possible status
    conditions of the field instrument.

    If any of the preceding HART commands produced a "status1" that was
    non-zero, a message is presented that says there was a possible error
    in getting the field instrument information.

    A table of the values found is presented.

    Waits for user to hit any key, then returns to calling program.


    ON ENTRY:   Requires no arguments.

    ON EXIT:    Returns nothing.

    REVISION LOG:
    1.  July 12, 1995:  Re-arranged so that the status2 info that is
        displayed is based on the most recent HART command.  If none
        of the commands work, status2 isn't displayed.


*/

#include "tm40.h"
#include "pup.h"
#define LGB LGREEN|_BLUE

void getinfo()
{
    extern COM com;
    extern byte status1,status2;
    extern byte pup(byte, byte *, byte *, byte);
    extern byte found;
    extern byte search(void);
    extern int hartcmd(byte *, byte, byte *, byte, byte *, byte);
    extern byte addr[];
    extern byte polling_addr;

    int i,j;
    byte mfr, mfrtype, softrev, hardrev, device_flags;
    byte s[20];
    byte tdd_data[41];  /*  tag, descriptor, date. */
    byte tdd_unpack_data[40];
    byte *pdate;
    byte tag[15],descriptor[17],date[30];
    byte msg_data[41];  /*  field instrum internal message. */
    byte message[40];
    byte sens_units; unsigned long int sens_ser_numb;
    #include "mfr.h"    /*  manufacturer's codes. */
    byte fass_data[41];
    unsigned long int final_assy_numb;
    byte wvariable,wvalue;
    byte month[][12] =  /*  table of months.  */
    {
        "Undefined",    /*  0   */
        "January",      /*  1   */
        "February",     /*  2   */
        "March",        /*  3   */
        "April",        /*  4   */
        "May",          /*  5   */
        "June",         /*  6   */
        "July",         /*  7   */
        "August",       /*  8   */
        "September",    /*  9   */
        "October",      /*  10  */
        "November",     /*  11  */
        "December"      /*  12  */
    };
    unsigned int year;
    byte status2_message[350];
    unsigned int tdd,msg,fass;
    byte fass_message[20];
    byte wvalueunits;
    byte cmd;
    byte byte_count;
    byte *prdv, pv, unitss;
    byte packed_tag[7], packed_descriptor[13];
    byte strmonth[3], strday[3], stryear[5];
    byte mmonth, day;
    byte junk[1];
    byte status2_OK;
    byte local_status2;
    byte local_status1;

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

    Wopen(5,18,13,62,2,WHITE|_BLUE,WHITE|_BLUE);
    WputCen("\n\nGetting Field Instrument Information...");

    status2_OK = 0;

    /*  save mfr, mfr type, software rev, hardware rev, flags. */
    mfr = addr[1]; mfrtype = addr[2];
    softrev = addr[6]; hardrev = addr[7];
    device_flags = addr[8];

    /*  get tag, descriptor, date. */
    tdd = hartcmd(junk, 0, tdd_data, 21, addr, 13);

    /*  command succeed?  check byte count.  */
    if (tdd != 256*21)
    {
        err1(13);   /*  didn't succeed.  */
        /*  indicate data not available. */
        strcpy(tag, "Not Available");
        strcpy(descriptor, "Not Available");
        strcpy(date, "Not Available");
    }
    else
    {
        /*  succeeded.  so unpack the first 18 bytes of response. */
        UNPACK(tdd_data, tdd_unpack_data, 18);

        /*  save tag. */
        strcpy(tag,strleft(tdd_unpack_data,8));

        /*  save descriptor. */
        strcpy(descriptor,strleft(&tdd_unpack_data[8],16));

        /*  put bounds on date.  */
        if (tdd_data[19] > 12) tdd_data[19] = 0;
        if (tdd_data[19] < 0) tdd_data[19] = 0;

        /*  save date as char string. */
        strcpy(date, month[tdd_data[19]]);
        if (strcmp(date, "Undefined") != 0)
        {
            /*  complete date if month defined. */
            itoa(tdd_data[18], s, 10);
            strcat(date, " "); strcat(date, s); strcat(date, ", ");
            itoa(tdd_data[20]+1900, s, 10);
            strcat(date, s);
        }

        /*  indicate status2 can be used and save it.  */
        ++status2_OK; local_status2 = status2;
    }

    /*  report any errors associated with getting tag, etc.  */
    if (status1 != 0) err(13);
    local_status1 = status1;

    /*  get field instrument message. */
    msg = hartcmd(junk, 0, msg_data, 24, addr, 12);

    /*  command successful?  */
    if (msg != 256*24)
    {
        err1(12);   /*  didn't succeed.  */
        /*  indicate message not available.  */
        strcpy(message, "Not Available");
    }
    else
    {
        /*  successful. */
        /*  unpack and save message. */
        UNPACK(msg_data, message, 24);
        message[32] = '\0';

        /*  indicate status2 can be used and save it.  */
        ++status2_OK; local_status2 = status2;
    }

    /*  report any errors associated with getting message.  */
    if (status1 != 0) err(12);
    local_status1 = local_status1 | status1;

    /*  get final assembly number. */
    fass = hartcmd(junk, 0, fass_data, 3, addr, 16);

    /*  command successful?  */
    if (fass != 3*256)
    {
        err1(16);   /*  didn't succeed.  */
        /*  indicate final assy number not available.  */
        strcpy(fass_message, "Not Available");
    }
    else
    {
        /*  success.  convert number to string. */
        final_assy_numb = fass_data[2] +
        256L*(fass_data[1] + 256L*fass_data[0]);
        itoa(final_assy_numb, fass_message, 10);

        /*  indicate status2 can be used and save it.  */
        ++status2_OK; local_status2 = status2;
    }

    /*  report any error associated with getting final assy number.  */
    if (status1 != 0) err(16);
    local_status1 = local_status1 | status1;

    Wclose();

    /*  if one of the commands succeeded, form message based on 
        most recent status2 byte.  */
    if (status2_OK != 0)
    {
        status2 = local_status2;

        strcpy(status2_message,"\n\n");
        if (status2 == 0) 
            strcat(status2_message,"    Field Instrument Reports it is OK.\n");
        if ((status2 & 0x80) != 0) 
            strcat(status2_message,"    Field Instrument Reports Malfunction.\n");
        if ((status2 & 0x40) != 0) strcat(status2_message,
            "    Field Instrument Reports Configuration Changed.\n");
        if ((status2 & 0x20) != 0) 
            strcat(status2_message,"    Field Instrument Reports Cold Start.\n");
        if ((status2 & 8) != 0) 
            strcat(status2_message,"    Field Instrument Reports Output Current Fixed.\n");
        if ((status2 & 4) != 0) 
            strcat(status2_message,"    Field Instrument Reports Analog Output Saturated.\n");
        if ((status2 & 2) != 0) 
        {
            strcat(status2_message,
                "    Field Instrument Reports Non-Primary\n");
            strcat(status2_message,
                "     Variable Out of Limits.\n");
        }
        if ((status2 & 1) != 0)
        {
            strcat(status2_message,
                "    Field Instrument Reports Primary\n");
            strcat(status2_message,
                "     Variable Out of Limits.\n");
        }
        strcat(status2_message,"\n\n                Hit any key to resume\n\n");

        /*  put up a screen that shows the status2 info.  */
        Wopen(1,13,15,67,2,WHITE|_RED,WHITE|_RED);
        Wtitle("<<  Field Instrument Condition  >>",TTL_CNT,YELLOW|_RED);
        Wputf("%s", status2_message); KeyWait(); Wclose();
    }

    /*  indicate error if any.  */
    if (local_status1 != 0)
    {
        Wopen(5,20,10,60,5,WHITE|_RED,WHITE|_RED);
        WputCen("\n\nPossible Error in Getting");
        WputCen("\nField Instrument Info.");
        WputCen("\n\nHit any key to resume.");
        KeyWait(); Wclose();
    }

    /* put up a screen that shows all the fi info. */
    wvariable = Wopen(1,5,23,40,5,WHITE|_RED,WHITE|_RED);
    WputCen("<< Field Instrument Property >>");
    wvalue = Wopen(1,41,23,75,5,LGREEN|_BLUE,LGREEN|_BLUE);
    WVputs(wvariable,"\n  ----------------------------------");
    WVputs(wvalue,"\n---------------------------------\n");
    WVputs(wvariable," Manufacturer\n");
    WVputf(wvalue, " %s\n",mfr_code[mfr]);
    WVputs(wvariable," Manufacturer's Device Type Code\n");
    WVputf(wvalue, " %d\n",mfrtype);
    WVputs(wvariable," Final Assembly Number\n");
    WVputf(wvalue, " %s\n", fass_message);
    WVputs(wvariable,"  ----------------------------------");
    WVputs(wvalue, "---------------------------------\n");
    WVputs(wvariable," Tag\n");
    WVputf(wvalue, " %s\n", tag);
    WVputs(wvariable, " Descriptor\n");
    WVputf(wvalue, " %s\n", descriptor);
    WVputs(wvariable," Date\n");
    WVputf(wvalue, " %s\n", date);
    WVputs(wvariable," Message\n");
    WVputf(wvalue, " %s\n", message);
    WVputs(wvariable,"  ----------------------------------");
    WVputs(wvalue, "---------------------------------\n");
    WVputs(wvariable," Software Revision Level\n");
    WVputf(wvalue, " %d\n", softrev);
    WVputs(wvariable," Hardware Revision Level\n");
    WVputf(wvalue, " %d\n", hardrev);
    WVputs(wvariable," Long Address (Hex)\n");
    for (i=1; i<=5; ++i) WVputf(wvalue, " %x ", addr[i]); WVputs(wvalue, "\n");
    WVputs(wvariable," Polling (short) Address (Hex)\n");
    WVputf(wvalue, " %x\n", polling_addr);
    WVputs(wvariable," Number of Preambles Required\n");
    WVputf(wvalue, " %d\n", addr[0]);
    WVputs(wvariable, "  ----------------------------------");
    WVputs(wvalue, "---------------------------------\n");
    WVputs(wvariable," Device Flags Byte (Hex)\n");
    WVputf(wvalue, " %x\n", device_flags);
    gotoxy(30,23);
    printf("Hit any key to resume");
    KeyWait(); Wclose(); Wclose();
}

