www.pudn.com > Bluetooth_config_2_0_X.zip > BprConfigDlg.cpp, change:2004-05-17,size:10503b


// BprConfigDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "BprConfig.h" 
#include "BprConfigDlg.h" 
#include "ConfigDlg.h" 
#include "RegPrnMainDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#include "KJW.h" 
///////////////////////////////////////////////////////////////////////////// 
// definition of my global constants & variables 
 
static unsigned char my_role; 
static BOOLEAN init_prn_info; 
static BOOLEAN open_port(void); 
static void close_port(void); 
static void PORT_EventHandler(unsigned char event, PORT_EVENT_PARA *event_para); 
 
///////////////////////////////////////////////////////////////////////////// 
// CBprConfigDlg dialog 
 
CBprConfigDlg::CBprConfigDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CBprConfigDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CBprConfigDlg) 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CBprConfigDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CBprConfigDlg) 
	DDX_Control(pDX, IDC_PORT, m_port); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CBprConfigDlg, CDialog) 
	//{{AFX_MSG_MAP(CBprConfigDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(ID_CONFIG, OnConfig) 
	ON_BN_CLICKED(ID_REG_PRN, OnRegPrn) 
	ON_CBN_SELENDOK(IDC_PORT, OnSelendokPort) 
	ON_BN_CLICKED(ID_TEST_RECEIPT, OnTestReceipt) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CBprConfigDlg message handlers 
 
BOOL CBprConfigDlg::OnInitDialog() 
{ 
    CDialog::OnInitDialog(); 
 
	// 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 
 
	// initialize the ComboBox selected items 
    m_port.SetCurSel(0);            // COM1 
 
    // copy the initial selections into global variables 
    m_port.GetLBText(0, port_name); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CBprConfigDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CBprConfigDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CBprConfigDlg::OnSelendokPort()  
{ 
	// TODO: Add your control notification handler code here 
 
    m_port.GetLBText(m_port.GetCurSel(), port_name); 
} 
 
void CBprConfigDlg::OnConfig()  
{ 
    // TODO: Add your control notification handler code here 
 
	// open the COM device for Bluetooth Printer Dongle 
    if (!open_port()) return; 
     
    // stop any on-going operations in the adapter 
    PORT_Reset(RESET_CURRENT_OP); 
    PORT_Clear(); 
 
    // open dialog 
    CConfigDlg dlg; 
    theApp.pConfigDlg = &dlg; 
    int nResponse = dlg.DoModal(); 
    if (nResponse == IDOK) { 
        // TODO: Place code here to handle when the dialog is 
        //  dismissed with OK 
    } 
    else if (nResponse == IDCANCEL) { 
        // TODO: Place code here to handle when the dialog is 
        //  dismissed with Cancel 
    } 
 
    // close the COM device 
    close_port(); 
} 
 
void CBprConfigDlg::OnRegPrn()  
{ 
    // TODO: Add your control notification handler code here 
 
    // open the COM device for Bluetooth Printer Dongle 
    if (!open_port()) return; 
 
    // stop any on-going operations in the adapter 
    PORT_Reset(RESET_CURRENT_OP); 
    PORT_Clear(); 
 
    // check if the adapter is configured as a client 
    PORT_ReadConfig(); 
 
    if (WaitForSingleObject(hEvent, 1000) != WAIT_OBJECT_0) { 
        MessageBox("Can't communicate with the Adapter.", "Error",  
                   MB_ICONERROR | MB_OK); 
        close_port(); 
        return; 
    } 
 
    if (my_role != CLIENT_DEVICE) { 
        MessageBox("To use this menu, configure the Adapter as a Master Device first.", 
                   "Notice", MB_ICONEXCLAMATION | MB_OK); 
        close_port(); 
        return; 
    } 
 
    // open dialog 
    CRegPrnMainDlg dlg; 
    theApp.pRegPrnMainDlg = &dlg; 
    int nResponse = dlg.DoModal(); 
    if (nResponse == IDOK) { 
        // TODO: Place code here to handle when the dialog is 
        //  dismissed with OK 
    } 
    else if (nResponse == IDCANCEL) { 
        // TODO: Place code here to handle when the dialog is 
        //  dismissed with Cancel 
    } 
 
    // close the COM device 
    close_port(); 
} 
 
static BOOLEAN 
open_port(void) 
{ 
    unsigned char baudrate; 
    int result, retry; 
 
    for (retry = 1; retry = 2; retry++) { 
        for (baudrate = BR_9600; baudrate = BR_38400; baudrate++) { 
         
            // open the COM device port 
	        result = PORT_Open(port_name, baudrate); 
			//result = PORT_Open(port_name, BR_19200 ); 
            if (result == -1) { 
                char msg[32]; 
                sprintf(msg, "Can't open %s.", port_name); 
                MessageBox(NULL, msg, "Error", MB_ICONERROR | MB_OK); 
                return FALSE; 
            } 
 
            Sleep(200 * retry); 
 
            // check if the adapter is in action 
            PORT_RegisterEventHandler((long)PORT_EventHandler, 0); 
            PORT_CheckStatus(); 
 
            if (WaitForSingleObject(hEvent, 200 * retry) == WAIT_OBJECT_0)  
                return TRUE; 
            else 
                PORT_Close(); 
        } 
    } 
 
    // there's no response from the adapter  
    MessageBox(NULL, "Can't communicate with the Adapter.", "Error",  
               MB_ICONERROR | MB_OK); 
    return FALSE; 
} 
 
static void 
close_port(void) 
{ 
    PORT_Close(); 
} 
 
static void  
PORT_EventHandler(unsigned char event, PORT_EVENT_PARA *event_para) 
{ 
	UINT i; 
	switch (event) { 
    case REPORT_STATUS: 
        // check if the status is OK 
        if (event_para->ReportStatus.Status) { 
            SetEvent(hEvent); 
        } 
        break; 
	 
	case REPORT_BD_ADDR: 
        memcpy(local_bd_addr, event_para->ReportBD_Addr.BD_Addr, 6); 
        SetEvent(hEvent); 
        break; 
 
    case REPORT_FW_VERSION: 
        memcpy(fw_version, event_para->ReportFW_Version.FW_Version, 14); 
        SetEvent(hEvent); 
        break; 
 
    case READ_CONFIG_RESULT: 
        // read the result 
        my_role = event_para->ReadConfigResult.Role; 
 
		memset((void *)&cur_config, 0, sizeof(CFG_INF)); 
        // read the result 
        cur_config.Role = event_para->ReadConfigResult.Role; 
        cur_config.Baudrate = event_para->ReadConfigResult.Baudrate; 
        cur_config.FlowControl = event_para->ReadConfigResult.FlowControl; 
        cur_config.AutoConnect = event_para->ReadConfigResult.AutoConnect; 
        cur_config.WaitForAllConnected = event_para->ReadConfigResult.WaitForAllConnected; 
        cur_config.PrintFW_Info = event_para->ReadConfigResult.PrintFW_Info; 
        cur_config.AutoDetect = event_para->ReadConfigResult.DeviceNameAutoDetect; 
        strcpy(cur_config.DevName, event_para->ReadConfigResult.DeviceName); 
        strcpy(cur_config.DevLocation, event_para->ReadConfigResult.DeviceLocation); 
		//break; 
 
        SetEvent(hEvent); 
		break; 
 
	case READ_PRNINFO_RESULT: 
        memset((void *)new_prn_table, 0, sizeof(new_prn_table)); 
		new_prn_count = event_para->ReadPrnInfoResult.NumPrinters; 
		for (i = 0; i  new_prn_count; i++) { 
			new_prn_table[i].ID = event_para->ReadPrnInfoResult.PrinterTable[i].ID; 
			memcpy(new_prn_table[i].BD_Addr,  
				   event_para->ReadPrnInfoResult.PrinterTable[i].BD_Addr, 6); 
            memcpy(new_prn_table[i].DeviceName,  
				   event_para->ReadPrnInfoResult.PrinterTable[i].DeviceName, 16); 
            memcpy(new_prn_table[i].DeviceLocation,  
				   event_para->ReadPrnInfoResult.PrinterTable[i].DeviceLocation, 13); 
		} 
		break; 
	 
	} 
} 
 
void CBprConfigDlg::OnTestReceipt()  
{ 
	// TODO: Add your control notification handler code here 
	 
	// open the COM device for Bluetooth Printer Dongle 
    if (!open_port()) return;	 
 
    // stop any on-going operations in the adapter 
    PORT_Reset(RESET_CURRENT_OP); 
    PORT_Clear(); 
 
	// first, read adapter's Bluetooth address 
    PORT_ReadBD_Addr(); 
 
    if (WaitForSingleObject(hEvent, 2000) != WAIT_OBJECT_0) { 
        return; 
    } 
 
    // next, read adapter's firmware version 
    PORT_ReadFW_Version(); 
 
    if (WaitForSingleObject(hEvent, 2000) != WAIT_OBJECT_0) { 
        return; 
    } 
 
    // check if the adapter is configured as a client 
    PORT_ReadConfig(); 
//	Sleep(300); 
 
    if (WaitForSingleObject(hEvent, 2000) != WAIT_OBJECT_0) { 
        MessageBox("Can't communicate with the Adapter.", "Error",  
                   MB_ICONERROR | MB_OK); 
        close_port(); 
        return; 
    } 
 
    if (my_role != CLIENT_DEVICE) { 
        MessageBox("To use this menu, configure the Adapter as a Master Device first.", 
                   "Notice", MB_ICONEXCLAMATION | MB_OK); 
        close_port(); 
        return; 
    } 
 
	// initialize variables 
	cur_prn_count = 0; 
    printing = FALSE; 
 
    // read initial printer table 
    init_prn_info = TRUE; 
    PORT_ReadPrnInfo(); 
	Sleep(300); 
 
	// open dialog 
    CKJW dlg;		// kjw 
    int nResponse = dlg.DoModal(); 
 
    if (nResponse == IDOK) { 
        // TODO: Place code here to handle when the dialog is 
        //  dismissed with OK 
    } 
    else if (nResponse == IDCANCEL) { 
        // TODO: Place code here to handle when the dialog is 
        //  dismissed with Cancel 
    } 
 
	// close the COM device 
    close_port(); 
}