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

📄 reboot.cpp

📁 手机RILGSM实现的源代码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//2006-07-10        cuihuawei				CEDB200001562				播放mp3时拍照,系统音量失效
//2007-03-27        likai                         CEDB200049188	            Upgrade codes to Crossbow
//2007-10-10      sunrenhong               WM600019169           delete check audio list for new audio interface
//2007-11-20      Wangxiaogang             WM600023724           Send IOCTRL to audio driver when reboot
//2008-01-07         qinxuetao              WM600027958       Turn the radio on in function RebootRestoreState()
//==============================================================================
#include <precomp.h>
#include "wavext.h"

#ifdef SIMULATE_HUNG_RADIO
BOOL g_fSimulateHungRadio = FALSE;
#endif


#ifdef RIL_RADIO_RESILIENCE

char g_szOpSelCmd[NETWK_CMDBUF_LENGTH];
BOOL g_fOperatorSelected = FALSE;
char g_szPBLocCmd[MISC_CMDBUF_LENGTH_PBLOC];
BOOL g_fPBLocationSet = FALSE;
extern DWORD   g_dwCacheDownlinkVolume;
extern DWORD   g_dwCacheUplinkVolume;
CRITICAL_SECTION g_csReboot;
BOOL g_fInitedFirstTime = FALSE;

extern BOOL g_bRadioOff;

SYSTEMTIME g_LastResetTime;
BOOL g_fRadioResetRecently =FALSE;
UINT g_cbLastLockCapsSize = 0;
void *g_pLastLockCaps = NULL;


//  2006-07-10  cuihuawei
extern BOOL g_bNetworkHelperRunning;
extern HANDLE g_hGetRegistrationEvent;

BOOL g_bRadioReboot = FALSE;
extern DWORD g_dwEquipmentState;

#define SECONDS_TO_100NS(SEC) (((UINT64)(SEC)) * 10000000ui64)
#define RESET_SPOOF_DURATION SECONDS_TO_100NS(10)

#ifdef OEM1_DRIVER
#include <csmi_api.h>
#endif

#ifdef OEM2_DRIVER
#define IOCTL_TTPCOM_REBOOT CTL_CODE( FILE_DEVICE_SERIAL_PORT, 2048, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif // OEM2_DRIVER

extern void FillEquipmentStateStruct(RILEQUIPMENTSTATE * pres, DWORD dwEqState);

//add by fengguisen for cc restore after radio reboot
BOOL IsPrimaryCard(void);

BOOL ShouldSpoofCommand()
{

    BOOL fReturn = FALSE;
    if (g_fRadioResetRecently)
    {
        SYSTEMTIME CurrentSystemTime;
        GetSystemTime(&CurrentSystemTime);
        UINT64 CurrentFileTime;
        UINT64 LastResetFileTime;
        if ((SystemTimeToFileTime(&CurrentSystemTime, (LPFILETIME)&CurrentFileTime)) &&
            (SystemTimeToFileTime(&g_LastResetTime, (LPFILETIME)&LastResetFileTime)))
        {
            if ((CurrentFileTime - LastResetFileTime) < RESET_SPOOF_DURATION)
            {
                fReturn = TRUE;  
            }
            else
            {
                // We've passed the time where we are comfortable spoofing this response.
                // set the flag so that we won't check next time
                g_fRadioResetRecently = FALSE;
            }
        }
    }
    return fReturn;
}

BOOL RebootRadio()
{
    FUNCTION_TRACE(RebootRadio);
    DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : CRilHandle::RebootRadio\r\n")));

    BOOL bReset = FALSE;
    //unset the helper thread before reboot      cuihauwei
    if (g_bNetworkHelperRunning)
    {
        SetEvent(g_hGetRegistrationEvent);
    }



#ifdef SIMULATE_HUNG_RADIO
    g_fSimulateHungRadio = FALSE;
#endif

    // Add platform specific code to reboot the radio here
#ifdef OEM1_DRIVER
    TCHAR tszComName[6];
    // Get the COM port name
    if (!GetRegistrySZ(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("ControlPort"), tszComName, 6))
    {
        _tcsncpyz(tszComName, TEXT("GTI1:"), 6);
    }
    
    // Try to open the COM port
    HANDLE hRebootPort = CreateFile(tszComName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

    if (hRebootPort != INVALID_HANDLE_VALUE)
    {
        DWORD dwBytesReturned = 0;
        DWORD dwRet = 0;
        HANDLE hWavDev = NULL;
        hWavDev = CreateFile(TEXT("WAV1:"),
                                        GENERIC_READ | GENERIC_WRITE,
                                        0, NULL,
                                        OPEN_EXISTING,
                                        0, NULL);
        RETAILMSG(TRUE, (TEXT("[TI]Create wave device file End!\r\n")));

        if(( hWavDev == INVALID_HANDLE_VALUE  ) || (hWavDev == NULL))
        {
            hWavDev = NULL;
            RETAILMSG(TRUE, (TEXT("[TI] WARNING, could not open the wave driver!\r\n")));
        }
        else
        {
            RETAILMSG(TRUE, (TEXT("[TI]start to send IOCTL_GSM_PRERESTARTED !\r\n")));
            if (!DeviceIoControl (hWavDev, IOCTL_GSM_PRERESTARTED, 0, 0, NULL, 0, &dwRet, NULL))
            {
                RETAILMSG(TRUE, (TEXT("[TI] IOCTL_GSM_PreRESTARTED failed --> Audio Handle is NULL or invalid\r\n")));
            }
            RETAILMSG(TRUE, (TEXT("[TI] IOCTL_GSM_PreRESTARTED End !\r\n")));
        }
#ifdef SIMULATE_HUNG_RADIO
        g_fSimulateHungRadio = FALSE;
#endif
        g_bRadioReboot = DeviceIoControl(hRebootPort, GTI_IOCTL_GC_BOOT, NULL, 0, NULL, 0, &dwBytesReturned, NULL);        
        CloseHandle(hRebootPort);
        
        if(( hWavDev == INVALID_HANDLE_VALUE  ) || (hWavDev == NULL))
        {
            hWavDev = NULL;
            RETAILMSG(TRUE, (TEXT("[TI] WARNING, could not open the wave driver!\r\n")));
        }
        else
        {
            RETAILMSG(TRUE, (TEXT("[TI]start to send IOCTL_GSM_RESTARTED !\r\n")));
            if (!DeviceIoControl (hWavDev, IOCTL_GSM_RESTARTED, 0, 0, NULL, 0, &dwRet, NULL))
            {
                RETAILMSG(TRUE, (TEXT("[TI] IOCTL_GSM_RESTARTED failed --> Audio Handle is NULL or invalid\r\n")));
            }
            CloseHandle(hWavDev);
            hWavDev = NULL;
            RETAILMSG(TRUE, (TEXT("[TI] IOCTL_GSM_RESTARTED End !\r\n")));
        }
        
        bReset = TRUE;
    }
#endif // OEM1_DRIVER

#ifdef OEM2_DRIVER
    TCHAR tszComName[6];
    if (!GetRegistrySZ(HKEY_LOCAL_MACHINE, g_tszRegKeyRIL, TEXT("ComPort"), tszComName, 6))
    {
        _tcsncpyz(tszComName, TEXT("MUX1:"), 6);
    }

    // Try to open the COM port
    HANDLE hRebootPort = CreateFile(tszComName, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if (INVALID_HANDLE_VALUE != hRebootPort)
    {
        DWORD dwBytesReturned = 0;
#ifdef SIMULATE_HUNG_RADIO
        g_fSimulateHungRadio = FALSE;
#endif
        DeviceIoControl(hRebootPort, IOCTL_TTPCOM_REBOOT, NULL, 0, NULL, 0,  &dwBytesReturned, NULL);
        CloseHandle(hRebootPort);
        bReset = TRUE;
    }
#endif // OEM2_DRIVER
    return bReset;
}

#if defined(OEM1_DRIVER) && defined(IMPLEMENT_DAR_REPORTING)
static HRESULT ParseDAR(LPCSTR szRsp, void*& pBlob, UINT& cbBlob)
{
    TBD_FUNCTION(ParseDAR);

#if RIL_WATSON_REPORT    
    g_fWatsonCalled = FALSE;
#endif // RIL_WATSON_REPORT
    SignalCriticalError(RILLOG_EVENT_RADIOFAILUREREASON, __LINE__, __FILE__);        
    return S_OK;
}
#endif

BOOL RebootRestoreState()
{
    BOOL fSuccess = FALSE;
    char szNextCmd[MAX_PATH];
    RILEQUIPMENTSTATE res;    
    CNotificationData *pnd = NULL;
    DWORD dwDisconnectReason = RIL_DISCINIT_REMOTE;
    DWORD dwOldReadyState = g_dwReadyState;
    BOOL OldRadioOff = g_bRadioOff;

    //add by fengguisen for cc restore after radio reboot
    RILDrv_CCRestoreAfterReboot();
    
    EnterCriticalSection(&g_csReboot);

    // Audio control: Make sure that we turn off the audio path both starting up
    RILDrv_CC_TurnOffAudioWetherOrNot();
    // We rebooted, so we want to cycle through the ready states from none again.
    g_dwReadyState = RIL_READYSTATE_NONE;        

    // Test hook for knowing when the radio rebooted
    //modify by fengguisen for cc restore after radio reboot
    if (IsPrimaryCard())
    {
        pnd = new CNotificationData;
        if (pnd && !pnd->InitFromRealBlob(RIL_NOTIFY_RADIORESET, NULL, 0))
        {
            delete pnd;
            pnd = NULL;
        }
        else
        {
            QueueCmdIgnoreRsp(APIID_NONE, NULL, CMDOPT_INIT | CMDOPT_NOOP, g_TimeoutCmdInit, NULL, pnd, 0, 0, 0);
        }
    }
    // Assume any calls were disconnected
    pnd = new CNotificationData;
    if (pnd && !pnd->InitFromRealBlob(RIL_NOTIFY_DISCONNECT, (void*)&dwDisconnectReason, sizeof(dwDisconnectReason)))
    {
        delete pnd;
        pnd = NULL;
    }
    else
    {
        QueueCmdIgnoreRsp(APIID_NONE, NULL, CMDOPT_INIT | CMDOPT_NOOP, g_TimeoutCmdInit, NULL, pnd, 0, 0, 0);
    }
#ifdef OEM2_DRIVER
    // Seems like the OEM2 radio needs a bit before we send down the Init strings.
    Sleep(500);
#endif // OEM2_DRIVER

    SendComInitString(COM_INIT_INDEX);
        
#if defined(OEM1_DRIVER) && defined(IMPLEMENT_DAR_REPORTING)
    // OEM1 uses %DAR to query the cause of the last radio crash.  We want this in the radio log or watson report
    QueueCmdIgnoreRsp(APIID_NONE, "AT%DAR\r", CMDOPT_INIT, g_TimeoutCmdInit, ParseDAR, NULL, 0, 0, 0);
#endif
            
    if (OldRadioOff == FALSE && IsPrimaryCard())
    {
        FillEquipmentStateStruct(&res, RIL_EQSTATE_MINIMUM);
        pnd = new CNotificationData;
        if (pnd && !pnd->InitFromRealBlob(RIL_NOTIFY_RADIOEQUIPMENTSTATECHANGED, (void*)&res, res.cbSize))
        {
            delete pnd;
            pnd = NULL;
        }
    }
    else
    {
        // Don't send a CFUN 0 notification if the radio was already off
        pnd = NULL;
    }
   
    // Cycle the radio equipment state, otherwise OEM1 radio will return lots of CME ERROR:3
    QueueCmdIgnoreRsp(APIID_SETEQUIPMENTSTATE, "AT+CFUN=0\r", CMDOPT_INIT | CMDOPT_FLYMOD, g_TimeoutCmdInit, NULL, pnd, 0, 0, 0);

    if ((OldRadioOff == FALSE) || (RIL_EQSTATE_FULL == g_dwEquipmentState))
    {
        if(IsPrimaryCard())
        {
            FillEquipmentStateStruct(&res, RIL_EQSTATE_FULL);
            pnd = new CNotificationData;
            if (pnd && !pnd->InitFromRealBlob(RIL_NOTIFY_RADIOEQUIPMENTSTATECHANGED, (void*)&res, res.cbSize))
            {
                delete pnd;
                pnd = NULL;
            }
        }
        else
        {
            pnd = NULL;
        }
#ifdef OEM1_DRIVER
        // We must send the terminal profile string along when we turn on the radio
        CHAR szCmdBuf[MAX_PATH];
        _snprintfz(szCmdBuf, ARRAY_LENGTH(szCmdBuf), "AT%%SATC=1,%s;+CFUN=1\r", g_RilSTKTerminalProfile.GetText());
        QueueCmdIgnoreRsp(APIID_SETEQUIPMENTSTATE, szCmdBuf, CMDOPT_INIT | CMDOPT_SETRADIOON | CMDOPT_IGNORERADIOOFF | CMDOPT_REINIT | CMDOPT_ONLINE, g_TimeoutCmdInit, NULL, pnd, 0, 0, 0);
#else
        QueueCmdIgnoreRsp(APIID_SETEQUIPMENTSTATE, "AT+CFUN=1\r", CMDOPT_INIT | CMDOPT_SETRADIOON | CMDOPT_IGNORERADIOOFF | CMDOPT_REINIT | CMDOPT_ONLINE, g_TimeoutCmdInit, NULL, pnd, 0, 0, 0);
#endif // ! OEM1_DRIVER
    }
    
    // if we just rebooted and were previously unlocked, then unlock the phone
    if (dwOldReadyState & RIL_READYSTATE_UNLOCKED)
    {
        CHAR szLastPIN[256];
        if (FetchPINSecure(szLastPIN, sizeof(szLastPIN)))
        {
            (void)_snprintfz(szNextCmd, MAX_PATH, "AT+CPIN=\"%s\"\r", szLastPIN);
            QueueCmdIgnoreRsp(APIID_NONE, szNextCmd, CMDOPT_INIT, g_TimeoutCmdInit, NULL, NULL, 0, 0, 0);
        }
    }    

    if (g_fOperatorSelected && (OldRadioOff == FALSE))
    {
        // if we just rebooted and were previously registered, then reregister on the network
        QueueCmdIgnoreRsp(APIID_NONE, g_szOpSelCmd, CMDOPT_INIT, g_TimeoutCmdInit, NULL, NULL, 0, 0, 0);
    }

    // Restore audio gain
    (void)_snprintfz(szNextCmd, MAX_PATH, "AT+CLVL=%u\r", g_dwCacheDownlinkVolume);
    QueueCmdIgnoreRsp(APIID_NONE, szNextCmd, CMDOPT_NONE, g_TimeoutCmdInit, NULL, NULL, 0, 0, 0);

    // Restore PB location
    if (g_fPBLocationSet)
    {
        QueueCmdIgnoreRsp(APIID_NONE, g_szPBLocCmd, CMDOPT_INIT, g_TimeoutCmdInit, NULL, NULL, 0, 0, 0);
    } 
    fSuccess = TRUE;

    LeaveCriticalSection(&g_csReboot);
    return fSuccess;
}

HRESULT RILDrv_RebootRadio (DWORD dwParam)
{
    FUNCTION_TRACE(RILDrv_RebootRadio);
    DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : RILDrv_RebootRadio\r\n")));

    HRESULT hr = E_INVALIDARG;
    CRilInstanceHandle* pHandle = ExtractHandle(dwParam);
    if (!pHandle)
    {
        goto Error;
    }

    if (!RebootRadio())
    {
        hr = E_NOTIMPL;
        goto Error;
    }

    // send an OK response
    if (!QueueCmd(pHandle, NULL, CMDOPT_NOOP, APIID_REBOOTRADIO, NULL, NULL, hr)) 
    {
        hr = E_FAIL;
        goto Error;
    }

Error:
    return hr;
}

#endif // RIL_RADIO_RESILIENCE

⌨️ 快捷键说明

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