//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   CSubsystemConfig.cpp
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      
//

#include "StdAfx.h"
#include "SubsystemConfig.h"

//////////////////////////////////////////////////////////////////////
// CSubsystemConfig class implementation.

CSubsystemConfig::CSubsystemConfig( void )
                 :tagSUBSYSTEMCONFIG()
{
    m_fpFile = NULL;
}

CSubsystemConfig::~CSubsystemConfig( void )
{
    Close();

    m_fpFile = NULL;
}

bool CSubsystemConfig::Open(    const int       &riSubsystemId,
                                const EACCESS   &reAccess )
{
    bool bSuccess = false;

    CString FileName;

    FileName.Format( "%sEdgeTechSubsystem%d.cfg",   static_cast<LPCTSTR>( CSubsystemConfig::GetAppPath()),
                                                    riSubsystemId );

    LPCTSTR pszFileName = static_cast<LPCTSTR>( FileName );

    // Ensure any existing file is closed, first.

    Close();

    // Open the newly specified file with the appropriate access rights.

    switch ( reAccess )
    {
        case accessWrite:

            bSuccess = (( m_fpFile = ::fopen( pszFileName, "wb" ) ) != NULL );
            break;

        case accessRead:
            
            // Open the file for read access. If it fails, it probably doesn't exist so try to create
            // it then re-open with read access rights.

            bSuccess = (( m_fpFile = ::fopen( pszFileName, "rb" ) ) != NULL );

            break;
    }

    return bSuccess;
}

bool CSubsystemConfig::Close( void )
{
    if ( m_fpFile != NULL )
    {
        fclose( m_fpFile );
        m_fpFile = NULL;
    }

    return ( m_fpFile == NULL );
}

bool CSubsystemConfig::IsOpen( void ) const
{
    return ( m_fpFile != NULL );
}

bool CSubsystemConfig::Write( void )
{
    if ( IsOpen() )
    {
        return ( ::fwrite( this, tagSUBSYSTEMCONFIG::Size(), 1, m_fpFile ) == 1 );
    }

    return false;
}

bool CSubsystemConfig::Read( void )
{
    bool bSuccess = false;

    if ( IsOpen() )
    {
        bSuccess = ( ::fread( this, tagSUBSYSTEMCONFIG::Size(), 1, m_fpFile ) == 1 );
    }

    if ( ! bSuccess )
    {
        tagSUBSYSTEMCONFIG::Reset();
    }

    return bSuccess;
}

CSubsystemConfig::operator SUBSYSTEMCONFIG * ( void )
{
    return static_cast<::SUBSYSTEMCONFIG *>( this );
}

CString CSubsystemConfig::GetAppPath( void )
{
    CString Path;

    LPSTR pszAppPath = Path.GetBuffer( _MAX_PATH );

    ::GetModuleFileName( NULL, pszAppPath, _MAX_PATH );

    int iLength;

    if ( ( iLength = strlen( pszAppPath ) ) > 0 )
    {
        while ( --iLength )
        {
            if ( ( pszAppPath[ iLength ] == '\\' ) ||
                 ( pszAppPath[ iLength ] == ':' )   )
            {
                pszAppPath[ iLength + 1 ] = '\0';
                break;
            }
        }
    }

    Path.ReleaseBuffer();

    return Path;
}

///////////////////////////////////////////////////////////////////////////////
// tagSUBSYSTEMCONFIG helpers.

tagSUBSYSTEMCONFIG::tagSUBSYSTEMCONFIG( void )
{
    Reset();
}

tagSUBSYSTEMCONFIG::~tagSUBSYSTEMCONFIG( void )
{
}

void tagSUBSYSTEMCONFIG::Reset( void )
{
    ::memset( this, 0x00, Size() );
}

size_t tagSUBSYSTEMCONFIG::Size( void )
{
    return sizeof( struct tagSUBSYSTEMCONFIG );
}

void tagSUBSYSTEMCONFIG::SetDefault( void )
{
    Reset();

    m_bAGCEnable               = true;
    m_bPingEnable              = false;
    m_bUseExternalTrigger      = false;

    m_iFrameDecimationFactor   = 1;
    m_iSampleDecimationFactor  = 1;

    m_eTriggerMode             = triggerInternal;

    m_fRange                   = 25.0f;
    m_fPingPeriod              = 10.0f;                    // 10 ms.
    m_iRxGain                  = 2000;                     // 2 (x 1000).

    m_aiChannelTxGain[ 0 ]     = 1000;
    m_aiChannelTxGain[ 1 ]     = 1000;
}
