/****************************************************************************/
/* Copyright 1998 MBARI                                                     */
/****************************************************************************/
/* Summary  : Iview restart program for vxWorks                             */
/* Filename : restartIview.c                                                */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon                                                       */
/* Version  : Version 1.0                                                   */
/* Created  : 03/11/98                                                      */
/* Modified : 03/11/98                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header:
 * $Log:
 */
/****************************************************************************/

#include <vxWorks.h>                /* vxWorks system declarations          */
#include <semLib.h>                 /* vxWorks semaphore functions          */
#include <taskLib.h>                /* vxWorks task library functions       */

#include "mbariTypes.h"             /* MBARI style guide type declarations  */
#include "mbariConst.h"             /* Miscellaneous constants              */

#include "datamgr.h"                /* Data Manager declarations            */
#include "dm_errno.h"               /* Data Manager error declarations      */

#include "rovPriority.h"            /* mbari Rov Application Priorities     */

#include "tiburon.h"                /* Tiburon definitions                  */
#include "iviewTask.h"              /* Integrated Viewing Task (IView)      */

#define IVIEW_TASK_NAME "iView"

/****************************************************************************/
/* Function    : restartIviewTask                                           */
/* Purpose     : Processes requests to restart iview                        */
/* Inputs      : NONE                                                       */
/* Outputs     : NONE                                                       */
/****************************************************************************/
   STATUS
restartIviewTask( Void )
{
    Int32   iviewTaskId;            /* Iview Task Id                        */
    SEM_ID  wakeupSem;              /* Used wake up this task periodically  */
    DM_Item restartIviewDm;         /* Restart Iview Data Manager Item      */

                                    /* Set this task priority               */
    taskPrioritySet(taskIdSelf(), 200);

                                    /* Create wakeup semaphore              */
    if ((wakeupSem = semBCreate(SEM_Q_FIFO, SEM_EMPTY)) == NULL )
    {
        logMsg("Restart Iview Task: Error creating semaphore\n");
        return;
    } /* if */

    restartIviewDm =
        initMicroDmItem(TIBURON_DM_PREFIX, RESTART_IVIEW_DM, DM_EMPTY, 1);

    dm_start_consumer(restartIviewDm, DM_STATIC, wakeupSem);

    FOREVER
    {                               /* Wait for requests to restart iview    */
        semTake(wakeupSem, WAIT_FOREVER);

        logMsg("restart Iview: Calling exitIview\n");

        exitIview();               /* Clean way to delete Iview task         */
                                   /* Wait for Iview Task to exit            */
        taskDelay(sysClkRateGet() * 3);

        if ((iviewTaskId = taskNameToId(IVIEW_TASK_NAME)) != ERROR)
        {
          logMsg("restart Iview: deleting Iview Task\n");
          taskDelete(iviewTaskId);
        } /* if */

        if (taskSpawn(IVIEW_TASK_NAME, IVIEW_PRIORITY, VX_FP_TASK, IVIEW_STACKSIZE,
          (FUNCPTR) iview, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) != ERROR)
          logMsg("restart Iview: IView Task Restarted\n");
        else
          logMsg("restart Iview: IView Task was NOT Restarted\n");

    } /* FOREVER */
} /* restartIviewTask() */




