/*****************************************************************************
* Copyright (c) 1990-98 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
*
*****************************************************************************/

// ServiceSna.cpp: implementation of the CServiceSna class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ServiceSna.h"
#include "ObjectFactory.h"
#include "SnaConfig.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(CServiceSna, CService)
CServiceSna::CServiceSna(HKEY const hKeyRoot, CString const strRegPath, CConfigObject const * const pParent) : CService(hKeyRoot, strRegPath, pParent)
{
   try
   {

       // Get adapter parameters
       AffirmThrow(GetParameters());

       // Solve our identity crisis
       SetIdentity(DXB_REGVAL_LINE_SERVICE_SNA);

	   // Set our Display Name
	   CString szDisplayName;
	   AffirmThrow(szDisplayName.LoadString(IDS_DisplayName_Service));
	   SetDisplayName(szDisplayName);

	   // define abilities
	//   SetAbility(CConfigObject::DOPROPERTIES);
       // Get our static config data
       AffirmThrow(GetConfigData());

   }
   catch (CAffirmException ae)
   {
   }
}

CServiceSna::~CServiceSna()
{

}
//////////////////////////////////////////////////////////////////////
// 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 SNA driver to
// read and utilize.
BOOL CServiceSna::GetParameters(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(CService::GetParameters());
      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 CServiceSna::SetParameters(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(CService::SetParameters());
	  CString szLineValue;
	  // SNID's are 1 based. We need to be 0 based.
	  szLineValue.Format(m_strSnaLineName,m_dwSnid-1);
      AffirmThrow(RegWriteStrValue(m_hKey, DXB_REGVAL_LINENAME, szLineValue));

#ifdef NT5	  
	  AffirmThrow(RegWriteStrValue(m_hKey, DXB_REGVAL_PDO_HDWID, DXB_HDWID_SNA));

	  // Construct the SNA SNADS Device Name
	  CString szSnaDevice;

	  TRACE("m_intVAdapterNumber	= %d\n",m_intVAdapterNumber);
	  TRACE("m_intLineNumber		= %d\n",m_intLineNumber);

	  szSnaDevice.Format("DGS%04x%d",m_intVAdapterNumber,m_intLineNumber);
	  TRACE("SNA Device Name = %s\n",szSnaDevice);
      AffirmThrow(RegWriteStrValue(m_hKey, DXB_REGVAL_SNA_DEVICE, szSnaDevice));
#endif
	  
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

   try
   {

	   fRemoveKey = TRUE;

#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_SNA_DEVICE));
#endif

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

   return fSuccess;
}


//////////////////////////////////////////////////////////////////////
// UI config methods
//////////////////////////////////////////////////////////////////////
// Currently NONE
BOOL CServiceSna::AddWizardPages(CConfigWizSheet * const pSheet)
{
   BOOL fSuccess = FALSE;

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

   return fSuccess;
}

// Currently NONE
BOOL CServiceSna::NextWizardPage(int const nState)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(FALSE);
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

// Simply pop up a page that asks user to use the SNA manager
// launch it for them as well if possible
BOOL CServiceSna::DoProperties(void)
{
	CSnaConfig dlg;
	dlg.DoModal();
	return TRUE;
}


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

BOOL CServiceSna::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_SNA_LINENAME, m_strSnaLineName));
      }
      catch (CAffirmException ae)
      {
      }

      RegCloseKey(hConfigData);

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

   return fSuccess;
}
