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

#include "dxbtypes.h"
#include "mgmthlc.h"

#include "LineCSURes.h"


typedef struct
{
   DWORD dwValue;
   UINT nDisplayNameId;
}
VALUEMAP;


// CSU Line Encoding Map
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSUEncodingMap[] =
{
   { LINE_CODE_B8ZS, IDS_Encoding_B8ZS },
   { LINE_CODE_AMI,  IDS_Encoding_AMI  },
};

static VALUEMAP const g_CSUEncodingMapE1[] =
{
   { LINE_CODE_HDB3, IDS_Encoding_HDB3 },
};

// CSU Framing Map
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSUFramingMap[] =
{
   { FRAME_ESF,            IDS_Framing_ESF            },
   { FRAME_D4,             IDS_Framing_D4             },
};

// CSU E1 Framing Map : This map should only be used for E1
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSUFramingMapE1[] =
{
   { FRAME_CEPT_CRC4,      IDS_Framing_CEPT_CRC4      },
   { FRAME_CEPT_WO_CRC4,   IDS_Framing_CEPT_WO_CRC4   },
};

// CSU EGL Map
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSUEGLMap[] =
{
   { EGL_LOW,  IDS_EGL_Low  },
   { EGL_HIGH, IDS_EGL_High },
};

// CSU LBO Map
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSULBOMap[] =
{
   { LBO_00_0_DB, IDS_LBO_00 },
   { LBO_07_5_DB, IDS_LBO_75 },
   { LBO_15_0_DB, IDS_LBO_15 },
   { LBO_22_5_DB, IDS_LBO_22 },
};

// CSU SHLen Map
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSUSHLenMap[] =
{
   { SH_0_133,   IDS_SHLen_0   },
   { SH_133_266, IDS_SHLen_133 },
   { SH_266_399, IDS_SHLen_266 },
   { SH_399_533, IDS_SHLen_399 },
   { SH_533_655, IDS_SHLen_533 },
};

// CSU Span Map
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSUSpanMap[] =
{
   { SHORT_HAUL, IDS_Span_Short },
   { LONG_HAUL,  IDS_Span_Long  },
};

// CSU Clocking Map
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSUClockingMap[] =
{
   { TIMING_LOCAL,   IDS_Clocking_Local   },
   { TIMING_LOOP,    IDS_Clocking_Loop    },
   // { TIMING_MVIP,    IDS_Clocking_MVIP }, - Not supported yet!
};

// CSU Jitter Map
//
//////////////////////////////////////////////////////////////////////

static VALUEMAP const g_CSUJitterMap[] =
{
   { JITTER_AUTO, IDS_Jitter_Auto      },
   { JITTER_RX,   IDS_Jitter_Rx        },
   { JITTER_TX,   IDS_Jitter_Tx        },
   { JITTER_NONE, IDS_Jitter_Disabled  },
};


static BOOL GetValues(VALUEMAP const * const pMap, int const cMapItems, CDWordArray & adwValues)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pMap != NULL);
      AffirmThrow(cMapItems > 0);
      AffirmThrow(AfxIsValidAddress(pMap, (sizeof(*pMap) * cMapItems), FALSE));

      for (int i = 0; i < cMapItems; i++)
      {
         adwValues.SetAtGrow(i, pMap[i].dwValue);
      }

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

   return fSuccess;
}

static BOOL GetDisplayNames(VALUEMAP const * const pMap, int const cMapItems, CStringArray & astrDisplayNames)
{
   BOOL fSuccess = FALSE;

   try
   {
      AffirmThrow(pMap != NULL);
      AffirmThrow(cMapItems > 0);
      AffirmThrow(AfxIsValidAddress(pMap, (sizeof(*pMap) * cMapItems), FALSE));

      for (int i = 0; i < cMapItems; i++)
      {
         CString strDisplayName;
         AffirmThrow(strDisplayName.LoadString(pMap[i].nDisplayNameId));
         astrDisplayNames.SetAtGrow(i, strDisplayName);
      }

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

   return fSuccess;
}

BOOL SValueMapCSU::Init(int const eValueMap)
{
   BOOL fSuccess = FALSE;

   try
   {
      adwValues.RemoveAll();
      astrDisplayNames.RemoveAll();

      static const struct
      {
         int eMap;
         VALUEMAP const * pMap;
         int cItems;
      }
      aMaps[] =
      {
         { ENCODING,    g_CSUEncodingMap,   ElementCount(g_CSUEncodingMap)   },
         { ENCODING_E1, g_CSUEncodingMapE1, ElementCount(g_CSUEncodingMapE1) },
         { FRAMING,     g_CSUFramingMap,    ElementCount(g_CSUFramingMap)    },
         { FRAMING_E1,  g_CSUFramingMapE1,  ElementCount(g_CSUFramingMapE1)  },
         { EGL,         g_CSUEGLMap,        ElementCount(g_CSUEGLMap)        },
         { LBO,         g_CSULBOMap,        ElementCount(g_CSULBOMap)        },
         { SHLEN,       g_CSUSHLenMap,      ElementCount(g_CSUSHLenMap)      },
         { SPAN,        g_CSUSpanMap,       ElementCount(g_CSUSpanMap)       },
         { CLOCKING,    g_CSUClockingMap,   ElementCount(g_CSUClockingMap)   },
         { JITTER,      g_CSUJitterMap,     ElementCount(g_CSUJitterMap)     },
      };

      VALUEMAP const * pMap = NULL;
      int cItems = 0;

      for (int i = 0; i < ElementCount(aMaps); i++)
      {
         if (eValueMap == aMaps[i].eMap)
         {
            pMap = aMaps[i].pMap;
            cItems = aMaps[i].cItems;
            break;
         }
      }

      AffirmThrow(pMap != NULL);
      AffirmThrow(cItems > 0);

      AffirmThrow(GetValues(pMap, cItems, adwValues));
      AffirmThrow(GetDisplayNames(pMap, cItems, astrDisplayNames));

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

   return fSuccess;
}

int SValueMapCSU::ToIndex(DWORD const dwValue, int const iDefault /* = -1 */)
{
   int iIndex = iDefault;

   try
   {
      for (int i = 0; i < adwValues.GetSize(); i++)
      {
         if (dwValue == adwValues[i])
         {
            iIndex = i;
            break;
         }
      }
   }
   catch (CAffirmException ae)
   {
   }

   return iIndex;
}

DWORD SValueMapCSU::FromIndex(int const iIndex)
{
   int dwValue = 0;      // not really sure what a good failure value would be

   try
   {
      AffirmThrow(iIndex >= 0);
      AffirmThrow(iIndex < adwValues.GetSize());
      dwValue = adwValues[iIndex];
   }
   catch (CAffirmException ae)
   {
   }

   return dwValue;
}

CString SValueMapCSU::DisplayNameFromValue(DWORD const dwValue)
{
   CString strDisplayName = TEXT("");

   try
   {
      int iIndex = ToIndex(dwValue);
      AffirmThrow(iIndex >= 0);
      AffirmThrow(iIndex < astrDisplayNames.GetSize());
      strDisplayName = astrDisplayNames[iIndex];
   }
   catch (CAffirmException ae)
   {
   }

   return strDisplayName;
}
