📄 util.cpp
字号:
}
//
// Append data to the response
//
BOOL CBuffer::Append(const LPCSTR szString, const UINT cbString)
{
// FUNCTION_TRACE(CBuffer::Append);
LPSTR szTemp = NULL;
BOOL fRet = FALSE;
UINT SafeAdd = 0;
if (!m_szData)
{
m_cbLength = 0;
m_cbAlloc = ALLOC_SIZE;
m_szData = new char[m_cbAlloc];
if (!m_szData)
{
goto Error;
}
m_szData[ALLOC_SIZE-1]=0;
}
if ( FALSE == safeIntUAddThree(m_cbLength, cbString, 1, &SafeAdd))
{
goto Error;
}
if (SafeAdd > m_cbAlloc)
{
do
{
if ( FALSE == safeIntUAdd(m_cbAlloc, ALLOC_SIZE, &m_cbAlloc))
{
goto Error;
}
} while (SafeAdd > m_cbAlloc);
szTemp = new char[m_cbAlloc];
if (!szTemp)
{
goto Error;
}
szTemp[m_cbAlloc-1] = 0;
memcpy(szTemp, m_szData, m_cbLength);
delete[] m_szData;
m_szData = szTemp;
}
memcpy(m_szData + m_cbLength, szString, cbString);
m_cbLength += cbString;
fRet = TRUE;
Error:
return fRet;
}
//
// Get the data out of the buffer
// (the caller takes ownership of the m_szData storage)
//
LPSTR CBuffer::GiveUpData()
{
// FUNCTION_TRACE(CBuffer::GiveUpData);
LPSTR szTemp = m_szData;
m_szData = NULL;
m_cbLength = 0;
m_cbAlloc = 0;
return szTemp;
}
//
//
//
void CBuffer::InheritData(CBuffer* pSrcBuffer)
{
// FUNCTION_TRACE(CBuffer::InheritData);
delete[] m_szData;
m_szData = NULL;
m_cbLength = 0;
m_cbAlloc = 0;
m_cbAlloc = pSrcBuffer->m_cbAlloc;
m_cbLength = pSrcBuffer->m_cbLength;
m_szData = pSrcBuffer->GiveUpData();
}
//
// Shared resource ctor
//
CSharedResource::CSharedResource()
: m_fInited(FALSE),
m_dwSharedUsers(0),
m_hSharedUseSph(NULL),
m_hExclusiveUseSph(NULL),
m_hCancelEvent(NULL)
{
FUNCTION_TRACE(CSharedResource::CSharedResource);
InitializeCriticalSection(&m_cs);
}
//
// Shared resource dtor
//
CSharedResource::~CSharedResource()
{
FUNCTION_TRACE(CSharedResource::~CSharedResource);
if (m_hSharedUseSph)
{
(void)CloseHandle(m_hSharedUseSph);
m_hSharedUseSph = NULL;
}
if (m_hExclusiveUseSph)
{
(void)CloseHandle(m_hExclusiveUseSph);
m_hExclusiveUseSph = NULL;
}
DeleteCriticalSection(&m_cs);
m_fInited = FALSE;
}
//
// Shared resource intialization
//
BOOL CSharedResource::Init(const HANDLE hCancelEvent)
{
FUNCTION_TRACE(CSharedResource::Init);
if (m_fInited)
{
goto Error;
}
m_hSharedUseSph = CreateSemaphore(NULL, 1, 1, NULL);
if (!m_hSharedUseSph)
{
goto Error;
}
m_hExclusiveUseSph = CreateSemaphore(NULL, 1, 1, NULL);
if (!m_hExclusiveUseSph)
{
goto Error;
}
m_hCancelEvent = hCancelEvent;
m_fInited = TRUE;
Error:
return m_fInited;
}
//
// Called to acquire shared use permission
//
BOOL CSharedResource::EnterSharedUse()
{
FUNCTION_TRACE(CSharedResource::EnterSharedUse);
HANDLE rghObjects[2] = { m_hSharedUseSph, m_hCancelEvent};
DWORD dwWait;
BOOL fRet = FALSE;
if (!m_fInited)
{
goto Error;
}
// Wait until shared use is granted
dwWait = WaitForMultipleObjects(2, rghObjects, FALSE, INFINITE);
if (WAIT_OBJECT_0 + 1 == dwWait)
{
// We hit the terminate event -- quit
goto Error;
}
else if (WAIT_OBJECT_0 != dwWait)
{
DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : WaitForMultipleObjects : Failed, RetVal=0x%x, GetLastError=0x%x\r\n"),dwWait,GetLastError()));
DEBUGCHK(WAIT_FAILED == dwWait);
goto Error;
}
{
SYNCBLOCK(m_cs);
// Grant shared use for anyone who's waiting
if (!ReleaseSemaphore(m_hSharedUseSph, 1, NULL))
{
goto Error;
}
// Increment the number of shared users
m_dwSharedUsers++;
// If this is the first shared user, we need to make sure there are no exclusive users at the moment
if (1 == m_dwSharedUsers)
{
// Wait until the exclusive user is done
rghObjects[0] = m_hExclusiveUseSph;
dwWait = WaitForMultipleObjects(2, rghObjects, FALSE, INFINITE);
if (WAIT_OBJECT_0 + 1 == dwWait)
{
// We hit the terminate event -- quit
goto Error;
}
else if (WAIT_OBJECT_0 != dwWait)
{
DEBUGCHK(FALSE);
goto Error;
}
}
}
fRet = TRUE;
Error:
return fRet;
}
//
// Called to relinquish shared use permission
//
BOOL CSharedResource::ExitSharedUse()
{
FUNCTION_TRACE(CSharedResource::ExitSharedUse);
SYNCBLOCK(m_cs);
BOOL fRet = FALSE;
if (!m_fInited)
{
goto Error;
}
// Decrement the number of shared users
m_dwSharedUsers--;
// If this is the last shared user, allow exclusive use
if (!m_dwSharedUsers)
{
if (!ReleaseSemaphore(m_hExclusiveUseSph, 1, NULL))
{
goto Error;
}
}
fRet = TRUE;
Error:
return fRet;
}
//
// Called to acquire exclusive use permission
//
BOOL CSharedResource::EnterExclusiveUse(PFN_SHRDUSE_ACTION pfnAction, const DWORD dwParam) const
{
FUNCTION_TRACE(CSharedResource::EnterExclusiveUse);
HANDLE rghObjects[2] = { m_hSharedUseSph, m_hCancelEvent};
DWORD dwWait;
BOOL fRet = FALSE;
int RetryCount = 10;
if (!m_fInited)
{
goto Error;
}
// Lock shared users out until this exclusive user can finish its operation
dwWait = WaitForMultipleObjects(2, rghObjects, FALSE, INFINITE);
if (WAIT_OBJECT_0 + 1 == dwWait)
{
// We hit the terminate event -- quit
goto Error;
}
else if (WAIT_OBJECT_0 != dwWait)
{
DEBUGCHK(FALSE);
goto Error;
}
for (;;)
{
// Call the custom action routine, passing it the specified parameter
pfnAction(dwParam);
// Wait until there are no other exclusive users
rghObjects[0] = m_hExclusiveUseSph;
dwWait = WaitForMultipleObjects(2, rghObjects, FALSE, 500);
if (WAIT_OBJECT_0 == dwWait)
{
// We own it now.
break;
}
if (WAIT_OBJECT_0 + 1 == dwWait)
{
// We hit the terminate event -- quit
goto Error;
}
else if (WAIT_TIMEOUT == dwWait)
{
// We timed out.
// Have we exceeded our retry count?
if (--RetryCount<=0)
{
goto Error;
}
// Try to signal and wait again.
}
else
{
// Some other error
DEBUGCHK(FALSE);
goto Error;
}
}
// Allow shared users to proceed (they will be locked out until there're no more exclusive users)
if (!ReleaseSemaphore(m_hSharedUseSph, 1, NULL))
{
goto Error;
}
fRet = TRUE;
Error:
return fRet;
}
//
// Called to relinquish exclusive use permission
//
BOOL CSharedResource::ExitExclusiveUse() const
{
FUNCTION_TRACE(CSharedResource::ExitExclusiveUse);
// Allow other exclusive users and shared users to proceed
return ReleaseSemaphore(m_hExclusiveUseSph, 1, NULL);
}
extern BOOL g_bRadioOff;
extern RILCALLINFO g_rgfCallStates[RIL_MAX_TRACKED_CALL_ID];
void IndicateCallActivityToAudioSubsystem(BOOL fActive, BOOL fCheckCallList)
{
HRESULT hr = E_FAIL;
if (fCheckCallList)
{
if (fActive)
{
DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : IndicateCallActivityToAudioSubsystem : fCheckCallList cannot be TRUE when fActive is TRUE\r\n")));
ASSERT(0);
return;
}
else
{
// Check the call list before indicating no calls are active
DWORD dwIndexCount;
BOOL fFoundActiveCall = FALSE;
// Look for an active call
for ( dwIndexCount = 0; dwIndexCount < RIL_MAX_TRACKED_CALL_ID; dwIndexCount++ )
{
// Valid entries set the cbSize
if ( g_rgfCallStates[dwIndexCount].cbSize == sizeof(RILCALLINFO))
{
// If ci_cpistatus is set the call could be connected or disconnected
// If ci_cpistatus is not set, the call is active by default (clcc processing)
if ( g_rgfCallStates[dwIndexCount].dwParams & RIL_PARAM_CI_CPISTATUS )
{
// If dwStatus is not disconnected, the call is considered active.
if ( g_rgfCallStates[dwIndexCount].dwStatus != RIL_CPISTAT_DISCONNECTED )
{
fFoundActiveCall = TRUE;
break;
}
}
else
{
fFoundActiveCall = TRUE;
break;
}
}
}
if (!fFoundActiveCall)
{
//Signal audio driver that there are no active calls
hr = PDD_IndicateCallActivityToAudioSubsystem(FALSE);
if ( FAILED( hr ) && E_NOTIMPL != hr )
{
DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : PDD_IndicateCallActivityToAudioSubsystem failed , hr = [0x%08x]\r\n"), hr));
}
}
}
}
else
{
// Don't check the call list
if (fActive)
{
//Signal audio driver that at least one call is active, if the radio is not off
if (!g_bRadioOff)
{
hr = PDD_IndicateCallActivityToAudioSubsystem(TRUE);
if ( FAILED( hr ) && E_NOTIMPL != hr )
{
DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : PDD_IndicateCallActivityToAudioSubsystem failed , hr = [0x%08x]\r\n"), hr));
}
}
}
else
{
//Signal audio driver that there are no active calls
hr = PDD_IndicateCallActivityToAudioSubsystem(FALSE);
if ( FAILED( hr ) && E_NOTIMPL != hr )
{
DEBUGMSG(ZONE_ERROR, (TEXT("RILDrv : E : PDD_IndicateCallActivityToAudioSubsystem failed , hr = [0x%08x]\r\n"), hr));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -