/** \file
 *
 *  Contains the SBIT class definition.
 *
 *  SBIT.h should only be included by SBIT.cpp
 *  Other classes should include SBITIF.h
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 */

#ifndef SBIT_H_
#define SBIT_H_

#include "component/ComponentRegistry.h"
#include "component/SyncComponent.h"

class UniversalDataReader;
class UniversalDataWriter;

/**
 *  The SBIT built-in-test component provides monitoring of
 *  vehicle health at startup. If it sees a fault, it will write to
 *  a fault flag.
 *
 *  SBIT.h should only be included by SBIT.cpp
 *  Other classes should include SBITIF.h
 *
 *  \ingroup modules_builtintest
 */
class SBIT : public SyncTestComponent
{
public:

    SBIT( const Module* module );

    virtual ~SBIT();

    /// Initialize function
    void initialize( void );

    /// The actual "payload" of the component
    void run();

    /// Uninit function
    void uninitialize( void );

    /// Should return [myNamespace]::SIMULATE_HARDWARE, or [myNamespace]::POWER, etc
    virtual ConfigURI getConfigURI( ConfigOption configOption ) const;

protected:



private:

    // Update configuration
    bool readConfig( void );

    // Checks actual vs. expected positions of the control surfaces
    bool ctrlSurfaceCheckPos( double elevExpect, double ruddExpect, double massExpect );

    // indicates whether things are ok to run
    bool ok_;

    bool sbitPass_;

    // slate entry to let others know that SBIT is running
    DataWriter* sbitRunningStateWriter_;

    DataWriter* elevatorAngleCmdWriter_;
    DataWriter* verticalModeWriter_;
    DataWriter* horizontalModeWriter_;
    DataWriter* rudderAngleCmdWriter_;
    DataWriter* massPosCmdWriter_;

    UniversalDataReader *elevatorAngleReader_;
    UniversalDataReader *rudderAngleReader_;
    UniversalDataReader *massPosReader_;

    ConfigReader* kernelReleaseCfgReader_;
    ConfigReader* kernelVersionCfgReader_;
    ConfigReader* elevatorLimitCfgReader_;
    ConfigReader* massLimitFwdCfgReader_;
    ConfigReader* massLimitAftCfgReader_;
    ConfigReader* massDefaultCfgReader_;
    ConfigReader* rudderLimitCfgReader_;

    ConfigReader* elevDeviationCfgReader_;
    ConfigReader* massDeviationCfgReader_;
    ConfigReader* ruddDeviationCfgReader_;


    ///*------------------- vehicle parameters --------------------------*/

    double elevatorLimit_;
    double massLimitFwd_;
    double massLimitAft_;
    double massDefault_;
    double rudderLimit_;
    double elevDeviation_;
    double massDeviation_;
    double ruddDeviation_;

    ///////////////////////////////////

    Timespan ctrlTimeout_;
    Timespan startDelay_;
    Timestamp startTime_;

    // Used to save state since there are long potential waiting periods for this device to respond
    enum SBITState
    {
        PRESTART,
        START,
        CTRLHI,
        SETLO,
        CTRLLO,
        SETCTR,
        CTRLCTR,
        DONE,
        IDLE
    };

    // To hold our state
    SBITState sbitState_;

};

#endif /*SBIT_H_*/
