//
//  Copyright © 2002, RESON Inc. All Rights Reserved.
//
//  No part of this file may be reproduced or transmitted in any form or by
//  any means, electronic or mechanical, including photocopy, recording, or
//  information storage or retrieval system, without permission in writing
//  from RESON Inc.
//
//  Filename:   6046Options.cpp
//
//  Project:    6046
//
//  Author(s):  W. Arcus.
//
//  Purpose:    
//              
//
//  Notes:      
//

#include "StdAfx.h"
#include "6046Options.h"
#include "RESON6046Hasp.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

///////////////////////////////////////////////////////////////////////////////
// Internal module defintions and declarations etc.

namespace                   // Begin unnamed namespace.
{
    const WORD          wPLCEnableBit_c = 0x0001;

    template <class tWord>
    inline void SetOrClearBit( tWord &rtWord, const tWord &rtBitMask, const bool &rbSet )
    {
        if ( rbSet )
        {
            rtWord |= rtBitMask;
        }
        else
        {
            rtWord ^= ( rtWord & rtBitMask );
        }
    }
}                           // End unnamed namespace.

//////////////////////////////////////////////////////////////////////
// C6046HaspOptions class implementation.

using std::auto_ptr;

C6046HaspOptions::C6046HaspOptions( void )
{
    bool bSuccess = false;

    try
    {
        m_Hasp = auto_ptr<CRESON6046Hasp>( new CRESON6046Hasp );
        bSuccess = ( m_Hasp.get() != NULL );
    }
    catch ( CHaspException e )
    {
        bSuccess = false;
        TRACE( _T( "C6046HaspOptions::C6046HaspOptions(), Hasp exception code: %d\n" ), e.GetStatus() );
    }
    catch ( ... )
    {
        bSuccess = false;
        TRACE( _T( "C6046HaspOptions::C6046HaspOptions(), unspecified exception caught\n" ) );
    }

    if ( ! bSuccess )
    {
        DeleteHasp();
    }
}

C6046HaspOptions::~C6046HaspOptions( void )
{
    DeleteHasp();
}

bool C6046HaspOptions::IsKeyPresent( void ) const
{
    bool bSuccess = false;

    try
    {
        bSuccess = ( m_Hasp->GetResonHaspType() == CRESON6046Hasp::resonHaspType );
    }
    catch ( CHaspException e )
    {
        bSuccess = false;
        TRACE( _T( "C6046HaspOptions::IsKeyPresent(), Hasp exception code: %d\n" ), e.GetStatus() );
    }
    catch ( ... )
    {
        bSuccess = false;
        TRACE( _T( "C6046HaspOptions::IsKeyPresent(), unspecified exception caught\n" ) );
    }

    return bSuccess;
}

bool C6046HaspOptions::IsPayloadControllerEnabled( void ) const
{
    bool bEnabled = false;

    try
    {
        bEnabled = ( ( m_Hasp->SecureCheck( CRESON6046Hasp::haspLayoutPLCOptions ) & wPLCEnableBit_c ) != 0 );
    }
    catch ( CHaspException e )
    {
        bEnabled = false;
        TRACE( _T( "N6046ReadOptions::IsPayloadControllerEnabled(), Hasp exception code: %d\n" ), e.GetStatus() );
    }
    catch ( ... )
    {
        bEnabled = false;
        TRACE( _T( "N6046ReadOptions::IsPayloadControllerEnabled(), unspecified exception caught\n" ) );
    }

    return bEnabled;
}

///////////////////////////////////////////////////////////////////////////////
// Hasp writing methods... only to be used for RESON internal products.

#ifdef _RESON_INTERNAL_

void C6046HaspOptions::SetPayloadControllerEnable( const bool &rbEnable )
{
    try
    {
        WORD wOptionsWord = static_cast<WORD>( m_Hasp->ReadWord( CRESON6046Hasp::haspLayoutPLCOptions ) );

        SetOrClearBit( wOptionsWord, wPLCEnableBit_c, rbEnable ); 

        m_Hasp->WriteWord( CRESON6046Hasp::haspLayoutPLCOptions, wOptionsWord );
    }
    catch ( CHaspException e )
    {
        TRACE( _T( "C6046HaspOptions::SetPayloadControllerEnable(), Hasp exception code: %d\n" ), e.GetStatus() );
    }
    catch ( ... )
    {
        TRACE( _T( "C6046HaspOptions::SetPayloadControllerEnable(), unspecified exception caught\n" ) );
    }
}

#endif

///////////////////////////////////////////////////////////////////////////////
// Private members.

inline
void C6046HaspOptions::DeleteHasp( void )
{
    try
    {
        if ( m_Hasp.get() != NULL )
        {
            delete ( m_Hasp.release() );
        }
    }
    catch ( ... )
    {
        TRACE( _T( "C6046HaspOptions::SetPayloadControllerEnable(), unspecified exception caught\n" ) );
    }

    m_Hasp = auto_ptr<CRESON6046Hasp>( NULL );
}

