/*****************************************************************************
 * Copyright (C) Gemalto M2M GmbH
 *****************************************************************************
 * Project: IMC-RIL-Driver
 *****************************************************************************
 * Author : Wireless Modules RD SW A2
 *****************************************************************************
 * $Workfile: Y:/z_ms-d/src/working/Quinn/RilDrv/rilsimtk.h $
 * $Revision: /main/ms-d/27 $
 *     $Date: 2013-06-10 13:10:27 $
 *   $Author: pmackent $
 *****************************************************************************
 * Target:
 *  Microsoft Windows Mobile 5.0
 *****************************************************************************
 * Description:
 *  Gemalto IMC RIL
 *****************************************************************************
 *
 * $Log: Y:/z_ms-d/src/working/Quinn/RilDrv/rilsimtk.h$ 
 * 
 * /main/ms-d/27   2013-06-10 13:10:27   pmackent 
 * Solve deadlock in SAT PLay Tone (IPIS100088390)
 *
 * /main/ms-d/26   2013-02-08 09:03:37   pmackent
 * Intel style SAT is now official
 *
 * /main/ms-d/25   2012-02-22 13:27:16   ctna0256
 * Encapsulate use of Intel AT command set
 *
 * /main/ms-d/24   2012-02-10 12:59:35   ctna0256
 * Store Idle Mode Status in registry (IPIS100077395)
 *
 * /main/ms-d/23   2012-02-08 15:15:52   ctna0256
 * Add Intel-style SAT
 *
 * /main/ms-d/21   2012-02-02 17:03:59   ctna0256
 * FD phonebook workaround and NITZ URCs for Intel platform
 *
 * /main/ms-d/20   2012-01-31 16:39:00   ctna0256
 * Added support for EHS5
 *
 * /main/ms-d/19   2012-01-20 13:20:43   ctna0256
 * Better handling of SAT Idle Mode Text in case of booting the device (IPIS100077101)
 *
 * /main/ms-d/18   2011-09-26 13:35:15   ctna0256
 * Comply with CE6 warning level 4
 *
 * /main/ms-d/17   2011-02-28 09:19:31   ctna0256
 * Reset SAT Idle Mode Text on start (IPIS100070301)
 *
 * /main/ms-d/16   2011-02-18 10:28:47   ctna0256
 * Merge automatic SAT response from Classic-RIL
 *
 * /main/ms-d/15   2011-02-02 10:55:22   ctna0256
 * Merge SAT call number handling from Classic-RIL
 *
 * /main/ms-d/14   2010-09-07 10:11:51   ctna0256
 * Workaround for missing ^SSTN: 254 URC (IPIS100067413)
 *
 * /main/ms-d/13   2009-10-07 09:22:29   ctna0256
 * Introduce no-operation command queue
 * Support 2nd alpha identifier in SAT setup call (IPIS100055375)
 *
 * /main/ms-d/12   2009-01-22 10:17:25   ctna0256
 * Optional re-call of RHA_ConfirmAudioMode after RHA_ActivateVoicePath
 *
 * /main/ms-d/11   2008-08-22 10:27:37   ctna0256
 * Set Cinterion copyright text
 *
 * /main/ms-d/10   2008-08-21 09:35:26   ctna0256
 * Prepared for new disclaimer handling
 *
 * /main/ms-d/9   2008-06-20 11:06:01   mp49210
 * Remove unused SIEMENS_MAGNETO define
 *
 * /main/ms-d/8   2008-04-02 14:11:33   mp49210
 * Lint check
 *
 * /main/ms-d/7   2008-03-31 13:34:47   mp49210
 * IPIS100017065: delay for automatic SAT responses
 * Support of new SSTR parameters
 * Support of 'no icon' option
 *
 * /main/ms-d/6   2008-03-14 16:09:34   mp49210
 * IPIS100045647: workaround for handling of SAT setup call while active voice call
 *
 * /main/ms-d/5   2007-12-12 16:17:41   mp49210
 * Merge SAT Setup Call handling from Classic RIL
 *
 * /main/ms-d/4   2007-11-21 16:56:15   mp49210
 * Check for missing SAT notifications in initialization.
 *
 * /main/ms-d/3   2007-03-27 11:36:00   mp49210
 * SAT in HC25 is now active before been activated.
 *
 * /main/ms-d/2   2006-12-27 14:20:10   mp49210
 * Workaround for wrong CME-error at AT+CPBR
 *
 * /main/ms-d/1   2006-10-25 16:52:00   wm36264
 * First transformation from classic RIL
 *
*/

//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Gemalto M2M GmbH
// Copyright (C) 2006-2008 Siemens AG
// Copyright (C) 2003 Intrinsyc Europe Ltd.
//
// Version: 1.0
// Date:    dummy
//
//////////////////////////////////////////////////////////////////////////////

/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-1999  Microsoft Corporation
--*/

// Activate binary trace of data passed to application
//#define SIMTK_ENABLE_BIN_TRACE

// Proactive SAT commands
#define SIMTK_CMD_REFRESH                1
#define SIMTK_CMD_SETUP_EVENTLIST        5
#define SIMTK_CMD_SETUP_CALL             16
#define SIMTK_CMD_SEND_SS                17
#define SIMTK_CMD_SEND_USSD              18
#define SIMTK_CMD_SEND_SMS               19
#define SIMTK_CMD_SEND_DTMF              20
#define SIMTK_CMD_LAUNCH_BROWSER         21
#define SIMTK_CMD_PLAY_TONE              32
#define SIMTK_CMD_DISPLAY_TEXT           33
#define SIMTK_CMD_GET_INKEY              34
#define SIMTK_CMD_GET_INPUT              35
#define SIMTK_CMD_SELECT_ITEM            36
#define SIMTK_CMD_SETUP_MENU             37
#define SIMTK_CMD_PROVIDE_LOCAL_INFO     38
#define SIMTK_CMD_SETUP_IDLE_TEXT        40
#define SIMTK_CMD_RUN_AT_CMD             52
#define SIMTK_CMD_LANGUAGE_NOTIFICATION  53
#define SIMTK_CMD_OPEN_CHANNEL           64
#define SIMTK_CMD_CLOSE_CHANNEL          65
#define SIMTK_CMD_RECIEVE_DATA           66
#define SIMTK_CMD_SEND_DATA              67
#define SIMTK_CMD_GET_ICON_DATA          250

// Envelope SAT commands
#define SIMTK_EVT_MENU_SELECTION         211
#define SIMTK_EVT_USER_ACTIVITY          232
#define SIMTK_EVT_IDLESCREEN_AVAILABLE   233
#define SIMTK_EVT_LANGUAGE_SELECTION     235
#define SIMTK_EVT_BROWSER_TERMINATION    236
#define SIMTK_EVT_END_SESSION            254

// Special Cinterion SAT response
#define SIMTK_RSP_SCREEN_BUSY            132
#define SIMTK_RSP_OK_NO_ICON             4

// Maximal length of SAT command
#define SIMTK_MAX_LENGTH                 0x2000
#define UCS2_LEN                         (MAX_ATCMD_LEN / 2)

// SAT command retries
#define SIMTK_CMD_RETRIES                1
#define SIMTK_CMD_RETRY_DELAY            2000

// Default timeout for command delay for certain SAT commands
#define SAT_CMD_DELAY_DEFAULT            3000

// Default timeout  for waiting for missing "^SSTN: 254" URC.
// If timeout is set to 0, the RIL won't try to detect missing
// "^SSTN: 254" URCs.
#define SAT_SSTN254_DEFAULT_TIMEOUT      0

// Default header for several SAT command text fields
#define SIMTK_TXT_STANDARD               "0D0008"
#define SIMTK_TXT_DEFAULT                "170008"
#define SIMTK_TXT_ALPHA                  "050080"
#define SIMTK_TXT_ITEM                   "8F000080"

#define SIMTK_TXT_STANDARD_SIZE          (sizeof(SIMTK_TXT_STANDARD) - 1)
#define SIMTK_TXT_DEFAULT_SIZE           (sizeof(SIMTK_TXT_DEFAULT) - 1)
#define SIMTK_TXT_ALPHA_SIZE             (sizeof(SIMTK_TXT_ALPHA) - 1)
#define SIMTK_TXT_ITEM_SIZE              (sizeof(SIMTK_TXT_ITEM) - 1)
#define SIMTK_TXT_MAX_SIZE               (2 * 254)

// The SAT PLAY TONE command is handled in two different ways:
// MC55, MC75 Rel 1 -3, AC75 Rel1, XT75: when radio sends SAT play tone notification (^SSTGI URC),
//      the tone starts immediately and is stoped with the next AT^SSTR command.
// Other radios: tone must be started with AT^SSTR command. The commands doesn't return until the
//      tone is complely played. To stop this tone, the AT command must be aborted.
// The RIL emulates the behavior of the old radios on the new ones, when this #define is set:
#define SAT_PLAY_TONE_OLD_STYLE
// Same meaning as above, but for the Intel-style SAT implementation.
//#define SAT_PLAY_TONE_OLD_STYLE_INTEL

// In this registry key the RIL stores
#define REG_LAST_IDLEMODETEXT   TEXT("LastIdleModeTextPresent")

// Some support macros
#define IS_EVEN(x)   (((x >> 1) << 1) == x)
#define ADD_DIGIT(p, i, x)   { if (IS_EVEN(i)) { p[i] = 'F'; p[i + 1] = x; p[i + 2] = '\0'; } else { p[i - 1] = x; } i++; }
#define SIMTK_PREPARE_TEXT(szTxt, pTxt, szInit)   (void)strcpy(szTxt, szInit); pTxt = szTxt + (sizeof(szInit) - 1);

#ifdef RIL_ENABLE_SIMTK
#define SAT_INLINE_VOID
#define SAT_INLINE_HRESULT
#define SAT_INLINE_BOOL
#define SAT_INLINE_BOOL_FALSE
#define SAT_INLINE_DWORD
#else // RIL_ENABLE_SIMTK
// Avoid unnecessary warnings when SAT is disabled. 4100 = "unrefereced formal
// parameter". This would appear in all SAT function, which are are replaced
// by these dummies:
#pragma warning ( disable : 4100 )
#define SAT_INLINE_VOID                  { }
#define SAT_INLINE_HRESULT               { return E_NOTIMPL; }
#define SAT_INLINE_BOOL                  { return TRUE; }
#define SAT_INLINE_BOOL_FALSE            { return FALSE; }
#define SAT_INLINE_DWORD                 { return 0; }
#endif // RIL_ENABLE_SIMTK
#define SAT_PARSE_PARAM                  LPCSTR szRspIn, void*& pBlob, UINT& cbBlob

#ifdef RIL_ENABLE_SIMTK
struct SAT_DELAY_THREAD_DATA {
    CSimToolkitHandling*  pSATHandle;
    DWORD dwResponse;
    DWORD dwDelay;
};
#endif

#ifdef SAT_PLAY_TONE_OLD_STYLE
struct PLAYTONE_THREAD_DATA {
    CSimToolkitHandling*  pSATHandle;
    CRilHandle* pRilHandle;
    BOOL fNoIcon;
    BOOL fIntelStyle;
};
#endif


//
// Class implementation for SIM-Toolkit handling
//
class CSimToolkitHandling {
public:
  CSimToolkitHandling(CRilHandle* pRilDevice) SAT_INLINE_VOID;
  ~CSimToolkitHandling() SAT_INLINE_VOID;
  BOOL              Init(CRilHandle* pRilDevice) SAT_INLINE_BOOL;
  BOOL              Deinit(void) SAT_INLINE_BOOL;
  HRESULT           GetProfile(CRilInstanceHandle* pHandle) SAT_INLINE_HRESULT;
  HRESULT           SetProfile(CRilInstanceHandle* pHandle) SAT_INLINE_HRESULT;
  BOOL              ParseNotification(LPCSTR& rszPointer) SAT_INLINE_BOOL;
  BOOL              ParseNotificationSTKPRO(LPCSTR& rszPointer) SAT_INLINE_BOOL;
  BOOL              ParseNotificationSTKCNF(LPCSTR& rszPointer) SAT_INLINE_BOOL;
  BOOL              ParseNotificationSTKCC(LPCSTR& rszPointer) SAT_INLINE_BOOL;
  static HRESULT    ParseGetDevCapsNotifications(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam) SAT_INLINE_HRESULT;
  HRESULT           SendCmdResponse(CRilInstanceHandle* pHandle, const RILSIMTOOLKITRSP* pRsp, LPBYTE pDetails, const DWORD dwDetailSize) SAT_INLINE_HRESULT;
  HRESULT           SendCmdResponseIntel(CRilInstanceHandle* pHandle, const RILSIMTOOLKITRSP* pRsp, LPBYTE pDetails, const DWORD dwDetailSize) SAT_INLINE_HRESULT;
  HRESULT           SendEventDownload(CRilInstanceHandle* pHandle, const DWORD dwEvent, LPBYTE pData, const DWORD dwDataSize) const SAT_INLINE_HRESULT;
  BOOL              SendCmdResponseWithDelay(CSimToolkitHandling*  pSATHandle, DWORD dwResponse, DWORD dwDelay = 0) const SAT_INLINE_BOOL_FALSE;
  BOOL              SATOverrideNeeded(LPCSTR szDial, BOOL &bNoIcon) SAT_INLINE_BOOL_FALSE;
  BOOL              SATNameOverrideNeeded(WCHAR* wszDial, WCHAR *wszName, UINT uiLenName) const SAT_INLINE_BOOL_FALSE;
  BOOL              SATNoIconModus(void) const SAT_INLINE_BOOL_FALSE;
  DWORD             SSTN254TimeoutThread(void) SAT_INLINE_DWORD;
  void              SATSSTRexecuted(void) SAT_INLINE_VOID;
  void              SSTN254TimeoutAbort(void) SAT_INLINE_VOID;
  void              ResetIdleModeText() SAT_INLINE_VOID;
#ifdef RIL_ENABLE_SIMTK
  void              SetSATPlayToneAudioMode(void);

private:
  HRESULT           QueueSimToolkitCmd(DWORD dwCmd);
  static HRESULT    ParseGetCommand(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  static HRESULT    ParseInitSATStatus(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  static HRESULT    ParseRspError(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam, DWORD& dwCode);
  static HRESULT    ParseRspOK(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  static HRESULT    ParseGetProfile(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  BOOL              ParseRefresh(LPCSTR szRsp, void*& pBlob, UINT& cbBlob);
  BOOL              ParseSetupCall(SAT_PARSE_PARAM);
  BOOL              ParseSendSS(SAT_PARSE_PARAM);
  BOOL              ParseSendUSSD(SAT_PARSE_PARAM);
  BOOL              ParseSendSMS(SAT_PARSE_PARAM);
  BOOL              ParsePlayTone(SAT_PARSE_PARAM);
  BOOL              ParseDisplayText(SAT_PARSE_PARAM);
  BOOL              ParseGetInkey(SAT_PARSE_PARAM);
  BOOL              ParseGetInput(SAT_PARSE_PARAM);
  BOOL              ParseSelectItem(SAT_PARSE_PARAM);
  BOOL              ParseSetupMenu(SAT_PARSE_PARAM);
  BOOL              ParseIdleText(SAT_PARSE_PARAM);
  BOOL              ParseRunATNotification(SAT_PARSE_PARAM);
  BOOL              ParseLanguageNotification(SAT_PARSE_PARAM);
  BOOL              ParseBIPOpenChannelNotification(SAT_PARSE_PARAM);
  BOOL              ParseBIPCloseChannelNotification(SAT_PARSE_PARAM);
  BOOL              ParseBIPReceiveDataNotification(SAT_PARSE_PARAM);
  BOOL              ParseBIPSendDataNotification(SAT_PARSE_PARAM);
  void              TraceBlob(BYTE* pBlob, DWORD dwSize);
  BOOL              HandleBlob(DWORD dwNotificationCode, void*& pBlob, UINT& cbBlob, BYTE* pData, DWORD dwSize);
  BOOL              PrepareRilCmdStruct(BYTE* pBuffer, RILSIMTOOLKITCMD** pRilCmd, DWORD dwType) const;
  BOOL              PrepareRilCmdError(RILSIMTOOLKITCMD* pRilCmd, DWORD dwError) const;
  BOOL              ParseSetupEventList(LPCSTR szRsp, void*& pBlob, UINT& cbBlob);
  BOOL              ParseSendDTMF(LPCSTR szRsp, void*& pBlob, UINT& cbBlob);
  BOOL              ParseLaunchBrowser(LPCSTR szRsp, void*& pBlob, UINT& cbBlob);
  void              SetIdleModeTextPresent(BOOL fIdleModeTextPresent);

  // The following functions are for Intel-style SAT commands
  BOOL              ParseSTKRefresh(LPCSTR& rszPointer);
  BOOL              ParseSTKSetupEventList(LPCSTR& rszPointer);
  BOOL              ParseSTKSetupCall(LPCSTR& rszPointer);
  BOOL              ParseSTKSendSS(LPCSTR& rszPointer);
  BOOL              ParseSTKSendUSSD(LPCSTR& rszPointer);
  BOOL              ParseSTKSendSMS(LPCSTR& rszPointer);
  BOOL              ParseSTKSendDTMF(LPCSTR& rszPointer);
  BOOL              ParseSTKLaunchBrowser(LPCSTR& rszPointer);
  BOOL              ParseSTKPlayTone(LPCSTR& rszPointer);
  BOOL              ParseSTKProDisplayText(LPCSTR& rszPointer);
  BOOL              ParseSTKProGetInkey(LPCSTR& rszPointer);
  BOOL              ParseSTKProGetInput(LPCSTR& rszPointer);
  BOOL              ParseSTKProSelectItem(LPCSTR& rszPointer);
  BOOL              ParseSTKProSetupMenu(LPCSTR& rszPointer);
  BOOL              ParseSTKProLocalInformation(LPCSTR& rszPointer);
  BOOL              ParseSTKProSetupIdleText(LPCSTR& rszPointer);
  BOOL              ParseSTKProRunATCmd(LPCSTR& rszPointer);
  BOOL              ParseSTKProLanguageNotification(LPCSTR& rszPointer);
  BOOL              ParseSTKProOpenChannelNotification(LPCSTR& rszPointer);

  BYTE              m_bCmdBuffer[SIMTK_MAX_LENGTH];
  BOOL              m_fLastSetupCallNo;
  char              m_szLastSetupCallNo[MAX_ATCMD_LEN];
  WCHAR             m_wszLastSetupCallName[UCS2_LEN];
  CRITICAL_SECTION  m_csLockBuffer;
  CRilHandle*       m_pRilDevice;
  BOOL              m_fNoResponse;
  BOOL              m_fIgnoreNextSSTR1616;
  BOOL              m_fIgnoreNextSSTR1632;
  BOOL              m_fIgnoreNextSSTR40;
  BOOL              m_fIdleModeTextPresent;
  UINT              m_nLastSATSetCallDetail;
  UINT              m_nLastSATDisplayTextFlags;
  BOOL              m_fApplicationCanDisplayIcons;
  BOOL              m_fLastCommandIncludedIcon;
  BOOL              m_fAutomaticSATResponse;
  DWORD             m_dwSATCallNameOverride;
  DWORD             m_dwSSTN254Timeout;
  HANDLE            m_hSATTimoutTerminate;
  HANDLE            m_hSATTimoutAbort;
  HANDLE            m_hIncoming_SSTN_URC;
  HANDLE            m_hExecuted_SSTR_Cmd;

#endif // RIL_ENABLE_SIMTK
};

