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

📄 chooseserverdlg.cpp

📁 基于widcomm的蓝牙传输功能程序
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////
//
//  Name        ChooseServerDlg.cpp
//  $Header:
//
//  Function    implementation of the CChooseServerDlg class.
//              
//
//  Date                         Modification
//  -----------------------------------------
//  5Jun2001    Jim French  Create
//
//  Copyright (c) 2000-2006, Broadcom Corporation, All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BlueObex.h"
#include "ChooseServerDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CChooseServerDlg * p_Dlg;
WORD axtoi(char *pch);

/////////////////////////////////////////////////////////////////////////////
// CChooseServerDlg dialog

CChooseServerDlg::CChooseServerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChooseServerDlg::IDD, pParent),
    m_num_devices(0),
    m_num_services_per_device(0),
    m_start_discovery(FALSE),
	m_discovery_in_progress(FALSE)
{
	//{{AFX_DATA_INIT(CChooseServerDlg)
	//}}AFX_DATA_INIT

    // Use the following events for thread notification.
  	m_hEventStopDiscoveryThread = CreateEvent(NULL, FALSE, FALSE, NULL);   // auto reset, initially reset
	m_hEventDiscoveryThreadKilled = CreateEvent(NULL, FALSE, FALSE, NULL); // auto reset, initially reset
    m_isDialogClosing = FALSE;
    m_wasDialogInterrupted = FALSE;
    m_pDiscoveryWorkerThread = NULL;
    p_Dlg = this;
}

CChooseServerDlg::~CChooseServerDlg()
{
	// The dialog owns the worker thread.
	// The destructor is responsible for killing the active worker
	// thread.

	// It's a good idea to wait for the worker thread to notify via a
	// "Thread done" event that it has killed itself. Otherwise, in the case
	// where the app is terminating, is possible (even if unlikely) that it
	// will detect a memory leak of the CWinThread object before the
	// CWinThread object has had a chance to auto-delete itself.

	DWORD dwExitCode;
	if (m_pDiscoveryWorkerThread != NULL &&
		GetExitCodeThread(m_pDiscoveryWorkerThread->m_hThread, &dwExitCode) &&
		dwExitCode == STILL_ACTIVE)
	{
        // Set flag to indicate the dialog is closing.  This is
        // used to distinguish between stopping Discovery due to
        // closing the dialog versus Discovery actually completing.
        //
        m_isDialogClosing = TRUE;

		// Kill the worker thread by setting the "Stop Discovery" 
        // thread" event.  Then wait until notified that the thread
        // was actually killed.
		SetEvent(m_hEventStopDiscoveryThread);
		WaitForSingleObject(m_hEventDiscoveryThreadKilled, 3000);
	}

  	CloseHandle(m_hEventStopDiscoveryThread);
	CloseHandle(m_hEventDiscoveryThreadKilled);
}

void CChooseServerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChooseServerDlg)
	DDX_Control(pDX, IDOK, m_OkButton);
	DDX_Control(pDX, IDC_SERVER_LIST, m_ServerList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChooseServerDlg, CDialog)
	//{{AFX_MSG_MAP(CChooseServerDlg)
	ON_NOTIFY(NM_CLICK, IDC_SERVER_LIST, OnClickServerList)
	ON_WM_DESTROY()
	//ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChooseServerDlg message handlers

BOOL CChooseServerDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

    m_timer_id = SetTimer (1, 500, NULL);

    // Create a column in the Server list to hold
    // the device name and address.
    //
    m_ServerList.InsertColumn(0, "Services", LVCFMT_LEFT, 1000);

    // Start Device Inquire immediately upon showing
    // dialog.  Inquiry is stopped when user clicks
    // the Discovery button or the Cancel button.
    ///
    if ( ! StartInquiry() )
        SetDlgItemText(IDC_STATUS_TEXT, "ERROR - Unable to start device inquiry.");

    // optional BDA filter for lab testing with large number of devices responding to inquiry
    GetBDAFilterFromReg();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CChooseServerDlg::string2BDAddr(BD_ADDR bdAddr, char *lpbdAddrString)
{
	if ( strlen(lpbdAddrString) != (BD_ADDR_LEN * 3 - 1))
		return FALSE;

	for (int i = 0; i<BD_ADDR_LEN; i++)
		bdAddr[i] = (char)axtoi(&lpbdAddrString[i *3]);
	return TRUE;

}

/********************************************************
lab test mechanism to filter inquiry results.  
Checks for desired server BDA in registry, filters results if found
********************************************************/
void CChooseServerDlg::GetBDAFilterFromReg()
{
    HKEY   hKey;
    DWORD  dwResult;
		
    memset(m_BdAddrFilter, 0, BD_ADDR_LEN);

    if ((dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Widcomm\\BtConfig\\Debug",
                    0, KEY_READ, &hKey)) == ERROR_SUCCESS)
    {
        TCHAR szFilter[256];
        DWORD dwBytes = 256;
        if (ERROR_SUCCESS == RegQueryValueEx (hKey, "SDKSampleTargetDevice", NULL, NULL, (LPBYTE)&szFilter, &dwBytes))
        {
            string2BDAddr(m_BdAddrFilter, szFilter);
        }
        RegCloseKey(hKey);
    }

}


void CChooseServerDlg::OnDestroy() 
{
	CDialog::OnDestroy();

    // User may have clicked 'Cancel' button after starting inquiry.  
    // Need to stop any inquiry if was started and not stopped yet.
    //
    StopInquiry();

	// wind down any discovery in progress
	SetEvent(m_hEventStopDiscoveryThread);
	WaitForSingleObject(m_hEventDiscoveryThreadKilled, 3000);
	
    //
    // Need to get rid of the item data.
    //
    int     item_count = m_num_devices;

    for (int x=0; x < item_count; x++)
    {
        CBdInfo* p_Info = m_pDevInfo[x];

        if (p_Info)
            delete p_Info;
    }
    m_ServerList.DeleteAllItems();
}

void CChooseServerDlg::OnClickServerList(NMHDR* pNMHDR, LRESULT* pResult) 
{
    POSITION pos = m_ServerList.GetFirstSelectedItemPosition();

	if (m_discovery_in_progress)
	{
		m_wasDialogInterrupted = TRUE;	// stops discovery for remaining devices
	    SetDlgItemText(IDC_STATUS_TEXT, "Discovery terminated ");
		Sleep(2000);
		SetEvent(m_hEventStopDiscoveryThread); // stops discovery on current device
	}

    // Was an item in the list selected?
    //
    if (pos != NULL)
    {

        int nItem = m_ServerList.GetNextSelectedItem(pos);
        CBdInfo* p_Info = (CBdInfo *) m_ServerList.GetItemData(nItem);

        m_OkButton.EnableWindow( TRUE );

        // Copy the selected information into member variables.
        // This makes it available when the user clicks OK.
        memcpy(m_BdAddr, p_Info->m_BdAddr, BD_ADDR_LEN);
        memcpy(m_serviceName, p_Info->m_serviceName, sizeof(m_serviceName));
        m_Name = p_Info->m_Name;    // friendly name
        m_Scn = p_Info->m_Scn;
    }
    //
    // There is no item (server) selected so user
    // cannot Ok at this point.
    else
        m_OkButton.EnableWindow( FALSE );

	*pResult = 0;
}

void CChooseServerDlg::DoAddDevice(BD_ADDR p_bda, BD_NAME bd_name)
{
    CString item_text;
    int     item_count = m_num_devices;
    BD_ADDR     zeroBda;
     
    // check for inquiry result filter stored in registry 

⌨️ 快捷键说明

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