/*****************************************************************************
* 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 "BRIConfig.h"


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

   try
   {
      AffirmThrow(hKey != NULL);

      HKEY hBRI = NULL;
      AffirmThrow(RegCreateKey(hKey, DXB_REGKEY_BRI, &hBRI));
      AffirmThrow(hBRI != NULL);

      try
      {
         REGVALUE aRegVals[] = 
         {
            { DXB_REGVAL_BRI_SWITCH_TYPE, &Data.switch_type,   sizeof(Data.switch_type),  REG_DWORD, 0 },
            { DXB_REGVAL_BRI_AUTO_SPID,   &Data.auto_spid,     sizeof(Data.auto_spid),    REG_DWORD, 0 },
            { DXB_REGVAL_BRI_LEASED_MASK, &Data.leased_mask,   sizeof(Data.leased_mask),  REG_DWORD, 0 },
            { DXB_REGVAL_BRI_DIR_COUNT,   &Data.dir_count,     sizeof(Data.dir_count),    REG_DWORD, 0 },
            { DXB_REGVAL_BRI_MSN,         &Data.msn,           sizeof(Data.msn),          REG_DWORD, 0 },
            { DXB_REGVAL_BRI_AUTOALERT,   &Data.autoalert,     sizeof(Data.autoalert),    REG_DWORD, 0 },
         };

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

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

#pragma message("NOTE: NOTE: We currently don't support the BRI autoalert feature!")
#pragma message("NOTE: NOTE: Defaulting BRI autoalert to BRI_AUTOALERT_ON")
         if (Data.autoalert == 0) Data.autoalert = BRI_AUTOALERT_ON;

#pragma message("NOTE: NOTE: Hard coding BRI dir_count to BRI_DIR_UPDATE | ElementCount(Data.directory)")
         if (Data.dir_count == 0) Data.dir_count = (BRI_DIR_UPDATE | ElementCount(Data.directory));

         for (int i = 0; i < ElementCount(Data.directory); i++)
         {
            CString strDir;
            strDir.Format(TEXT("%s%d"), DXB_REGKEY_BRI_DIRECTORY_BASE, i);

            HKEY hDir = NULL;
            AffirmThrow(RegCreateKey(hBRI, strDir, &hDir));
            AffirmThrow(hDir != NULL);

            try
            {
               AffirmThrow(RegReadSzValue(hDir, DXB_REGVAL_BRI_DIRECTORY_NUMBER,      (char *)Data.directory[i].number,      sizeof(Data.directory[i].number)));
               AffirmThrow(RegReadSzValue(hDir, DXB_REGVAL_BRI_DIRECTORY_SUB_ADDRESS, (char *)Data.directory[i].sub_address, sizeof(Data.directory[i].sub_address)));
               AffirmThrow(RegReadSzValue(hDir, DXB_REGVAL_BRI_DIRECTORY_SPID,        (char *)Data.directory[i].spid,        sizeof(Data.directory[i].spid)));
            }
            catch (CAffirmException ae)
            {
            }

            RegCloseKey(hDir);
         }

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

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(hKey != NULL);

      HKEY hBRI = NULL;
      AffirmThrow(RegCreateKey(hKey, DXB_REGKEY_BRI, &hBRI));
      AffirmThrow(hBRI != NULL);

      try
      {
         // do BRI_MSN_ON/BRI_MSN_OFF logic here
         Data.msn = ((Data.switch_type == BRI_ETSI) && (Data.directory[0].number[0] != '\0')) ? BRI_MSN_ON : BRI_MSN_OFF;

         REGVALUE aRegVals[] = 
         {
            { DXB_REGVAL_BRI_SWITCH_TYPE, (void *)Data.switch_type,  sizeof(Data.switch_type),  REG_DWORD,  0 },
            { DXB_REGVAL_BRI_AUTO_SPID,   (void *)Data.auto_spid,    sizeof(Data.auto_spid),    REG_DWORD,  0 },
            { DXB_REGVAL_BRI_LEASED_MASK, (void *)Data.leased_mask,  sizeof(Data.leased_mask),  REG_DWORD,  0 },
            { DXB_REGVAL_BRI_DIR_COUNT,   (void *)Data.dir_count,    sizeof(Data.dir_count),    REG_DWORD,  0 },
            { DXB_REGVAL_BRI_MSN,         (void *)Data.msn,          sizeof(Data.msn),          REG_DWORD,  0 },
            { DXB_REGVAL_BRI_AUTOALERT,   (void *)Data.autoalert,    sizeof(Data.autoalert),    REG_DWORD,  0 },
         };

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

         for (int i = 0; i < ElementCount(Data.directory); i++)
         {
            CString strDir;
            strDir.Format(TEXT("%s%d"), DXB_REGKEY_BRI_DIRECTORY_BASE, i);

            HKEY hDir = NULL;
            AffirmThrow(RegCreateKey(hBRI, strDir, &hDir));
            AffirmThrow(hDir != NULL);

            try
            {
               AffirmThrow(RegWriteSzValue(hDir, DXB_REGVAL_BRI_DIRECTORY_NUMBER,      (char *)Data.directory[i].number));
               AffirmThrow(RegWriteSzValue(hDir, DXB_REGVAL_BRI_DIRECTORY_SUB_ADDRESS, (char *)Data.directory[i].sub_address));
               AffirmThrow(RegWriteSzValue(hDir, DXB_REGVAL_BRI_DIRECTORY_SPID,        (char *)Data.directory[i].spid));
            }
            catch (CAffirmException ae)
            {
            }

            RegCloseKey(hDir);
         }

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

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

   return fSuccess;
}

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

   try
   {
      AffirmThrow(hKey != NULL);
      AffirmThrow(RegDelKey(hKey, DXB_REGKEY_BRI));
      fSuccess = TRUE;
   }
   catch (CAffirmException ae)
   {
   }

   return fSuccess;
}
