/*****************************************************************************
* 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
*
*****************************************************************************/
// ServicePPP.cpp: implementation of the CServicePPP class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ServicePPP.h"
#include "ObjectFactory.h"
#include "resource.h"
#include "Line.h"
#include "RegHelpers.h"
#include "pppreg.h"
#include "PPPConfig.h"

#ifdef NT5
#include "DriverServices.h"
#endif

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CServicePPP, CService)
CServicePPP::CServicePPP(HKEY const hKeyRoot, CString const strRegPath, CConfigObject const * const pParent) : CService(hKeyRoot, strRegPath, pParent)
{
   try
   {
	   m_pwpNRZPage = NULL;
       // Get parameters
       AffirmThrow(GetParameters());

       // Solve our identity crisis
       SetIdentity(DXB_REGVAL_LINE_SERVICE_PPP);
	   // Set our Display Name
	   CString szDisplayName;
	   AffirmThrow(szDisplayName.LoadString(IDS_DisplayName_Service));
	   SetDisplayName(szDisplayName);

      // For the CSU, NRZ settings are not applicable
      // NRZ is currently the only property so we inhibit at the top level
      int Abilities = m_fIsCsuLine ? CConfigObject::NOABILITIES : CConfigObject::DOPROPERTIES;
      SetAbility(Abilities);

       // Get our static config data
       AffirmThrow(GetConfigData());

   }
   catch (CAffirmException ae)
   {
   }
}

CServicePPP::~CServicePPP()
{
	delete m_pwpNRZPage;
}
//////////////////////////////////////////////////////////////////////
// Get/Set parameters
//////////////////////////////////////////////////////////////////////

// we really don't do much with our parameters, so we don't need to
// read them. The parameters are simply written for the PPP driver to
// read and utilize.
BOOL CServicePPP::GetParameters(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(CService::GetParameters());
//	  AffirmThrow(RegReadStrValue(m_hKey,DXB_REGVAL_SERVICE_INSTANCE,m_strServiceInstance));
	  AffirmThrow(RegReadIntValue(m_hKey,PPP_REGVAL_NRZ,&m_nNRZ,PPP_REGVAL_NRZ_DEFVAL));
	  AffirmThrow(RegReadIntValue(m_hKey,PPP_REGVAL_IS_CSU,&m_fIsCsuLine,FALSE));
     fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

// write some params that the SNA driver will read
// SNID is written by the base class
// OEMSETUP.INF will write Adapter and ServiceName for NT4.0
BOOL CServicePPP::SetParameters(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(CService::SetParameters());
	  CLine* pLine = (CLine*)(GetParent()->GetParent());
	  AffirmThrow(pLine != NULL);
	  AffirmThrow(RegWriteIntValue(m_hKey,PPP_REGVAL_LINESPEED,pLine->GetLineSpeed()));
	  AffirmThrow(RegWriteIntValue(m_hKey,PPP_REGVAL_CLOCKING,pLine->GetLineClocking()));
      AffirmThrow(RegWriteIntValue(m_hKey,PPP_REGVAL_NRZ,m_nNRZ));

      // For CSU lines, the number of endpoints and fractional T1 info are needed by the PPP client driver
      if ( m_fIsCsuLine )
      {
         AffirmThrow(RegWriteIntValue(m_hKey,PPP_REGVAL_ENDPOINTS,pLine->GetConfiguredEndPoints()));
         AffirmThrow(RegWriteIntValue(m_hKey,DXB_REGVAL_CSU_FRACT1_TS_MASK,pLine->GetFracT1Mask()));
         AffirmThrow(RegWriteIntValue(m_hKey,DXB_REGVAL_CSU_FRACT1_SPD_MASK,pLine->GetFracT1SpeedMask()));
      } else {
         AffirmThrow(RegWriteIntValue(m_hKey,PPP_REGVAL_ENDPOINTS,PPP_REGVAL_ENDPOINTS_DEFVAL));
      }

	  CString szLineValue;

	  // Format the Line name by appending the SNID, if the Line Name
	  // require a SNID
	  szLineValue.Format(m_strPPPLineName,m_dwSnid-1);
      AffirmThrow(RegWriteStrValue(m_hKey, DXB_REGVAL_LINENAME, szLineValue));

#ifdef NT5

	  CString szDescription;
	  szDescription.Format("PPP - Line %d",m_intLineNumber+1); // 1 based for the user...

	  AffirmThrow(RegWriteStrValue(m_hKey, DXB_REGVAL_DESCRIPTION, szDescription));	  
     if (m_fIsCsuLine)
        AffirmThrow(RegWriteStrValue(m_hKey, DXB_REGVAL_PDO_HDWID, DXB_HDWID_PPP_CSU));
     else
        AffirmThrow(RegWriteStrValue(m_hKey, DXB_REGVAL_PDO_HDWID, DXB_HDWID_PPP));

	  AffirmThrow(RegWriteDWordValue(m_hKey, DXB_REGVAL_PDO_DEVTYPE, DXB_DEVICE_NETCARD));
#endif

      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

// remove the registry parameters
BOOL CServicePPP::RemoveParameters(BOOL fRemoveKey)
{
   BOOL fSuccess = FALSE;

   try
   {
#ifdef NT5
	  CString strInstance;
	  // this value would have been written by the coinstaller for the client
	  // driver, when the driver got installed.
	  RegReadStrValue(m_hKey, DXB_REGVAL_REGINSTANCE, strInstance);

	  if (!strInstance.IsEmpty())
	  {
		DeleteService(strInstance);
		strInstance.Empty();
		RegWriteStrValue(m_hKey, DXB_REGVAL_REGINSTANCE, strInstance);
	  }

	  AffirmThrow(RegDelValue(m_hKey, DXB_REGVAL_PDO_HDWID));
	  AffirmThrow(RegDelValue(m_hKey, DXB_REGVAL_PDO_DEVTYPE));
#endif

      AffirmThrow(CService::RemoveParameters(fRemoveKey));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

#if defined CONFIGURE_CIRCUITS
BOOL CServicePPP::BuildDisplayTree(CTreeNode * const pNode)
{
   BOOL fSuccess = FALSE;

   try
   {
      TRACE("CServicePPP(%s)::BuildDisplayTree()\n", GetIdentity());

      // If this is on a CSU line, don't show a node, the line will...
      if (!m_fIsCsuLine)
         fSuccess = CService::BuildDisplayTree(pNode); // Call the base class default
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}
#endif // #if defined CONFIGURE_CIRCUITS

BOOL CServicePPP::DoProperties(void)
{
	BOOL fSuccess = FALSE;

	try
	{
		CPPPConfig dlg;
		dlg.m_nNRZ = m_nNRZ;
		if (dlg.DoModal() == IDOK)
			m_nNRZ = dlg.m_nNRZ;
		fSuccess = TRUE;
	}
	catch (CAffirmException ae)
	{
	}

	return fSuccess;
}

#define CHOICE_NRZ 0
#define CHOICE_NRZI 1
BOOL CServicePPP::AddWizardPages(CConfigWizSheet* const pSheet)
{
	BOOL bSuccess = FALSE;
	try
	{
		AffirmThrow(pSheet != NULL);
      // For the CSU, NRZ settings are not applicable
      if ( !m_fIsCsuLine )
      {
		   // add the LAPB page
		   if (!m_pwpNRZPage)
		   {
			   CString strTitle,strInstructions;
			   strTitle.LoadString(IDS_WIZNRZTITLE);
			   strInstructions.LoadString(IDS_WIZNRZINS);
			   m_pwpNRZPage = new CWizPageRadioButtons(this,WP_NRZ,strTitle,strInstructions);
			   AffirmThrow(m_pwpNRZPage != NULL);
			   m_pwpNRZPage->SetHelpID(IDH_WIZ_NRZPAGE);
		   }	
		   CStringArray astrValues;
		   CString strTemp((LPCTSTR)IDS_NRZ);
		   astrValues.SetAtGrow(CHOICE_NRZ,strTemp);

		   strTemp.LoadString(IDS_NRZI);
		   astrValues.SetAtGrow(CHOICE_NRZI,strTemp);
		   m_pwpNRZPage->SetValues(astrValues,m_nNRZ);
		   pSheet->InsertPage(m_pwpNRZPage);
      }
		
		bSuccess = TRUE;

	}
	catch(CAffirmException ae)
	{
	}
	return bSuccess;
}

BOOL CServicePPP::NextWizardPage(int const State)
{
	BOOL bSuccess = FALSE;
	try
	{
		switch(State)
		{
		case WP_NRZ:
			// extract nrz choice
			m_nNRZ = m_pwpNRZPage->GetChoice();
			break;
		default: 
			AffirmThrow(FALSE);
			break;
		}
		bSuccess = TRUE;
	}
	catch(CAffirmException ae)
	{
	}

	return bSuccess;
}

//////////////////////////////////////////////////////////////////////
// Config data
//////////////////////////////////////////////////////////////////////

BOOL CServicePPP::GetConfigData(void)
{
   BOOL fSuccess = FALSE;

   try
   {

	  // Get our static config data
	  AffirmThrow(CService::GetConfigData());

      CString strConfigDataRegPath;
      AffirmThrow(BuildConfigDataRegPath(strConfigDataRegPath));

      HKEY hConfigData = NULL;
      AffirmThrow(RegOpenKey(HKEY_LOCAL_MACHINE, strConfigDataRegPath, &hConfigData));
      AffirmThrow(hConfigData != NULL);

      try
      {
         AffirmThrow(RegReadStrValue(hConfigData, DXB_REGVAL_PPP_LINENAME, m_strPPPLineName));
      }
      catch (CAffirmException ae)
      {
      }

      RegCloseKey(hConfigData);

      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

