/*****************************************************************************
* Copyright (c) 1990-2000 Digi International Inc., All Rights Reserved
* 
* This software contains proprietary and confidential information of Digi
* International Inc.  By accepting transfer of this copy, Recipient agrees
* to retain this software in confidence, to prevent disclosure to others,
* and to make no use of this software other than that for which it was
* delivered.  This is an unpublished copyrighted work of Digi International
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
* prohibited.
* 
* Restricted Rights Legend
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7031 or
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -
* Restricted Rights at 48 CFR 52.227-19, as applicable.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
*
*****************************************************************************/


// FRConfig.cpp : implementation file
//

#include "stdafx.h"
#include "defs.h"
#include "FRConfig.h"
#include "frreg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFRGeneral property page

IMPLEMENT_DYNCREATE(CFRGeneral, CPropertyPage)

CFRGeneral::CFRGeneral(FRAMERELAY * const pFR) : CPropertyPage(CFRGeneral::IDD)
{
   ASSERT(NULL != pFR);
   m_pFR = pFR;
	//{{AFX_DATA_INIT(CFRGeneral)
	m_BECNFC = 0;
	m_MaxFrameSize = 2000;
	m_InjectionText = _T("");
	//}}AFX_DATA_INIT
}

CFRGeneral::~CFRGeneral()
{
}

void CFRGeneral::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFRGeneral)
	DDX_Control(pDX, IDC_Injection, m_Injection);
	DDX_Check(pDX, IDC_BECNFlowControl, m_BECNFC);
	DDV_MinMaxDWord(pDX, m_MaxFrameSize, 5, 8192);
	DDX_Text(pDX, IDC_InjectionText, m_InjectionText);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFRGeneral, CPropertyPage)
	//{{AFX_MSG_MAP(CFRGeneral)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFRGeneral message handlers

BOOL CFRGeneral::OnInitDialog() 
{
	m_BECNFC = (m_pFR->Flags & FRS_LINK_FLAGS_BECN_XON_XOFF) > 0;
	m_MaxFrameSize = (m_pFR->max_frsize);

	CPropertyPage::OnInitDialog();
	
	m_Injection.SetRange(0, 2, FALSE);
	if (m_pFR->Flags & FRS_LINK_FLAGS_FORCED_INJECTION)
		m_Injection.SetPos(2);
	else if (m_pFR->Flags & FRS_LINK_FLAGS_BUFFERED_INJECTION)
		m_Injection.SetPos(1);
	else if (m_pFR->Flags & FRS_LINK_FLAGS_STANDARD_INJECTION)
		m_Injection.SetPos(0);

	OnHScroll(0, 0, NULL);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CFRGeneral::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	CString insert;

	if (m_Injection.GetPos() == 0)
		insert.LoadString(IDS_INJECTIONSTANDARD);
	else if (m_Injection.GetPos() == 1)
		insert.LoadString(IDS_INJECTIONBUFFERED);
	else if (m_Injection.GetPos() == 2)
		insert.LoadString(IDS_INJECTIONFORCED);

	m_InjectionText.Format(IDS_INJECTIONMSG, insert);
	UpdateData(FALSE);
}

void CFRGeneral::OnOK() 
{
	DWORD Flags = FRS_LINKS_FLAGS_ALWAYS_SET;

	if (m_Injection.GetPos() == 0)
		Flags |= FRS_LINK_FLAGS_STANDARD_INJECTION;
	else if (m_Injection.GetPos() == 1)
		Flags |= FRS_LINK_FLAGS_BUFFERED_INJECTION;
	else if (m_Injection.GetPos() == 2)
		Flags |= FRS_LINK_FLAGS_FORCED_INJECTION;

	if (m_BECNFC)
		Flags |= FRS_LINK_FLAGS_BECN_XON_XOFF;

	DWORD LMIFlags = m_pFR->Flags & LMIFLAGSMASK;
	
	m_pFR->Flags = Flags | LMIFlags;
	m_pFR->max_frsize = m_MaxFrameSize;
	
	CPropertyPage::OnOK();
}

/////////////////////////////////////////////////////////////////////////////
// CFRLMI property page

IMPLEMENT_DYNCREATE(CFRLMI, CPropertyPage)

CFRLMI::CFRLMI(FRAMERELAY * const pFR) : CPropertyPage(CFRLMI::IDD)
{
   ASSERT(NULL != pFR);
   m_pFR = pFR;
	//{{AFX_DATA_INIT(CFRLMI)
	m_BandwidthControl = FALSE;
	m_LMIDLCI = 1023;
	m_LMIFC = FALSE;
	m_LMIRevision = 0;
	m_N1 = 1;
	m_N2 = 2;
	m_N3 = 4;
	m_T1 = 10;
	//}}AFX_DATA_INIT
}

CFRLMI::~CFRLMI()
{
}

void CFRLMI::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFRLMI)
	DDX_Control(pDX, IDC_Revision, m_LMIRevisionBox);
	DDX_Check(pDX, IDC_LMIBandwidthControl, m_BandwidthControl);
	DDX_Check(pDX, IDC_LMIFlowControl, m_LMIFC);
	DDX_CBIndex(pDX, IDC_Revision, m_LMIRevision);
	//}}AFX_DATA_MAP
	if (m_LMIRevision > 0) {
		DDX_Text(pDX, IDC_LMIDLCI, m_LMIDLCI);
		DDV_MinMaxDWord(pDX, m_LMIDLCI, 0, 1023);
		DDX_Text(pDX, IDC_N2, m_N2);
		DDV_MinMaxDWord(pDX, m_N2, 1, 10);
		DDX_Text(pDX, IDC_N3, m_N3);
		DDV_MinMaxDWord(pDX, m_N3, 1, 10);

		DDX_Text(pDX, IDC_N1, m_N1);
		DDV_MinMaxDWord(pDX, m_N1, 0, 255);
		DDX_Text(pDX, IDC_T1, m_T1);
		DDV_MinMaxDWord(pDX, m_T1, 0, 30);
		}
	else {
		DDX_Text(pDX, IDC_LMIDLCI, m_LMIDLCI);
		DDX_Text(pDX, IDC_N2, m_N2);
		DDX_Text(pDX, IDC_N3, m_N3);
		DDX_Text(pDX, IDC_N1, m_N1);
 		DDX_Text(pDX, IDC_T1, m_T1);
		}
	if (pDX->m_bSaveAndValidate && m_LMIRevision > 0 && m_N2 > m_N3) {
		AfxMessageBox(IDS_THRESHHOLDERROR);
		pDX->PrepareEditCtrl(IDC_N2);
		pDX->Fail();
		}

}



BEGIN_MESSAGE_MAP(CFRLMI, CPropertyPage)
	//{{AFX_MSG_MAP(CFRLMI)
	ON_CBN_SELCHANGE(IDC_Revision, OnSelchangeRevision)
	ON_BN_CLICKED(IDC_Advanced, OnAdvanced)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFRLMI message handlers

void CFRLMI::OnSelchangeRevision() 
{
	// TODO: Add your control notification handler code here
	m_LMIRevision = m_LMIRevisionBox.GetCurSel();
	CWnd *ctrl = GetDlgItem(IDC_Revision)->GetWindow(GW_HWNDNEXT);
	BOOL stratacom = m_LMIRevision == 2;
	for (short i = 0; i < 14; i++) {
		if (i < 11 || i > 13)
			ctrl->EnableWindow(m_LMIRevision > 0);
		else
			ctrl->EnableWindow(stratacom);
		ctrl = ctrl->GetWindow(GW_HWNDNEXT);
		}

	if (stratacom ^ m_BandwidthControl || stratacom ^ m_LMIFC) {
		m_BandwidthControl = stratacom;
		m_LMIFC = stratacom;
		}

	m_LMIDLCI = m_LMIRevision > 2 ? 0 : 1023;
	UpdateData(FALSE);
	GetDlgItem(IDC_Advanced)->EnableWindow(m_LMIRevision > 0);
}

void CFRLMI::OnAdvanced() 
{
	CString szName;
	GetWindowText(szName);
	CPropertySheet sheet(szName);
	// Get current value of T1 before calling the advanced dialog
	// LMIAdv uses this value to do some validations.
	m_pFR->mT1 = GetDlgItemInt(IDC_T1);  
	CFRLMIAdv m_lmiadv(m_pFR);
	sheet.AddPage(&m_lmiadv);
	sheet.DoModal();
}

BOOL CFRLMI::OnInitDialog() 
{
	DWORD Flags = m_pFR->Flags;

	m_LMIRevision = 0;
	if (Flags & FRS_LINK_FLAGS_LMI_REV1)
		m_LMIRevision = 1;
	else if (Flags & FRS_LINK_FLAGS_GANG_OF_4)
		m_LMIRevision = 2;
	else if (Flags & FRS_LINK_FLAGS_ANNEX_D)
		m_LMIRevision = 3;
	else if (Flags & FRS_LINK_FLAGS_ANNEX_A)
		m_LMIRevision = 4;
		
	m_N1 = m_pFR->mN1;
	m_N2 = m_pFR->mN2;
	m_N3 = m_pFR->mN3;
	m_T1 = m_pFR->mT1;

	CPropertyPage::OnInitDialog();
	OnSelchangeRevision();
	m_LMIDLCI = m_pFR->lmi_dlci;
	m_LMIFC = (Flags & FRS_LINK_FLAGS_LMI_XON_XOFF) > 0;
	m_BandwidthControl = (Flags & FRS_LINK_FLAGS_LMI_BANDWIDTH_CONTROL) > 0;
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CFRLMI::OnOK() 
{
	DWORD FlagsLMI = m_pFR->Flags & LMIFLAGSMASK;

	FlagsLMI = FlagsLMI & FRS_LINK_FLAGS_DCE;

	if (m_LMIRevision == 1)
		FlagsLMI |= FRS_LINK_FLAGS_LMI_REV1;
	else if (m_LMIRevision == 2) {
		FlagsLMI |= FRS_LINK_FLAGS_GANG_OF_4;
		
		if (m_LMIFC)
			FlagsLMI |= FRS_LINK_FLAGS_LMI_XON_XOFF;
		if (m_BandwidthControl)
			FlagsLMI |= FRS_LINK_FLAGS_LMI_BANDWIDTH_CONTROL;
		}
	else if (m_LMIRevision == 3)
		FlagsLMI |= FRS_LINK_FLAGS_ANNEX_D;
	else if (m_LMIRevision == 4)
		FlagsLMI |= FRS_LINK_FLAGS_ANNEX_A;
	else
		FlagsLMI = 0;

	DWORD Flags = m_pFR->Flags & ~LMIFLAGSMASK;

	m_pFR->Flags = Flags | FlagsLMI;
	m_pFR->lmi_dlci = m_LMIDLCI;
	m_pFR->mN1 = m_N1;
	m_pFR->mN2 = m_N2;
	m_pFR->mN3 = m_N3;
	m_pFR->mT1 = m_T1;
	
	CPropertyPage::OnOK();
}


/////////////////////////////////////////////////////////////////////////////
// CFRLMIAdv dialog

IMPLEMENT_DYNCREATE(CFRLMIAdv,CPropertyPage)

CFRLMIAdv::CFRLMIAdv(FRAMERELAY * const pFR) : CPropertyPage(CFRLMIAdv::IDD)
{
   ASSERT (NULL != pFR);
   m_pFR = pFR;
	//{{AFX_DATA_INIT(CFRLMIAdv)
	m_DCEOn = FALSE;
	m_T2 = 0;
	m_T3 = 0;
	m_N4 = 0;
	//}}AFX_DATA_INIT

	DWORD Flags = m_pFR->Flags;
	m_DCEOn = (Flags & FRS_LINK_FLAGS_DCE) > 0;

	m_N4 = m_pFR->mN4;
	m_T2 = m_pFR->mT2;
	m_T3 = m_pFR->mT3;

}

CFRLMIAdv::~CFRLMIAdv()
{
}

void CFRLMIAdv::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFRLMIAdv)
	DDX_Control(pDX, IDC_DCE, m_DCEButton);
	DDX_Check(pDX, IDC_DCE, m_DCEOn);
	DDX_Text(pDX, IDC_T2, m_T2);
	DDX_Text(pDX, IDC_T3, m_T3);
	DDX_Text(pDX, IDC_N4, m_N4);
	//}}AFX_DATA_MAP
	DWORD T1 = m_pFR->mT1;

	if (pDX->m_bSaveAndValidate && m_DCEOn && m_T2 <= T1) {
		CString msg;

		msg.Format(IDS_T2LessThanT1, T1);
		AfxMessageBox(msg);
		pDX->PrepareEditCtrl(IDC_T2);
		pDX->Fail();
		}

}


BEGIN_MESSAGE_MAP(CFRLMIAdv, CPropertyPage)
	//{{AFX_MSG_MAP(CFRLMIAdv)
	ON_BN_CLICKED(IDC_DCE, OnDce)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFRLMIAdv message handlers

void CFRLMIAdv::OnDce() 
{
	// TODO: Add your control notification handler code here
	CWnd *ctrl = m_DCEButton.GetWindow(GW_HWNDNEXT);

	BOOL dce = m_DCEButton.GetCheck();
	while	(ctrl) {
		ctrl->EnableWindow(dce);
		ctrl = ctrl->GetWindow(GW_HWNDNEXT);
		}
}

BOOL CFRLMIAdv::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	OnDce();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CFRLMIAdv::OnOK() 
{
	DWORD Flags = m_pFR->Flags;

	if (m_DCEOn)
		Flags |= FRS_LINK_FLAGS_DCE;
	else
		Flags &= ~FRS_LINK_FLAGS_DCE;

	m_pFR->Flags = Flags;
	m_pFR->mN4 = m_N4;
	m_pFR->mT2 = m_T2;
	m_pFR->mT3 = m_T3;
	
	CPropertyPage::OnOK();
}


