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

📄 connectionmgrdlg.cpp

📁 基于widcomm的蓝牙传输功能程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// ConnectionMgrDlg.cpp : implementation file
//
//  Copyright (c) 2000-2005, Broadcom Corporation, All Rights Reserved.

#include "stdafx.h"
#include "BlueObex.h"
#include "checkheaders.h"
#include "ConnectionMgrDlg.h"
#include "ChooseServerDlg.h"
#include "btversinfo.h"

static char m_serviceNameForServer[BT_MAX_SERVICE_NAME_LEN + 1] = "WIDCOMM DK OBEX Service";
static const CString crlf("\r\n");

// forward references
static CString BdAddrToString(BD_ADDR bd_addr);
static CString GetErrorString(tOBEX_ERRORS code);
static CString GetResponseString(tOBEX_RESPONSE_CODE rsp);
static CString GetTargetString(UINT8 * p_m_target);
static CString GetHeadersString(CObexHeaders *p_headers);
static CString GetBodyString(CObexHeaders *p_headers);

// {98395DEB-7184-4287-8AEE-E78DF5224F8E}
static GUID service_guid = 
{ 0x98395deb, 0x7184, 0x4287, { 0x8a, 0xee, 0xe7, 0x8d, 0xf5, 0x22, 0x4f, 0x8e } };

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
  	CString m_strVersion;
	CString m_strCopyRight;


	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
    virtual BOOL OnInitDialog();

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

BOOL CAboutDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
    m_strVersion = _T("BlueObex Version: ");
    m_strVersion += _T(BTWPRODUCTVERSIONSTR);
    m_strCopyRight = _T(BTWLEGALCOPYRIGHTSTR);
    SetDlgItemText(IDC_COPYRIGHT, m_strCopyRight);
    SetDlgItemText(IDC_VERSION, m_strVersion);
    return true;
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


CHeadersListEntry::CHeadersListEntry ():
	obex_req_resp(0),
	mtu(0),
	scn(0),
	final(FALSE),
	backup(FALSE),
	create(FALSE)
{
	p_object = new CObexHeaders;
	memset(bd_addr, 0xff, sizeof(BD_ADDR));
}

 
CHeadersListEntry::CHeadersListEntry (WCHAR * p_file_name)
{
	p_object = new CObexHeaders;
	p_object->SetName(p_file_name);
	file = p_file_name;
}

CHeadersListEntry::~CHeadersListEntry()
{
	if (p_object)
		delete p_object;
}

CHeadersListEntry::CHeadersListEntry(const CHeadersListEntry & x)
{
	file			= x.file;
	obex_req_resp	= x.obex_req_resp;
	mtu				= x.mtu;		
	memcpy (bd_addr, x.bd_addr, sizeof (BD_ADDR));	
	scn				= x.scn;		
	final			= x.final;		
	backup			= x.backup;		
	create			= x.create;		
	p_object = new  CObexHeaders;

	UINT32 length;
	if (x.p_object->GetNameLength(&length))
	{
		WCHAR * p_name = (WCHAR *) malloc (length * sizeof (WCHAR));
		x.p_object->GetName(p_name);
		p_object->SetName(p_name);
		free (p_name);
	}
}

CHeadersListEntry& CHeadersListEntry::operator= (const CHeadersListEntry & x)
{
	file			= x.file;
	obex_req_resp	= x.obex_req_resp;
	mtu				= x.mtu;		
	memcpy (bd_addr, x.bd_addr, sizeof (BD_ADDR));	
	scn				= x.scn;		
	final			= x.final;		
	backup			= x.backup;		
	create			= x.create;
	if (p_object != NULL)
		delete p_object;

	p_object = new  CObexHeaders;

	UINT32 length;
	if (x.p_object->GetNameLength(&length))
	{
		WCHAR * p_name = (WCHAR *) malloc (length * sizeof (WCHAR));
		x.p_object->GetName(p_name);
		p_object->SetName(p_name);
		free (p_name);
	}
		return *this;
}

/////////////////////////////////////////////////////////////////////////////
// CConnectionMgrDlg dialog


CConnectionMgrDlg::CConnectionMgrDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConnectionMgrDlg::IDD, pParent),
	m_pSdpService(NULL),
	m_isSessionStarted(FALSE),
	m_isServer(FALSE),
	m_pBtIf(NULL),
	m_isConnected(FALSE),
	m_scn(0),
	p_m_target(NULL),
	m_mtu(OBEX_DEFAULT_MTU),
	client_list_index(0),
	client_list_max(0),
    server_list_index(0),
    server_list_max(0)
{
	//{{AFX_DATA_INIT(CConnectionMgrDlg)
	//}}AFX_DATA_INIT
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);


	// initialize fixed headers objects
	WCHAR object_name_open[] = L"Client Open Object";
	client_open_headers.SetName(object_name_open);

	WCHAR object_name_close[] = L" Close Object";
	WCHAR object_description_close[] = L" Close Description";
	client_close_headers.SetName(object_name_close);
	client_close_headers.SetDescription(object_description_close);

}


void CConnectionMgrDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConnectionMgrDlg)
	DDX_Control(pDX, IDC_SEND_RECEIVE_LOG, m_SendReceiveLog);
	DDX_Control(pDX, IDC_SERVER_CHECK, m_ServerCheck);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CConnectionMgrDlg, CDialog)
	//{{AFX_MSG_MAP(CConnectionMgrDlg)
	ON_WM_SYSCOMMAND()
	ON_LBN_DBLCLK(IDC_SEND_RECEIVE_LIST, OnDblclkSendReceiveList)
	//ON_WM_TIMER()
	ON_WM_PAINT()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_START_BUTTON, OnStartButton)
    ON_WM_CLOSE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConnectionMgrDlg message handlers
void CConnectionMgrDlg::OnStartButton() 
{
    // TODO: Add your control notification handler code here
    if (! m_isSessionStarted)
    {
        // Are we a Obex Server (or a Client)?
        //
        m_isServer = IsDlgButtonChecked(IDC_SERVER_CHECK);

        if ( m_isServer )
        {
            // use compiled in service name for server
            memcpy(m_serviceName, m_serviceNameForServer, BT_MAX_SERVICE_NAME_LEN + 1);
        }
        else
        {
            // clear service name so DoChooseServer can set it
//            memset(m_serviceName, 0, BT_MAX_SERVICE_NAME_LEN + 1);
            // use compiled in service name for client also
            memcpy(m_serviceName, m_serviceNameForServer, BT_MAX_SERVICE_NAME_LEN + 1);

            // The client must choose a ComChat Server to
            // connect to.
            //
            if (! DoChooseServer())
                return;     // User did not choose a server!
        }

        StartSession();
    }
    else
    {
        StopSession();
    }
}

void CConnectionMgrDlg::OnDblclkSendReceiveList() 
{
	// TODO: Add your control notification handler code here
	
}

void CConnectionMgrDlg::OnTimer(UINT_PTR nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnTimer(nIDEvent);
}

void CConnectionMgrDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CConnectionMgrDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	
	// Do not call CDialog::OnPaint() for painting messages
}

extern "C" BOOL CheckHeaders(CString str);

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

    m_timer_id = SetTimer (1, 200, NULL);
	
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	// TODO: Add extra initialization here

	// setup some objects for testing
	init_objects();

	// run  exerciser for services to a CObexHeaders object
	CString result;
	result = checker.Fill();
	AddLogText(result);

	result = checker.Verify();
	AddLogText(result);

	AddLogText ("Proceed to Start session as client or server");

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CConnectionMgrDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
    if (m_isSessionStarted)
        StopSession();

	// TODO: Add your message handler code here
	if (p_m_target != NULL)
	{
		free (p_m_target);
		p_m_target = NULL;
	}
	
	m_client_list.RemoveAll();
    m_server_list.RemoveAll();

}
                                                                                                             
void CConnectionMgrDlg::init_objects()		// setup some objects for testing
{
    // construct a list of client objects to be sent to server to exercise the OBEX protocol layer
    // Simple PUT request - has a non-empty body header
    WCHAR c_object_name_0[] = L"Put Request";
    m_client_list.Add(CHeadersListEntry(c_object_name_0));
    char c_object_body_0[] = "Put Body ";
    m_client_list[0].p_object->SetBody((UINT8 *)c_object_body_0, (UINT32) strlen (c_object_body_0), TRUE);
    m_client_list[0].final = TRUE;
    m_client_list[0].obex_req_resp = OBEX_REQ_PUT;

    // Simple GET request
    WCHAR c_object_name_1[] = L"Get Request";
    m_client_list.Add(CHeadersListEntry(c_object_name_1));
    char c_object_body_1[] = "Get Body";
    m_client_list[1].p_object->SetBody((UINT8 *)c_object_body_1, (UINT32) strlen (c_object_body_1), TRUE);
    m_client_list[1].final = TRUE;
    m_client_list[1].obex_req_resp = OBEX_REQ_GET;

    // SetPath request
    WCHAR c_object_name_2[] = L"Set Path Request";
    m_client_list.Add(CHeadersListEntry(c_object_name_2));
    char c_object_body_2[] = "Set Path Body";
    m_client_list[2].p_object->SetBody((UINT8 *)c_object_body_2, (UINT32) strlen (c_object_body_2), TRUE);
    m_client_list[2].final = TRUE;
    m_client_list[2].obex_req_resp = OBEX_REQ_SETPATH;

    // PUT create request - has an 'empty' body
    WCHAR c_object_name_3[] = L"Put Create Request";
    m_client_list.Add(CHeadersListEntry(c_object_name_3));
    m_client_list[3].p_object->SetBody(NULL, 0, TRUE);
    m_client_list[3].final = TRUE;
    m_client_list[3].obex_req_resp = OBEX_REQ_PUT;

    // PUT delete request - has no body header at all
    WCHAR c_object_name_4[] = L"Put Delete Request";
    m_client_list.Add(CHeadersListEntry(c_object_name_4));
    m_client_list[4].final = TRUE;
    m_client_list[4].obex_req_resp = OBEX_REQ_PUT;	// no body at all

    // three part GET - server sends part of the body in each of the three GET responses
    WCHAR c_object_name_5[] = L"Get Request Part 1 of 3";
    m_client_list.Add(CHeadersListEntry(c_object_name_5));
    m_client_list[5].final = TRUE;
    m_client_list[5].obex_req_resp = OBEX_REQ_GET;

    WCHAR c_object_name_6[] = L"Get Request Part 2 of 3";
    m_client_list.Add(CHeadersListEntry(c_object_name_6));
    m_client_list[6].final = TRUE;
    m_client_list[6].obex_req_resp = OBEX_REQ_GET;

    WCHAR c_object_name_7[] = L"Get Request Part 3 of 3";
    m_client_list.Add(CHeadersListEntry(c_object_name_7));
    m_client_list[7].final = TRUE;
    m_client_list[7].obex_req_resp = OBEX_REQ_GET;

#ifndef OBEX_ABORT_NOT_OK
    // multi-part GET interrupted by an ABORT from the client after the first response
    WCHAR c_object_name_8[] = L"Get Request Abort Part 1";
    m_client_list.Add(CHeadersListEntry(c_object_name_8));
    m_client_list[8].final = TRUE;
    m_client_list[8].obex_req_resp = OBEX_REQ_GET;

    // client aborts the multi-part GET
    WCHAR c_object_name_9[] = L"Abort Get Request";
    m_client_list.Add(CHeadersListEntry(c_object_name_9));
    char c_object_body_9[] = "Abort Request Body Part";
    m_client_list[9].p_object->SetBody((UINT8 *)c_object_body_9, (UINT32) strlen (c_object_body_9), TRUE);
    m_client_list[9].obex_req_resp = OBEX_REQ_ABORT;
#else
    // avoids ABORT confirm problem from WIN CE
    WCHAR c_object_name_8[] = L"Simple Get Request";
    m_client_list.Add(CHeadersListEntry(c_object_name_8));

⌨️ 快捷键说明

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