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


// AdapterChaska.cpp: implementation of the CAdapterChaska class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AdapterChaska.h"
#include "ObjectFactory.h"
#include "ModemCCDisplayNameMap.h"

#include "Resource.h"

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

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

IMPLEMENT_DYNAMIC(CAdapterChaska, CAdapter)

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

      if (m_dwModemCC == 0)
      {
         DWORD dwModemCC = 0;

#if !defined NT5
         // For NT4/3.51 we use the actual PCI Sub ID from the board
         switch (m_hwResources.busSpecific.Pci.dwSubPciID)
         {
            case DIGI_PCI_SUBDEVICEID_CHASKA_40:
            case DIGI_PCI_SUBDEVICEID_CHASKA_80: 
            case IBM_PCI_SUBDEVICEID_CHASKA_40:
            case IBM_PCI_SUBDEVICEID_CHASKA_80:
               dwModemCC = COUNTRY_CODE_UNITED_STATES; break;
            
            case DIGI_PCI_SUBDEVICEID_CHASKA_41:
            case DIGI_PCI_SUBDEVICEID_CHASKA_81:
            case IBM_PCI_SUBDEVICEID_CHASKA_41:
            case IBM_PCI_SUBDEVICEID_CHASKA_81:
               dwModemCC = COUNTRY_CODE_UNITED_KINGDOM; break;

            case DIGI_PCI_SUBDEVICEID_CHASKA_42:
            case DIGI_PCI_SUBDEVICEID_CHASKA_82:
            case IBM_PCI_SUBDEVICEID_CHASKA_42:
            case IBM_PCI_SUBDEVICEID_CHASKA_82:
               dwModemCC = COUNTRY_CODE_GERMANY; break;

            case DIGI_PCI_SUBDEVICEID_CHASKA_43:
            case DIGI_PCI_SUBDEVICEID_CHASKA_83:
            case IBM_PCI_SUBDEVICEID_CHASKA_43:
            case IBM_PCI_SUBDEVICEID_CHASKA_83:
               dwModemCC = COUNTRY_CODE_AUSTRALIA; break;

            case DIGI_PCI_SUBDEVICEID_CHASKA_44:
            case DIGI_PCI_SUBDEVICEID_CHASKA_84:
            case IBM_PCI_SUBDEVICEID_CHASKA_44:
            case IBM_PCI_SUBDEVICEID_CHASKA_84:
               dwModemCC = COUNTRY_CODE_SPAIN; break;

            case DIGI_PCI_SUBDEVICEID_CHASKA_45:
            case DIGI_PCI_SUBDEVICEID_CHASKA_85:
            case IBM_PCI_SUBDEVICEID_CHASKA_45:
            case IBM_PCI_SUBDEVICEID_CHASKA_85:
               dwModemCC = COUNTRY_CODE_CZECH_REPUBLIC; break;

            default: AffirmThrow(FALSE); break;
         }
#else // #if !defined NT5
      // For NT5 use Identity of the board

      // DAA 0
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_40) ||
	         (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_80) )
      {
         dwModemCC = COUNTRY_CODE_UNITED_STATES;
      }

      // DAA 1
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_41) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_81) )
      {
         dwModemCC = COUNTRY_CODE_UNITED_KINGDOM;
      }

      // DAA 2
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_42) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_82) )
      {
         dwModemCC = COUNTRY_CODE_GERMANY;
      }

      // DAA 3
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_43) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_83) )
      {
         dwModemCC = COUNTRY_CODE_AUSTRALIA;
      }

#if defined DAA4_SUPPORTED
      // DAA 4
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_44) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_84) )
      {
         dwModemCC = COUNTRY_CODE_SPAIN;
      }
#endif // #if defined DAA4_SUPPORTED

#if defined DAA5_SUPPORTED
      // DAA 5
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_45) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_85) )
      {
         dwModemCC = COUNTRY_CODE_CZECH_REPUBLIC;
      }
#endif // #if defined DAA5_SUPPORTED

#endif // #if !defined NT5

         m_dwModemCC = dwModemCC;
      }

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

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

      // Init wiz mode stuff
      m_WizModemCC.Construct(CPageModemCC::WIZARD, this, WP_MODEMCC);
      m_WizModemCC.Init(m_adwModemCCsAllowed);
   }
   catch (CAffirmException ae)
   {
   }
}

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

BOOL CAdapterChaska::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 serial
      SEnumDescendentsCriteria EnumCriteria(DXB_REGVAL_PORT_TYPE_SERIAL);
      CConfigObject * pPort = NULL;
      CTreeNode Node;
      while ( (pPort = EnumDescendents(EnumCriteria)) != NULL)
      {
         pPort->BuildDisplayTree(&Node);
         pNode->AddBranch(Node);
      }

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

   return fSuccess;
}

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

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

   try
   {
      AffirmThrow(RegReadDWordValue(m_hKey, DXB_REGVAL_MODEM_NATIONALITY, &m_dwModemCC));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

   try
   {
      AffirmThrow(CAdapter::SetParameters());
      AffirmThrow(RegWriteDWordValue(m_hKey, DXB_REGVAL_MODEM_NATIONALITY, m_dwModemCC));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

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

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

   try
   {
      static DWORD const adwModemCCsDAA0[] = CHASKA_DAA0_DRV_CC_LIST_INIT;
      static DWORD const adwModemCCsDAA1[] = CHASKA_DAA1_DRV_CC_LIST_INIT;
      static DWORD const adwModemCCsDAA2[] = CHASKA_DAA2_DRV_CC_LIST_INIT;
      static DWORD const adwModemCCsDAA3[] = CHASKA_DAA3_DRV_CC_LIST_INIT;
      // static DWORD const adwModemCCsDAA4[] = CHASKA_DAA4_DRV_CC_LIST_INIT;
      // static DWORD const adwModemCCsDAA5[] = CHASKA_DAA5_DRV_CC_LIST_INIT;

      DWORD const * adwModemCCs = NULL;
      int cModemCCs = 0;

#if !defined NT5
      switch (m_hwResources.busSpecific.Pci.dwSubPciID)
      {
         case DIGI_PCI_SUBDEVICEID_CHASKA_40:
         case DIGI_PCI_SUBDEVICEID_CHASKA_80:
         case IBM_PCI_SUBDEVICEID_CHASKA_40:
         case IBM_PCI_SUBDEVICEID_CHASKA_80:
            adwModemCCs = adwModemCCsDAA0; cModemCCs = ElementCount(adwModemCCsDAA0); break;

         case DIGI_PCI_SUBDEVICEID_CHASKA_41:
         case DIGI_PCI_SUBDEVICEID_CHASKA_81:
         case IBM_PCI_SUBDEVICEID_CHASKA_41:
         case IBM_PCI_SUBDEVICEID_CHASKA_81:
            adwModemCCs = adwModemCCsDAA1; cModemCCs = ElementCount(adwModemCCsDAA1); break;

         case DIGI_PCI_SUBDEVICEID_CHASKA_42:
         case DIGI_PCI_SUBDEVICEID_CHASKA_82:
         case IBM_PCI_SUBDEVICEID_CHASKA_42:
         case IBM_PCI_SUBDEVICEID_CHASKA_82:
            adwModemCCs = adwModemCCsDAA2; cModemCCs = ElementCount(adwModemCCsDAA2); break;

         case DIGI_PCI_SUBDEVICEID_CHASKA_43:
         case DIGI_PCI_SUBDEVICEID_CHASKA_83:
         case IBM_PCI_SUBDEVICEID_CHASKA_43:
         case IBM_PCI_SUBDEVICEID_CHASKA_83:
            adwModemCCs = adwModemCCsDAA3; cModemCCs = ElementCount(adwModemCCsDAA3); break;

         // case DIGI_PCI_SUBDEVICEID_CHASKA_44:
         // case DIGI_PCI_SUBDEVICEID_CHASKA_84:
         // case IBM_PCI_SUBDEVICEID_CHASKA_44:
         // case IBM_PCI_SUBDEVICEID_CHASKA_84:
         //    adwModemCCs = adwModemCCsDAA4; cModemCCs = ElementCount(adwModemCCsDAA4); break;

         // case DIGI_PCI_SUBDEVICEID_CHASKA_45:
         // case DIGI_PCI_SUBDEVICEID_CHASKA_85:
         // case IBM_PCI_SUBDEVICEID_CHASKA_45:
         // case IBM_PCI_SUBDEVICEID_CHASKA_85:
         //    adwModemCCs = adwModemCCsDAA5; cModemCCs = ElementCount(adwModemCCsDAA5); break;

         default: AffirmThrow(FALSE); break;
      }
#else // #if !defined NT5
	// use the board identity

      // DAA 0
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_40) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_80) )
      {
         adwModemCCs = adwModemCCsDAA0;
         cModemCCs = ElementCount(adwModemCCsDAA0);
      }

      // DAA 1
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_41) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_81) )
      {
         adwModemCCs = adwModemCCsDAA1;
         cModemCCs = ElementCount(adwModemCCsDAA1);
      }

      // DAA 2
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_42) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_82) )
      {
         adwModemCCs = adwModemCCsDAA2;
         cModemCCs = ElementCount(adwModemCCsDAA2);
      }

      // DAA 3
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_43) ||
         (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_83) )
      {
         adwModemCCs = adwModemCCsDAA3;
         cModemCCs = ElementCount(adwModemCCsDAA3);
      }

#if defined DAA4_SUPPORTED
      // DAA 4
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_44) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_84) )
      {
         adwModemCCs = adwModemCCsDAA4;
         cModemCCs = ElementCount(adwModemCCsDAA4);
      }
#endif // #if defined DAA4_SUPPORTED

#if defined DAA5_SUPPORTED
      // DAA 5
      if (	(GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_45) ||
            (GetIdentity() == DXB_REGVAL_ADAPTER_ENUM_CHASKA_85) )
      {
         adwModemCCs = adwModemCCsDAA5;
         cModemCCs = ElementCount(adwModemCCsDAA5);
      }
#endif // #if defined DAA5_SUPPORTED

#endif // #if !defined NT5

      AffirmThrow(adwModemCCs != NULL);
      AffirmThrow(cModemCCs > 0);

      m_adwModemCCsAllowed.RemoveAll();

      for (int i = 0; i < cModemCCs; i++)
      {
         m_adwModemCCsAllowed.Add(adwModemCCs[i]);
      }

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

   return fSuccess;
}

//////////////////////////////////////////////////////////////////////
// Wizard config methods
//////////////////////////////////////////////////////////////////////

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

   try
   {
      AffirmThrow(pSheet != NULL);

#if defined(_DEBUG)
      if ((m_pwsSheet != NULL) && (m_pwsSheet != pSheet))
         TRACE("AddWizardPages() called for different sheet!\n");
#endif

      m_pwsSheet = pSheet;

      AffirmThrow(CAdapter::AddWizardPages(pSheet));

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

   return fSuccess;
}

BOOL CAdapterChaska::RemoveWizardPages(CConfigWizSheet * const pSheet)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pSheet != NULL);

      pSheet->RemovePage(&m_WizModemCC);

      AffirmThrow(CAdapter::RemoveWizardPages(pSheet));

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

   return fSuccess;
}

BOOL CAdapterChaska::HandleWizWelcome(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(m_pwsSheet != NULL);

      m_WizModemCC.SetModemCC(m_dwModemCC);
      AffirmThrow(m_pwsSheet->InsertPage(&m_WizModemCC));

      AffirmThrow(CAdapter::HandleWizWelcome());

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

   return fSuccess;
}

BOOL CAdapterChaska::HandleWizModemCC(void)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(m_pwsSheet != NULL);

      m_dwModemCC = m_WizModemCC.GetModemCC();

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

   return fSuccess;
}

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

   try
   {
      switch (nState)
      {
         case WP_WELCOME: AffirmThrow(HandleWizWelcome());                break;
         case WP_MODEMCC: AffirmThrow(HandleWizModemCC());                break;
         default:         AffirmThrow(CAdapter::NextWizardPage(nState));  break;
      }

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

   return fSuccess;
}

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

CString CAdapterChaska::GetInformationText(void)
{
   CString strInfo = TEXT("");

   try
   {
      CModemCCDisplayNameMap mapModemCCDisplayNames;
      strInfo.Format(IDS_Information_ModemCC, mapModemCCDisplayNames[m_dwModemCC]);
   }
   catch (CAffirmException ae)
   {
   }

   return strInfo;
}

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

   try
   {
      CPageModemCC PropModemCC(CPageModemCC::PROPPAGE);
      PropModemCC.Init(m_adwModemCCsAllowed);
      PropModemCC.SetModemCC(m_dwModemCC);

      CPropertySheet PropSheet(GetDisplayName());
      PropSheet.AddPage(&PropModemCC);
      
      PropSheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
      if (PropSheet.DoModal() == IDOK)
      {
         m_dwModemCC = PropModemCC.GetModemCC();
         fThingsHaveChanged = TRUE;
      }
   }
   catch (CAffirmException ae)
   {
   }

   return fThingsHaveChanged;
}
