#include "stdafx.h"
#include "OTA.h"

ThreadSection g_csTask;
static _SIACP_ g_Siacp[CMD_SIZE] = {
	{"EnterFactory", "AA", "10", "01", false, 100, 100},
	{"LeaveFactory", "AA", "10", "00", false, 100, 100},
	{"WBInit", "AA", "16", "02", false, 100, 100},//白平衡数据初始化（适用于所有信源）
	{"GetProjectID", "AA", "84", "00", false, 100, 100},
	{"GetSoftVersion", "AA", "57", "00", false, 100, 100},
	{"GetDeviceId", "AA", "BE", "01", true, 100, 100},
	{"GetClientType", "AA", "8C", "00", false, 100, 100},
	{"GetMAC", "AA", "BE", "00", true, 100, 100},
	{"GetHDCPKey", "AA", "EE", "00", false, 100, 100},
	{"GetHDCPKey22", "AA", "E7", "00", false, 100, 100},
	{"GetWidi", "AA", "E7", "00", false, 100, 100},
	{"GetNetflixESN", "AA", "BE", "06", true, 100, 100},
	{"GetWidevine", "AA", "EC", "00", true, 500,500},
	{"GetCiKey", "AA", "EC", "01", true, 500,500},
	{"GetOSDLanguage", "AA", "97", "00", true, 100, 100},
	{"GetShopLanguage", "AA", "97", "01", true, 100, 100},
	{"GetChannel", "AA", "97", "13", true, 100, 100},
	// 设置;
	{"SetProjectId", "AA", "70", "", false, 100, 100},
	{"SetDeviceID", "AA", "B2", "", false, 100, 100},
	{"SetMAC", "AA", "B3", "", false, 100, 100},
	{"SetHDCPKey", "AA FE", "86", "", false, 100, 100},
	{"SetHDCPKey22", "AA FE", "E4", "", false, 100, 100},
	{"SetNetflixESN", "AA", "99 00", "", false, 100, 100},
	{"SetWidi", "AA FE", "E4", "", false, 100, 100}, //与hdcp22相同指令
	{"SetWidevine", "AA FE", "E9 00", "", false, 500,500},
	{"SetCiKey", "AA FE", "E9 01", "", false, 500,500},
	{"SetOSDLanguage", "AA", "96 00", "", true, 100, 100},
	{"SetShopLanguage", "AA", "96 01", "", true, 100, 100},
	// 设置;
	{"SetChannel", "AA", "15", "", false, 100, 100},
	{"SetWBNormal", "AA", "4D 04 01 01", "", false, 100, 100},
	{"SetWBCool", "AA", "4D 04 02 01", "", false, 100, 100},
	{"SetWBWarm", "AA", "4D 04 03 01", "", false, 100, 100},
	// 检测;
	{"CheckMAC", "AA", "B4", "00", false, 100, 100},
	{"CheckDeviceId", "AA", "B4", "00", false, 100, 100},
	{"CheckHDCP", "AA", "87", "00", false, 100, 100},
	{"CheckNetflixESN", "AA", "9A", "00", true, 100, 100},
	{"CheckWidi", "AA", "E5", "00", false, 100, 100},
	{"CheckWidevine", "AA", "EA", "00", true, 100, 100},

	{"CheckCikey", "AA", "EA", "01", true, 100, 100},
	{"CheckHDCP22", "AA", "E5", "00", false, 100, 100},
	{"StarWarmUpMode", "AA", "13", "01", false, 100, 100},
	{"StopWarmUpMode", "AA", "13", "00", false, 100, 100},
	// 工厂菜单显示;
	{"ShowFactoryMenu", "AA", "11", "01", false, 100, 100},
	// 工厂菜单隐藏;
	{"HideFactoryMenu", "AA", "11", "00", false, 100, 100},
	// 工厂信息显示内容开;
	{"ShowFactoryInformation", "AA", "11", "02", false, 100, 100},
	// 工厂信息显示内容关;
	{"HideFactoryInformation", "AA", "11", "03", false, 100, 100},
	// 老化模式开;
	{"EnterAgingModel", "AA", "13", "01", false, 100, 100},
	// 老化模式关;
	{"LeaveAgingModel", "AA", "13", "00", false, 100, 100},
	// 读取老化时间;
	{"ReadAgingTime", "AA", "7A", "00", true, 100, 100},
	// USB 功能检测查询;
	//{"StopWarmUpMode", "AA", "17", "00", false, 100, 100},
	// 红色增益;
	{"SetRedGainRegister", "AA", "40", "", false, 100, 100},
	// 绿色增益;
	{"SetGreenGainRegister", "AA", "41", "", false, 100, 100},
	// 蓝色增益;
	{"SetBlueGainRegister", "AA", "42", "", false, 100, 100},
	// 红色偏移;
	{"SetRedOffsetRegister", "AA", "43", "", false, 100, 100},
	// 绿色偏移;
	{"SetGreenOffsetRegister", "AA", "44", "", false, 100, 100},
	// 蓝色偏移;
	{"SetBlueOffsetRegister", "AA", "45", "", false, 100, 100},
};

std::vector<_SIACP_> g_vtSiacp;

const unsigned short CRC16_TABLE[16] = {
	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
	0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF
};

unsigned short CRC16Calculate(byte *pBuffer, unsigned int wordLength)
{
	unsigned short wordCRC;
	byte byteTemp;

	wordCRC = 0xFFFF;
	while (wordLength--)
	{
		byteTemp = (byte)(wordCRC >> 0x0C);
		wordCRC <<= 4;
		wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) >> 0x04)];
		byteTemp = (byte)(wordCRC >> 0x0C);
		wordCRC <<= 4;
		wordCRC ^= CRC16_TABLE[byteTemp ^ ((*pBuffer) & 0x0F)];
		pBuffer++;
	}

	return wordCRC;
}

unsigned char TwoHexChar2Char(char ch1, char ch2)
{
	char Numb1;
	char Numb2;

	if (ch1 >= 'A')
		Numb1 = (toupper(ch1) - '0' - 7) * 16;
	else
		Numb1 = (ch1 - '0') * 16;

	if (ch2 >= 'A')
		Numb2 = (toupper(ch2) - '0' - 7);
	else
		Numb2 = (ch2 - '0');

	return (Numb1 + Numb2);
}

std::string HexString2Bytes(std::string strHex, const int &len /* = 3 */)
{
	byte value = 0;
	std::string strBytes;
	int nSize = strHex.size();
	for (int i = 0; i < nSize; i += len)
	{
		strBytes.push_back(TwoHexChar2Char(strHex[i], strHex[i + 1]));
	}

	return strBytes;
}

std::string Bytes2HexString(const unsigned char *pbuffer, int nLen, char chSpace)
{
	std::string hex;
	char szhex[5] = {0};
	for (int i = 0; i < nLen; i++)
	{
		memset(szhex, 0, 5);
		_stprintf_s(szhex, "%02X%c", pbuffer[i], chSpace);
		hex.append(szhex);
	}

	return hex.substr(0, hex.size() - 1);
}

std::string Bytes2HexString(const unsigned char *pbuffer, int nLen)
{
	std::string hex;
	char szhex[5] = {0};
	for (int i = 0; i < nLen; i++)
	{
		memset(szhex, 0, 5);
		_stprintf_s(szhex, "%02X", pbuffer[i]);
		hex.append(szhex);
	}

	return hex;
}

BOOL IsValidString(LPCTSTR lpszString)
{
	if (lpszString == NULL)
		return FALSE;

	do
	{
		// ASCII可显示的字符;
		if (*lpszString < 32 || *lpszString > 126)
		{
			return FALSE;
		}
	} while (*++lpszString);

	return TRUE;
}

// 去除空格;
std::string &trim(std::string &str)
{
	int nIndex = 0;
	while ((nIndex = str.find_first_of(' ')) != std::string::npos)
		str.erase(nIndex, 1);

	return str;
}

CSIACP::CSIACP(void) //:m_vtcommand(g_siacp, g_siacp+sizeof(g_siacp)/sizeof(_SIACP_))
{
	m_pMainDlg = NULL;
	m_pSerial = NULL;
	LoadCommand();
}

CSIACP::~CSIACP(void)
{
	CloseComm();
}

void CSIACP::SetMainDlg(void* p)
{
	m_pMainDlg = p;
}

void CSIACP::ShowMessage(LPCTSTR lpMsg, LOG_ENUM logtype)
{
	if (m_pMainDlg && lpMsg)
	{
		//CTCLCopyToolDlg* pMainDlg = (CTCLCopyToolDlg*)m_pMainDlg;
		//pMainDlg->SetOptionLog(std::string(lpMsg), logtype);
	}
}

void CSIACP::LoadCommand()
{
	// 解析xml;
	tinyxml2::XMLDocument doc;
	if (tinyxml2::XML_SUCCESS != doc.LoadFile(_T("Siacp.xml")))
	{
		SaveCommand(g_Siacp, _T("Siacp.xml"));

		g_vtSiacp.clear();
		for ( int i = 0; i < CMD_SIZE; i++ )
		{
			g_vtSiacp.push_back(g_Siacp[i]);
		}
		return;
	}

	std::string ver;
	std::string desc;
	g_vtSiacp.clear();
	tinyxml2::XMLElement *pXmlRoot = NULL;
	if ((pXmlRoot = doc.RootElement()) != NULL)
	{
		if (_tcsicmp(pXmlRoot->Value(), "CommandList") == 0)
		{
			// 属性;
			const tinyxml2::XMLAttribute *pAttr = pXmlRoot->FirstAttribute();
			while (pAttr)
			{
				if (_tcsicmp(pAttr->Name(), "ver") == 0)
				{
					ver = pAttr->Value();
				}

				if (_tcsicmp(pAttr->Name(), "desc") == 0)
				{
					desc = pAttr->Value();
				}

				pAttr = pAttr->Next();
			}

			// 子项;
			tinyxml2::XMLElement *pXmlElent = pXmlRoot->FirstChildElement();
			while (pXmlElent)
			{
				_SIACP_ siacp;
				if (_tcsicmp(pXmlElent->Value(), _T("Item")) == 0)
				{
					tinyxml2::XMLElement *pItem = pXmlElent->FirstChildElement();
					while (pItem)
					{
						if (_tcsicmp(pItem->Value(), _T("Name")) == 0)
						{
							siacp.name = pItem->GetText();
						}
						else if (_tcsicmp(pItem->Value(), _T("HeadCode")) == 0)
						{
							siacp.head = pItem->GetText();
						}
						else if (_tcsicmp(pItem->Value(), _T("ucCommand")) == 0)
						{
							siacp.code = pItem->GetText();
						}
						else if (_tcsicmp(pItem->Value(), _T("Mark")) == 0)
						{
							siacp.mark = pItem->GetText();
						}
						else if (_tcsicmp(pItem->Value(), _T("MultiParams")) == 0)
						{
							siacp.bMulticode = pItem->BoolText();
						}
						else if (_tcsicmp(pItem->Value(), _T("ReadWaitTime")) == 0)
						{
							siacp.read_wait_time = pItem->IntText();
						}
						else if (_tcsicmp(pItem->Value(), _T("CmdWaitTime")) == 0)
						{
							siacp.cmd_wait_time = pItem->IntText();
						}

						pItem = pItem->NextSiblingElement();
					}
				}
				g_vtSiacp.push_back(siacp);
				pXmlElent = pXmlElent->NextSiblingElement();
			}
		}
	}
}

void CSIACP::SaveCommand(std::vector<_SIACP_> &vtSiacp, std::string path)
{
	tinyxml2::XMLDocument doc;
	//添加申明可以使用如下两行
	tinyxml2::XMLDeclaration *declaration = doc.NewDeclaration();
	doc.InsertFirstChild(declaration);

	tinyxml2::XMLElement *root = doc.NewElement("CommandList");
	root->SetAttribute("ver", "9.5");
	root->SetAttribute("desc", "command info");
	doc.InsertEndChild(root);

	std::vector<_SIACP_>::iterator it = vtSiacp.begin();
	for (; it != vtSiacp.end(); it++)
	{
		tinyxml2::XMLElement *pItem = doc.NewElement("Item");

		tinyxml2::XMLElement *pName = doc.NewElement("Name");
		pName->SetText(it->name.c_str());
		pItem->InsertEndChild(pName);

		tinyxml2::XMLElement *pHeadCode = doc.NewElement("HeadCode");
		pHeadCode->SetText(it->head.c_str());
		pItem->InsertEndChild(pHeadCode);

		tinyxml2::XMLElement *pucCommand = doc.NewElement("ucCommand");
		pucCommand->SetText(it->code.c_str());
		pItem->InsertEndChild(pucCommand);

		tinyxml2::XMLElement *pMark = doc.NewElement("Mark");
		pMark->SetText(it->mark.c_str());
		pItem->InsertEndChild(pMark);

		tinyxml2::XMLElement *pMultiParams = doc.NewElement("MultiParams");
		pMultiParams->SetText(it->bMulticode);
		pItem->InsertEndChild(pMultiParams);

		tinyxml2::XMLElement *pReadWaitTime = doc.NewElement("ReadWaitTime");
		pReadWaitTime->SetText(it->read_wait_time);
		pItem->InsertEndChild(pReadWaitTime);

		tinyxml2::XMLElement* pCmdWaitTime = doc.NewElement("CmdWaitTime");
		pCmdWaitTime->SetText(it->cmd_wait_time);
		pItem->InsertEndChild(pCmdWaitTime);

		root->InsertEndChild(pItem);
	}

	doc.SaveFile(path.c_str());
}

void CSIACP::SaveCommand(_SIACP_ (&Siacp)[CMD_SIZE], std::string path)
{
	tinyxml2::XMLDocument doc;
	//添加申明可以使用如下两行
	tinyxml2::XMLDeclaration *declaration = doc.NewDeclaration();
	doc.InsertFirstChild(declaration);

	tinyxml2::XMLElement *root = doc.NewElement("CommandList");
	root->SetAttribute("ver", "9.5");
	root->SetAttribute("desc", "command info");
	doc.InsertEndChild(root);

	for (int i = 0; i < CMD_SIZE; i++)
	{
		tinyxml2::XMLElement *pItem = doc.NewElement("Item");

		tinyxml2::XMLElement *pName = doc.NewElement("Name");
		pName->SetText(Siacp[i].name.c_str());
		pItem->InsertEndChild(pName);

		tinyxml2::XMLElement *pHeadCode = doc.NewElement("HeadCode");
		pHeadCode->SetText(Siacp[i].head.c_str());
		pItem->InsertEndChild(pHeadCode);

		tinyxml2::XMLElement *pucCommand = doc.NewElement("ucCommand");
		pucCommand->SetText(Siacp[i].code.c_str());
		pItem->InsertEndChild(pucCommand);

		tinyxml2::XMLElement *pMark = doc.NewElement("Mark");
		pMark->SetText(Siacp[i].mark.c_str());
		pItem->InsertEndChild(pMark);

		tinyxml2::XMLElement *pMultiParams = doc.NewElement("MultiParams");
		pMultiParams->SetText(Siacp[i].bMulticode);
		pItem->InsertEndChild(pMultiParams);

		tinyxml2::XMLElement *pReadWaitTime = doc.NewElement("ReadWaitTime");
		pReadWaitTime->SetText(Siacp[i].read_wait_time);
		pItem->InsertEndChild(pReadWaitTime);

		tinyxml2::XMLElement* pCmdWaitTime = doc.NewElement("CmdWaitTime");
		pCmdWaitTime->SetText(Siacp[i].cmd_wait_time);
		pItem->InsertEndChild(pCmdWaitTime);

		root->InsertEndChild(pItem);
	}

	doc.SaveFile(path.c_str());
}

BOOL CSIACP::OpenComm(int nSerialPort, DWORD dwBaudrate)
{
	if ( nSerialPort <= 0 )
	{
		////Global::WriteReportLog(1, "Parameter error", "(lpszCom) Invalid parameter!");
		return FALSE;
	}

	// 释放打开的;
	if (m_pSerial)
		delete m_pSerial;
	m_pSerial = NULL;

	m_pSerial = new CSynSerial();
	if (m_pSerial == NULL)
	{
		//Global::WriteReportLog(1, "Memory error", "(m_pSerial) Fail to create object!");
		return FALSE;
	}

	m_pSerial->OpenSerialPort(nSerialPort, dwBaudrate, 8, 0, 1, 0, 1000);
	if (m_pSerial->IsOpen() == FALSE)
	{
		delete m_pSerial;
		m_pSerial = NULL;
		//Global::WriteReportLog(1, "Serial port error", "Open Serial Port Fail !");
		return FALSE;
	}

	// 超时值设置;
	COMMTIMEOUTS comTimeOut;
	comTimeOut.ReadIntervalTimeout = 50;
	comTimeOut.ReadTotalTimeoutConstant = 8000;   // 读取所有字节总超时值;
	comTimeOut.ReadTotalTimeoutMultiplier = 100;  // 读取1个字节超时值;
	comTimeOut.WriteTotalTimeoutConstant = 5000;  // 写所有字节的总超时值;
	comTimeOut.WriteTotalTimeoutMultiplier = 100; // 写1个字节的超时值;
	m_pSerial->SetTimeouts(comTimeOut);

	return TRUE;
}

void CSIACP::CloseComm()
{
	if (m_pSerial)
		delete m_pSerial;
	m_pSerial = NULL;
}

BOOL CSIACP::ExecSerialCommand(const _SIACP_ &siacp, std::string command)
{
	if (m_pSerial == NULL || !m_pSerial->IsOpen())
	{
		//Global::WriteReportLog(1, "Serial port error", "Serial Port un't Open!");
		return FALSE;
	}

	byte szRecive[BUFFER_LEN] = {0};
	DWORD dwWritten = m_pSerial->WriteComm((BYTE*)command.c_str(), command.size());
	//Global::WriteTextLog(_T("COM Write:%s"), Bytes2HexString((const byte *)command.c_str(), command.size(), ' ').c_str());
	if (dwWritten != command.size())
	{
		//Global::WriteTextLog("COM Written Error");
		//Global::WriteReportLog(1, "Serial Writing Error", "COM Written Error, Buffer=%s", Bytes2HexString((const byte*)command.c_str(), command.size(), ' ').c_str());
		return FALSE;
	}

	// 暂停时间;// 等待设备响应完成;
	Sleep(siacp.read_wait_time);
	DWORD dwBytesRead = m_pSerial->ReadComm(szRecive, BUFFER_LEN);
	if (dwBytesRead == 0)
	{
		//Global::WriteTextLog("COM Read Error");
		//Global::WriteReportLog(1, "Serial Reading Error","COM Read Error(No Data Return), Write=%s!", Bytes2HexString((const byte*)command.c_str(), command.size(), ' ').c_str());
		return FALSE;
	}

	//Global::WriteTextLog(_T("COM Read:%s\r"), Bytes2HexString(szRecive, dwBytesRead, ' ').c_str());

	// 下一条命令等待时间;
	Sleep(siacp.cmd_wait_time);
	// 解析数据;
	return ParserSerialData(m_vtdata, siacp, szRecive, dwBytesRead);
}

BOOL CSIACP::ParserSerialData(std::vector<RTN> &vtdata, const _SIACP_ &siacp, byte *pbuffer, DWORD dwlen)
{
	vtdata.clear();
	if (pbuffer == NULL || dwlen < 5)
	{
		//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Return length less than 5! Buffer = %s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
		return FALSE;
	}

	// 返回码;
	byte byReturn = TV_Return;
	byte byHead = (byte)HexString2Bytes(siacp.head)[0];
	byte byCode = (byte)HexString2Bytes(siacp.code)[0];

	if (byHead == TV_Debug)
	{
		byReturn = TV_Return;
	}
	else if (byHead == TV_Panel_Debug)
	{
		byReturn = TV_Panel_Return;
	}
	else if (byHead == TV_Debug_Other)
	{
		byReturn = TV_Other_Return;
	}
	else
	{
		//Global::WriteTextLog("COM Read Parser: Invalid return code!");
		//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Invalid return code! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
		return FALSE;
	}

	std::string data;
	data.append((const char *)pbuffer, dwlen);

	int nABFE = 0;
	std::string package;
	int package_len = 0;
	// 已取出的长度;
	int nTookenLen = 0;
	unsigned short usCRCValue;
	bool mark = true;
	while (true)
	{
begin:
		if (mark)
		{
			while ((byte)data.at(0) != byReturn)
			{
				data.erase(0, 1);
				dwlen--;
				if (dwlen < 5)
				{
					//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Return data error! Buffer = %s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
					return FALSE;
				}
			}
			while ((byte)data.at(1) != 0x05)
			{
				data.erase(0, 2);
				dwlen = dwlen - 2;
				if (dwlen < 5)
				{
					//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Return data error! Buffer = %s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
					return FALSE;
				}
				goto begin;
			}
			while ((byte)data.at(2) != 0x0A)
			{
				data.erase(0, 3);
				dwlen = dwlen - 3;
				if (dwlen < 5)
				{
					//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Return data error! Buffer = %s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
					return FALSE;
				}
				goto begin;
			}
			while ((byte)data.at(3) != 0xDF)
			{
				data.erase(0, 4);
				dwlen = dwlen - 4;
				if (dwlen < 5)
				{
					//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Return data error! Buffer = %s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
					return FALSE;
				}
				goto begin;
			}
			while ((byte)data.at(4) != 0x4E)
			{
				data.erase(0, 5);
				dwlen = dwlen - 5;
				if (dwlen < 5)
				{
					//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Return data error! Buffer = %s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
					return FALSE;
				}
				goto begin;
			}
		}
		mark = false;
		// 是否已取完所有数据;
		if (nTookenLen >= dwlen)
			break;

		// 取出长度;
		if ((byte)data.at(nTookenLen + 1) == 0xFE)
		{
			nABFE = 2; //FE占1字节,长度多占1字节;共多占2字节;
			package_len = ((unsigned int)data[nTookenLen + 2] << 8) | (byte)data[nTookenLen + 3];
		}
		else
		{
			nABFE = 0;
			package_len = data[nTookenLen + 1];
		}

		// 包长度是否在有效长度内;
		if (package_len > (dwlen - nTookenLen))
		{
			//Global::WriteTextLog("COM Read Parser: Incorrect length of returned data!");
			//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Incorrect length of returned data! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
			return FALSE;
		}

		// 取出数据包;
		package = data.substr(nTookenLen, nTookenLen + package_len);

		// 校验包头是否正确;
		if (byte(package[0]) != byReturn)
		{
			//Global::WriteTextLog("COM Read Parser: Data Header Check Error!");
			//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Data Header Check Error! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
			return FALSE;
		}

		// 校验crc;
		usCRCValue = CRC16Calculate((unsigned char *)package.data(), package_len - 2);
		if (((usCRCValue >> 8) & 0xFF) != (unsigned char)package[package_len - 2] || (usCRCValue & 0xFF) != (unsigned char)package[package_len - 1])
		{
			//Global::WriteTextLog("COM Read Parser: Data CRC validation failure!");
			//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,Data CRC validation failure! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
			return FALSE;
		}

		// 第一组返回，检测结果码
		if (nTookenLen == 0)
		{
			if (package[2] != RC_OK)
			{
				//Global::WriteTextLog("COM Read Parser: The return code of the response data is invalid!");
				//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,The return code of the response data is invalid! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
				return FALSE;
			}
		}
		else
		{
			// 响应数据的命令码 = 发送数据的命令码 - 1;
			if (byte(package[2 + nABFE] - 1) != byCode)
			{
				//Global::WriteTextLog("COM Read Parser: The command code of response data does not match the command code of sending data!");
				//Global::WriteReportLog(1, "Serial Parsing error", "cmd=%s,The command code of response data does not match the command code of sending data! Buffer=%s", siacp.name.c_str(), Bytes2HexString(pbuffer, dwlen, ' ').c_str());
				return FALSE;
			}

			RTN rtn;
			// 获取结果值;
			if (siacp.bMulticode)
			{
				rtn.len = package_len - 6 - nABFE;
				rtn.data = package.substr(4 + nABFE, package_len - 6 - nABFE);
			}
			else
			{
				rtn.len = package_len - 5 - nABFE;
				rtn.data = package.substr(3 + nABFE, package_len - 5 - nABFE);
			}
			vtdata.push_back(rtn);
			break;//最多解析2段
		}

		nTookenLen += package_len;
	}

	return TRUE;
}

const _SIACP_ *CSIACP::GetSiacp(std::string name)
{
	std::vector<_SIACP_>::iterator it = g_vtSiacp.begin();
	for (; it != g_vtSiacp.end(); it++)
	{
		if (_tcsicmp(it->name.c_str(), name.c_str()) == 0)
		{
			return &*it;
		}
	}

	return NULL;
}

std::string CSIACP::GetSiacpCommand(const _SIACP_ &siacp, std::string others, int othersLen)
{
	std::string command;
	command.append(HexString2Bytes(siacp.head).c_str(), (siacp.head.size() + 1) / 3);
	command.append(HexString2Bytes(siacp.code).c_str(), (siacp.code.size() + 1) / 3);
	command.append(HexString2Bytes(siacp.mark).c_str(), (siacp.mark.size() + 1) / 3);
	command.append(others.c_str(), othersLen);

	// 长度;
	byte szlen[2] = {0};
	int len = command.size() + 3; // 3=长度本身占1位+2位crc;
	if ((byte)command[1] == 0xFE)
	{
		szlen[0] = ((len + 1) >> 8) & 0xFF;
		szlen[1] = (len + 1) & 0xFF;
		command.insert(2, (char *)szlen, 2);
	}
	else
	{
		szlen[0] = len & 0xFF;
		command.insert(1, (char *)szlen, 1);
	}

	// crc校验;
	byte szcrc[2] = {0};
	WORD usCRCValue = CRC16Calculate((byte *)command.c_str(), command.size());
	szcrc[0] = (usCRCValue >> 8) & 0xFF;
	szcrc[1] = usCRCValue & 0xFF;
	command.append((char *)szcrc, 2);

	return command;
}

std::string CSIACP::GetSiacpCommand(std::string name, std::string others, int othersLen)
{
	std::string command;
	std::vector<_SIACP_>::iterator it = g_vtSiacp.begin();
	for (; it != g_vtSiacp.end(); it++)
	{
		if (_tcsicmp(name.c_str(), it->name.c_str()) == 0)
		{
			command.append(HexString2Bytes(it->head).c_str(), (it->head.size() + 1) / 3);
			command.append(HexString2Bytes(it->code).c_str(), (it->code.size() + 1) / 3);
			command.append(HexString2Bytes(it->mark).c_str(), (it->mark.size() + 1) / 3);
			command.append(others.c_str(), othersLen);

			// 长度;
			byte szlen[2] = {0};
			int len = command.size() + 3; // 长度本身占1位;
			if ((byte)command[1] == 0xFE)
			{
				szlen[0] = ((len + 1) >> 8) & 0xFF;
				szlen[1] = (len + 1) & 0xFF;
				command.insert(2, (char *)szlen, 2);
			}
			else
			{
				szlen[0] = len & 0xFF;
				command.insert(1, (char *)szlen, 1);
			}

			// crc校验;
			byte szcrc[2] = {0};
			int usCRCValue = CRC16Calculate((byte *)command.c_str(), command.size());
			szcrc[0] = (usCRCValue >> 8) & 0xFF;
			szcrc[1] = usCRCValue & 0xFF;
			command.append((char *)szcrc, 2);

			break;
		}
	}

	return command;
}

BOOL CSIACP::SendCommand(std::string cmd_name, const byte *pszOthers, DWORD dwOthersLen)
{
	std::string others;
	if (pszOthers && dwOthersLen > 0)
	{
		others.append((char *)pszOthers, dwOthersLen);
	}

	// 获取命令协议;
	const _SIACP_ *pSiacp = GetSiacp(cmd_name);
	if (pSiacp == NULL)
	{
		//Global::WriteTextLog("This command is not supported!");
		//Global::WriteReportLog(1, "Invalid command", "This command（%s） is not supported!", cmd_name.c_str());
		return FALSE;
	}

	std::string command = GetSiacpCommand(*pSiacp, others, dwOthersLen);
	if (command.size() == 0)
	{
		//Global::WriteTextLog("Generation command failed!");
		//Global::WriteReportLog(1, "Generation command failed", "command=%s", cmd_name.c_str());
		return FALSE;
	}

	return ExecSerialCommand(*pSiacp, command);
}

BOOL CSIACP::SendCommand2(std::string cmd_name, std::string data /* = "" */, size_t nlen /* = 0 */)
{
	// 获取命令协议;
	const _SIACP_ *pSiacp = GetSiacp(cmd_name);
	if (pSiacp == NULL)
	{
		//Global::WriteTextLog("This command is not supported!");
		//Global::WriteReportLog(1, "Invalid command", "This command（%s） is not supported!", cmd_name.c_str());
		return FALSE;
	}

	std::string command = GetSiacpCommand(*pSiacp, data, nlen);
	if (command.size() == 0)
	{
		//Global::WriteTextLog("Generation command failed!");
		//Global::WriteReportLog(1, "Generation command failed", "command=%s", cmd_name.c_str());
		return FALSE;
	}

	return ExecSerialCommand(*pSiacp, command);
}

BOOL CSIACP::SCBC_WaitTVBoot()
{
	byte szRecive[BUFFER_LEN] = { 0 };
	DWORD dwBytesRead = 0;
	// 根据配置文件，是否需要读等待;
	if ( true )
		dwBytesRead = m_pSerial->ReadComm(szRecive, BUFFER_LEN);
	if (dwBytesRead == 0)
	{
		if ( SCBC_EnterFactory() )
		{
			SCBC_LeaveFactory();
			return TRUE;
		}		
	}

	return FALSE;
}

BOOL CSIACP::SCBC_MTKInit()
{
	if (m_pSerial == NULL)
	{
		//Global::WriteTextLog("串口对象未初始化");
		//Global::WriteReportLog(1, "MTKInit Error", "Serial port object uninitialized!");
		return FALSE;
	}

	if (!m_pSerial->IsOpen())
	{
		//Global::WriteTextLog("未打开串口");
		//Global::WriteReportLog(1, "MTKInit Error", "Unopened Serial Port!");
		return FALSE;
	}

	unsigned char ucBufInitMTK8223[16] = {0x54, 0x8A, 0x51, 0x37, 0x56, 0x92, 0x1C, 0xB2, 0x50, 0x80, 0x50, 0x02, 0x50, 0x01, 0x50, 0xE2};
	m_pSerial->WriteComm(ucBufInitMTK8223, 16);
	Sleep(50);

	return TRUE;
}

// 对外接口;
BOOL CSIACP::SCBC_EnterFactory()
{
	return SendCommand("EnterFactory");
}

BOOL CSIACP::SCBC_LeaveFactory()
{
	return SendCommand("LeaveFactory");
}

BOOL CSIACP::SCBC_WBInit()
{
	return SendCommand("WBInit");
}

BOOL CSIACP::SCBC_GetProjectId(int &pid)
{
	BOOL bRet = SendCommand("GetProjectId");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.len == 2)
		{
			pid = (byte)rtn.data[0] << 8;
			pid += (byte)rtn.data[1];
		}
		else
		{
			pid = (byte)rtn.data[0];
		}
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetSoftVersion(std::string &strVer)
{
	BOOL bRet = SendCommand("GetSoftVersion");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (!IsValidString(rtn.data.c_str()))
		{
			//Global::WriteTextLog("%s:%s%s!", __FUNCTION__, "tv version string is invalid=", rtn.data.c_str());
			//Global::WriteReportLog(1, "Copy Key", "%s:%s%s!", __FUNCTION__, "tv version string is invalid=", rtn.data.c_str());

			return FALSE;
		}

		strVer.append(rtn.data.c_str(), rtn.len);
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetDeviceId(std::string &strDid)
{
	BOOL bRet = SendCommand("GetDeviceId");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		strDid.append(rtn.data.c_str(), rtn.len);
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetClientType(std::string &strClt)
{
	BOOL bRet = SendCommand("GetClientType");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (!IsValidString(rtn.data.c_str()))
		{
			//Global::WriteTextLog("%s:%s%s!", __FUNCTION__, "tv client string is invalid=", rtn.data.c_str());
			//Global::WriteReportLog(1, "Copy Key", "%s:%s%s!", __FUNCTION__, "tv client string is invalid=", rtn.data.c_str());

			return FALSE;
		}
		strClt.append(rtn.data.c_str(), rtn.len);
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetMAC(std::string &strMac)
{
	BOOL bRet = SendCommand("GetMAC");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		strMac = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len, '-');
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetHDCPKey(std::string &strHDCP)
{
	BOOL bRet = SendCommand("GetHDCPKey");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		//strHDCP.append(rtn.data.c_str(), rtn.len);
		strHDCP = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetHDCPKey22(std::string &strHDCP22)
{
	BOOL bRet = SendCommand("GetHDCPKey22");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		//strHDCP22.append(rtn.data.c_str(), rtn.len);
		strHDCP22 = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetWidi(std::string &strWidi)
{
	BOOL bRet = SendCommand("GetWidi");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		//strWidi.append(rtn.data.c_str(), rtn.len);
		strWidi = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetNetflixESN(std::string &strESN)
{
	BOOL bRet = SendCommand("GetNetflixESN");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		strESN.append(rtn.data.c_str(), rtn.len);
		//strESN = Bytes2HexString((const byte*)rtn.data.c_str(), rtn.len, ' ');
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetWidevine(std::string &strWidevine)
{
	BOOL bRet = SendCommand("GetWidevine");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		//strWidevine.append(rtn.data.c_str(), rtn.len);
		strWidevine = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetCiKey(std::string &strCikey)
{
	BOOL bRet = SendCommand("GetCiKey");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		//strCikey.append(rtn.data.c_str(), rtn.len);
		strCikey = Bytes2HexString((const byte *)rtn.data.c_str(), rtn.len);
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetOSDLanguage(std::string &strOSDLan)
{
	BOOL bRet = SendCommand("GetOSDLanguage");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		strOSDLan.append(rtn.data.c_str(), rtn.len);
		strOSDLan = Bytes2HexString((const byte*)rtn.data.c_str(), rtn.len, ' ');
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetShopLanguage(std::string &strShopLan)
{
	BOOL bRet = SendCommand("GetShopLanguage");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		strShopLan.append(rtn.data.c_str(), rtn.len);
		strShopLan = Bytes2HexString((const byte*)rtn.data.c_str(), rtn.len, ' ');
	}

	return bRet;
}

BOOL CSIACP::SCBC_GetChannel(std::string& channel)
{
	BOOL bRet = SendCommand("GetChannel");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		channel.append(rtn.data.c_str(), rtn.len);
		channel = Bytes2HexString((const byte*)rtn.data.c_str(), rtn.len, ' ');
	}

	return bRet;
}

BOOL CSIACP::SCBC_SetProjectId(const int &pid)
{
	std::string data;
	if (pid > 0xFF)
	{
		data.append(1, (pid >> 8) & 0xFF);
		data.append(1, pid & 0xFF);
	}
	else
	{
		data.append(1, pid & 0xFF);
	}

	return SendCommand2("SetProjectId", data, data.size());
}

BOOL CSIACP::SCBC_SetProjectId(std::string pid)
{
	return SCBC_SetProjectId(atoi(pid.c_str()));
}

BOOL CSIACP::SCBC_SetProjectId(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetProjectId", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetDeviceId(std::string strDeviceId)
{
	if (strDeviceId.size() < 40)
	{
		return FALSE;
	}

	return SendCommand2("SetDeviceId", strDeviceId, strDeviceId.size());
}

BOOL CSIACP::SCBC_SetDeviceId(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetDeviceId", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetMAC(std::string strMac)
{
	if (strMac.size() < 16)
		return FALSE;

	std::string data = HexString2Bytes(strMac);
	return SendCommand2("SetMAC", data, (strMac.size() + 1) / 3);
}

BOOL CSIACP::SCBC_SetMAC(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetMAC", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetHDCPKey(std::string strHDCP, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(strHDCP, bHasSpace ? 3 : 2);
	return SendCommand2("SetHDCPKey", data, (strHDCP.size() + 1) / (bHasSpace ? 3 : 2));
}

BOOL CSIACP::SCBC_SetHDCPKey(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetHDCPKey", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetHDCPKey22(std::string strHDCP22, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(strHDCP22, bHasSpace ? 3 : 2);
	return SendCommand2("SetHDCPKey22", data, (strHDCP22.size() + 1) / (bHasSpace ? 3 : 2));
}

BOOL CSIACP::SCBC_SetHDCPKey22(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetHDCPKey22", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetNetflixESN(std::string strESN, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(strESN, bHasSpace ? 3 : 2);
	return SendCommand2("SetNetflixESN", data, (strESN.size() + 1) / (bHasSpace ? 3 : 2));
}

BOOL CSIACP::SCBC_SetNetflixESN(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetNetflixESN", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetWidi(std::string strWidi, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(strWidi, bHasSpace ? 3 : 2);
	return SendCommand2("SetWidi", data, (strWidi.size() + 1) / (bHasSpace ? 3 : 2));
}

BOOL CSIACP::SCBC_SetWidi(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetWidi", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetWidevine(std::string strWidevine, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(strWidevine, bHasSpace ? 3 : 2);
	return SendCommand2("SetWidevine", data, (strWidevine.size() + 1) / (bHasSpace ? 3 : 2));
}

BOOL CSIACP::SCBC_SetWidevine(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetWidevine", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetCiKey(std::string strCiKey, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(strCiKey, bHasSpace ? 3 : 2);
	return SendCommand2("SetCiKey", data, (strCiKey.size() + 1) / (bHasSpace ? 3 : 2));
}

BOOL CSIACP::SCBC_SetCiKey(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetCiKey", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetOSDLanguage(std::string lan, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(lan, bHasSpace ? 3 : 2);
	return SendCommand2("SetOSDLanguage", data, data.size());
}

BOOL CSIACP::SCBC_SetOSDLanguage(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetOSDLanguage", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetShopLanguage(std::string lan, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(lan, bHasSpace ? 3 : 2);
	return SendCommand2("SetShopLanguage", data, data.size());
}

BOOL CSIACP::SCBC_SetShopLanguage(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetShopLanguage", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetChannel(std::string channel, BOOL bHasSpace /* = TRUE */)
{
	std::string data = HexString2Bytes(channel, bHasSpace ? 3 : 2);
	return SendCommand2("SetChannel", data, data.size());
}

BOOL CSIACP::SCBC_SetChannel(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetChannel", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetChannel(int channel)
{
	std::string data;
	data.append(1, channel & 0xFF);
	return SendCommand2("SetChannel", data, data.size());
}

BOOL CSIACP::SCBC_SetWBNormal(std::string data)
{
	return SendCommand2("SetWBNormal", data, data.size());
}

BOOL CSIACP::SCBC_SetWBNormal(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetWBNormal", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetWBCool(std::string data)
{
	return SendCommand2("SetWBCool", data, data.size());
}

BOOL CSIACP::SCBC_SetWBCool(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetWBCool", pBuffer, nLen);
}

BOOL CSIACP::SCBC_SetWBWarm(std::string data)
{
	return SendCommand2("SetWBWarm", data, data.size());
}

BOOL CSIACP::SCBC_SetWBWarm(const byte *pBuffer, const int &nLen)
{
	return SendCommand("SetWBWarm", pBuffer, nLen);
}

BOOL CSIACP::SCBC_CheckDeviceId()
{
	BOOL bRet = SendCommand("CheckDeviceId");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bydata = (byte)rtn.data.at(0);
#if TEMP_5659
		if (bydata == 0x03 || bydata == 0x02 || bydata == 0x01)
			return TRUE;
		else
			return FALSE;
#else
		if (bydata == 0x02 || bydata == 0x03 || bydata == 0x07)
			return TRUE;
		else
			return FALSE;
#endif
	}

	return bRet;
}

BOOL CSIACP::SCBC_CheckMAC()
{
	BOOL bRet = SendCommand("CheckMAC");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bydata = (byte)rtn.data.at(0);
		if (bydata == 0x01 || bydata == 0x03 || bydata == 0x07)
			return TRUE;
		else
			return FALSE;
	}

	return bRet;
}

BOOL CSIACP::SCBC_CheckHDCP()
{
	BOOL bRet = SendCommand("CheckHDCP");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bydata = (byte)rtn.data.at(0);
		if (bydata == 0x01)
			return TRUE;
		else
			return FALSE;
	}

	return bRet;
}

BOOL CSIACP::SCBC_CheckHDCP22()
{
	BOOL bRet = SendCommand("CheckHDCP22");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bydata = (byte)rtn.data.at(0);
		if (bydata == 0x01)
			return TRUE;
		else
			return FALSE;
	}

	return bRet;
}

BOOL CSIACP::SCBC_CheckNetflixESN()
{
	BOOL bRet = SendCommand("CheckNetflixESN");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bydata = (byte)rtn.data.at(0);
		if (bydata == 0x01)
			return TRUE;
		else
			return FALSE;
	}

	return bRet;
}

BOOL CSIACP::SCBC_CheckWidi()
{
	BOOL bRet = SendCommand("CheckWidi");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bydata = (byte)rtn.data.at(0);
		if (bydata == 0x01)
			return TRUE;
		else
			return FALSE;
	}

	return bRet;
}

BOOL CSIACP::SCBC_CheckWidevine()
{
	BOOL bRet = SendCommand("CheckWidevine");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bydata = (byte)rtn.data.at(0);
		if (bydata == 0x01)
			return TRUE;
		else
			return FALSE;
	}

	return bRet;
}

BOOL CSIACP::SCBC_CheckCikey()
{
	BOOL bRet = SendCommand("CheckCikey");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");

			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bydata = (byte)rtn.data.at(0);
		if (bydata == 0x01)
			return TRUE;
		else
			return FALSE;
	}

	return bRet;
}

BOOL CSIACP::SCBC_StarWarmUpMode()
{
	return SendCommand("StarWarmUpMode");
}

BOOL CSIACP::SCBC_StopWarmUpMode()
{
	return SendCommand("StopWarmUpMode");
}

BOOL CSIACP::SCBC_ShowFactoryMenu()
{
	return SendCommand("ShowFactoryMenu");
}

BOOL CSIACP::SCBC_HideFactoryMenu()
{
	return SendCommand("HideFactoryMenu");
}

BOOL CSIACP::SCBC_ShowFactoryInformation()
{
	return SendCommand("ShowFactoryInformation");
}

BOOL CSIACP::SCBC_HideFactoryInformation()
{
	return SendCommand("HideFactoryInformation");
}

BOOL CSIACP::SCBC_EnterAgingModel()
{
	return SendCommand("EnterAgingModel");
}

BOOL CSIACP::SCBC_LeaveAgingModel()
{
	return SendCommand("LeaveAgingModel");
}

BOOL CSIACP::SCBC_ReadAgingTime(int &min)
{
	BOOL bRet = SendCommand("ReadAgingTime");
	if (bRet)
	{
		if (m_vtdata.size() == 0)
		{
			//Global::WriteTextLog("%s:%s", __FUNCTION__, "No data return!");
			//Global::WriteReportLog(1, "Copy Key", "%s:%s", __FUNCTION__, "No data return!");
			return FALSE;
		}

		RTN rtn = m_vtdata.at(0);
		if (rtn.data.size() == 0)
		{
			return FALSE;
		}

		byte bymin = (byte)rtn.data.at(0);
		byte byhour = (byte)rtn.data.at(1);
		
		min = bymin + 60*byhour;
	}

	return bRet;
}

BOOL CSIACP::SCBC_SetRedGainRegister(int value)
{
	std::string data;
	if (value > 0xFF)
	{
		data.append(1, (value >> 8) & 0xFF);
		data.append(1, value & 0xFF);
	}
	else
	{
		data.append(1, value & 0xFF);
	}

	return SendCommand2("SetRedGainRegister", data, data.size());
}

BOOL CSIACP::SCBC_SetGreenGainRegister(int value)
{
	std::string data;
	if (value > 0xFF)
	{
		data.append(1, (value >> 8) & 0xFF);
		data.append(1, value & 0xFF);
	}
	else
	{
		data.append(1, value & 0xFF);
	}

	return SendCommand2("SetGreenGainRegister", data, data.size());
}

BOOL CSIACP::SCBC_SetBlueGainRegister(int value)
{
	std::string data;
	if (value > 0xFF)
	{
		data.append(1, (value >> 8) & 0xFF);
		data.append(1, value & 0xFF);
	}
	else
	{
		data.append(1, value & 0xFF);
	}

	return SendCommand2("SetBlueGainRegister", data, data.size());
}

BOOL CSIACP::SCBC_SetRedOffsetRegister(int value)
{
	std::string data;
	if (value > 0xFF)
	{
		data.append(1, (value >> 8) & 0xFF);
		data.append(1, value & 0xFF);
	}
	else
	{
		data.append(1, value & 0xFF);
	}

	return SendCommand2("SetRedOffsetRegister", data, data.size());
}

BOOL CSIACP::SCBC_SetGreenOffsetRegister(int value)
{
	std::string data;
	if (value > 0xFF)
	{
		data.append(1, (value >> 8) & 0xFF);
		data.append(1, value & 0xFF);
	}
	else
	{
		data.append(1, value & 0xFF);
	}

	return SendCommand2("SetGreenOffsetRegister", data, data.size());
}

BOOL CSIACP::SCBC_SetBlueOffsetRegister(int value)
{
	std::string data;
	if (value > 0xFF)
	{
		data.append(1, (value >> 8) & 0xFF);
		data.append(1, value & 0xFF);
	}
	else
	{
		data.append(1, value & 0xFF);
	}

	return SendCommand2("SetBlueOffsetRegister", data, data.size());
}