/*****************************************************************************
 * Copyright (C) Cinterion Wireless Modules GmbH
 *****************************************************************************
 * Project: CE-MUX-Driver
 *****************************************************************************
 * Author : Siemens AG Berlin COM WM RD SW1
 *****************************************************************************
 * $Workfile: O:/mwohler_z_ms-d/z_ms-d/src/working/Classic/MuxDrv/MuxDrv.h $
 * $Revision: /main/ms-d/22 $
 * $Date: 2014-03-14 11:53:44 $
 * $Author: mwohler $
 *****************************************************************************
 * Target:
 *  Microsoft Windows CE 4.2 or 5.0
 *****************************************************************************
 * Description:
 *  Cinterion Multiplex Driver general header file
 *****************************************************************************
 *
 * $Log: O:/mwohler_z_ms-d/z_ms-d/src/working/Classic/MuxDrv/MuxDrv.h$ 
 * 
 * /main/ms-d/22   2014-03-14 11:53:44   mwohler 
 * Use latest version of mux protocol implementation based on development for Miami client driver 
 * 
 * /main/ms-d/21   2014-02-05 13:41:02   pmackent 
 * New registry keys to set number of mux channels
 *
 * /main/ms-d/20   2009-09-02 16:02:08   ctna0450
 * Separated module shutdown detection from data reading
 *
 * /main/ms-d/19   2009-08-10 10:02:52   ctna0450
 * Fixed problem with retail trace setting
 *
 * /main/ms-d/18   2009-07-09 10:10:46   ctna0256
 * Configure task priorities via registry (IPIS100056661)
 *
 * /main/ms-d/17   2009-02-18 11:58:15   ctna0256
 * Set copyright text
 *
 * /main/ms-d/16   2008-08-20 11:38:38   ctna0450
 * Prepared for new disclaimer handling
 *
 * /main/ms-d/15   2008-06-20 14:41:58   wm36264
 * Switch to Cinterion branding
 *
 * /main/ms-d/14   2007-05-08 16:47:11   kr36712
 * IPIS100034485
 * reopen implemented for suspend/resume over usb-driver
 *
 * /main/ms-d/13   2006-08-23 16:10:48   wm36264
 * Added defines for HDLC framing (Mux step 2)
 *
 * /main/ms-d/12   2006-06-22 17:35:23   kr36712
 * 0xF9 resynchronisation integrated
 *
 * /main/ms-d/11   2006-05-11 14:01:23   wm36264
 * Avoid false power off detection while testing with DSB board
 *
 * /main/ms-d/10   2006-04-27 14:51:52   wm36264
 * Introduced callback to adjust hardware queue sizes from outside the mux protocol handling
 *
 * /main/ms-d/9   2005-11-28 15:01:10   wm36264
 * Improved module power off detection
 *
 * /main/ms-d/8   2005-10-24 14:23:21   WM36264
 * Added file descriptions
 *
 * /main/ms-d/7   2005-10-20 13:09:50   WM36264
 * Added proper copyright header
 *
 * /main/ms-d/6   2005-10-13 11:50:07   wm36264
 * Removed usage of "toolbox.h" (copied used tool to internal header).
 *
 * /main/ms-d/5   2005-10-11 15:42:55   wm36264
 * Modified logic for module initialization and deinitialization
 *
 * /main/ms-d/4   2005-10-10 16:06:10   wm36264
 * Moved debug macros to where they are needed
 *
 * /main/ms-d/3   2005-10-10 15:35:53   wm36264
 * Reworked macros for debug output
 *
 * /main/ms-d/2   2005-10-07 16:50:29   wm36264
 * Use püositive regfistry values to enable init deinit and shutdown for compatibility with Intrinsyc mux driver.
 *
 * /main/ms-d/1   2005-10-07 16:30:09   wm36264
 * First version of Windows CE Muxer
 *
*/

//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Cinterion Wireless Modules GmbH
// Copyright (C) 2005-2008 Siemens AG
//
// Version: 1.0
// Date:    dummy
//
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//
// MuxDrv.h
//
// This file contains definitions globally used in the Windows CE Multiplex
// Driver's operating system dependant implementation.
//
//////////////////////////////////////////////////////////////////////////////

#pragma once

// Registry access of virtual port section
#define REGVAL_VCOM_MUXINSTANCE       TEXT("MuxInstance")
#define REGVAL_VCOM_DLCI              TEXT("DLCI")

// Registry access of mux driver section
#define REGVAL_MUXDRV_BASEPORT        TEXT("ParentPort")
#define REGVAL_MUXDRV_BAUDRATE        TEXT("BaudRate")
#define REGVAL_MUXDRV_INIT            TEXT("EnableInitialization")
#define REGVAL_MUXDRV_SHUTDOWN        TEXT("EnableShutdown")
#define REGVAL_MUXDRV_INIT_CMD        TEXT("InitCommand%d")
#define REGVAL_MUXDRV_DEINIT_CMD      TEXT("DeinitCommand%d")
#define REGVAL_MUXDRV_WAIT_RESUME     TEXT("WaitAfterResume")
#define REGVAL_MUXDRV_DISABLE_RESYNC  TEXT("DisableResync")
#define REGVAL_MUXDRV_MAX_MUXVERSION  TEXT("MaxMuxVersion")
#define REGVAL_MUXDRV_HDLC_WND_SIZE   TEXT("HdlcWindowSize")
#define REGVAL_MUXDRV_HDLC_FRM_SIZE   TEXT("HdlcFrameSize")
#define REGVAL_MUXDRV_BASEPORT_REOPEN TEXT("BasePortReopen")
#define REGVAL_MUXDRV_INITTASKPRIO    TEXT("InitTaskPriority")
#define REGVAL_MUXDRV_TASKPRIORITY    TEXT("TaskPriority")
#define REGVAL_MUXDRV_SET_DLCI        TEXT("SetMaxDlci")
#define REGVAL_MUXDRV_DISABLE_DSR     TEXT("DisableDsrMonitor")

// Used AT commands
#define ATCMD_MANDATORY_INIT          "AT&S0\\Q3\r"
#define ATCMD_START_MUX               "AT+CMUX=0\r"
#define ATCMD_SHUTDOWN                "AT^SMSO\r"

// The COMM_MASK used in the ReadDataThread()
#define ISALIVE_COMM_MASK             (EV_DSR)

// Timing values
#define TIMEOUT_MODULE_START          30000
#define TIMEOUT_MODULE_STOP           60000
#define TIME_MODULE_OFF_MIN           2000

// Array with baud rates to be scanned during baud rate synchronization.
// The first zero is the placeholder for the required baud rate filled
// by the scanning algorithm.
#define BAUDRATES_TO_SCAN             { 0, 115200, 230400, 57600, 38400 }
#define BAUDRATES_RETRIES             2
#define DEFAULT_HDLC_WINDOW_SIZE      4

// Used task priority. A value of "0" means, that the default priority
// will be used. This value can be overwritten by the registry keys
// REGVAL_MUXDRV_INITTASKPRIO and REGVAL_MUXDRV_TASKPRIORITY.
// The value for the init task priority applies only for the start of
// the multiplexer protocol from sending AT+CMUX=0 until all port are
// initialized. Afterwards the default priority will be used.
// The value of REGVAL_MUXDRV_INITTASKPRIO applies to the buffered base
// port, as well.
#define MUX_DEFAULT_INIT_TASK_PRIORITY   0
#define MUX_DEFAULT_TASK_PRIORITY        0

// The internal mux driver interface
#define FILE_DEVICE_CINTERION_MPX     2071
#define IOCTL_MPX_ADD_VCOM            CTL_CODE(FILE_DEVICE_CINTERION_MPX, 1, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_MPX_REMOVE_VCOM         CTL_CODE(FILE_DEVICE_CINTERION_MPX, 2, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_MPX_POWER_FAIL          CTL_CODE(FILE_DEVICE_CINTERION_MPX, 3, METHOD_BUFFERED, FILE_ANY_ACCESS)


//////////////////////////////////////////////////////////////////////////////

// Class for creating blocks of code that are wrapped in a critical section
class SYNCBLOCK_Implementation {
public:
  SYNCBLOCK_Implementation(CRITICAL_SECTION& rcs) { m_pcs = &rcs; EnterCriticalSection(m_pcs); }
  ~SYNCBLOCK_Implementation() { LeaveCriticalSection(m_pcs); }
private:
  CRITICAL_SECTION* m_pcs;
};

// Class for creating blocks of code inside a SYNCBLOCK that are NOT wrapped in a critical section
class UNSYNCBLOCK_Implementation {
public:
  UNSYNCBLOCK_Implementation(CRITICAL_SECTION& rcs) { m_pcs = &rcs; LeaveCriticalSection(m_pcs); }
  ~UNSYNCBLOCK_Implementation() { EnterCriticalSection(m_pcs); }
private:
  CRITICAL_SECTION* m_pcs;
};

// Macros to make SYNCBLOCK and UNSYNCBLOCK easier to use
#define SYNCBLOCK(cs)                SYNCBLOCK_Implementation    SYNCBLOCK_Object(cs)
#define SYNCBLOCK2(cs)               SYNCBLOCK_Implementation    SYNCBLOCK2_Object(cs)
#define SYNCBLOCK3(cs)               SYNCBLOCK_Implementation    SYNCBLOCK3_Object(cs)
#define UNSYNCBLOCK(cs)              UNSYNCBLOCK_Implementation  UNSYNCBLOCK_Object(cs)
#define UNSYNCBLOCK2(cs)             UNSYNCBLOCK_Implementation  UNSYNCBLOCK2_Object(cs)
#define UNSYNCBLOCK3(cs)             UNSYNCBLOCK_Implementation  UNSYNCBLOCK3_Object(cs)

//////////////////////////////////////////////////////////////////////////////

// Macros for debug output
#if defined(RETAIL_MUXDRV_OUTPUT) || defined(DEBUG)
#define MUXDBGHEX(a, Data, Len)      TraceHexData(a, Data, Len)
#define MUXDBGCRLF(a, T1, T2)        TraceCRLFString(a, T1, T2)
#else // WANT_RETAIL_MUXDRV_OUTPUT
#define MUXDBGHEX(a, Data, Len)
#define MUXDBGCRLF(a, T1, T2)
#endif // WANT_RETAIL_MUXDRV_OUTPUT
