/****************************************************************************/
/* Copyright 1995 to 1996 MBARI                                             */
/****************************************************************************/
/* Summary  : Vehicle Heartbeat Generator for vxWorks                       */
/* Filename : heartbeat.c                                                   */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 10/12/94                                                      */
/* Modified : 05/20/96                                                      */
/* 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    : heartbeatTask                                              */
/* Purpose     : Generates Vehicle Heartbeat                                */
/* Inputs      : NONE                                                       */
/* Outputs     : NONE, but DM Item is modified                              */
/****************************************************************************/
   STATUS
heartbeatTask( tiburonCpuId cpuId )
{
    SEM_ID  wakeupSem;              /* Used wake up this task periodically  */
    DM_Item heartBeatDm;            /* Vehicle Heartbeat Data Manager Item  */
    Nat32   heartbeat = 0;          /* Vehicle Heartbeat                    */

                                    /* Set this task priority               */
    taskPrioritySet(taskIdSelf(), HEARTBEAT_TASK_PRIORITY);

                                    /* Create wakeup semaphore               */
    if ((wakeupSem = semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL )
    {
        logMsg("Vehicle Status Task: Error creating semaphore\n");
        return;
    } /* if */


    if (cpuId == TIBURON_CPU1)
        heartBeatDm =
          initMicroDmItem(TIBURON_DM_PREFIX, TIBURON_HEARTBEAT_DM, DM_NAT32, 1);

    else if (cpuId == CONSOLE_CPU)
        heartBeatDm =
          initMicroDmItem(TIBURON_DM_PREFIX, CONSOLE_HEARTBEAT_DM, DM_NAT32, 1);

    else
    {
        logMsg("Heartbeat not supported for this CPU\n");
        return (ERROR);
    } /* else */

    dm_start_provider(heartBeatDm, TIBURON_HEARTBEAT_RATE);

    FOREVER
    {
        heartbeat++;                /* Increment heartbeat                   */
        writeDmItem(heartBeatDm, &heartbeat, sizeof(heartbeat));
                                    /* Wait heartbeat time                   */
        semTake(wakeupSem, (TIBURON_HEARTBEAT_RATE / USECS_PER_SEC)
             * sysClkRateGet());
    } /* FOREVER */
} /* heartbeatTask() */





