
// TCL ToolsDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "TCL Copy Tool.h"
#include "TCL Copy ToolDlg.h"
#include "afxdialogex.h"
#include "ChassisConfigDlg.h"
#include "filehelper.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

std::string g_true_host;
HHOOK CTCLCopyToolDlg::m_hHook;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

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

	// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CTCLCopyToolDlg 对话框



CTCLCopyToolDlg::CTCLCopyToolDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_TCLCOPYTOOL_DIALOG, pParent)
	, m_str_pid(_T(""))
	, m_str_sn(_T(""))
	, m_str_bid(_T(""))
{
	m_pCurChassis = nullptr;
	m_bRunning = FALSE;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTCLCopyToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, COMBO_COM, m_cb_com);
	DDX_Control(pDX, COMBO_BD, m_cb_baudrate);
	DDX_Control(pDX, COMBO_MODE, m_cb_mode);
	DDX_Control(pDX, COMBO_CHASSIS, m_cb_chassis);
	// 是否抄写控件;
	DDX_Control(pDX, CHECK_PID, m_check_pid);
	DDX_Control(pDX, CHECK_CHANNEL, m_check_channel);
	DDX_Control(pDX, CHECK_OSD_LANG, m_check_osd_lang);
	DDX_Control(pDX, CHECK_SHOP_LANG, m_check_shop_lang);
	DDX_Control(pDX, CHECK_DID, m_check_did);
	DDX_Control(pDX, CHECK_MAC, m_check_mac);
	DDX_Control(pDX, CHECK_HDCP, m_check_hdcp);
	DDX_Control(pDX, CHECK_HDCP22, m_check_hdcp22);
	DDX_Control(pDX, CHECK_WIDI, m_check_widi);
	DDX_Control(pDX, CHECK_WIDEVINE, m_check_widevine);
	DDX_Control(pDX, CHECK_ESN, m_check_esn);
	DDX_Control(pDX, CHECK_CIKEY, m_check_cikey);
	DDX_Control(pDX, CHECK_MTK_INIT, m_check_mtk_init);
	DDX_Control(pDX, CHECK_WB_INIT, m_check_wb_init);
	// 下拉框;
	DDX_Control(pDX, COMBO_CHANNEL, m_cb_channel);
	DDX_Control(pDX, COMBO_OSD_LANG, m_cb_osd_lang);
	DDX_Control(pDX, COMBO_SHOP_LANG, m_cb_shop_lang);
	// 余量显示控件;
	DDX_Control(pDX, STATIC_DID_COUNT, m_lb_did_count);
	DDX_Control(pDX, STATIC_MAC_COUNT, m_lb_mac_count);
	DDX_Control(pDX, STATIC_HDCP_COUNT, m_lb_hdcp_count);
	DDX_Control(pDX, STATIC_HDCP22_COUNT, m_lb_hdcp22_count);
	DDX_Control(pDX, STATIC_WIDI_COUNT, m_lb_widi_count);
	DDX_Control(pDX, STATIC_WIDEVINE_COUNT, m_lb_widevine_count);
	DDX_Control(pDX, STATIC_ESN_COUNT, m_lb_esn_count);
	DDX_Control(pDX, STATIC_CI_COUNT, m_lb_cikey_count);
	// 写状态显示控件;
	DDX_Control(pDX, STATIC_PID_WRITE, m_lb_pid_write);
	DDX_Control(pDX, STATIC_CHANNEL_WRITE, m_lb_channel_write);
	DDX_Control(pDX, STATIC_OSD_LANG_WRITE, m_lb_osd_lang_write);
	DDX_Control(pDX, STATIC_SHOP_LANG_WRITE, m_lb_shop_lang_write);

	DDX_Control(pDX, STATIC_DID_WRITE, m_lb_did_write);
	DDX_Control(pDX, STATIC_MAC_WRITE, m_lb_mac_write);
	DDX_Control(pDX, STATIC_HDCP_WRITE, m_lb_hdcp_write);
	DDX_Control(pDX, STATIC_HDCP22_WRITE, m_lb_hdcp22_write);
	DDX_Control(pDX, STATIC_WIDI_WRITE, m_lb_widi_write);
	DDX_Control(pDX, STATIC_WIDEVINE_WRITE, m_lb_widevine_write);
	DDX_Control(pDX, STATIC_ESN_WRITE, m_lb_esn_write);
	DDX_Control(pDX, STATIC_CI_WRITE, m_lb_cikey_write);
	// 检测状态显示控件;
	DDX_Control(pDX, STATIC_PID_CHECK, m_lb_pid_check);
	DDX_Control(pDX, STATIC_CHANNEL_CHECK, m_lb_channel_check);
	DDX_Control(pDX, STATIC_OSD_LANG_CHECK, m_lb_osd_lang_check);
	DDX_Control(pDX, STATIC_SHOP_LANG_CHECK, m_lb_shop_lang_check);

	DDX_Control(pDX, STATIC_DID_CHECK, m_lb_did_check);
	DDX_Control(pDX, STATIC_MAC_CHECK, m_lb_mac_check);
	DDX_Control(pDX, STATIC_HDCP_CHECK, m_lb_hdcp_check);
	DDX_Control(pDX, STATIC_HDCP22_CHECK, m_lb_hdcp22_check);
	DDX_Control(pDX, STATIC_WIDI_CHECK, m_lb_widi_check);
	DDX_Control(pDX, STATIC_WIDEVINE_CHECK, m_lb_widevine_check);
	DDX_Control(pDX, STATIC_ESN_CHECK, m_lb_esn_check);
	DDX_Control(pDX, STATIC_CI_CHECK, m_lb_cikey_check);
	// 读验证状态显示控件;
	DDX_Control(pDX, STATIC_PID_READ, m_lb_pid_read);
	DDX_Control(pDX, STATIC_CHANNEL_READ, m_lb_channel_read);
	DDX_Control(pDX, STATIC_OSD_LANG_READ, m_lb_osd_lang_read);
	DDX_Control(pDX, STATIC_SHOP_LANG_READ, m_lb_shop_lang_read);

	DDX_Control(pDX, STATIC_DID_READ, m_lb_did_read);
	DDX_Control(pDX, STATIC_MAC_READ, m_lb_mac_read);
	DDX_Control(pDX, STATIC_HDCP_READ, m_lb_hdcp_read);
	DDX_Control(pDX, STATIC_HDCP22_READ, m_lb_hdcp22_read);
	DDX_Control(pDX, STATIC_WIDI_READ, m_lb_widi_read);
	DDX_Control(pDX, STATIC_WIDEVINE_READ, m_lb_widevine_read);
	DDX_Control(pDX, STATIC_ESN_READ, m_lb_esn_read);
	DDX_Control(pDX, STATIC_CI_READ, m_lb_cikey_read);
	DDX_Text(pDX, EDIT_PID, m_str_pid);
	DDX_Control(pDX, STATIC_FACTORY_MODE, m_lb_enter_factory_mode);
	DDX_Control(pDX, STATIC_ENTER_FACTORY_MODE, m_lb_enter_factory_mode_status);
	DDX_Control(pDX, STATIC_COLUMN_A1, m_lb_column_a1);
	DDX_Control(pDX, STATIC_COLUMN_A2, m_lb_column_a2);
	DDX_Control(pDX, STATIC_COLUMN_A3, m_lb_column_a3);
	DDX_Control(pDX, STATIC_COLUMN_A4, m_lb_column_a4);
	DDX_Control(pDX, STATIC_COLUMN_A5, m_lb_column_a5);

	DDX_Control(pDX, STATIC_COLUMN_B1, m_lb_column_b1);
	DDX_Control(pDX, STATIC_COLUMN_B2, m_lb_column_b2);
	DDX_Control(pDX, STATIC_COLUMN_B3, m_lb_column_b3);
	DDX_Control(pDX, STATIC_COLUMN_B4, m_lb_column_b4);
	DDX_Control(pDX, STATIC_COLUMN_B5, m_lb_column_b5);
	DDX_Control(pDX, STATIC_RESULT, m_lb_result);
	DDX_Control(pDX, BTN_HIDE, m_btn_hide);
	DDX_Control(pDX, STATIC_WB_INIT, m_lb_wb_init);
	DDX_Text(pDX, EDIT_ORDER, m_str_bid);
	DDX_Control(pDX, CHECK_WB_WRITE, m_check_wb_write);
	DDX_Control(pDX, STATIC_WB_WRITE, m_lb_wb_write);
	DDX_Control(pDX, RICHEDIT2_LOG, m_edit_log);
	DDX_Control(pDX, CHECK_VERIFY_PID, m_check_verify_pid);
	DDX_Control(pDX, CHECK_VERIFY_CLIENT_TYPE, m_check_verify_client_type);
	DDX_Control(pDX, CHECK_VERIFY_SOFT_VERSION, m_check_verify_soft_version);
	DDX_Control(pDX, CHECK_SET, m_check_set);
	DDX_Control(pDX, IDCANCEL, m_btn_cancel);
	DDX_Control(pDX, BTN_START, m_btn_manual_start);
	DDX_Control(pDX, CHECK_FOCUS, m_check_focus);
	DDX_Control(pDX, CHECK_LOCK, m_check_lock);
	DDX_Control(pDX, IDC_STATIC_TEST_MODE, m_lb_test_mode);
}

BEGIN_MESSAGE_MAP(CTCLCopyToolDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_WM_DEVICECHANGE()
	ON_BN_CLICKED(BTN_HIDE, &CTCLCopyToolDlg::OnBnClickedHide)
	ON_CBN_SELCHANGE(COMBO_MODE, &CTCLCopyToolDlg::OnCbnSelchangeMode)
	ON_CBN_SELCHANGE(COMBO_CHASSIS, &CTCLCopyToolDlg::OnCbnSelchangeChassis)
	ON_BN_CLICKED(BTN_START, &CTCLCopyToolDlg::OnBnClickedStart)
	//	ON_WM_CTLCOLOR()
	ON_WM_CTLCOLOR()
	//ON_WM_ERASEBKGND()
	ON_EN_CHANGE(EDIT_SN, &CTCLCopyToolDlg::OnEnChangeSn)
	ON_BN_CLICKED(BTN_WB_FILE, &CTCLCopyToolDlg::OnBnClickedWbFile)
	ON_BN_CLICKED(CHECK_LOCK, &CTCLCopyToolDlg::OnBnClickedLock)
	ON_BN_CLICKED(CHECK_FOCUS, &CTCLCopyToolDlg::OnBnClickedFocus)
	ON_EN_CHANGE(EDIT_ORDER, &CTCLCopyToolDlg::OnEnChangeOrder)
	ON_COMMAND_RANGE(CHECK_DID, CHECK_CIKEY, OnGetKeyCountClicked)

	ON_BN_CLICKED(BTN_CONFIG, &CTCLCopyToolDlg::OnBnClickedConfig)
	ON_CBN_SELCHANGE(COMBO_COM, &CTCLCopyToolDlg::OnCbnSelchangeCom)
	ON_CBN_SELCHANGE(COMBO_BD, &CTCLCopyToolDlg::OnCbnSelchangeBd)
END_MESSAGE_MAP()


// CTCLCopyToolDlg 消息处理程序

BOOL CTCLCopyToolDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

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

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	SetWindowTitle("");

	ShowWindow(SW_NORMAL);
	CenterWindow();

	// 定时器，一直设置焦点控件;
	SetTimer(0, 500, NULL);
	g_siacp.SetMainDlg(this);
	g_ota.SetMainDlg(this);
	// TODO: 在此添加额外的初始化代码
	Global::GetSysSerialPort(m_vtCOM);
	InitCombobox_port();
	InitCombobox_baudrate();
	InitCtrl();
	UpdateKeyCount();
#if ENABLE_CHASSIS_CONFIG
	GetDlgItem(BTN_CONFIG)->ShowWindow(SW_SHOW);
#endif

#ifdef TEST
	SetTimer(1, 5000, NULL);
#endif
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

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

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CTCLCopyToolDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
#if 0
		CRect rect;
		CPaintDC dc(this);
		GetClientRect(rect);
		dc.FillSolidRect(rect, DEFAULT_DLG_COLOR); //设置为绿色背景
#endif
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTCLCopyToolDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CTCLCopyToolDlg::InitCtrl()
{
	// 测试模式提醒;
	m_lb_test_mode.SetText(_T("Test Mode"));
	m_lb_test_mode.SetBkColor(RGB(255, 255, 0));
	m_lb_test_mode.SetTextColor(WARN_TEXT_COLOR);
	m_lb_test_mode.SetFontSize(8);

	// 按钮;
	m_btn_hide.SetFlat();
	m_btn_hide.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	m_btn_hide.OffsetColor(CButtonST::BTNST_COLOR_FG_IN, 30);
	m_btn_hide.SetColor(CButtonST::BTNST_COLOR_BK_IN, RGB(255, 255, 0));//背景颜色;
	m_btn_hide.SetColor(CButtonST::BTNST_COLOR_BK_OUT, RGB(255, 255, 0));//背景颜色;
	m_btn_hide.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS, RGB(255, 250, 11));//背景颜色;

	m_btn_cancel.SetFlat(FALSE);
	m_btn_cancel.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	m_btn_cancel.OffsetColor(CButtonST::BTNST_COLOR_FG_IN, 30);
	m_btn_cancel.SetColor(CButtonST::BTNST_COLOR_BK_IN, RGB(255, 255, 0));//背景颜色;
	m_btn_cancel.SetColor(CButtonST::BTNST_COLOR_BK_OUT, RGB(255, 255, 0));//背景颜色;
	m_btn_cancel.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS, RGB(255, 250, 11));//背景颜色;

	m_btn_manual_start.SetFlat(FALSE);
	m_btn_manual_start.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	m_btn_manual_start.OffsetColor(CButtonST::BTNST_COLOR_FG_IN, 30);
	m_btn_manual_start.SetColor(CButtonST::BTNST_COLOR_BK_IN, RGB(128, 255, 128));//背景颜色;
	m_btn_manual_start.SetColor(CButtonST::BTNST_COLOR_BK_OUT, RGB(128, 255, 128));//背景颜色;
	m_btn_manual_start.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS, RGB(128, 250, 11));//背景颜色;

	// combobox
	m_cb_com.SelectString(0, Global::g_tConfig.com.c_str());
	m_cb_baudrate.SelectString(0, Global::g_tConfig.baudrate.c_str());
	g_siacp.OpenComm(Global::g_tConfig.com.c_str(), _ttol(Global::g_tConfig.baudrate.c_str()));
#ifdef TEST
	Global::g_tConfig.mode = 0;
	keybd_event(VK_F2, 0, 0, 0);
	keybd_event(VK_F2, 0, KEYEVENTF_KEYUP, 0);
	SetDlgItemText(EDIT_ORDER, _T("IDX141819B"));
#endif
	m_cb_mode.SetCurSel(Global::g_tConfig.mode);
	EnableChassisCheckbox();

	GetDlgItem(EDIT_ORDER)->EnableWindow(Global::g_tConfig.mode < 2);
	GetDlgItem(EDIT_SN)->EnableWindow(Global::g_tConfig.mode < 3);
	GetDlgItem(CHECK_LOCK)->EnableWindow(Global::g_tConfig.mode < 2);
	GetDlgItem(CHECK_FOCUS)->EnableWindow(Global::g_tConfig.mode < 3);

	// 设置wb file;
	if (Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis) != Global::g_tConfig.chassislist.end())
	{
		SetDlgItemText(STATIC_WB_FILE, Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis)->second.WBFile.c_str());
		if (Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis)->second.WBFile.size())
		{// 加载wb file;
			std::string wbfile = Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis)->second.WBFile;
			m_chWBNormalBuffer[R] = GetPrivateProfileInt(_T("WBA value:"), _T("HDMI white R:"), NULL, wbfile.c_str());
			m_chWBNormalBuffer[G] = GetPrivateProfileInt(_T("WBA value:"), _T("HDMI white G:"), NULL, wbfile.c_str());
			m_chWBNormalBuffer[B] = GetPrivateProfileInt(_T("WBA value:"), _T("HDMI white B:"), NULL, wbfile.c_str());
			m_chWBWarmBuffer[R] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Warm white R:"), NULL, wbfile.c_str());
			m_chWBWarmBuffer[G] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Warm white G:"), NULL, wbfile.c_str());
			m_chWBWarmBuffer[B] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Warm white B:"), NULL, wbfile.c_str());
			m_chWBCoolBuffer[R] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Cool white R:"), NULL, wbfile.c_str());
			m_chWBCoolBuffer[G] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Cool white G:"), NULL, wbfile.c_str());
			m_chWBCoolBuffer[B] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Cool white B:"), NULL, wbfile.c_str());
		}
	}

	// 填充combobox;
	std::map<std::string, Global::TChassis>::iterator it = Global::g_tConfig.chassislist.begin();
	for (; it != Global::g_tConfig.chassislist.end(); it++)
	{
		m_cb_chassis.AddString(it->first.c_str());
	}

	m_cb_chassis.EnableWindow(Global::g_tConfig.mode > 1);
	if (Global::g_tConfig.mode > 1)
	{
		m_cb_chassis.SelectString(0, Global::g_tConfig.chassis.c_str());
	}
	else
	{
		m_cb_chassis.SetCurSel(-1);
	}

	EnableChassisCheckbox();

	if (Global::g_tConfig.mode > 1)
	{// chassis
		it = Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis);
		if (it != Global::g_tConfig.chassislist.end())
		{
			// check box;
			if (it->second.ProjectID != 0)
			{
				SetDlgItemInt(EDIT_PID, it->second.ProjectID);
				m_check_pid.SetCheck(it->second.IsWritePID);
			}
			else
			{
				m_check_pid.SetCheck(FALSE);
			}
			m_check_channel.SetCheck(it->second.IsCopyChannel);
			m_check_osd_lang.SetCheck(it->second.IsOsdLanguage);
			m_check_shop_lang.SetCheck(it->second.IsShopLanguage);
			m_check_did.SetCheck(it->second.IsCopyDID);
			m_check_mac.SetCheck(it->second.IsCopyMAC);
			m_check_hdcp.SetCheck(it->second.IsCopyKEY);
			m_check_hdcp22.SetCheck(it->second.IsCopyKEY2_2);
			m_check_widi.SetCheck(it->second.IsCopyWiDi);
			m_check_widevine.SetCheck(it->second.IsCopyWidevine);
			m_check_esn.SetCheck(it->second.IsCopyESN);
			m_check_cikey.SetCheck(it->second.IsCopyCikey);
			m_check_mtk_init.SetCheck(it->second.IsMTKInit);
			m_check_wb_init.SetCheck(it->second.IsWBInit);
			m_check_wb_write.SetCheck(it->second.IsCopyWB);

			// combobox;
			for (auto it : Global::g_tConfig.channel)
			{
				m_cb_channel.AddString(it.first.c_str());
			}

			for (auto it : Global::g_tConfig.language)
			{
				m_cb_osd_lang.AddString(it.first.c_str());
			}

			for (auto it : Global::g_tConfig.language)
			{
				m_cb_shop_lang.AddString(it.first.c_str());
			}
		}
	}

	// 其他单独初始化;
	m_lb_enter_factory_mode.SetBorder();
	m_lb_enter_factory_mode.SetFontBold();
	m_lb_enter_factory_mode.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_enter_factory_mode.SetTextColor(NORMAL_TEXT_COLOR);
	m_lb_enter_factory_mode.SetFontSize(10);
	m_lb_enter_factory_mode.SetFontName(_T("微软雅黑"));

	m_lb_enter_factory_mode_status.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_enter_factory_mode_status.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_enter_factory_mode_status.SetFontSize(10);
	m_lb_enter_factory_mode_status.SetFontName(_T("微软雅黑"));

	// 结果显示 ;
	m_lb_result.SetBorder();
	m_lb_result.SetFontBold();
	m_lb_result.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_result.SetTextColor(NORMAL_TEXT_COLOR);
	m_lb_result.SetFontSize(10);
	m_lb_result.SetFontName(_T("微软雅黑"));

	// 其他控件初始化;
	InitColumn();
	InitKeyCountLabel();
	InitWriteStatusLabel();
	InitReadStatusLabel();
	InitCheckStatusLabel();
	InitLogCharFormat();

#if 1
	m_hThread = CreateThread(NULL, 0, ThreadGetMessage, this, 0, &m_dwThreadId);
	if (m_hThread)
	{
		CloseHandle(m_hThread);
	}
#endif
}

void CTCLCopyToolDlg::InitColumn()
{
	//////////////////////////////////////////////////////////////////////////
	m_lb_column_a1.SetFontItalic();
	m_lb_column_a2.SetFontItalic();
	m_lb_column_a3.SetFontItalic();
	m_lb_column_a4.SetFontItalic();
	m_lb_column_a5.SetFontItalic();
	m_lb_column_b1.SetFontItalic();
	m_lb_column_b2.SetFontItalic();
	m_lb_column_b3.SetFontItalic();
	m_lb_column_b4.SetFontItalic();
	m_lb_column_b5.SetFontItalic();
	//////////////////////////////////////////////////////////////////////////
// 	m_lb_column_a1.SetFontBold();
// 	m_lb_column_a2.SetFontBold();
// 	m_lb_column_a3.SetFontBold();
// 	m_lb_column_a4.SetFontBold();
// 	m_lb_column_a5.SetFontBold();
// 	m_lb_column_b1.SetFontBold();
// 	m_lb_column_b2.SetFontBold();
// 	m_lb_column_b3.SetFontBold();
// 	m_lb_column_b4.SetFontBold();
// 	m_lb_column_b5.SetFontBold();
	//////////////////////////////////////////////////////////////////////////
	m_lb_column_a1.SetBorder();
	m_lb_column_a2.SetBorder();
	m_lb_column_a3.SetBorder();
	m_lb_column_a4.SetBorder();
	m_lb_column_a5.SetBorder();
	m_lb_column_b1.SetBorder();
	m_lb_column_b2.SetBorder();
	m_lb_column_b3.SetBorder();
	m_lb_column_b4.SetBorder();
	m_lb_column_b5.SetBorder();
	//////////////////////////////////////////////////////////////////////////
	m_lb_column_a1.SetFontSize(10);
	m_lb_column_a2.SetFontSize(10);
	m_lb_column_a3.SetFontSize(10);
	m_lb_column_a4.SetFontSize(10);
	m_lb_column_a5.SetFontSize(10);
	m_lb_column_b1.SetFontSize(10);
	m_lb_column_b2.SetFontSize(10);
	m_lb_column_b3.SetFontSize(10);
	m_lb_column_b4.SetFontSize(10);
	m_lb_column_b5.SetFontSize(10);
	//////////////////////////////////////////////////////////////////////////
	m_lb_column_a1.SetFontName(_T("微软雅黑"));
	m_lb_column_a2.SetFontName(_T("微软雅黑"));
	m_lb_column_a3.SetFontName(_T("微软雅黑"));
	m_lb_column_a4.SetFontName(_T("微软雅黑"));
	m_lb_column_a5.SetFontName(_T("微软雅黑"));
	m_lb_column_b1.SetFontName(_T("微软雅黑"));
	m_lb_column_b2.SetFontName(_T("微软雅黑"));
	m_lb_column_b3.SetFontName(_T("微软雅黑"));
	m_lb_column_b4.SetFontName(_T("微软雅黑"));
	m_lb_column_b5.SetFontName(_T("微软雅黑"));
	//////////////////////////////////////////////////////////////////////////
	m_lb_column_a1.SetTextColor(RGB(26, 72, 204));
	m_lb_column_a2.SetTextColor(RGB(26, 72, 204));
	m_lb_column_a3.SetTextColor(RGB(26, 72, 204));
	m_lb_column_a4.SetTextColor(RGB(26, 72, 204));
	m_lb_column_a5.SetTextColor(RGB(26, 72, 204));
	m_lb_column_b1.SetTextColor(RGB(26, 72, 204));
	m_lb_column_b2.SetTextColor(RGB(26, 72, 204));
	m_lb_column_b3.SetTextColor(RGB(26, 72, 204));
	m_lb_column_b4.SetTextColor(RGB(26, 72, 204));
	m_lb_column_b5.SetTextColor(RGB(26, 72, 204));
	//////////////////////////////////////////////////////////////////////////
	m_lb_column_a1.SetBkColor(RGB(153, 217, 234));
	m_lb_column_a2.SetBkColor(RGB(153, 217, 234));
	m_lb_column_a3.SetBkColor(RGB(153, 217, 234));
	m_lb_column_a4.SetBkColor(RGB(153, 217, 234));
	m_lb_column_a5.SetBkColor(RGB(153, 217, 234));
	m_lb_column_b1.SetBkColor(RGB(153, 217, 234));
	m_lb_column_b2.SetBkColor(RGB(153, 217, 234));
	m_lb_column_b3.SetBkColor(RGB(153, 217, 234));
	m_lb_column_b4.SetBkColor(RGB(153, 217, 234));
	m_lb_column_b5.SetBkColor(RGB(153, 217, 234));
}

void CTCLCopyToolDlg::InitCombobox_port()
{
	CComboBox* pCommbox = ((CComboBox*)GetDlgItem(COMBO_COM));
	for (std::vector<std::string>::iterator it = m_vtCOM.begin(); it != m_vtCOM.end(); it++)
	{
		if (!IsPortInserted(it->c_str()))
			pCommbox->AddString(it->c_str());
	}
}

void CTCLCopyToolDlg::InitCombobox_baudrate()
{
	CComboBox* pCommbox = ((CComboBox*)GetDlgItem(COMBO_BD));
	pCommbox = ((CComboBox*)GetDlgItem(COMBO_BD));
	pCommbox->AddString(_T("4800"));
	pCommbox->AddString(_T("9600"));
	pCommbox->AddString(_T("19200"));
	pCommbox->AddString(_T("38400"));
	pCommbox->AddString(_T("115200"));
}

void CTCLCopyToolDlg::InitCombobox_Chassis()
{
	int nCount = m_cb_chassis.GetCount();
	if (nCount == 0 || nCount != Global::g_tConfig.chassislist.size())
	{
		CString strChassis = _T("");
		int nCurSel = m_cb_chassis.GetCurSel();
		if (nCurSel != CB_ERR)
		{
			m_cb_chassis.GetLBText(nCurSel, strChassis);
		}

		m_cb_chassis.ResetContent();
		std::map<std::string, Global::TChassis>::iterator it = Global::g_tConfig.chassislist.begin();
		for (; it != Global::g_tConfig.chassislist.end(); it++)
		{
			m_cb_chassis.AddString(it->first.c_str());
		}

		if (strChassis.IsEmpty() && Global::g_tConfig.mode > 1)
		{
			m_cb_chassis.SetCurSel(0);
			m_cb_chassis.GetLBText(0, strChassis);
		}
		else
			m_cb_chassis.SelectString(0, strChassis.GetString());

		if (Global::g_tConfig.mode > 1)
		{
			m_pCurChassis = &Global::g_tConfig.chassislist.find(strChassis.GetString())->second;
			UpdateChassisCheckBoxStatus();
		}
	}
}

void CTCLCopyToolDlg::InitKeyCountLabel()
{
	// did 余量控件;
	m_lb_did_count.SetText(_T("-----"));
	m_lb_did_count.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_did_count.SetFontSize(12);
	m_lb_did_count.SetFontName(_T("Arial"));

	m_lb_mac_count.SetText(_T("-----"));
	m_lb_mac_count.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_mac_count.SetFontSize(12);
	m_lb_mac_count.SetFontName(_T("Arial"));

	m_lb_hdcp_count.SetText(_T("-----"));
	m_lb_hdcp_count.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_hdcp_count.SetFontSize(12);
	m_lb_hdcp_count.SetFontName(_T("Arial"));

	m_lb_hdcp22_count.SetText(_T("-----"));
	m_lb_hdcp22_count.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_hdcp22_count.SetFontSize(12);
	m_lb_hdcp22_count.SetFontName(_T("Arial"));

	m_lb_widi_count.SetText(_T("-----"));
	m_lb_widi_count.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_widi_count.SetFontSize(12);
	m_lb_widi_count.SetFontName(_T("Arial"));

	m_lb_widevine_count.SetText(_T("-----"));
	m_lb_widevine_count.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_widevine_count.SetFontSize(12);
	m_lb_widevine_count.SetFontName(_T("Arial"));

	m_lb_esn_count.SetText(_T("-----"));
	m_lb_esn_count.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_esn_count.SetFontSize(12);
	m_lb_esn_count.SetFontName(_T("Arial"));

	m_lb_cikey_count.SetText(_T("-----"));
	m_lb_cikey_count.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_cikey_count.SetFontSize(12);
	m_lb_cikey_count.SetFontName(_T("Arial"));
}

void CTCLCopyToolDlg::InitWriteStatusLabel()
{
	m_lb_wb_init.SetText(_T("NT"));
	m_lb_wb_init.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_wb_init.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_wb_init.SetFontSize(10);
	m_lb_wb_init.SetFontName(_T("微软雅黑"));

	m_lb_pid_write.SetText(_T("NT"));
	m_lb_pid_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_pid_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_pid_write.SetFontSize(10);
	m_lb_pid_write.SetFontName(_T("微软雅黑"));

	m_lb_channel_write.SetText(_T("NT"));
	m_lb_channel_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_channel_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_channel_write.SetFontSize(10);
	m_lb_channel_write.SetFontName(_T("微软雅黑"));

	m_lb_osd_lang_write.SetText(_T("NT"));
	m_lb_osd_lang_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_osd_lang_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_osd_lang_write.SetFontSize(10);
	m_lb_osd_lang_write.SetFontName(_T("微软雅黑"));

	m_lb_shop_lang_write.SetText(_T("NT"));
	m_lb_shop_lang_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_shop_lang_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_shop_lang_write.SetFontSize(10);
	m_lb_shop_lang_write.SetFontName(_T("微软雅黑"));

	//////////////////////////////////////////////////////////////////////////
	m_lb_did_write.SetText(_T("NT"));
	m_lb_did_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_did_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_did_write.SetFontSize(10);
	m_lb_did_write.SetFontName(_T("微软雅黑"));

	m_lb_mac_write.SetText(_T("NT"));
	m_lb_mac_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_mac_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_mac_write.SetFontSize(10);
	m_lb_mac_write.SetFontName(_T("微软雅黑"));

	m_lb_hdcp_write.SetText(_T("NT"));
	m_lb_hdcp_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_hdcp_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_hdcp_write.SetFontSize(10);
	m_lb_hdcp_write.SetFontName(_T("微软雅黑"));

	m_lb_hdcp22_write.SetText(_T("NT"));
	m_lb_hdcp22_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_hdcp22_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_hdcp22_write.SetFontSize(10);
	m_lb_hdcp22_write.SetFontName(_T("微软雅黑"));

	m_lb_widi_write.SetText(_T("NT"));
	m_lb_widi_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_widi_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_widi_write.SetFontSize(10);
	m_lb_widi_write.SetFontName(_T("微软雅黑"));

	m_lb_widevine_write.SetText(_T("NT"));
	m_lb_widevine_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_widevine_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_widevine_write.SetFontSize(10);
	m_lb_widevine_write.SetFontName(_T("微软雅黑"));

	m_lb_esn_write.SetText(_T("NT"));
	m_lb_esn_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_esn_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_esn_write.SetFontSize(10);
	m_lb_esn_write.SetFontName(_T("微软雅黑"));

	m_lb_cikey_write.SetText(_T("NT"));
	m_lb_cikey_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_cikey_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_cikey_write.SetFontSize(10);
	m_lb_cikey_write.SetFontName(_T("微软雅黑"));

	m_lb_wb_write.SetText(_T("NT"));
	m_lb_wb_write.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_wb_write.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_wb_write.SetFontSize(10);
	m_lb_wb_write.SetFontName(_T("微软雅黑"));
}

void CTCLCopyToolDlg::InitReadStatusLabel()
{
	m_lb_pid_read.SetText(_T("NT"));
	m_lb_pid_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_pid_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_pid_read.SetFontSize(10);
	m_lb_pid_read.SetFontName(_T("微软雅黑"));

	m_lb_channel_read.SetText(_T("NT"));
	m_lb_channel_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_channel_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_channel_read.SetFontSize(10);
	m_lb_channel_read.SetFontName(_T("微软雅黑"));

	m_lb_osd_lang_read.SetText(_T("NT"));
	m_lb_osd_lang_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_osd_lang_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_osd_lang_read.SetFontSize(10);
	m_lb_osd_lang_read.SetFontName(_T("微软雅黑"));

	m_lb_shop_lang_read.SetText(_T("NT"));
	m_lb_shop_lang_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_shop_lang_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_shop_lang_read.SetFontSize(10);
	m_lb_shop_lang_read.SetFontName(_T("微软雅黑"));

	//////////////////////////////////////////////////////////////////////////
	m_lb_did_read.SetText(_T("NT"));
	m_lb_did_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_did_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_did_read.SetFontSize(10);
	m_lb_did_read.SetFontName(_T("微软雅黑"));

	m_lb_mac_read.SetText(_T("NT"));
	m_lb_mac_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_mac_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_mac_read.SetFontSize(10);
	m_lb_mac_read.SetFontName(_T("微软雅黑"));

	m_lb_hdcp_read.SetText(_T("NT"));
	m_lb_hdcp_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_hdcp_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_hdcp_read.SetFontSize(10);
	m_lb_hdcp_read.SetFontName(_T("微软雅黑"));

	m_lb_hdcp22_read.SetText(_T("NT"));
	m_lb_hdcp22_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_hdcp22_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_hdcp22_read.SetFontSize(10);
	m_lb_hdcp22_read.SetFontName(_T("微软雅黑"));

	m_lb_widi_read.SetText(_T("NT"));
	m_lb_widi_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_widi_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_widi_read.SetFontSize(10);
	m_lb_widi_read.SetFontName(_T("微软雅黑"));

	m_lb_widevine_read.SetText(_T("NT"));
	m_lb_widevine_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_widevine_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_widevine_read.SetFontSize(10);
	m_lb_widevine_read.SetFontName(_T("微软雅黑"));

	m_lb_esn_read.SetText(_T("NT"));
	m_lb_esn_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_esn_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_esn_read.SetFontSize(10);
	m_lb_esn_read.SetFontName(_T("微软雅黑"));

	m_lb_cikey_read.SetText(_T("NT"));
	m_lb_cikey_read.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_cikey_read.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_cikey_read.SetFontSize(10);
	m_lb_cikey_read.SetFontName(_T("微软雅黑"));
}

void CTCLCopyToolDlg::InitCheckStatusLabel()
{
	m_lb_pid_check.SetText(_T("NT"));
	m_lb_pid_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_pid_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_pid_check.SetFontSize(10);
	m_lb_pid_check.SetFontName(_T("微软雅黑"));

	m_lb_channel_check.SetText(_T("NT"));
	m_lb_channel_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_channel_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_channel_check.SetFontSize(10);
	m_lb_channel_check.SetFontName(_T("微软雅黑"));

	m_lb_osd_lang_check.SetText(_T("NT"));
	m_lb_osd_lang_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_osd_lang_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_osd_lang_check.SetFontSize(10);
	m_lb_osd_lang_check.SetFontName(_T("微软雅黑"));

	m_lb_shop_lang_check.SetText(_T("NT"));
	m_lb_shop_lang_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_shop_lang_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_shop_lang_check.SetFontSize(10);
	m_lb_shop_lang_check.SetFontName(_T("微软雅黑"));
	//////////////////////////////////////////////////////////////////////////
	m_lb_did_check.SetText(_T("NT"));
	m_lb_did_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_did_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_did_check.SetFontSize(10);
	m_lb_did_check.SetFontName(_T("微软雅黑"));

	m_lb_mac_check.SetText(_T("NT"));
	m_lb_mac_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_mac_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_mac_check.SetFontSize(10);
	m_lb_mac_check.SetFontName(_T("微软雅黑"));

	m_lb_hdcp_check.SetText(_T("NT"));
	m_lb_hdcp_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_hdcp_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_hdcp_check.SetFontSize(10);
	m_lb_hdcp_check.SetFontName(_T("微软雅黑"));

	m_lb_hdcp22_check.SetText(_T("NT"));
	m_lb_hdcp22_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_hdcp22_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_hdcp22_check.SetFontSize(10);
	m_lb_hdcp22_check.SetFontName(_T("微软雅黑"));

	m_lb_widi_check.SetText(_T("NT"));
	m_lb_widi_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_widi_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_widi_check.SetFontSize(10);
	m_lb_widi_check.SetFontName(_T("微软雅黑"));

	m_lb_widevine_check.SetText(_T("NT"));
	m_lb_widevine_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_widevine_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_widevine_check.SetFontSize(10);
	m_lb_widevine_check.SetFontName(_T("微软雅黑"));

	m_lb_esn_check.SetText(_T("NT"));
	m_lb_esn_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_esn_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_esn_check.SetFontSize(10);
	m_lb_esn_check.SetFontName(_T("微软雅黑"));

	m_lb_cikey_check.SetText(_T("NT"));
	m_lb_cikey_check.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_cikey_check.SetTextColor(DEFAULT_TEXT_COLOR);
	m_lb_cikey_check.SetFontSize(10);
	m_lb_cikey_check.SetFontName(_T("微软雅黑"));
}

void CTCLCopyToolDlg::RetSetSiacpStatus()
{
	m_edit_log.SetWindowText(_T(""));
	m_lb_enter_factory_mode_status.SetText(_T("NT"));
	m_lb_enter_factory_mode_status.SetBkColor(DEFAULT_BG_COLOR);
	m_lb_enter_factory_mode_status.SetTextColor(NORMAL_TEXT_COLOR);

	//InitKeyCountLabel();
	InitCheckStatusLabel();
	InitReadStatusLabel();
	InitWriteStatusLabel();
}

// 串口是否已插入commbobox中;
BOOL CTCLCopyToolDlg::IsPortInserted(LPCTSTR lpPort)
{
	CComboBox* pCommbox = ((CComboBox*)GetDlgItem(COMBO_COM));

	BOOL bInserted = FALSE;
	CString strCommPort = _T("");
	int nCount = pCommbox->GetCount();
	for (int i = 0; i < nCount; i++)
	{
		pCommbox->GetLBText(i, strCommPort);
		if (strCommPort.CompareNoCase(lpPort) == 0)
		{
			bInserted = TRUE;
			break;
		}
	}

	return bInserted;
}

void CTCLCopyToolDlg::EnableChassisCheckbox()
{
	BOOL bStatus = FALSE;
	if (Global::g_tConfig.mode == 0)
	{
		bStatus = FALSE;
	}
	else if (Global::g_tConfig.mode == 1)
	{
		bStatus = TRUE;
	}
	else
	{
		bStatus = _tcsicmp(Global::g_tConfig.chassis.c_str(), _T("other")) == 0;
	}

	m_check_did.EnableWindow(bStatus);
	m_check_mac.EnableWindow(bStatus);
	m_check_hdcp.EnableWindow(bStatus);
	m_check_hdcp22.EnableWindow(bStatus);
	m_check_widi.EnableWindow(bStatus);
	m_check_widevine.EnableWindow(bStatus);
	m_check_esn.EnableWindow(bStatus);
	m_check_cikey.EnableWindow(bStatus);
	// 校验;
	m_check_verify_pid.EnableWindow(Global::g_tConfig.mode < 2);
	m_check_verify_client_type.EnableWindow(Global::g_tConfig.mode < 2);
	m_check_verify_soft_version.EnableWindow(Global::g_tConfig.mode < 2);

	if (m_pCurChassis == nullptr && Global::g_tConfig.mode != 0)
	{
		//m_check_wb_write.EnableWindow(bStatus);
		//m_check_mtk_init.EnableWindow(bStatus);
		//m_check_wb_init.EnableWindow(bStatus);
		//m_check_pid.EnableWindow(bStatus);
		//m_check_channel.EnableWindow(bStatus);
		//m_check_osd_lang.EnableWindow(bStatus);
		//m_check_shop_lang.EnableWindow(bStatus);
		//下拉框;
		//m_cb_channel.EnableWindow(bStatus);
		//m_cb_osd_lang.EnableWindow(bStatus);
		//m_cb_shop_lang.EnableWindow(bStatus);
	}

	if (Global::g_tConfig.mode == 0)
	{
		GetDlgItem(EDIT_PID)->EnableWindow(bStatus);
		m_check_pid.EnableWindow(bStatus);
	}
	else
	{
		GetDlgItem(EDIT_PID)->EnableWindow(TRUE);
		m_check_pid.EnableWindow(TRUE);
	}
}

void CTCLCopyToolDlg::UpdateKeyCount(BOOL bUseThread /*= TRUE*/)
{
	static auto _update_key_count = [&]()
	{
		SetOptionLog(CString(_T("Update Key Count now, Please wait...\r")), OK_LOG);
		// 如果是自动在线的，需要获取批次号;
		if (Global::g_tConfig.mode < 2) // 自动在线+人工在线;
		{
			UpdateAutoOnlineMode();
		}
		else // 离线模式+本地模式;
		{
			m_cb_chassis.EnableWindow(FALSE);
			m_cb_mode.EnableWindow(FALSE);
			if (Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis) == Global::g_tConfig.chassislist.end())
			{
				m_cb_chassis.EnableWindow();
				m_cb_mode.EnableWindow();
				return;
			}

			m_pCurChassis = &Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis)->second;
			// 清空余量显示;
			InitKeyCountLabel();
			// 更新Chassis控件状态;
			UpdateChassisCheckBoxStatus();
			// 更新了Chassis后再获取余量;
			SetLabelKeyCount(_T("DeviceID"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("MAC"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("HDCP_KEY"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("HDCP2.2_KEY"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("WiDi"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("Widevine_KEY"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("NETFILX_ESN"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("CI_PLUS_KEY"), Global::g_tConfig.mode);
			m_cb_chassis.EnableWindow();
			m_cb_mode.EnableWindow();
		}

		SetOptionLog(CString(_T("Update Key Count Ok...\r")), OK_LOG);
	};

	if (bUseThread)
	{
		std::thread t([&](CTCLCopyToolDlg* p) {
			p->GetDlgItem(BTN_START)->EnableWindow(FALSE);
			p->GetDlgItem(IDOK)->EnableWindow(FALSE);
			_update_key_count();
			p->GetDlgItem(BTN_START)->EnableWindow(TRUE);
			p->GetDlgItem(IDOK)->EnableWindow(TRUE);
			}, this);
		t.detach();
	}
	else
	{
		_update_key_count();
	}
	Sleep(500);
}

void CTCLCopyToolDlg::SetLabelKeyCount(std::string count, CLabel* pLabel)
{
	// 如果空,设置醒目颜色表示获取余量失败;
	if (count.size() == 0)
	{
		pLabel->SetText(_T("-----"));
		pLabel->SetTextColor(WARN_TEXT_COLOR);
	}
	else
	{
		// 去掉逗号;
		while (count.find_first_of(',') != std::string::npos)
			count.erase(count.find_first_of(','), 1);

		pLabel->SetText(count.c_str());
		pLabel->SetTextColor(_ttoi(count.c_str()) > Global::g_tConfig.warncount ? OK_TEXT_COLOR : WARN_TEXT_COLOR);
	}
}

void CTCLCopyToolDlg::SetLabelKeyCount(std::string key_type, int mode /* =0 */)
{
	std::string count;
	std::string key_value;
	if (m_pCurChassis == nullptr)
	{
		InitKeyCountLabel();
		return;
	}

	if (_tcsicmp(key_type.c_str(), _T("DeviceID")) == 0 && m_check_did.GetCheck())
	{
		//m_check_did.SetCheck(m_pCurChassis->IsCopyDID);
		key_value = m_pCurChassis->ClientType;
		if (mode == 3) // 本地模式;
		{
			count = GetFileCount(_T("DeviceID"));
		}
		else
		{
			g_ota.GetKeyInfo((mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/statdeviceid.do?","devicetype=" + key_value, "count", "value", count);
		}
		SetLabelKeyCount(count, &m_lb_did_count);
	}
	else if (_tcsicmp(key_type.c_str(), _T("MAC")) == 0 && m_check_mac.GetCheck())
	{
		//m_check_mac.SetCheck(m_pCurChassis->IsCopyMAC);
		key_value = m_pCurChassis->MACType;
		if (mode == 3) // 本地模式;
		{
			count = GetFileCount(_T("MAC"));
		}
		else
		{
			g_ota.GetKeyInfo((mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/statmac.do?", "typeString=" + key_value, "count", "value", count);
		}
		SetLabelKeyCount(count, &m_lb_mac_count);
	}
	else if (_tcsicmp(key_type.c_str(), _T("HDCP_KEY")) == 0 && m_check_hdcp.GetCheck())
	{
		//m_check_hdcp.SetCheck(m_pCurChassis->IsCopyKEY);
		key_value = m_pCurChassis->HDCPKeyType;
		if (mode == 3) // 本地模式;
		{
			count = GetFileCount(_T("HDCPKEY"));
		}
		else
		{
			g_ota.GetKeyInfo((mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/stathdcpkey.do?", "type=" + key_value, "count", "value", count);
		}
		SetLabelKeyCount(count, &m_lb_hdcp_count);
	}
	else if (_tcsicmp(key_type.c_str(), _T("CI_PLUS_KEY")) == 0 && m_check_cikey.GetCheck())
	{
		//m_check_cikey.SetCheck(m_pCurChassis->IsCopyCikey);
		key_value = m_pCurChassis->CIKeyType;
		if (mode == 3) // 本地模式;
		{
			count = GetFileCount(_T("CIKEY"));
		}
		else
		{
			g_ota.GetKeyInfo((mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/statcikey.do?", "type=" + key_value, "count", "value", count);
		}
		SetLabelKeyCount(count, &m_lb_cikey_count);
	}
	else if (_tcsicmp(key_type.c_str(), _T("WiDi")) == 0 && m_check_widi.GetCheck())
	{
		//m_check_widi.SetCheck(m_pCurChassis->IsCopyWiDi);
		key_value = m_pCurChassis->WiDiType;
		if (mode == 3) // 本地模式;
		{
			count = GetFileCount(_T("WiDi"));
		}
		else
		{
			g_ota.GetKeyInfo((mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/statwidi.do?", "type=" + key_value, "count", "value", count);
		}
		SetLabelKeyCount(count, &m_lb_widi_count);
	}
	else if (_tcsicmp(key_type.c_str(), _T("Widevine_KEY")) == 0 && m_check_widevine.GetCheck())
	{
		//m_check_widevine.SetCheck(m_pCurChassis->IsCopyWidevine);
		key_value = m_pCurChassis->WidevineType;
		if (mode == 3) // 本地模式;
		{
			count = GetFileCount(_T("Widevine"));
		}
		else
		{
			g_ota.GetKeyInfo((mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/statwidevine.do?", "type=" + key_value, "count", "value", count);
		}
		SetLabelKeyCount(count, &m_lb_widevine_count);
	}
	else if (_tcsicmp(key_type.c_str(), _T("HDCP2.2_KEY")) == 0 && m_check_hdcp22.GetCheck())
	{
		//m_check_hdcp22.SetCheck(m_pCurChassis->IsCopyKEY2_2);
		key_value = m_pCurChassis->HDCPKey22Type;
		if (mode == 3) // 本地模式;
		{
			count = GetFileCount(_T("HDCPKEY22"));
		}
		else
		{
			g_ota.GetKeyInfo((mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/stathdcpkey2.do?", "type=" + key_value, "count", "value", count);
		}
		SetLabelKeyCount(count, &m_lb_hdcp22_count);
	}
	else if (_tcsicmp(key_type.c_str(), _T("NETFILX_ESN")) == 0 && m_check_esn.GetCheck())
	{
		//m_check_esn.SetCheck(m_pCurChassis->IsCopyESN);
		key_value = m_pCurChassis->ESNType;
		if (mode == 3) // 本地模式;
		{
			count = GetFileCount(_T("ESN"));
		}
		else
		{
			g_ota.GetKeyInfo((mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/statnetfilxesn.do?", "type=" + key_value, "count", "value", count);
		}
		SetLabelKeyCount(count, &m_lb_esn_count);
	}
}

void CTCLCopyToolDlg::UpdateChassisCheckBoxStatus()
{
	if (m_pCurChassis != nullptr)
	{
		// check box;
		if (m_pCurChassis->ProjectID != 0)
		{
			SetDlgItemInt(EDIT_PID, m_pCurChassis->ProjectID);
			m_check_pid.SetCheck(m_pCurChassis->IsWritePID);
		}
		else
		{
			m_check_pid.SetCheck(FALSE);
			SetDlgItemInt(EDIT_PID, 0);
		}

		m_check_channel.SetCheck(m_pCurChassis->IsCopyChannel);
		m_check_osd_lang.SetCheck(m_pCurChassis->IsOsdLanguage);
		m_check_shop_lang.SetCheck(m_pCurChassis->IsShopLanguage);
		m_check_did.SetCheck(m_pCurChassis->IsCopyDID);
		m_check_mac.SetCheck(m_pCurChassis->IsCopyMAC);
		m_check_hdcp.SetCheck(m_pCurChassis->IsCopyKEY);
		m_check_hdcp22.SetCheck(m_pCurChassis->IsCopyKEY2_2);
		m_check_widi.SetCheck(m_pCurChassis->IsCopyWiDi);
		m_check_widevine.SetCheck(m_pCurChassis->IsCopyWidevine);
		m_check_esn.SetCheck(m_pCurChassis->IsCopyESN);
		m_check_cikey.SetCheck(m_pCurChassis->IsCopyCikey);
		m_check_mtk_init.SetCheck(m_pCurChassis->IsMTKInit);
		m_check_wb_init.SetCheck(m_pCurChassis->IsWBInit);
		m_check_wb_write.SetCheck(m_pCurChassis->IsCopyWB);
	}
	else
	{
		EnableChassisCheckbox();
	}
}

void CTCLCopyToolDlg::SaveChange2Config()
{
	if (Global::g_tConfig.mode < 2)
		return;
	std::map<std::string, Global::TChassis>::iterator it;
	it = Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis);
	if (it != Global::g_tConfig.chassislist.end())
	{
		it->second.IsWritePID = m_check_pid.GetCheck();
		UpdateData(TRUE);
		it->second.ProjectID = _ttoi(m_str_pid.GetString());
		it->second.IsCopyChannel = m_check_channel.GetCheck();
		it->second.IsOsdLanguage = m_check_osd_lang.GetCheck();
		it->second.IsShopLanguage = m_check_shop_lang.GetCheck();
		it->second.IsCopyDID = m_check_did.GetCheck();
		it->second.IsCopyMAC = m_check_mac.GetCheck();
		it->second.IsCopyKEY = m_check_hdcp.GetCheck();
		it->second.IsCopyKEY2_2 = m_check_hdcp22.GetCheck();
		it->second.IsCopyWiDi = m_check_widi.GetCheck();
		it->second.IsCopyWidevine = m_check_widevine.GetCheck();
		it->second.IsCopyESN = m_check_esn.GetCheck();
		it->second.IsCopyCikey = m_check_cikey.GetCheck();
		it->second.IsMTKInit = m_check_mtk_init.GetCheck();
		it->second.IsWBInit = m_check_wb_init.GetCheck();
		it->second.IsCopyWB = m_check_wb_write.GetCheck();
	}

	int nCurSel = 0;
	CString strCOM = _T(""), strBuadrate = _T("");
	nCurSel = m_cb_com.GetCurSel();
	if (nCurSel != CB_ERR)
	{
		m_cb_com.GetLBText(nCurSel, strCOM);
	}

	nCurSel = m_cb_baudrate.GetCurSel();
	if (nCurSel != CB_ERR)
	{
		m_cb_baudrate.GetLBText(nCurSel, strBuadrate);
	}

	Global::g_tConfig.com = strCOM.GetString();
	Global::g_tConfig.baudrate = strBuadrate.GetString();

	Global::SetConfig();
}

void CTCLCopyToolDlg::UpdateAutoOnlineMode()
{
	CString strBid = _T("");
	GetDlgItemText(EDIT_ORDER, strBid);
	if (strBid.IsEmpty())
	{
		RetSetChassisCheckBox();
		InitKeyCountLabel();
		m_strLastBid.Empty();
		m_pCurChassis = nullptr;
	}
	else
	{
		if (m_strLastBid.CompareNoCase(strBid) != 0)
		{
			if (strBid.GetLength() < 5)
			{
				//SetOptionLog(CString(_T("The length of the order is not enough!\r")), ERROR_LOG);
				return;
			}

			m_strLastBid = strBid;
			// 变更订单号,重置;
			m_check_verify_pid.SetCheck(TRUE);
			m_check_verify_client_type.SetCheck(TRUE);
			m_check_verify_soft_version.SetCheck(TRUE);

			if (g_ota.GetMIDInfo(g_midInfo, strBid.GetString(), Global::g_strMacs))
			{
				if (Global::g_bTestHost)
				{
					g_true_host = g_midInfo.host;
					g_midInfo.host = "http://test.admin.ota.qhmoka.com/IDManage";
				}

				m_pCurChassis = &Global::g_AutoOnlineChassis;
				// 更新check box状态;
				UpdateChassisCheckBoxStatus();
				// 更新Chassis状态后再获取余量;
				SetLabelKeyCount(_T("DeviceID"), Global::g_tConfig.mode);
				SetLabelKeyCount(_T("MAC"), Global::g_tConfig.mode);
				SetLabelKeyCount(_T("HDCP_KEY"), Global::g_tConfig.mode);
				SetLabelKeyCount(_T("HDCP2.2_KEY"), Global::g_tConfig.mode);
				SetLabelKeyCount(_T("WiDi"), Global::g_tConfig.mode);
				SetLabelKeyCount(_T("Widevine_KEY"), Global::g_tConfig.mode);
				SetLabelKeyCount(_T("NETFILX_ESN"), Global::g_tConfig.mode);
				SetLabelKeyCount(_T("CI_PLUS_KEY"), Global::g_tConfig.mode);

				m_edit_log.SetWindowText(_T(""));
				SetOptionLog(CString(_T("Get Mid Info Successful!\r")), OK_LOG);
			}
			else
			{
				m_pCurChassis = nullptr;
				RetSetChassisCheckBox();
				InitKeyCountLabel();
				m_strLastBid.Empty();
				SetOptionLog(CString(_T("Get Mid Info Failed!\r")), ERROR_LOG);
				SetOptionLog(_T("Current PC MAC:") + Global::g_strMacs + _T("\r"), ERROR_LOG);
			}
		}
		else
		{
			SetOptionLog(CString(_T("No change in order number, Use last Mid Info!\r")), OK_LOG);

			SetLabelKeyCount(_T("DeviceID"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("MAC"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("HDCP_KEY"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("HDCP2.2_KEY"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("WiDi"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("Widevine_KEY"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("NETFILX_ESN"), Global::g_tConfig.mode);
			SetLabelKeyCount(_T("CI_PLUS_KEY"), Global::g_tConfig.mode);
		}
	}
}

void CTCLCopyToolDlg::RetSetChassisCheckBox()
{
	m_check_channel.SetCheck(FALSE);
	m_check_osd_lang.SetCheck(FALSE);
	m_check_shop_lang.SetCheck(FALSE);
	m_check_did.SetCheck(FALSE);
	m_check_mac.SetCheck(FALSE);
	m_check_hdcp.SetCheck(FALSE);
	m_check_hdcp22.SetCheck(FALSE);
	m_check_widi.SetCheck(FALSE);
	m_check_widevine.SetCheck(FALSE);
	m_check_esn.SetCheck(FALSE);
	m_check_cikey.SetCheck(FALSE);
	m_check_mtk_init.SetCheck(FALSE);
	m_check_wb_init.SetCheck(FALSE);
	m_check_wb_write.SetCheck(FALSE);
	m_check_pid.SetCheck(FALSE);

	// 清除pid;
	SetDlgItemText(EDIT_PID, _T(""));
}

void CTCLCopyToolDlg::SetLabelResult(int nStatus /* = 0 */, BOOL bWriteLog /*= FALSE*/)
{
	if (nStatus == -1)
	{
		m_lb_result.SetText(_T("NT"));
		m_lb_result.SetFontBold();
		m_lb_result.SetTextColor(NORMAL_TEXT_COLOR);
		m_lb_result.SetBkColor(DEFAULT_BG_COLOR);
	}
	else if (nStatus == 1)
	{
		m_lb_result.SetText(_T("OK"));
		m_lb_result.SetFontBold();
		m_lb_result.SetBkColor(STATUS_OK_COLOR);
		m_lb_result.SetTextColor(RGB(255, 243, 0));
		if (bWriteLog)
			SetOptionLog(CString(_T("\rTest OK!\r")), OK_LOG);
	}
	else if (nStatus == 0)
	{
		m_lb_result.SetText(_T("NG"));
		m_lb_result.SetFontBold();
		m_lb_result.SetBkColor(STATUS_ERROR_COLOR);
		m_lb_result.SetTextColor(RGB(255, 243, 0));
		if (bWriteLog)
			SetOptionLog(CString(_T("\rTest Error!\r")), ERROR_LOG);
	}
}

void CTCLCopyToolDlg::SetSiacpStatus(CLabel& label, BOOL bOK)
{
	label.SetText(bOK ? _T("OK") : _T("NG"));
	label.SetFontBold();
	label.SetBkColor(bOK ? STATUS_OK_COLOR : STATUS_ERROR_COLOR);
	label.SetTextColor(RGB(255, 243, 0));
}

void CTCLCopyToolDlg::SetWindowTitle(std::string client_type)
{
	DWORD sFileVersion[4] = { 0 };
	DWORD sProductVerion[4] = { 0 };
	Global::GetVersion(NULL, sFileVersion, sProductVerion);
	TCHAR szVersion[128] = { 0 };
	_stprintf_s(szVersion, _T("%d.%d.%d.%d"), sFileVersion[0], sFileVersion[1], sFileVersion[2], sFileVersion[3]);

	CString strVer;

	if (client_type.size() == 0)
	{
		strVer.Format(_T("TCL Copy Tool                  Version: %d.%d.%d.%d"),
			sFileVersion[0], sFileVersion[1], sFileVersion[2], sFileVersion[3]);
	}
	else
	{
		if (client_type.find_first_of(_T("SCBC")) != std::string::npos)
			strVer.Format(_T("SCBC User Center Copy Tool                  Version: %d.%d.%d.%d"),
				sFileVersion[0], sFileVersion[1], sFileVersion[2], sFileVersion[3]);
		else
			strVer.Format(_T("Huan User Center Copy Tool                 Version: %d.%d.%d.%d"),
				sFileVersion[0], sFileVersion[1], sFileVersion[2], sFileVersion[3]);
	}

	SetWindowText(strVer);
}

std::string CTCLCopyToolDlg::GetFileCount(std::string key_type)
{
	filehelper fh;
	STR_VEC vtfiles;
	fh.getfolderfiles((_T(".\\DataDir\\") + key_type).c_str(), NULL, &vtfiles);

	TCHAR szKeyCount[10] = { 0 };
	_itoa_s(vtfiles.size(), szKeyCount, 10);

	return std::string(szKeyCount);
}

BOOL CTCLCopyToolDlg::ReadKeyFile(std::string dir, std::string& data)
{
	filehelper fh;
	std::string file;
	fh.find1st(dir, file);

	FILE* pf = NULL;
	if (fopen_s(&pf, file.c_str(), _T("rb")) == 0)
	{
		if (pf)
		{
			fseek(pf, 0, SEEK_END);
			size_t fs = ftell(pf);
			fseek(pf, 0, SEEK_SET);

			byte* pdata = (byte*)malloc(fs);
			if (pdata)
			{
				fread(pdata, fs, 1, pf);
				fclose(pf);

				data.append((char*)pdata, fs);
				free(pdata);

				m_vt_key_files.push_back(file);

				return TRUE;
			}
			fclose(pf);
		}
	}
	return FALSE;
}

BOOL CTCLCopyToolDlg::ReadKeyFile(std::string dir, std::string& data, std::string& file)
{
	filehelper fh;
	fh.find1st(dir, file);

	FILE* pf = NULL;
	if (fopen_s(&pf, file.c_str(), _T("rb")) == 0)
	{
		if (pf)
		{
			fseek(pf, 0, SEEK_END);
			size_t fs = ftell(pf);
			fseek(pf, 0, SEEK_SET);

			byte* pdata = (byte*)malloc(fs);
			if (pdata)
			{
				fread(pdata, fs, 1, pf);
				fclose(pf);

				data.append((char*)pdata, fs);
				free(pdata);

				m_vt_key_files.push_back(file);

				return TRUE;
			}
			fclose(pf);
		}
	}
	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_MTKInit()
{
	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_MTKInit()) == TRUE)
	{
		SetOptionLog(CString(_T("MTK Init Successful!\r")));
		//SetSiacpStatus(m_lb_enter_factory_mode_status, TRUE);
	}
	else
	{
		SetOptionLog(CString(_T("MTK Init Failed!\r")), ERROR_LOG);
		// 输出日志到界面中;
		//SetSiacpStatus(m_lb_enter_factory_mode_status, FALSE);
		SetLabelResult(FALSE);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_EnterFactoryMode()
{
	AUTOTIMING;
	BOOL bRet = FALSE;
	// 进入工厂模式;
	if ((bRet = g_siacp.SCBC_EnterFactory()) == TRUE)
	{
		SetOptionLog(CString(_T("Enter Factory Mode Successful!\r")));
		SetSiacpStatus(m_lb_enter_factory_mode_status, TRUE);
	}
	else
	{
		// 输出日志到界面中;
		SetOptionLog(CString(_T("Enter Factory Mode Failed!\r")), ERROR_LOG);
		SetSiacpStatus(m_lb_enter_factory_mode_status, FALSE);
		SetLabelResult(FALSE);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetClientType(std::string& client_type)
{
	AUTOTIMING;
	BOOL bRet = FALSE;
	// 获取Client Type;
	if ((bRet = g_siacp.SCBC_GetClientType(client_type)) == TRUE)
	{
		SetOptionLog(CString(_T("Get Client Type Successful:\r")));
		SetOptionLog(client_type + _T("\r"), INFO_LOG);
		SetWindowTitle(client_type);
		//SetSiacpStatus(m_lb_enter_factory_mode_status, TRUE);
	}
	else
	{
#if DEL_CLIENT_TYPE_ERROR_LOG
		SetOptionLog(CString(_T("Get Client Type Failed!\r")), NORMAL_LOG);
#else
		// 输出日志到界面中;
		SetOptionLog(CString(_T("Get Client Type Failed!\r")), ERROR_LOG);
#endif
		//SetSiacpStatus(m_lb_enter_factory_mode_status, FALSE);
		SetLabelResult(FALSE);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetSofteVersion(std::string& soft_version)
{
	AUTOTIMING;
	BOOL bRet = FALSE;
	// 时入工厂模式;
	if ((bRet = g_siacp.SCBC_GetSoftVersion(soft_version)) == TRUE)
	{
		SetOptionLog(CString(_T("Get Soft Version Successful:\r")));
		SetOptionLog(soft_version + _T("\r"), INFO_LOG);
		//SetSiacpStatus(m_lb_enter_factory_mode_status, TRUE);
	}
	else
	{
		// 输出日志到界面中;
		SetOptionLog(CString(_T("Get Soft Version Failed!\r")), ERROR_LOG);
		//SetSiacpStatus(m_lb_enter_factory_mode_status, FALSE);
		SetLabelResult(FALSE);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_SetProjectId()
{
	AUTOTIMING;
	if (!m_check_pid.GetCheck() || m_str_pid.IsEmpty())
		return FALSE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_SetProjectId(_ttoi(m_str_pid.GetString()))) == TRUE)
	{
		SetSiacpStatus(m_lb_pid_write);
		SetOptionLog(CString(_T("Write project Successful!\r")));
	}
	else
	{
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_pid_write, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Write project id Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetProjectId()
{
	AUTOTIMING;
	int pid = 0;
	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_GetProjectId(pid)) == TRUE)
	{
		if (_ttoi(m_str_pid.GetString()) == pid)
		{
			SetSiacpStatus(m_lb_pid_read);
			SetOptionLog(CString(_T("Get project Successful:")));
			SetOptionLog(m_str_pid + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_pid_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get project id Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetWB()
{
	AUTOTIMING;
	BOOL bRet1 = g_siacp.SCBC_SetWBNormal(m_chWBNormalBuffer, WB_MAX);

	// 冷模式色温赋值
	BOOL bRet2 = g_siacp.SCBC_SetWBCool(m_chWBCoolBuffer, WB_MAX);

	// 暖模式色温赋值
	BOOL bRet3 = g_siacp.SCBC_SetWBWarm(m_chWBWarmBuffer, WB_MAX);

	BOOL bRet = bRet1 & bRet2 & bRet3;
	if (bRet)
	{
		SetSiacpStatus(m_lb_wb_write);
		SetOptionLog(CString(_T("Write WB Successful!\r")));
	}
	else
	{
		SetSiacpStatus(m_lb_wb_write, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Write WB Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_SetChannel(std::string& channel)
{
	AUTOTIMING;
	int nCurSel = 0;
	if (!m_check_channel.GetCheck() || (nCurSel = m_cb_channel.GetCurSel()) == CB_ERR)
		return TRUE;

	BOOL bRet = FALSE;
	CString strChannel = _T("");
	m_cb_channel.GetLBText(nCurSel, strChannel);
	channel = Global::g_tConfig.channel.find(strChannel.GetString())->second;
	if ((bRet = g_siacp.SCBC_SetChannel(channel)) == TRUE)
	{
		SetSiacpStatus(m_lb_channel_write);
		SetOptionLog(CString(_T("Set Channel Successful!\r")));
	}
	else
	{
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_channel_write, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Set Channel Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetChannel(std::string channel)
{
	AUTOTIMING;
	int nCurSel = 0;
	if (!m_check_channel.GetCheck() || (nCurSel = m_cb_channel.GetCurSel()) == CB_ERR)
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	CString strChannel = _T("");
	m_cb_channel.GetLBText(nCurSel, strChannel);
	if ((bRet = g_siacp.SCBC_GetChannel(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsCHANNEL || _tcsicmp(data.c_str(), channel.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_channel_read);
			SetOptionLog(CString(_T("Get Channel Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_channel_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get Channel Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetOSDLanguage(std::string& language)
{
	AUTOTIMING;
	int nCurSel = 0;
	if (!m_check_osd_lang.GetCheck() || (nCurSel = m_cb_osd_lang.GetCurSel()) == CB_ERR)
		return TRUE;

	BOOL bRet = FALSE;
	CString strLanguage = _T("");
	m_cb_channel.GetLBText(nCurSel, strLanguage);
	language = Global::g_tConfig.language.find(strLanguage.GetString())->second;
	if ((bRet = g_siacp.SCBC_SetOSDLanguage(language)) == TRUE)
	{
		SetSiacpStatus(m_lb_osd_lang_write);
		SetOptionLog(CString(_T("Set OSD Language Successful!\r")));
	}
	else
	{
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_osd_lang_write, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Set OSD Language Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetOSDLanguage(std::string language)
{
	AUTOTIMING;
	int nCurSel = 0;
	if (!m_check_osd_lang.GetCheck() || (nCurSel = m_cb_osd_lang.GetCurSel()) == CB_ERR)
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	CString strLanguage = _T("");
	m_cb_channel.GetLBText(nCurSel, strLanguage);
	if ((bRet = g_siacp.SCBC_GetOSDLanguage(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsOSD || _tcsicmp(data.c_str(), language.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_shop_lang_read);
			SetOptionLog(CString(_T("Get OSD Language Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_shop_lang_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get OSD Language Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetShopLanguage(std::string& language)
{
	AUTOTIMING;
	int nCurSel = 0;
	if (!m_check_shop_lang.GetCheck() || (nCurSel = m_cb_shop_lang.GetCurSel()) == CB_ERR)
		return TRUE;

	BOOL bRet = FALSE;
	CString strLanguage = _T("");
	m_cb_shop_lang.GetLBText(nCurSel, strLanguage);
	language = Global::g_tConfig.language.find(strLanguage.GetString())->second;
	if ((bRet = g_siacp.SCBC_SetShopLanguage(language)) == TRUE)
	{
		SetSiacpStatus(m_lb_shop_lang_write);
		SetOptionLog(CString(_T("Set Shop Language Successful!\r")));
	}
	else
	{
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_shop_lang_write, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Set Shop Language Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetShopLanguage(std::string language)
{
	AUTOTIMING;
	int nCurSel = 0;
	if (!m_check_shop_lang.GetCheck() || (nCurSel = m_cb_shop_lang.GetCurSel()) == CB_ERR)
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	CString strLanguage = _T("");
	m_cb_channel.GetLBText(nCurSel, strLanguage);
	if ((bRet = g_siacp.SCBC_GetShopLanguage(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsSHOP || _tcsicmp(data.c_str(), language.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_shop_lang_read);
			SetOptionLog(CString(_T("Get Shop Language Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_shop_lang_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get Shop Language Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetDeviceId(std::string& deviceid)
{
	AUTOTIMING;
	if (!m_check_did.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if (Global::g_tConfig.mode == 3)
	{
		std::string file;
		if (ReadKeyFile(Global::g_tConfig.keyfolder.find("DIDFolder")->second, deviceid/*, file*/))
		{
			if ((bRet = g_siacp.SCBC_SetDeviceId(deviceid)) == TRUE)
			{
				//DeleteFile(file.c_str());
				SetSiacpStatus(m_lb_did_write);
				SetOptionLog(CString(_T("Set Device id Successful!\r")));
				return TRUE;
			}
		}
	}
	else if (g_ota.GetKeyInfo((Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/getid.do?",
		"devicetype=" + m_pCurChassis->ClientType + "&sn=" + m_str_sn.GetString(), "id", "deviceid", deviceid))
	{
		if ((bRet = g_siacp.SCBC_SetDeviceId(deviceid)) == TRUE)
		{
			SetSiacpStatus(m_lb_did_write);
			SetOptionLog(CString(_T("Set Device id Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_did_write, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Set Device id Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_CheckDeviceId()
{
	AUTOTIMING;
	if (!m_check_did.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_CheckDeviceId()) == TRUE)
	{
		SetSiacpStatus(m_lb_did_check);
		SetOptionLog(CString(_T("Check Device id Successful!\r")));
	}
	else
	{
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_did_check, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Check Device id Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetDeviceId(std::string deviceid)
{
	AUTOTIMING;
	if (!m_check_did.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	if ((bRet = g_siacp.SCBC_GetDeviceId(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsDID || _tcsicmp(deviceid.c_str(), data.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_did_read);
			SetOptionLog(CString(_T("Get Device id Successful:\r")));
			SetOptionLog(data + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_did_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get Device id Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetMac(std::string& mac)
{
	AUTOTIMING;
	if (!m_check_mac.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if (Global::g_tConfig.mode == 3)
	{
		std::string file;
		if (ReadKeyFile(Global::g_tConfig.keyfolder.find("MACFolder")->second, mac/*, file*/))
		{
			if ((bRet = g_siacp.SCBC_SetMAC((byte*)mac.data(), mac.size())) == TRUE)
			{
				//DeleteFile(file.c_str());
				mac = Bytes2HexString((const byte*)mac.c_str(), mac.size(), '-');
				SetSiacpStatus(m_lb_mac_write);
				SetOptionLog(CString(_T("Set MAC Successful!\r")));
				return TRUE;
			}
		}
	}
	else if (g_ota.GetKeyInfo((Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/getmac.do?",
		"typeString=" + m_pCurChassis->MACType + "&sn=" + m_str_sn.GetString(), "mac", "value", mac))
	{
		if ((bRet = g_siacp.SCBC_SetMAC(mac)) == TRUE)
		{
			SetSiacpStatus(m_lb_mac_write);
			SetOptionLog(CString(_T("Set MAC Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_mac_write, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Set MAC Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_CheckMac()
{
	AUTOTIMING;
	if (!m_check_mac.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_CheckMAC()) == TRUE)
	{
		SetSiacpStatus(m_lb_mac_check);
		SetOptionLog(CString(_T("Check MAC Successful!\r")));
	}
	else {
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_mac_check, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Check MAC Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetMac(std::string mac)
{
	AUTOTIMING;
	if (!m_check_mac.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	if ((bRet = g_siacp.SCBC_GetMAC(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsMAC || _tcsicmp(mac.c_str(), data.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_mac_read);
			SetOptionLog(CString(_T("Get MAC Successful:\r")));
			SetOptionLog(data + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_mac_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get MAC Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetHDCP(std::string& hdcp)
{
	AUTOTIMING;
	if (!m_check_hdcp.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if (Global::g_tConfig.mode == 3)
	{
		std::string file;
		if (ReadKeyFile(Global::g_tConfig.keyfolder.find("KEYFolder")->second, hdcp/*, file*/))
		{
			if ((bRet = g_siacp.SCBC_SetHDCPKey((byte*)hdcp.data(), hdcp.size())) == TRUE)
			{
				//DeleteFile(file.c_str());
				hdcp = Bytes2HexString((const byte*)hdcp.c_str(), hdcp.size());
				SetSiacpStatus(m_lb_hdcp_write);
				SetOptionLog(CString(_T("Set HDCP Successful!\r")));
				return TRUE;
			}
		}
	}
	else if (g_ota.GetKeyInfo((Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/gethdcpkey.do?",
		"type=" + m_pCurChassis->HDCPKeyType + "&sn=" + m_str_sn.GetString(), "key", "value", hdcp))
	{
		if ((bRet = g_siacp.SCBC_SetHDCPKey(hdcp)) == TRUE)
		{
			SetSiacpStatus(m_lb_hdcp_write);
			SetOptionLog(CString(_T("Set HDCP Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_hdcp_write, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Set HDCP Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_CheckHDCP()
{
	AUTOTIMING;
	if (!m_check_hdcp.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_CheckHDCP()) == TRUE)
	{
		SetSiacpStatus(m_lb_hdcp_check);
		SetOptionLog(CString(_T("Check HDCP Successful!\r")));
	}
	else {
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_hdcp_check, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Check HDCP Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetHDCP(std::string hdcp)
{
	AUTOTIMING;
	if (!m_check_hdcp.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	if ((bRet = g_siacp.SCBC_GetHDCPKey(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsHDCP || _tcsicmp(hdcp.c_str(), data.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_hdcp_read);
			SetOptionLog(CString(_T("Get HDCP Successful:\r")));
			SetOptionLog(data + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_hdcp_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get HDCP Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetHDCP22(std::string& hdcp22)
{
	AUTOTIMING;
	if (!m_check_hdcp22.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if (Global::g_tConfig.mode == 3)
	{
		std::string file;
		if (ReadKeyFile(Global::g_tConfig.keyfolder.find("HDCPKEY22Folder")->second, hdcp22/*, file*/))
		{
			if ((bRet = g_siacp.SCBC_SetHDCPKey22((byte*)hdcp22.data(), hdcp22.size())) == TRUE)
			{
				//DeleteFile(file.c_str());
				hdcp22 = Bytes2HexString((const byte*)hdcp22.c_str(), hdcp22.size());
				SetSiacpStatus(m_lb_hdcp22_write);
				SetOptionLog(CString(_T("Set HDCP2 Successful!\r")));
				return TRUE;
			}
		}
	}
	else if (g_ota.GetKeyInfo((Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/gethdcpkey2.do?",
		"type=" + m_pCurChassis->HDCPKey22Type + "&sn=" + m_str_sn.GetString(), "key", "value", hdcp22))
	{
		if ((bRet = g_siacp.SCBC_SetHDCPKey22(hdcp22)) == TRUE)
		{
			SetSiacpStatus(m_lb_hdcp22_write);
			SetOptionLog(CString(_T("Set HDCP2 Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_hdcp22_write, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Set HDCP2 Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_CheckHDCP22()
{
	AUTOTIMING;
	if (!m_check_hdcp22.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_CheckHDCP22()) == TRUE)
	{
		SetSiacpStatus(m_lb_hdcp22_check);
		SetOptionLog(CString(_T("Check HDCP2 Successful!\r")));
	}
	else {
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_hdcp22_check, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Check HDCP2 Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetHDCP22(std::string hdcp22)
{
	AUTOTIMING;
	if (!m_check_hdcp22.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	if ((bRet = g_siacp.SCBC_GetHDCPKey22(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsHDCP22 || _tcsicmp(hdcp22.c_str(), data.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_hdcp22_read);
			SetOptionLog(CString(_T("Get HDCP2 Successful:\r")));
			SetOptionLog(data + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_hdcp22_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get HDCP2 Failed!\r")), ERROR_LOG);

	return FALSE;
}


BOOL CTCLCopyToolDlg::Siacp_SetWidi(std::string& widi)
{
	AUTOTIMING;
	if (!m_check_widi.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if (Global::g_tConfig.mode == 3)
	{
		std::string file;
		if (ReadKeyFile(Global::g_tConfig.keyfolder.find("WiDiFolder")->second, widi/*, file*/))
		{
			if ((bRet = g_siacp.SCBC_SetWidi((byte*)widi.data(), widi.size())) == TRUE)
			{
				//DeleteFile(file.c_str());
				widi = Bytes2HexString((const byte*)widi.c_str(), widi.size());
				SetSiacpStatus(m_lb_widi_write);
				SetOptionLog(CString(_T("Set Widi Successful!\r")));
				return TRUE;
			}
		}
	}
	else if (g_ota.GetKeyInfo((Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/getwidi.do?",
		"type=" + m_pCurChassis->WiDiType + "&sn=" + m_str_sn.GetString(), "key", "value", widi))
	{
		if ((bRet = g_siacp.SCBC_SetWidi(widi)) == TRUE)
		{
			SetSiacpStatus(m_lb_widi_write);
			SetOptionLog(CString(_T("Set Widi Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_widi_write, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Set Widi Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_CheckWidi()
{
	AUTOTIMING;
	if (!m_check_widi.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_CheckWidi()) == TRUE)
	{
		SetSiacpStatus(m_lb_widi_check);
		SetOptionLog(CString(_T("Check Widi Successful!\r")));
	}
	else {
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_widi_check, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Check Widi Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetWidi(std::string widi)
{
	AUTOTIMING;
	if (!m_check_widi.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	if ((bRet = g_siacp.SCBC_GetWidi(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsWIDI || _tcsicmp(widi.c_str(), data.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_widi_read);
			SetOptionLog(CString(_T("Get Widi Successful:\r")));
			SetOptionLog(data + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_widi_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get Widi Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetWidevine(std::string& widevine)
{
	AUTOTIMING;
	if (!m_check_widevine.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if (Global::g_tConfig.mode == 3)
	{
		std::string file;
		if (ReadKeyFile(Global::g_tConfig.keyfolder.find("WidevineFolder")->second, widevine/*, file*/))
		{
			if ((bRet = g_siacp.SCBC_SetWidevine((byte*)widevine.data(), widevine.size())) == TRUE)
			{
				//DeleteFile(file.c_str());
				widevine = Bytes2HexString((const byte*)widevine.c_str(), widevine.size());
				SetSiacpStatus(m_lb_widevine_write);
				SetOptionLog(CString(_T("Set widevine Successful!\r")));
				return TRUE;
			}
		}
	}
	else if (g_ota.GetKeyInfo((Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/getwidevine.do?",
		"type=" + m_pCurChassis->WidevineType + "&sn=" + m_str_sn.GetString(), "widevine", "value", widevine))
	{
		if ((bRet = g_siacp.SCBC_SetWidevine(widevine)) == TRUE)
		{
			SetSiacpStatus(m_lb_widevine_write);
			SetOptionLog(CString(_T("Set widevine Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_widevine_write, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Set widevine Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_CheckWidevine()
{
	AUTOTIMING;
	if (!m_check_widevine.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_CheckWidevine()) == TRUE)
	{
		SetSiacpStatus(m_lb_widevine_check);
		SetOptionLog(CString(_T("Check widevine Successful!\r")));
	}
	else {
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_widevine_check, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Check widevine Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetWidevine(std::string widevine)
{
	AUTOTIMING;
	if (!m_check_widevine.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	if ((bRet = g_siacp.SCBC_GetWidevine(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsWIDEVINE || _tcsicmp(widevine.c_str(), data.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_widevine_read);
			SetOptionLog(CString(_T("Get widevine Successful:\r")));
			SetOptionLog(data + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_widevine_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get widevine Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetESN(std::string& esn)
{
	AUTOTIMING;
	if (!m_check_esn.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if (Global::g_tConfig.mode == 3)
	{
		std::string file;
		if (ReadKeyFile(Global::g_tConfig.keyfolder.find("ESNFolder")->second, esn/*, file*/))
		{
			if ((bRet = g_siacp.SCBC_SetNetflixESN((byte*)esn.data(), esn.size())) == TRUE)
			{
				//DeleteFile(file.c_str());
				esn = Bytes2HexString((const byte*)esn.c_str(), esn.size());
				SetSiacpStatus(m_lb_esn_write);
				SetOptionLog(CString(_T("Set Netflix ESN Successful!\r")));
				return TRUE;
			}
		}
	}
	else if (g_ota.GetKeyInfo((Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/getnetfilxesn.do?",
		"type=" + m_pCurChassis->ESNType + "&sn=" + m_str_sn.GetString(), "esn", "value", esn))
	{
		if ((bRet = g_siacp.SCBC_SetNetflixESN(esn)) == TRUE)
		{
			SetSiacpStatus(m_lb_esn_write);
			SetOptionLog(CString(_T("Set Netflix ESN Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_esn_write, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Set Netflix ESN Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_CheckESN()
{
	AUTOTIMING;
	if (!m_check_esn.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_CheckNetflixESN()) == TRUE)
	{
		SetSiacpStatus(m_lb_esn_check);
		SetOptionLog(CString(_T("Check Netflix ESN Successful!\r")));
	}
	else {
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_esn_check, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Check Netflix ESN Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetESN(std::string esn)
{
	AUTOTIMING;
	if (!m_check_esn.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	if ((bRet = g_siacp.SCBC_GetNetflixESN(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsESN || _tcsicmp(esn.c_str(), data.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_esn_read);
			SetOptionLog(CString(_T("Check Netflix ESN Successful:\r")));
			SetOptionLog(data + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_esn_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get Netflix ESN Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_SetCikey(std::string& cikey)
{
	AUTOTIMING;
	if (!m_check_cikey.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if (Global::g_tConfig.mode == 3)
	{
		std::string file;
		if (ReadKeyFile(Global::g_tConfig.keyfolder.find("CIKeyFolder")->second, cikey/*, file*/))
		{
			if ((bRet = g_siacp.SCBC_SetCiKey((byte*)cikey.data(), cikey.size())) == TRUE)
			{
				//DeleteFile(file.c_str());
				cikey = Bytes2HexString((const byte*)cikey.c_str(), cikey.size());
				SetSiacpStatus(m_lb_cikey_write);
				SetOptionLog(CString(_T("Set cikey Successful!\r")));
				return TRUE;
			}
		}
	}
	else if (g_ota.GetKeyInfo((Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/getcikey.do?",
		"type=" + m_pCurChassis->CIKeyType + "&sn=" + m_str_sn.GetString(), "key", "value", cikey))
	{
		if ((bRet = g_siacp.SCBC_SetCiKey(cikey)) == TRUE)
		{
			SetSiacpStatus(m_lb_cikey_write);
			SetOptionLog(CString(_T("Set cikey Successful!\r")));
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_cikey_write, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Set cikey Failed!\r")), ERROR_LOG);

	return FALSE;
}

BOOL CTCLCopyToolDlg::Siacp_CheckCikey()
{
	AUTOTIMING;
	if (!m_check_cikey.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	if ((bRet = g_siacp.SCBC_CheckCikey()) == TRUE)
	{
		SetSiacpStatus(m_lb_cikey_check);
		SetOptionLog(CString(_T("Check cikey Successful!\r")));
	}
	else {
		// 输出日志到界面中;
		SetSiacpStatus(m_lb_cikey_check, FALSE);
		SetLabelResult(FALSE);
		SetOptionLog(CString(_T("Check cikey Failed!\r")), ERROR_LOG);
	}

	return bRet;
}

BOOL CTCLCopyToolDlg::Siacp_GetCikey(std::string cikey)
{
	AUTOTIMING;
	if (!m_check_cikey.GetCheck())
		return TRUE;

	BOOL bRet = FALSE;
	std::string data;
	if ((bRet = g_siacp.SCBC_GetCiKey(data)) == TRUE)
	{
		if (!Global::g_tConfig.writedone.IsCI || _tcsicmp(cikey.c_str(), data.c_str()) == 0)
		{
			SetSiacpStatus(m_lb_cikey_read);
			SetOptionLog(CString(_T("Get cikey Successful:\r")));
			SetOptionLog(data + _T("\r"), INFO_LOG);
			return TRUE;
		}
	}

	// 输出日志到界面中;
	SetSiacpStatus(m_lb_cikey_read, FALSE);
	SetLabelResult(FALSE);
	SetOptionLog(CString(_T("Get cikey Failed!\r")), ERROR_LOG);

	return FALSE;
}

void CTCLCopyToolDlg::InitLogCharFormat()
{
	//////////////////////////////////////////////////////////////////////////
	// 字体初始化;
	m_cf_normal.cbSize = sizeof(CHARFORMAT);
	m_cf_normal.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_BOLD;
	m_cf_normal.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_BOLD | CFE_AUTOCOLOR);
	//cf.dwEffects ^= CFE_AUTOCOLOR;
	m_cf_normal.crTextColor = BLACK_TEXT_COLOR;
	m_cf_normal.yHeight = 180;
	memset(m_cf_normal.szFaceName, 0, LF_FACESIZE);
	memcpy(m_cf_normal.szFaceName, _T("宋体"), sizeof(_T("宋体")));

	// 错误;
	m_cf_error.cbSize = sizeof(CHARFORMAT);
	m_cf_error.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_BOLD;
	m_cf_error.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_AUTOCOLOR);
	//cf.dwEffects ^= CFE_AUTOCOLOR;
	m_cf_error.crTextColor = WARN_TEXT_COLOR;
	m_cf_error.yHeight = 220;
	memset(m_cf_error.szFaceName, 0, LF_FACESIZE);
	memcpy(m_cf_error.szFaceName, _T("宋体"), sizeof(_T("宋体")));

	// 成功;
	m_cf_ok.cbSize = sizeof(CHARFORMAT);
	m_cf_ok.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_BOLD;
	m_cf_ok.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_AUTOCOLOR);
	//cf.dwEffects ^= CFE_AUTOCOLOR;
	m_cf_ok.crTextColor = OK_TEXT_COLOR;
	m_cf_ok.yHeight = 220;
	memset(m_cf_ok.szFaceName, 0, LF_FACESIZE);
	memcpy(m_cf_ok.szFaceName, _T("宋体"), sizeof(_T("宋体")));

	// 信息;
	m_cf_info.cbSize = sizeof(CHARFORMAT);
	m_cf_info.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_BOLD;
	m_cf_info.dwEffects = (unsigned long)~(CFE_UNDERLINE | CFE_BOLD | CFE_AUTOCOLOR);
	//cf.dwEffects ^= CFE_AUTOCOLOR;
	m_cf_info.crTextColor = NORMAL_TEXT_COLOR;
	m_cf_info.yHeight = 200;
	memset(m_cf_info.szFaceName, 0, LF_FACESIZE);
	memcpy(m_cf_info.szFaceName, _T("宋体"), sizeof(_T("宋体")));
	//////////////////////////////////////////////////////////////////////////
}

DWORD __stdcall CTCLCopyToolDlg::ThreadGetMessage(LPVOID lpParam)
{
	MSG msg;
	CTCLCopyToolDlg* pDlg = (CTCLCopyToolDlg*)lpParam;
	while (::GetMessage(&msg, NULL, 0, 0))
	{
		switch (msg.message)
		{
		case WM_GET_BARCODE:
		{
			Sleep(300); // 等待条码输入完成;
			CString strSN = _T("");
			pDlg->GetDlgItemTextA(EDIT_SN, strSN);
			if (strSN.GetLength() >= 12)
			{
				pDlg->GetDlgItemText(EDIT_SN, pDlg->m_str_sn);
				//pDlg->OnBnClickedStart();//线程中不能使用UpdateData();
				pDlg->PostMessage(WM_COMMAND, MAKEWPARAM(BTN_START, BN_CLICKED), NULL);
			}
			else
			{
				pDlg->SetOptionLog(CString("Sn length less than 12:") + strSN + CString(_T("\r")), INFO_LOG);
			}
			// 完成后清空编辑框;
			pDlg->SetDlgItemText(EDIT_SN, _T(""));
			pDlg->m_bGetBarCode = FALSE;
		}
		break;
		case WM_GET_ORDERNUM:
		{
			//Sleep(2500); // 等待订单号输入完成;
			pDlg->GetDlgItem(BTN_START)->EnableWindow(FALSE);
			pDlg->GetDlgItem(IDOK)->EnableWindow(FALSE);
			pDlg->UpdateAutoOnlineMode();
			pDlg->m_bGetOrderNum = FALSE;
			pDlg->GetDlgItem(BTN_START)->EnableWindow(TRUE);
			pDlg->GetDlgItem(IDOK)->EnableWindow(TRUE);
		}
		break;
		default:
			break;
		}
	}
	return 0;
}

LRESULT __stdcall CTCLCopyToolDlg::CBTHookProc(long nCode, WPARAM wparam, LPARAM lparam)
{
	if (nCode == HCBT_ACTIVATE)
	{
		::SetDlgItemText((HWND)wparam, IDYES, "&Continue");
		::SetDlgItemText((HWND)wparam, IDNO, "&Cancel");
		::SetDlgItemText((HWND)wparam, IDOK, "&OK");
		::SetDlgItemText((HWND)wparam, IDCANCEL, "&Cancel");
		UnhookWindowsHookEx(m_hHook);
	}

	return LRESULT(0);
}

//实现串口热插拔  
BOOL CTCLCopyToolDlg::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
{
	switch (nEventType)
	{
		// 串口拨掉;
	case DBT_DEVICEREMOVECOMPLETE:
		break;
		// 串口插入;
	case DBT_DEVICEARRIVAL:
		Global::GetSysSerialPort(m_vtCOM);
		InitCombobox_port();
		break;
	default:
		break;
	}
	return TRUE;
}

void CTCLCopyToolDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (nIDEvent == 0)
	{
		if (Global::g_tConfig.mode < 3)
		{
			if (m_bAutofocus)
			{
				if (GetDlgItem(EDIT_SN) != GetFocus())
					GetDlgItem(EDIT_SN)->SetFocus();
			}

			CWnd* pWnd = GetFocus();
			if (pWnd == GetDlgItem(EDIT_SN))
			{
				// 关闭输入法;
				HWND hWnd = ::GetForegroundWindow();
				HIMC hImc = ImmGetContext(hWnd);
				if (ImmGetOpenStatus(hImc))
					ImmSetOpenStatus(hImc, FALSE);
			}
		}
	}
#ifdef TEST
	else if (nIDEvent == 1)
	{
		if (m_str_sn.IsEmpty() && g_midInfo.host.size() && GetDlgItem(EDIT_SN)->IsWindowEnabled())
			SetDlgItemText(EDIT_SN, _T("6aca86586a84851744c"));
	}
#endif

	CDialogEx::OnTimer(nIDEvent);
}


void CTCLCopyToolDlg::OnBnClickedHide()  // 隐藏界面;
{
	// TODO: 在此添加控件通知处理程序代码
	CRect rcHide;
	static BOOL bHide = FALSE;
	if (!bHide)
	{// 隐藏;
		GetWindowRect(m_rcWind);
		GetDlgItem(IDC_GROUP_HIDE)->GetWindowRect(rcHide);
		m_rcWind.right = rcHide.left + 8;
		MoveWindow(m_rcWind);
		SetDlgItemText(BTN_HIDE, _T(">"));

		if (Global::g_bTestHost)
		{
			m_lb_test_mode.GetWindowRect(m_rcWind);
			m_rcWind.right = rcHide.left;
			ScreenToClient(m_rcWind);
			m_lb_test_mode.MoveWindow(m_rcWind);
			m_lb_test_mode.SetText("Test Mode");
		}
	}
	else
	{// 伸展;
		GetWindowRect(m_rcWind);
		GetDlgItem(IDC_GROUP_HIDE)->GetWindowRect(rcHide);
		m_rcWind.right += rcHide.Width() + 15;
		MoveWindow(m_rcWind);
		SetDlgItemText(BTN_HIDE, _T("<"));

		if (Global::g_bTestHost)
		{
			m_lb_test_mode.GetWindowRect(m_rcWind);
			m_rcWind.right += rcHide.Width() + 15;
			ScreenToClient(m_rcWind);
			m_lb_test_mode.MoveWindow(m_rcWind);
			m_lb_test_mode.SetText("Test Mode");
		}
		}

	bHide = !bHide;
	}

void CTCLCopyToolDlg::OnCbnSelchangeMode() // 模式;
{
	// TODO: 在此添加控件通知处理程序代码
	SaveChange2Config();
	SetLabelResult(-1);
	RetSetSiacpStatus();
	SetDlgItemText(RICHEDIT2_LOG, _T(""));

#if ENABLE_VCODE
	if (m_cb_mode.GetCurSel() != 0)
	{

	}
#else
	Global::g_tConfig.mode = m_cb_mode.GetCurSel();
#endif
	m_cb_chassis.EnableWindow(Global::g_tConfig.mode > 1);
	GetDlgItem(CHECK_LOCK)->EnableWindow(Global::g_tConfig.mode < 2);
	GetDlgItem(CHECK_FOCUS)->EnableWindow(Global::g_tConfig.mode < 3);
	EnableChassisCheckbox();

	if (Global::g_tConfig.mode < 2) // 自动+手动;
	{
		m_cb_chassis.SetCurSel(-1);
		GetDlgItem(EDIT_ORDER)->EnableWindow(!m_check_lock.GetCheck());
		GetDlgItem(EDIT_SN)->EnableWindow(TRUE);
	}
	else // 离线+本地;
	{
		int nCurSel = 0;
		CString strChassis = _T("");
		if (CB_ERR != (nCurSel = m_cb_chassis.GetCurSel()))
		{
			m_cb_chassis.GetLBText(nCurSel, strChassis);
			Global::g_tConfig.chassis = strChassis.GetString();
		}
		else
		{
			m_cb_chassis.SelectString(0, Global::g_tConfig.chassis.c_str());
		}

		if (m_check_lock.GetCheck())
		{
			m_check_lock.SetCheck(FALSE);
			PostMessage(WM_COMMAND, MAKEWPARAM(CHECK_LOCK, BN_CLICKED), NULL);
		}

		if (Global::g_tConfig.mode == 2) // 离线;
		{
			GetDlgItem(EDIT_ORDER)->EnableWindow(FALSE);
			GetDlgItem(EDIT_SN)->EnableWindow(TRUE);
		}
		else  // 本地;
		{
			GetDlgItem(EDIT_ORDER)->EnableWindow(FALSE);
			GetDlgItem(EDIT_SN)->EnableWindow(FALSE);
		}
	}

	if (Global::g_tConfig.mode > 1) // 离线+本地，清空;
	{
		//m_str_sn.Empty();
		SetDlgItemText(EDIT_SN, _T(""));
		m_str_bid.Empty();
		UpdateData(FALSE);
	}

	UpdateKeyCount();
	UpdateChassisCheckBoxStatus();
}


void CTCLCopyToolDlg::OnCbnSelchangeChassis() // Chassis
{
	SetDlgItemText(RICHEDIT2_LOG, _T(""));
	// TODO: 在此添加控件通知处理程序代码
	int nCurSel = m_cb_chassis.GetCurSel();
	if (nCurSel != CB_ERR)
	{
		CString strChassis;
		m_cb_chassis.GetLBText(nCurSel, strChassis);
		SaveChange2Config();
		Global::g_tConfig.chassis = strChassis.GetString();

		UpdateKeyCount();
		//UpdateChassisCheckBoxStatus();
		EnableChassisCheckbox();
	}
}


void CTCLCopyToolDlg::OnBnClickedStart()  // 开始;
{
	static ThreadSection _critSection;
	AutoThreadSection aSection(&_critSection);

	m_dwTickCount = GetTickCount64();
	if (m_bRunning)
		return;

	m_bRunning = TRUE;
	UpdateData();
	SaveChange2Config();
	SetLabelResult(-1);
	RetSetSiacpStatus();
	m_vt_key_files.clear();

	if (!m_str_sn.IsEmpty())
	{
		GetDlgItem(EDIT_SN)->EnableWindow(FALSE);
		SetOptionLog(CString(_T("Current SN:")) + m_str_sn + CString(_T("\r")), INFO_LOG);
	}

	// 打开串口;
	if (!g_siacp.IsOpen())
	{
		int nIndex = -1;
		CString strCom = _T("");
		if (CB_ERR != (nIndex = m_cb_com.GetCurSel()))
		{
			m_cb_com.GetLBText(nIndex, strCom);
			if (CB_ERR != (nIndex = m_cb_baudrate.GetCurSel()))
			{
				CString strBaudrate = _T("");
				m_cb_baudrate.GetLBText(nIndex, strBaudrate);
				if (!g_siacp.OpenComm(strCom.GetString(), _ttoi(strBaudrate.GetString())))
				{
					m_bRunning = FALSE;
					m_str_sn.Empty();
					GetDlgItem(EDIT_SN)->EnableWindow();
					MessageBox(_T("Open Serial Port Fail, Please Check!"), _T("Error"), MB_OK | MB_ICONERROR);
					return;
				}
			}
			else
			{
				m_bRunning = FALSE;
				m_str_sn.Empty();
				GetDlgItem(EDIT_SN)->EnableWindow();
				MessageBox(_T("No selection of baud rate!"), _T("Error"), MB_OK | MB_ICONERROR);
				return;
			}
		}
		else
		{
			m_bRunning = FALSE;
			m_str_sn.Empty();
			GetDlgItem(EDIT_SN)->EnableWindow();
			MessageBox(_T("No Serial Port Selection!"), _T("Error"), MB_OK | MB_ICONERROR);
			return;
		}
	}

	if (Global::g_tConfig.mode < 2)
	{
		if (m_str_bid.IsEmpty())
		{
			m_bRunning = FALSE;
			m_str_sn.Empty();
			GetDlgItem(EDIT_SN)->EnableWindow();
			MessageBox(_T("Order number cannot be empty"), _T("error"), MB_OK | MB_TOPMOST | MB_ICONERROR);
			GetDlgItem(EDIT_ORDER)->SetFocus();
			return;
		}

		UpdateAutoOnlineMode();
	}

	// TODO: 在此添加控件通知处理程序代码
	std::thread t([](CTCLCopyToolDlg* p) {
		p->GetDlgItem(BTN_START)->EnableWindow(FALSE);
		p->GetDlgItem(IDOK)->EnableWindow(FALSE);

		// 开机盲抄等待;
		BOOL bTVBoot = FALSE;
		//Sleep(Global::g_tConfig.waitboot);
		int nWatiCount = Global::g_tConfig.waitboot / 1000;
		p->SetOptionLog(CString(_T("Waiting TV Boot...\r")));
		for (int i = 0; i < nWatiCount; i++)
		{
			if (g_siacp.SCBC_WaitTVBoot())
			{
				p->SetOptionLog(CString(_T("TV Boot Ok\r")), OK_LOG);
				bTVBoot = TRUE;
				break;
			}
			Sleep(1000);
		}

		if (!bTVBoot)
		{
			p->SetOptionLog(CString(_T("TV Boot Failed\r")), ERROR_LOG);
			p->SetLabelResult(0);
			goto over;
		}

		if (p->m_check_mtk_init.GetCheck())
		{
			if (!p->Siacp_MTKInit())
			{
				p->SetLabelResult(0);
				goto over;
			}
		}

		if (p->Siacp_EnterFactoryMode())
		{
			bool result = true;
			CString strLog = _T("");
			std::string url, context;// post结果;
			std::string client_type, softe_version, deviceid, mac;
			std::string hdcp, hdcp22, widi, widevine, esn, cikey, channel, language;

			// 读取client type;
			p->Siacp_GetClientType(client_type);
			// 校验client type是否与服务器一致;
			if (Global::g_tConfig.mode < 2 && p->m_check_verify_client_type.GetCheck())
			{
				if (_tcsicmp(client_type.c_str(), g_midInfo.clienttype.c_str()))
				{
					p->m_hHook = ::SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTHookProc, AfxGetInstanceHandle(), GetCurrentThreadId());

					CString strMsg;
					strMsg.Format(_T("Current core client type: %s \r\nis inconsistent with RDM mass-produced client type:%s!\r\nplease check and confirm!"),
						client_type.c_str(), g_midInfo.clienttype.c_str());
					if (p->MessageBox(strMsg, _T("Warnning"), MB_YESNO | MB_TOPMOST | MB_ICONERROR) == IDYES)
					{
						p->m_check_verify_client_type.SetCheck(FALSE);
					}
					else
					{
						result = false;
						goto end;
					}
				}
			}

			// 读取版本号;
			p->Siacp_GetSofteVersion(softe_version);
			if (Global::g_tConfig.mode < 2 && p->m_check_verify_soft_version.GetCheck())
			{
				if (_tcsicmp(softe_version.c_str(), g_midInfo.version.c_str()))
				{
					p->m_hHook = ::SetWindowsHookEx(WH_CBT, (HOOKPROC)CBTHookProc, AfxGetInstanceHandle(), GetCurrentThreadId());
					CString strMsg;
					strMsg.Format(_T("Current core software version: %s\r\nis inconsistent with RDM mass-produced software version:%s!\r\nplease check and confirm!"),
						softe_version.c_str(), g_midInfo.version.c_str());
					if (p->MessageBox(strMsg, _T("Warnning"), MB_YESNO | MB_TOPMOST | MB_ICONERROR) == IDYES)
					{
						p->m_check_verify_soft_version.SetCheck(FALSE);
					}
					else
					{
						result = false;
						goto end;
					}
				}
			}

			if (p->m_check_did.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsDID ? p->Siacp_SetDeviceId(deviceid) : true))
				{
					if ((Global::g_tConfig.writecheck.IsDID ? p->Siacp_CheckDeviceId() : true) &&
						(Global::g_tConfig.readcheck.IsDID ? p->Siacp_GetDeviceId(deviceid) : true))
					{
						context.append("deviceid=" + deviceid + "&");
					}
					else
					{
						result = false;
						goto end;
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_mac.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsMAC ? p->Siacp_SetMac(mac) : true))
				{
					if ((Global::g_tConfig.writecheck.IsMAC ? p->Siacp_CheckMac() : true) &&
						(Global::g_tConfig.readcheck.IsMAC ? p->Siacp_GetMac(mac) : true))
					{
						context.append("mac=" + mac + "&");
					}
					else
					{
						result = false;
						goto end;
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_hdcp.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsHDCP ? p->Siacp_SetHDCP(hdcp) : true))
				{
					if ((Global::g_tConfig.writecheck.IsHDCP ? p->Siacp_CheckHDCP() : true) &&
						(Global::g_tConfig.readcheck.IsHDCP ? p->Siacp_GetHDCP(hdcp) : true))
					{
						context.append("hdcpkey=" + hdcp + "&");
					}
					else
					{
						result = false;
						goto end;
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_hdcp22.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsHDCP22 ? p->Siacp_SetHDCP22(hdcp22) : true))
				{
					if ((Global::g_tConfig.writecheck.IsHDCP22 ? p->Siacp_CheckHDCP22() : true) &&
						(Global::g_tConfig.readcheck.IsHDCP22 ? p->Siacp_GetHDCP22(hdcp22) : true))
					{
						context.append("hdcpkey2=" + hdcp22 + "&");
					}
					else
					{
						result = false;
						goto end;
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_widi.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsWIDI ? p->Siacp_SetWidi(widi) : true))
				{
					if ((Global::g_tConfig.writecheck.IsWIDI ? p->Siacp_CheckWidi() : true) &&
						(Global::g_tConfig.readcheck.IsWIDI ? p->Siacp_GetWidi(widi) : true))
					{
						context.append("widi=" + widi + "&");
					}
					else
					{
						result = false;
						goto end;
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_widevine.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsWIDEVINE ? p->Siacp_SetWidevine(widevine) : true))
				{
					if ((Global::g_tConfig.writecheck.IsWIDEVINE ? p->Siacp_CheckWidevine() : true) &&
						(Global::g_tConfig.readcheck.IsWIDEVINE ? p->Siacp_GetWidevine(widevine) : true))
					{
						context.append("widevine=" + widevine + "&");
					}
					else
					{
						result = false;
						goto end;
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_esn.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsESN ? p->Siacp_SetESN(esn) : true))
				{
					if ((Global::g_tConfig.writecheck.IsESN ? p->Siacp_CheckESN() : true) &&
						(Global::g_tConfig.readcheck.IsESN ? p->Siacp_GetESN(esn) : true))
					{
						context.append("netfilxesn=" + esn + "&");
					}
					else
					{
						result = false;
						goto end;
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_cikey.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsCI ? p->Siacp_SetCikey(cikey) : true))
				{
					if ((Global::g_tConfig.writecheck.IsCI ? p->Siacp_CheckCikey() : true) &&
						(Global::g_tConfig.readcheck.IsCI ? p->Siacp_GetCikey(cikey) : true))
					{
						context.append("cikey=" + cikey + "&");
					}
					else
					{
						result = false;
						goto end;
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_wb_init.GetCheck())
			{
				if (!g_siacp.SCBC_WBInit())
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_wb_write.GetCheck())
			{
				if (p->Siacp_SetWB())
				{
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_channel.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsCHANNEL ? p->Siacp_SetChannel(channel) : true) &&
					(Global::g_tConfig.readcheck.IsCHANNEL ? p->Siacp_GetChannel(channel) : true))
				{
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_osd_lang.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsOSD ? p->Siacp_SetOSDLanguage(language) : true) &&
					(Global::g_tConfig.readcheck.IsOSD ? p->Siacp_GetOSDLanguage(language) : true))
				{
				}
				else
				{
					result = false;
					goto end;
				}
			}

			if (p->m_check_shop_lang.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsSHOP ? p->Siacp_SetShopLanguage(language) : true) &&
					(Global::g_tConfig.readcheck.IsSHOP ? p->Siacp_GetShopLanguage(language) : true))
				{
				}
				else
				{
					result = false;
					goto end;
				}
			}

			// 注意：切换pid一般放在最后处理;
			if (p->m_check_pid.GetCheck())
			{
				if ((Global::g_tConfig.writedone.IsPID ? p->Siacp_SetProjectId() : true) && (Global::g_tConfig.readcheck.IsPID ? p->Siacp_GetProjectId() : true))
				{
					// 写入的pid再次读取出来校验;
					if (Global::g_tConfig.mode < 3 /*&& p->m_check_verify_pid.GetCheck()*/)
					{
						context.append("projectid=" + std::string(p->m_str_pid.GetString()) + "&");
					}
				}
				else
				{
					result = false;
					goto end;
				}
			}

			DWORD dwCount = GetTickCount64() - p->m_dwTickCount;
			strLog.Format(_T("\rAll key Copy Sucessfull,It took %d milliseconds!\r\r"), dwCount);
			LOG4C((LOG_INFO, _T("%s"), strLog));
			p->SetOptionLog(strLog, INFO_LOG);
			p->DeleteKeyFiles();

			// 成功后,上报结果;
			if (Global::g_tConfig.mode < 3 && p->m_check_did.GetCheck())
			{
				if (softe_version.size())
					context.append("sversionid=" + softe_version + "&");
				else
					context.append("sversionid=" + g_midInfo.version + "&");

				if (client_type.size())
					context.append("clienttype=" + client_type + "&");
				else
					context.append("clienttype=" + g_midInfo.clienttype + "&");

				if (Global::g_tConfig.mode < 2)
				{
					context.append("ordernum=" + std::string(p->m_str_bid.GetString()) + "&");
				}
				if (!p->m_str_sn.IsEmpty())
				{
					context.append("sn=" + std::string(p->m_str_sn.GetString()) + "&");
				}

				int nRet = 0;
				std::string xml;
				url = (Global::g_tConfig.mode < 2 ? g_midInfo.host : Global::g_tConfig.serverurl) + "/report.do?";
				if (!g_ota.GetKeyInfo(url, context, "", "", xml))
				{
					// 上报失败;
					p->SetOptionLog(CString(_T("Upload result Failed!\r")), ERROR_LOG);
					for (int i = 0; i < 5 ; i++)
					{
						nRet = g_db.InsertReportInfo(url, context);
						if (nRet == 0 || nRet == SQLITE_CONSTRAINT)
						{
							break;
						}
						else
						{
							// 重新打开数据库;
							g_db.Open();
						}
					}
				}
				else
				{
					p->SetOptionLog(CString(_T("Upload result Successful!\r")), OK_LOG);
					for (int i = 0; i < 5; i++)
					{
						nRet = g_db.InsertReportInfo(url, context, true);
						if (nRet == 0 || nRet == SQLITE_CONSTRAINT)
						{
							break;
						}
						else
						{
							// 重新打开数据库;
							g_db.Open();
						}
					}
				}
			}
		end:
			p->m_str_sn.Empty();
			// 更新余量;
			p->UpdateKeyCount(FALSE);
			p->SetLabelResult(result, TRUE);
		}

	over:
		p->m_str_sn.Empty();
		p->GetDlgItem(EDIT_SN)->EnableWindow();
		p->GetDlgItem(BTN_START)->EnableWindow(TRUE);
		p->GetDlgItem(IDOK)->EnableWindow(TRUE);
		p->m_bRunning = FALSE;
	}, this);
	t.detach();
}


HBRUSH CTCLCopyToolDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
	// TODO:  在此更改 DC 的任何特性
// 	if (nCtlColor == CTLCOLOR_STATIC)
// 	{
// 		//pDC->SetBkColor(RGB(239, 228, 176)); //字体背景色 
// 		pDC->SetBkMode(TRANSPARENT);
// 		return (HBRUSH)::GetStockObject(NULL_BRUSH);
// 	}
	// TODO:  如果默认的不是所需画笔，则返回另一个画笔
	return hbr;
}


void CTCLCopyToolDlg::OnEnChangeOrder() // 订单;
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
#if 0 // 由lock锁定订单号时，触发获取key信息;
	if (!m_bGetOrderNum)
	{
		m_bGetOrderNum = TRUE;
		SetLabelResult(-1);
		RetSetSiacpStatus();
		InitKeyCountLabel();
		PostThreadMessage(m_dwThreadId, WM_GET_ORDERNUM, 0, 0);
	}
#endif
	// TODO:  在此添加控件通知处理程序代码
}


void CTCLCopyToolDlg::OnEnChangeSn() // 条码;
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
	if (!m_bGetBarCode && m_str_sn.IsEmpty())
	{
		m_bGetBarCode = TRUE;
		PostThreadMessage(m_dwThreadId, WM_GET_BARCODE, 0, 0);
	}
}


void CTCLCopyToolDlg::OnBnClickedWbFile()
{
	// TODO: 在此添加控件通知处理程序代码
	CString filePath, fileName;

	CFileDialog fileDialog(
		TRUE,									// 创建打开文件对话框, FALSE是保存文件对话框
		".wba",									// 默认打开文件类型
		NULL,									// 默认打开文件名
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, // 打开只读文件
		"WB config file(*.wba)|*.wba");			// 所有可以打开的文件类型

	if (fileDialog.DoModal() == IDOK)
	{
		filePath = fileDialog.GetPathName(); // 取出文件路径
		CString strWBFile = fileDialog.GetFileName();
		fileName.Format(_T(".//DataDir//%s"), strWBFile);

		if (CopyFile(filePath, fileName, FALSE))
		{
			Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis)->second.WBFile = strWBFile.GetString();

			m_chWBNormalBuffer[R] = GetPrivateProfileInt(_T("WBA value:"), _T("HDMI white R:"), NULL, fileName);
			m_chWBNormalBuffer[G] = GetPrivateProfileInt(_T("WBA value:"), _T("HDMI white G:"), NULL, fileName);
			m_chWBNormalBuffer[B] = GetPrivateProfileInt(_T("WBA value:"), _T("HDMI white B:"), NULL, fileName);
			m_chWBWarmBuffer[R] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Warm white R:"), NULL, fileName);
			m_chWBWarmBuffer[G] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Warm white G:"), NULL, fileName);
			m_chWBWarmBuffer[B] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Warm white B:"), NULL, fileName);
			m_chWBCoolBuffer[R] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Cool white R:"), NULL, fileName);
			m_chWBCoolBuffer[G] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Cool white G:"), NULL, fileName);
			m_chWBCoolBuffer[B] = WB_OFFSET + GetPrivateProfileInt(_T("WBA value:"), _T("Cool white B:"), NULL, fileName);

			GetDlgItem(STATIC_WB_FILE)->SetWindowText(strWBFile);
		}
	}
}


void CTCLCopyToolDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类
	SaveChange2Config();
	CDialogEx::OnCancel();
}


void CTCLCopyToolDlg::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类
	if (GetDlgItem(BTN_START)->IsWindowEnabled() && !m_bGetBarCode) // m_bGetBarCode不使用条码回车功能;
		PostMessage(WM_COMMAND, MAKEWPARAM(BTN_START, BN_CLICKED), NULL);
}


void CTCLCopyToolDlg::OnBnClickedLock()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL bCheck = ((CButton*)GetDlgItem(CHECK_LOCK))->GetCheck();
	GetDlgItem(EDIT_ORDER)->EnableWindow(!bCheck && Global::g_tConfig.mode < 2);
	m_check_pid.EnableWindow(!bCheck && Global::g_tConfig.mode != 0);
	GetDlgItem(EDIT_PID)->EnableWindow(!bCheck && Global::g_tConfig.mode != 0);
	m_check_channel.EnableWindow(!bCheck);
	m_check_osd_lang.EnableWindow(!bCheck);
	m_check_shop_lang.EnableWindow(!bCheck);

	m_cb_osd_lang.EnableWindow(!bCheck);
	m_cb_shop_lang.EnableWindow(!bCheck);
	m_cb_channel.EnableWindow(!bCheck);

	// auto+manual模式，check;
	if ( Global::g_tConfig.mode < 2 && bCheck )
	{
		if (!m_bGetOrderNum)
		{
			m_bGetOrderNum = TRUE;
			SetLabelResult(-1);
			RetSetSiacpStatus();
			InitKeyCountLabel();
			PostThreadMessage(m_dwThreadId, WM_GET_ORDERNUM, 0, 0);
		}
	}
}


void CTCLCopyToolDlg::OnBnClickedFocus()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bAutofocus = ((CButton*)GetDlgItem(CHECK_FOCUS))->GetCheck();
}


void CTCLCopyToolDlg::OnGetKeyCountClicked(UINT id)
{
	switch (id)
	{
	case CHECK_DID:
		SetLabelKeyCount(_T("DeviceID"), Global::g_tConfig.mode);
		break;
	case CHECK_MAC:
		SetLabelKeyCount(_T("MAC"), Global::g_tConfig.mode);
		break;
	case CHECK_HDCP:
		SetLabelKeyCount(_T("HDCP_KEY"), Global::g_tConfig.mode);
		break;
	case CHECK_HDCP22:
		SetLabelKeyCount(_T("HDCP2.2_KEY"), Global::g_tConfig.mode);
		break;
	case CHECK_WIDI:
		SetLabelKeyCount(_T("WiDi"), Global::g_tConfig.mode);
		break;
	case CHECK_WIDEVINE:
		SetLabelKeyCount(_T("Widevine_KEY"), Global::g_tConfig.mode);
		break;
	case CHECK_ESN:
		SetLabelKeyCount(_T("NETFILX_ESN"), Global::g_tConfig.mode);
		break;
	case CHECK_CIKEY:
		SetLabelKeyCount(_T("CI_PLUS_KEY"), Global::g_tConfig.mode);
		break;
	default:
		break;
	}
}


void CTCLCopyToolDlg::OnBnClickedConfig() // 配置Chassis;
{
	UpdateData();
	// 先保存;
	SaveChange2Config();
	// 清空状态;
	SetLabelResult(-1);
	RetSetSiacpStatus();
	CChassisConfigDlg dlg;
	if (dlg.DoModal() == IDOK)
	{
		InitCombobox_Chassis();
		if (Global::g_tConfig.mode > 1)
		{
			UpdateKeyCount();
			//UpdateChassisCheckBoxStatus();
		}
	}
}


BOOL CTCLCopyToolDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	static BOOL bTopWnd = FALSE;
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
			// F2开启或关闭测试服务器地址;
		case VK_F2:
		{
			Global::g_bTestHost = !Global::g_bTestHost;
			m_lb_test_mode.ShowWindow(Global::g_bTestHost);

			if (Global::g_bTestHost)
			{
				if (g_midInfo.host.size() && _tcsicmp(g_midInfo.host.c_str(), "http://test.admin.ota.qhmoka.com/IDManage") != 0)
				{
					g_true_host = g_midInfo.host;
					g_midInfo.host = "http://test.admin.ota.qhmoka.com/IDManage";
				}
			}
			else
			{
				if (_tcsicmp(g_midInfo.host.c_str(), "http://test.admin.ota.qhmoka.com/IDManage") == 0 && g_true_host.size())
					g_midInfo.host = g_true_host;
			}
		}
		break;
		// 前置窗口;
		case VK_F3:
		{
			CString strText;
			GetWindowText(strText);

			if (bTopWnd == FALSE)
			{
				SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);//窗口置顶
				strText.Format(_T("%s  Front window (Press F3 to Cancel Or Open)"), strText.GetString());
			}
			else
			{
				SetWindowPos(&wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);//取消窗口置顶	
				strText.Replace(_T("Front window (Press F3 to Cancel Or Open)"), _T(""));
			}
			bTopWnd = !bTopWnd;
			SetWindowText(strText);
		}
		break;
		case VK_F5:
		{// 先保存;
			PostMessage(WM_COMMAND, MAKEWPARAM(BTN_CONFIG, BN_CLICKED), NULL);
		}
		break;
		default:
			break;
		}
	}
	return CDialogEx::PreTranslateMessage(pMsg);
}


void CTCLCopyToolDlg::OnCbnSelchangeCom()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strData;
	int nCurSel = m_cb_com.GetCurSel();
	if (nCurSel != CB_ERR)
	{
		m_cb_com.GetLBText(nCurSel, strData);
		Global::g_tConfig.com = strData.GetString();

		nCurSel = m_cb_baudrate.GetCurSel();
		if (nCurSel != CB_ERR)
		{
			m_cb_baudrate.GetLBText(nCurSel, strData);
			Global::g_tConfig.baudrate = strData.GetString();
		}
	}

	// 重新打开串口;
	g_siacp.OpenComm(Global::g_tConfig.com.c_str(), _ttol(Global::g_tConfig.baudrate.c_str()));
}


void CTCLCopyToolDlg::OnCbnSelchangeBd()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strData;
	int nCurSel = m_cb_com.GetCurSel();
	if (nCurSel != CB_ERR)
	{
		m_cb_com.GetLBText(nCurSel, strData);
		Global::g_tConfig.com = strData.GetString();

		nCurSel = m_cb_baudrate.GetCurSel();
		if (nCurSel != CB_ERR)
		{
			m_cb_baudrate.GetLBText(nCurSel, strData);
			Global::g_tConfig.baudrate = strData.GetString();
		}
	}

	// 重新打开串口;
	g_siacp.OpenComm(Global::g_tConfig.com.c_str(), _ttol(Global::g_tConfig.baudrate.c_str()));
}
