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


#include "stdafx.h"
#include "DS1Config.h"


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

   try
   {
      AffirmThrow(hKey != NULL);

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

      try
      {
         REGVALUE aRegVals[] = 
         {
            { DXB_REGVAL_DS1_SIGNALLING,        &Data.signalling,          sizeof(Data.signalling),         REG_DWORD,         0        },
            { DXB_REGVAL_DS1_WINK_DIGITS,       &Data.wink_digits,         sizeof(Data.wink_digits),        REG_DWORD, (void *)UINT_MAX },
            { DXB_REGVAL_DS1_LEASED_MASK,       &Data.leased_mask,         sizeof(Data.leased_mask),        REG_DWORD,         0        },
            { DXB_REGVAL_DS1_NFAS_MASTER_PORT,  &Data.nfas_master_port,    sizeof(Data.nfas_master_port),   REG_DWORD,         0        },
            { DXB_REGVAL_DS1_NFAS_INTERFACE_ID, &Data.nfas_interface_id,   sizeof(Data.nfas_interface_id),  REG_DWORD,         0        },
         };

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

#pragma message("NOTE: NOTE: We currently don't support the leased_mask feature!")
         if (Data.leased_mask == 0) Data.leased_mask = DS1_LEASED_NONE;

#pragma message("NOTE: NOTE: We currently don't support nfas master port!")
         if (Data.nfas_master_port == 0) Data.nfas_master_port = (NFAS_MODIFY_BIT | 0);

#pragma message("NOTE: NOTE: We currently don't support nfas interface id!")
         if (Data.nfas_interface_id == 0) Data.nfas_interface_id = (NFAS_MODIFY_BIT | 0);

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

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(hKey != NULL);

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

      try
      {
         REGVALUE aRegVals[] = 
         {
            { DXB_REGVAL_DS1_SIGNALLING,        (void *)Data.signalling,         sizeof(Data.signalling),         REG_DWORD,  0 },
            { DXB_REGVAL_DS1_WINK_DIGITS,       (void *)Data.wink_digits,        sizeof(Data.wink_digits),        REG_DWORD,  0 },
            { DXB_REGVAL_DS1_LEASED_MASK,       (void *)Data.leased_mask,        sizeof(Data.leased_mask),        REG_DWORD,  0 },
            { DXB_REGVAL_DS1_NFAS_MASTER_PORT,  (void *)Data.nfas_master_port,   sizeof(Data.nfas_master_port),   REG_DWORD,  0 },
            { DXB_REGVAL_DS1_NFAS_INTERFACE_ID, (void *)Data.nfas_interface_id,  sizeof(Data.nfas_interface_id),  REG_DWORD,  0 },
         };

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

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

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(hKey != NULL);

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

      try
      {
         static TCHAR const * const aRegVals[] = 
         {
            DXB_REGVAL_DS1_SIGNALLING,
            DXB_REGVAL_DS1_WINK_DIGITS,
            DXB_REGVAL_DS1_LEASED_MASK,
            DXB_REGVAL_DS1_NFAS_MASTER_PORT,
            DXB_REGVAL_DS1_NFAS_INTERFACE_ID,
         };

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

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

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

   return fSuccess;
}
