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

📄 rilsimtkitbyte.cpp

📁 手机RILGSM实现的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:

/****************************************************************************

    FUNCTION:   ReadCommonByteList

    PURPOSE:    Reads a list of bytes

    PARAMETERS: ppbData - Pointer to the Bytes (we allocate it)
                pdwLen - The count of bytes

    RETURNS:    HRESULT

****************************************************************************/

HRESULT CRilSimToolkitCommand::ReadCommonByteList(BYTE **ppbData, DWORD *pdwLen)
{
    HRESULT hr = S_OK;
    DWORD dwLength;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: +CRilSimToolkitCommand::ReadCommonByteList\r\n")));

    ASSERT(*ppbData == NULL);

    // OK, read in the length
    hr = ReadCmdLength(&dwLength);
    if (FAILED(hr))
    {
        goto Exit;
    }

    // OK, we should have this much space left
    if (m_dwParseLen < dwLength)
    {
        // Nope, not enough space left
        DEBUGMSG(ZONE_ERROR, (TEXT("RilDrv : SIMTKit: Expected %d bytes, only have %d\r\n"), dwLength, m_dwParseLen));
        hr = E_FAIL;
        goto Exit;
    }

    *ppbData = new BYTE[dwLength];
    if (!(*ppbData))
    {
        hr = E_OUTOFMEMORY;
        goto Exit;
    }

    memcpy(*ppbData, m_lpbParse, dwLength);
    *pdwLen = dwLength;
    m_lpbParse += dwLength;
    m_dwParseLen -= dwLength;

Exit:
    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: -CRilSimToolkitCommand::ReadCommonByteList\r\n")));
    return hr;
}


/****************************************************************************

    FUNCTION:   ReadCommonByteAndByteList

    PURPOSE:    Reads in a length, followed by a byte of data, followed
                by a bytelist that is length-1.

    PARAMETERS: pbData - Pointer to the Bytes (we allocate it)
                ppbData - Pointer to the Bytes (we allocate it)
                pdwLen - The count of bytes

    RETURNS:    HRESULT

****************************************************************************/

HRESULT CRilSimToolkitCommand::ReadCommonByteAndByteList(DWORD* pdwByte, BYTE **ppbData, DWORD *pdwLen)
{
    HRESULT hr = S_OK;
    DWORD dwLength;
    DWORD dwByteListLen;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: +CRilSimToolkitCommand::ReadCommonByteAndByteList\r\n")));

    // OK, read in the length
    hr = ReadCmdLength(&dwLength);
    if (FAILED(hr))
    {
        goto Exit;
    }

    // OK, we should have this much space left
    if (m_dwParseLen < dwLength)
    {
        // Nope, not enough space left
        DEBUGMSG(ZONE_ERROR, (TEXT("RilDrv : SIMTKit: Expected %d bytes, only have %d\r\n"), dwLength, m_dwParseLen));
        hr = E_FAIL;
        goto Exit;
    }

    // if length of tag is 0, there is nothing to parse.
    if (0 == dwLength)
    {
        goto Exit;
    }

    // Next byte is the value
    *pdwByte = (*(m_lpbParse++));

    // Size of the byte list is length - 1
    dwByteListLen = dwLength - 1;
    *ppbData = new BYTE[dwByteListLen];
    if (!(*ppbData))
    {
        hr = E_OUTOFMEMORY;
        goto Exit;
    }

    memcpy(*ppbData, m_lpbParse, dwByteListLen);
    *pdwLen= dwByteListLen;
    m_lpbParse += dwByteListLen;
    m_dwParseLen -= dwLength;

Exit:
    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: -CRilSimToolkitCommand::ReadCommonByteAndByteList\r\n")));

    return hr;
}

/****************************************************************************

    FUNCTION:   ReadMenuItem

    PURPOSE:    Reads menu items in, setting m_rgpsmi and m_numpsmi

    PARAMETERS: ppData - Pointer to pointer to data
                pdwSpaceLeft - How much space is left, in bytes
                ppsmi - Pointer to pointer to SIMMENUITEM

    RETURNS:    HRESULT

****************************************************************************/

HRESULT CRilSimToolkitCommand::ReadMenuItem()
{
    HRESULT hr = S_OK;
    DWORD dwIdentifier, dwUsedLen, dwLen, dwSize;
    TCHAR *pwszText = NULL;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: +CRilSimToolkitCommand::ReadMenuItem\r\n")));

    ASSERT((m_rgpsmi == NULL) && (m_numpsmi == 0));

    // Let's go through and read items in -- note that the at least one is MANDATORY, all
    // additional menu items are completely optional.  We should first allocate memory
    m_rgpsmi = new LPSIMMENUITEM[10];
    if (!m_rgpsmi)
    {
        hr = E_OUTOFMEMORY;
        goto Exit;
    }

    memset(m_rgpsmi, 0, sizeof(LPSIMMENUITEM)  * 10);

    for (;;)
    {
        // Now, figure out the length of the text and identifier
        hr = ReadCmdLength(&dwLen);
        if (FAILED(hr))
        {
            DEBUGMSG(ZONE_ERROR, (TEXT("RilDrv : SIMTKit: ReadCmdLength failed %x\r\n"), hr));
            goto Exit;
        }

        if (0 == dwLen && 0 == m_numpsmi)
        {
            // The first menu item is empty. We're done.
            goto Exit;
        }

        // Are there enough characters left?
        if (dwLen < 2)
        {
            DEBUGMSG(ZONE_ERROR, (TEXT("RilDrv : SIMTKit: Not even 2 characters left, only %d\r\n"), dwLen));
            hr = E_FAIL;
            goto Exit;
        }

        // OK, let's get the text, it's encoded as is EFADN
        dwIdentifier = (DWORD) *(m_lpbParse++);
        m_dwParseLen--;
        delete pwszText;
        pwszText = NULL;
        hr = ReadEFADNText(&pwszText, (dwLen - 1), &dwUsedLen);
        if (FAILED(hr))
        {
            DEBUGMSG(ZONE_ERROR, (TEXT("RilDrv : SIMTKit: ReadEFADNText failed %x\r\n"), hr));
            goto Exit;
        }

        // OK, let's store this one -- do we need more memory for our array?
        if (m_numpsmi && ((m_numpsmi % 10) == 0))
        {
            // Yep, re-allocate
            LPSIMMENUITEM *psmiTemp = new LPSIMMENUITEM[m_numpsmi + 10];

            if (!psmiTemp)
            {
                hr = E_OUTOFMEMORY;
                goto Exit;
            }

            memcpy(psmiTemp, m_rgpsmi, m_numpsmi * sizeof(LPSIMMENUITEM));
            delete[] m_rgpsmi;
            m_rgpsmi = psmiTemp;
        }

        // Now we can allocate memory -- note that dwLen is the length of the text plus 1
        dwSize = ROUNDTODWORD(sizeof(SIMMENUITEM)) + ROUNDTODWORD(dwUsedLen);
        m_rgpsmi[m_numpsmi] = NULL;
        m_rgpsmi[m_numpsmi] = (SIMMENUITEM *) new BYTE[dwSize];

        if (NULL != m_rgpsmi[m_numpsmi])
        {
            memset(m_rgpsmi[m_numpsmi], 0, dwSize);
            m_rgpsmi[m_numpsmi]->cbSize = dwSize;
            m_rgpsmi[m_numpsmi]->dwParams = SIM_PARAM_SIMMENUITEM_IDENTIFIER | SIM_PARAM_SIMMENUITEM_TEXTOFFSET |
                                    SIM_PARAM_SIMMENUITEM_TEXTSIZE;
            m_rgpsmi[m_numpsmi]->dwIdentifier = dwIdentifier;
            m_rgpsmi[m_numpsmi]->dwTextSize = dwUsedLen;
            m_rgpsmi[m_numpsmi]->dwTextOffset = ROUNDTODWORD(sizeof(SIMMENUITEM));
            lstrcpy((TCHAR *) ((LPBYTE) m_rgpsmi[m_numpsmi] + m_rgpsmi[m_numpsmi]->dwTextOffset), pwszText);

            // Great, we added this menu item, so increase m_numpsmi
            m_numpsmi++;
        }

        // OK, is there another menu item in the pipe line?
        if (m_dwParseLen && (((*m_lpbParse) & 0x7f) == 0x0f))
        {
            // Yes, there is -- read it in and stay in the loop
            m_lpbParse++;
            m_dwParseLen--;
        }
        else
        {
            // Nope, that's it for menu items
            break;
        }
    }

Exit:
    if (FAILED(hr))
    {
        if (NULL != m_rgpsmi)
        {
            // Free memory
            for (UINT i = 0; i < m_numpsmi; i++)
            {
                if (NULL != m_rgpsmi[i])
                {
                    delete[] (BYTE*)m_rgpsmi[i];
                    m_rgpsmi[i] = NULL;
                }
            }
            delete[] m_rgpsmi;
            m_rgpsmi = NULL;
        }
        m_numpsmi = 0;
    }

    delete pwszText;
    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: -CRilSimToolkitCommand::ReadMenuItem\r\n")));
    return hr;
}

/****************************************************************************

    FUNCTION:   ReadNextAction

    PURPOSE:    Reads the next action for a menu item, assumes that m_rgpsmi
                    and m_numpsmi have already been set

    PARAMETERS: None

    RETURNS:    HRESULT

****************************************************************************/

HRESULT CRilSimToolkitCommand::ReadNextAction()
{
    HRESULT hr = S_OK;
    DWORD dwLength, i;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: +CRilSimToolkitCommand::ReadNextAction\r\n")));

    ASSERT((m_rgpsmi != NULL) && (m_numpsmi > 0));

    // See how many next action items there are
    dwLength = (DWORD) (*(m_lpbParse++));
    m_dwParseLen--;

    if ((dwLength > m_numpsmi) || (m_dwParseLen < dwLength))
    {
        // Nope, this is no good
        DEBUGMSG(ZONE_ERROR, (TEXT("RilDrv : SIMTKit: Length %d doesn't fit between %d and %d\r\n"), dwLength, m_numpsmi, m_dwParseLen));
        hr = E_FAIL;
        goto Exit;
    }

    // OK, go through and set them, please
    for (i = 0; i < dwLength; i++)
    {
        m_rgpsmi[i]->dwParams |= SIM_PARAM_SIMMENUITEM_NEXTACTION;
        m_rgpsmi[i]->dwNextAction = (DWORD) (*(m_lpbParse++));
    }

Exit:
    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: -CRilSimToolkitCommand::ReadNextAction\r\n")));
    return hr;
}

/****************************************************************************

    FUNCTION:   ReadDuration

    PURPOSE:    Reads the duration from a command, setting m_dwDuration

    PARAMETERS: None

    RETURNS:    HRESULT

****************************************************************************/

HRESULT CRilSimToolkitCommand::ReadDuration()
{
    HRESULT hr = S_OK;
    DWORD dwUnits;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: +CRilSimToolkitCommand::ReadDuration\r\n")));

    ASSERT(m_dwDuration == SIMTONE_DEFAULTDURATION);

    // Note, we will now read 3 more bytes, so we'd better have it!
    if (m_dwParseLen < 3)
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("RilDrv : SIMTKit: Need 3 bytes, only have %d\r\n"), m_dwParseLen));
        hr = E_FAIL;
        goto Exit;
    }

    // Second byte is length, which should be 2
    if (*(m_lpbParse++) != 2)
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("RilDrv : SIMTKit: Second byte should be 2, is %x\r\n"), *(m_lpbParse - 1)));
        hr = E_FAIL;
        goto Exit;
    }

    // Third byte is time unit, 0 = minutes, 1 = seconds, 2 = 0.1 seconds
    // Fourth byte is the value
    dwUnits = (*(m_lpbParse++));
    m_dwDuration = (*(m_lpbParse++));
    switch (dwUnits)
    {
        case SIMTKIT_POLLINTERVAL_UNIT_DURATIONMINUTES:
            m_dwDuration *= 60000;
            break;

        case SIMTKIT_POLLINTERVAL_UNIT_DURATIONSECONDS:
            m_dwDuration *= 1000;
            break;

        case SIMTKIT_POLLINTERVAL_UNIT_DURATIONTENTHS:
            m_dwDuration *= 100;
            break;

        default:
            // Illegal value
            hr = E_FAIL;
            goto Exit;
            break;
    }
    m_dwParseLen -= 3;
    DEBUGMSG(ZONE_INFO, (TEXT("RilDrv : SIMTKit: Duration %d, remaining length %d\r\n"), m_dwDuration, m_dwParseLen));

Exit:
    DEBUGMSG(ZONE_FUNCTION, (TEXT("RilDrv : SIMTKit: -CRilSimToolkitCommand::ReadDuration\r\n")));

    return hr;
}

/****************************************************************************

    FUNCTION:   ReadResponseLength

    PURPOSE:    Reads the response length from a

⌨️ 快捷键说明

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