/****************************************************************************/
/* Copyright 1992 to 1997 MBARI                                             */
/****************************************************************************/
/* Summary  : Main ROV Software Initialization Task for VxWorks             */
/* Filename : startup.c                                                     */
/* Author   : Andrew Pearce                                                 */
/* Project  : Tiburon ROV                                                   */
/* Version  : Version 1.0                                                   */
/* Created  : 10/05/92                                                      */
/* Modified : 01/15/97                                                      */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/* $Header: $
 * $Log:    $
 * 
 */
/****************************************************************************/

#include <vxWorks.h>                /* vxWorks system declarations          */
#include <strLib.h>                 /* vxWorks string library functions     */
#include <taskLib.h>                /* vxWorks task library functions       */
#include <rebootLib.h>              /* vxWorks system reboot library funcs  */

#include "types.h"                  /* mbari style guide type declarations  */
#include "usrTime.h"                /* MBARI time and date functions        */
#include "rovPriority.h"            /* MBARI Rov Application Priorities     */
#include "datamgr.h"                /* Data Manager Declarations            */
#include "tiburon.h"        	    /* Tiburon Definitions                  */
#include "idleCpu.h"                /* Idle CPU Monitor Task                */
#include "hourLog.h"	            /* Hour log task Declarations           */
#include "powerMgr.h"	            /* Power Manager Declarations           */
#include "sio32Server.h"            /* SIO32 serial I/O Declarations        */
#include "ersDcon.h"                /* Emergency Recover System Declarations*/
#include "vmeIbc.h"                 /* VME Can IBC Declarations             */
#include "sonarDcon.h"              /* Sonar Dcon Declarations              */
#include "sensorDcon.h"             /* Sensor Dcon Declarations             */
#include "sio32Diag.h"              /* SIO32 Diagnostic Task                */
#include "telemDiag.h"              /* Telemetery Board Micro Task          */
#include "thrusterIF.h"	            /* Thruster Interface Task              */
#include "powerIbc.h"               /* Power Can DCon Task                  */
#include "lightingDcon.h"           /* Lighting DCon Task                   */
#include "rovControl.h"	            /* ROV Controller Definitions           */
#include "panTilt.h"	            /* Pan/Tilt Interface Task              */
#include "cameraDcon.h"	            /* Camera Dcon Interface Task           */
#include "camera.h"	            /* Panasonic Camera Interface Task      */
#include "acMicro.h"	            /* AC Microcontroller Interface Task    */
#include "vbSystem.h"	            /* Variable Buoyancy System Definitions */
#include "state.h"                  /* Kalman State Switch Task Definitions */
#include "../sio32Chan.h"	    /* SIO32 Serial Channel Allocations     */

extern 	void sysVmeBusReset( void );
extern  int telnetTaskPriority;

/****************************************************************************/
/* Function    : spawnTask                                                  */
/* Purpose     : Executes a vxWorks task. Convenience function for taskSpawn*/
/* Inputs      : name, priority, stack size, task options, messages for     */
/*               successful and unsuccessful task creation. func ptr & args */
/* Outputs     : Returns OK or ERROR                                        */
/****************************************************************************/
    LOCAL STATUS
spawnTask( char *taskName, int priority, int stacksize, int options,
    char *okMsg, char *errorMsg, FUNCPTR entryPoint, int arg1, int arg2)
{
                                    /* Initialize Task                      */
    if ( taskSpawn( taskName, priority, options, stacksize,
       entryPoint, arg1, arg2, 0, 0, 0, 0, 0, 0, 0, 0 ) != ERROR)
    {
       logMsg(okMsg);
       return(OK);
    } /* if */

    logMsg(errorMsg);
    return(ERROR);
} /* spawnTask() */

/****************************************************************************/
/* Function    : rovInit                                                    */
/* Purpose     : Initializes application software for Tiburon main VME CPU  */
/* Inputs      : None - Add calls to start application software here        */
/* Outputs     : None                                                       */
/****************************************************************************/
    Void
rovInit( Void )
{				    /* Set priority of initialization task  */
    taskPrioritySet(taskIdSelf(),  ROV_INIT_TASK_PRIORITY);

    usrTimeInit();		    /* Initialize MBARI time library         */

    telnetTaskPriority = 200;

    if (netDevCreate ("batray:", "batray.mbari.org", 0) != OK)
	logMsg("ROV Init: error creating ftp file system device\n");

    dm_init( (DM_Define*) NULL );   /* Initialize Data Manager              */	
    taskDelay(3 * sysClkRateGet());

    spawnTask("heartbeat", HEARTBEAT_TASK_PRIORITY, HEARTBEAT_TASK_STACKSIZE,0,
	"Heartbeat Task Initialized\n",
        "Heartbeat Task Initialization Failed\n", 
	(FUNCPTR) heartbeatTask, TIBURON_CPU1, NULL);

    spawnTask("consoleStatus", CONSOLE_STATUS_PRIORITY,
        TIBURON_STATUS_STACKSIZE, 0,
        "Console Status Monitor Task Initialized\n",
        "Console Status Monitor Task Initialization Failed\n",
	(FUNCPTR) consoleStatusTask, 0, NULL);       

    spawnTask("idleCpu", IDLE_CPU_TASK_PRIORITY, IDLE_CPU_TASK_STACKSIZE, 0,
	"Idle CPU Task Initialized\n",
        "Idle CPU Task Initialization Failed\n", 
	(FUNCPTR) idleCpuTask,
        (int) TIBURON_DM_PREFIX TIBURON_IDLE_CPU_PERCENT_DM,
        (int) TIBURON_DM_PREFIX TIBURON_IDLE_CPU_ALARM_DM);

    spawnTask("hourLog", HOUR_LOG_PRIORITY, HOUR_LOG_STACKSIZE, VX_FP_TASK,
	"Hour Log Task Initialized\n",
        "Hour Log Task Initialization Failed\n", 
	(FUNCPTR) hourLogTask, NULL, NULL);

    spawnTask("powerMgr", POWER_PRIORITY, POWER_STACKSIZE, VX_FP_TASK,
        "ROV Init: Power Manager Initialized\n",
	"ROV Init: Power Manager Initialization Failed\n",
	(FUNCPTR) powerTask, NULL, NULL);

    spawnTask("sio32Deamon", SIO32_DEAMON_PRIORITY, SIO32_DEAMON_STACK, 0,
        "Telem board SIO32 Deamon Task Initialized\n",
        "Telem board SIO32 Deamon Task Initialization Failed\n",
	(FUNCPTR) sio32DeamonTask, (int) TELEM_BOARD_NAME, NULL);       

    spawnTask("vmeIbc", VME_IBC_SRQ_PRIORITY, VME_IBC_STACKSIZE, VX_FP_TASK,
        "ROV Init: VME IBC Interface Task Initialized\n",
	"ROV Init: VME IBC Interface Task Initialization Failed\n",
	(FUNCPTR) VME_IBC_Task, VME_IBC_SERIAL_CHAN, NULL);

    spawnTask("StbdHoriz", THRUST_SRQ_PRIORITY, THRUST_SRQ_STACKSIZE, 0,
	"ROV Init: Stbd Horizontal Thruster Interface Task Initialized\n",
	"ROV Init: Stbd Horizontal Thruster Interface Task Failed\n",
	(FUNCPTR) thrusterTask, STBD_HORIZ, STBD_HORIZ_THRUST_SERIAL_CHAN);

    spawnTask("PortHoriz", THRUST_SRQ_PRIORITY, THRUST_SRQ_STACKSIZE, 0,
        "ROV Init: Port Horizontal Thruster Interface Task Initialized\n",
	"ROV Init: Port Horizontal Thruster Interface Task Failed\n",
	(FUNCPTR) thrusterTask, PORT_HORIZ, PORT_HORIZ_THRUST_SERIAL_CHAN);

    spawnTask("PortVert",  THRUST_SRQ_PRIORITY, THRUST_SRQ_STACKSIZE, 0,
	"ROV Init: Port Vertical Thruster Interface Task Initialized\n",
	"ROV Init: Port Vertical Thruster Interface Task Failed\n",
	(FUNCPTR) thrusterTask, PORT_VERT, PORT_VERT_THRUST_SERIAL_CHAN);

    spawnTask("StbdVert",  THRUST_SRQ_PRIORITY, THRUST_SRQ_STACKSIZE, 0,
	"ROV Init: Stbd Vertical Thruster Interface Task Initialized\n",
	"ROV Init: Stbd Vertical Thruster Interface Task Failed\n",
	(FUNCPTR) thrusterTask, STBD_VERT, STBD_VERT_THRUST_SERIAL_CHAN);

    spawnTask("FwdLat",    THRUST_SRQ_PRIORITY, THRUST_SRQ_STACKSIZE, 0,
	"ROV Init: Forward Lateral Thruster Interface Task Initialized\n",
	"ROV Init: Forward Lateral Thruster Interface Task Failed\n",
	(FUNCPTR) thrusterTask, FWD_LATERAL, FWD_LAT_THRUST_SERIAL_CHAN);

    spawnTask("AftLat",    THRUST_SRQ_PRIORITY, THRUST_SRQ_STACKSIZE, 0,
	"ROV Init: Aft Lateral Thruster Interface Task Initialized\n",
	"ROV Init: Aft Lateral Thruster Interface Task Failed\n",
	(FUNCPTR) thrusterTask, AFT_LATERAL, AFT_LAT_THRUST_SERIAL_CHAN);

    spawnTask("ROVCtrl",   ROV_CTRL_PRIORITY, ROV_CTRL_STACKSIZE, VX_FP_TASK,
	"ROV Init: ROV Control System Task Initialized\n",
	"ROV Init: ROV Control System Task Failed\n",
	(FUNCPTR) controlTask, NULL, NULL );

    spawnTask("sensorIbc", SENSOR_IBC_SRQ_PRIORITY, SENSOR_IBC_STACKSIZE, 0,
	"ROV Init: Sensor DCcon Interface Task Initialized\n",
        "ROV Init: Sensor DCon Interface Task Failed\n",
 	(FUNCPTR) sensorDconTask, SENSOR_IBC_SERIAL_CHAN,  NULL);

    logMsg("ROV VME Init: Software Initialization Complete\n");

    taskDelay(sysClkRateGet() * 5);
} /* rovInit() */


