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


#include "stdafx.h"
#include "CSUConfig.h"


BOOL SCSUConfig::Get(HKEY const hKey)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(hKey != NULL);

      HKEY hCSU = NULL;
      AffirmThrow(RegCreateKey(hKey, DXB_REGKEY_DS1, &hCSU));
      AffirmThrow(hCSU != NULL);

      try
      {
         REGVALUE aRegVals[] = 
         {
            { DXB_REGVAL_DS1_CODING,            &Data.coding,              sizeof(Data.coding),             REG_DWORD,  0 },
            { DXB_REGVAL_DS1_FRAMING,           &Data.framing,             sizeof(Data.framing),            REG_DWORD,  0 },
            { DXB_REGVAL_DS1_IF_TYPE,           &Data.if_type,             sizeof(Data.if_type),            REG_DWORD,  0 },
            { DXB_REGVAL_DS1_LONG_HAUL_LBO,     &Data.long_haul_lbo,       sizeof(Data.long_haul_lbo),      REG_DWORD,  0 },
            { DXB_REGVAL_DS1_SHORT_HAUL_LEN,    &Data.short_haul_len,      sizeof(Data.short_haul_len),     REG_DWORD,  0 },
            { DXB_REGVAL_DS1_CLOCKING,          &Data.clocking,            sizeof(Data.clocking),           REG_DWORD,  0 },
            { DXB_REGVAL_DS1_JITTER_ATTEN,      &Data.jitter_atten,        sizeof(Data.jitter_atten),       REG_DWORD,  0 },
            { DXB_REGVAL_DS1_EQ_GAIN_LIMIT,     &Data.eq_gain_limit,       sizeof(Data.eq_gain_limit),      REG_DWORD,  0 },
         };

         AffirmThrow(RegReadMultipleValues(hCSU, aRegVals, ElementCount(aRegVals)));

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

      RegCloseKey(hCSU);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL SCSUConfig::Set(HKEY const hKey)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(hKey != NULL);

      HKEY hCSU = NULL;
      AffirmThrow(RegCreateKey(hKey, DXB_REGKEY_DS1, &hCSU));
      AffirmThrow(hCSU != NULL);

      try
      {
         REGVALUE aRegVals[] = 
         {
            { DXB_REGVAL_DS1_CODING,            (void *)Data.coding,             sizeof(Data.coding),             REG_DWORD,  0 },
            { DXB_REGVAL_DS1_FRAMING,           (void *)Data.framing,            sizeof(Data.framing),            REG_DWORD,  0 },
            { DXB_REGVAL_DS1_IF_TYPE,           (void *)Data.if_type,            sizeof(Data.if_type),            REG_DWORD,  0 },
            { DXB_REGVAL_DS1_LONG_HAUL_LBO,     (void *)Data.long_haul_lbo,      sizeof(Data.long_haul_lbo),      REG_DWORD,  0 },
            { DXB_REGVAL_DS1_SHORT_HAUL_LEN,    (void *)Data.short_haul_len,     sizeof(Data.short_haul_len),     REG_DWORD,  0 },
            { DXB_REGVAL_DS1_CLOCKING,          (void *)Data.clocking,           sizeof(Data.clocking),           REG_DWORD,  0 },
            { DXB_REGVAL_DS1_JITTER_ATTEN,      (void *)Data.jitter_atten,       sizeof(Data.jitter_atten),       REG_DWORD,  0 },
            { DXB_REGVAL_DS1_EQ_GAIN_LIMIT,     (void *)Data.eq_gain_limit,      sizeof(Data.eq_gain_limit),      REG_DWORD,  0 },
         };

         AffirmThrow(RegWriteMultipleValues(hCSU, aRegVals, ElementCount(aRegVals)));

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

      RegCloseKey(hCSU);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL SCSUConfig::Remove(HKEY const hKey)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(hKey != NULL);

      HKEY hCSU = NULL;
      AffirmThrow(RegCreateKey(hKey, DXB_REGKEY_DS1, &hCSU));
      AffirmThrow(hCSU != NULL);

      try
      {
         static TCHAR const * const aRegVals[] = 
         {
            DXB_REGVAL_DS1_CODING,
            DXB_REGVAL_DS1_FRAMING,
            DXB_REGVAL_DS1_IF_TYPE,
            DXB_REGVAL_DS1_LONG_HAUL_LBO,
            DXB_REGVAL_DS1_SHORT_HAUL_LEN,
            DXB_REGVAL_DS1_CLOCKING,
            DXB_REGVAL_DS1_JITTER_ATTEN,
            DXB_REGVAL_DS1_EQ_GAIN_LIMIT,
         };

         for (int i = 0; i < ElementCount(aRegVals); i++)
         {
            Affirm(RegDelValue(hCSU, aRegVals[i]));
         }

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

      RegCloseKey(hCSU);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}


// Fractional T1 Configuration bits

BOOL SFracT1Config::Get(HKEY const hKey)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(hKey != NULL);

      HKEY hCSU = NULL;
      AffirmThrow(RegCreateKey(hKey, DXB_REGKEY_DS1, &hCSU));
      AffirmThrow(hCSU != NULL);

      try
      {
         AffirmThrow(RegReadDWordValue(hCSU, DXB_REGVAL_CSU_FRACT1_TS_MASK, &TimeSlotMask, DXB_REGVAL_CSU_FRACT1_TS_MASK_DEFAULT));
         AffirmThrow(RegReadDWordValue(hCSU, DXB_REGVAL_CSU_FRACT1_SPD_MASK, &SpeedMask, DXB_REGVAL_CSU_FRACT1_SPD_MASK_DEFAULT));
         TRACE("SFracT1Config::Get:TimeSlotMask 0x%x\n", TimeSlotMask);
         TRACE("SFracT1Config::Get:SpeedMask 0x%x\n", SpeedMask);

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


      RegCloseKey(hCSU);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL SFracT1Config::Set(HKEY const hKey)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(hKey != NULL);

      HKEY hCSU = NULL;
      AffirmThrow(RegCreateKey(hKey, DXB_REGKEY_DS1, &hCSU));
      AffirmThrow(hCSU != NULL);

      try
      {
         AffirmThrow(RegWriteDWordValue(hCSU, DXB_REGVAL_CSU_FRACT1_TS_MASK, TimeSlotMask));
         AffirmThrow(RegWriteDWordValue(hCSU, DXB_REGVAL_CSU_FRACT1_SPD_MASK, SpeedMask));
         fSuccess = TRUE;
      }
      catch (CAffirmException ae)
      {
      }

      RegCloseKey(hCSU);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}

BOOL SFracT1Config::Remove(HKEY const hKey)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(hKey != NULL);

      HKEY hCSU = NULL;
      AffirmThrow(RegCreateKey(hKey, DXB_REGKEY_DS1, &hCSU));
      AffirmThrow(hCSU != NULL);

      try
      {
         Affirm(RegDelValue(hCSU, DXB_REGVAL_CSU_FRACT1_TS_MASK));
         Affirm(RegDelValue(hCSU, DXB_REGVAL_CSU_FRACT1_SPD_MASK));
         fSuccess = TRUE;
      }
      catch (CAffirmException ae)
      {
      }

      RegCloseKey(hCSU);
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}
