#include <apf9.h>
#include <logger.h>
#include <control.h>
#include <cmds.h>
#include <eeprom.h>

/* function prototypes */
int FwStartUp(void);

/* the firmware revision is supplied automatically from the compile command */
const unsigned long FwRev=FWREV;

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * $Id: apf9main.c,v 1.10 2009/07/16 14:05:52 swift Exp $
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Copyright University of Washington.   Written by Dana Swift.
 *
 * This software was developed at the University of Washington using funds
 * generously provided by the US Office of Naval Research, the National
 * Science Foundation, and NOAA.
 *  
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * RCS Log:
 *
 * $Log: apf9main.c,v $
 * Revision 1.10  2009/07/16 14:05:52  swift
 * Implement mag-switch shut-down feature and 20mA squawk.
 *
 * Revision 1.9  2007/05/08 18:10:39  swift
 * Added attribution just below the copyright in the main comment section.
 *
 * Revision 1.8  2006/10/13 18:53:09  swift
 * Add conditioning to logstream.
 *
 * Revision 1.7  2006/08/17 21:17:59  swift
 * Modifications to allow better logging control.
 *
 * Revision 1.6  2006/04/21 13:45:42  swift
 * Changed copyright attribute.
 *
 * Revision 1.5  2004/12/29 23:11:26  swift
 * Modified LogEntry() to use strings stored in the CODE segment.  This saves
 * lots of space in the DATA segment and significantly speeds code start-up.
 *
 * Revision 1.4  2004/03/24 00:15:01  swift
 * Added provision for debuglevel to be a persistent variable.
 *
 * Revision 1.3  2003/11/20 18:59:47  swift
 * Added GNU Lesser General Public License to source file.
 *
 * Revision 1.2  2003/09/11 17:15:01  swift
 * Fixed return value of FwStartUp().
 *
 * Revision 1.1  2003/08/31 23:14:38  swift
 * Initial revision
 *
 *========================================================================*/
/* firmware to control an Apex Sbe41 profiler                             */
/*========================================================================*/
void main(void)
{
   /* run firmware startup code */
   FwStartUp();

   /* check for user request to enter command mode */
   if (CmdModeRequest()) CmdMode();

   /* check magnetic switch user interface for mission launch request */
   if (MagSwitchToggled())
   {
      /* get the current state of the mission */
      enum State state = StateGet();

      /* if a mission is active then toggle to pressure-activation mode */
      if (state>INACTIVE && state<EOS) {StateSet(PACTIVATE); if (ConioActive()) Apf9Squawk20mA(10000);}
      
      /* launch the mission */
      else MissionLaunch();
      
      /* reset the magnetic reset-switch */
      MagSwitchReset();
   }
   
   /* transfer to mission control agent */
   MissionControlAgent();
}

/*------------------------------------------------------------------------*/
/* function to initialize the mission on program startup                  */
/*------------------------------------------------------------------------*/
/**
   This function initializes the mission at program startup.  A positive
   value is returned on success; otherwise zero is returned.
*/
int FwStartUp(void)
{
   /* define the logging signature */
   static cc FuncName[] = "FwStartUp()";

   int status=0;

   /* initialize APF9 hardware and low-level code */
   Apf9Init();

   /* validate the mission currently stored in nonvolatile ram */
   if (MissionValidate()<=0)
   {
      /* create the message */
      static cc msg[]="Attempt to validate mission failed.  "
         "Initializing with default mission.\n";

      /* log the message */
      LogEntry(FuncName,msg);

      /* initialize the mission with the default mission */
      mission=DefaultMission;
   }
   else {status=1;}

   /* validate the logstream */
   if (fstreamok(logstream)<=0) logstream=NULL;
   
   /* validate the debuglevel */
   if (debuglevel>5) debugbits=2;

   return status;
}
