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

📄 dialengine.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
📖 第 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.
//
#include "DialEngine.hpp"
#include "DialPlanParser.hpp"
#include "CommonFunctions.hpp"
#include "VoIPApp.hpp"
#include <winreg.h>
#include <Debug.hpp>
#include "SettingsApi.hpp"

#define REG_szSecurityVoIPPhoneSetting_RegexSMSBlockedNumberKey TEXT("Security\\Phone\\VoIP\\SMSBlockedNumber")
#define REG_szSecurityVoIPPhoneSetting_RegexVoIPPhoneNumberKey  TEXT("Security\\Phone\\VoIP\\VoIPPhoneNumber")
#define REG_szSecurityVoIPPhoneSetting_Regex                    TEXT("Regex")

//prototype for GetCallerIDOverrideString - privately exported by tpcutil
const WCHAR* GetCallerIDOverrideString();

//constants
const WCHAR DialEngine_t::sc_DefaultDialPlan[]  = L"file:\\\\\\windows\\ipdialplan.xml";
const WCHAR* DialEngine_t::sc_SIPServerKeywords[] =
{
    L"#use_sipsrv_address#",
    L"#use_sipsrv_host_name#",
};

//public
const WCHAR DialEngine_t::sc_RegexNamePrefix[] = L"DialPlan";
const UINT DialEngine_t::sc_MinNumberOfDigits = 3;
const UINT DialEngine_t::sc_MaxRegexNameBuffer = _countof(DialEngine_t::sc_RegexNamePrefix) + DialEngine_t::sc_MinNumberOfDigits;


/*------------------------------------------------------------------------------
    DialingRule_t::CleanUpDialingRule

    Cleans up a DialingRule_t structure

    Parameters:
        : IN - DialingRule_t* - The structure to be cleaned up

    Returns:
        - S_OK returned if the rule was succesfully cleaned up
        - E_INVALIDARG returned if the caller passed a NULL pointer
------------------------------------------------------------------------------*/
HRESULT
DialingRule_t::CleanUpDialingRule(
    void
    )
{
    if (!this)
    {
        ASSERT(0);
        return E_POINTER;
    }

    FlushCompiledRegex(); 

    if (m_RegexString != NULL)
    {
        SysFreeString(m_RegexString);
        m_RegexString = NULL;
    }

    if (m_DialString != NULL)
    {
        SysFreeString(m_DialString);
        m_DialString = NULL;
    }

    if (m_DisplayString != NULL)
    {
        SysFreeString(m_DisplayString);
        m_DisplayString = NULL;
    }

    if (m_TransferString!= NULL)
    {
        SysFreeString(m_TransferString);
        m_TransferString = NULL;
    }

    return S_OK;
}


/*------------------------------------------------------------------------------
    DialingRule_t::FlushCompiledRegex
    
    Flush out the pre-compiled regular expression
------------------------------------------------------------------------------*/
HRESULT
DialingRule_t::FlushCompiledRegex(
    void
    )
{
    ASSERT(this != NULL); 
    
    if (m_pRegex != NULL)
    {
        m_pRegex->Release(); 
        m_pRegex = NULL; 
    }

    return S_OK; 
}
/*------------------------------------------------------------------------------
    AutoDialRule_t::CleanUpAutoDialRule

    Cleans up a AutoDialRule_t structure

    Returns:
        - S_OK returned if the rule was succesfully cleaned up
        - E_INVALIDARG returned if the caller passed a NULL pointer
------------------------------------------------------------------------------*/
HRESULT
AutoDialRule_t::CleanUpAutoDialRule(
    void
    )
{
    if (!this)
    {
        ASSERT(0);
        return E_POINTER;
    }

    if (m_Prefix != NULL)
    {
        SysFreeString(m_Prefix); 
        m_Prefix = NULL; 
    }

    return S_OK;
}


/*------------------------------------------------------------------------------
    DialEngine_t::DialEngine_t

    Constructor
------------------------------------------------------------------------------*/
DialEngine_t::DialEngine_t(
    )
{
    TRACE(ZONE_PHONEAPP_CTOR); 
    
    m_pDialingRules     = NULL;
    m_pAutoDialRules    = NULL; 
}

/*------------------------------------------------------------------------------
    DialEngine_t::~DialEngine_t

    Destructor
------------------------------------------------------------------------------*/
DialEngine_t::~DialEngine_t(
    )
{
    TRACE(ZONE_PHONEAPP_CTOR);

    UnInitializeDialPlan();
}

/*------------------------------------------------------------------------------
    DialEngine_t::UpdateDialPlan

    Update the dial plan

    Parameters:
        void

    Returns:
        - S_OK if no errors occur
        - Error code returned otherwise
------------------------------------------------------------------------------*/
HRESULT
DialEngine_t::UpdateDialPlan(
    void
    )
{
    HRESULT hr;
    ce::auto_bstr DialPlanXML;
    CComPtr<DialPlanParser_t> cpDialPlanParser;
    DialingRuleQueue*   pNewDialingRules;
    AutoDialRuleQueue*  pNewAutoDialRules; 

    hr = DialPlanParser_t::CreateDialPlanParser(&cpDialPlanParser);
    if (FAILED(hr))
    {
        PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"Failed to intialize dial plan engine. Error 0x%08x -- ", hr));
        return hr;
    }

    // Create a new queue for dialing rules
    pNewDialingRules = new DialingRuleQueue;
    if (pNewDialingRules == NULL)
    {
        PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"OOM - Failed to create DialingRuleQueue"));
        return E_OUTOFMEMORY;
    }

    // Create a new queue for auto-dial rules
    pNewAutoDialRules = new AutoDialRuleQueue; 
    if (pNewAutoDialRules == NULL)
    {
        PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"OOM - Failed at create AutoDialRuleQueue")); 
        hr = E_OUTOFMEMORY; 
        goto exit; 
    }

    // Load dial plan from the registry (if defined)
    // Use default dial plan if there is any failure
    hr = LoadCustomDialPlan(&DialPlanXML);
    if (FAILED(hr))
    {
        PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"Cannot load custom dial plan. Error 0x%08x -- ", hr));
        goto use_default_plan;
    }

    // Validate custom dial plan
    // Use default dial plan if there is any failure
    hr = cpDialPlanParser->ProcessXML(DialPlanXML);
    if (FAILED(hr))
    {
        PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"Failed to parse custom dial plan. Error 0x%08x -- ", hr));
        goto use_default_plan;
    }

    goto transfer_rules;

use_default_plan:
    // Load default dial plan
    DialPlanXML.close();
    hr = LoadDefaultDialPlan(&DialPlanXML);
    if (FAILED(hr))
    {
        ASSERT(0);
        // Need to log this error
        PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"Cannot load default dial plan. Error 0x%08x -- ", hr));
        goto exit;
    }

    hr = cpDialPlanParser->ProcessXML(DialPlanXML);
    if (FAILED(hr))
    {
        ASSERT(0);
        // Need to log this error
        PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"Failed to parse default dial plan. Error 0x%08x -- ", hr));
        goto exit;
    }

transfer_rules:
    // Make new rules the new dialing rules
    hr = cpDialPlanParser->FlushNewRules(
                    DialPlanParser_t::FlushOperationTransfer,
                    pNewDialingRules, 
                    pNewAutoDialRules
                    );
    if (FAILED(hr))
    {
        ASSERT(0);
        // Need to log this error
        PHONEAPP_DEBUGMSG(ZONE_PHONEAPP_ERROR, (L"Failed to initialize default dial plan. Error 0x%08x -- ", hr));
        goto exit;
    }

    // Safe to replace dialing rules
    if (m_pDialingRules != NULL)
    {
        hr = CleanUpDialingRuleQueue(m_pDialingRules);
        delete m_pDialingRules;
    }

    m_pDialingRules = pNewDialingRules;
    pNewDialingRules = NULL;

    DialPlanParser_t::DebugDumpDialingRuleQueue(m_pDialingRules);
    
    // Safe to replace autodial rules
    if (m_pAutoDialRules != NULL)
    {
        hr = CleanUpAutoDialRuleQueue(m_pAutoDialRules); 
        delete m_pAutoDialRules; 
    }

    m_pAutoDialRules = pNewAutoDialRules; 
    pNewAutoDialRules = NULL; 


    DialPlanParser_t::DebugDumpAutoDialRuleQueue(m_pAutoDialRules); 

    // Update regular expressions in the registry
    UpdateRegularExpressions(RegexVoIPPhoneNumber);
    UpdateRegularExpressions(RegexSMSBlockedNumber);

exit:
    
    if (pNewDialingRules != NULL)
    {
        delete pNewDialingRules;
    }

    if (pNewAutoDialRules != NULL)
    {
        delete pNewAutoDialRules; 
    }

    //update registry to indicate the availability of dialing rules
    PHSetValue(
        phsAutoDialRulesAvailability, 
        (m_pAutoDialRules == NULL || m_pAutoDialRules->empty()) ? 0 : 1
        ); 
    

    return hr;
}

/*------------------------------------------------------------------------------
    DialEngine_t::InitializeDialPlan

    Initializes dial plan

    Parameters:
        : IN - pVoIPPhoneCanvas: the valid pointer to the VoIPPhoneCanvas_t object

    Returns:
        - S_OK if no errors occur
        - Error code returned otherwise
------------------------------------------------------------------------------*/
HRESULT
DialEngine_t::InitializeDialPlan(
    void
    )
{
    // force updating dial plan to get the initial dial plan
    HRESULT hr = UpdateDialPlan();
    if (FAILED(hr))
    {
        return hr;
    }

    // register itself as a setting handler to the Settings list
    hr = (GetApp()->GetSettings()).RegisterHandler(
                            SEF_VOIP_DIALPLAN,
                            static_cast<ISettingChangeHandler_t*>(this)
                            );

    return hr;
}

/*------------------------------------------------------------------------------
    DialEngine_t::UnInitializeDialPlan

    UnInitializes dial plan

    Parameters:
        : none

    Returns:
        - S_OK if no errors occur
        - Error code returned otherwise
------------------------------------------------------------------------------*/
HRESULT
DialEngine_t::UnInitializeDialPlan(
    void
    )
{
    HRESULT hr = S_OK;

    (GetApp()->GetSettings()).UnregisterHandler(
                    static_cast<ISettingChangeHandler_t*>(this)
                    );

    if (m_pDialingRules != NULL)
    {
        hr = CleanUpDialingRuleQueue(m_pDialingRules);
        delete m_pDialingRules;
        m_pDialingRules = NULL;

⌨️ 快捷键说明

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