/////////////////////////////////////////////////////////////////////////////// // // (c) Digi International Inc. 2003. All Rights Reserved // // Digi provides this document "as is," without warranty of any kind, // either expressed or implied, including, but not limited to, the implied // warranties of fitness or merchantability for a particular purpose. Digi // may make improvements and/or changes in this document or in the product(s) // and/or the program(s) described in this document at any time. // // This document could include technical inaccuracies or typographical errors. // Changes are periodically made to the information herein; these changes may // be incorporated in new editions of the publication. // // Digi International Inc. // 11001 Bren Road East // Minnetonka, MN 55343, USA // Tel: +1 (952) 912-3444 // Fax: +1 (952) 912-4952 // http://www.digi.com/ // /////////////////////////////////////////////////////////////////////////////// // FinderDlg.cpp : implementation file // #include "stdafx.h" #include "Finder.h" #include "FinderDlg.h" #include "ConfigIP.h" #include "addp.h" #include "Reboot.h" #include "DeviceInfo.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define WM_START_ADDP_SEARCH (WM_APP + 1) #define WM_GET_ADDP_SEARCH_RESULTS (WM_APP + 2) #define SEARCHING _T("Searching...") ///////////////////////////////////////////////////////////////////////////// // CFinderDlg dialog CFinderDlg::CFinderDlg(CWnd* pParent /*=NULL*/) : CDialog(CFinderDlg::IDD, pParent) { //{{AFX_DATA_INIT(CFinderDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CFinderDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CFinderDlg) DDX_Control(pDX, IDC_INFOLIST, m_DeviceListCtrl); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CFinderDlg, CDialog) //{{AFX_MSG_MAP(CFinderDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_WEBUI, OnWebUI) ON_BN_CLICKED(IDC_CONFIGUREIP, OnConfigureIP) ON_BN_CLICKED(IDC_REFRESH, OnRefresh) ON_BN_CLICKED(IDC_REBOOT, OnReboot) ON_BN_CLICKED(IDC_QUERYDEVICE, OnQueryDevice) ON_WM_DESTROY() //}}AFX_MSG_MAP ON_MESSAGE(WM_START_ADDP_SEARCH, StartADDPSearch) ON_MESSAGE(WM_GET_ADDP_SEARCH_RESULTS, AddADDPResult) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFinderDlg message handlers BOOL CFinderDlg::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 /////CODE TO SET UP COLUMNS AND HEADINGS m_DeviceListCtrl.InsertColumn(COL_IPADDRESS, "IP Address", LVCFMT_LEFT, 110); m_DeviceListCtrl.InsertColumn(COL_MACADDRESS, "MAC Address", LVCFMT_LEFT, 110); m_DeviceListCtrl.InsertColumn(COL_NAME, "Name", LVCFMT_LEFT, 110); m_DeviceListCtrl.InsertColumn(COL_PRODUCT, "Product", LVCFMT_LEFT, 129); /////Style Setup for the CtrlList m_DeviceListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT); UpdateData(false); m_ADDPSearchHandle = ADDPOpen(ADDP_VENDOR_ID_DIGI); //Get handle ASSERT(m_ADDPSearchHandle != NULL); OnRefresh(); //Call Search to initially populate list 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 CFinderDlg::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 CFinderDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CFinderDlg::OnWebUI() { addp_device_info_t *DeviceInfo = GetItemDeviceInfo(m_DeviceListCtrl.GetNextItem(-1, LVNI_SELECTED)); ASSERT(DeviceInfo != NULL); CString URL; URL.Format("http://%s", inet_ntoa(DeviceInfo->ip_address)); ShellExecute(this->GetSafeHwnd(), NULL, URL, NULL, NULL, SW_SHOWNORMAL); } void CFinderDlg::OnConfigureIP() { addp_device_info_t *DeviceInfo = GetItemDeviceInfo(m_DeviceListCtrl.GetNextItem(-1, LVNI_SELECTED)); ASSERT(DeviceInfo != NULL); CConfigIP Dlg(this); Dlg.m_DevInfo = DeviceInfo; Dlg.DoModal(); } void CFinderDlg::OnRefresh() { ASSERT(m_ADDPSearchHandle != NULL); ADDPStopSearch(m_ADDPSearchHandle); //CLEAR LIST OUT FOR NEW STUFF m_DeviceListCtrl.SetRedraw(FALSE); RemoveAllItemsFromList(); m_DeviceListCtrl.InsertItem(0, SEARCHING); m_DeviceListCtrl.SetRedraw(TRUE); m_DeviceListCtrl.RedrawWindow(); PostMessage(WM_START_ADDP_SEARCH); } LRESULT CFinderDlg::StartADDPSearch(WPARAM wParam, LPARAM lParam) { ASSERT(m_ADDPSearchHandle != NULL); #define DO_SYNC_SEARCH #if defined DO_SYNC_SEARCH BeginWaitCursor(); ADDPStartSyncSearch(m_ADDPSearchHandle); EndWaitCursor(); PostMessage(WM_GET_ADDP_SEARCH_RESULTS); #else ADDPStartAsyncSearchMessage(m_ADDPSearchHandle, this->GetSafeHwnd(), WM_GET_ADDP_SEARCH_RESULTS, NULL); #endif return 0; } void CFinderDlg::OnReboot() { addp_device_info_t *DeviceInfo = NULL; DeviceInfo = GetItemDeviceInfo(m_DeviceListCtrl.GetNextItem(-1, LVNI_SELECTED)); ASSERT(DeviceInfo != NULL); CReboot Dlg(this); Dlg.m_DevInfo = DeviceInfo; Dlg.DoModal(); } void CFinderDlg::OnQueryDevice() { addp_device_info_t *DeviceInfo = NULL; DeviceInfo = GetItemDeviceInfo(m_DeviceListCtrl.GetNextItem(-1, LVNI_SELECTED)); ASSERT(DeviceInfo != NULL); CDeviceInfo Dlg(this); Dlg.m_DevInfo = DeviceInfo; Dlg.DoModal(); } LRESULT CFinderDlg::AddADDPResult(WPARAM wParam, LPARAM lParam) { if (m_DeviceListCtrl.GetItemText(0, 0) == SEARCHING) { m_DeviceListCtrl.DeleteItem(0); } int DeviceIndex =0; addp_device_info_t Result; while(ADDPGetSearchResultCount(m_ADDPSearchHandle) > 0) { ADDPGetSearchResults(m_ADDPSearchHandle, &Result, 1); addp_device_info_t *pResult = new addp_device_info_t; *pResult = Result; CString MacAddress; MacAddress.Format("%02x:%02x:%02x:%02x:%02x:%02x", Result.mac_address[0], Result.mac_address[1], Result.mac_address[2], Result.mac_address[3], Result.mac_address[4], Result.mac_address[5]); //populate list with all devices info DeviceIndex = m_DeviceListCtrl.InsertItem(m_DeviceListCtrl.GetItemCount(), inet_ntoa(Result.ip_address)); m_DeviceListCtrl.SetItem(DeviceIndex, COL_MACADDRESS, LVIF_TEXT, MacAddress,0, 0, 0, 0); m_DeviceListCtrl.SetItem(DeviceIndex, COL_NAME, LVIF_TEXT, Result.name, 0, 0, 0, 0); m_DeviceListCtrl.SetItem(DeviceIndex, COL_PRODUCT, LVIF_TEXT, Result.product_name, 0, 0, 0, 0); m_DeviceListCtrl.SetItemData(DeviceIndex, (DWORD)pResult); // Used for sorting. DeviceIndex++; } if(m_DeviceListCtrl.GetItemCount() == 0) { m_DeviceListCtrl.InsertItem(0, "No devices found."); } else { m_DeviceListCtrl.SetItemState(0, LVNI_FOCUSED|LVNI_SELECTED, LVNI_FOCUSED|LVNI_SELECTED); } return 0; } addp_device_info_t* CFinderDlg::GetItemDeviceInfo(int iItem) { addp_device_info_t *DeviceInfo = NULL; if(iItem >= 0 && iItem < m_DeviceListCtrl.GetItemCount()) { DeviceInfo = (addp_device_info_t *)m_DeviceListCtrl.GetItemData(iItem); } return DeviceInfo; } void CFinderDlg::OnDestroy() { ADDPStopSearch(m_ADDPSearchHandle); ADDPClose(m_ADDPSearchHandle); RemoveAllItemsFromList(); CDialog::OnDestroy(); } void CFinderDlg::RemoveAllItemsFromList() { while(m_DeviceListCtrl.GetItemCount() > 0) { addp_device_info_t *DeviceInfo = GetItemDeviceInfo(0); if(DeviceInfo != NULL) { delete DeviceInfo; } //delete item m_DeviceListCtrl.DeleteItem(0); } }