/****************************************************************************/
/* Copyright 1997 MBARI                                                     */
/****************************************************************************/
/* Summary  : Network & Data Manager Turnaround time                        */
/* Filename : tatTop.c                                                      */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 04/03/97                                                      */
/* Modified : 04/03/97                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header:
 * $Log:
 */
/****************************************************************************/

#include <vxWorks.h>                /* vxWorks system declarations          */
#include <semLib.h>                 /* vxWorks semaphore functions          */
#include <stdioLib.h>               /* vxWorks standard I/O functions       */
#include <msgQLib.h>                /* vxWorks Message Queue Library        */

#include "mbariTypes.h"             /* MBARI style guide type declarations  */
#include "mbariConst.h"             /* Miscellaneous constants              */

#include "rovPriority.h"            /* MBARI Rov Application Priorities     */
#include "usrTime.h"                /* MBARI time and date functions        */

#include "datamgr.h"                /* Data Manager declarations            */
#include "dm_errno.h"               /* Data Manager error declarations      */

#include "tiburon.h"                /* Tiburon definitions                  */

/****************************************************************************/
/* Function    : tatTopTask                                                 */
/* Purpose     : Measures turnaround time to the vehicle via Network and DM */
/* Inputs      : NONE                                                       */
/* Outputs     : NONE, but DM Item is modified                              */
/****************************************************************************/
   STATUS
tatTopTask( Void )
{
    SEM_ID  dmUpdateSem;            /* Used wake up this task periodically  */
    DM_Item consoleDmItem;          /* Data Manager Item written by console */
    DM_Item vehicleDmItem;          /* Data Manager Item written by vehicle */

    Int32   consoleCount;
    Int32   vehicleCount;

    DM_Time startTime;              /* Start time in usecs                  */
    DM_Time endTime;                /* End time in usecs                    */
    Nat32   deltaTime;              /* Time difference in microseconds      */
    Nat32   minTime, maxTime, sumTime;
    Nat32   count;

                                    /* Set this task priority               */
    taskPrioritySet(taskIdSelf(), TURN_AROUND_TASK_PRIORITY);

                                    /* Create wakeup semaphore              */
    if ((dmUpdateSem = semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL )
    {
        logMsg("Turnaround Time Task: Error creating semaphore\n");
        return;
    } /* if */

    consoleDmItem =
        initMicroDmItem(TIBURON_DM_PREFIX, CONSOLE_TURN_AROUND_DM, DM_INT32, 1);

    vehicleDmItem =
        initMicroDmItem(TIBURON_DM_PREFIX, VEHICLE_TURN_AROUND_DM, DM_INT32, 1);

    dm_start_provider(consoleDmItem, DM_ASYNC);
    dm_start_consumer(vehicleDmItem, DM_ASYNC, dmUpdateSem);

    consoleCount = 1;

    minTime = (Nat32) -1;
    maxTime = sumTime = count = 0;

    FOREVER
    {                               /* Get time & date from clock           */
        gettimeofday(&startTime, (struct timezone *) NULL);

        writeDmItem(consoleDmItem, &consoleCount, sizeof(consoleCount));

                                    /* Wait heartbeat time                   */
        if (semTake(dmUpdateSem, sysClkRateGet()) == ERROR)
            printf("TAT Timeout  seq %d\n", consoleCount);
        else
        {
            dm_read(vehicleDmItem, &vehicleCount, sizeof(vehicleCount),
                (DM_Time *) NULL);

            gettimeofday(&endTime, (struct timezone *) NULL);

            deltaTime = ((endTime.tv_sec  - startTime.tv_sec) * USECS_PER_SEC +
                         (endTime.tv_usec - startTime.tv_usec)) / 1000;

            minTime = min(minTime, deltaTime);
            maxTime = max(maxTime, deltaTime);
            sumTime += deltaTime;
            count++;

            printf("TAT seq %d time %d msec min/max/avg %d/%d/%d\n",
                    consoleCount, deltaTime, minTime, maxTime, sumTime / count);
        } /* else */

        consoleCount++;
    } /* FOREVER */
} /* tatTopTask() */


/****************************************************************************/
/* Function    : tatBottomTask                                              */
/* Purpose     : Measures turnaround time to the vehicle via Network and DM */
/* Inputs      : NONE                                                       */
/* Outputs     : NONE, but DM Item is modified                              */
/****************************************************************************/
   STATUS
tatBottomTask( Void )
{
    SEM_ID  dmUpdateSem;            /* Used wake up this task periodically  */
    DM_Item consoleDmItem;          /* Data Manager Item written by console */
    DM_Item vehicleDmItem;          /* Data Manager Item written by vehicle */
    Int32   consoleCount;

                                    /* Set this task priority               */
    taskPrioritySet(taskIdSelf(), TURN_AROUND_TASK_PRIORITY);

                                    /* Create wakeup semaphore              */
    if ((dmUpdateSem = semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL )
    {
        logMsg("Turnaround Time Task: Error creating semaphore\n");
        return;
    } /* if */

    consoleDmItem =
        initMicroDmItem(TIBURON_DM_PREFIX, CONSOLE_TURN_AROUND_DM, DM_INT32, 1);

    vehicleDmItem =
        initMicroDmItem(TIBURON_DM_PREFIX, VEHICLE_TURN_AROUND_DM, DM_INT32, 1);

    dm_start_consumer(consoleDmItem, DM_STATIC, dmUpdateSem);
    dm_start_provider(vehicleDmItem, DM_STATIC);

    FOREVER
    {
        semTake(dmUpdateSem, WAIT_FOREVER);

        dm_read(consoleDmItem, &consoleCount, sizeof(consoleCount),
                (DM_Time *) NULL);

        writeDmItem(vehicleDmItem, &consoleCount, sizeof(consoleCount));

    } /* FOREVER */
} /* tatBottomTask() */





