/*****************************************************************************
* 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
*
*****************************************************************************/


// AdapterAcceleportXp.cpp: implementation of the CAdapterAcceleportXp class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AdapterAcceleportXp.h"
#include "ObjectFactory.h"
#include "PropPageLatency.h"

#include "Resource.h"

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

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

IMPLEMENT_DYNAMIC(CAdapterAcceleportXp, CComOnlyAdapter)

CAdapterAcceleportXp::CAdapterAcceleportXp(HKEY const hKeyRoot, CString const & strRegPath, CConfigObject const * const pParent) : CComOnlyAdapter(hKeyRoot, strRegPath, pParent)
{
   TRACE("CAdapterAcceleportXp::CAdapterAcceleportXp\n");
   try
   {
      // Get parameters
      AffirmThrow(GetParameters());

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

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

   }
   catch (CAffirmException ae)
   {
   }
}

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

//
// Configuration Parameters
//

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

   // For DXB, low latency implies using interrupts, for ASYN, it's simply low latency
   try
   {
      AffirmThrow(CComOnlyAdapter::GetParameters());
      AffirmThrow(RegReadDWordValue(m_hKey, DXB_REGVAL_USE_INTERRUPTS, &m_dwLowLatency, DXB_REGVAL_USE_INTERRUPTS_DEFAULT));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

   try
   {
      AffirmThrow(CComOnlyAdapter::SetParameters());
      AffirmThrow(RegWriteDWordValue(m_hKey, DXB_REGVAL_USE_INTERRUPTS, m_dwLowLatency));

      // Now we need to update our children's copy of this parameter
      // We will write it on behalf of the port object since our prop page handles it

      // enumerate all of the ports for this adapter, they're always type StdCom
      SEnumDescendentsCriteria EnumCriteria(DXB_REGVAL_PORT_TYPE_STD_COM);
      CConfigObject * pPort = NULL;
      HKEY hkPortConfigKey;
      while ( (pPort = EnumDescendents(EnumCriteria)) != NULL)
      {
         hkPortConfigKey = pPort->GetOpenConfigKey();
         AffirmThrow(NULL != hkPortConfigKey);
         AffirmThrow(RegWriteDWordValue(hkPortConfigKey, DXB_REGVAL_ASYN_LOWLATENCY, m_dwLowLatency));
      }

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

   return fSuccess;
}

//
// UI Handling
//

BOOL CAdapterAcceleportXp::BuildDisplayTree(CTreeNode * const pNode)
{
   // Show the ports directly under the adapter, the intermediate objects
   // (line, protocol, service) have no user config or information

   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pNode != NULL);

      pNode->SetDisplayName(GetDisplayName());
      pNode->SetConfigObject(this);

      // enumerate all of the ports for this adapter, they're always type StdCom
      SEnumDescendentsCriteria EnumCriteria(DXB_REGVAL_PORT_TYPE_STD_COM);
      CConfigObject * pPort = NULL;
      CTreeNode Node;
      while ( (pPort = EnumDescendents(EnumCriteria)) != NULL)
      {
         pPort->BuildDisplayTree(&Node);
         pNode->AddBranch(Node);
      }

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

   return fSuccess;
}

CString CAdapterAcceleportXp::GetInformationText(void)
{
   CString strInfo = TEXT("");
   CString strOn((LPCSTR)IDS_INFO_ON);
   CString strOff((LPCSTR)IDS_INFO_OFF);

   try
   {
      strInfo.Format(IDS_Information_LatencyFormat, (0 != m_dwLowLatency) ? strOn : strOff);
   }
   catch (CAffirmException ae)
   {
   }

   return strInfo;
}

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

   try
   {
      CPropertySheet PropSheet(GetDisplayName());

      CPropPageLatency PropPageLatency;
      PropPageLatency.m_fLowLatency = (0 != m_dwLowLatency);

      PropSheet.AddPage(&PropPageLatency);
      
      PropSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
      if (PropSheet.DoModal() == IDOK)
      {
         if (PropPageLatency.m_fLowLatency != (0 != m_dwLowLatency))
         {
            m_dwLowLatency = PropPageLatency.m_fLowLatency ? 1 : 0;
            fThingsHaveChanged = TRUE;
         }
      }
   }
   catch (CAffirmException ae)
   {
   }

   return fThingsHaveChanged;
}
