/*****************************************************************************
 * Copyright (C) Gemalto M2M GmbH
 *****************************************************************************
 * Project: IMC-RIL-Driver
 *****************************************************************************
 * Author : Wireless Modules RD SW A2
 *****************************************************************************
 * $Workfile: Y:/z_ms-d/src/working/Quinn/RilDrv/EonsHand.h $
 * $Revision: /main/ms-d/18 $
 *     $Date: 2013-02-28 11:43:53 $
 *   $Author: pmackent $
 *****************************************************************************
 * Target:
 *  Microsoft Windows Mobile 5.0
 *****************************************************************************
 * Description:
 *  Gemalto IMC RIL
 *****************************************************************************
 *
 * $Log: Y:/z_ms-d/src/working/Quinn/RilDrv/EonsHand.h$ 
 * 
 * /main/ms-d/18   2013-02-28 11:43:53   pmackent 
 * Increase buffer size for 3G SIM cards (for example EF_SST)
 *
 * /main/ms-d/17   2012-02-22 13:26:37   ctna0256
 * Encapsulate use of Intel AT command set
 *
 * /main/ms-d/16   2012-01-31 16:38:23   ctna0256
 * Added support for EHS5
 *
 * /main/ms-d/15   2011-02-18 14:45:51   ctna0256
 * Read SIM Service Table
 *
 * /main/ms-d/14   2009-09-28 11:54:42   ctna0256
 * Remove Lint warnings
 *
 * /main/ms-d/13   2009-09-04 09:21:36   ctna0256
 * Workaround for wrong 2G/3G-SIM detection in module (IPIS100057390)
 *
 * /main/ms-d/12   2009-07-13 09:24:54   ctna0256
 * Read-IMSI-Workaround only after PIN (IPIS100056758)
 *
 * /main/ms-d/11   2009-04-08 11:37:27   ctna0256
 * Fast registration
 *
 * /main/ms-d/10   2009-04-06 14:20:23   ctna0256
 * Read IMSI before using AT+CRSM
 *
 * /main/ms-d/9   2008-11-21 15:04:36   ctna0450
 * Detect pnn table coding sceme properly
 *
 * /main/ms-d/8   2008-08-22 10:23:39   ctna0256
 * Set Cinterion copyright text
 *
 * /main/ms-d/7   2008-08-21 09:34:22   ctna0256
 * Prepared for new disclaimer handling
 *
 * /main/ms-d/6   2008-06-19 16:12:28   mp49210
 * Special handling of US MCC values
 *
 * /main/ms-d/5   2008-06-12 11:38:34   mp49210
 * Workaround for wrong characters in EONS name
 *
 * /main/ms-d/4   2008-04-24 10:42:42   mp49210
 * New options for display service provider name
 *
 * /main/ms-d/3   2008-04-10 15:21:35   mp49210
 * New optional handling of service provider name
 *
 * /main/ms-d/2   2007-02-09 15:15:09   mp49210
 * Lint compatibility
 *
 * /main/ms-d/1   2006-10-25 16:53:26   wm36264
 * First transformation from classic RIL
 *
*/

//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Gemalto M2M GmbH
// Copyright (C) 2006-2008 Siemens AG
//
// Version: 1.0
// Date:    dummy
//
//////////////////////////////////////////////////////////////////////////////

#define EONS_MAX_BUF_LINES   64
#define EONS_MAX_BUF_BYTES   64
#define EONS_MAX_CRSM        16
#define EONS_MAX_PNN         10
#define EONS_MAX_OPL         64
#define EONS_MAX_OPN_LEN     24

// Needed for composite AT+CRSM command (emulatoed EONS only):
#define MAX_ATCMD_LEN_EONS   760

// Numbers of retries for AT+CRSM commands:
#define DEFAULT_RETRIES_FOR_CRSM          10
#define DEFAULT_RETRIES_FOR_CRSM_LONGNAME 1
#define DEFAULT_RETRY_DELAY_FOR_CRSM      2000

// Enum for the state of EONS support
enum EONS_STATE {
  EONS_DISABLED = 0,
  EONS_NOT_AVAILABLE,
  EONS_ENABLED,
  EONS_EMULATED
};

// These values are returned by the LoadTable() functions.
#define READ_EONS_SIM_OK               0
#define READ_EONS_SIM_INVALID          1
#define READ_EONS_SIM_MODULE_FAILURE   2

// These values are the valid return values of UseServiceProvider()
#define USE_SERVICE_PROVDER_NAME_OFF         0
#define USE_SERVICE_PROVDER_NAME_ON          1
#define USE_SERVICE_PROVDER_NAME_COMBINATION 2
#define USE_SERVICE_PROVDER_NAME_ON_NAMEONLY 3

// SIM files needed for EONS
#define EF_PNN      0x6fc5
#define EF_OPL      0x6fc6
#define EF_OPR      0x6f18
#define EF_SMST     0x6f38

// Coding sceme of pnn table operator names
#define PNN_CODING_MASK   0x70
#define PNN_CODING_GSM    0x00
#define PNN_CODING_UCS2   0x10

//
// Handling of EONS service
//
class CEonsHandling {
public:
  CEonsHandling();
  ~CEonsHandling();
  BOOL       Init(CRilHandle* pRilDevice);
  BOOL       ReInit(void);
  BOOL       Deinit(void);
  BOOL       GetOperatorInfo(LPSTR szLongName, LPSTR szShortName, INT iBufferLen);
  BOOL       GetOperatorInfoFromTable(WORD wNetworkCode, LPSTR szLongName, LPSTR szShortName, INT iBufferLen);
  DWORD      UseServiceProvider(void) const { return m_dwUseServiceProvider; };
  EONS_STATE GetEONSState(void) const { return m_SupportType; }
  DWORD      EONSInitThreadProc(void);
  BOOL       MustReadIMSIbeforeReadingSIM(void) const { return m_fMustReadIMSIbeforeReadingSIM && (g_dwSIMLockedState == RIL_LOCKEDSTATE_READY); };
  BOOL       IsEONSready(void) const;

private:
  BOOL         IsLongName(void);
  BOOL         EnableModuleHandling(void);
  INT          LoadPnnTable(void);
  INT          LoadOplTable(void);
  BOOL         ScanPnnTable(void);
  BOOL         ScanOplTable(void);
  BOOL         GetSIMFIleInfo(SIMTYPE iSIMType, INT &iRecordLen, INT &iNumRecords) const;
  BOOL         GetSIMFIleInfo(SIMTYPE iSIMType, INT &iFileLen) const;
  DWORD        GenerateATCmd(char* szBuf, DWORD dwBufLen, DWORD dwAddr, DWORD dwStartRec, DWORD dwLength, DWORD dwMaxRecs) const;
  BOOL         ScanPnnName(const BYTE* pSrc, char* pDst, DWORD dwLen) const;
  void         TraceEonsInfo(void) const;
  void         ReadSIMServiceTable(void);
  BOOL         IsUSAMCC(WORD wMCC) const { return ((wMCC >= 310 && wMCC <= 316) ? TRUE : FALSE); };
  static HRESULT ParseEONSData(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
#ifdef SUPPORTS_EHSX_R1
  static HRESULT ParseXCOPSData(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
#endif
  static HRESULT ParseSimData(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  static HRESULT ParseSmoniData(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);

  // Info from EF - PNN table
  typedef struct tagPnnInfo {
    char szLongOperatorName[EONS_MAX_OPN_LEN + 1];
    char szShortOperatorName[EONS_MAX_OPN_LEN + 1];
  } PNN_INFO, *PPNN_INFO;

  // Info from EF - OPL table
  typedef struct tagOPLInfo {
    BOOL  fValid;
    BYTE  pbMCC[3];
    BYTE  pbMNC[3];
    WORD  wLACLow;
    WORD  wLACHigh;
    BYTE  bPLMN;
  } OPL_INFO, *POPL_INFO;

  HANDLE            m_hReadyEvent;
  EONS_STATE        m_SupportType;
  CRilHandle*       m_pRilDevice;
  DWORD             m_dwPnnEntries;
  DWORD             m_dwOplEntries;
  PNN_INFO          m_tPnnInfo[EONS_MAX_PNN];
  OPL_INFO          m_tOplInfo[EONS_MAX_OPL];
  CRITICAL_SECTION  m_csRunningCmd;
  CRITICAL_SECTION  m_csRunningReadTable;
  DWORD             m_dwLines;
  DWORD             m_dwBytes[EONS_MAX_BUF_LINES];
  BYTE              m_bBuffer[EONS_MAX_BUF_LINES][EONS_MAX_BUF_BYTES];
  char              m_szModuleLongOperatorName[EONS_MAX_OPN_LEN + 1];
  char              m_szModuleShortOperatorName[EONS_MAX_OPN_LEN + 1];
  char              m_szModuleServiceProviderName[EONS_MAX_OPN_LEN + 1];
  UINT              m_nEONSType;
  BOOL              m_fIsLongName;
  WORD              m_wSimSw1;
  WORD              m_wSimSw2;
  WORD              m_wCurrentMCC;
  WORD              m_wCurrentMNC;
  WORD              m_wCurrentLAC;
  DWORD             m_dwUseServiceProvider;
  BOOL              m_fEONSTableAvailable;
  BOOL              m_fDisableEONSSIMReadTable;
  BOOL              m_fMustReadIMSIbeforeReadingSIM;
};

