/******************************************************************************
 *                                                                         
 *               Copyright 2004-2006 Agilent Technologies
 *               All rights reserved.
 *
 *****************************************************************************/
 
#include "stdafx.h"
#include "CoAgilent34980AChannel.h"

/// <summary>
///	If True, the specific driver can use the channel for internal path creation.
///  The only operations that may be performed on configuration channels are reading
/// and writing the IsConfigurationChannel property.
/// </summary>
/// <remarks>
///	
/// </remarks>
HRESULT Agilent34980AChannel::IIviSwtchChannel_get_IsConfigurationChannel(VARIANT_BOOL* Val)
{
	HRESULT hr = S_OK;

	*Val = VARIANT_FALSE;
	CString channel( GetInstanceName() );
	SwitchUtils::Channel ch( TranslatePhysicalName( channel ), channel );
	// remove
	if( GetRoot()->m_listConstConfigChannels.Find( ch ) || GetRoot()->m_listConfigChannels.Find( ch ) ) 
		return *Val = VARIANT_TRUE, hr;

	return hr;
}

/// <summary>
///	If True, the specific driver can use the channel for internal path creation.
///  The only operations that may be performed on configuration channels are reading
/// and writing the IsConfigurationChannel property.
/// </summary>
/// <remarks>
///	
/// </remarks>
HRESULT Agilent34980AChannel::IIviSwtchChannel_put_IsConfigurationChannel(VARIANT_BOOL Val)
{
	HRESULT hr = S_OK;

	CString channel( GetInstanceName() );
	SwitchUtils::Channel ch( TranslatePhysicalName( channel ), channel );

	if( ! ch.bValid )
		return ReportInvalidValueError( _T("IAgilent34921AChannel_put_IsConfigurationChannel"), _T("channel"), channel );

	if( (Val == VARIANT_TRUE) && GetRoot()->m_listSourceChannels.Find( ch ) )
	{ // if this channel is source, it can't be config too
		return ReportInvalidValueError( _T("IAgilent34921AChannel_put_IsConfigurationChannel"), _T("channel"), channel );
	}

	POSITION pos = GetRoot()->m_listConfigChannels.Find( ch );
	POSITION posConst = GetRoot()->m_listConstConfigChannels.Find( ch );
	if( Val==VARIANT_TRUE )
	{
		if( pos || posConst )
			return hr; // user wants to add; it is already there
		GetRoot()->m_listConfigChannels.AddTail( ch );
	}
	else
	{
		if( posConst ) 
			return hr; // attempt to remove const config channel (relay), unavailable to user
		if( pos ) GetRoot()->m_listConfigChannels.RemoveAt( pos );
	}
	return hr;
}

/// <summary>
///	If True, the channel is a source channel.  Two channels that are either sources
/// or have their own connections to sources, may not be connected.  This prevents
/// connections that might cause damage to channels, devices, or system.
/// </summary>
/// <remarks>
///	
/// </remarks>
HRESULT Agilent34980AChannel::IIviSwtchChannel_get_IsSourceChannel(VARIANT_BOOL* Val)
{
	HRESULT hr = S_OK;

	*Val = VARIANT_FALSE;
	CString channel( GetInstanceName() );
	SwitchUtils::Channel ch( TranslatePhysicalName( channel ), channel );

	for( POSITION pos = GetRoot()->m_listSourceChannels.GetHeadPosition(); pos; )
	{
		if( GetRoot()->m_listSourceChannels.GetNext( pos ) == ch )
			return *Val = VARIANT_TRUE, hr;
	}

	return hr;
}

/// <summary>
///	If True, the channel is a source channel.  Two channels that are either sources
/// or have their own connections to sources, may not be connected.  This prevents
/// connections that might cause damage to channels, devices, or system.
/// </summary>
/// <remarks>
///	
/// </remarks>
HRESULT Agilent34980AChannel::IIviSwtchChannel_put_IsSourceChannel(VARIANT_BOOL Val)
{
	HRESULT hr = S_OK;

	CString channel( GetInstanceName() );
	SwitchUtils::Channel ch( TranslatePhysicalName( channel ), channel );

	if( ! ch.bValid )
		return ReportInvalidValueError( _T("IAgilent34921AChannel_put_IsSourceChannel"), _T("channel"), channel );
	// remove
	if( (Val==VARIANT_TRUE) && (GetRoot()->m_listConfigChannels.Find( ch ) || GetRoot()->m_listConstConfigChannels.Find( ch )) )
	{ // search Config Channels; if this channel is config channel, it can't be source too
		return ReportInvalidValueError( _T("IAgilent34921AChannel_put_IsSourceChannel"), _T("channel"), channel );
	}
	
	if( Val==VARIANT_TRUE )
	{
		if( GetRoot()->m_listSourceChannels.Find( ch ) )
			return hr; // it is already there
		GetRoot()->m_listSourceChannels.AddTail( ch );
	}
	else
	{
		POSITION pos = GetRoot()->m_listSourceChannels.Find( ch );
		if( pos ) GetRoot()->m_listSourceChannels.RemoveAt( pos );
	}
	return hr;
}

/// <summary>
///	Pointer to the class-compliant IIviSwtchCharacteristics interface
/// </summary>
/// <remarks>
///	
/// </remarks>
HRESULT Agilent34980AChannel::IIviSwtchChannel_get_Characteristics(IIviSwtchCharacteristics** Val)
{
	HRESULT hr = S_OK;

	hr = InternalQueryInterface(this, _GetEntries(), __uuidof(*Val), reinterpret_cast<void**>(Val));

	return hr;
}


