/****************************************************************************/
/* Copyright 1992 to 1993, MBARI                                            */
/****************************************************************************/
/* Summary  : IBC 80C196 CPU Board Functions for VxWorks                    */
/* Filename : ibc.c                                                         */
/* Author   : Andrew Pearce                                                 */
/* Project  : New ROV                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 04/09/93                                                      */
/* Modified : 04/09/93                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: /usr/tiburon/vw/ibc/lib/RCS/ibc.c,v 1.1 1998/02/10 21:28:42 oreilly Exp oreilly $
 * $Log: ibc.c,v $
 * Revision 1.1  1998/02/10 21:28:42  oreilly
 * Initial revision
 *
 * Revision 1.1  93/07/01  14:34:29  14:34:29  pean (Andrew Pearce)
 * Initial revision
 *
 *
 */
/****************************************************************************/

#include <vxWorks.h>                /* vxWorks system declarations          */
#include <semLib.h>                 /* vxWorks semaphore functions          */
#include <wdLib.h>                  /* vxWorks watch dog timer support decls*/
#include <msgQLib.h>                /* vxWorks Message Queue Library        */

#include <mbariTypes.h>             /* mbari style guide type declarations  */
#include <mbariConst.h>             /* Miscellaneous constants              */

#include "sio32.h"                  /* SIO32 hardware and driver information*/
#include <sio32Server.h>            /* Sio32 server communication services  */
#include <sio32Client.h>            /* Sio32 client communication services  */
#include <microCmd.h>               /* Microcontoller command functions     */

#include "applic.h"                 /* Microcontroller applications         */
#include "microcmd.h"               /* Microcontroller commands             */

#define Boolean MBool               /* Map microcontroller Boolean to MBool */

#include "ibc_card.h"               /* IBC Card Types                       */
#include "ibc_cmd.h"                /* IBC Card Serial Commands             */

#include "ibc.h"                    /* Function Prototypes for ibc.c        */

/****************************************************************************/
/* Function    : ibcCpuGetCardConfig                                        */
/* Purpose     : Read list of IBC cards from Data Concentrator              */
/* Inputs      : Sio32 channel, ptr to Num cards, array of IBC addresses    */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
ibcCpuGetCardConfig( sio32Chan *IBC_SerialChan, ibcCardConfig *ibcCards )
{
    Int16  card;
    Word   reply[IBC_MAX_CARDS + 2];

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan,
         &reply, sizeof(Word) * IBC_MAX_CARDS + 2,
         2, IBC_CPU_APP | IBC_CARD_CONFIG ) == ERROR ) return(ERROR);

                                    /* Check command result                  */
    if (wordFromBuf(reply, 0) != CMD_OK)
        return(ERROR);
                                    /* Extract number of IBC cards found     */
    ibcCards->numCards = (Nat16) wordFromBuf(reply, sizeof(Word));
    if (ibcCards->numCards > IBC_MAX_CARDS) return(ERROR);
                                    /* Extract IBC board addresses           */
    for (card = 0; card < ibcCards->numCards; card++)
        ibcCards->address[card] = wordFromBuf(reply, (card + 2) * sizeof(Word));

    for (card = card; card < IBC_MAX_CARDS; card++)
        ibcCards->address[card] = 0;

    return(OK);
} /* ibcCpuGetCardConfig() */

/****************************************************************************/
/* Function    : ibcCpuGetHumidity                                          */
/* Purpose     : Reads humidity sensor value from IBC CPU                   */
/* Inputs      : Sio32 channel, pointer to humidity sensor value            */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
ibcCpuGetHumidity( sio32Chan *IBC_SerialChan, Int16 *humidity )
{
                                    /* Send command to microcontroller      */
    return(microCommand(IBC_SerialChan, IBC_CPU_APP | READ_HUMIDITY,
                        (Word *) humidity));
} /* ibcCpuGetHumidity() */

/****************************************************************************/
/* Function    : ibcCpuSetHumidityThresh                                    */
/* Purpose     : Sets humidity alarm threshold value from IBC CPU           */
/* Inputs      : Sio32 channel, humidity alarm threshold value              */
/* Outputs     : Return OK or ERROR                                         */
/****************************************************************************/
    STATUS
ibcCpuSetHumidityThresh( sio32Chan *IBC_SerialChan, Int16 humidityWarnThresh,
    Int16 humidityAlarmThresh )
{
    Word    status;                 /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &status, sizeof(status),
         3, IBC_CPU_APP | SET_HUMIDITY_THRESH,
         humidityWarnThresh, humidityAlarmThresh ) == ERROR )
        return(ERROR);
                                    /* Check command result                  */
    return (wordFromBuf(&status, 0) == CMD_OK ? OK : ERROR);
} /* ibcCpuSetHumidityThresh() */

/****************************************************************************/
/* Function    : ibcCpuGetHumidityThresh                                    */
/* Purpose     : Reads humidity alarm threshold value from IBC CPU          */
/* Inputs      : Sio32 channel, pointer to humidity alarm threshold value   */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
ibcCpuGetHumidityThresh( sio32Chan *IBC_SerialChan, Int16 *humidityWarnThresh,
    Int16 *humidityAlarmThresh )
{
    Int16   cmdStatus;              /* Command Status                       */
    Word    reply[3];               /* Command Result from microcontroller  */

                                    /* Send command to microcontroller      */
    if ( sendMicroCommand( IBC_SerialChan, &reply, sizeof(Word) * 3,
         1, IBC_CPU_APP | GET_HUMIDITY_THRESH ) == ERROR )
        return(ERROR);
                                    /* Check if command result is CMD OK    */
    if (wordFromBuf(&reply, 0) != CMD_OK)
        return(ERROR);              /* Command failed so return ERROR       */

                                    /* Command OK so update currents        */
    wordsFromBuf(reply, 3, &cmdStatus, humidityWarnThresh, humidityAlarmThresh);

    return (OK);
} /* ibcCpuGetHumidityThresh() */

/****************************************************************************/
/* Function    : ibcCpuGetHumidityAlarm                                     */
/* Purpose     : Reads humidity alarm status from IBC CPU                   */
/* Inputs      : Sio32 channel, pointer to humidity alarm value             */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    STATUS
ibcCpuGetHumidityAlarm( sio32Chan *IBC_SerialChan, MBool *humidityAlarm )
{
                                    /* Send command to microcontroller      */
    return( microCommand( IBC_SerialChan, IBC_CPU_APP | READ_HUMIDITY_ALARM,
         (Word *) humidityAlarm) );
} /* ibcCpuGetHumidityAlarm() */

