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

📄 util.h

📁 ril source code for Windows CE
💻 H
📖 第 1 页 / 共 3 页
字号:
//
// 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.
//
/*++
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.

Module Name:

util.h

Abstract:


Notes:


--*/

#pragma once

#include "rildebug.h"
#include "FunctionTrace.h"

//
// Character mapping structure used for binary search
//
struct CHARMAP {
    char ch;
    WCHAR wch;
};

//
// Encoding types
//
enum ENCODING_TYPE {
    ENCODING_GSMDEFAULT = 0,// GSM Default alphabet
    ENCODING_GSMDEFAULT_HEX, // GSM Default alphabet, HEX encoded
    ENCODING_GSMDEFAULT_UNPACKED, // GSM Default alphabet, 7 bit packed see GSM 07.07 5.5
    ENCODING_GSMDEFAULT_HEX_UNPACKED, // GSM Default alphabet, HEX encoded, not 7 bit packed see GSM 07.07 5.5
    ENCODING_GSMDEFAULT_UNICODE, // Unicode alphabet, HEX encoded
    ENCODING_GSMDEFAULT_UTF8, // Unicode alphabet, UTF-8 encoded
    // Others to be added later
};

//
// Function declarations
//
BOOL UnicodeCharToGSM(const WCHAR wch, char *pchRet);
BOOL GSMCharToUnicode(const char ch, WCHAR *pwchRet);
char SemiByteToChar(const BYTE bByte, const BOOL fHigh);
BYTE SemiByteCharsToByte(const char chHigh, const char chLow);
BOOL GSMHexToGSM(const LPCSTR sIn, const UINT cbIn, __out_bcount( cbOut ) const LPSTR sOut, const UINT cbOut, UINT& rcbUsed);
BOOL GSMToGSMHex(const LPCSTR sIn, const UINT cbIn, __out_bcount( cbOut ) const LPSTR sOut, const UINT cbOut, UINT& rcbUsed);
BOOL ConvertFromUnicode(const ENCODING_TYPE enc, const LPCWSTR wsIn, const UINT cchIn, __out_bcount( cbOut ) const LPSTR sOut, const UINT cbOut,
                        UINT& rcbUsed);
BOOL ConvertToUnicode(const ENCODING_TYPE enc, const LPCSTR sIn, const UINT cbIn, __out_ecount( cchOut ) const LPWSTR wsOut, const UINT cchOut,
                      UINT& rcchUsed);
BOOL ConvertTECharSetToUnicode(const LPCSTR sIn, const UINT cchIn,
                               const LPWSTR wsOut, const UINT cchOut,
                               UINT * pcchUsed);
BOOL ConvertUnicodeToTECharSet(const LPWSTR wsIn, const UINT cchIn,
                               const LPSTR sOut, const UINT cchOut,
                               UINT * pcchUsed);
HRESULT RILAddressToString(const RILADDRESS& rraAddress, __out_bcount( cbOut ) const LPSTR szOut, const UINT cbOut, BYTE& rbTypeOfAddress, ENCODING_TYPE etEncoding = ENCODING_GSMDEFAULT_HEX);
BOOL StringToRILAddress(const LPCSTR szAddress, const BYTE bTypeOfAddress, RILADDRESS& rraAddress, ENCODING_TYPE etEncoding = ENCODING_GSMDEFAULT_HEX);
HRESULT RILSubAddressToString(const RILSUBADDRESS& rrsaSubAddress, __out_ecount( cchOut ) const LPSTR szOut, const UINT cchOut, BYTE& rbType);
BOOL StringToRILSubAddress(const LPCSTR szSubAddress, const BYTE bType, RILSUBADDRESS& rrsaSubAddress);
HRESULT DetermineSimResponseError(DWORD dwSW1, DWORD dwSW2);
BOOL IsElementarySimFile(DWORD dwFileID);
BOOL ComposeCmdWithByteArray(const LPCSTR szPrefix, const BYTE* const pbBytes, const UINT cbBytes,
                             const LPCSTR szPostfix, LPSTR& rszCmd);
char * BeginLineSpecificCommand( __out_ecount( cchSize ) char *szCmdDst, UINT cchSize, DWORD dwAddressId);
BOOL AllocateOrReallocateStorage(BYTE** const prgrData, const size_t stSize, const UINT nUsed, UINT* const pnAllocated,
                                 const UINT nGrowSize);
void UpdateSIMState(DWORD dwSimState);
HRESULT StringFilterW( __out_ecount( cchDest ) LPWSTR wszDest, size_t cchDest, const WCHAR *wszSrc, const WCHAR *wszFilter);

#ifdef GPRS_CONTEXT_CACHING
void UpdateGPRSContextCommandCache( LPCSTR szCmd );
void ClearGPRSContextCommandCache();
bool IsGPRSContextCommandCached( LPCSTR szCmd );
#endif GPRS_CONTEXT_CACHING
#ifdef RIL_RADIO_RESILIENCE
void SavePINSecure(LPCSTR szNewPIN);
BOOL FetchPINSecure(LPSTR szPinBuffer, DWORD cbBufferSIze);
#endif // RIL_RADIO_RESILIENCE
void IndicateCallActivityToAudioSubsystem(BOOL fActive, BOOL fCheckCallList);
//
// Class declarations
//

// Growable character buffer
class CBuffer
{
public:
            CBuffer();
            virtual ~CBuffer();

    BOOL    Append(const LPCSTR szString, const UINT cbString);
    LPSTR   GetData() const     { return m_szData; };
    UINT    GetLength() const   { return m_cbLength; };
    LPSTR   GiveUpData();
    void    InheritData(CBuffer* pSrcBuffer);

protected:
    LPSTR   m_szData;
    UINT    m_cbLength;
    UINT    m_cbAlloc;
};


// Doubly-linked list element
class CListElem
{
public:
                CListElem() : m_pNext(NULL), m_pPrev(NULL) {};
                ~CListElem() {};

    CListElem*  GetNext() const         { return m_pNext; };
    CListElem*  GetPrev() const         { return m_pPrev; };

    void        SetNext(CListElem* p)   { m_pNext = p; };
    void        SetPrev(CListElem* p)   { m_pPrev = p; };

private:
    CListElem*  m_pNext;
    CListElem*  m_pPrev;
};


// Callback used when entering exclusive use mode
typedef void (*PFN_SHRDUSE_ACTION)(DWORD dwParam);


// Shared resource with readers/writers synchronization
class CSharedResource
{
public:
            CSharedResource();
            ~CSharedResource();

    BOOL    Init(const HANDLE hCancelEvent);
    BOOL    EnterSharedUse();
    BOOL    ExitSharedUse();
    BOOL    EnterExclusiveUse(PFN_SHRDUSE_ACTION pfnAction, const DWORD dwParam) const;
    BOOL    ExitExclusiveUse() const;

private:
    BOOL                m_fInited;
    CRITICAL_SECTION    m_cs;
    DWORD               m_dwSharedUsers;
    HANDLE              m_hSharedUseSph;
    HANDLE              m_hExclusiveUseSph;
    HANDLE              m_hCancelEvent;
};


// Function to be passed to CQueue::Enum().
//    This function should return TRUE for enumeration to stop.
typedef BOOL (*PFN_QUEUE_ENUM)(void* pItem, DWORD dwData);

// Function to be passed to CQueue::ConditionalGet().
//    This function should return TRUE for for the item to be removed from the queue.
typedef BOOL (*PFN_QUEUE_TEST)(void* pItem, DWORD dwData);

// Function to be used to destroy items in the queue
// when the queue is destroyed.
typedef void (*PFN_QUEUE_ITEMDTOR)(void *pItem);

template <class Type, UINT Size>
class CBoundedQueue
{
private:
    CEvent              m_eventSpace;
    CUniquelyNamedEvent m_eventItems;
    Type *              m_rgpItems[Size];
    UINT                m_nUsed;
    PFN_QUEUE_ITEMDTOR  m_pfnItemDtor;
    CRITICAL_SECTION    m_cs;

public:
            CBoundedQueue(PFN_QUEUE_ITEMDTOR pfnItemDtor = NULL);
    virtual ~CBoundedQueue();

    BOOL    Ready() const { return m_eventSpace.Ready() && m_eventItems.Ready(); }
    BOOL    Init();
    BOOL    Put(Type* const pItem);
    BOOL    Get(Type** ppItem);
    BOOL    Peek(Type** ppItem);
    HRESULT ConditionalGet(const PFN_QUEUE_TEST pfnTest, const DWORD dwData, Type** rpItem);

    const
    TCHAR * GetItemsEventName() { return m_eventItems.GetName(); }
    HANDLE  GetItemsEvent() { return m_eventItems.GetHandle(); }
    HANDLE  GetSpaceEvent() { return m_eventSpace.GetHandle(); }
};

template <class Type, UINT Size>
CBoundedQueue<Type, Size>::CBoundedQueue(PFN_QUEUE_ITEMDTOR pfnItemDtor)
:   m_nUsed(0),
    m_pfnItemDtor(pfnItemDtor)
{
    InitializeCriticalSection(&m_cs);

    for (UINT i = 0; i < Size; i++)
        m_rgpItems[i] = NULL;
}

template <class Type, UINT Size>
CBoundedQueue<Type, Size>::~CBoundedQueue()
{
    // In case someone is still waiting on
    // this queue, try to avoid conflicts...
    if (m_eventItems.Ready())
        m_eventItems.Reset();   // no items to dequeue
    if (m_eventSpace.Ready())
        m_eventSpace.Reset();   // no space for enqueue

    if (m_pfnItemDtor)
    {
        for (UINT i = 0; i < m_nUsed; i++)
        {
            m_pfnItemDtor(m_rgpItems[i]);
            m_rgpItems[i] = NULL;
        }
    }

    m_nUsed = 0;

    DeleteCriticalSection(&m_cs);
}

template <class Type, UINT Size>
BOOL CBoundedQueue<Type, Size>::Init()
{
    return
        m_eventSpace.Init(TRUE, TRUE) &&
        m_eventItems.Init(TRUE, FALSE, TEXT("CBoundedQueue_"));
}

template <class Type, UINT Size>
BOOL CBoundedQueue<Type, Size>::Put(Type * const pItem)
{
    ASSERT(Ready());

    SYNCBLOCK(m_cs);

    if (m_nUsed < Size)
    {
        // Put the element in our queue.
        m_rgpItems[m_nUsed++] = pItem;

        // If this filled the queue buffer,
        // then reset the space avail event.
        if (m_nUsed == Size)
            m_eventSpace.Reset();

        // There's something new in the queue,
        // so signal waiting threads.
        m_eventItems.Set();

        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

template <class Type, UINT Size>
BOOL CBoundedQueue<Type, Size>::Get(Type ** ppItem)
{
    return ConditionalGet(NULL, 0, ppItem);
}

template <class Type, UINT Size>
BOOL CBoundedQueue<Type, Size>::Peek(Type ** ppItem)
{
    ASSERT(Ready());

    ASSERT(ppItem);
    if (!ppItem)
        return FALSE;

    SYNCBLOCK(m_cs);

    if (m_nUsed > 0)
    {
        // Copy the element from the queue.
        *ppItem = m_rgpItems[0];

        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

template <class Type, UINT Size>
HRESULT CBoundedQueue<Type, Size>::ConditionalGet(const PFN_QUEUE_TEST pfnTest,
                                                  const DWORD dwData,
                                                  Type** ppItem)
{
    ASSERT(Ready());

    ASSERT(ppItem);
    if (!ppItem)
        return S_FALSE;

    SYNCBLOCK(m_cs);

    if (m_nUsed > 0)
    {
        // Copy the element from the queue.
        Type * pItem = m_rgpItems[0];

        // If there is a condition, make sure
        // it is satisfied.
        if (pfnTest && !pfnTest(pItem, dwData))
            return E_FAIL;

        // One less in queue now...
        m_nUsed--;

        if (m_nUsed)
        {
            // Slide remaining items over to the beginning of the buffer.
            memmove(m_rgpItems, (BYTE*)m_rgpItems + sizeof(Type*), sizeof(Type*) * m_nUsed);

            // If we just made space, then set this event.
            if (Size - 1 == m_nUsed)
                m_eventSpace.Set();
        }
        else
        {
            // The last item was copied from the queue,
            // so reset this event.
            m_eventItems.Reset();
        }

        // NULL out the space that was freed up.
        m_rgpItems[m_nUsed] = NULL;

        // Set the output parameter.
        *ppItem = pItem;

        return S_OK;
    }
    else
    {
        return S_FALSE;
    }
}

template <class Type>
class CUnboundedQueue
{
private:
    CUniquelyNamedEvent m_eventItems;
    Type **             m_rgpItems;
    DWORD               m_dwInitialSize;
    DWORD               m_dwCurrentSize;
    UINT                m_nUsed;
    PFN_QUEUE_ITEMDTOR  m_pfnItemDtor;
    CRITICAL_SECTION    m_cs;

protected:
    BOOL    GrowQueue();

public:
            CUnboundedQueue(PFN_QUEUE_ITEMDTOR pfnItemDtor = NULL);
    virtual ~CUnboundedQueue();

    BOOL    Ready() const { return m_rgpItems != NULL && m_eventItems.Ready(); }
    BOOL    Init(DWORD dwInitialSize);
    BOOL    Put(Type* const pItem);
    BOOL    Get(Type** ppItem);
    BOOL    Peek(Type** ppItem);
    HRESULT ConditionalGet(const PFN_QUEUE_TEST pfnTest, const DWORD dwData, Type** rpItem);

    const
    TCHAR * GetItemsEventName() { return m_eventItems.GetName(); }
    HANDLE  GetItemsEvent() { return m_eventItems.GetHandle(); }

    UINT    GetSize() { return m_nUsed; }
    DWORD   GetCurrentBufferSize() { return m_dwCurrentSize; }
};

template <class Type>
CUnboundedQueue<Type>::CUnboundedQueue(PFN_QUEUE_ITEMDTOR pfnItemDtor)
:   m_nUsed(0),
    m_dwInitialSize(0),
    m_dwCurrentSize(0),
    m_rgpItems(NULL),
    m_pfnItemDtor(pfnItemDtor)
{
    InitializeCriticalSection(&m_cs);
}

template <class Type>

⌨️ 快捷键说明

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