// PhoneListDlg.cpp : implementation file
//

#include "stdafx.h"
#include "palmtool.h"
#include "UdpSocket.h"
#include "TcpSocket.h"
#include "PhoneListDlg.h"
#include "..\\inc\\p_strs.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPhoneListDlg dialog


CPhoneListDlg::CPhoneListDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPhoneListDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPhoneListDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CPhoneListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPhoneListDlg)
	DDX_Control(pDX, IDC_PHONE_LIST, m_ctlPhones);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPhoneListDlg, CDialog)
	//{{AFX_MSG_MAP(CPhoneListDlg)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_REFRESH_PHONES, OnRefreshPhones)
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_UDP_SOCKET_DATA, OnUdpData)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPhoneListDlg message handlers

BOOL CPhoneListDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	DWORD dwStyle;
	
	dwStyle = m_ctlPhones.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
	m_ctlPhones.SetExtendedStyle(dwStyle);
	m_ctlPhones.InsertColumn(0, "IP Address", LVCFMT_CENTER, 130, 0);
	m_ctlPhones.InsertColumn(1, "Mac Address", LVCFMT_CENTER, 160, 1);
	m_ctlPhones.InsertColumn(2, "Type & Version", LVCFMT_CENTER, 200, 2);
	m_ctlPhones.InsertColumn(3, "IP Type", LVCFMT_CENTER, 80, 3);
	m_ctlPhones.InsertColumn(4, "Protocol", LVCFMT_CENTER, 80, 4);
	m_ctlPhones.InsertColumn(5, "Phone Number", LVCFMT_CENTER, 140, 5);
	m_ctlPhones.InsertColumn(6, "Log on", LVCFMT_CENTER, 70, 6);
	m_ctlPhones.InsertColumn(7, "Busy", LVCFMT_CENTER, 60, 7);
//	m_ctlPhones.InsertColumn(8, "ID", LVCFMT_CENTER, 60, 8);

	m_udp.Enter(GetSafeHwnd(), WM_UDP_SOCKET_DATA, 0xc0a80101, 1030);
	m_udp_new.Enter(GetSafeHwnd(), WM_UDP_SOCKET_DATA, 0xc0a80101, 61030);	// Windows XP service pack 2 used 1030 udp port, we have to change it

	SendHello();

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

void CPhoneListDlg::OnOK() 
{
	// TODO: Add extra validation here
	int iItem;
	
	POSITION pos = m_ctlPhones.GetFirstSelectedItemPosition();

	if (pos)
	{
		iItem = m_ctlPhones.GetNextSelectedItem(pos);
		m_strSelectedIP = m_ctlPhones.GetItemText(iItem, 0);
	}
	else
	{
		m_strSelectedIP.Empty();
	}

	CDialog::OnOK();
}

void CPhoneListDlg::OnDestroy() 
{
	// TODO: Add your message handler code here
	m_udp.Leave();
	m_udp_new.Leave();
	CDialog::OnDestroy();
}

void CPhoneListDlg::OnRefreshPhones() 
{
	m_ctlPhones.DeleteAllItems();
	UpdateData(FALSE);
	SendHello();
}

#define REPORT_IP_POS			0	// 0-3 IP
#define REPORT_MAC_POS			4	// 4-9 MAC
#define REPORT_VERSION_POS		10	// 10-39 Version
#define REPORT_NUMBER_POS		40	// 40-59 Phone Number
#define REPORT_TYPE_POS			60	// 60 ip type
#define REPORT_LOGON_POS		61	// 61 Logon status
#define REPORT_CALL_POS			62	// 62 Call status
#define REPORT_PROTOCOL_POS		63	// 63 Protocol
#define REPORT_TOTAL_LEN		64

#if _MSC_VER >= 1300
LRESULT CPhoneListDlg::OnUdpData(WPARAM wParam, LPARAM lParam)
#else
void CPhoneListDlg::OnUdpData(WPARAM wParam, LPARAM lParam)
#endif
{
	ULONG uPeerIP = (ULONG)wParam;
	WSABUF * p = (WSABUF *)lParam;
	UCHAR * pData = (UCHAR *)p->buf;
	int iLength = p->len;
	CString strIP, strMac, strVer, strNumber, strIPType, strProtocol, strLogon, strCall, strID;
	
	strIP.Format("%d.%d.%d.%d", pData[REPORT_IP_POS], pData[REPORT_IP_POS+1], pData[REPORT_IP_POS+2], pData[REPORT_IP_POS+3]);
	strMac.Format("%02x-%02x-%02x-%02x-%02x-%02x", pData[REPORT_MAC_POS], pData[REPORT_MAC_POS+1], pData[REPORT_MAC_POS+2],pData[REPORT_MAC_POS+3],pData[REPORT_MAC_POS+4],pData[REPORT_MAC_POS+5]);
	strVer = (char *)(pData+REPORT_VERSION_POS);
	strNumber = (char *)(pData+REPORT_NUMBER_POS);
	strIPType = g_cIpTypeDisplay[pData[REPORT_TYPE_POS]];
/*	switch (pData[REPORT_TYPE_POS])
	{
	case 0:
		strIPType = "static";
		break;
	case 1:
		strIPType = "dhcp";
		break;
	case 2:
		strIPType = "pppoe";
		break;
	case 3:
		strIPType = "modem";
		break;
	}
*/
//	strProtocol = g_cProtocolDisplay[pData[REPORT_PROTOCOL_POS]];
	switch (pData[REPORT_PROTOCOL_POS])
	{
	case 0:
		strProtocol = "h323";
		break;
	case 1:
		strProtocol = "sip";
		break;
	case 2:
		strProtocol = "mgcp";
		break;
	case 3:
		strProtocol = "n2p";
		break;
	case 4:
		strProtocol = "iax2";
		break;
	case 5:
		strProtocol = "wp";
		break;
	case 6:
		strProtocol = "epn";
		break;
	default:
		strProtocol = "others";
		break;
	}

	strLogon = (pData[REPORT_LOGON_POS] == 1) ? "Yes" : "No";
	strCall = (pData[REPORT_CALL_POS] == 1) ? "Yes" : "No";
	strID = "Test";
	AddPhone(strIP, strMac, strVer, strIPType, strProtocol, strNumber, strLogon, strCall, strID);

	free(p->buf);
	free(p);
#if _MSC_VER >= 1300
	return 0;
#endif
}


/////////////////////////////////////////////////////////////////////////////
// CPhoneListDlg protected functions

void CPhoneListDlg::SendHello()
{
	CUdpSocket multicast;
	char * p = (char *)malloc(32);
	multicast.Enter(GetSafeHwnd(), 0, 0xe0010205, 1025);
	Sleep(0);
	sprintf(p, "Hello, world!");
	multicast.Write(p, strlen(p));
	Sleep(0);
	multicast.Leave();
}

void CPhoneListDlg::AddPhone(CString strIP, CString strMac, CString strType, CString strIPType, CString strProtocol, CString strNum, CString strLog, CString strBusy, CString strID)
{
	int i, iTotal, iItem;
	LV_ITEM	lv_item;

	iTotal = m_ctlPhones.GetItemCount();
	for (i = 0; i < iTotal; i ++)
	{
		if (strIP == m_ctlPhones.GetItemText(i, 0))
		{	// do not add same ip address phones
//			return;
		}
	}

	lv_item.mask = LVIF_TEXT; 
	lv_item.iItem = iTotal;
	lv_item.iSubItem = 0;
	lv_item.pszText = (char *)(LPCTSTR)strIP;
	iItem = m_ctlPhones.InsertItem(&lv_item); // insert new lv_item
	m_ctlPhones.SetItemText(iItem, 1, strMac);
	m_ctlPhones.SetItemText(iItem, 2, strType);
	m_ctlPhones.SetItemText(iItem, 3, strIPType);
	m_ctlPhones.SetItemText(iItem, 4, strProtocol);
	m_ctlPhones.SetItemText(iItem, 5, strNum);
	m_ctlPhones.SetItemText(iItem, 6, strLog);
	m_ctlPhones.SetItemText(iItem, 7, strBusy);
//	m_ctlPhones.SetItemText(iItem, 8, strID);
}

