/****************************************************************************/
/* Copyright 1992 to 1996 MBARI                                             */
/****************************************************************************/
/* Summary  : Set Tiburon Data Manager Items to stecified state             */
/* Filename : setState.c                                                    */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 04/29/96                                                      */
/* Modified : 04/29/96                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: $
 * $Log:    $
 */
/****************************************************************************/

#include <vxWorks.h>                /* vxWorks system declarations          */
#include <stdioLib.h>               /* vxWorks standard I/O functions       */
#include <string.h>                 /* vxWorks string functions             */
#include <lstLib.h>                 /* vxWorks Linked List library          */
#include <semLib.h>                 /* vxWorks Semaphore Library            */

#include <mbariTypes.h>             /* mbari style guide type declarations  */
#include <mbariConst.h>             /* Miscellaneous constants              */
#include <usrTime.h>                /* MBARI time and date functions        */
#include <rovPriority.h>            /* MBARI Rov Application Priorities     */
#include <usrTime.h>                /* MBARI time and date functions        */

#include <tiburon.h>                /* Tiburon definitions                  */

#include <datamgr.h>                /* Data Manager declarations            */
#include <dm_errno.h>               /* Data Manager error declarations      */

#define ROV_DOWN_CONFIG_FILE  "/usr/tiburon/config/powerDownState"
#define FIELD_DELIM           " ,\t\n"
#define LINE_LEN              132

    MLocal Int32
getDmItemSize(DM_Item item)
{
    Int32 size;
    DmItemSts itemStatus;

    if (dm_item_status(item, &itemStatus) != SUCCESS)
        return (ERROR);

    size = itemStatus.is_size;

    dm_item_status_free(&itemStatus);
    return(size);
} /* getDmItemSize() */

    MLocal DM_Type
getDmItemType(DM_Item item)
{
    DM_Type type;
    DmItemSts itemStatus;

    if (dm_item_status(item, &itemStatus) != SUCCESS)
        return (ERROR);

    type = itemStatus.is_type;

    dm_item_status_free(&itemStatus);
    return(type);
} /* getDmItemType() */

    STATUS
asciiToBinary(DM_Type type, char* ascii, char* value)
{
    switch( type )
    {
        case DM_EMPTY:
          *value = 0;
          break;

        case DM_CHAR:
        case DM_UCHAR:
          sscanf(ascii, "%c", value);
          break;

        case DM_INT16:
        case DM_MBOOL:
          sscanf(ascii, "%hd", value);
          break;

        case DM_NAT16:
          sscanf(ascii, "%hu", value);
          break;

        case DM_INT32:
        case DM_ENUM:
          sscanf(ascii, "%d", value);
          break;

        case DM_NAT32:
          sscanf(ascii, "%u", value);
          break;

        case DM_FLT32:
        case DM_FLT64:
          sscanf(ascii, "%f", value);
          break;

        case DM_PTR:
          sscanf(ascii, "%f#x", value);
          break;

        default:
          printf("Unknown type %d\n", type );
          return (ERROR);

    } /* switch */

    return (OK);
} /* asciiToBinary() */

    STATUS
setDmItemsToFileValues( char *dmFile )
{
    FILE   *configFile;             /* Configuration File Handle             */
    char   buffer[LINE_LEN];        /* Scratch buffer for freads             */
    char   *token, *ptr, *ascii;

    DM_Time updateTime;

    struct
    {
        DM_Item item;
        DM_Size size;
        DM_Type type;
        Int32   index;
        char    value[MAX_ITM_SIZE];
    } dmItem;

    if ( (configFile = fopen(dmFile, "r")) != NULL )
    {
                                    /* Get time & date from clock           */
        gettimeofday(&updateTime, (struct timezone *) NULL);

                                    /* Start reading configuration file      */
        while(fgets(buffer, LINE_LEN, configFile) != NULL)
        {
            token = (strtok(buffer, FIELD_DELIM));
            if (token[0] == '#')    /* Ignore for comment lines              */
                break;

            if ((ptr = strtok(NULL, FIELD_DELIM)) == NULL)
                break;

            if ((ascii  = strtok(NULL, FIELD_DELIM)) == NULL)
            {
                dmItem.index = 0;
                ascii = ptr;
            } /* if */
            else
                dmItem.index = atoi(ptr);

            if ( (dmItem.item = dm_lookup(token, dmItem.index)) == NO_ITEM)
                break;

            if ( (dmItem.size = getDmItemSize(dmItem.item)) == ERROR)
                break;

            dmItem.type = getDmItemType(dmItem.item);
            if (asciiToBinary(dmItem.type, ascii, dmItem.value) == OK)
            {
                dm_urgent_open(dmItem.item, DM_STATIC);
                                    /* Write item to Data Manager           */

                dm_write(dmItem.item, dmItem.value, dmItem.size,
                     &updateTime);

                dm_urgent_close(dmItem.item);
            } /* if */
        } /* while */
    } /* if */

    fclose(configFile);
} /* setDmItemToFileValues() */

    STATUS
tiburonPowerDownTask ( Void )
{
    DM_Item tiburonAliveDm;
    MBool   vehicleAlive;
    SEM_ID  wakeupSem;              /* Wakes up this task when vehicle dies */

                                    /* Set this task priority               */
    taskPrioritySet(taskIdSelf(), TIBURON_DOWN_TASK_PRIORITY);

                                    /* Create wakeup semaphore              */
    if ((wakeupSem = semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL )
    {
        logMsg("Vehicle Power Down Task: Error creating semaphore\n");
        return;
    } /* if */

    taskDelay(sysClkRateGet() * 10);              /* Ten second power delay */

    tiburonAliveDm =
        initMicroDmItem(TIBURON_DM_PREFIX, TIBURON_ALIVE_DM, DM_MBOOL, 1);

    if ( (tiburonAliveDm == ERROR) ||
        (dm_start_consumer(tiburonAliveDm, DM_STATIC, wakeupSem) != SUCCESS) )
            return;

    FOREVER
    {
        semTake(wakeupSem, WAIT_FOREVER);

        dm_read(tiburonAliveDm, (char *) &vehicleAlive,
                sizeof(vehicleAlive), (DM_Time *) NULL);

        if (!vehicleAlive)
        {
            logMsg("Vehicle Died\n");
            setDmItemsToFileValues(ROV_DOWN_CONFIG_FILE);
        } /* if */
    } /* FOREVER */

} /* tiburonPowerDownTask() */













