// VoiceDialerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VoiceDialer.h"
#include "VoiceDialerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define TIMER_DeviceSearch				WM_USER + 0x1000
#define WM_USER_NOTIFY_DEVICE_REMOVAL	WM_USER + 0x1001
#define WM_USER_NOTIFY_CALLINFO	        WM_USER + 0x1002
// global pointer to self
CVoiceDialerDlg* g_This = NULL;

void GOBICALLBACK DeviceRemovalCallback(ULONG nStatus);
void GOBICALLBACK CallInfoCallback(ULONG nStatus);


// CVoiceDialerDlg dialog

CVoiceDialerDlg::CVoiceDialerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVoiceDialerDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVoiceDialerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, m_editStatus);
	DDX_Control(pDX, IDC_LIST1, lst_Log);
	DDX_Control(pDX, IDC_EDIT2, phoneno);
	DDX_Control(pDX, IDC_COMBOCMD, m_combo_cmd);
	DDX_Control(pDX, IDC_EDIT_PROFILE, m_edit_profile);
	DDX_Control(pDX, IDC_EDIT_EARMUTE, m_edit_earmute);
	DDX_Control(pDX, IDC_EDIT_MICMUTE, m_edit_micmute);
	DDX_Control(pDX, IDC_EDIT_GENERATOR, m_edit_generator);
	DDX_Control(pDX, IDC_EDIT_VOLUME, m_edit_volume);
	DDX_Control(pDX, IDC_EDIT_CWT, m_edit_cwt);
	DDX_Control(pDX, IDC_EDIT_ERROR, m_edit_error);
	DDX_Control(pDX, IDC_EDIT_GAIN, m_edit_gain);
	DDX_Control(pDX, IDC_EDIT_LEVEL, m_edit_level);
	DDX_Control(pDX, IDC_EDIT_LOOPBACK, m_edit_loopback);
	DDX_Control(pDX, IDC_CHECK_KEEP, m_check_keep);
	DDX_Control(pDX, IDC_EDIT_SWITCH, m_edit_switch);
	DDX_Control(pDX, IDC_EDIT_STAGE, m_edit_stage);
	DDX_Control(pDX, IDC_EDIT_B0, m_edit_b0);
	DDX_Control(pDX, IDC_EDIT_B1, m_edit_b1);
	DDX_Control(pDX, IDC_EDIT_B2, m_edit_b2);
	DDX_Control(pDX, IDC_EDIT_A1, m_edit_a1);
	DDX_Control(pDX, IDC_EDIT_A2, m_edit_a2);
	DDX_Control(pDX, IDC_CHECK_SWITCH, m_check_switch);
	DDX_Control(pDX, IDC_CHECK_COEFF, m_check_coeff);
	DDX_Control(pDX, IDC_EDIT_DEVICE, m_edit_device);
	DDX_Control(pDX, IDC_EDIT_ID, m_edit_id);
	DDX_Control(pDX, IDC_EDIT_PARAM, m_edit_param);
}

BEGIN_MESSAGE_MAP(CVoiceDialerDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_WM_TIMER()
	ON_MESSAGE(WM_USER_NOTIFY_DEVICE_REMOVAL, OnNotifDeviceRemoval)
	ON_MESSAGE(WM_USER_NOTIFY_CALLINFO, OnCallInfoCallback)
	ON_BN_CLICKED(IDC_BUTTON3, &CVoiceDialerDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CVoiceDialerDlg::OnBnClickedButton4)
//	ON_CBN_EDITCHANGE(IDC_COMBOCMD, &CVoiceDialerDlg::OnCbnEditchangeCombocmd)
ON_BN_CLICKED(IDC_BTNGET, &CVoiceDialerDlg::OnBnClickedBtnget)
ON_BN_CLICKED(IDC_BTNSET, &CVoiceDialerDlg::OnBnClickedBtnset)
ON_BN_CLICKED(IDC_BTNCLEAR, &CVoiceDialerDlg::OnBnClickedBtnclear)
END_MESSAGE_MAP()


// CVoiceDialerDlg message handlers

BOOL CVoiceDialerDlg::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
	this->m_editStatus.SetWindowTextW(L"Searching for device...");

	// TODO: Add extra initialization here
	g_This = this;
	m_DeviceSearchTimer=SetTimer(TIMER_DeviceSearch, 1000, NULL);
	memset(&m_callinfo, 0, sizeof(arrCallInfo));

	m_combo_cmd.AddString(L"CFG");
	m_combo_cmd.AddString(L"Profile");
	m_combo_cmd.AddString(L"Volume");
	m_combo_cmd.AddString(L"AVMute");
	m_combo_cmd.AddString(L"STG");
	m_combo_cmd.AddString(L"SpeakerGain");
	m_combo_cmd.AddString(L"MicGain");
	m_combo_cmd.AddString(L"AVTXG");
	m_combo_cmd.AddString(L"AVRXG");
	m_combo_cmd.AddString(L"VolDB");
	m_combo_cmd.AddString(L"TxVol");
	m_combo_cmd.AddString(L"NVDEF");
	m_combo_cmd.AddString(L"LPBK");
	m_combo_cmd.AddString(L"AVEC");
	m_combo_cmd.AddString(L"AVNS");
	//m_combo_cmd.AddString(L"TXPCMIIR");
	//m_combo_cmd.AddString(L"RXPCMIIR");
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CVoiceDialerDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_VOICEDIALER_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_VOICEDIALER_DIALOG));
	}
}
#endif


// Search for available device
BOOL CVoiceDialerDlg::DeviceSearch()
{
	// Attempt to connect to first available device
	if (!QCWWANConnect())
	{
        m_editStatus.SetWindowText(_T("Connected to Device"));
        //m_btnSend.EnableWindow(TRUE);
        //m_btnDelete.EnableWindow(TRUE); 
        //m_btnDeleteAll.EnableWindow(TRUE); 
        UpdateData(TRUE);

		//m_SmsEngine.StartupEngine(&m_InboxUI);
		return TRUE;
	}
	
	return FALSE;
}

// Session state callback handler
static void GOBICALLBACK DeviceRemovalCallback(ULONG nStatus)
{
	g_This->PostMessage(WM_USER_NOTIFY_DEVICE_REMOVAL, (WPARAM)nStatus, NULL);
}

static void GOBICALLBACK CallInfoCallback(  arrCallInfo* inInfo,	
											arrRemotePartyNum* 	remoteNumInfo,
											arrRemotePartyName* remoteName,
											arrAlertingType* 	alertType,
											arrSvcOption*		servieOpt,
											arrCallEndReason*   callEnd,
											arrAlphaIDInfo* alphaIDInfo,
											arrPeerNumInfo*  connectedPartyInfo,
											arrDiagInfo* diagInfo,
											arrPeerNumInfo* calledPartyInfo,
											arrPeerNumInfo* redirectInfo,
											arrAlertingPattern* alertPattern)
{
	TCallInfoCallback * callinfo;
	callinfo = (TCallInfoCallback *)malloc(sizeof(TCallInfoCallback));
	memset(callinfo, 0, sizeof(TCallInfoCallback));
	if (callinfo)
	{
		if (inInfo)
		{
			callinfo->inInfo=(arrCallInfo*)malloc(sizeof(arrCallInfo));
			if (callinfo->inInfo) memcpy(callinfo->inInfo, inInfo, sizeof(arrCallInfo));
		}
		if (remoteNumInfo)
		{
			callinfo->remoteNumInfo=(arrRemotePartyNum*)malloc(sizeof(arrRemotePartyNum));
			if (callinfo->remoteNumInfo) memcpy(callinfo->remoteNumInfo, remoteNumInfo, sizeof(arrRemotePartyNum));
		}
		if (remoteName)
		{
			callinfo->remoteName=(arrRemotePartyName*)malloc(sizeof(arrRemotePartyName));
			if (callinfo->remoteName) memcpy(callinfo->remoteName, remoteName, sizeof(arrRemotePartyName));
		}
		if (alertType)
		{
			callinfo->alertType=(arrAlertingType*)malloc(sizeof(arrAlertingType));
			if (callinfo->alertType) memcpy(callinfo->alertType, alertType, sizeof(arrAlertingType));
		}
		if (servieOpt)
		{
			callinfo->servieOpt=(arrSvcOption*)malloc(sizeof(arrSvcOption));
			if (callinfo->servieOpt) memcpy(callinfo->servieOpt, servieOpt, sizeof(arrSvcOption));
		}
		if (callEnd)
		{
			callinfo->callEnd=(arrCallEndReason*)malloc(sizeof(arrCallEndReason));
			if (callinfo->callEnd) memcpy(callinfo->callEnd, callEnd, sizeof(arrCallEndReason));
		}
		if (alphaIDInfo)
		{
			callinfo->alphaIDInfo=(arrAlphaIDInfo*)malloc(sizeof(arrAlphaIDInfo));
			if (callinfo->alphaIDInfo) memcpy(callinfo->alphaIDInfo, alphaIDInfo, sizeof(arrAlphaIDInfo));
		}
		if (connectedPartyInfo)
		{
			callinfo->connectedPartyInfo=(arrPeerNumInfo*)malloc(sizeof(arrPeerNumInfo));
			if (callinfo->connectedPartyInfo) memcpy(callinfo->connectedPartyInfo, connectedPartyInfo, sizeof(arrPeerNumInfo));
		}
		if (diagInfo)
		{
			callinfo->diagInfo=(arrDiagInfo*)malloc(sizeof(arrDiagInfo));
			if (callinfo->diagInfo) memcpy(callinfo->diagInfo, diagInfo, sizeof(arrDiagInfo));
		}	
		if (calledPartyInfo)
		{
			callinfo->calledPartyInfo=(arrPeerNumInfo*)malloc(sizeof(arrPeerNumInfo));
			if (callinfo->calledPartyInfo) memcpy(callinfo->calledPartyInfo, calledPartyInfo, sizeof(arrPeerNumInfo));
		}	
		if (redirectInfo)
		{
			callinfo->redirectInfo=(arrPeerNumInfo*)malloc(sizeof(arrPeerNumInfo));
			if (callinfo->redirectInfo) memcpy(callinfo->redirectInfo, redirectInfo, sizeof(arrPeerNumInfo));
		}	
		if (alertPattern)
		{
			callinfo->alertPattern=(arrAlertingPattern*)malloc(sizeof(arrAlertingPattern));
			if (callinfo->alertPattern) memcpy(callinfo->alertPattern, alertPattern, sizeof(arrAlertingPattern));
		}
		g_This->PostMessage(WM_USER_NOTIFY_CALLINFO, (WPARAM)callinfo, NULL);
	}

}


void CVoiceDialerDlg::OnTimer(UINT_PTR nIDEvent)
{
	switch (nIDEvent)
	{
		// search for available device
		case TIMER_DeviceSearch:
			KillTimer(nIDEvent);
			if (DeviceSearch())
			{
				// device found

				// Assign GOBI callback handlers
				SwiSetDeviceRemovalCallback(DeviceRemovalCallback);
                SetCallInfoCallback(CallInfoCallback);

    	        m_editStatus.SetWindowText(_T("Connected to Device"));


			}
			else
			{
				// Device not found
    	        m_editStatus.SetWindowText(_T("Unable find device"));
                //m_btnSend.EnableWindow(FALSE);
				m_DeviceSearchTimer=SetTimer(TIMER_DeviceSearch, 1000, NULL);
            }
			break;
		default:
			break;
	}

	CDialog::OnTimer(nIDEvent);
}



LRESULT CVoiceDialerDlg::OnCallInfoCallback(WPARAM wParam, LPARAM lParam)
{
	TCallInfoCallback *info;
	info=(TCallInfoCallback*)wParam;
	if (info)
	{
		if (info->inInfo)
		{
			memcpy(&m_callinfo, info->inInfo, sizeof(arrCallInfo));
			for (int i =0 ; i<info->inInfo->numInstances; i++)
			{
				if (info->inInfo->callInfo[i].callState==2)
				{
					CString str;
					str.FormatMessage(L"QmiID:%1!d! call incoming", info->inInfo->callInfo[i].callID);
					this->lst_Log.InsertString(0, str);
				}
				if (info->inInfo->callInfo[i].callState==3)
				{
					CString str;
					str.FormatMessage(L"QmiID:%1!d! call connected", info->inInfo->callInfo[i].callID);
					this->lst_Log.InsertString(0, str);
				}
				else if (info->inInfo->callInfo[i].callState==9)
				{
					CString str;
					str.FormatMessage(L"QmiID:%1!d! call ended", info->inInfo->callInfo[i].callID);
					this->lst_Log.InsertString(0, str);
					//we will not receive another update for this call, we can remove it from local cache now.
					m_callinfo.numInstances--;
					m_callinfo.callInfo[i].callState=0;
					
				}
				
				/*else
				{
					CString str;
					str.FormatMessage(L"call QmiID:%1!d! state %2!d!", info->inInfo->callInfo[i].callID, info->inInfo->callInfo[i].callState);
					this->lst_Log.InsertString(0, str);
				}*/
			}
		}
		for (int i =0 ; i<info->remoteNumInfo->numInstances; i++)
		{
			if (info->remoteNumInfo->rmtPtyNum[i].number[0])
			{
				USES_CONVERSION;
				CString str;
				str.FormatMessage(L"caller id %1!s!", CA2W(info->remoteNumInfo->rmtPtyNum[i].number));
				this->lst_Log.InsertString(0, str);
			}
		}
		if (info->inInfo) free(info->inInfo);
		if (info->remoteNumInfo) free(info->remoteNumInfo);
		if (info->remoteName) free(info->remoteName);
		if (info->alertType) free(info->alertType);
		if (info->servieOpt) free(info->servieOpt);
		if (info->callEnd) free(info->callEnd);
		if (info->alphaIDInfo) free(info->alphaIDInfo);
		if (info->connectedPartyInfo) free(info->connectedPartyInfo);
		if (info->diagInfo) free(info->diagInfo);
		if (info->calledPartyInfo) free(info->calledPartyInfo);
		if (info->redirectInfo) free(info->redirectInfo);
		if (info->alertPattern) free(info->alertPattern);
		free(info);
	}

	return 0;
}
LRESULT CVoiceDialerDlg::OnNotifDeviceRemoval(WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);

	// Shutdown API
	QCWWANDisconnect();
	
	memset(&m_callinfo, 0, sizeof(arrCallInfo));

	// start timer to search for available device
	SetTimer(TIMER_DeviceSearch, 1000, NULL);
	this->m_editStatus.SetWindowTextW(L"Searching for device...");

	return 0;
}


void CVoiceDialerDlg::OnOK() 
{
	OnCancel();
}

void CVoiceDialerDlg::OnCancel() 
{
	// clear callback handlers
	SwiSetDeviceRemovalCallback(NULL);

	// disconnect from device
	QCWWANDisconnect();
    g_This = NULL;
	
	CDialog::OnCancel();
}

//Talk
void CVoiceDialerDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
	if (m_callinfo.numInstances)
	{
		for (int i=0;i<m_callinfo.numInstances;i++)
		{
			if (m_callinfo.callInfo[i].callState==0x02)
			{
				CString str;
				str.FormatMessage(L"Answering call ID %1!d!", m_callinfo.callInfo[i].callID);
				this->lst_Log.InsertString(0, str);
				AnswerCall(&m_callinfo.callInfo[i].callID, NULL);			
				break;
			}
		}
	}
	else
	{
		USES_CONVERSION;
		BYTE			CallID;
		TCHAR wphoneno[80];
		tDialReqParam 	dialParams = {0};
		phoneno.GetWindowTextW(wphoneno, 80);
		
		sprintf(dialParams.callNumber, CW2A(wphoneno));
		DialCall(&dialParams, &CallID, NULL, NULL, NULL);
	}
}


//End
void CVoiceDialerDlg::OnBnClickedButton4()
{
	// TODO: Add your control notification handler code here
	for (int i=0;i<m_callinfo.numInstances;i++)
	{
		if (
			(m_callinfo.callInfo[i].callState==0x02)||//Incoming
			(m_callinfo.callInfo[i].callState==0x03)||//Conversation
			(m_callinfo.callInfo[i].callState==0x05)||//paging
			(m_callinfo.callInfo[i].callState==0x06)||//hold
			(m_callinfo.callInfo[i].callState==0x07)//waiting
			)
		{
			CString str;
			str.FormatMessage(L"Ending call ID %1!d!", m_callinfo.callInfo[i].callID);
			this->lst_Log.InsertString(0, str);
			EndCall(&m_callinfo.callInfo[i].callID, NULL);
		}

	}
}




void CVoiceDialerDlg::OnBnClickedBtnget()
{
	// TODO: Add your control notification handler code here
	m_combo_cmd.GetCurSel();
	CString command;
	m_combo_cmd.GetLBText(m_combo_cmd.GetCurSel(), command);
	int i = 0;
	ULONG ret = 0;
	TCHAR wbuf[50];
	int maxBuf = 50;
	if(0 == command.Compare(L"CFG"))
	{
		//WCHAR paramBuf[100];
		//int maxParamBuf = 100;
		BYTE profile;
		AudioConfig AudioCfg[6];
		BYTE size = 6;

		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		
		ret = SwiM2MAV_AudioGetAVCFG(AudioCfg, &size);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", AudioCfg[profile].PIFACEID);
			m_edit_id.SetWindowText(wbuf);
			wsprintf(wbuf, L"%d", AudioCfg[profile].ACDBDevice);
			m_edit_device.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"Profile"))
	{
		BYTE generator, profile, earmute, micmute, outGenerator, volume, cwtMute;
		m_edit_generator.GetWindowText(wbuf, maxBuf);
		generator = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioGetProfile(&generator, &profile, &earmute, &micmute, &outGenerator, &volume, &cwtMute);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", profile);
			m_edit_profile.SetWindowText(wbuf);
			wsprintf(wbuf, L"%d", earmute);
			m_edit_earmute.SetWindowText(wbuf);
			wsprintf(wbuf, L"%d", micmute);
			m_edit_micmute.SetWindowText(wbuf);
			wsprintf(wbuf, L"%d", outGenerator);
			m_edit_generator.SetWindowText(wbuf);
			wsprintf(wbuf, L"%d", volume);
			m_edit_volume.SetWindowText(wbuf);
			wsprintf(wbuf, L"%d", cwtMute);
			m_edit_cwt.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"Volume"))
	{
		BYTE generator, profile, volume;
		m_edit_generator.GetWindowText(wbuf, maxBuf);
		generator = (BYTE) _wtoi(wbuf);
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioGetVolume(&profile, &generator, &volume);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", volume);
			m_edit_volume.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVMute"))
	{
		BYTE profile, earmute, micmute, cwtMute;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioGetAVMute(&profile, &earmute, &micmute, &cwtMute);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", earmute);
			m_edit_earmute.SetWindowText(wbuf);
			wsprintf(wbuf, L"%d", micmute);
			m_edit_micmute.SetWindowText(wbuf);
			wsprintf(wbuf, L"%d", cwtMute);
			m_edit_cwt.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"STG"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE)_wtoi(wbuf);
		ret = SwiM2MAV_AudioGetSTG(&profile, &gain);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", gain);
			m_edit_gain.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"SpeakerGain"))
	{
		BYTE profile;
		USHORT gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE)_wtoi(wbuf);
		ret = SwiM2MAV_AudioGetSpeakerGain(&profile, &gain);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", gain);
			m_edit_gain.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"MicGain"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioGetMicGain(&profile, &gain);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", gain);
			m_edit_gain.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVTXG"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioGetAVTXG(&profile, &gain);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", gain);
			m_edit_gain.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVRXG"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioGetAVRXG(&profile, &gain);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", gain);
			m_edit_gain.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"VolDB"))
	{
		BYTE profile, generator, level;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);

		m_edit_generator.GetWindowText(wbuf, maxBuf);
		generator = (BYTE) _wtoi(wbuf);

		m_edit_level.GetWindowText(wbuf, maxBuf);
		level = (BYTE) _wtoi(wbuf);
		
		ret = SwiM2MAV_AudioGetVolDB(&profile, &generator, &level, &gain);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", gain);
			m_edit_gain.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"TxVol"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);

		ret = SwiM2MAV_AudioGetTxVolume(&profile, &gain);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", gain);
			m_edit_gain.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"TXPCMIIR"))
	{
		PCMIIRFilter coeff;
		BYTE profile, opMode, uSwitch, stage;
		bool bSwitch = (BST_CHECKED == m_check_switch.GetCheck());
		bool bCoeff = (BST_CHECKED == m_check_coeff.GetCheck());
		if (bSwitch)
		{
			m_edit_profile.GetWindowText(wbuf, maxBuf);
			profile = (BYTE) _wtoi(wbuf);
			m_edit_switch.GetWindowText(wbuf, maxBuf);
			uSwitch = (BYTE) _wtoi(wbuf);
			opMode = 0;
			//ret = SwiM2MAV_AudioGetTXPCMIIR(&profile, &opMode, &uSwitch, &stage, NULL);
			if (!ret)
			{
				wsprintf(wbuf, L"%d", uSwitch);
				m_edit_switch.SetWindowText(wbuf);
				wsprintf(wbuf, L"%d", stage);
				m_edit_stage.SetWindowText(wbuf);
			}
			wsprintf(wbuf, L"%d", ret);
			m_edit_error.SetWindowText(wbuf);
		}
		else if (bCoeff)
		{
			m_edit_profile.GetWindowText(wbuf, maxBuf);
			profile = (BYTE) _wtoi(wbuf);
			m_edit_stage.GetWindowText(wbuf, maxBuf);
			stage = (BYTE) _wtoi(wbuf);
			opMode = stage;
			//ret = SwiM2MAV_AudioGetTXPCMIIR(&profile, &opMode, NULL, &stage, &coeff);
			if (!ret)
			{
				wsprintf(wbuf, L"%08X", coeff.B0);
				m_edit_b0.SetWindowText(wbuf);
				wsprintf(wbuf, L"%08X", coeff.B1);
				m_edit_b1.SetWindowText(wbuf);
				wsprintf(wbuf, L"%08X", coeff.B2);
				m_edit_b2.SetWindowText(wbuf);
				wsprintf(wbuf, L"%08X", coeff.A1);
				m_edit_a1.SetWindowText(wbuf);
				wsprintf(wbuf, L"%08X", coeff.A2);
				m_edit_a2.SetWindowText(wbuf);
			}
			wsprintf(wbuf, L"%d", ret);
			m_edit_error.SetWindowText(wbuf);
		}
	}
	else if (0 == command.Compare(L"RXPCMIIR"))
	{
		PCMIIRFilter coeff;
		BYTE profile, opMode, uSwitch, stage;
		bool bSwitch = (BST_CHECKED == m_check_switch.GetCheck());
		bool bCoeff = (BST_CHECKED == m_check_coeff.GetCheck());
		if (bSwitch)
		{
			m_edit_profile.GetWindowText(wbuf, maxBuf);
			profile = (BYTE) _wtoi(wbuf);
			m_edit_switch.GetWindowText(wbuf, maxBuf);
			uSwitch = (BYTE) _wtoi(wbuf);
			opMode = 0;
			//ret = SwiM2MAV_AudioGetRXPCMIIR(&profile, &opMode, &uSwitch, &stage, NULL);
			if (!ret)
			{
				wsprintf(wbuf, L"%d", uSwitch);
				m_edit_switch.SetWindowText(wbuf);
				wsprintf(wbuf, L"%d", stage);
				m_edit_stage.SetWindowText(wbuf);
			}
			wsprintf(wbuf, L"%d", ret);
			m_edit_error.SetWindowText(wbuf);
		}
		else if (bCoeff)
		{
			m_edit_profile.GetWindowText(wbuf, maxBuf);
			profile = (BYTE) _wtoi(wbuf);
			m_edit_stage.GetWindowText(wbuf, maxBuf);
			stage = (BYTE) _wtoi(wbuf);
			opMode = stage;
			//ret = SwiM2MAV_AudioGetRXPCMIIR(&profile, &opMode, NULL, &stage, &coeff);
			if (!ret)
			{
				wsprintf(wbuf, L"%08X", coeff.B0);
				m_edit_b0.SetWindowText(wbuf);
				wsprintf(wbuf, L"%08X", coeff.B1);
				m_edit_b1.SetWindowText(wbuf);
				wsprintf(wbuf, L"%08X", coeff.B2);
				m_edit_b2.SetWindowText(wbuf);
				wsprintf(wbuf, L"%08X", coeff.A1);
				m_edit_a1.SetWindowText(wbuf);
				wsprintf(wbuf, L"%08X", coeff.A2);
				m_edit_a2.SetWindowText(wbuf);
			}
			wsprintf(wbuf, L"%d", ret);
			m_edit_error.SetWindowText(wbuf);
		}
	}
	else if (0 == command.Compare(L"AVEC"))
	{
		BYTE profile, uSwitch;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		
		ret = SwiM2MAV_AudioGetAVEC(&profile, &uSwitch);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", uSwitch);
			m_edit_switch.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVNS"))
	{
		BYTE profile, uSwitch;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		
		ret = SwiM2MAV_AudioGetAVNS(&profile, &uSwitch);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", uSwitch);
			m_edit_switch.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}/* GET */
}

void CVoiceDialerDlg::OnBnClickedBtnset()
{
	// TODO: Add your control notification handler code here
	m_combo_cmd.GetCurSel();
	CString command;
	m_combo_cmd.GetLBText(m_combo_cmd.GetCurSel(), command);
	int i = 0;
	ULONG ret = 0;
	TCHAR wbuf[50];
	int maxBuf = 50;
	if(0 == command.Compare(L"CFG"))
	{
		WCHAR paramBuf[100];
		int maxParamBuf = 100;
		BYTE device, profile, id;
		PIFACETable m_pifaceTable;
		PIFACETable* pPifaceTable = NULL;

		m_edit_device.GetWindowText(wbuf, maxBuf);
		device = (BYTE) _wtoi(wbuf);
		
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		
		m_edit_id.GetWindowText(wbuf, maxBuf);
		id = (BYTE) _wtoi(wbuf);
		
		if (id == 0)
		{
			m_edit_param.GetWindowText(paramBuf, maxParamBuf);
			m_pifaceTable.PIFACEParamLen = wcslen(paramBuf)/2;
			for (int j = 0; j < m_pifaceTable.PIFACEParamLen && j < MAX_PIFACE_TABLE_SIZE; j++)
			{
				WCHAR tmp[50];
				wcscpy(tmp, paramBuf+j*2);
				tmp[2]=0x0;
				m_pifaceTable.PIFACEParam[j] = (BYTE) wcstoul(tmp, NULL, 16);
			}
			pPifaceTable = &m_pifaceTable;
		}
		ret = SwiM2MAV_AudioSetAVCFG(&profile, &device, &id, pPifaceTable);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"Profile"))
	{
		BYTE generator, profile, earmute, micmute, volume, cwtMute;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_generator.GetWindowText(wbuf, maxBuf);
		generator = (BYTE) _wtoi(wbuf);
		m_edit_earmute.GetWindowText(wbuf, maxBuf);
		earmute = (BYTE) _wtoi(wbuf);
		m_edit_micmute.GetWindowText(wbuf, maxBuf);
		micmute = (BYTE) _wtoi(wbuf);
		m_edit_volume.GetWindowText(wbuf, maxBuf);
		volume = (BYTE) _wtoi(wbuf);
		m_edit_cwt.GetWindowText(wbuf, maxBuf);
		cwtMute = (BYTE) _wtoi(wbuf);

		ret = SwiM2MAV_AudioSetProfile(&profile, &earmute, &micmute, &generator, &volume, &cwtMute);
		if (!ret)
		{
			wsprintf(wbuf, L"%d", generator);
			m_edit_generator.SetWindowText(wbuf);
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"Volume"))
	{
		BYTE generator, profile, volume;
		m_edit_generator.GetWindowText(wbuf, maxBuf);
		generator = (BYTE) _wtoi(wbuf);
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_volume.GetWindowText(wbuf, maxBuf);
		volume = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetVolume(&profile, &generator, &volume);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVMute"))
	{
		BYTE profile, earmute, micmute, cwtMute;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_earmute.GetWindowText(wbuf, maxBuf);
		earmute = (BYTE) _wtoi(wbuf);
		m_edit_micmute.GetWindowText(wbuf, maxBuf);
		micmute = (BYTE) _wtoi(wbuf);
		m_edit_cwt.GetWindowText(wbuf, maxBuf);
		cwtMute = (BYTE) _wtoi(wbuf);
		
		ret = SwiM2MAV_AudioSetAVMute(&profile, &earmute, &micmute, &cwtMute);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"STG"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_gain.GetWindowText(wbuf, maxBuf);
		gain = (WORD) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetSTG(&profile, &gain);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"SpeakerGain"))
	{
		BYTE profile;
		USHORT gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_gain.GetWindowText(wbuf, maxBuf);
		gain = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetSpeakerGain(&profile, &gain);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"MicGain"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_gain.GetWindowText(wbuf, maxBuf);
		gain = (WORD) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetMicGain(&profile, &gain);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVTXG"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_gain.GetWindowText(wbuf, maxBuf);
		gain = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetAVTXG(&profile, &gain);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVRXG"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_gain.GetWindowText(wbuf, maxBuf);
		gain = (WORD) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetAVRXG(&profile, &gain);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"VolDB"))
	{
		BYTE profile, generator, level;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);

		m_edit_generator.GetWindowText(wbuf, maxBuf);
		generator = (BYTE) _wtoi(wbuf);

		m_edit_level.GetWindowText(wbuf, maxBuf);
		level = (BYTE) _wtoi(wbuf);
		
		m_edit_gain.GetWindowText(wbuf, maxBuf);
		gain = (WORD) _wtoi(wbuf);
		
		ret = SwiM2MAV_AudioSetVolDB(&profile, &generator, &level, &gain);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if  (0 == command.Compare(L"TxVol"))
	{
		BYTE profile;
		WORD gain;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		
		m_edit_gain.GetWindowText(wbuf, maxBuf);
		gain = (WORD) _wtoi(wbuf);
		
		ret = SwiM2MAV_AudioSetTxVolume(&profile, &gain);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"NVDEF"))
	{
		ret = SwiM2MAV_AudioNVDef();
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"LPBK"))
	{
		BYTE enable;
		m_edit_loopback.GetWindowText(wbuf, maxBuf);
		enable = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetLPBK(&enable);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"TXPCMIIR"))
	{
		PCMIIRSetConfig	 config;
		PCMIIRSetCoeff	coeff;
		PCMIIRSetConfig* pConfig = NULL;
		PCMIIRSetCoeff* pCoeff = NULL;
		bool bSwitch = (BST_CHECKED == m_check_switch.GetCheck());
		bool bCoeff = (BST_CHECKED == m_check_coeff.GetCheck());
		if (bSwitch)		
		{
			m_edit_profile.GetWindowText(wbuf, maxBuf);
			config.Profile = (BYTE) _wtoi(wbuf);
			m_edit_stage.GetWindowText(wbuf, maxBuf);
			config.Stage = (BYTE) _wtoi(wbuf);
			m_edit_switch.GetWindowText(wbuf, maxBuf);
			config.Switch = (BYTE) _wtoi(wbuf);
			pConfig = &config;
		}
		if (bCoeff)
		{
			m_edit_profile.GetWindowText(wbuf, maxBuf);
			coeff.Profile = (BYTE) _wtoi(wbuf);
			m_edit_stage.GetWindowText(wbuf, maxBuf);
			coeff.Stage = (BYTE) _wtoi(wbuf);
			m_edit_b0.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.B0 = wcstoul(wbuf, NULL, 16);
			m_edit_b1.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.B1 = wcstoul(wbuf, NULL, 16);
			m_edit_b2.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.B2 = wcstoul(wbuf, NULL, 16);
			m_edit_a1.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.A1 = wcstoul(wbuf, NULL, 16);
			m_edit_a2.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.A2 = wcstoul(wbuf, NULL, 16);
			pCoeff = & coeff;
		}
		//ret = SwiM2MAV_AudioSetTXPCMIIR(pConfig, pCoeff);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"RXPCMIIR"))
	{
		PCMIIRSetConfig	 config;
		PCMIIRSetCoeff	coeff;
		PCMIIRSetConfig* pConfig = NULL;
		PCMIIRSetCoeff* pCoeff = NULL;
		bool bSwitch = (BST_CHECKED == m_check_switch.GetCheck());
		bool bCoeff = (BST_CHECKED == m_check_coeff.GetCheck());
		if (bSwitch)		
		{
			m_edit_profile.GetWindowText(wbuf, maxBuf);
			config.Profile = (BYTE) _wtoi(wbuf);
			m_edit_stage.GetWindowText(wbuf, maxBuf);
			config.Stage = (BYTE) _wtoi(wbuf);
			m_edit_switch.GetWindowText(wbuf, maxBuf);
			config.Switch = (BYTE) _wtoi(wbuf);
			pConfig = &config;
		}
		if (bCoeff)
		{
			m_edit_profile.GetWindowText(wbuf, maxBuf);
			coeff.Profile = (BYTE) _wtoi(wbuf);
			m_edit_stage.GetWindowText(wbuf, maxBuf);
			coeff.Stage = (BYTE) _wtoi(wbuf);
			m_edit_b0.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.B0 = wcstoul(wbuf, NULL, 16);
			m_edit_b1.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.B1 = wcstoul(wbuf, NULL, 16);
			m_edit_b2.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.B2 = wcstoul(wbuf, NULL, 16);
			m_edit_a1.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.A1 = wcstoul(wbuf, NULL, 16);
			m_edit_a2.GetWindowText(wbuf, maxBuf);
			coeff.Coeff.A2 = wcstoul(wbuf, NULL, 16);
			pCoeff = & coeff;
		}
		//ret = SwiM2MAV_AudioSetRXPCMIIR(pConfig, pCoeff);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVEC"))
	{
		BYTE profile, uSwitch;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_switch.GetWindowText(wbuf, maxBuf);
		uSwitch = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetAVEC(&profile, &uSwitch);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}
	else if (0 == command.Compare(L"AVNS"))
	{
		BYTE profile, uSwitch;
		m_edit_profile.GetWindowText(wbuf, maxBuf);
		profile = (BYTE) _wtoi(wbuf);
		m_edit_switch.GetWindowText(wbuf, maxBuf);
		uSwitch = (BYTE) _wtoi(wbuf);
		ret = SwiM2MAV_AudioSetAVNS(&profile, &uSwitch);
		if (!ret)
		{
			
		}
		wsprintf(wbuf, L"%d", ret);
		m_edit_error.SetWindowText(wbuf);
	}/* SET */
}

void CVoiceDialerDlg::OnBnClickedBtnclear()
{
	// TODO: Add your control notification handler code here
	if (!m_check_keep.GetCheck())
	{
		m_edit_profile.SetWindowText(L"");
		m_edit_generator.SetWindowText(L"");
	}
	m_edit_earmute.SetWindowText(L"");
	m_edit_micmute.SetWindowText(L"");
	m_edit_volume.SetWindowText(L"");
	m_edit_cwt.SetWindowText(L"");
	m_edit_error.SetWindowText(L"");
	m_edit_gain.SetWindowText(L"");
	m_edit_level.SetWindowText(L"");
	m_edit_loopback.SetWindowText(L"");

	m_edit_switch.SetWindowText(L"");
	m_edit_stage.SetWindowText(L"");
	m_edit_b0.SetWindowText(L"");
	m_edit_b1.SetWindowText(L"");
	m_edit_b2.SetWindowText(L"");
	m_edit_a1.SetWindowText(L"");
	m_edit_a2.SetWindowText(L"");
}
