⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comhand.cpp

📁 手机RILGSM实现的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*++
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

Module Name:

comhand.cpp

Abstract:


Notes:


--*/

//------------------------------------------------------------------------------
//  Revision History
//  Date                   Author                    Activity ID                Activity Headline
//  2006-02-21         Zhangchunzhong    CEDB00007161          Add trace module in paladin project  
//  2007-09-27         Hanshuanghuan     WM600017919           输入错误的USSD号码时提示正在拨号时间过长
//  2007-10-10         sunrenhong           WM600019169           delete check audio list for new audio interface
//  2007-12-03         sunrenhong            WM600024808          Clear share memory when send cfun =0
//  2008-01-14         Sunrenhong          WM600029046          fix the bug WM600029046
//  2008-03-17         Wanghongbo          WM600033421          fix the bug WM600033421
//------------------------------------------------------------------------------

#include "precomp.h"
#ifdef RIL_LAST_ERROR
#include "ccoreutl.h"
#endif

// Initialize radio off to true, since this is how we boot up.
BOOL g_bRadioOff = TRUE;
#ifdef RIL_LAST_ERROR
extern DWORD g_dwLastError;
#endif

#ifdef OEM2_DRIVER
extern HANDLE g_hCPINNEvent;
#endif


//add by fengguisen for test
extern BOOL g_HasGetISMI ;
extern LPCSTR g_szUserIdentityString;
//end add

extern HWND g_hUSSDWnd;
//
// Virtual serial device handle ctor
//
CComHandle::CComHandle()
: m_hDownstream(INVALID_HANDLE_VALUE),
#ifdef OEM2_DRIVER
m_hDownstream2(INVALID_HANDLE_VALUE),
#endif
m_hCommandModeEvent(NULL),
m_hCancelEvent(NULL),
#ifndef DEDICATED_DATA_PORT
m_hDataModeInterrupted(NULL),
#endif
#ifdef DEDICATED_DATA_PORT
m_hDataModeEvent(NULL),
#endif
m_pBuffer(NULL),
m_dwDownstreamBaudRate(115200),
m_dwStatQuantum(500),
m_pOwner(NULL),
m_fInited(FALSE),
m_fPortOpened(FALSE),
m_fDataMode(FALSE),
m_fCancelledDial(FALSE),
m_dwNumTimeouts(0),
m_dwMaxTimeouts(2)
{
    // FUNCTION_TRACE(CComHandle::CComHandle);
    DWORD dwTemp;

    InitializeCriticalSection(&m_csDataMode);
    InitializeCriticalSection(&m_csOwner);
    InitializeCriticalSection(&m_csStats);

    // Read the baud rate for the downstream modem
    if (GetRegistryDWORD(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("BaudRate"), &dwTemp))
    {
        m_dwDownstreamBaudRate = dwTemp;
    }

    // Read the data mode interruption time
    if (GetRegistryDWORD(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("DataModeInteruptionQuantum"), &dwTemp))
    {
        m_dwStatQuantum = dwTemp;
    }

    // Read the data mode interruption time
    if (GetRegistryDWORD(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("MaxTimeouts"), &dwTemp))
    {
        m_dwMaxTimeouts = dwTemp;
    }

    // Intialize read and write data bickets to 0
    memset(m_rgdwReadStatBits,  0x00, STAT_BUCKETS * sizeof(DWORD));
    memset(m_rgdwWriteStatBits, 0x00, STAT_BUCKETS * sizeof(DWORD));

    // Set the initial timestamps for com activity stats
    m_dwReadStatTimestamp = GetTickCount();
    m_dwWriteStatTimestamp = m_dwReadStatTimestamp;
}


//
// Virtual serial device handle dtor
//
CComHandle::~CComHandle()
{
    // FUNCTION_TRACE(CComHandle::~CComHandle);
    if (m_fPortOpened)
    {
        (void)CloseDownstreamPort();
        m_fPortOpened = FALSE;
    }

    if (m_hCommandModeEvent)
    {
        CloseHandle(m_hCommandModeEvent);
        m_hCommandModeEvent = NULL;
    }

    if (m_hCancelEvent)
    {
        CloseHandle(m_hCancelEvent);
        m_hCancelEvent = NULL;
    }

#ifndef DEDICATED_DATA_PORT
    if (m_hDataModeInterrupted)
    {
        CloseHandle(m_hDataModeInterrupted);
        m_hDataModeInterrupted = NULL;
    }
#endif

#ifdef DEDICATED_DATA_PORT
    if (m_hDataModeEvent)
    {
        CloseHandle(m_hDataModeEvent);
        m_hDataModeEvent = NULL;
    }
#endif

    delete m_pBuffer;
    m_pBuffer = NULL;

    DeleteCriticalSection(&m_csDataMode);
    DeleteCriticalSection(&m_csOwner);
    DeleteCriticalSection(&m_csStats);
    m_fInited = FALSE;
}


//
// Virtual serial device handle initialization
//
BOOL CComHandle::Init()
{
    // FUNCTION_TRACE(CComHandle::Init);
    if (m_fInited)
    {
        goto Error;
    }

    // Create command-mode event
    m_hCommandModeEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
    if (!m_hCommandModeEvent)
    {
        goto Error;
    }

    // Create cancel event
    m_hCancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!m_hCancelEvent)
    {
        goto Error;
    }

#ifndef DEDICATED_DATA_PORT
    // Create data mode interrupt event
    m_hDataModeInterrupted = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!m_hDataModeInterrupted)
    {
        goto Error;
    }
#endif

#if DEDICATED_DATA_PORT

    // Create event to signal data mode to data port
    m_hDataModeEvent = CreateEvent(NULL, TRUE, FALSE, RILDRIVERDATAMODE_EVENT);
    if (!m_hDataModeEvent)
    {
        goto Error;
    }

#endif

    // Create backup buffer
    m_pBuffer = new CBuffer;
    if (!m_pBuffer)
    {
        goto Error;
    }

    // Initialize the shared resource underneath
    m_fInited = CSharedResource::Init(m_hCancelEvent);

    Error:
    return m_fInited;
}

// Causes a reboot if more than some number of commands in a row fail
void CComHandle::SetTimeoutStatus(BOOL bTimedOut)
{
    // FUNCTION_TRACE(CComHandle::SetTimeoutStatus);
    if (bTimedOut)
    {
        m_dwNumTimeouts++;
        if (m_dwNumTimeouts>=m_dwMaxTimeouts)
        {
            DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : CComHandle::SetTimeoutStatus : Too many timeouts, rebooting via CPM\r\n")));
            SignalCriticalError(RILLOG_EVENT_TOOMANYTIMEOUTS, __LINE__, __FILE__);
        }
    }
    else
    {
        m_dwNumTimeouts=0;
    }
}

//
// Open downstream port
//
BOOL CComHandle::OpenDownstreamPort()
{
    // FUNCTION_TRACE(CComHandle::OpenDownstreamPort);

#ifdef UNDER_CE
    DWORD dwFlags = 0;
#else   // UNDER_CE
    DWORD dwFlags = FILE_FLAG_OVERLAPPED;
#endif // UNDER_CE

    TCHAR tszComName[6];
    DWORD dwTimeout;
    DWORD nAttempts = 0;
    DWORD nMaxAttempts;
    BOOL fRet = FALSE;

    // Get the COM port name
    if (!GetRegistrySZ(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("ComPort"), tszComName, 6))
    {
        _tcsncpyz(tszComName, TEXT("COM2:"), 6);
    }

    DEBUGMSG(ZONE_TRACE, (TEXT("RILDrv : t : CComHandle::OpenDownstreamPort : Connecting to COM port: %s\r\n"), tszComName));

#ifdef OEM2_DRIVER
    TCHAR tszComNameNotifications[6];
    // Get the Notification COM port name
    if (!GetRegistrySZ(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("ComPortNotifications"), tszComNameNotifications, 6))
    {
        _tcsncpyz(tszComName, TEXT("COM2:"), 6);
    }

    DEBUGMSG(ZONE_TRACE, (TEXT("RILDrv : t : CComHandle::OpenDownstreamPort : Connecting to Notifications COM port: %s\r\n"), tszComNameNotifications));
#endif


    // Get the max number of times we should try to open the COM port
    if (!GetRegistryDWORD(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("MaxOpenAttempts"), &nMaxAttempts))
    {
        nMaxAttempts = 3;
    }

    // Get the amount of time we should sleep between attempts
    if (!GetRegistryDWORD(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("OpenTimeout"), &dwTimeout))
    {
        dwTimeout = 1000;
    }

    // Open the COM port
    while (1)
    {
        // Try to open the COM port
        m_hDownstream = CreateFile(tszComName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, dwFlags, NULL);

        // If we succeeded or exhausted the number of attempts, quit looping
        nAttempts++;
        if (m_hDownstream != INVALID_HANDLE_VALUE || nAttempts >= nMaxAttempts)
        {
            break;
        }

        // Sleep the required amount of time and try again
        Sleep(dwTimeout);
    }

#ifdef OEM2_DRIVER
    nAttempts = 0;
    // Open the Notifications COM port
    while (1)
    {
        // Try to open the COM port
        m_hDownstream2 = CreateFile(tszComNameNotifications, GENERIC_READ, 0, NULL, OPEN_EXISTING, dwFlags, NULL);

        // If we succeeded or exhausted the number of attempts, quit looping
        nAttempts++;
        if (m_hDownstream2 != INVALID_HANDLE_VALUE || nAttempts >= nMaxAttempts)
        {
            break;
        }

        // Sleep the required amount of time and try again
        Sleep(dwTimeout);
    }

    if (INVALID_HANDLE_VALUE == m_hDownstream2)
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : CComHandle::OpenDownstreamPort : Couldn't open downstream port 2\r\n")));
        goto Error;
    }
#endif

    if (INVALID_HANDLE_VALUE == m_hDownstream)
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : CComHandle::OpenDownstreamPort : Couldn't open downstream port\r\n")));
        goto Error;
    }

    if (!InitCommState(NULL))
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : CComHandle::OpenDownstreamPort : Couldn't Init comm state\r\n")));
        goto Error;
    }

    m_fPortOpened = TRUE;
    fRet = TRUE;

    Error:
    if (!fRet)
    {
        if (INVALID_HANDLE_VALUE != m_hDownstream)
        {
            (void)CloseHandle(m_hDownstream);
        }
#ifdef OEM2_DRIVER
        if (INVALID_HANDLE_VALUE != m_hDownstream2)
        {
            (void)CloseHandle(m_hDownstream2);
        }
#endif
    }
    return fRet;
}


//
// Close downstream port
//
BOOL CComHandle::CloseDownstreamPort()
{
    // FUNCTION_TRACE(CComHandle::CloseDownstreamPort);
    BOOL fRet = FALSE;

    if (!m_fInited || !m_fPortOpened || INVALID_HANDLE_VALUE == m_hDownstream)
    {
        goto Error;
    }

#ifdef OEM2_DRIVER
    if (INVALID_HANDLE_VALUE == m_hDownstream2)
    {
        goto Error;
    }
#endif

    // Release all the waiting operations
    (void)SetEvent(m_hCancelEvent);

    // Close the downstream port
    (void)CloseHandle(m_hDownstream);
    m_hDownstream = INVALID_HANDLE_VALUE;
    
#ifdef OEM2_DRIVER
    // Close the downstream notifications port
    (void)CloseHandle(m_hDownstream2);
    m_hDownstream2 = INVALID_HANDLE_VALUE;
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -