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


// ServiceAsyn.cpp: implementation of the CServiceAsyn class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ServiceAsyn.h"
#include "ObjectFactory.h"
#include "PortSerial.h"
#include "PortStdCom.h"

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

#include "Resource.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CServiceAsyn, CService)

CServiceAsyn::CServiceAsyn(HKEY const hKeyRoot, CString const strRegPath, CConfigObject const * const pParent) : CService(hKeyRoot, strRegPath, pParent)
{
   try
   {
      // Get parameters
      AffirmThrow(GetParameters());

      // Solve our identity crisis
      SetIdentity(DXB_REGVAL_LINE_SERVICE_ASYN);

      // Set our display name
      CString strDisplayName;
      AffirmThrow(strDisplayName.LoadString(IDS_DisplayName_Serial));
      SetDisplayName(strDisplayName);

      // Set what we can do
      SetAbility(CConfigObject::NOABILITIES);

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

      // Give birth to our children
      for (int i = 0; i < m_cMaxPorts; i++)
      {
         AffirmThrow(AddChild(i));
      }
   }
   catch (CAffirmException ae)
   {
   }
}

CServiceAsyn::~CServiceAsyn()
{
   try
   {
   }
   catch (CAffirmException ae)
   {
   }
}

BOOL CServiceAsyn::AddChild(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      for (int i = 0; i < m_cMaxPorts; i++)
      {
         BOOL fChildAlreadyExists = FALSE;

         for (int j = 0; j < m_apChildren.GetSize(); j++)
         {
            AffirmThrow(m_apChildren[j] != NULL);
            CPort * pChild = (CPort *)m_apChildren[j];

            if (i == pChild->GetPortNumber())
            {
               fChildAlreadyExists = TRUE;
               break;
            }
         }

         if (!fChildAlreadyExists)
         {
            AffirmThrow(AddChild(i));
            fSuccess = TRUE;
            break;
         }
      }
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CServiceAsyn::AddChild(int const nChild)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(nChild >= 0);

      INT_PTR iNewChild = m_apChildren.Add(NULL);

      // This registry path thingie needs a little explanation!
      //    Here's the layout so far:
      //       ...\DigiDxb?\Parameters\XXXX\N         (where ? = the adapter instance, and
      //                                                     N = 0, 1, ... # of lines, and
      //                                                     XXXX = the service name)
      //    The port parameters reside at:
      //       ...\DigiDxb?\Parameters\XXXX\N\n       (where n = 0, 1, 2, ... # of ports)
      //    So, pass in the our current hKey, and the port number sub-key.

      // The ASYN service handles two types of async ports, internal modem and standard
      // NOTE NOTE: The protocol (services's parent) naming is a bit confusing:
      // DXB_REGVAL_PROTOCOL_ENUM_SERIAL -> DXB_REGVAL_PORT_TYPE_STD_COM
      // DXB_REGVAL_PROTOCOL_ENUM_PRI, BRI, T1, E1, POTS -> DXB_REGVAL_PORT_TYPE_SERIAL
      {
         BOOL fStd;
         CString strPort;
         strPort.Format(TEXT("%d"), nChild);
         // Check the line protocol to determine our port type
         fStd = (GetParent()->GetIdentity() == DXB_REGVAL_PROTOCOL_ENUM_SERIAL);
         TRACE("CServiceAsyn::AddChild():Parent:%s\n", GetParent()->GetIdentity());
         // DXB_REGVAL_PORT_TYPE_SERIAL is default
         AffirmThrow(NewConfigObject(&m_apChildren[iNewChild],
                     CConfigObject::PORT,
                     ( fStd ? DXB_REGVAL_PORT_TYPE_STD_COM : DXB_REGVAL_PORT_TYPE_SERIAL ),
                     m_hKey,
                     strPort,
                     this));
      }

      AffirmThrow(m_apChildren[iNewChild] != NULL);
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

//////////////////////////////////////////////////////////////////////
// Get/Set parameters
//////////////////////////////////////////////////////////////////////

BOOL CServiceAsyn::GetParameters(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(CService::GetParameters());
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CServiceAsyn::SetParameters(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(CService::SetParameters());
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL CServiceAsyn::RemoveParameters(BOOL fRemoveKey /* = FALSE */)
{
   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,TRUE);
		strInstance.Empty();
		RegWriteStrValue(m_hKey, DXB_REGVAL_REGINSTANCE, strInstance);
	  }

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

   return fSuccess;
}

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

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

   try
   {
      AffirmThrow(CService::GetConfigData());

      CString strConfigDataRegPath;
      AffirmThrow(BuildConfigDataRegPath(strConfigDataRegPath));

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

      try
      {
      }
      catch (CAffirmException ae)
      {
      }

      RegCloseKey(hConfigData);

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

   return fSuccess;
}

//////////////////////////////////////////////////////////////////////
// UI config methods
//////////////////////////////////////////////////////////////////////

BOOL CServiceAsyn::AddWizardPages(CConfigWizSheet * const pSheet)
{
   BOOL fSuccess = FALSE;

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

   return fSuccess;
}

BOOL CServiceAsyn::NextWizardPage(int const nState)
{
   BOOL fSuccess = FALSE;

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

   return fSuccess;
}

BOOL CServiceAsyn::DoProperties(void)
{
   BOOL fThingsHaveChanged = FALSE;

   try
   {
      AffirmThrow(FALSE);
   }
   catch (CAffirmException ae)
   {
   }

   return fThingsHaveChanged;
}
